VTK  9.7.20260913
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
10#include "vtkObject.h"
11#include "vtkSmartPointer.h"
13#include "vtk_wgpu.h"
14
15#include <unordered_map>
16#include <unordered_set>
17
19
20VTK_ABI_NAMESPACE_BEGIN
21
27class VTKRENDERINGWEBGPU_EXPORT vtkWebGPUComputePassInternals : public vtkObject
28{
29public:
32
33 void PrintSelf(ostream& os, vtkIndent indent) override;
34
39
41
46 vtkGetSmartPointerMacro(WGPUConfiguration, vtkWebGPUConfiguration);
48
50
56
61 WGPUBindGroupLayoutEntry CreateBindGroupLayoutEntry(
62 uint32_t binding, vtkWebGPUComputeBuffer::BufferMode mode);
63
67 WGPUBindGroupLayoutEntry CreateBindGroupLayoutEntry(uint32_t binding,
70
77 WGPUBindGroupLayoutEntry CreateBindGroupLayoutEntry(uint32_t binding,
79 WGPUTextureViewDimension textureViewDimension);
80
85 WGPUBindGroupEntry CreateBindGroupEntry(
86 WGPUBuffer buffer, uint32_t binding, vtkWebGPUComputeBuffer::BufferMode mode, uint32_t offset);
87
92 WGPUBindGroupEntry CreateBindGroupEntry(uint32_t binding, WGPUTextureView textureView);
93
98
104
109 static vtkWebGPU::BindGroupLayout CreateBindGroupLayout(
110 const WGPUDevice& device, const std::vector<WGPUBindGroupLayoutEntry>& layoutEntries);
111
115 std::vector<WGPUBindGroupEntry> CreateBindGroupEntries(
116 const std::vector<vtkWebGPUComputeBuffer*>& buffers);
117
127 bool CheckBufferIndex(int bufferIndex, const std::string& callerFunctionName);
128
134 bool CheckTextureIndex(int textureIndex, const std::string& callerFunctionName);
135
141 bool CheckTextureViewIndex(int textureViewIndex, const std::string& callerFunctionName);
142
148
156 void RecreateBufferBindGroup(int bufferIndex);
157
163 vtkSmartPointer<vtkWebGPUComputeBuffer> buffer, vtkWebGPU::Buffer wgpuBuffer);
164
166
174 vtkSmartPointer<vtkWebGPUComputeBuffer> buffer, vtkWebGPU::Buffer& wgpuBuffer);
175
177 vtkSmartPointer<vtkWebGPUComputeTexture> texture, vtkWebGPU::Texture& wgpuTexture);
179
184 vtkWebGPU::Buffer GetWGPUBuffer(std::size_t bufferIndex);
185
191 vtkSmartPointer<vtkWebGPUComputeTexture> texture, vtkWebGPU::Texture wgpuTexture);
192
197 void RecreateBuffer(int bufferIndex, vtkIdType newByteSize);
198
202 void RecreateTexture(int textureIndex);
203
211 void RecreateTextureViews(int textureIndex);
212
217 vtkWebGPU::TextureView CreateWebGPUTextureView(
218 vtkSmartPointer<vtkWebGPUComputeTextureView> textureView, vtkWebGPU::Texture wgpuTexture);
219
226 vtkSmartPointer<vtkWebGPUComputeBuffer> buffer, vtkWebGPU::Buffer wgpuBuffer);
227
239 vtkSmartPointer<vtkWebGPUComputeTexture> texture, vtkWebGPU::Texture newWgpuTexture);
240
245 void RecreateTextureBindGroup(int textureIndex);
246
252
262
267
275 vtkWebGPU::PipelineLayout CreateWebGPUComputePipelineLayout();
276
280 WGPUCommandEncoder CreateCommandEncoder();
281
285 WGPUComputePassEncoder CreateComputePassEncoder(const WGPUCommandEncoder& commandEncoder);
286
293 void WebGPUDispatch(unsigned int groupsX, unsigned int groupsY, unsigned int groupsZ);
294
299 void SubmitCommandEncoderToQueue(const WGPUCommandEncoder& commandEncoder);
300
305
306 // Compute pass whose internals this class represents
308
309protected:
312
313private:
318 // For the mapper to be able to access the vtkWebGPU::Buffer objects for use in a render pipeline
320
322 void operator=(const vtkWebGPUComputePassInternals&) = delete;
323
328 bool Initialized = false;
329 // Whether or not the binds group / layouts have changed since they were last created and so they
330 // need to be recreated. Defaults to true since the bind group / layouts are initially not created
331 // so they are invalid.
332 bool BindGroupOrLayoutsInvalidated = true;
333
334 // Device of the compute pipeline this pass belongs to. Used to submit commands.
336
337 // The compute pipeline this compute pass belongs to.
339
340 vtkWebGPU::ShaderModule ShaderModule;
341 // List of the bind groups, used to set the bind groups of the compute pass at each dispatch
342 std::vector<vtkWebGPU::BindGroup> BindGroups;
343 // Maps a bind group index to to the list of bind group entries for this group. These
344 // entries will be used at the creation of the bind groups
345 std::unordered_map<int, std::vector<WGPUBindGroupEntry>> BindGroupEntries;
346 std::vector<vtkWebGPU::BindGroupLayout> BindGroupLayouts;
347 // Maps a bind group index to to the list of bind group layout entries for this group.
348 // These layout entries will be used at the creation of the bind group layouts
349 std::unordered_map<int, std::vector<WGPUBindGroupLayoutEntry>> BindGroupLayoutEntries;
350 // WebGPU compute shader pipeline
351 vtkWebGPU::ComputePipeline ComputePipeline;
352
353 // Object responsible for the management (creation, re-creatioin, deletion, ...) of textures and
354 // their texture views
356
357 // Object responsible for the management (creation, re-creatioin, deletion, ...) of buffers
359};
360
361VTK_ABI_NAMESPACE_END
362
363#endif
a simple class to control print indentation
Definition vtkIndent.h:108
Hold a reference to a vtkObjectBase instance.
a weak reference to a vtkObject.
void RecreateRenderTexture(vtkSmartPointer< vtkWebGPUComputeRenderTexture > renderTexture)
Recreates a render texture given a new textureView and possibly new parameters as specified in the 'r...
bool GetRegisteredBufferFromPipeline(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer, vtkWebGPU::Buffer &wgpuBuffer)
Checks whether the given compute buffer/texture has already been registered in the pipeline associate...
void UpdateWebGPUBuffer(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer, vtkWebGPU::Buffer wgpuBuffer)
Updates the vtkWebGPU::Buffer reference that a compute buffer is associated to.
vtkWeakPointer< vtkWebGPUComputePipeline > GetAssociatedPipeline()
Get/set the compute pipeline to which this compute pass belongs to.
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.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
bool GetRegisteredTextureFromPipeline(vtkSmartPointer< vtkWebGPUComputeTexture > texture, vtkWebGPU::Texture &wgpuTexture)
Checks whether the given compute buffer/texture has already been registered in the pipeline associate...
vtkWeakPointer< vtkWebGPUComputePass > ParentPass
vtkWebGPU::Buffer GetWGPUBuffer(std::size_t bufferIndex)
Returns the vtkWebGPU::Buffer object for a buffer in this compute pass buffer storage given its index...
~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.
WGPUBindGroupLayoutEntry 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...
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 corresponds to a texture of this compute pass.
void UpdateComputeTextureAndViews(vtkSmartPointer< vtkWebGPUComputeTexture > texture, vtkWebGPU::Texture newWgpuTexture)
Makes sure that the compute texture given in parameter internally points to the given newWgpuTexture.
void RegisterBufferToPipeline(vtkSmartPointer< vtkWebGPUComputeBuffer > buffer, vtkWebGPU::Buffer wgpuBuffer)
Registers a buffer to the associated compute pipeline of this compute pass so that other compute pass...
vtkWebGPU::TextureView CreateWebGPUTextureView(vtkSmartPointer< vtkWebGPUComputeTextureView > textureView, vtkWebGPU::Texture wgpuTexture)
Utilitary method to create a vtkWebGPU::TextureView from a ComputeTextureView and the texture this vt...
void RecreateTextureBindGroup(int textureIndex)
After recreating a vtkWebGPU::Buffer, the bind group entry (and the bind group) will need to be updat...
static vtkWebGPU::BindGroupLayout CreateBindGroupLayout(const WGPUDevice &device, const std::vector< WGPUBindGroupLayoutEntry > &layoutEntries)
Creates the bind group layout of a given list of buffers (that must all belong to the same bind group...
WGPUCommandEncoder CreateCommandEncoder()
Creates and returns a command encoder.
WGPUBindGroupLayoutEntry CreateBindGroupLayoutEntry(uint32_t binding, vtkSmartPointer< vtkWebGPUComputeTexture > computeTexture, WGPUTextureViewDimension textureViewDimension)
Overload mainly used for creating the layout entry of render textures where we don't have a vtkWebGPU...
WGPUBindGroupEntry CreateBindGroupEntry(WGPUBuffer 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 ReleaseResources()
Releases the resources of this compute pass internals.
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 corresponds to a texture texture view of this compute pass.
void RecreateBuffer(int bufferIndex, vtkIdType newByteSize)
Destroys and recreates a buffer with the given newByteSize Only the vtkWebGPU::Buffer object is recre...
WGPUBindGroupLayoutEntry 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 SetWGPUConfiguration(vtkWebGPUConfiguration *config)
Get/set the device used by this compute pass (usually the device of the compute pipeline holding this...
vtkWebGPU::PipelineLayout CreateWebGPUComputePipelineLayout()
Creates the compute pipeline layout associated with the bind group layouts of this compute pass.
void RegisterTextureToPipeline(vtkSmartPointer< vtkWebGPUComputeTexture > texture, vtkWebGPU::Texture wgpuTexture)
Registers a texture to the associated compute pipeline of this compute pass so that other compute pas...
bool CheckBufferIndex(int bufferIndex, const std::string &callerFunctionName)
Checks if a given index is suitable for indexing Buffers.
static vtkWebGPUComputePassInternals * New()
void SetAssociatedPipeline(vtkWeakPointer< vtkWebGPUComputePipeline > associatedPipeline)
Get/set the compute pipeline to which this compute pass belongs to.
WGPUComputePassEncoder CreateComputePassEncoder(const WGPUCommandEncoder &commandEncoder)
Creates a compute pass encoder from a command encoder.
void WebGPUDispatch(unsigned int groupsX, unsigned int groupsY, unsigned int groupsZ)
Encodes the compute pass and dispatches the workgroups.
std::vector< WGPUBindGroupEntry > CreateBindGroupEntries(const std::vector< vtkWebGPUComputeBuffer * > &buffers)
Creates the bind group entries given a list of buffers.
void SetParentPass(vtkWeakPointer< vtkWebGPUComputePass > parentPass)
Sets the parent pass of this internals class.
void SubmitCommandEncoderToQueue(const WGPUCommandEncoder &commandEncoder)
Finishes the encoding of a command encoder and submits the resulting command buffer to the queue.
WGPUBindGroupEntry CreateBindGroupEntry(uint32_t binding, WGPUTextureView textureView)
Given a texture view, creates the associated bind group entry that will be used when creating the bin...
A compute pipeline is the orchestrator of a collection of compute passes.
Create a webgpu device for use in rendering and compute pipelines.
int vtkIdType
Definition vtkType.h:363
Reference-counted ownership for WebGPU C API handles.