VTK  9.7.1
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"
11 #include "vtkWebGPUConfiguration.h"
12 #include "vtk_wgpu.h" // for webgpu
13 
14 #include <unordered_map>
15 #include <unordered_set>
16 
18 
19 VTK_ABI_NAMESPACE_BEGIN
20 
26 class VTKRENDERINGWEBGPU_EXPORT vtkWebGPUComputePassInternals : public vtkObject
27 {
28 public:
31 
32  void PrintSelf(ostream& os, vtkIndent indent) override;
33 
37  void SetParentPass(vtkWeakPointer<vtkWebGPUComputePass> parentPass);
38 
40 
44  void SetWGPUConfiguration(vtkWebGPUConfiguration* config);
45  vtkGetSmartPointerMacro(WGPUConfiguration, vtkWebGPUConfiguration);
47 
49 
52  vtkWeakPointer<vtkWebGPUComputePipeline> GetAssociatedPipeline();
53  void SetAssociatedPipeline(vtkWeakPointer<vtkWebGPUComputePipeline> associatedPipeline);
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,
86 
91  wgpu::BindGroupEntry CreateBindGroupEntry(uint32_t binding, wgpu::TextureView textureView);
92 
96  void CreateShaderModule();
97 
102  void CreateBindGroupsAndLayouts();
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 
146  bool CheckBufferCorrectness(vtkSmartPointer<vtkWebGPUComputeBuffer> buffer);
147 
155  void RecreateBufferBindGroup(int bufferIndex);
156 
161  void RegisterBufferToPipeline(
162  vtkSmartPointer<vtkWebGPUComputeBuffer> buffer, wgpu::Buffer wgpuBuffer);
163 
165 
172  bool GetRegisteredBufferFromPipeline(
173  vtkSmartPointer<vtkWebGPUComputeBuffer> buffer, wgpu::Buffer& wgpuBuffer);
174 
175  bool GetRegisteredTextureFromPipeline(
176  vtkSmartPointer<vtkWebGPUComputeTexture> texture, wgpu::Texture& wgpuTexture);
178 
183  wgpu::Buffer GetWGPUBuffer(std::size_t bufferIndex);
184 
189  void RegisterTextureToPipeline(
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 
236  void UpdateComputeTextureAndViews(
237  vtkSmartPointer<vtkWebGPUComputeTexture> texture, wgpu::Texture newWgpuTexture);
238 
243  void RecreateTextureBindGroup(int textureIndex);
244 
249  void SetupRenderBuffer(vtkSmartPointer<vtkWebGPUComputeRenderBuffer> renderBuffer);
250 
259  void RecreateRenderTexture(vtkSmartPointer<vtkWebGPUComputeRenderTexture> renderTexture);
260 
264  void CreateWebGPUComputePipeline();
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 
302  void ReleaseResources();
303 
304  // Compute pass whose internals this class represents
306 
307 protected:
308  vtkWebGPUComputePassInternals() = default;
309  ~vtkWebGPUComputePassInternals() override;
310 
311 private:
312  friend class vtkWebGPUComputePass;
316  // For the mapper to be able to access the wgpu::Buffer objects for use in a render pipeline
318 
320  void operator=(const vtkWebGPUComputePassInternals&) = delete;
321 
326  bool Initialized = false;
327  // Whether or not the binds group / layouts have changed since they were last created and so they
328  // need to be recreated. Defaults to true since the bind group / layouts are initially not created
329  // so they are invalid.
330  bool BindGroupOrLayoutsInvalidated = true;
331 
332  // Device of the compute pipeline this pass belongs to. Used to submit commands.
333  vtkSmartPointer<vtkWebGPUConfiguration> WGPUConfiguration;
334 
335  // The compute pipeline this compute pass belongs to.
336  vtkWeakPointer<vtkWebGPUComputePipeline> AssociatedPipeline;
337 
338  wgpu::ShaderModule ShaderModule;
339  // List of the bind groups, used to set the bind groups of the compute pass at each dispatch
340  std::vector<wgpu::BindGroup> BindGroups;
341  // Maps a bind group index to to the list of bind group entries for this group. These
342  // entries will be used at the creation of the bind groups
343  std::unordered_map<int, std::vector<wgpu::BindGroupEntry>> BindGroupEntries;
344  std::vector<wgpu::BindGroupLayout> BindGroupLayouts;
345  // Maps a bind group index to to the list of bind group layout entries for this group.
346  // These layout entries will be used at the creation of the bind group layouts
347  std::unordered_map<int, std::vector<wgpu::BindGroupLayoutEntry>> BindGroupLayoutEntries;
348  // WebGPU compute shader pipeline
349  wgpu::ComputePipeline ComputePipeline;
350 
351  // Object responsible for the management (creation, re-creatioin, deletion, ...) of textures and
352  // their texture views
354 
355  // Object responsible for the management (creation, re-creatioin, deletion, ...) of buffers
357 };
358 
359 VTK_ABI_NAMESPACE_END
360 
361 #endif
Internals of the vtkWebGPUComputePass.
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.
Hold a reference to a vtkObjectBase instance.
Definition: vtkMeta.h:23
int vtkIdType
Definition: vtkType.h:363
Internal implementation details of vtkWebGPUPointCloudMapper.
a simple class to control print indentation
Definition: vtkIndent.h:28
This class manages the creation/deletion/recreation/resizing/updating of compute buffers used by a co...
vtkWeakPointer< vtkWebGPUComputePass > ParentPass
Create a webgpu device for use in rendering and compute pipelines.
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...
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
A compute pass is an abstraction for offloading computation from the CPU onto the GPU using WebGPU co...