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 kUInt2, // vector of 2 32-bit unsigned ints
46
47 kByte, // signed byte
48 kByte2, // vector of 2 8-bit signed bytes
49 kByte4, // vector of 4 8-bit signed bytes
50 kUByte, // unsigned byte
51 kUByte2, // vector of 2 8-bit unsigned bytes
52 kUByte4, // vector of 4 8-bit unsigned bytes
53
54 kUByte_norm, // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f.
55 kUByte4_norm, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f, 255 -> 1.0f.
56
57 kShort2, // vector of 2 16-bit shorts.
58 kShort4, // vector of 4 16-bit shorts.
59
60 kUShort2, // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535.
61 kUShort2_norm, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
62
63 kInt,
64 kUInt,
65
66 kUShort_norm, // unsigned short, e.g. depth, 0 -> 0.0f, 65535 -> 1.0f.
67
68 kUShort4_norm, // vector of 4 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
69
70 kLast = kUShort4_norm
71 };
72 static const int kVertexAttribTypeCount = (int)(VertexAttribType::kLast) + 1;
73
74
75 /**
76 * Returns the size of the attrib type in bytes.
77 */
VertexAttribTypeSize(VertexAttribType type)78 static constexpr inline size_t VertexAttribTypeSize(VertexAttribType type) {
79 switch (type) {
80 case VertexAttribType::kFloat:
81 return sizeof(float);
82 case VertexAttribType::kFloat2:
83 return 2 * sizeof(float);
84 case VertexAttribType::kFloat3:
85 return 3 * sizeof(float);
86 case VertexAttribType::kFloat4:
87 return 4 * sizeof(float);
88 case VertexAttribType::kHalf:
89 return sizeof(uint16_t);
90 case VertexAttribType::kHalf2:
91 return 2 * sizeof(uint16_t);
92 case VertexAttribType::kHalf4:
93 return 4 * sizeof(uint16_t);
94 case VertexAttribType::kInt2:
95 return 2 * sizeof(int32_t);
96 case VertexAttribType::kInt3:
97 return 3 * sizeof(int32_t);
98 case VertexAttribType::kInt4:
99 return 4 * sizeof(int32_t);
100 case VertexAttribType::kUInt2:
101 return 2 * sizeof(uint32_t);
102 case VertexAttribType::kByte:
103 return 1 * sizeof(char);
104 case VertexAttribType::kByte2:
105 return 2 * sizeof(char);
106 case VertexAttribType::kByte4:
107 return 4 * sizeof(char);
108 case VertexAttribType::kUByte:
109 return 1 * sizeof(char);
110 case VertexAttribType::kUByte2:
111 return 2 * sizeof(char);
112 case VertexAttribType::kUByte4:
113 return 4 * sizeof(char);
114 case VertexAttribType::kUByte_norm:
115 return 1 * sizeof(char);
116 case VertexAttribType::kUByte4_norm:
117 return 4 * sizeof(char);
118 case VertexAttribType::kShort2:
119 return 2 * sizeof(int16_t);
120 case VertexAttribType::kShort4:
121 return 4 * sizeof(int16_t);
122 case VertexAttribType::kUShort2: [[fallthrough]];
123 case VertexAttribType::kUShort2_norm:
124 return 2 * sizeof(uint16_t);
125 case VertexAttribType::kInt:
126 return sizeof(int32_t);
127 case VertexAttribType::kUInt:
128 return sizeof(uint32_t);
129 case VertexAttribType::kUShort_norm:
130 return sizeof(uint16_t);
131 case VertexAttribType::kUShort4_norm:
132 return 4 * sizeof(uint16_t);
133 }
134 SkUNREACHABLE;
135 }
136
137 enum class UniformSlot {
138 // TODO: Want this?
139 // Meant for uniforms that change rarely to never over the course of a render pass
140 // kStatic,
141 // Meant for uniforms that are defined and used by the RenderStep portion of the pipeline shader
142 kRenderStep,
143 // Meant for uniforms that are defined and used by the paint parameters (ie SkPaint subset)
144 kPaint,
145 // Meant for gradient storage buffer.
146 kGradient
147 };
148
149 /*
150 * Depth and stencil settings
151 */
152 enum class CompareOp : uint8_t {
153 kAlways,
154 kNever,
155 kGreater,
156 kGEqual,
157 kLess,
158 kLEqual,
159 kEqual,
160 kNotEqual
161 };
162 static constexpr int kCompareOpCount = 1 + (int)CompareOp::kNotEqual;
163
164 enum class StencilOp : uint8_t {
165 kKeep,
166 kZero,
167 kReplace, // Replace stencil value with reference (only the bits enabled in fWriteMask).
168 kInvert,
169 kIncWrap,
170 kDecWrap,
171 // NOTE: clamping occurs before the write mask. So if the MSB is zero and masked out, stencil
172 // values will still wrap when using clamping ops.
173 kIncClamp,
174 kDecClamp
175 };
176 static constexpr int kStencilOpCount = 1 + (int)StencilOp::kDecClamp;
177
178 // These barrier types are not utilized by all backends, but we define them at this level anyhow
179 // since it impacts the logic used to group & sort draws.
180 enum class BarrierType : uint8_t {
181 kAdvancedNoncoherentBlend,
182 kReadDstFromInput,
183 };
184
185 struct DepthStencilSettings {
186 // Per-face settings for stencil
187 struct Face {
188 constexpr Face() = default;
FaceDepthStencilSettings::Face189 constexpr Face(StencilOp stencilFail,
190 StencilOp depthFail,
191 StencilOp dsPass,
192 CompareOp compare,
193 uint32_t readMask,
194 uint32_t writeMask)
195 : fStencilFailOp(stencilFail)
196 , fDepthFailOp(depthFail)
197 , fDepthStencilPassOp(dsPass)
198 , fCompareOp(compare)
199 , fReadMask(readMask)
200 , fWriteMask(writeMask) {}
201
202 StencilOp fStencilFailOp = StencilOp::kKeep;
203 StencilOp fDepthFailOp = StencilOp::kKeep;
204 StencilOp fDepthStencilPassOp = StencilOp::kKeep;
205 CompareOp fCompareOp = CompareOp::kAlways;
206 uint32_t fReadMask = 0xffffffff;
207 uint32_t fWriteMask = 0xffffffff;
208
209 constexpr bool operator==(const Face& that) const {
210 return this->fStencilFailOp == that.fStencilFailOp &&
211 this->fDepthFailOp == that.fDepthFailOp &&
212 this->fDepthStencilPassOp == that.fDepthStencilPassOp &&
213 this->fCompareOp == that.fCompareOp &&
214 this->fReadMask == that.fReadMask &&
215 this->fWriteMask == that.fWriteMask;
216 }
217 };
218
219 constexpr DepthStencilSettings() = default;
DepthStencilSettingsDepthStencilSettings220 constexpr DepthStencilSettings(Face front,
221 Face back,
222 uint32_t stencilRef,
223 bool stencilTest,
224 CompareOp depthCompare,
225 bool depthTest,
226 bool depthWrite)
227 : fFrontStencil(front)
228 , fBackStencil(back)
229 , fStencilReferenceValue(stencilRef)
230 , fDepthCompareOp(depthCompare)
231 , fStencilTestEnabled(stencilTest)
232 , fDepthTestEnabled(depthTest)
233 , fDepthWriteEnabled(depthWrite) {}
234
235 constexpr bool operator==(const DepthStencilSettings& that) const {
236 return this->fFrontStencil == that.fFrontStencil &&
237 this->fBackStencil == that.fBackStencil &&
238 this->fStencilReferenceValue == that.fStencilReferenceValue &&
239 this->fDepthCompareOp == that.fDepthCompareOp &&
240 this->fStencilTestEnabled == that.fStencilTestEnabled &&
241 this->fDepthTestEnabled == that.fDepthTestEnabled &&
242 this->fDepthWriteEnabled == that.fDepthWriteEnabled;
243 }
244
245 Face fFrontStencil;
246 Face fBackStencil;
247 uint32_t fStencilReferenceValue = 0;
248 CompareOp fDepthCompareOp = CompareOp::kAlways;
249 bool fStencilTestEnabled = false;
250 bool fDepthTestEnabled = false;
251 bool fDepthWriteEnabled = false;
252 };
253
254 }; // namespace skgpu::graphite
255
256 #endif // skgpu_graphite_DrawTypes_DEFINED
257