VTK  9.7.1
vtkWebGPUComputePassBufferStorageInternals.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 vtkWebGPUComputePassBufferStorageInternals_h
5 #define vtkWebGPUComputePassBufferStorageInternals_h
6 
7 #include "vtkObject.h"
8 #include "vtkSmartPointer.h" // for smart pointers
9 #include "vtkWeakPointer.h" // for weak pointers
10 #include "vtkWebGPUComputeBuffer.h" // for compute buffers
11 #include "vtkWebGPUComputePass.h" // for enum
12 #include "vtkWebGPUComputeRenderBuffer.h" // for compute render buffers
13 #include "vtkWebGPUConfiguration.h" // for ivar
14 #include "vtk_wgpu.h" // for webgpu
15 
16 VTK_ABI_NAMESPACE_BEGIN
17 
25 {
26 public:
29 
42  {
43  SUCCESS = 0,
46  };
47 
53  vtkSetSmartPointerMacro(ParentPassWGPUConfiguration, vtkWebGPUConfiguration);
54 
59 
67 
72  wgpu::Buffer GetWGPUBuffer(std::size_t bufferIndex);
73 
80 
84  unsigned int GetBufferByteSize(std::size_t bufferIndex);
85 
92  void ResizeBuffer(std::size_t bufferIndex, vtkIdType newByteSize);
93 
98  void RecreateBuffer(std::size_t bufferIndex, vtkIdType newByteSize);
99 
100  /*
101  * This function maps the buffer, making it accessible to the CPU. This is
102  * an asynchronous operation, meaning that the given callback will be called
103  * when the mapping is done.
104  *
105  * The buffer data can then be read from the callback and stored
106  * in a buffer (std::vector<>, vtkDataArray, ...) passed in via the userdata pointer for example
107  */
108  void ReadBufferFromGPU(
109  std::size_t bufferIndex, vtkWebGPUComputePass::BufferMapAsyncCallback callback, void* userdata);
110 
124  wgpu::Buffer wgpuBuffer, std::size_t& outBufferIndex);
125 
138  void WriteBuffer(std::size_t bufferIndex, const void* bytes, std::size_t numBytes)
139  {
140  vtkWebGPUComputeBuffer* buffer = this->Buffers[bufferIndex];
141  vtkIdType byteSize = buffer->GetByteSize();
142  vtkIdType givenSize = numBytes;
143 
144  if (givenSize > byteSize)
145  {
146  vtkLog(ERROR,
147  "void* data given to UpdateBufferData with index "
148  << bufferIndex << " is too big. " << givenSize
149  << "bytes were given but the buffer is only " << byteSize
150  << " bytes long. No data was updated by this call.");
151 
152  return;
153  }
154 
155  wgpu::Buffer wgpuBuffer = this->WebGPUBuffers[bufferIndex];
156  this->ParentPassWGPUConfiguration->WriteBuffer(wgpuBuffer, 0, bytes, numBytes);
157  }
158 
160  std::size_t bufferIndex, vtkIdType byteOffset, const void* bytes, std::size_t numBytes)
161  {
162  vtkWebGPUComputeBuffer* buffer = this->Buffers[bufferIndex];
163  vtkIdType byteSize = buffer->GetByteSize();
164  vtkIdType givenSize = numBytes;
165 
166  if (givenSize + byteOffset > byteSize)
167  {
168  vtkLog(ERROR,
169  "void* data given to WriteBuffer with index "
170  << bufferIndex << " and offset " << byteOffset << " is too big. " << givenSize
171  << "bytes and offset " << byteOffset << " were given but the buffer is only " << byteSize
172  << " bytes long. No data was updated by this call.");
173 
174  return;
175  }
176 
177  wgpu::Buffer wgpuBuffer = this->WebGPUBuffers[bufferIndex];
178  this->ParentPassWGPUConfiguration->WriteBuffer(wgpuBuffer, byteOffset, bytes, numBytes);
179  }
180 
192  void UpdateBufferData(std::size_t bufferIndex, vtkDataArray* newData);
193 
199  void UpdateBufferData(std::size_t bufferIndex, vtkIdType byteOffset, vtkDataArray* newData);
200 
210  bool CheckBufferIndex(std::size_t bufferIndex, const std::string& callerFunctionName);
211 
219 
225 
229  void ReleaseResources();
230 
236 
241  static wgpu::BufferBindingType ComputeBufferModeToBufferBindingType(
243 
244 protected:
247 
248 private:
250 
253  void operator=(const vtkWebGPUComputePassBufferStorageInternals&) = delete;
254 
255  // Compute pass that uses this buffer storage
256  vtkWeakPointer<vtkWebGPUComputePass> ParentComputePass;
257  // Device of the parent compute pass that is used when creating buffers
258  vtkSmartPointer<vtkWebGPUConfiguration> ParentPassWGPUConfiguration;
259 
260  // Compute buffers
261  std::vector<vtkSmartPointer<vtkWebGPUComputeBuffer>> Buffers;
262  // WebGPU buffers associated with the compute buffers, in the same order
263  std::vector<wgpu::Buffer> WebGPUBuffers;
264 };
265 
266 #endif
267 
268 VTK_ABI_NAMESPACE_END
void WriteBuffer(std::size_t bufferIndex, const void *bytes, std::size_t numBytes)
Updates the data of a buffer.
Internals of the vtkWebGPUComputePass.
int AddBuffer(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer)
Adds a buffer to the pipeline and uploads its data to the device.
void SetupRenderBuffer(vtkSmartPointer< vtkWebGPUComputeRenderBuffer > renderBuffer)
Binds the buffer to the pipeline at the WebGPU level.
abstract base class for most VTK objects
Definition: vtkObject.h:52
void WriteBuffer(const wgpu::Buffer &buffer, std::uint64_t offset, const void *data, std::size_t sizeBytes, const char *description=nullptr)
Convenient method used to write data into an existing buffer.
wgpu::Buffer GetWGPUBuffer(std::size_t bufferIndex)
Returns the wgpu::Buffer object for a buffer in this compute pass buffer storage given its index...
void ReadBufferFromGPU(std::size_t bufferIndex, vtkWebGPUComputePass::BufferMapAsyncCallback callback, void *userdata)
static vtkWebGPUComputePassBufferStorageInternals * New()
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...
UpdateBufferStatusCode UpdateWebGPUBuffer(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer, wgpu::Buffer wgpuBuffer, std::size_t &outBufferIndex)
Updates the wgpu::Buffer reference that a compute buffer is associated to.
bool CheckBufferIndex(std::size_t bufferIndex, const std::string &callerFunctionName)
Checks if a given index is suitable for indexing a buffer of this storage.
void UpdateBufferData(std::size_t bufferIndex, vtkDataArray *newData)
Updates the data of a buffer with a vtkDataArray.
void AddRenderBuffer(vtkSmartPointer< vtkWebGPUComputeRenderBuffer > renderBuffer)
Adds a render texture to the pipeline.
#define vtkLog(verbosity_name, x)
Add to log given the verbosity level.
Definition: vtkLogger.h:494
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
virtual vtkIdType GetByteSize()
Get/set the size in bytes of the data passed in via SetData().
bool CheckBufferCorrectness(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer)
Makes some various (and obvious) checks to ensure that the buffer is ready to be created.
void SetComputePass(vtkWeakPointer< vtkWebGPUComputePass > parentComputePass)
Sets the compute pass that uses the buffers of this storage.
unsigned int GetBufferByteSize(std::size_t bufferIndex)
Returns the size in bytes of a buffer.
void ReleaseResources()
Releases the buffers & resources held by this buffer storage.
This class manages the creation/deletion/recreation/resizing/updating of compute buffers used by a co...
void ResizeBuffer(std::size_t bufferIndex, vtkIdType newByteSize)
Resizes a buffer.
static wgpu::BufferBindingType ComputeBufferModeToBufferBindingType(vtkWebGPUComputeBuffer::BufferMode mode)
Internal method used to convert the user friendly BufferMode to the internal enum wgpu::BufferBinding...
UpdateBufferStatusCode
Enum used by the returned value of UpdateWebGPUBuffer() to indicate what operation was done internall...
Create a webgpu device for use in rendering and compute pipelines.
void WriteBuffer(std::size_t bufferIndex, vtkIdType byteOffset, const void *bytes, std::size_t numBytes)
static wgpu::BufferUsage ComputeBufferModeToBufferUsage(vtkWebGPUComputeBuffer::BufferMode mode)
Internal method used to convert the user friendly BufferMode to the internal enum wgpu::BufferUsage...
void RecreateBuffer(std::size_t bufferIndex, vtkIdType newByteSize)
Destroys and recreates a buffer with the given newByteSize Only the wgpu::Buffer object is recreated ...
std::function< void(const void *, void *)> BufferMapAsyncCallback