VTK  9.4.20250306
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
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 "vtkWebGPUShaderDatabase.h" // for shader database
26#include "vtk_wgpu.h" // for webgpu
27
28VTK_ABI_NAMESPACE_BEGIN
29
32class vtkImageData;
33class vtkTypeUInt32Array;
34class VTKRENDERINGWEBGPU_EXPORT vtkWebGPURenderWindow : public vtkRenderWindow
35{
36public:
38 void PrintSelf(ostream& os, vtkIndent indent) override;
39
44 virtual bool WindowSetup() = 0;
45
49 virtual void CreateAWindow() = 0;
50
54 virtual void DestroyWindow() = 0;
55
68 void Initialize() override;
69
70 void Start() override;
71
75 void End() override;
76
80 void Render() override;
81
86 void StereoMidpoint() override;
87 void Frame() override;
88
89 const char* GetRenderingBackend() override;
90
92
97 unsigned char* GetPixelData(int x, int y, int x2, int y2, int front, int right) override;
99 int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right) override;
101 int x, int y, int x2, int y2, unsigned char* data, int front, int right) override;
103 int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front, int right) override;
105
107
110 float* GetRGBAPixelData(int x, int y, int x2, int y2, int front, int right = 0) override;
112 int x, int y, int x2, int y2, int front, vtkFloatArray* data, int right = 0) override;
114 int x, int y, int x2, int y2, float* data, int front, int blend = 0, int right = 0) override;
115 int SetRGBAPixelData(int x, int y, int x2, int y2, vtkFloatArray* data, int front, int blend = 0,
116 int right = 0) override;
117 void ReleaseRGBAPixelData(float* data) override;
118 unsigned char* GetRGBACharPixelData(
119 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, vtkUnsignedCharArray* data, int right = 0) override;
122 int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char* data, int front,
123 int blend = 0, int right = 0) override;
124 int SetRGBACharPixelData(int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front,
125 int blend = 0, int right = 0) override;
127
129
132 float* GetZbufferData(int x1, int y1, int x2, int y2) override;
133 int GetZbufferData(int x1, int y1, int x2, int y2, float* z) override;
134 int GetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
135 int SetZbufferData(int x1, int y1, int x2, int y2, float* buffer) override;
136 int SetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
138
143 int GetColorBufferSizes(int* rgba) override;
147 void WaitForCompletion() override;
148
152 int SupportsOpenGL() override;
153
157 const char* ReportCapabilities() override;
158
164
170
172 vtkGetSmartPointerMacro(WGPUConfiguration, vtkWebGPUConfiguration);
173
180
186
191 std::string PreprocessShaderSource(const std::string& source) const;
192
196 wgpu::RenderPassEncoder NewRenderPass(wgpu::RenderPassDescriptor& descriptor);
197
202 wgpu::RenderBundleEncoder NewRenderBundleEncoder(wgpu::RenderBundleEncoderDescriptor& descriptor);
203
208 wgpu::CommandEncoder GetCommandEncoder();
209
214
218 void FlushCommandBuffers(vtkTypeUInt32 count, wgpu::CommandBuffer* buffers);
219
225
229 wgpu::TextureView GetDepthStencilView();
230
234 wgpu::TextureFormat GetDepthStencilFormat();
235
240
244 wgpu::Device GetDevice();
245
249 wgpu::Adapter GetAdapter();
250
254 wgpu::TextureFormat GetPreferredSurfaceTextureFormat();
255
260
262
272
275
277 std::function<void(const void* mappedData, int bytesPerRow, void* userdata)>;
278
280 {
281 ColorRGBA,
282 ColorRGB,
283 Depth,
284 Ids,
285 };
287
288protected:
291
300 virtual std::string MakeDefaultWindowNameWithBackend() = 0;
301
302 bool WGPUInit();
304
307
310
313
316
319
321
323
324 bool RenderTexturesSetup = false;
325
326 wgpu::Surface Surface;
327 wgpu::CommandEncoder CommandEncoder;
328 int SurfaceConfiguredSize[2];
329 wgpu::TextureFormat PreferredSurfaceTextureFormat = wgpu::TextureFormat::BGRA8Unorm;
330 wgpu::TextureFormat PreferredSelectorIdsTextureFormat = wgpu::TextureFormat::RGBA32Uint;
332 {
333 wgpu::Texture Texture;
334 wgpu::TextureView View;
335 wgpu::TextureFormat Format;
337 };
339
341 {
342 wgpu::Texture Texture;
343 wgpu::TextureView View;
344 wgpu::TextureFormat Format;
345 };
348
350 {
351 wgpu::Origin3D Origin;
352 wgpu::Extent3D Extent;
353 wgpu::TexelCopyBufferLayout Layout;
354 wgpu::Buffer Buffer; // for SetPixelData
355 };
357
359 {
360 std::string Key;
361 wgpu::BindGroup BindGroup;
362 };
364
368
371 int DepthCopyBufferIndex = 0;
372 int DepthCopyTextureIndex = 0;
373
374 int ScreenSize[2];
375
376private:
377 // For accessing SubmitCommandBuffer to submit custom prop render work
379 // For accessing HardwareSelectorAttachment
381
383 void operator=(const vtkWebGPURenderWindow&) = delete;
384
389 void InitializeRendererComputePipelines();
390
395 void SubmitCommandBuffer(int count, wgpu::CommandBuffer* commandBuffer);
396
400 void PostRenderComputePipelines();
401
406 void PostRasterizationRender();
407
408 void ReadTextureFromGPU(wgpu::Texture& wgpuTexture, wgpu::TextureFormat format,
409 std::size_t mipLevel, wgpu::TextureAspect aspect, wgpu::Origin3D offsets,
410 wgpu::Extent3D extents, TextureMapCallback callback, void* userData);
411
412 void ReadTextureFromGPU(wgpu::Texture& wgpuTexture, wgpu::TextureFormat format,
413 std::size_t mipLevel, wgpu::TextureAspect aspect, TextureMapCallback callback, void* userData);
414
415 void GetIdsData(int x1, int y1, int x2, int y2, vtkTypeUInt32* values);
416 void GetIdsData(int x1, int y1, int x2, int y2, vtkTypeUInt32Array* data);
417
418 // Render textures acquired by the user on this render window. They are kept here in case the
419 // render window is resized, in which case, we'll need to resize the render textures --> We need
420 // access to the textures
421 std::vector<vtkSmartPointer<vtkWebGPUComputeRenderTexture>> ComputeRenderTextures;
422};
423
424VTK_ABI_NAMESPACE_END
425#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
create a window for renderers to draw into
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.
implements the device specific code of vtkWebGPUHardwareSelector.
Class to create and retrieve render pipelines based on a given key.
WebGPU rendering window.
vtkWGPUAttachment IdsAttachment
vtkGetNewMacro(WGPUPipelineCache, vtkWebGPURenderPipelineCache)
Get the pipeline cache for this renderer.
vtkWGPUDepthStencil DepthStencilAttachment
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.
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? 0-false, 1-true.
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.
void SetWGPUConfiguration(vtkWebGPUConfiguration *config)
const char * ReportCapabilities() override
Get report of capabilities for the render window.
wgpu::Adapter GetAdapter()
Get the webgpu adapter.
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...
virtual void CreateAWindow()=0
Create a not-off-screen window.
vtkWGPUAttachment ColorAttachment
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.
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...
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...
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...
virtual std::string MakeDefaultWindowNameWithBackend()=0
Construct the window title as "Visualization Toolkit - <WindowSystem> <GraphicsBackend>" Ex: "Visuali...
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
virtual void DestroyWindow()=0
Destroy a not-off-screen window.
wgpu::RenderPassEncoder NewRenderPass(wgpu::RenderPassDescriptor &descriptor)
Create a new render pass encoder on the webgpu device.
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
virtual bool WindowSetup()=0
Concrete render windows must create a platform window and initialize this->WindowId.
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.
std::function< void(const void *mappedData, int bytesPerRow, void *userdata)> TextureMapCallback
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...
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.
window superclass for vtkRenderWindow
Definition vtkWindow.h:48
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)