VTK  9.7.1
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 
15 class vtkDataArray;
25 
26 VTK_ABI_NAMESPACE_BEGIN
27 
50 class VTKRENDERINGWEBGPU_EXPORT vtkWebGPUComputePass : public vtkObject
51 {
52 public:
58  static vtkWebGPUComputePass* New();
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 
97  int AddBuffer(vtkSmartPointer<vtkWebGPUComputeBuffer> buffer);
98 
103  void AddRenderBuffer(vtkSmartPointer<vtkWebGPUComputeRenderBuffer> renderBuffer);
104 
109  int AddRenderTexture(vtkSmartPointer<vtkWebGPUComputeRenderTexture> renderTexture);
110 
117  int AddTexture(vtkSmartPointer<vtkWebGPUComputeTexture> texture);
118 
123  vtkSmartPointer<vtkWebGPUComputeTextureView> CreateTextureView(int textureIndex);
124 
128  int AddTextureView(vtkSmartPointer<vtkWebGPUComputeTextureView> textureView);
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 
170  vtkSmartPointer<vtkWebGPUComputeTexture> GetComputeTexture(int textureIndex);
171 
178  vtkSmartPointer<vtkWebGPUComputeTextureView> GetTextureView(int textureViewIndex);
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 
244  void ReadTextureFromGPU(
245  int textureIndex, int mipLevel, TextureMapAsyncCallback callback, void* userdata);
246 
260  void UpdateBufferData(int bufferIndex, const std::vector<T>& newData)
261  {
262  this->WriteBufferData(bufferIndex, 0, newData.data(), newData.size() * sizeof(T));
263  }
264 
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 
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 
322  void ReleaseResources();
323 
324 protected:
326  ~vtkWebGPUComputePass() override;
327 
328 private:
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
345  friend class vtkWebGPURenderWindow;
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 
365 VTK_ABI_NAMESPACE_END
366 
367 #endif
Internals of the vtkWebGPUComputePass.
std::function< void(const void *, int, void *)> TextureMapAsyncCallback
void UpdateTextureData(int textureIndex, const std::vector< T > &data)
Uploads the given data to the texture starting at pixel (0, 0)
abstract base class for most VTK objects
Definition: vtkObject.h:52
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
Render textures are returned by calls to vtkWebGPUPolyDataMapper::AcquireXXXXRenderTexture() and repr...
int vtkIdType
Definition: vtkType.h:363
Represents the set of parameters that will be used to create a compute shader buffer on the device wh...
Internal implementation details of vtkWebGPUPointCloudMapper.
a simple class to control print indentation
Definition: vtkIndent.h:28
void UpdateBufferData(int bufferIndex, const std::vector< T > &newData)
Updates the data of a buffer.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
void UpdateBufferData(int bufferIndex, vtkIdType byteOffset, const std::vector< T > &data)
Similar to the overload without offset of this function.
This class manages the creation/deletion/recreation/resizing/updating of compute buffers used by a co...
Render buffers are returned by calls to vtkWebGPUPolyDataMapper::AcquirePointAttributeComputeRenderBu...
Represents the set of parameters that will be used to create a compute shader texture on the device w...
A compute pipeline is the orchestrator of a collection of compute passes.
This class manages the creation/deletion/recreation/ of compute textures used by a compute pass...
WebGPU rendering window.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
std::function< void(const void *, void *)> BufferMapAsyncCallback
A compute pass is an abstraction for offloading computation from the CPU onto the GPU using WebGPU co...