VTK  9.6.20260418
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
18
19#ifndef vtkWebGPURenderWindow_h
20#define vtkWebGPURenderWindow_h
21
22#include "vtkRenderWindow.h"
23
24#include "vtkRenderingWebGPUModule.h" // for export macro
25#include "vtkWebGPUComputePipeline.h" // for the compute pipelines of this render window
26#include "vtkWebGPUComputeRenderTexture.h" // for compute render textures
27#include "vtkWebGPURenderPipelineCache.h" // for vtkWebGPURenderPipelineCache
28#include "vtkWebGPURenderTextureCache.h" // for texture cache
29#include "vtkWebGPUShaderDatabase.h" // for shader database
30#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
31#include "vtk_wgpu.h" // for webgpu
32
33VTK_ABI_NAMESPACE_BEGIN
34
38class vtkWebGPUTextureCache;
39class vtkImageData;
40class vtkTypeUInt32Array;
41class VTKRENDERINGWEBGPU_EXPORT VTK_MARSHALAUTO vtkWebGPURenderWindow : public vtkRenderWindow
42{
43public:
51 void PrintSelf(ostream& os, vtkIndent indent) override;
52
56 virtual void CreateAWindow();
57
61 virtual void DestroyWindow();
62
75 void Initialize() override;
76
77 void Start() override;
78
82 void End() override;
83
87 void Render() override;
88
93 void StereoMidpoint() override;
94 void Frame() override;
95
96 const char* GetRenderingBackend() override;
97
101 void* GetGenericContext() override;
102
104
109 unsigned char* GetPixelData(int x, int y, int x2, int y2, int front, int right) override;
111 int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right) override;
113 int x, int y, int x2, int y2, unsigned char* data, int front, int right) override;
115 int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front, int right) override;
117
119
122 float* GetRGBAPixelData(int x, int y, int x2, int y2, int front, int right = 0) override;
124 int x, int y, int x2, int y2, int front, vtkFloatArray* data, int right = 0) override;
126 int x, int y, int x2, int y2, float* data, int front, int blend = 0, int right = 0) override;
127 int SetRGBAPixelData(int x, int y, int x2, int y2, vtkFloatArray* data, int front, int blend = 0,
128 int right = 0) override;
129 void ReleaseRGBAPixelData(float* data) override;
130 unsigned char* GetRGBACharPixelData(
131 int x, int y, int x2, int y2, int front, int right = 0) override;
133 int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right = 0) override;
134 int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char* data, int front,
135 int blend = 0, int right = 0) override;
136 int SetRGBACharPixelData(int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front,
137 int blend = 0, int right = 0) override;
139
141
144 float* GetZbufferData(int x1, int y1, int x2, int y2) override;
145 int GetZbufferData(int x1, int y1, int x2, int y2, float* z) override;
146 int GetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
147 int SetZbufferData(int x1, int y1, int x2, int y2, float* buffer) override;
148 int SetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
150
152
155 vtkTypeUInt32* GetIdsData(int x1, int y1, int x2, int y2);
156 void GetIdsData(int x1, int y1, int x2, int y2, vtkTypeUInt32Array* data);
158
163 int GetColorBufferSizes(int* rgba) override;
167 void WaitForCompletion() override;
168
172 int SupportsOpenGL() override;
173
177 const char* ReportCapabilities() override;
178
184
190
192 vtkGetSmartPointerMacro(WGPUConfiguration, vtkWebGPUConfiguration);
193
200
206
212
217 std::string PreprocessShaderSource(const std::string& source) const;
218
222 wgpu::RenderPassEncoder NewRenderPass(wgpu::RenderPassDescriptor& descriptor);
223
228 wgpu::RenderBundleEncoder NewRenderBundleEncoder(wgpu::RenderBundleEncoderDescriptor& descriptor);
229
234 wgpu::CommandEncoder GetCommandEncoder();
235
240
244 void FlushCommandBuffers(vtkTypeUInt32 count, wgpu::CommandBuffer* buffers);
245
251
255 wgpu::TextureView GetDepthStencilView();
256
260 wgpu::TextureFormat GetDepthStencilFormat();
261
266
270 wgpu::Device GetDevice();
271
275 wgpu::Adapter GetAdapter();
276
280 wgpu::TextureFormat GetPreferredSurfaceTextureFormat();
281
286
288
300
302 std::function<void(const void* mappedData, int bytesPerRow, void* userdata)>;
303
312
316 bool EnsureDisplay() override;
317
321 void* GetGenericDisplayId() override;
322
323protected:
326
335 virtual std::string MakeDefaultWindowNameWithBackend();
336
337private:
338 // For accessing SubmitCommandBuffer to submit custom prop render work
340
342 void operator=(const vtkWebGPURenderWindow&) = delete;
343
348 void InitializeRendererComputePipelines();
349
354 void SubmitCommandBuffer(int count, wgpu::CommandBuffer* commandBuffer);
355
359 void PostRenderComputePipelines();
360
365 void PostRasterizationRender();
366
367 struct ComponentMapping
368 {
369 int Map[4];
370 int InComponents;
371 int OutComponents;
372 bool Valid;
373 };
374
390 ComponentMapping GetComponentMapping(wgpu::TextureFormat format, int desiredOutComponents);
391
392 template <typename TOutput, typename TInput>
393 struct PixelReadbackCallbackData
394 {
395 TOutput* OutputValues;
396 uint32_t Width, Height;
397 ComponentMapping Mapping;
398 std::function<TOutput(TInput)> Converter;
399 };
400
415 template <typename TOutput, typename TInput>
416 TOutput* GetTextureDataInternal(wgpu::Texture texture, wgpu::TextureFormat format, int x1, int y1,
417 int x2, int y2, const ComponentMapping& componentMapping,
418 std::function<TOutput(TInput)> converter = nullptr);
419
425 std::uint32_t FlipY(std::uint32_t y);
426
436 void ReadTextureFromGPU(wgpu::Texture& wgpuTexture, wgpu::TextureFormat format,
437 std::size_t mipLevel, wgpu::TextureAspect aspect, wgpu::Origin3D offsets,
438 wgpu::Extent3D extents, TextureMapCallback callback, void* userData);
439
445 void ReadTextureFromGPU(wgpu::Texture& wgpuTexture, wgpu::TextureFormat format,
446 std::size_t mipLevel, wgpu::TextureAspect aspect, TextureMapCallback callback, void* userData);
447
453 bool WGPUInit();
454
458 void WGPUFinalize();
459
465 void CreateSurface();
466
470 void ConfigureSurface();
471
477 void UnconfigureSurface();
478
480
485 void CreateOffscreenColorAttachment();
486 void DestroyOffscreenColorAttachment();
488
490
495 void CreateIdsAttachment();
496 void DestroyIdsAttachment();
498
500
505 void CreateDepthStencilAttachment();
506 void DestroyDepthStencilAttachment();
508
510
514 void CreateColorCopyPipeline();
515 void DestroyColorCopyPipeline();
517
523 void RecreateComputeRenderTextures();
524
531 void RenderOffscreenTexture();
532
537 virtual void SyncWithHardware();
538
539 bool RenderTexturesSetup = false;
540
541 wgpu::Surface Surface;
542 wgpu::CommandEncoder CommandEncoder;
543 int SurfaceConfiguredSize[2];
544 wgpu::TextureFormat PreferredSurfaceTextureFormat = wgpu::TextureFormat::BGRA8Unorm;
545 wgpu::TextureFormat PreferredSelectorIdsTextureFormat = wgpu::TextureFormat::RGBA32Uint;
546 struct vtkWGPUDepthStencil
547 {
548 wgpu::Texture Texture;
549 wgpu::TextureView View;
550 wgpu::TextureFormat Format;
551 bool HasStencil;
552 };
553 vtkWGPUDepthStencil DepthStencilAttachment;
554
555 struct vtkWGPUAttachment
556 {
557 wgpu::Texture Texture;
558 wgpu::TextureView View;
559 wgpu::TextureFormat Format;
560 };
561 vtkWGPUAttachment ColorAttachment;
562 vtkWGPUAttachment IdsAttachment;
563
564 struct vtkWGPUUserStagingPixelData
565 {
566 wgpu::Origin3D Origin;
567 wgpu::Extent3D Extent;
568 wgpu::TexelCopyBufferLayout Layout;
569 wgpu::Buffer Buffer; // for SetPixelData
570 };
571 vtkWGPUUserStagingPixelData StagingPixelData;
572
573 struct vtkWGPUFullScreenQuad
574 {
575 std::string Key;
576 wgpu::BindGroup BindGroup;
577 };
578 vtkWGPUFullScreenQuad ColorCopyRenderPipeline;
579
580 vtkSmartPointer<vtkWebGPUConfiguration> WGPUConfiguration;
581 vtkNew<vtkWebGPUShaderDatabase> WGPUShaderDatabase;
582 vtkNew<vtkWebGPURenderPipelineCache> WGPUPipelineCache;
583 vtkNew<vtkWebGPURenderTextureCache> WGPUTextureCache;
584
585 vtkSmartPointer<vtkWebGPUComputePipeline> DepthCopyPipeline;
586 vtkSmartPointer<vtkWebGPUComputePass> DepthCopyPass;
587 int DepthCopyBufferIndex = 0;
588 int DepthCopyTextureIndex = 0;
589
590 int ScreenSize[2];
591
592 // Render textures acquired by the user on this render window. They are kept here in case the
593 // render window is resized, in which case, we'll need to resize the render textures --> We need
594 // access to the textures
595 std::vector<vtkSmartPointer<vtkWebGPUComputeRenderTexture>> ComputeRenderTextures;
596};
597
598#define vtkWebGPURenderWindow_OVERRIDE_ATTRIBUTES vtkWebGPURenderWindow::CreateOverrideAttributes()
599VTK_ABI_NAMESPACE_END
600#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
Attribute for vtkObjectFactory overrides.
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.
vtkGetNewMacro(WGPUPipelineCache, vtkWebGPURenderPipelineCache)
Get the pipeline cache for this renderer.
static vtkOverrideAttribute * CreateOverrideAttributes()
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.
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.
void SetWGPUConfiguration(vtkWebGPUConfiguration *config)
vtkTypeUInt32 * GetIdsData(int x1, int y1, int x2, int y2)
Get the Ids data from the last render.
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...
friend class vtkWebGPUComputeOcclusionCuller
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...
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...
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...
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...
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...
~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.
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.
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