Debian Codegraph @

src/gpu/graphite/compute/ComputeStep.h

raw
1 /*
2 * Copyright 2023 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_compute_ComputeStep_DEFINED
9 #define skgpu_graphite_compute_ComputeStep_DEFINED
10
11 #include "include/core/SkSpan.h"
12 #include "include/private/base/SkTArray.h"
13 #include "include/private/base/SkTo.h"
14 #include "src/base/SkEnumBitMask.h"
15 #include "src/gpu/graphite/ComputeTypes.h"
16 #include "src/gpu/graphite/ResourceTypes.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21 #include <string_view>
22 #include <tuple>
23
24 enum SkColorType : int;
25 struct SkISize;
26
27 namespace skgpu {
28 struct BufferWriter;
29 }
30
31 namespace skgpu::graphite {
32
33 class UniformManager;
34
35 /**
36 * A `ComputeStep` represents a compute pass within a wider draw operation. A `ComputeStep`
37 * implementation describes an invocation of a compute program and its data binding layout.
38 *
39 * A `ComputeStep` can perform arbitrary operations on the GPU over various types of data, including
40 * geometry and image processing. The data processed by a `ComputeStep` can be inputs (textures or
41 * buffers) populated on the CPU, data forwarded to and from other `ComputeStep` invocations (via
42 * "slots"), transient storage buffers/textures that are only used within an individual dispatch,
43 * geometry attribute (vertex/index/instance) and indirect draw parameters of a subsequent raster
44 * pipeline stage, as well as texture outputs.
45 *
46 * The data flow between sequential `ComputeStep` invocations within a DispatchGroup is achieved by
47 * operating over a shared "resource table". `ComputeStep`s can declare a resource with a slot
48 * number. Multiple `ComputeStep`s in a group that declare a resource with the same slot number will
49 * have access to the same backing resource object through that slot:
50 *
51 * _______________ _______________
52 * | | | |
53 * | ---[Slot 0]--- |
54 * | | | |
55 * | ---[Slot 1]--- |
56 * | ComputeStep 1 | | ComputeStep 2 |
57 * | ---[Slot 2] | |
58 * | | | |
59 * | | [Slot 3]--- |
60 * | | | |
61 * --------------- ---------------
62 *
63 * In the example above, slots 0 and 1 are accessed by both ComputeSteps, while slots 2 and 3 are
64 * exclusively accessed by ComputeStep 1 and 2 respectively. Alternately, slots 2 and 3 could be
65 * declared as "private" resources which are visible to a single ComputeStep.
66 *
67 * Similarly, raster stage geometry buffers that are specified as the output of a ComputeStep can be
68 * used to assign the draw buffers of a RenderStep.
69 *
70 * It is the responsibility of the owning entity (e.g. a RendererProvider) to ensure that a chain of
71 * ComputeStep and RenderStep invocations have a compatible resource and data-flow layout.
72 */
73 class ComputeStep {
74 public:
75 enum class DataFlow {
76 // A private binding is a resource that is only visible to a single ComputeStep invocation.
77 kPrivate,
78
79 // Bindings with a slot number that can be used to forward data between a series of
80 // `ComputeStep`s. This DataFlow type is accompanied with a "slot number" that can be
81 // shared by multiple `ComputeStep`s in a group.
82 kShared,
83 };
84
85 enum class ResourceType {
86 kUniformBuffer,
87 kStorageBuffer,
88 kReadOnlyStorageBuffer,
89
90 // An indirect buffer is a storage buffer populated by this ComputeStep to determine the
91 // global dispatch size of a subsequent ComputeStep within the same DispatchGroup. The
92 // contents of the buffer must be laid out according to the `IndirectDispatchArgs` struct
93 // definition declared in ComputeTypes.h.
94 kIndirectBuffer,
95
96 kWriteOnlyStorageTexture,
97 kReadOnlyTexture,
98 kSampledTexture,
99 };
100
101 enum class ResourcePolicy {
102 kNone,
103
104 // The memory of the resource will be initialized to 0
105 kClear,
106
107 // The ComputeStep will be asked to initialize the memory on the CPU via
108 // `ComputeStep::prepareStorageBuffer` or `ComputeStep::prepareUniformBuffer` prior to
109 // pipeline execution. This may incur a transfer cost on platforms that do not allow buffers
110 // to be mapped in shared memory.
111 //
112 // If multiple ComputeSteps in a DispatchGroup declare a mapped resource with the same
113 // shared slot number, only the first ComputeStep in the group will receive a call to
114 // prepare the buffer.
115 //
116 // This only has meaning for buffer resources. A resource with the `kUniformBuffer` resource
117 // type must specify the `kMapped` resource policy.
118 kMapped,
119 };
120
121 struct ResourceDesc final {
122 ResourceType fType;
123 DataFlow fFlow;
124 ResourcePolicy fPolicy;
125
126 // This field only has meaning (and must have a non-negative value) if `fFlow` is
127 // `DataFlow::kShared`.
128 int fSlot;
129
130 // The SkSL variable declaration code excluding the layout and type definitions. This field
131 // is ignored for a ComputeStep that supports native shader source.
132 const char* fSkSL = "";
133
134 constexpr ResourceDesc(ResourceType type,
135 DataFlow flow,
136 ResourcePolicy policy,
137 int slot = -1)
138 : fType(type), fFlow(flow), fPolicy(policy), fSlot(slot) {}
139
140 constexpr ResourceDesc(ResourceType type,
141 DataFlow flow,
142 ResourcePolicy policy,
143 int slot,
144 const char* sksl)
145 : fType(type), fFlow(flow), fPolicy(policy), fSlot(slot), fSkSL(sksl) {}
146
147 constexpr ResourceDesc(ResourceType type,
148 DataFlow flow,
149 ResourcePolicy policy,
150 const char* sksl)
151 : fType(type), fFlow(flow), fPolicy(policy), fSlot(-1), fSkSL(sksl) {}
152 };
153
154 // On platforms that support late bound workgroup shared resources (e.g. Metal) a ComputeStep
155 // can optionally provide a list of memory sizes and binding indices.
156 struct WorkgroupBufferDesc {
157 // The buffer size in bytes.
158 uint32_t size;
159 uint32_t index;
160 };
161
162 virtual ~ComputeStep() = default;
163
164 // Returns a complete SkSL compute program. The returned SkSL must constitute a complete compute
165 // program and declare all resource bindings starting at `nextBindingIndex` in the order in
166 // which they are enumerated by `ComputeStep::resources()`.
167 //
168 // If this ComputeStep supports native shader source then it must override
169 // `nativeShaderSource()` instead.
170 virtual std::string computeSkSL() const;
171
172 // A ComputeStep that supports native shader source then then it must implement
173 // `nativeShaderSource()` and return the shader source in the requested format. This is intended
174 // to instantiate a compute pipeline from a pre-compiled shader module. The returned source must
175 // constitute a shader module that contains at least one compute entry-point function that
176 // matches the specified name.
177 enum class NativeShaderFormat {
178 kWGSL,
179 kMSL,
180 };
181 struct NativeShaderSource {
182 std::string_view fSource;
183 std::string fEntryPoint;
184 };
185 virtual NativeShaderSource nativeShaderSource(NativeShaderFormat) const;
186
187 // This method will be called for buffer entries in the ComputeStep's resource list to
188 // determine the required allocation size. The ComputeStep must return a non-zero value.
189 //
190 // TODO(b/279955342): Provide a context object, e.g. a type a associated with
191 // DispatchGroup::Builder, to aid the ComputeStep in its buffer size calculations.
192 virtual size_t calculateBufferSize(int resourceIndex, const ResourceDesc&) const;
193
194 // This method will be called for storage texture entries in the ComputeStep's resource list to
195 // determine the required dimensions and color type. The ComputeStep must return a non-zero
196 // value for the size and a valid color type.
197 virtual std::tuple<SkISize, SkColorType> calculateTextureParameters(int resourceIndex,
198 const ResourceDesc&) const;
199
200 // This method will be called for sampler entries in the ComputeStep's resource list to
201 // determine the sampling and tile mode options.
202 virtual SamplerDesc calculateSamplerParameters(int resourceIndex, const ResourceDesc&) const;
203
204 // Return the global dispatch size (aka "workgroup count") for this step based on the draw
205 // parameters. The default value is a workgroup count of (1, 1, 1)
206 //
207 // TODO(b/279955342): Provide a context object, e.g. a type a associated with
208 // DispatchGroup::Builder, to aid the ComputeStep in its buffer size calculations.
209 virtual WorkgroupSize calculateGlobalDispatchSize() const;
210
211 // Populates a storage buffer resource which was specified as "mapped". This method will only be
212 // called once for a resource right after its allocation and before pipeline execution. For
213 // shared resources, only the first ComputeStep in a DispatchGroup will be asked to prepare the
214 // buffer.
215 //
216 // `resourceIndex` matches the order in which `resource` was enumerated by
217 // `ComputeStep::resources()`.
218 virtual void prepareStorageBuffer(int resourceIndex,
219 const ResourceDesc& resource,
220 BufferWriter&&) const;
221
222 // Populates a uniform buffer resource. This method will be called once for a resource right
223 // after its allocation and before pipeline execution. For shared resources, only the first
224 // ComputeStep in a DispatchGroup will be asked to prepare the buffer.
225 //
226 // `resourceIndex` matches the order in which `resource` was enumerated by
227 // `ComputeStep::resources()`.
228 //
229 // The implementation must use the provided `UniformManager` to populate the buffer. On debug
230 // builds, the implementation must validate the buffer layout by setting up an expectation, for
231 // example:
232 //
233 // SkDEBUGCODE(mgr->setExpectedUniforms({{"foo", SkSLType::kFloat}}));
234 //
235 // TODO(b/279955342): Provide a context object, e.g. a type a associated with
236 // DispatchGroup::Builder, to aid the ComputeStep in its buffer size calculations.
237 virtual void prepareUniformBuffer(int resourceIndex,
238 const ResourceDesc&,
239 UniformManager*) const;
240
241 SkSpan<const ResourceDesc> resources() const { return SkSpan(fResources); }
242 SkSpan<const WorkgroupBufferDesc> workgroupBuffers() const { return SkSpan(fWorkgroupBuffers); }
243
244 // Identifier that can be used as part of a unique key for a compute pipeline state object
245 // associated with this `ComputeStep`.
246 uint32_t uniqueID() const { return fUniqueID; }
247
248 // Returns a debug name for the subclass implementation.
249 const char* name() const { return fName.c_str(); }
250
251 // The size of the workgroup for this ComputeStep's entry point function. This value is hardware
252 // dependent. On Metal, this value should be used when invoking the dispatch API call. On all
253 // other backends, this value will be baked into the pipeline.
254 WorkgroupSize localDispatchSize() const { return fLocalDispatchSize; }
255
256 bool supportsNativeShader() const { return SkToBool(fFlags & Flags::kSupportsNativeShader); }
257
258 protected:
259 enum class Flags : uint8_t {
260 kNone = 0b00000,
261 kSupportsNativeShader = 0b00010,
262 };
263 SK_DECL_BITMASK_OPS_FRIENDS(Flags)
264
265 ComputeStep(std::string_view name,
266 WorkgroupSize localDispatchSize,
267 SkSpan<const ResourceDesc> resources,
268 SkSpan<const WorkgroupBufferDesc> workgroupBuffers = {},
269 Flags baseFlags = Flags::kNone);
270
271 private:
272 // Disallow copy and move
273 ComputeStep(const ComputeStep&) = delete;
274 ComputeStep(ComputeStep&&) = delete;
275
276 uint32_t fUniqueID;
277 SkEnumBitMask<Flags> fFlags;
278 std::string fName;
279 skia_private::TArray<ResourceDesc> fResources;
280 skia_private::TArray<WorkgroupBufferDesc> fWorkgroupBuffers;
281
282 // TODO(b/240615224): Subclasses should simply specify the workgroup size that they need.
283 // The ComputeStep constructor should check and reduce that number based on the maximum
284 // supported workgroup size stored in Caps. In Metal, we'll pass this number directly to the
285 // dispatch API call. On other backends, we'll use this value to generate the right SkSL
286 // workgroup size declaration to avoid any validation failures.
287 WorkgroupSize fLocalDispatchSize;
288 };
289 SK_MAKE_BITMASK_OPS(ComputeStep::Flags)
290
291 } // namespace skgpu::graphite
292
293 #endif // skgpu_graphite_compute_ComputeStep_DEFINED