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_DrawTypes_DEFINED
9 #define skgpu_DrawTypes_DEFINED
10
11 #include "experimental/graphite/include/GraphiteTypes.h"
12
13 namespace skgpu {
14
15 class Buffer;
16
17 enum class CType : unsigned {
18 // Any float/half, vector of floats/half, or matrices of floats/halfs are a tightly
19 // packed array of floats. Similarly, any bool/shorts/ints are a tightly packed array
20 // of int32_t.
21 kDefault,
22 // Can be used with kFloat3x3 or kHalf3x3
23 kSkMatrix,
24
25 kLast = kSkMatrix
26 };
27
28 /**
29 * This enum is used to specify the load operation to be used when a RenderPass begins execution
30 */
31 enum class LoadOp : uint8_t {
32 kLoad,
33 kClear,
34 kDiscard,
35
36 kLast = kDiscard
37 };
38 inline static constexpr int kLoadOpCount = (int)(LoadOp::kLast) + 1;
39
40 /**
41 * This enum is used to specify the store operation to be used when a RenderPass ends execution.
42 */
43 enum class StoreOp : uint8_t {
44 kStore,
45 kDiscard,
46
47 kLast = kDiscard
48 };
49 inline static constexpr int kStoreOpCount = (int)(StoreOp::kLast) + 1;
50
51 /**
52 * Geometric primitives used for drawing.
53 */
54 enum class PrimitiveType : uint8_t {
55 kTriangles,
56 kTriangleStrip,
57 kPoints,
58 };
59
60 /**
61 * Types used to describe format of vertices in buffers.
62 */
63 enum class VertexAttribType : uint8_t {
64 kFloat = 0,
65 kFloat2,
66 kFloat3,
67 kFloat4,
68 kHalf,
69 kHalf2,
70 kHalf4,
71
72 kInt2, // vector of 2 32-bit ints
73 kInt3, // vector of 3 32-bit ints
74 kInt4, // vector of 4 32-bit ints
75
76 kByte, // signed byte
77 kByte2, // vector of 2 8-bit signed bytes
78 kByte4, // vector of 4 8-bit signed bytes
79 kUByte, // unsigned byte
80 kUByte2, // vector of 2 8-bit unsigned bytes
81 kUByte4, // vector of 4 8-bit unsigned bytes
82
83 kUByte_norm, // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f.
84 kUByte4_norm, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f, 255 -> 1.0f.
85
86 kShort2, // vector of 2 16-bit shorts.
87 kShort4, // vector of 4 16-bit shorts.
88
89 kUShort2, // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535.
90 kUShort2_norm, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
91
92 kInt,
93 kUInt,
94
95 kUShort_norm,
96
97 kUShort4_norm, // vector of 4 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
98
99 kLast = kUShort4_norm
100 };
101 static const int kVertexAttribTypeCount = (int)(VertexAttribType::kLast) + 1;
102
103
104 /**
105 * Returns the size of the attrib type in bytes.
106 */
VertexAttribTypeSize(VertexAttribType type)107 static constexpr inline size_t VertexAttribTypeSize(VertexAttribType type) {
108 switch (type) {
109 case VertexAttribType::kFloat:
110 return sizeof(float);
111 case VertexAttribType::kFloat2:
112 return 2 * sizeof(float);
113 case VertexAttribType::kFloat3:
114 return 3 * sizeof(float);
115 case VertexAttribType::kFloat4:
116 return 4 * sizeof(float);
117 case VertexAttribType::kHalf:
118 return sizeof(uint16_t);
119 case VertexAttribType::kHalf2:
120 return 2 * sizeof(uint16_t);
121 case VertexAttribType::kHalf4:
122 return 4 * sizeof(uint16_t);
123 case VertexAttribType::kInt2:
124 return 2 * sizeof(int32_t);
125 case VertexAttribType::kInt3:
126 return 3 * sizeof(int32_t);
127 case VertexAttribType::kInt4:
128 return 4 * sizeof(int32_t);
129 case VertexAttribType::kByte:
130 return 1 * sizeof(char);
131 case VertexAttribType::kByte2:
132 return 2 * sizeof(char);
133 case VertexAttribType::kByte4:
134 return 4 * sizeof(char);
135 case VertexAttribType::kUByte:
136 return 1 * sizeof(char);
137 case VertexAttribType::kUByte2:
138 return 2 * sizeof(char);
139 case VertexAttribType::kUByte4:
140 return 4 * sizeof(char);
141 case VertexAttribType::kUByte_norm:
142 return 1 * sizeof(char);
143 case VertexAttribType::kUByte4_norm:
144 return 4 * sizeof(char);
145 case VertexAttribType::kShort2:
146 return 2 * sizeof(int16_t);
147 case VertexAttribType::kShort4:
148 return 4 * sizeof(int16_t);
149 case VertexAttribType::kUShort2: [[fallthrough]];
150 case VertexAttribType::kUShort2_norm:
151 return 2 * sizeof(uint16_t);
152 case VertexAttribType::kInt:
153 return sizeof(int32_t);
154 case VertexAttribType::kUInt:
155 return sizeof(uint32_t);
156 case VertexAttribType::kUShort_norm:
157 return sizeof(uint16_t);
158 case VertexAttribType::kUShort4_norm:
159 return 4 * sizeof(uint16_t);
160 }
161 }
162
163 /*
164 * Struct returned by the DrawBufferManager that can be passed into bind buffer calls on the
165 * CommandBuffer.
166 */
167 struct BindBufferInfo {
168 const Buffer* fBuffer = nullptr;
169 size_t fOffset = 0;
170
171 operator bool() const { return SkToBool(fBuffer); }
172
173 bool operator==(const BindBufferInfo& o) const {
174 return fBuffer == o.fBuffer && (!fBuffer || fOffset == o.fOffset);
175 }
176 bool operator!=(const BindBufferInfo& o) const {
177 return !(*this == o);
178 }
179 };
180
181 /*
182 * Depth and stencil settings
183 */
184 enum class CompareOp : uint8_t {
185 kAlways,
186 kNever,
187 kGreater,
188 kGEqual,
189 kLess,
190 kLEqual,
191 kEqual,
192 kNotEqual
193 };
194 static constexpr int kCompareOpCount = 1 + (int)CompareOp::kNotEqual;
195
196 enum class StencilOp : uint8_t {
197 kKeep,
198 kZero,
199 kReplace, // Replace stencil value with reference (only the bits enabled in fWriteMask).
200 kInvert,
201 kIncWrap,
202 kDecWrap,
203 // NOTE: clamping occurs before the write mask. So if the MSB is zero and masked out, stencil
204 // values will still wrap when using clamping ops.
205 kIncClamp,
206 kDecClamp
207 };
208 static constexpr int kStencilOpCount = 1 + (int)StencilOp::kDecClamp;
209
210 struct DepthStencilSettings {
211 // Per-face settings for stencil
212 struct Face {
213 constexpr Face() = default;
FaceDepthStencilSettings::Face214 constexpr Face(StencilOp stencilFail,
215 StencilOp depthFail,
216 StencilOp dsPass,
217 CompareOp compare,
218 uint32_t readMask,
219 uint32_t writeMask)
220 : fStencilFailOp(stencilFail)
221 , fDepthFailOp(depthFail)
222 , fDepthStencilPassOp(dsPass)
223 , fCompareOp(compare)
224 , fReadMask(readMask)
225 , fWriteMask(writeMask) {}
226
227 StencilOp fStencilFailOp = StencilOp::kKeep;
228 StencilOp fDepthFailOp = StencilOp::kKeep;
229 StencilOp fDepthStencilPassOp = StencilOp::kKeep;
230 CompareOp fCompareOp = CompareOp::kAlways;
231 uint32_t fReadMask = 0xffffffff;
232 uint32_t fWriteMask = 0xffffffff;
233
234 constexpr bool operator==(const Face& that) const {
235 return this->fStencilFailOp == that.fStencilFailOp &&
236 this->fDepthFailOp == that.fDepthFailOp &&
237 this->fDepthStencilPassOp == that.fDepthStencilPassOp &&
238 this->fCompareOp == that.fCompareOp &&
239 this->fReadMask == that.fReadMask &&
240 this->fWriteMask == that.fWriteMask;
241 }
242 };
243
244 constexpr DepthStencilSettings() = default;
DepthStencilSettingsDepthStencilSettings245 constexpr DepthStencilSettings(Face front,
246 Face back,
247 uint32_t stencilRef,
248 bool stencilTest,
249 CompareOp depthCompare,
250 bool depthTest,
251 bool depthWrite)
252 : fFrontStencil(front)
253 , fBackStencil(back)
254 , fStencilReferenceValue(stencilRef)
255 , fDepthCompareOp(depthCompare)
256 , fStencilTestEnabled(stencilTest)
257 , fDepthTestEnabled(depthTest)
258 , fDepthWriteEnabled(depthWrite) {}
259
260 constexpr bool operator==(const DepthStencilSettings& that) const {
261 return this->fFrontStencil == that.fFrontStencil &&
262 this->fBackStencil == that.fBackStencil &&
263 this->fStencilReferenceValue == that.fStencilReferenceValue &&
264 this->fDepthCompareOp == that.fDepthCompareOp &&
265 this->fStencilTestEnabled == that.fStencilTestEnabled &&
266 this->fDepthTestEnabled == that.fDepthTestEnabled &&
267 this->fDepthWriteEnabled == that.fDepthWriteEnabled;
268 }
269
270 Face fFrontStencil;
271 Face fBackStencil;
272 uint32_t fStencilReferenceValue = 0;
273 CompareOp fDepthCompareOp = CompareOp::kAlways;
274 bool fStencilTestEnabled = false;
275 bool fDepthTestEnabled = false;
276 bool fDepthWriteEnabled = false;
277 };
278
279 }; // namespace skgpu
280
281 #endif // skgpu_DrawTypes_DEFINED
282