VTK  9.3.20240919
vtkWebGPUComputePassInternals.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 vtkWebGPUComputePassInternals_h
5#define vtkWebGPUComputePassInternals_h
6
9#include "vtkObject.h"
10#include "vtkSmartPointer.h"
12#include "vtk_wgpu.h" // for webgpu
13
14#include <unordered_map>
15#include <unordered_set>
16
18
19VTK_ABI_NAMESPACE_BEGIN
20
26class VTKRENDERINGWEBGPU_EXPORT vtkWebGPUComputePassInternals : public vtkObject
27{
28public:
31
32 void PrintSelf(ostream& os, vtkIndent indent) override;
33
38
40
47
49
55
60 wgpu::BindGroupLayoutEntry CreateBindGroupLayoutEntry(
61 uint32_t binding, vtkWebGPUComputeBuffer::BufferMode mode);
62
66 wgpu::BindGroupLayoutEntry CreateBindGroupLayoutEntry(uint32_t binding,
69
76 wgpu::BindGroupLayoutEntry CreateBindGroupLayoutEntry(uint32_t binding,
78 wgpu::TextureViewDimension textureViewDimension);
79
84 wgpu::BindGroupEntry CreateBindGroupEntry(wgpu::Buffer buffer, uint32_t binding,
85 vtkWebGPUComputeBuffer::BufferMode mode, uint32_t offset);
86
91 wgpu::BindGroupEntry CreateBindGroupEntry(uint32_t binding, wgpu::TextureView textureView);
92
97
103
108 static wgpu::BindGroupLayout CreateBindGroupLayout(
109 const wgpu::Device& device, const std::vector<wgpu::BindGroupLayoutEntry>& layoutEntries);
110
114 std::vector<wgpu::BindGroupEntry> CreateBindGroupEntries(
115 const std::vector<vtkWebGPUComputeBuffer*>& buffers);
116
126 bool CheckBufferIndex(int bufferIndex, const std::string& callerFunctionName);
127
133 bool CheckTextureIndex(int textureIndex, const std::string& callerFunctionName);
134
140 bool CheckTextureViewIndex(int textureViewIndex, const std::string& callerFunctionName);
141
147
155 void RecreateBufferBindGroup(int bufferIndex);
156
162 vtkSmartPointer<vtkWebGPUComputeBuffer> buffer, wgpu::Buffer wgpuBuffer);
163
165
173 vtkSmartPointer<vtkWebGPUComputeBuffer> buffer, wgpu::Buffer& wgpuBuffer);
174
176 vtkSmartPointer<vtkWebGPUComputeTexture> texture, wgpu::Texture& wgpuTexture);
178
183 wgpu::Buffer GetWGPUBuffer(std::size_t bufferIndex);
184
190 vtkSmartPointer<vtkWebGPUComputeTexture> texture, wgpu::Texture wgpuTexture);
191
196 void RecreateBuffer(int bufferIndex, vtkIdType newByteSize);
197
201 void RecreateTexture(int textureIndex);
202
210 void RecreateTextureViews(int textureIndex);
211
216 wgpu::TextureView CreateWebGPUTextureView(
217 vtkSmartPointer<vtkWebGPUComputeTextureView> textureView, wgpu::Texture wgpuTexture);
218
224 void UpdateWebGPUBuffer(vtkSmartPointer<vtkWebGPUComputeBuffer> buffer, wgpu::Buffer wgpuBuffer);
225
237 vtkSmartPointer<vtkWebGPUComputeTexture> texture, wgpu::Texture newWgpuTexture);
238
243 void RecreateTextureBindGroup(int textureIndex);
244
250
260
265
273 wgpu::PipelineLayout CreateWebGPUComputePipelineLayout();
274
278 wgpu::CommandEncoder CreateCommandEncoder();
279
283 wgpu::ComputePassEncoder CreateComputePassEncoder(const wgpu::CommandEncoder& commandEncoder);
284
291 void WebGPUDispatch(unsigned int groupsX, unsigned int groupsY, unsigned int groupsZ);
292
297 void SubmitCommandEncoderToQueue(const wgpu::CommandEncoder& commandEncoder);
298
303
304 // Compute pass whose internals this class represents
306
307protected:
310
311private:
316 // For the mapper to be able to access the wgpu::Buffer objects for use in a render pipeline
318
323 bool Initialized = false;
324 // Wheter or not the binds group / layouts have changed since they were last created and so they
325 // need to be recreated. Defaults to true since the bind group / layouts are initially not created
326 // so they are invalid.
327 bool BindGroupOrLayoutsInvalidated = true;
328
329 // Device of the compute pipeline this pass belongs to. Used to submit commands.
331
332 // The compute pipeline this compute pass belongs to.
334
335 wgpu::ShaderModule ShaderModule;
336 // List of the bind groups, used to set the bind groups of the compute pass at each dispatch
337 std::vector<wgpu::BindGroup> BindGroups;
338 // Maps a bind group index to to the list of bind group entries for this group. These
339 // entries will be used at the creation of the bind groups
340 std::unordered_map<int, std::vector<wgpu::BindGroupEntry>> BindGroupEntries;
341 std::vector<wgpu::BindGroupLayout> BindGroupLayouts;
342 // Maps a bind group index to to the list of bind group layout entries for this group.
343 // These layout entries will be used at the creation of the bind group layouts
344 std::unordered_map<int, std::vector<wgpu::BindGroupLayoutEntry>> BindGroupLayoutEntries;
345 // WebGPU compute shader pipeline
346 wgpu::ComputePipeline ComputePipeline;
347
348 // Object responsible for the management (creation, re-creatioin, deletion, ...) of textures and
349 // their texture views
351
352 // Object responsible for the management (creation, re-creatioin, deletion, ...) of buffers
354};
355
356VTK_ABI_NAMESPACE_END
357
358#endif
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.
a weak reference to a vtkObject.
This class manages the creation/deletion/recreation/resizing/updating of compute buffers used by a co...
Internals of the vtkWebGPUComputePass.
void RecreateRenderTexture(vtkSmartPointer< vtkWebGPUComputeRenderTexture > renderTexture)
Recreates a render texture given a new textureView and possibly new parameters as specified in the 'r...
void RegisterTextureToPipeline(vtkSmartPointer< vtkWebGPUComputeTexture > texture, wgpu::Texture wgpuTexture)
Registers a texture to the associated compute pipeline of this compute pass so that other compute pas...
bool GetRegisteredBufferFromPipeline(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer, wgpu::Buffer &wgpuBuffer)
Checks whether the given compute buffer/texture has already been registered in the pipeline associate...
std::vector< wgpu::BindGroupEntry > CreateBindGroupEntries(const std::vector< vtkWebGPUComputeBuffer * > &buffers)
Creates the bind group entries given a list of buffers.
vtkWeakPointer< vtkWebGPUComputePipeline > GetAssociatedPipeline()
Get/set the compute pipeline to which this compute pass belongs to.
wgpu::BindGroupEntry CreateBindGroupEntry(wgpu::Buffer buffer, uint32_t binding, vtkWebGPUComputeBuffer::BufferMode mode, uint32_t offset)
Given a buffer, creates the associated bind group entry that will be used when creating the bind grou...
void CreateBindGroupsAndLayouts()
Creates all the bind groups and bind group layouts of this compute pass from the buffers that have be...
void SetupRenderBuffer(vtkSmartPointer< vtkWebGPUComputeRenderBuffer > renderBuffer)
Binds the buffer to the compute pass at the WebGPU level.
wgpu::Buffer GetWGPUBuffer(std::size_t bufferIndex)
Returns the wgpu::Buffer object for a buffer in this compute pass buffer storage given its index.
wgpu::PipelineLayout CreateWebGPUComputePipelineLayout()
Creates the compute pipeline layout associated with the bind group layouts of this compute pass.
wgpu::CommandEncoder CreateCommandEncoder()
Creates and returns a command encoder.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void UpdateComputeTextureAndViews(vtkSmartPointer< vtkWebGPUComputeTexture > texture, wgpu::Texture newWgpuTexture)
Makes sure that the compute texture given in parameter internally points to the given newWgpuTexture.
wgpu::TextureView CreateWebGPUTextureView(vtkSmartPointer< vtkWebGPUComputeTextureView > textureView, wgpu::Texture wgpuTexture)
Utilitary method to create a wgpu::TextureView from a ComputeTextureView and the texture this wgpu::T...
vtkWeakPointer< vtkWebGPUComputePass > ParentPass
wgpu::BindGroupLayoutEntry CreateBindGroupLayoutEntry(uint32_t binding, vtkWebGPUComputeBuffer::BufferMode mode)
Given a buffer, creates the associated bind group layout entry that will be used when creating the bi...
~vtkWebGPUComputePassInternals() override
void CreateShaderModule()
Compiles the shader source given into a WGPU shader module.
void RecreateTexture(int textureIndex)
Destroys and recreates the texture with the given index.
void CreateWebGPUComputePipeline()
Creates the compute pipeline that will be used to dispatch the compute shader.
void RecreateTextureViews(int textureIndex)
Recreates all the texture views of a texture given its index.
bool CheckBufferCorrectness(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer)
Returns true if the buffer is ready to be added to the compute pass or not.
bool CheckTextureIndex(int textureIndex, const std::string &callerFunctionName)
Checks if a given index correponds to a texture of this compute pass.
void RegisterBufferToPipeline(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer, wgpu::Buffer wgpuBuffer)
Registers a buffer to the associated compute pipeline of this compute pass so that other compute pass...
void UpdateWebGPUBuffer(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer, wgpu::Buffer wgpuBuffer)
Updates the wgpu::Buffer reference that a compute buffer is associated to.
void RecreateTextureBindGroup(int textureIndex)
After recreating a wgpu::Buffer, the bind group entry (and the bind group) will need to be updated.
void ReleaseResources()
Releases the resources of this compute pass internals.
bool GetRegisteredTextureFromPipeline(vtkSmartPointer< vtkWebGPUComputeTexture > texture, wgpu::Texture &wgpuTexture)
Checks whether the given compute buffer/texture has already been registered in the pipeline associate...
void SubmitCommandEncoderToQueue(const wgpu::CommandEncoder &commandEncoder)
Finishes the encoding of a command encoder and submits the resulting command buffer to the queue.
void RecreateBufferBindGroup(int bufferIndex)
Recreates the bind group and bind group entry of a buffer (given by its index)
bool CheckTextureViewIndex(int textureViewIndex, const std::string &callerFunctionName)
Checks if a given index correponds to a texture texture view of this compute pass.
static wgpu::BindGroupLayout CreateBindGroupLayout(const wgpu::Device &device, const std::vector< wgpu::BindGroupLayoutEntry > &layoutEntries)
Creates the bind group layout of a given list of buffers (that must all belong to the same bind group...
vtkGetSmartPointerMacro(WGPUConfiguration, vtkWebGPUConfiguration)
Get/set the device used by this compute pass (usually the device of the compute pipeline holding this...
void RecreateBuffer(int bufferIndex, vtkIdType newByteSize)
Destroys and recreates a buffer with the given newByteSize Only the wgpu::Buffer object is recreated ...
void SetWGPUConfiguration(vtkWebGPUConfiguration *config)
Get/set the device used by this compute pass (usually the device of the compute pipeline holding this...
bool CheckBufferIndex(int bufferIndex, const std::string &callerFunctionName)
Checks if a given index is suitable for indexing Buffers.
wgpu::BindGroupLayoutEntry CreateBindGroupLayoutEntry(uint32_t binding, vtkSmartPointer< vtkWebGPUComputeTexture > computeTexture, wgpu::TextureViewDimension textureViewDimension)
Overload mainly used for creating the layout entry of render textures where we don't have a vtkWebGPU...
static vtkWebGPUComputePassInternals * New()
wgpu::BindGroupLayoutEntry CreateBindGroupLayoutEntry(uint32_t binding, vtkSmartPointer< vtkWebGPUComputeTexture > computeTexture, vtkSmartPointer< vtkWebGPUComputeTextureView > textureView)
Given a texture and its view, creates the associated bind group layout entry and returns it.
void SetAssociatedPipeline(vtkWeakPointer< vtkWebGPUComputePipeline > associatedPipeline)
Get/set the compute pipeline to which this compute pass belongs to.
wgpu::BindGroupEntry CreateBindGroupEntry(uint32_t binding, wgpu::TextureView textureView)
Given a texture view, creates the associated bind group entry that will be used when creating the bin...
void WebGPUDispatch(unsigned int groupsX, unsigned int groupsY, unsigned int groupsZ)
Encodes the compute pass and dispatches the workgroups.
wgpu::ComputePassEncoder CreateComputePassEncoder(const wgpu::CommandEncoder &commandEncoder)
Creates a compute pass encoder from a command encoder.
void SetParentPass(vtkWeakPointer< vtkWebGPUComputePass > parentPass)
Sets the parent pass of this internals class.
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...
A compute pipeline is the orchestrator of a collection of compute passes.
Create a webgpu device for use in rendering and compute pipelines.
Internal implementation details of vtkWebGPUPointCloudMapper.
int vtkIdType
Definition vtkType.h:315