VTK  9.7.1
vtkWebGPUComputePassTextureStorageInternals.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 vtkWebGPUComputePassTextureStorageInternals_h
5 #define vtkWebGPUComputePassTextureStorageInternals_h
6 
7 #include "vtkObject.h"
8 #include "vtkSmartPointer.h" // for smart pointers
9 #include "vtkWeakPointer.h" // for the weak pointer of the parent compute pass
10 #include "vtkWebGPUComputePass.h" // for enum
11 #include "vtkWebGPUComputeRenderTexture.h" // for compute render textures
12 #include "vtkWebGPUComputeTexture.h" // for compute textures
13 #include "vtkWebGPUComputeTextureView.h" // for compute texture views
14 #include "vtkWebGPUConfiguration.h" // for ivar
15 #include "vtk_wgpu.h" // for webgpu
16 
17 #include <cstddef>
18 #include <unordered_map>
19 #include <unordered_set>
20 
21 VTK_ABI_NAMESPACE_BEGIN
22 
24 
32 {
33 public:
36 
43  vtkSetSmartPointerMacro(ParentPassWGPUConfiguration, vtkWebGPUConfiguration);
44 
49 
57  bool CheckTextureIndex(std::size_t textureIndex, const std::string& callerFunctionName);
58 
67  bool CheckTextureViewIndex(std::size_t textureViewIndex, const std::string& callerFunctionName);
68 
73 
78 
82  bool CheckParentComputePass(const std::string& callerFunctionName);
83 
87  void RecreateTexture(std::size_t textureIndex);
88 
96 
103  vtkSmartPointer<vtkWebGPUComputeTextureView> GetTextureView(std::size_t textureViewIndex);
104 
111  void RecreateComputeTexture(std::size_t textureIndex);
112 
120  void RecreateTextureViews(std::size_t textureIndex);
121 
126  void RecreateTextureView(std::size_t textureViewIndex);
127 
132  wgpu::TextureView CreateWebGPUTextureView(
133  vtkSmartPointer<vtkWebGPUComputeTextureView> textureView, wgpu::Texture wgpuTexture);
134 
146  vtkSmartPointer<vtkWebGPUComputeTexture> texture, wgpu::Texture newWgpuTexture);
147 
159 
167 
172 
178 
188 
192  void DeleteTextureViews(std::size_t textureIndex);
193 
209  void RebindTextureView(std::size_t group, uint32_t binding, std::size_t textureViewIndex);
210 
211  /*
212  * This function maps the buffer, making it accessible to the CPU. This is
213  * an asynchronous operation, meaning that the given callback will be called
214  * when the mapping is done.
215  *
216  * The buffer data can then be read from the callback and stored
217  * in a buffer (std::vector<>, vtkDataArray, ...) passed in via the userdata pointer for example
218  */
219  void ReadTextureFromGPU(std::size_t textureIndex, int mipLevel,
220  vtkWebGPUComputePass::TextureMapAsyncCallback callback, void* userdata);
221 
225  void WriteTexture(std::size_t textureIndex, const void* bytes, std::size_t numBytes)
226  {
227  if (!CheckTextureIndex(textureIndex, "UpdateTextureData"))
228  {
229  return;
230  }
231 
232  const auto wgpuTexture = this->WebGPUTextures[textureIndex];
233  auto& texture = this->Textures[textureIndex];
234 
235  if (numBytes > static_cast<std::size_t>(texture->GetByteSize()))
236  {
237  vtkLog(ERROR,
238  "The given data is larger than what the texture \""
239  << texture->GetLabel() << "\" with byte size: " << texture->GetByteSize());
240 
241  return;
242  }
243 
244  // Uploading from std::vector or vtkDataArray if one of the two is present
245  const std::string textureLabel = texture->GetLabel();
246  const char* textureLabelCStr = textureLabel.c_str();
247  const uint32_t bytesPerRow = texture->GetBytesPerPixel() * texture->GetWidth();
248  this->ParentPassWGPUConfiguration->WriteTexture(wgpuTexture, bytesPerRow, numBytes, bytes,
249  /*srcOffset=*/0, /*dstOffset=*/{ 0, 0, 0 }, /*dstMipLevel=*/0,
250  /*textureLabel=*/textureLabelCStr);
251  }
252 
256  void ReleaseResources();
257 
262  static wgpu::TextureDimension ComputeTextureDimensionToWebGPU(
264 
274  static wgpu::TextureViewDimension ComputeTextureDimensionToViewDimension(
276 
281  static wgpu::TextureFormat ComputeTextureFormatToWebGPU(
283 
290  static wgpu::TextureUsage ComputeTextureModeToUsage(
292 
298  static wgpu::StorageTextureAccess ComputeTextureModeToShaderStorage(
300 
306  static wgpu::StorageTextureAccess ComputeTextureViewModeToShaderStorage(
308 
313  static wgpu::TextureSampleType ComputeTextureSampleTypeToWebGPU(
315 
320  static wgpu::TextureAspect ComputeTextureViewAspectToWebGPU(
322 
323 protected:
326 
327 private:
329 
332  void operator=(const vtkWebGPUComputePassTextureStorageInternals&) = delete;
333 
334  // Compute pass that uses this texture storage
335  vtkWeakPointer<vtkWebGPUComputePass> ParentComputePass = nullptr;
336  // Device of the parent compute pass that is used when creating textures and texture views
337  vtkSmartPointer<vtkWebGPUConfiguration> ParentPassWGPUConfiguration;
338 
339  // Compute textures of the storage
340  std::vector<vtkSmartPointer<vtkWebGPUComputeTexture>> Textures;
341  // Compute render textures of this the storage
342  std::vector<vtkSmartPointer<vtkWebGPUComputeRenderTexture>> RenderTextures;
343  // Maps the compute render texture to the internal wgpu::Texture that they use
344  std::unordered_map<vtkSmartPointer<vtkWebGPUComputeRenderTexture>, wgpu::Texture>
345  RenderTexturesToWebGPUTexture;
346  // WebGPU textures associated with the compute texture in the same order
347  std::vector<wgpu::Texture> WebGPUTextures;
348 
349  // A map of the compute textures associated with all the texture views of it
350  // that have been created
351  std::unordered_map<vtkSmartPointer<vtkWebGPUComputeTexture>,
352  std::unordered_set<vtkSmartPointer<vtkWebGPUComputeTextureView>>>
353  ComputeTextureToViews;
354  // List of the texture views added by the user. Can be used to find a texture
355  // view from its index (indices which the user manipulates)
356  std::vector<vtkSmartPointer<vtkWebGPUComputeTextureView>> TextureViews;
357  // Compute textures views mapped to their WebGPU textures views
358  std::unordered_map<vtkSmartPointer<vtkWebGPUComputeTextureView>, wgpu::TextureView>
359  TextureViewsToWebGPUTextureViews;
360 };
361 
362 VTK_ABI_NAMESPACE_END
363 
364 #endif
static wgpu::TextureFormat ComputeTextureFormatToWebGPU(vtkWebGPUComputeTexture::TextureFormat format)
Internal method used to convert the user friendly TextureFormat enum to its wgpu::TextureFormat equiv...
void UpdateComputeTextureAndViews(vtkSmartPointer< vtkWebGPUComputeTexture > texture, wgpu::Texture newWgpuTexture)
Makes sure that the compute texture given in parameter internally points to the given newWgpuTexture...
void RecreateComputeTexture(std::size_t textureIndex)
Recreates a compute texture.
Internals of the vtkWebGPUComputePass.
std::function< void(const void *, int, void *)> TextureMapAsyncCallback
void ReleaseResources()
Releases the textures & resources held by this texture storage.
static wgpu::TextureAspect ComputeTextureViewAspectToWebGPU(vtkWebGPUComputeTextureView::TextureViewAspect aspect)
Internal method used to convert the user friendly TextureAspect enum to its wgpu::TextureAspect equiv...
abstract base class for most VTK objects
Definition: vtkObject.h:52
void SetComputePass(vtkWeakPointer< vtkWebGPUComputePass > parentComputePass)
Sets the compute pass that uses the textures and texture views used by this storage.
void RecreateTexture(std::size_t textureIndex)
Destroys and recreates the texture with the given index.
TextureMode
How will the texture be used by the shader.
static wgpu::TextureSampleType ComputeTextureSampleTypeToWebGPU(vtkWebGPUComputeTexture::TextureSampleType sampleType)
Internal method used to convert the user friendly TextureSampleType enum to its wgpu::TextureSampleTy...
TextureViewAspect
What will the shader sample from the texture when calling a sampling function.
int AddTexture(vtkSmartPointer< vtkWebGPUComputeTexture > texture)
Adds a texture to the storage and upload its data to the device.
static vtkWebGPUComputePassTextureStorageInternals * New()
int AddTextureView(vtkSmartPointer< vtkWebGPUComputeTextureView > textureView)
Adds a texture view to the compute pass and returns its index.
void RecreateRenderTexture(vtkSmartPointer< vtkWebGPUComputeRenderTexture > renderTexture)
Recreates a render texture given a new textureView and possibly new parameters as specified in the 'r...
vtkSmartPointer< vtkWebGPUComputeTextureView > GetTextureView(std::size_t textureViewIndex)
Retrieves the texture view associated with the given texture view index.
Hold a reference to a vtkObjectBase instance.
Definition: vtkMeta.h:23
bool CheckParentComputePass(const std::string &callerFunctionName)
Checks whether or not the associated ParentComputePass and ParentPassDevice are non-null.
void RecreateTextureViews(std::size_t textureIndex)
Recreates all the texture views of a texture given its index.
void WriteTexture(wgpu::Texture texture, uint32_t bytesPerRow, uint32_t byteSize, const void *data, uint32_t srcOffset=0, wgpu::Origin3D dstOffset={0, 0, 0}, uint32_t dstMipLevel=0, const char *description=nullptr)
Upload byteSize of data from the data pointer to the given texture, assuming bytesPerRow bytes of dat...
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...
bool CheckTextureIndex(std::size_t textureIndex, const std::string &callerFunctionName)
Checks if a given index is suitable for indexing this->Textures.
bool CheckTextureCorrectness(vtkWebGPUComputeTexture *texture)
Makes sure the texture is correct with regards to its properties (size, ...)
static wgpu::StorageTextureAccess ComputeTextureModeToShaderStorage(vtkWebGPUComputeTexture::TextureMode mode, const std::string &textureLabel)
Internal method used to get the StorageTextureAccess mode associated with a TextureMode.
void RebindTextureView(std::size_t group, uint32_t binding, std::size_t textureViewIndex)
This function allows the usage of multiple texture views on a single binding point (group / binding c...
#define vtkLog(verbosity_name, x)
Add to log given the verbosity level.
Definition: vtkLogger.h:494
static wgpu::TextureUsage ComputeTextureModeToUsage(vtkWebGPUComputeTexture::TextureMode mode, const std::string &textureLabel)
Internal method used to convert the user friendly TextureMode enum to its wgpu::TextureUsage equivale...
vtkSmartPointer< vtkWebGPUComputeTextureView > CreateTextureView(std::size_t textureIndex)
Returns a new texture view on the given texture (given by the index) that can be configured and then ...
TextureFormat
RGBA8_UNORM: Uses RGB + alpha.
static wgpu::StorageTextureAccess ComputeTextureViewModeToShaderStorage(vtkWebGPUComputeTextureView::TextureViewMode mode, const std::string &textureViewLabel)
Internal method used to get the StorageTextureAccess mode associated with a TextureViewMode.
vtkSmartPointer< vtkWebGPUComputeTexture > GetComputeTexture(std::size_t textureIndex)
Retrieves the compute texture associated with the given texture index.
static wgpu::TextureDimension ComputeTextureDimensionToWebGPU(vtkWebGPUComputeTexture::TextureDimension dimension)
Internal method used to convert the user friendly Dimension enum to its wgpu::TextureDimension equiva...
TextureSampleType
Determines what kind of value is returned when reading from the texture in the compute shader...
TextureDimension
How the texture data is arranged.
bool CheckTextureViewIndex(std::size_t textureViewIndex, const std::string &callerFunctionName)
Checks if a given index is suitable for indexing this->TextureViews.
Create a webgpu device for use in rendering and compute pipelines.
Represents the set of parameters that will be used to create a compute shader texture on the device w...
This class manages the creation/deletion/recreation/ of compute textures used by a compute pass...
void ReadTextureFromGPU(std::size_t textureIndex, int mipLevel, vtkWebGPUComputePass::TextureMapAsyncCallback callback, void *userdata)
TextureViewMode
The mode of the texture view to define what operations will be doable on the texture in the shader...
static wgpu::TextureViewDimension ComputeTextureDimensionToViewDimension(vtkWebGPUComputeTexture::TextureDimension dimension)
This function does a simple mapping between the dimension of the texture (vtkWebGPUComputeTexture::Te...
bool CheckTextureViewCorrectness(vtkWebGPUComputeTextureView *textureView)
Makes sure the texture view is correct with regards to its properties (binding, group, ...)
void WriteTexture(std::size_t textureIndex, const void *bytes, std::size_t numBytes)
Uploads the given data to the texture starting at pixel (0, 0)
int AddRenderTexture(vtkSmartPointer< vtkWebGPUComputeRenderTexture > renderTexture)
Adds a render texture to the storage.
void RecreateTextureView(std::size_t textureViewIndex)
Recreates a compute texture view.
void DeleteTextureViews(std::size_t textureIndex)
Deletes all the texture views of a given texture (given by its index)
A compute pass is an abstraction for offloading computation from the CPU onto the GPU using WebGPU co...