xref: /aosp_15_r20/external/skia/src/gpu/graphite/DrawTypes.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 struct DepthStencilSettings {
179     // Per-face settings for stencil
180     struct Face {
181         constexpr Face() = default;
FaceDepthStencilSettings::Face182         constexpr Face(StencilOp stencilFail,
183                        StencilOp depthFail,
184                        StencilOp dsPass,
185                        CompareOp compare,
186                        uint32_t readMask,
187                        uint32_t writeMask)
188                 : fStencilFailOp(stencilFail)
189                 , fDepthFailOp(depthFail)
190                 , fDepthStencilPassOp(dsPass)
191                 , fCompareOp(compare)
192                 , fReadMask(readMask)
193                 , fWriteMask(writeMask) {}
194 
195         StencilOp fStencilFailOp = StencilOp::kKeep;
196         StencilOp fDepthFailOp = StencilOp::kKeep;
197         StencilOp fDepthStencilPassOp = StencilOp::kKeep;
198         CompareOp fCompareOp = CompareOp::kAlways;
199         uint32_t fReadMask = 0xffffffff;
200         uint32_t fWriteMask = 0xffffffff;
201 
202         constexpr bool operator==(const Face& that) const {
203             return this->fStencilFailOp == that.fStencilFailOp &&
204                    this->fDepthFailOp == that.fDepthFailOp &&
205                    this->fDepthStencilPassOp == that.fDepthStencilPassOp &&
206                    this->fCompareOp == that.fCompareOp &&
207                    this->fReadMask == that.fReadMask &&
208                    this->fWriteMask == that.fWriteMask;
209         }
210     };
211 
212     constexpr DepthStencilSettings() = default;
DepthStencilSettingsDepthStencilSettings213     constexpr DepthStencilSettings(Face front,
214                                    Face back,
215                                    uint32_t stencilRef,
216                                    bool stencilTest,
217                                    CompareOp depthCompare,
218                                    bool depthTest,
219                                    bool depthWrite)
220             : fFrontStencil(front)
221             , fBackStencil(back)
222             , fStencilReferenceValue(stencilRef)
223             , fDepthCompareOp(depthCompare)
224             , fStencilTestEnabled(stencilTest)
225             , fDepthTestEnabled(depthTest)
226             , fDepthWriteEnabled(depthWrite) {}
227 
228     constexpr bool operator==(const DepthStencilSettings& that) const {
229         return this->fFrontStencil == that.fFrontStencil &&
230                this->fBackStencil == that.fBackStencil &&
231                this->fStencilReferenceValue == that.fStencilReferenceValue &&
232                this->fDepthCompareOp == that.fDepthCompareOp &&
233                this->fStencilTestEnabled == that.fStencilTestEnabled &&
234                this->fDepthTestEnabled == that.fDepthTestEnabled &&
235                this->fDepthWriteEnabled == that.fDepthWriteEnabled;
236     }
237 
238     Face fFrontStencil;
239     Face fBackStencil;
240     uint32_t fStencilReferenceValue = 0;
241     CompareOp fDepthCompareOp = CompareOp::kAlways;
242     bool fStencilTestEnabled = false;
243     bool fDepthTestEnabled = false;
244     bool fDepthWriteEnabled = false;
245 };
246 
247 };  // namespace skgpu::graphite
248 
249 #endif // skgpu_graphite_DrawTypes_DEFINED
250