VTK  9.6.20260213
vtkWebGPURenderWindow.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
15
16#ifndef vtkWebGPURenderWindow_h
17#define vtkWebGPURenderWindow_h
18
19#include "vtkRenderWindow.h"
20
21#include "vtkRenderingWebGPUModule.h" // for export macro
22#include "vtkWebGPUComputePipeline.h" // for the compute pipelines of this render window
23#include "vtkWebGPUComputeRenderTexture.h" // for compute render textures
24#include "vtkWebGPURenderPipelineCache.h" // for vtkWebGPURenderPipelineCache
25#include "vtkWebGPURenderTextureCache.h" // for texture cache
26#include "vtkWebGPUShaderDatabase.h" // for shader database
27#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
28#include "vtk_wgpu.h" // for webgpu
29
30VTK_ABI_NAMESPACE_BEGIN
31
35class vtkWebGPUTextureCache;
36class vtkImageData;
37class vtkTypeUInt32Array;
38class VTKRENDERINGWEBGPU_EXPORT VTK_MARSHALAUTO vtkWebGPURenderWindow : public vtkRenderWindow
39{
40public:
48 void PrintSelf(ostream& os, vtkIndent indent) override;
49
53 virtual void CreateAWindow();
54
58 virtual void DestroyWindow();
59
72 void Initialize() override;
73
74 void Start() override;
75
79 void End() override;
80
84 void Render() override;
85
90 void StereoMidpoint() override;
91 void Frame() override;
92
93 const char* GetRenderingBackend() override;
94
98 void* GetGenericContext() override;
99
101
106 unsigned char* GetPixelData(int x, int y, int x2, int y2, int front, int right) override;
108 int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right) override;
110 int x, int y, int x2, int y2, unsigned char* data, int front, int right) override;
112 int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front, int right) override;
114
116
119 float* GetRGBAPixelData(int x, int y, int x2, int y2, int front, int right = 0) override;
121 int x, int y, int x2, int y2, int front, vtkFloatArray* data, int right = 0) override;
123 int x, int y, int x2, int y2, float* data, int front, int blend = 0, int right = 0) override;
124 int SetRGBAPixelData(int x, int y, int x2, int y2, vtkFloatArray* data, int front, int blend = 0,
125 int right = 0) override;
126 void ReleaseRGBAPixelData(float* data) override;
127 unsigned char* GetRGBACharPixelData(
128 int x, int y, int x2, int y2, int front, int right = 0) override;
130 int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right = 0) override;
131 int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char* data, int front,
132 int blend = 0, int right = 0) override;
133 int SetRGBACharPixelData(int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front,
134 int blend = 0, int right = 0) override;
136
138
141 float* GetZbufferData(int x1, int y1, int x2, int y2) override;
142 int GetZbufferData(int x1, int y1, int x2, int y2, float* z) override;
143 int GetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
144 int SetZbufferData(int x1, int y1, int x2, int y2, float* buffer) override;
145 int SetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
147
149
152 vtkTypeUInt32* GetIdsData(int x1, int y1, int x2, int y2);
153 void GetIdsData(int x1, int y1, int x2, int y2, vtkTypeUInt32Array* data);
155
160 int GetColorBufferSizes(int* rgba) override;
164 void WaitForCompletion() override;
165
169 int SupportsOpenGL() override;
170
174 const char* ReportCapabilities() override;
175
181
187
189 vtkGetSmartPointerMacro(WGPUConfiguration, vtkWebGPUConfiguration);
190
197
203
209
214 std::string PreprocessShaderSource(const std::string& source) const;
215
219 wgpu::RenderPassEncoder NewRenderPass(wgpu::RenderPassDescriptor& descriptor);
220
225 wgpu::RenderBundleEncoder NewRenderBundleEncoder(wgpu::RenderBundleEncoderDescriptor& descriptor);
226
231 wgpu::CommandEncoder GetCommandEncoder();
232
237
241 void FlushCommandBuffers(vtkTypeUInt32 count, wgpu::CommandBuffer* buffers);
242
248
252 wgpu::TextureView GetDepthStencilView();
253
257 wgpu::TextureFormat GetDepthStencilFormat();
258
263
267 wgpu::Device GetDevice();
268
272 wgpu::Adapter GetAdapter();
273
277 wgpu::TextureFormat GetPreferredSurfaceTextureFormat();
278
283
285
295
298
300 std::function<void(const void* mappedData, int bytesPerRow, void* userdata)>;
301
310
315
319 bool EnsureDisplay() override;
320
324 void* GetGenericDisplayId() override;
325
326protected:
329
338 virtual std::string MakeDefaultWindowNameWithBackend();
339
340 bool WGPUInit();
342
346
349
352
355
358
360
362
363 virtual void SyncWithHardware();
364
366
367 wgpu::Surface Surface;
368 wgpu::CommandEncoder CommandEncoder;
370 wgpu::TextureFormat PreferredSurfaceTextureFormat = wgpu::TextureFormat::BGRA8Unorm;
371 wgpu::TextureFormat PreferredSelectorIdsTextureFormat = wgpu::TextureFormat::RGBA32Uint;
373 {
374 wgpu::Texture Texture;
375 wgpu::TextureView View;
376 wgpu::TextureFormat Format;
378 };
380
382 {
383 wgpu::Texture Texture;
384 wgpu::TextureView View;
385 wgpu::TextureFormat Format;
386 };
389
391 {
392 wgpu::Origin3D Origin;
393 wgpu::Extent3D Extent;
394 wgpu::TexelCopyBufferLayout Layout;
395 wgpu::Buffer Buffer; // for SetPixelData
396 };
398
400 {
401 std::string Key;
402 wgpu::BindGroup BindGroup;
403 };
405
410
415
417
418private:
419 // For accessing SubmitCommandBuffer to submit custom prop render work
421
423 void operator=(const vtkWebGPURenderWindow&) = delete;
424
429 void InitializeRendererComputePipelines();
430
435 void SubmitCommandBuffer(int count, wgpu::CommandBuffer* commandBuffer);
436
440 void PostRenderComputePipelines();
441
446 void PostRasterizationRender();
447
448 struct ComponentMapping
449 {
450 int Map[4];
451 int InComponents;
452 int OutComponents;
453 bool Valid;
454 };
455
456 ComponentMapping GetComponentMapping(wgpu::TextureFormat format, int desiredOutComponents);
457
458 template <typename TOutput, typename TInput>
459 struct PixelReadbackCallbackData
460 {
461 TOutput* OutputValues;
462 uint32_t Width, Height;
463 ComponentMapping Mapping;
464 std::function<TOutput(TInput)> Converter;
465 };
466
467 template <typename TOutput, typename TInput>
468 TOutput* GetTextureDataInternal(wgpu::Texture texture, wgpu::TextureFormat format, int x1, int y1,
469 int x2, int y2, const ComponentMapping& componentMapping,
470 std::function<TOutput(TInput)> converter = nullptr);
471
472 std::uint32_t FlipY(std::uint32_t y);
473
474 void ReadTextureFromGPU(wgpu::Texture& wgpuTexture, wgpu::TextureFormat format,
475 std::size_t mipLevel, wgpu::TextureAspect aspect, wgpu::Origin3D offsets,
476 wgpu::Extent3D extents, TextureMapCallback callback, void* userData);
477
478 void ReadTextureFromGPU(wgpu::Texture& wgpuTexture, wgpu::TextureFormat format,
479 std::size_t mipLevel, wgpu::TextureAspect aspect, TextureMapCallback callback, void* userData);
480
481 // Render textures acquired by the user on this render window. They are kept here in case the
482 // render window is resized, in which case, we'll need to resize the render textures --> We need
483 // access to the textures
484 std::vector<vtkSmartPointer<vtkWebGPUComputeRenderTexture>> ComputeRenderTextures;
485};
486
487#define vtkWebGPURenderWindow_OVERRIDE_ATTRIBUTES vtkWebGPURenderWindow::CreateOverrideAttributes()
488VTK_ABI_NAMESPACE_END
489#endif
dynamic, self-adjusting array of float
topologically and geometrically regular array of data
a simple class to control print indentation
Definition vtkIndent.h:108
Allocate and hold a VTK object.
Definition vtkNew.h:167
Attribute for vtkObjectFactory overrides.
platform-independent render window interaction including picking and frame rate control.
Hold a reference to a vtkObjectBase instance.
dynamic, self-adjusting array of unsigned char
This culler does both frustum culling and occlusion culling.
Create a webgpu device for use in rendering and compute pipelines.
Class to create and retrieve render pipelines based on a given key.
Class to create and retrieve render Textures based on a given key.
vtkWGPUAttachment IdsAttachment
vtkGetNewMacro(WGPUPipelineCache, vtkWebGPURenderPipelineCache)
Get the pipeline cache for this renderer.
static vtkOverrideAttribute * CreateOverrideAttributes()
vtkWGPUDepthStencil DepthStencilAttachment
virtual void DestroyWindow()
Destroy a not-off-screen window.
void Frame() override
A termination method performed at the end of the rendering process to do things like swapping buffers...
void CreateCommandEncoder()
Initializes a new command encoder.
wgpu::TextureFormat GetPreferredSurfaceTextureFormat()
Get the texture format preferred for the surface.
wgpu::TextureFormat GetPreferredSelectorIdsTextureFormat()
Get the texture format preferred for selector IDs.
void RecreateComputeRenderTextures()
wgpu::TextureFormat GetDepthStencilFormat()
Get the texture format of the depth-stencil attachment.
bool EnsureDisplay() override
Ensure RenderWindow's display is opened.
vtkGetNewMacro(WGPUTextureCache, vtkWebGPURenderTextureCache)
Get the texture cache for this renderer.
std::string PreprocessShaderSource(const std::string &source) const
Replaces all include statements in the given source code with source code corresponding to the includ...
void End() override
Update the system, if needed, at end of render process.
vtkSmartPointer< vtkImageData > SaveAttachmentToVTI(AttachmentTypeForVTISnapshot type)
int SupportsOpenGL() override
Does this render window support OpenGL?
int SetZbufferData(int x1, int y1, int x2, int y2, float *buffer) override
Set/Get the zbuffer data from an image.
float * GetRGBAPixelData(int x, int y, int x2, int y2, int front, int right=0) override
Set/Get the pixel data of an image, transmitted as RGBARGBA...
wgpu::CommandEncoder GetCommandEncoder()
Get the currently used command encoder.
vtkNew< vtkWebGPURenderTextureCache > WGPUTextureCache
void SetWGPUConfiguration(vtkWebGPUConfiguration *config)
vtkTypeUInt32 * GetIdsData(int x1, int y1, int x2, int y2)
Get the Ids data from the last render.
void SetInteractor(vtkRenderWindowInteractor *) override
Set the interactor for the window.
const char * ReportCapabilities() override
Get report of capabilities for the render window.
wgpu::Adapter GetAdapter()
Get the webgpu adapter.
std::function< void(const void *mappedData, int bytesPerRow, void *userdata)> TextureMapCallback
bool HasStencil()
Whether the offscreen render target has stencil capabilities.
int GetPixelData(int x, int y, int x2, int y2, int front, vtkUnsignedCharArray *data, int right) override
Set/Get the pixel data of an image, transmitted as RGBRGB... front in this context indicates that the...
vtkWGPUAttachment ColorAttachment
friend class vtkWebGPUComputeOcclusionCuller
vtkSmartPointer< vtkWebGPUConfiguration > WGPUConfiguration
wgpu::TextureView GetHardwareSelectorAttachmentView()
int GetRGBAPixelData(int x, int y, int x2, int y2, int front, vtkFloatArray *data, int right=0) override
Set/Get the pixel data of an image, transmitted as RGBARGBA...
void ReleaseRGBAPixelData(float *data) override
Set/Get the pixel data of an image, transmitted as RGBARGBA...
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static vtkWebGPURenderWindow * New()
Instantiate the class.
int GetRGBACharPixelData(int x, int y, int x2, int y2, int front, vtkUnsignedCharArray *data, int right=0) override
Set/Get the pixel data of an image, transmitted as RGBARGBA...
void * GetGenericDisplayId() override
Get the generic display id for the window.
int SetPixelData(int x, int y, int x2, int y2, unsigned char *data, int front, int right) override
Set/Get the pixel data of an image, transmitted as RGBRGB... front in this context indicates that the...
unsigned char * GetRGBACharPixelData(int x, int y, int x2, int y2, int front, int right=0) override
Set/Get the pixel data of an image, transmitted as RGBARGBA...
wgpu::TextureFormat PreferredSurfaceTextureFormat
vtkWGPUFullScreenQuad ColorCopyRenderPipeline
float * GetZbufferData(int x1, int y1, int x2, int y2) override
Set/Get the zbuffer data from an image.
void WaitForCompletion() override
Block the thread until work queue completes all submitted work.
int SetPixelData(int x, int y, int x2, int y2, vtkUnsignedCharArray *data, int front, int right) override
Set/Get the pixel data of an image, transmitted as RGBRGB... front in this context indicates that the...
wgpu::CommandEncoder CommandEncoder
void CreateDepthStencilAttachment()
void DestroyDepthStencilAttachment()
void ReleaseGraphicsResources(vtkWindow *) override
Free up any graphics resources associated with this window a value of NULL means the context may alre...
bool InitializeFromCurrentContext() override
Initialize the render window from the information associated with the currently activated OpenGL cont...
vtkSmartPointer< vtkWebGPUComputePipeline > DepthCopyPipeline
vtkGetNewMacro(WGPUShaderDatabase, vtkWebGPUShaderDatabase)
Get a database of all WebGPU shader source codes in VTK.
void Initialize() override
Creates the WebGPU context, swapchain, depth buffer, color attachment, ...
void CreateOffscreenColorAttachment()
void DestroyOffscreenColorAttachment()
wgpu::TextureView GetDepthStencilView()
Get a view of the depth-stencil attachment used in the offscreen render target.
int SetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray *buffer) override
Set/Get the zbuffer data from an image.
int GetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray *buffer) override
Set/Get the zbuffer data from an image.
void Render() override
Handle opengl specific code and calls superclass.
int SetRGBAPixelData(int x, int y, int x2, int y2, float *data, int front, int blend=0, int right=0) override
Set/Get the pixel data of an image, transmitted as RGBARGBA...
wgpu::RenderBundleEncoder NewRenderBundleEncoder(wgpu::RenderBundleEncoderDescriptor &descriptor)
Create a new render bundle encoder on the webgpu device.
vtkSmartPointer< vtkWebGPUComputeRenderTexture > AcquireDepthBufferRenderTexture()
Returns a vtkWebGPUComputeRenderTexture ready to be added to a compute pipeline using vtkWebGPUComput...
vtkSmartPointer< vtkWebGPUComputePass > DepthCopyPass
void FlushCommandBuffers(vtkTypeUInt32 count, wgpu::CommandBuffer *buffers)
Sends a given command buffer to the device queue.
void Start() override
Start the rendering process for a frame.
const char * GetRenderingBackend() override
What rendering backend has the user requested.
vtkSmartPointer< vtkWebGPUComputeRenderTexture > AcquireFramebufferRenderTexture()
Returns a vtkWebGPUComputeRenderTexture ready to be added to a compute pipeline using vtkWebGPUComput...
vtkNew< vtkWebGPURenderPipelineCache > WGPUPipelineCache
wgpu::RenderPassEncoder NewRenderPass(wgpu::RenderPassDescriptor &descriptor)
Create a new render pass encoder on the webgpu device.
virtual void SyncWithHardware()
int SetRGBACharPixelData(int x, int y, int x2, int y2, vtkUnsignedCharArray *data, int front, int blend=0, int right=0) override
Set/Get the pixel data of an image, transmitted as RGBARGBA...
vtkNew< vtkWebGPUShaderDatabase > WGPUShaderDatabase
~vtkWebGPURenderWindow() override
void * GetGenericContext() override
Get the generic context pointer.
virtual std::string MakeDefaultWindowNameWithBackend()
Construct the window title as "Visualization Toolkit - <WindowSystem> <GraphicsBackend>" Ex: "Visuali...
int GetZbufferData(int x1, int y1, int x2, int y2, float *z) override
Set/Get the zbuffer data from an image.
wgpu::TextureView GetOffscreenColorAttachmentView()
Get a view of the color attachment used in the offscreen render target.
vtkWGPUUserStagingPixelData StagingPixelData
int SetRGBAPixelData(int x, int y, int x2, int y2, vtkFloatArray *data, int front, int blend=0, int right=0) override
Set/Get the pixel data of an image, transmitted as RGBARGBA...
void StereoMidpoint() override
Intermediate method performs operations required between the rendering of the left and right eye.
int GetColorBufferSizes(int *rgba) override
Get the size of the color buffer.
wgpu::Device GetDevice()
Get the webgpu device.
int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char *data, int front, int blend=0, int right=0) override
Set/Get the pixel data of an image, transmitted as RGBARGBA...
virtual void CreateAWindow()
Create a not-off-screen window.
wgpu::TextureFormat PreferredSelectorIdsTextureFormat
void GetIdsData(int x1, int y1, int x2, int y2, vtkTypeUInt32Array *data)
Get the Ids data from the last render.
unsigned char * GetPixelData(int x, int y, int x2, int y2, int front, int right) override
Set/Get the pixel data of an image, transmitted as RGBRGB... front in this context indicates that the...
Class to add and retrieve source code for shader files for a specified key.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
@ Valid
Cell is in a good state.
#define VTK_MARSHALAUTO
#define VTK_NEWINSTANCE