VTK  9.3.20240919
vtkWebGPUComputePass.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef vtkWebGPUComputePass_h
5#define vtkWebGPUComputePass_h
6
7#include "vtkObject.h"
8
9#include "vtkRenderingWebGPUModule.h" // For export macro
10#include "vtkSmartPointer.h" // for arg
11#include "vtk_wgpu.h" // for webgpu
12
13#include <type_traits> // for enable_if_t
14
15class vtkDataArray;
25
26VTK_ABI_NAMESPACE_BEGIN
27
50class VTKRENDERINGWEBGPU_EXPORT vtkWebGPUComputePass : public vtkObject
51{
52public:
60
61 void PrintSelf(ostream& os, vtkIndent indent) override;
62
64
67 vtkGetMacro(ShaderSource, std::string);
68 vtkSetMacro(ShaderSource, std::string);
70
71 void SetShaderSourceFromPath(const char* shaderFilePath);
72
74
77 vtkGetMacro(ShaderEntryPoint, std::string);
78 vtkSetMacro(ShaderEntryPoint, std::string);
80
82
87 vtkGetMacro(Label, std::string&);
88 void SetLabel(const std::string& label);
90
98
104
110
118
124
129
144 void RebindTextureView(int group, int binding, int textureViewIndex);
145
149 void DeleteTextureViews(int textureIndex);
150
154 unsigned int GetBufferByteSize(int bufferIndex);
155
162 void ResizeBuffer(int bufferIndex, vtkIdType newByteSize);
163
171
179
185 void RecreateComputeTexture(int textureIndex);
186
191 void RecreateTextureView(int textureViewIndex);
192
193 /*
194 * Callback called when the asynchronous mapping of a buffer is done
195 * and data is ready to be copied.
196 * This callback takes three parameters:
197 *
198 * - A first void pointer to the data mapped from the GPU ready to be copied
199 * - A second void pointer pointing to user data, which can essentially be anything
200 * needed by the callback to copy the data to the CPU
201 */
202 using BufferMapAsyncCallback = std::function<void(const void*, void*)>;
203
204 /*
205 * This function maps the buffer, making it accessible to the CPU. This is
206 * an asynchronous operation, meaning that the given callback will be called
207 * when the mapping is done.
208 *
209 * The buffer data can then be read from the callback and stored
210 * in a buffer (std::vector<>, vtkDataArray, ...) passed in via the userdata pointer for example
211 */
212 void ReadBufferFromGPU(int bufferIndex, BufferMapAsyncCallback callback, void* userdata);
213
214 /*
215 * Callback called when the asynchronous mapping of a texture is done
216 * and data is ready to be copied.
217 * This callback takes three parameters:
218 *
219 * - A void pointer to the data mapped from the GPU ready to be copied.
220 *
221 * - An integer representing how many bytes per row the mapped data contains. This is
222 * useful because some padding has probably be done on the buffer to satisfy WebGPU size
223 * constraints. At the time of writing, buffers for texture mapping need a number of bytes per row
224 * that is a multiple of 256 bytes. This means that for a texture of 300x300 RGBA (300 * 4 = 1200
225 * bytes per row), there will be 80 bytes of additional padding to achieve 1280 bytes per row
226 * which is a multiple of 256. In this case, the integer argument of the callback will contain the
227 * value '1280' and it is then the responsibility of the user to only read relevant data (i.e. the
228 * 1200 first bytes of each row since the 80 last bytes are irrelevant padding).
229 *
230 * - A second void pointer pointing to user data, which can essentially be anything
231 * needed by the callback to copy the data to the CPU
232 */
233 using TextureMapAsyncCallback = std::function<void(const void*, int, void*)>;
234
245 int textureIndex, int mipLevel, TextureMapAsyncCallback callback, void* userdata);
246
259 template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
260 void UpdateBufferData(int bufferIndex, const std::vector<T>& newData)
261 {
262 this->WriteBufferData(bufferIndex, 0, newData.data(), newData.size() * sizeof(T));
263 }
264
270 template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
271 void UpdateBufferData(int bufferIndex, vtkIdType byteOffset, const std::vector<T>& data)
272 {
273 this->WriteBufferData(bufferIndex, byteOffset, data.data(), data.size() * sizeof(T));
274 }
275
287 void UpdateBufferData(int bufferIndex, vtkDataArray* newData);
288
294 void UpdateBufferData(int bufferIndex, vtkIdType byteOffset, vtkDataArray* newData);
295
299 template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
300 void UpdateTextureData(int textureIndex, const std::vector<T>& data)
301 {
302 this->WriteTextureData(textureIndex, data.data(), data.size() * sizeof(T));
303 }
304
306 /*
307 * Set/get the number of workgroups in each dimension that are used by each Dispatch() call.
308 */
309 void SetWorkgroups(int groupsX, int groupsY, int groupsZ);
311
315 void Dispatch();
316
323
324protected:
327
328private:
330 void operator=(const vtkWebGPUComputePass&) = delete;
331
332 void WriteBufferData(
333 int bufferIndex, vtkIdType byteOffset, const void* data, std::size_t numBytes);
334
335 void WriteTextureData(int textureIndex, const void* data, std::size_t numBytes);
336
341 friend class vtkWebGPUHelpers;
342 // For the mapper to be able to access the internals to access the wgpu::Buffer objects for use
343 // in a render pipeline
346 friend class vtkWebGPURenderer;
347
348 std::string ShaderSource;
349 std::string ShaderEntryPoint;
350
351 // How many groups to launch when dispatching the compute
352 unsigned int GroupsX = 0, GroupsY = 0, GroupsZ = 0;
353
354 // Label used for the wgpu compute pipeline of this VTK compute pipeline
355 std::string Label = "WebGPU compute pass";
356
357 // Label used for the wgpu command encoders created and used by this VTK compute pipeline
358 std::string WGPUCommandEncoderLabel = "WebGPU command encoder \"" + this->Label + "\"";
359 std::string WGPUComputePipelineLabel = "WebGPU pipeline \"" + this->Label + "\"";
360
361 // Internal implementation of the compute pass
363};
364
365VTK_ABI_NAMESPACE_END
366
367#endif
abstract superclass for arrays of numeric data
a simple class to control print indentation
Definition vtkIndent.h:108
abstract base class for most VTK objects
Definition vtkObject.h:162
Hold a reference to a vtkObjectBase instance.
Represents the set of parameters that will be used to create a compute shader buffer on the device wh...
This class manages the creation/deletion/recreation/resizing/updating of compute buffers used by a co...
Internals of the vtkWebGPUComputePass.
This class manages the creation/deletion/recreation/ of compute textures used by a compute pass.
A compute pass is an abstraction for offloading computation from the CPU onto the GPU using WebGPU co...
void AddRenderBuffer(vtkSmartPointer< vtkWebGPUComputeRenderBuffer > renderBuffer)
Adds a render buffer to the pass.
std::function< void(const void *, void *)> BufferMapAsyncCallback
void ReleaseResources()
Releases the resources used by this compute pass.
static vtkWebGPUComputePass * New()
Note to the VTK user: A compute pass should always be acquired through vtkWebGPUComputePipeline::Crea...
~vtkWebGPUComputePass() override
void DeleteTextureViews(int textureIndex)
Deletes all the texture views of a given texture (given by its index)
int AddTextureView(vtkSmartPointer< vtkWebGPUComputeTextureView > textureView)
Adds a texture view to the compute pass and returns its index.
void UpdateBufferData(int bufferIndex, const std::vector< T > &newData)
Updates the data of a buffer.
vtkSmartPointer< vtkWebGPUComputeTexture > GetComputeTexture(int textureIndex)
Retrieves the compute texture associated with the given texture index.
std::function< void(const void *, int, void *)> TextureMapAsyncCallback
int AddTexture(vtkSmartPointer< vtkWebGPUComputeTexture > texture)
Adds a texture to the pass and upload its data to the device.
void UpdateBufferData(int bufferIndex, vtkIdType byteOffset, const std::vector< T > &data)
Similar to the overload without offset of this function.
void SetLabel(const std::string &label)
Set/get the label of the compute pass.
void UpdateTextureData(int textureIndex, const std::vector< T > &data)
Uploads the given data to the texture starting at pixel (0, 0)
int AddRenderTexture(vtkSmartPointer< vtkWebGPUComputeRenderTexture > renderTexture)
Adds a render texture to the pass.
void UpdateBufferData(int bufferIndex, vtkDataArray *newData)
Updates the data of a buffer with a vtkDataArray.
int AddBuffer(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer)
Adds a buffer to the pass and uploads its data to the device.
void RecreateTextureView(int textureViewIndex)
Recreates a compute texture view.
void ResizeBuffer(int bufferIndex, vtkIdType newByteSize)
Resizes a buffer of the pass.
void ReadBufferFromGPU(int bufferIndex, BufferMapAsyncCallback callback, void *userdata)
vtkSmartPointer< vtkWebGPUComputeTextureView > GetTextureView(int textureViewIndex)
Retrieves the texture view associated with the given texture view index.
void UpdateBufferData(int bufferIndex, vtkIdType byteOffset, vtkDataArray *newData)
Similar to the overload without offset of this function.
void RecreateComputeTexture(int textureIndex)
Recreates a compute texture.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void Dispatch()
Dispatch the compute pass with (X, Y, Z) = (groupX, groupsY, groupZ) groups.
vtkSmartPointer< vtkWebGPUComputeTextureView > CreateTextureView(int textureIndex)
Returns a new texture view on the given texture (given by the index) that can be configured and then ...
unsigned int GetBufferByteSize(int bufferIndex)
Returns the size in bytes of a buffer.
void RebindTextureView(int group, int binding, int textureViewIndex)
This function allows the usage of multiple texture views on a single binding point (group / binding c...
void SetWorkgroups(int groupsX, int groupsY, int groupsZ)
void SetShaderSourceFromPath(const char *shaderFilePath)
void ReadTextureFromGPU(int textureIndex, int mipLevel, TextureMapAsyncCallback callback, void *userdata)
This function maps the texture into a linear memory block, making it accessible to the CPU.
A compute pipeline is the orchestrator of a collection of compute passes.
Render buffers are returned by calls to vtkWebGPUPolyDataMapper::AcquirePointAttributeComputeRenderBu...
Render textures are returned by calls to vtkWebGPUPolyDataMapper::AcquireXXXXRenderTexture() and repr...
Represents the set of parameters that will be used to create a compute shader texture on the device w...
Internal implementation details of vtkWebGPUPointCloudMapper.
int vtkIdType
Definition vtkType.h:315