VTK  9.7.20260904
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
223 void ReadBufferFromGPU(int bufferIndex, vtkDataArray* targetArray);
224
225 /*
226 * Callback called when the asynchronous mapping of a texture is done
227 * and data is ready to be copied.
228 * This callback takes three parameters:
229 *
230 * - A void pointer to the data mapped from the GPU ready to be copied.
231 *
232 * - An integer representing how many bytes per row the mapped data contains. This is
233 * useful because some padding has probably be done on the buffer to satisfy WebGPU size
234 * constraints. At the time of writing, buffers for texture mapping need a number of bytes per row
235 * that is a multiple of 256 bytes. This means that for a texture of 300x300 RGBA (300 * 4 = 1200
236 * bytes per row), there will be 80 bytes of additional padding to achieve 1280 bytes per row
237 * which is a multiple of 256. In this case, the integer argument of the callback will contain the
238 * value '1280' and it is then the responsibility of the user to only read relevant data (i.e. the
239 * 1200 first bytes of each row since the 80 last bytes are irrelevant padding).
240 *
241 * - A second void pointer pointing to user data, which can essentially be anything
242 * needed by the callback to copy the data to the CPU
243 */
244 using TextureMapAsyncCallback = std::function<void(const void*, int, void*)>;
245
256 int textureIndex, int mipLevel, TextureMapAsyncCallback callback, void* userdata);
257
270 template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
271 void UpdateBufferData(int bufferIndex, const std::vector<T>& newData)
272 {
273 this->WriteBufferData(bufferIndex, 0, newData.data(), newData.size() * sizeof(T));
274 }
275
281 template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
282 void UpdateBufferData(int bufferIndex, vtkIdType byteOffset, const std::vector<T>& data)
283 {
284 this->WriteBufferData(bufferIndex, byteOffset, data.data(), data.size() * sizeof(T));
285 }
286
298 void UpdateBufferData(int bufferIndex, vtkDataArray* newData);
299
305 void UpdateBufferData(int bufferIndex, vtkIdType byteOffset, vtkDataArray* newData);
306
310 template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
311 void UpdateTextureData(int textureIndex, const std::vector<T>& data)
312 {
313 this->WriteTextureData(textureIndex, data.data(), data.size() * sizeof(T));
314 }
315
317 /*
318 * Set/get the number of workgroups in each dimension that are used by each Dispatch() call.
319 */
320 void SetWorkgroups(int groupsX, int groupsY, int groupsZ);
322
326 void Dispatch();
327
334
335protected:
338
339private:
341 void operator=(const vtkWebGPUComputePass&) = delete;
342
343 void WriteBufferData(
344 int bufferIndex, vtkIdType byteOffset, const void* data, std::size_t numBytes);
345
346 void WriteTextureData(int textureIndex, const void* data, std::size_t numBytes);
347
352 friend class vtkWebGPUHelpers;
353 // For the mapper to be able to access the internals to access the wgpu::Buffer objects for use
354 // in a render pipeline
357 friend class vtkWebGPURenderer;
358
359 std::string ShaderSource;
360 std::string ShaderEntryPoint;
361
362 // How many groups to launch when dispatching the compute
363 unsigned int GroupsX = 0, GroupsY = 0, GroupsZ = 0;
364
365 // Label used for the wgpu compute pipeline of this VTK compute pipeline
366 std::string Label = "WebGPU compute pass";
367
368 // Label used for the wgpu command encoders created and used by this VTK compute pipeline
369 std::string WGPUCommandEncoderLabel = "WebGPU command encoder \"" + this->Label + "\"";
370 std::string WGPUComputePipelineLabel = "WebGPU pipeline \"" + this->Label + "\"";
371
372 // Internal implementation of the compute pass
374};
375
376VTK_ABI_NAMESPACE_END
377
378#endif
a simple class to control print indentation
Definition vtkIndent.h:108
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.
void AddRenderBuffer(vtkSmartPointer< vtkWebGPUComputeRenderBuffer > renderBuffer)
Adds a render buffer to the pass.
std::function< void(const void *, void *)> BufferMapAsyncCallback
friend class vtkWebGPUComputePassBufferStorageInternals
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...
friend class vtkWebGPUComputePassTextureStorageInternals
~vtkWebGPUComputePass() override
friend class vtkWebGPUPointCloudMapperInternals
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.
void ReadBufferFromGPU(int bufferIndex, vtkDataArray *targetArray)
Asynchronously copies a GPU buffer's contents into a VTK data array.
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.
friend class vtkWebGPUComputePassInternals
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.
friend class vtkWebGPUComputePipeline
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...
#define vtkDataArray
int vtkIdType
Definition vtkType.h:363