1 /*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef skgpu_graphite_DrawTypes_DEFINED
9 #define skgpu_graphite_DrawTypes_DEFINED
10
11 #include "include/gpu/graphite/GraphiteTypes.h"
12
13 #include "src/gpu/graphite/ResourceTypes.h"
14
15 #include <array>
16
17 namespace skgpu::graphite {
18
19 class Buffer;
20
21 /**
22 * Geometric primitives used for drawing.
23 */
24 enum class PrimitiveType : uint8_t {
25 kTriangles,
26 kTriangleStrip,
27 kPoints,
28 };
29
30 /**
31 * Types used to describe format of vertices in buffers.
32 */
33 enum class VertexAttribType : uint8_t {
34 kFloat = 0,
35 kFloat2,
36 kFloat3,
37 kFloat4,
38 kHalf,
39 kHalf2,
40 kHalf4,
41
42 kInt2, // vector of 2 32-bit ints
43 kInt3, // vector of 3 32-bit ints
44 kInt4, // vector of 4 32-bit ints
45
46 kByte, // signed byte
47 kByte2, // vector of 2 8-bit signed bytes
48 kByte4, // vector of 4 8-bit signed bytes
49 kUByte, // unsigned byte
50 kUByte2, // vector of 2 8-bit unsigned bytes
51 kUByte4, // vector of 4 8-bit unsigned bytes
52
53 kUByte_norm, // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f.
54 kUByte4_norm, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f, 255 -> 1.0f.
55
56 kShort2, // vector of 2 16-bit shorts.
57 kShort4, // vector of 4 16-bit shorts.
58
59 kUShort2, // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535.
60 kUShort2_norm, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
61
62 kInt,
63 kUInt,
64
65 kUShort_norm, // unsigned short, e.g. depth, 0 -> 0.0f, 65535 -> 1.0f.
66
67 kUShort4_norm, // vector of 4 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
68
69 kLast = kUShort4_norm
70 };
71 static const int kVertexAttribTypeCount = (int)(VertexAttribType::kLast) + 1;
72
73
74 /**
75 * Returns the size of the attrib type in bytes.
76 */
VertexAttribTypeSize(VertexAttribType type)77 static constexpr inline size_t VertexAttribTypeSize(VertexAttribType type) {
78 switch (type) {
79 case VertexAttribType::kFloat:
80 return sizeof(float);
81 case VertexAttribType::kFloat2:
82 return 2 * sizeof(float);
83 case VertexAttribType::kFloat3:
84 return 3 * sizeof(float);
85 case VertexAttribType::kFloat4:
86 return 4 * sizeof(float);
87 case VertexAttribType::kHalf:
88 return sizeof(uint16_t);
89 case VertexAttribType::kHalf2:
90 return 2 * sizeof(uint16_t);
91 case VertexAttribType::kHalf4:
92 return 4 * sizeof(uint16_t);
93 case VertexAttribType::kInt2:
94 return 2 * sizeof(int32_t);
95 case VertexAttribType::kInt3:
96 return 3 * sizeof(int32_t);
97 case VertexAttribType::kInt4:
98 return 4 * sizeof(int32_t);
99 case VertexAttribType::kByte:
100 return 1 * sizeof(char);
101 case VertexAttribType::kByte2:
102 return 2 * sizeof(char);
103 case VertexAttribType::kByte4:
104 return 4 * sizeof(char);
105 case VertexAttribType::kUByte:
106 return 1 * sizeof(char);
107 case VertexAttribType::kUByte2:
108 return 2 * sizeof(char);
109 case VertexAttribType::kUByte4:
110 return 4 * sizeof(char);
111 case VertexAttribType::kUByte_norm:
112 return 1 * sizeof(char);
113 case VertexAttribType::kUByte4_norm:
114 return 4 * sizeof(char);
115 case VertexAttribType::kShort2:
116 return 2 * sizeof(int16_t);
117 case VertexAttribType::kShort4:
118 return 4 * sizeof(int16_t);
119 case VertexAttribType::kUShort2: [[fallthrough]];
120 case VertexAttribType::kUShort2_norm:
121 return 2 * sizeof(uint16_t);
122 case VertexAttribType::kInt:
123 return sizeof(int32_t);
124 case VertexAttribType::kUInt:
125 return sizeof(uint32_t);
126 case VertexAttribType::kUShort_norm:
127 return sizeof(uint16_t);
128 case VertexAttribType::kUShort4_norm:
129 return 4 * sizeof(uint16_t);
130 }
131 SkUNREACHABLE;
132 }
133
134 enum class UniformSlot {
135 // TODO: Want this?
136 // Meant for uniforms that change rarely to never over the course of a render pass
137 // kStatic,
138 // Meant for uniforms that are defined and used by the RenderStep portion of the pipeline shader
139 kRenderStep,
140 // Meant for uniforms that are defined and used by the paint parameters (ie SkPaint subset)
141 kPaint,
142 // Meant for gradient storage buffer.
143 kGradient
144 };
145
146 /*
147 * Depth and stencil settings
148 */
149 enum class CompareOp : uint8_t {
150 kAlways,
151 kNever,
152 kGreater,
153 kGEqual,
154 kLess,
155 kLEqual,
156 kEqual,
157 kNotEqual
158 };
159 static constexpr int kCompareOpCount = 1 + (int)CompareOp::kNotEqual;
160
161 enum class StencilOp : uint8_t {
162 kKeep,
163 kZero,
164 kReplace, // Replace stencil value with reference (only the bits enabled in fWriteMask).
165 kInvert,
166 kIncWrap,
167 kDecWrap,
168 // NOTE: clamping occurs before the write mask. So if the MSB is zero and masked out, stencil
169 // values will still wrap when using clamping ops.
170 kIncClamp,
171 kDecClamp
172 };
173 static constexpr int kStencilOpCount = 1 + (int)StencilOp::kDecClamp;
174
175 struct DepthStencilSettings {
176 // Per-face settings for stencil
177 struct Face {
178 constexpr Face() = default;
FaceDepthStencilSettings::Face179 constexpr Face(StencilOp stencilFail,
180 StencilOp depthFail,
181 StencilOp dsPass,
182 CompareOp compare,
183 uint32_t readMask,
184 uint32_t writeMask)
185 : fStencilFailOp(stencilFail)
186 , fDepthFailOp(depthFail)
187 , fDepthStencilPassOp(dsPass)
188 , fCompareOp(compare)
189 , fReadMask(readMask)
190 , fWriteMask(writeMask) {}
191
192 StencilOp fStencilFailOp = StencilOp::kKeep;
193 StencilOp fDepthFailOp = StencilOp::kKeep;
194 StencilOp fDepthStencilPassOp = StencilOp::kKeep;
195 CompareOp fCompareOp = CompareOp::kAlways;
196 uint32_t fReadMask = 0xffffffff;
197 uint32_t fWriteMask = 0xffffffff;
198
199 constexpr bool operator==(const Face& that) const {
200 return this->fStencilFailOp == that.fStencilFailOp &&
201 this->fDepthFailOp == that.fDepthFailOp &&
202 this->fDepthStencilPassOp == that.fDepthStencilPassOp &&
203 this->fCompareOp == that.fCompareOp &&
204 this->fReadMask == that.fReadMask &&
205 this->fWriteMask == that.fWriteMask;
206 }
207 };
208
209 constexpr DepthStencilSettings() = default;
DepthStencilSettingsDepthStencilSettings210 constexpr DepthStencilSettings(Face front,
211 Face back,
212 uint32_t stencilRef,
213 bool stencilTest,
214 CompareOp depthCompare,
215 bool depthTest,
216 bool depthWrite)
217 : fFrontStencil(front)
218 , fBackStencil(back)
219 , fStencilReferenceValue(stencilRef)
220 , fDepthCompareOp(depthCompare)
221 , fStencilTestEnabled(stencilTest)
222 , fDepthTestEnabled(depthTest)
223 , fDepthWriteEnabled(depthWrite) {}
224
225 constexpr bool operator==(const DepthStencilSettings& that) const {
226 return this->fFrontStencil == that.fFrontStencil &&
227 this->fBackStencil == that.fBackStencil &&
228 this->fStencilReferenceValue == that.fStencilReferenceValue &&
229 this->fDepthCompareOp == that.fDepthCompareOp &&
230 this->fStencilTestEnabled == that.fStencilTestEnabled &&
231 this->fDepthTestEnabled == that.fDepthTestEnabled &&
232 this->fDepthWriteEnabled == that.fDepthWriteEnabled;
233 }
234
235 Face fFrontStencil;
236 Face fBackStencil;
237 uint32_t fStencilReferenceValue = 0;
238 CompareOp fDepthCompareOp = CompareOp::kAlways;
239 bool fStencilTestEnabled = false;
240 bool fDepthTestEnabled = false;
241 bool fDepthWriteEnabled = false;
242 };
243
244 }; // namespace skgpu::graphite
245
246 #endif // skgpu_graphite_DrawTypes_DEFINED
247