VTK  9.4.20250130
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 "vtkTypeUInt8Array.h" // for ivar
23#include "vtkWebGPUComputePipeline.h" // for the compute pipelines of this render window
24#include "vtkWebGPUComputeRenderTexture.h" // for compute render textures
25#include "vtkWebGPURenderPipelineCache.h" // for vtkWebGPURenderPipelineCache
26#include "vtkWebGPUShaderDatabase.h" // for shader database
27#include "vtk_wgpu.h" // for webgpu
28
29VTK_ABI_NAMESPACE_BEGIN
30
33
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
94 void ReadPixels();
95
97
102 unsigned char* GetPixelData(int x, int y, int x2, int y2, int front, int right) override;
104 int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right) override;
106 int x, int y, int x2, int y2, unsigned char* data, int front, int right) override;
108 int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front, int right) override;
110
112
115 float* GetRGBAPixelData(int x, int y, int x2, int y2, int front, int right = 0) override;
117 int x, int y, int x2, int y2, int front, vtkFloatArray* data, int right = 0) override;
119 int x, int y, int x2, int y2, float* data, int front, int blend = 0, int right = 0) override;
120 int SetRGBAPixelData(int x, int y, int x2, int y2, vtkFloatArray* data, int front, int blend = 0,
121 int right = 0) override;
122 void ReleaseRGBAPixelData(float* data) override;
123 unsigned char* GetRGBACharPixelData(
124 int x, int y, int x2, int y2, int front, int right = 0) override;
126 int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right = 0) override;
127 int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char* data, int front,
128 int blend = 0, int right = 0) override;
129 int SetRGBACharPixelData(int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front,
130 int blend = 0, int right = 0) override;
132
134
137 float* GetZbufferData(int x1, int y1, int x2, int y2) override;
138 int GetZbufferData(int x1, int y1, int x2, int y2, float* z) override;
139 int GetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
140 int SetZbufferData(int x1, int y1, int x2, int y2, float* buffer) override;
141 int SetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
143
148 int GetColorBufferSizes(int* rgba) override;
152 void WaitForCompletion() override;
153
157 int SupportsOpenGL() override;
158
162 const char* ReportCapabilities() override;
163
169
175
178
185
191
196 std::string PreprocessShaderSource(const std::string& source) const;
197
201 wgpu::RenderPassEncoder NewRenderPass(wgpu::RenderPassDescriptor& descriptor);
202
207 wgpu::RenderBundleEncoder NewRenderBundleEncoder(wgpu::RenderBundleEncoderDescriptor& descriptor);
208
213 wgpu::CommandEncoder GetCommandEncoder();
214
219
223 void FlushCommandBuffers(vtkTypeUInt32 count, wgpu::CommandBuffer* buffers);
224
229
233 wgpu::TextureView GetDepthStencilView();
234
238 wgpu::TextureFormat GetDepthStencilFormat();
239
244
248 wgpu::Device GetDevice();
249
253 wgpu::Adapter GetAdapter();
254
258 wgpu::TextureFormat GetPreferredSurfaceTextureFormat();
259
261
271
274
275protected:
278
287 virtual std::string MakeDefaultWindowNameWithBackend() = 0;
288
289 bool WGPUInit();
291
294
297
300
303
305
307
308 bool RenderTexturesSetup = false;
309
310 wgpu::Surface Surface;
311 wgpu::CommandEncoder CommandEncoder;
312 int SurfaceConfiguredSize[2];
313 wgpu::TextureFormat PreferredSurfaceTextureFormat = wgpu::TextureFormat::BGRA8Unorm;
314
316 {
317 wgpu::Texture Texture;
318 wgpu::TextureView View;
319 wgpu::TextureFormat Format;
321 };
323
325 {
326 wgpu::Texture Texture;
327 wgpu::TextureView View;
328 wgpu::TextureFormat Format;
329 wgpu::Buffer OffscreenBuffer;
330 };
332
334 {
335 wgpu::Origin3D Origin;
336 wgpu::Extent3D Extent;
337 wgpu::TextureDataLayout Layout;
338 wgpu::Buffer Buffer; // for SetPixelData
339 };
341
343 {
344 wgpu::RenderPipeline Pipeline;
345 wgpu::BindGroup BindGroup;
346 };
347
349
351 {
353 wgpu::Buffer src;
354 unsigned long size;
356 } BufferMapReadContext;
357
362
363 int ScreenSize[2];
364
365private:
366 // For accessing SubmitCommandBuffer to submit custom prop render work
368
370 void operator=(const vtkWebGPURenderWindow&) = delete;
371
376 void InitializeRendererComputePipelines();
377
382 void SubmitCommandBuffer(int count, wgpu::CommandBuffer* commandBuffer);
383
387 void PostRenderComputePipelines();
388
393 void PostRasterizationRender();
394
399 void CopyFramebufferToOffscreenBuffer();
400
401 // Render textures acquired by the user on this render window. They are kept here in case the
402 // render window is resized, in which case, we'll need to resize the render textures --> We need
403 // access to the textures
404 std::vector<vtkSmartPointer<vtkWebGPUComputeRenderTexture>> ComputeRenderTextures;
405};
406
407VTK_ABI_NAMESPACE_END
408#endif
dynamic, self-adjusting array of float
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.
Class to create and retrieve render pipelines based on a given key.
WebGPU rendering window.
vtkGetNewMacro(WGPUPipelineCache, vtkWebGPURenderPipelineCache)
Get the pipeline cache for this renderer.
void DestroyDepthStencilTexture()
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.
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.
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.
void ReadPixels()
Reads pixels into the CachedPixelBytes variable.
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.
vtkGetSmartPointerMacro(WGPUConfiguration, vtkWebGPUConfiguration)
void SetWGPUConfiguration(vtkWebGPUConfiguration *config)
void CreateOffscreenColorAttachments()
vtkWGPUColorAttachment ColorAttachment
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.
vtkSmartPointer< vtkWebGPUConfiguration > WGPUConfiguration
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...
void DestroyOffscreenColorAttachments()
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...
float * GetZbufferData(int x1, int y1, int x2, int y2) override
Set/Get the zbuffer data from an image.
void DestroyFSQGraphicsPipeline()
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 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...
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, ...
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...
vtkNew< vtkTypeUInt8Array > CachedPixelBytes
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...
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...
void CreateFSQGraphicsPipeline()
vtkNew< vtkWebGPUShaderDatabase > WGPUShaderDatabase
~vtkWebGPURenderWindow() override
void CreateDepthStencilTexture()
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.
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...
vtkWGPUFullScreenQuad FSQ
void StereoMidpoint() override
Intermediate method performs operations required between the rendering of the left and right eye.
vtkWGPUDeptStencil DepthStencil
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
vtkSmartPointer< vtkTypeUInt8Array > dst
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)