VTK  9.7.20261206
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
8#include "vtkObject.h"
9#include "vtkSmartPointer.h" // for smart pointers
10#include "vtkWeakPointer.h" // for the weak pointer of the parent compute pass
11#include "vtkWebGPUComputePass.h" // for enum
12#include "vtkWebGPUComputeRenderTexture.h" // for compute render textures
13#include "vtkWebGPUComputeTexture.h" // for compute textures
14#include "vtkWebGPUComputeTextureView.h" // for compute texture views
15#include "vtkWebGPUConfiguration.h" // for ivar
16#include "vtk_wgpu.h"
17
18#include <cstddef>
19#include <unordered_map>
20#include <unordered_set>
21
22VTK_ABI_NAMESPACE_BEGIN
23
25
33{
34public:
37
44 vtkSetSmartPointerMacro(ParentPassWGPUConfiguration, vtkWebGPUConfiguration);
45
50
58 bool CheckTextureIndex(std::size_t textureIndex, const std::string& callerFunctionName);
59
68 bool CheckTextureViewIndex(std::size_t textureViewIndex, const std::string& callerFunctionName);
69
74
79
83 bool CheckParentComputePass(const std::string& callerFunctionName);
84
88 void RecreateTexture(std::size_t textureIndex);
89
97
105
112 void RecreateComputeTexture(std::size_t textureIndex);
113
121 void RecreateTextureViews(std::size_t textureIndex);
122
127 void RecreateTextureView(std::size_t textureViewIndex);
128
133 vtkWebGPU::TextureView CreateWebGPUTextureView(
134 vtkSmartPointer<vtkWebGPUComputeTextureView> textureView, vtkWebGPU::Texture wgpuTexture);
135
147 vtkSmartPointer<vtkWebGPUComputeTexture> texture, vtkWebGPU::Texture newWgpuTexture);
148
160
168
173
179
189
193 void DeleteTextureViews(std::size_t textureIndex);
194
210 void RebindTextureView(std::size_t group, uint32_t binding, std::size_t textureViewIndex);
211
212 /*
213 * This function maps the buffer, making it accessible to the CPU. This is
214 * an asynchronous operation, meaning that the given callback will be called
215 * when the mapping is done.
216 *
217 * The buffer data can then be read from the callback and stored
218 * in a buffer (std::vector<>, vtkDataArray, ...) passed in via the userdata pointer for example
219 */
220 void ReadTextureFromGPU(std::size_t textureIndex, int mipLevel,
221 vtkWebGPUComputePass::TextureMapAsyncCallback callback, void* userdata);
222
226 void WriteTexture(std::size_t textureIndex, const void* bytes, std::size_t numBytes)
227 {
228 if (!CheckTextureIndex(textureIndex, "UpdateTextureData"))
229 {
230 return;
231 }
232
233 const auto wgpuTexture = this->WebGPUTextures[textureIndex];
234 auto& texture = this->Textures[textureIndex];
235
236 if (numBytes > static_cast<std::size_t>(texture->GetByteSize()))
237 {
238 vtkLog(ERROR,
239 "The given data is larger than what the texture \""
240 << texture->GetLabel() << "\" with byte size: " << texture->GetByteSize());
241
242 return;
243 }
244
245 // Uploading from std::vector or vtkDataArray if one of the two is present
246 const std::string textureLabel = texture->GetLabel();
247 const char* textureLabelCStr = textureLabel.c_str();
248 const uint32_t bytesPerRow = texture->GetBytesPerPixel() * texture->GetWidth();
249 this->ParentPassWGPUConfiguration->WriteTexture(wgpuTexture.Get(), bytesPerRow,
250 static_cast<uint32_t>(numBytes), bytes,
251 /*srcOffset=*/0, /*dstOffset=*/{ 0, 0, 0 }, /*dstMipLevel=*/0,
252 /*textureLabel=*/textureLabelCStr);
253 }
254
259
264 static WGPUTextureDimension ComputeTextureDimensionToWebGPU(
266
276 static WGPUTextureViewDimension ComputeTextureDimensionToViewDimension(
278
283 static WGPUTextureFormat ComputeTextureFormatToWebGPU(
285
292 static WGPUTextureUsage ComputeTextureModeToUsage(
293 vtkWebGPUComputeTexture::TextureMode mode, const std::string& textureLabel);
294
300 static WGPUStorageTextureAccess ComputeTextureModeToShaderStorage(
301 vtkWebGPUComputeTexture::TextureMode mode, const std::string& textureLabel);
302
308 static WGPUStorageTextureAccess ComputeTextureViewModeToShaderStorage(
309 vtkWebGPUComputeTextureView::TextureViewMode mode, const std::string& textureViewLabel);
310
315 static WGPUTextureSampleType ComputeTextureSampleTypeToWebGPU(
317
322 static WGPUTextureAspect ComputeTextureViewAspectToWebGPU(
324
325protected:
328
329private:
331
334 void operator=(const vtkWebGPUComputePassTextureStorageInternals&) = delete;
335
336 // Compute pass that uses this texture storage
337 vtkWeakPointer<vtkWebGPUComputePass> ParentComputePass = nullptr;
338 // Device of the parent compute pass that is used when creating textures and texture views
339 vtkSmartPointer<vtkWebGPUConfiguration> ParentPassWGPUConfiguration;
340
341 // Compute textures of the storage
342 std::vector<vtkSmartPointer<vtkWebGPUComputeTexture>> Textures;
343 // Compute render textures of this the storage
344 std::vector<vtkSmartPointer<vtkWebGPUComputeRenderTexture>> RenderTextures;
345 // Maps the compute render texture to the internal vtkWebGPU::Texture that they use
346 std::unordered_map<vtkSmartPointer<vtkWebGPUComputeRenderTexture>, vtkWebGPU::Texture>
347 RenderTexturesToWebGPUTexture;
348 // WebGPU textures associated with the compute texture in the same order
349 std::vector<vtkWebGPU::Texture> WebGPUTextures;
350
351 // A map of the compute textures associated with all the texture views of it
352 // that have been created
353 std::unordered_map<vtkSmartPointer<vtkWebGPUComputeTexture>,
354 std::unordered_set<vtkSmartPointer<vtkWebGPUComputeTextureView>>>
355 ComputeTextureToViews;
356 // List of the texture views added by the user. Can be used to find a texture
357 // view from its index (indices which the user manipulates)
358 std::vector<vtkSmartPointer<vtkWebGPUComputeTextureView>> TextureViews;
359 // Compute textures views mapped to their WebGPU textures views
360 std::unordered_map<vtkSmartPointer<vtkWebGPUComputeTextureView>, vtkWebGPU::TextureView>
361 TextureViewsToWebGPUTextureViews;
362};
363
364VTK_ABI_NAMESPACE_END
365
366#endif
Hold a reference to a vtkObjectBase instance.
a weak reference to a vtkObject.
bool CheckTextureViewCorrectness(vtkWebGPUComputeTextureView *textureView)
Makes sure the texture view is correct with regards to its properties (binding, group,...
vtkSmartPointer< vtkWebGPUComputeTextureView > GetTextureView(std::size_t textureViewIndex)
Retrieves the texture view associated with the given texture view index.
int AddTextureView(vtkSmartPointer< vtkWebGPUComputeTextureView > textureView)
Adds a texture view to the compute pass and returns its index.
void ReadTextureFromGPU(std::size_t textureIndex, int mipLevel, vtkWebGPUComputePass::TextureMapAsyncCallback callback, void *userdata)
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 RecreateTextureViews(std::size_t textureIndex)
Recreates all the texture views of a texture given its index.
vtkSmartPointer< vtkWebGPUComputeTexture > GetComputeTexture(std::size_t textureIndex)
Retrieves the compute texture associated with the given texture index.
static WGPUTextureDimension ComputeTextureDimensionToWebGPU(vtkWebGPUComputeTexture::TextureDimension dimension)
Internal method used to convert the user friendly Dimension enum to its WGPUTextureDimension equivale...
static WGPUTextureSampleType ComputeTextureSampleTypeToWebGPU(vtkWebGPUComputeTexture::TextureSampleType sampleType)
Internal method used to convert the user friendly TextureSampleType enum to its WGPUTextureSampleType...
static vtkWebGPUComputePassTextureStorageInternals * New()
static WGPUTextureViewDimension ComputeTextureDimensionToViewDimension(vtkWebGPUComputeTexture::TextureDimension dimension)
This function does a simple mapping between the dimension of the texture (vtkWebGPUComputeTexture::Te...
void UpdateComputeTextureAndViews(vtkSmartPointer< vtkWebGPUComputeTexture > texture, vtkWebGPU::Texture newWgpuTexture)
Makes sure that the compute texture given in parameter internally points to the given newWgpuTexture.
static WGPUStorageTextureAccess ComputeTextureViewModeToShaderStorage(vtkWebGPUComputeTextureView::TextureViewMode mode, const std::string &textureViewLabel)
Internal method used to get the StorageTextureAccess mode associated with a TextureViewMode.
static WGPUTextureAspect ComputeTextureViewAspectToWebGPU(vtkWebGPUComputeTextureView::TextureViewAspect aspect)
Internal method used to convert the user friendly TextureAspect enum to its WGPUTextureAspect equival...
void ReleaseResources()
Releases the textures & resources held by this texture storage.
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...
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.
static WGPUStorageTextureAccess ComputeTextureModeToShaderStorage(vtkWebGPUComputeTexture::TextureMode mode, const std::string &textureLabel)
Internal method used to get the StorageTextureAccess mode associated with a TextureMode.
bool CheckTextureCorrectness(vtkWebGPUComputeTexture *texture)
Makes sure the texture is correct with regards to its properties (size, ...).
void DeleteTextureViews(std::size_t textureIndex)
Deletes all the texture views of a given texture (given by its index).
void SetComputePass(vtkWeakPointer< vtkWebGPUComputePass > parentComputePass)
Sets the compute pass that uses the textures and texture views used by this storage.
void RecreateRenderTexture(vtkSmartPointer< vtkWebGPUComputeRenderTexture > renderTexture)
Recreates a render texture given a new textureView and possibly new parameters as specified in the 'r...
static WGPUTextureUsage ComputeTextureModeToUsage(vtkWebGPUComputeTexture::TextureMode mode, const std::string &textureLabel)
Internal method used to convert the user friendly TextureMode enum to its WGPUTextureUsage equivalent...
bool CheckTextureIndex(std::size_t textureIndex, const std::string &callerFunctionName)
Checks if a given index is suitable for indexing this->Textures.
static WGPUTextureFormat ComputeTextureFormatToWebGPU(vtkWebGPUComputeTexture::TextureFormat format)
Internal method used to convert the user friendly TextureFormat enum to its WGPUTextureFormat equival...
int AddTexture(vtkSmartPointer< vtkWebGPUComputeTexture > texture)
Adds a texture to the storage and upload its data to the device.
void RecreateTextureView(std::size_t textureViewIndex)
Recreates a compute texture view.
void RecreateComputeTexture(std::size_t textureIndex)
Recreates a compute texture.
void RecreateTexture(std::size_t textureIndex)
Destroys and recreates the texture with the given index.
bool CheckTextureViewIndex(std::size_t textureViewIndex, const std::string &callerFunctionName)
Checks if a given index is suitable for indexing this->TextureViews.
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 ...
bool CheckParentComputePass(const std::string &callerFunctionName)
Checks whether or not the associated ParentComputePass and ParentPassDevice are non-null.
A compute pass is an abstraction for offloading computation from the CPU onto the GPU using WebGPU co...
std::function< void(const void *, int, void *)> TextureMapAsyncCallback
Represents the set of parameters that will be used to create a compute shader texture on the device w...
Create a webgpu device for use in rendering and compute pipelines.
TextureDimension
How the texture data is arranged.
TextureFormat
RGBA8_UNORM: Uses RGB + alpha.
TextureSampleType
Determines what kind of value is returned when reading from the texture in the compute shader.
TextureMode
How will the texture be used by the shader.
TextureViewAspect
What will the shader sample from the texture when calling a sampling function.
TextureViewMode
The mode of the texture view to define what operations will be doable on the texture in the shader.
#define vtkLog(verbosity_name, x)
Add to log given the verbosity level.
Definition vtkLogger.h:513
Reference-counted ownership for WebGPU C API handles.