VTK  9.3.20240919
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
16VTK_ABI_NAMESPACE_BEGIN
17
25{
26public:
29
42 {
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 */
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->GetDevice().GetQueue().WriteBuffer(
157 wgpuBuffer, 0, bytes, numBytes);
158 }
159
161 std::size_t bufferIndex, vtkIdType byteOffset, const void* bytes, std::size_t numBytes)
162 {
163 vtkWebGPUComputeBuffer* buffer = this->Buffers[bufferIndex];
164 vtkIdType byteSize = buffer->GetByteSize();
165 vtkIdType givenSize = numBytes;
166
167 if (givenSize + byteOffset > byteSize)
168 {
169 vtkLog(ERROR,
170 "void* data given to WriteBuffer with index "
171 << bufferIndex << " and offset " << byteOffset << " is too big. " << givenSize
172 << "bytes and offset " << byteOffset << " were given but the buffer is only " << byteSize
173 << " bytes long. No data was updated by this call.");
174
175 return;
176 }
177
178 wgpu::Buffer wgpuBuffer = this->WebGPUBuffers[bufferIndex];
179 this->ParentPassWGPUConfiguration->GetDevice().GetQueue().WriteBuffer(
180 wgpuBuffer, byteOffset, bytes, numBytes);
181 }
182
194 void UpdateBufferData(std::size_t bufferIndex, vtkDataArray* newData);
195
201 void UpdateBufferData(std::size_t bufferIndex, vtkIdType byteOffset, vtkDataArray* newData);
202
212 bool CheckBufferIndex(std::size_t bufferIndex, const std::string& callerFunctionName);
213
221
227
232
238
243 static wgpu::BufferBindingType ComputeBufferModeToBufferBindingType(
245
246protected:
249
250private:
252
253 // Compute pass that uses this buffer storage
254 vtkWeakPointer<vtkWebGPUComputePass> ParentComputePass;
255 // Device of the parent compute pass that is used when creating buffers
256 vtkSmartPointer<vtkWebGPUConfiguration> ParentPassWGPUConfiguration;
257
258 // Compute buffers
259 std::vector<vtkSmartPointer<vtkWebGPUComputeBuffer>> Buffers;
260 // WebGPU buffers associated with the compute buffers, in the same order
261 std::vector<wgpu::Buffer> WebGPUBuffers;
262};
263
264#endif
265
266VTK_ABI_NAMESPACE_END
abstract superclass for arrays of numeric data
abstract base class for most VTK objects
Definition vtkObject.h:162
Hold a reference to a vtkObjectBase instance.
a weak reference to a vtkObject.
Represents the set of parameters that will be used to create a compute shader buffer on the device wh...
virtual vtkIdType GetByteSize()
Get/set the size in bytes of the data passed in via SetData().
This class manages the creation/deletion/recreation/resizing/updating of compute buffers used by a co...
bool CheckBufferCorrectness(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer)
Makes some various (and obvious) checks to ensure that the buffer is ready to be created.
bool CheckBufferIndex(std::size_t bufferIndex, const std::string &callerFunctionName)
Checks if a given index is suitable for indexing a buffer of this storage.
static wgpu::BufferUsage ComputeBufferModeToBufferUsage(vtkWebGPUComputeBuffer::BufferMode mode)
Internal method used to convert the user friendly BufferMode to the internal enum wgpu::BufferUsage.
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.
vtkSetSmartPointerMacro(ParentPassWGPUConfiguration, vtkWebGPUConfiguration)
Sets the device that will be used by this buffer storage when creating buffers.
void UpdateBufferData(std::size_t bufferIndex, vtkDataArray *newData)
Updates the data of a buffer with a vtkDataArray.
int AddBuffer(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer)
Adds a buffer to the pipeline and uploads its data to the device.
void AddRenderBuffer(vtkSmartPointer< vtkWebGPUComputeRenderBuffer > renderBuffer)
Adds a render texture to the pipeline.
void RecreateBuffer(std::size_t bufferIndex, vtkIdType newByteSize)
Destroys and recreates a buffer with the given newByteSize Only the wgpu::Buffer object is recreated ...
void ReadBufferFromGPU(std::size_t bufferIndex, vtkWebGPUComputePass::BufferMapAsyncCallback callback, void *userdata)
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 WriteBuffer(std::size_t bufferIndex, const void *bytes, std::size_t numBytes)
Updates the data of a buffer.
void SetupRenderBuffer(vtkSmartPointer< vtkWebGPUComputeRenderBuffer > renderBuffer)
Binds the buffer to the pipeline at the WebGPU level.
void UpdateBufferData(std::size_t bufferIndex, vtkIdType byteOffset, vtkDataArray *newData)
Similar to the overload without offset of this function.
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...
void WriteBuffer(std::size_t bufferIndex, vtkIdType byteOffset, const void *bytes, std::size_t numBytes)
void ResizeBuffer(std::size_t bufferIndex, vtkIdType newByteSize)
Resizes a buffer.
static vtkWebGPUComputePassBufferStorageInternals * New()
void SetComputePass(vtkWeakPointer< vtkWebGPUComputePass > parentComputePass)
Sets the compute pass that uses the buffers of this storage.
UpdateBufferStatusCode UpdateWebGPUBuffer(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer, wgpu::Buffer wgpuBuffer, std::size_t &outBufferIndex)
Updates the wgpu::Buffer reference that a compute buffer is associated to.
Internals of the vtkWebGPUComputePass.
std::function< void(const void *, void *)> BufferMapAsyncCallback
Create a webgpu device for use in rendering and compute pipelines.
wgpu::Device GetDevice()
Get handles of the WGPU adapter/device/instance.
#define vtkLog(verbosity_name, x)
Add to log given the verbosity level.
Definition vtkLogger.h:513
int vtkIdType
Definition vtkType.h:315