Debian Codegraph @

src/gpu/graphite/DrawCommands.h

raw
1 /*
2 * Copyright 2022 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_DrawPassCommands_DEFINED
9 #define skgpu_graphite_DrawPassCommands_DEFINED
10
11 #include "include/core/SkRect.h"
12 #include "src/base/SkArenaAlloc.h"
13 #include "src/base/SkBlockAllocator.h"
14 #include "src/base/SkTBlockList.h"
15 #include "src/gpu/graphite/CommandTypes.h"
16 #include "src/gpu/graphite/ResourceTypes.h"
17
18 #include <array>
19 #include <cstdint>
20 #include <type_traits>
21 #include <utility>
22
23 namespace skgpu::graphite {
24
25 enum class BarrierType : uint8_t;
26 enum class PrimitiveType : uint8_t;
27 enum class UniformSlot;
28
29 class TextureProxy;
30
31 namespace DrawPassCommands {
32
33 struct AddBarrier;
34 struct BindAppendDataBuffer;
35 struct BindGraphicsPipeline;
36 struct BindIndexBuffer;
37 struct BindIndirectBuffer;
38 struct BindStaticDataBuffer;
39 struct BindTexturesAndSamplers;
40 struct BindUniformBuffer;
41 struct Draw;
42 struct DrawIndexed;
43 struct DrawIndexedIndirect;
44 struct DrawIndexedInstanced;
45 struct DrawIndirect;
46 struct DrawInstanced;
47 struct SetBlendConstants;
48 struct SetScissor;
49
50 // A list of all the commands types used by a DrawPass.
51 // Each of these is reified into a struct below.
52 //
53 // The design of this systems is based on SkRecords.
54
55 // (We're using the macro-of-macro trick here to do several different things with the same list.)
56 //
57 // We leave this SKGPU_DRAW_COMMAND_TYPES macro defined for use by code that wants to operate on
58 // DrawPassCommands types polymorphically.
59 #define SKGPU_DRAW_PASS_COMMAND_TYPES(M) \
60 M(BindGraphicsPipeline) \
61 M(SetBlendConstants) \
62 M(BindUniformBuffer) \
63 M(BindStaticDataBuffer) \
64 M(BindAppendDataBuffer) \
65 M(BindIndirectBuffer) \
66 M(BindIndexBuffer) \
67 M(BindTexturesAndSamplers) \
68 M(SetScissor) \
69 M(Draw) \
70 M(DrawIndexed) \
71 M(DrawInstanced) \
72 M(DrawIndexedInstanced) \
73 M(DrawIndirect) \
74 M(DrawIndexedIndirect) \
75 M(AddBarrier)
76
77 // Defines DrawPassCommands::Type, an enum of all draw command types.
78 #define ENUM(T) k##T,
79 enum class Type { SKGPU_DRAW_PASS_COMMAND_TYPES(ENUM) };
80 #undef ENUM
81
82 #define ACT_AS_PTR(ptr) \
83 operator T*() const { return ptr; } \
84 T* operator->() const { return ptr; }
85
86 // PODArray doesn't own the pointer's memory, and we assume the data is POD.
87 template <typename T>
88 class PODArray {
89 public:
90 PODArray() {}
91 PODArray(T* ptr) : fPtr(ptr) {}
92 // Default copy and assign.
93
94 ACT_AS_PTR(fPtr)
95 private:
96 T* fPtr;
97 };
98
99 #undef ACT_AS_PTR
100
101 // A macro to make it a little easier to define a struct that can be stored in DrawPass.
102 #define COMMAND(T, ...) \
103 struct T { \
104 static constexpr Type kType = Type::k##T; \
105 __VA_ARGS__; \
106 };
107
108 COMMAND(BindGraphicsPipeline,
109 uint32_t fPipelineIndex)
110 COMMAND(SetBlendConstants,
111 std::array<float, 4> fBlendConstants)
112 COMMAND(BindUniformBuffer,
113 BindBufferInfo fInfo;
114 UniformSlot fSlot)
115 COMMAND(BindStaticDataBuffer,
116 BindBufferInfo fStaticData)
117 COMMAND(BindAppendDataBuffer,
118 BindBufferInfo fAppendData)
119 COMMAND(BindIndexBuffer,
120 BindBufferInfo fIndices)
121 COMMAND(BindIndirectBuffer,
122 BindBufferInfo fIndirect)
123 COMMAND(BindTexturesAndSamplers,
124 int fNumTexSamplers;
125 PODArray<const TextureProxy*> fTextures;
126 PODArray<SamplerDesc> fSamplers)
127 COMMAND(SetScissor,
128 Scissor fScissor)
129 COMMAND(Draw,
130 PrimitiveType fType;
131 uint32_t fBaseVertex;
132 uint32_t fVertexCount)
133 COMMAND(DrawIndexed,
134 PrimitiveType fType;
135 uint32_t fBaseIndex;
136 uint32_t fIndexCount;
137 uint32_t fBaseVertex)
138 COMMAND(DrawInstanced,
139 PrimitiveType fType;
140 uint32_t fBaseVertex;
141 uint32_t fVertexCount;
142 uint32_t fBaseInstance;
143 uint32_t fInstanceCount)
144 COMMAND(DrawIndexedInstanced,
145 PrimitiveType fType;
146 uint32_t fBaseIndex;
147 uint32_t fIndexCount;
148 uint32_t fBaseVertex;
149 uint32_t fBaseInstance;
150 uint32_t fInstanceCount)
151 COMMAND(DrawIndirect,
152 PrimitiveType fType)
153 COMMAND(DrawIndexedIndirect,
154 PrimitiveType fType)
155 COMMAND(AddBarrier,
156 BarrierType fType)
157
158 #undef COMMAND
159
160 #define ASSERT_TRIV_DES(T) static_assert(std::is_trivially_destructible<T>::value);
161 SKGPU_DRAW_PASS_COMMAND_TYPES(ASSERT_TRIV_DES)
162 #undef ASSERT_TRIV_DES
163 #define ASSERT_TRIV_CPY(T) static_assert(std::is_trivially_copyable<T>::value);
164 SKGPU_DRAW_PASS_COMMAND_TYPES(ASSERT_TRIV_CPY)
165 #undef ASSERT_TRIV_CPY
166
167 class List {
168 public:
169 List() = default;
170 ~List() = default;
171
172 int count() const { return fCommands.count(); }
173
174 void bindGraphicsPipeline(uint32_t pipelineIndex) {
175 this->add<BindGraphicsPipeline>(pipelineIndex);
176 }
177
178 void setBlendConstants(std::array<float, 4> blendConstants) {
179 this->add<SetBlendConstants>(blendConstants);
180 }
181
182 void bindUniformBuffer(BindBufferInfo info, UniformSlot slot) {
183 this->add<BindUniformBuffer>(info, slot);
184 }
185
186 // Caller must write 'numTexSamplers' textures and sampler descriptions into the two returned
187 // arrays. The texture proxies must have refs held for the lifetime of this command list.
188 std::pair<const TextureProxy**, SamplerDesc*>
189 bindDeferredTexturesAndSamplers(int numTexSamplers) {
190 const TextureProxy** textures = fAlloc.makeArrayDefault<const TextureProxy*>(numTexSamplers);
191 SamplerDesc* samplers = fAlloc.makeArrayDefault<SamplerDesc>(numTexSamplers);
192 this->add<BindTexturesAndSamplers>(numTexSamplers, textures, samplers);
193 return {textures, samplers};
194 }
195
196 void setScissor(SkIRect scissor) {
197 this->add<SetScissor>(Scissor(scissor));
198 }
199
200 void bindStaticDataBuffer(BindBufferInfo staticAttribs) {
201 this->add<BindStaticDataBuffer>(staticAttribs);
202 }
203
204 void bindAppendDataBuffer(BindBufferInfo appendAttribs) {
205 this->add<BindAppendDataBuffer>(appendAttribs);
206 }
207
208 void bindIndexBuffer(BindBufferInfo indices) {
209 this->add<BindIndexBuffer>(indices);
210 }
211
212 void bindIndirectBuffer(BindBufferInfo indirect) {
213 this->add<BindIndirectBuffer>(indirect);
214 }
215
216 void draw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount) {
217 this->add<Draw>(type, baseVertex, vertexCount);
218 }
219
220 void drawIndexed(PrimitiveType type, unsigned int baseIndex,
221 unsigned int indexCount, unsigned int baseVertex) {
222 this->add<DrawIndexed>(type, baseIndex, indexCount, baseVertex);
223 }
224
225 void drawInstanced(PrimitiveType type,
226 unsigned int baseVertex, unsigned int vertexCount,
227 unsigned int baseInstance, unsigned int instanceCount) {
228 this->add<DrawInstanced>(type, baseVertex, vertexCount, baseInstance, instanceCount);
229 }
230
231 void drawIndexedInstanced(PrimitiveType type,
232 unsigned int baseIndex, unsigned int indexCount,
233 unsigned int baseVertex, unsigned int baseInstance,
234 unsigned int instanceCount) {
235 this->add<DrawIndexedInstanced>(type,
236 baseIndex,
237 indexCount,
238 baseVertex,
239 baseInstance,
240 instanceCount);
241 }
242
243 void drawIndirect(PrimitiveType type) {
244 this->add<DrawIndirect>(type);
245 }
246
247 void drawIndexedIndirect(PrimitiveType type) {
248 this->add<DrawIndexedIndirect>(type);
249 }
250
251 void addBarrier(BarrierType type) {
252 this->add<AddBarrier>(type);
253 }
254
255 using Command = std::pair<Type, void*>;
256 using Iter = SkTBlockList<Command, 16>::CIter;
257 Iter commands() const { return fCommands.items(); }
258
259 private:
260 template <typename T, typename... Args>
261 void add(Args&&... args) {
262 T* cmd = fAlloc.make<T>(T{std::forward<Args>(args)...});
263 fCommands.push_back(std::make_pair(T::kType, cmd));
264 }
265
266 SkTBlockList<Command, 16> fCommands{SkBlockAllocator::GrowthPolicy::kFibonacci};
267
268 // fAlloc needs to be a data structure which can append variable length data in contiguous
269 // chunks, returning a stable handle to that data for later retrieval.
270 SkArenaAlloc fAlloc{256};
271 };
272
273 } // namespace DrawPassCommands
274
275 } // namespace skgpu::graphite
276
277 #endif // skgpu_graphite_DrawPassCommands_DEFINED