VTK  9.7.20260915
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 C API
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
94 void Finalize() override;
95
96 void Start() override;
97
101 void End() override;
102
106 void Render() override;
107
112 void StereoMidpoint() override;
113 void Frame() override;
114
115 const char* GetRenderingBackend() override;
116
120 void* GetGenericContext() override;
121
123
128 unsigned char* GetPixelData(int x, int y, int x2, int y2, int front, int right) override;
130 int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right) override;
132 int x, int y, int x2, int y2, unsigned char* data, int front, int right) override;
134 int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front, int right) override;
136
138
141 float* GetRGBAPixelData(int x, int y, int x2, int y2, int front, int right = 0) override;
143 int x, int y, int x2, int y2, int front, vtkFloatArray* data, int right = 0) override;
145 int x, int y, int x2, int y2, float* data, int front, int blend = 0, int right = 0) override;
146 int SetRGBAPixelData(int x, int y, int x2, int y2, vtkFloatArray* data, int front, int blend = 0,
147 int right = 0) override;
148 void ReleaseRGBAPixelData(float* data) override;
149 unsigned char* GetRGBACharPixelData(
150 int x, int y, int x2, int y2, int front, int right = 0) override;
152 int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right = 0) override;
153 int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char* data, int front,
154 int blend = 0, int right = 0) override;
155 int SetRGBACharPixelData(int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front,
156 int blend = 0, int right = 0) override;
158
160
163 float* GetZbufferData(int x1, int y1, int x2, int y2) override;
164 int GetZbufferData(int x1, int y1, int x2, int y2, float* z) override;
165 int GetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
166 int SetZbufferData(int x1, int y1, int x2, int y2, float* buffer) override;
167 int SetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
169
171
174 vtkTypeUInt32* GetIdsData(int x1, int y1, int x2, int y2);
176 void GetIdsData(int x1, int y1, int x2, int y2, vtkTypeUInt32Array* data);
178
183 int GetColorBufferSizes(int* rgba) override;
187 void WaitForCompletion() override;
188
192 int SupportsOpenGL() override;
193
197 const char* ReportCapabilities() override;
198
204
210
212
223 vtkGetSmartPointerMacro(WGPUConfiguration, vtkWebGPUConfiguration);
225
232
238
244
249 std::string PreprocessShaderSource(const std::string& source) const;
250
254 WGPURenderPassEncoder NewRenderPass(WGPURenderPassDescriptor& descriptor);
255
260 WGPURenderBundleEncoder NewRenderBundleEncoder(WGPURenderBundleEncoderDescriptor& descriptor);
261
266 WGPUCommandEncoder GetCommandEncoder();
267
272
276 void FlushCommandBuffers(vtkTypeUInt32 count, WGPUCommandBuffer* buffers);
277
283
287 WGPUTextureView GetDepthStencilView();
288
292 WGPUTextureFormat GetDepthStencilFormat();
293
298
302 WGPUDevice GetDevice();
303
307 WGPUAdapter GetAdapter();
308
313
318
320
332
334 std::function<void(const void* mappedData, int bytesPerRow, void* userdata)>;
335
337 {
338 ColorRGBA,
339 ColorRGB,
340 Depth,
341 Ids,
342 };
344
349
353 bool EnsureDisplay() override;
354
358 void* GetGenericDisplayId() override;
359
361
371 void SetCustomSurfaceDescriptor(const WGPUSurfaceDescriptor* descriptor);
373 const WGPUSurfaceDescriptor* GetCustomSurfaceDescriptor() const;
375
376protected:
379
389
390private:
391 // For accessing SubmitCommandBuffer to submit custom prop render work
393
395 void operator=(const vtkWebGPURenderWindow&) = delete;
396
401 void InitializeRendererComputePipelines();
402
407 void SubmitCommandBuffer(int count, WGPUCommandBuffer* commandBuffer);
408
412 void PostRenderComputePipelines();
413
418 void PostRasterizationRender();
419
420 struct ComponentMapping
421 {
422 int Map[4];
423 int InComponents;
424 int OutComponents;
425 bool Valid;
426 };
427
443 ComponentMapping GetComponentMapping(WGPUTextureFormat format, int desiredOutComponents);
444
445 template <typename TOutput, typename TInput>
446 struct PixelReadbackCallbackData
447 {
448 TOutput* OutputValues;
449 uint32_t Width, Height;
450 ComponentMapping Mapping;
451 std::function<TOutput(TInput)> Converter;
452 // Set once the mapped pixels have been copied into OutputValues. The caller
453 // owns the flag and waits on it; see GetTextureDataInternal.
454 bool* Mapped;
455 };
456
471 template <typename TOutput, typename TInput>
472 TOutput* GetTextureDataInternal(WGPUTexture texture, WGPUTextureFormat format, int x1, int y1,
473 int x2, int y2, const ComponentMapping& componentMapping,
474 std::function<TOutput(TInput)> converter = nullptr);
475
481 std::uint32_t FlipY(std::uint32_t y);
482
492 void ReadTextureFromGPU(WGPUTexture& wgpuTexture, WGPUTextureFormat format, std::size_t mipLevel,
493 WGPUTextureAspect aspect, WGPUOrigin3D offsets, WGPUExtent3D extents,
494 TextureMapCallback callback, void* userData);
495
501 void ReadTextureFromGPU(WGPUTexture& wgpuTexture, WGPUTextureFormat format, std::size_t mipLevel,
502 WGPUTextureAspect aspect, TextureMapCallback callback, void* userData);
503
509 bool WGPUInit();
510
518 void WGPUFinalize();
519
525 void CreateSurface();
526 WGPUSurface CreateSurfaceFromHardwareWindow(WGPUInstance instance);
527
531 void ConfigureSurface();
532
538 void UnconfigureSurface();
539
552 bool RendersOffScreenOnly();
553
555
560 void CreateOffscreenColorAttachment();
561 void DestroyOffscreenColorAttachment();
563
565
570 void CreateIdsAttachment();
571 void DestroyIdsAttachment();
573
575
580 void CreateDepthStencilAttachment();
581 void DestroyDepthStencilAttachment();
583
585
589 void CreateColorCopyPipeline();
590 void DestroyColorCopyPipeline();
592
598 void RecreateComputeRenderTextures();
599
606 void RenderOffscreenTexture();
607
612 virtual void SyncWithHardware();
613
614 bool RenderTexturesSetup = false;
615
616 WGPUSurface Surface = nullptr;
617 WGPUTexture SurfaceTexture = nullptr;
618 WGPUCommandEncoder CommandEncoder = nullptr;
619 int SurfaceConfiguredSize[2];
620 WGPUTextureFormat PreferredSurfaceTextureFormat = WGPUTextureFormat_BGRA8Unorm;
621 WGPUTextureFormat PreferredSelectorIdsTextureFormat = WGPUTextureFormat_RGBA32Uint;
622 struct vtkWGPUDepthStencil
623 {
624 WGPUTexture Texture = nullptr;
625 WGPUTextureView View = nullptr;
626 WGPUTextureFormat Format;
627 bool HasStencil;
628 };
629 vtkWGPUDepthStencil DepthStencilAttachment;
630
631 struct vtkWGPUAttachment
632 {
633 WGPUTexture Texture = nullptr;
634 WGPUTextureView View = nullptr;
635 WGPUTextureFormat Format;
636 };
637 vtkWGPUAttachment ColorAttachment;
638 vtkWGPUAttachment IdsAttachment;
639
640 struct vtkWGPUUserStagingPixelData
641 {
642 // Defined out of line: the members need the WGPU_*_INIT defaults, and
643 // vtkWrapHierarchy cannot expand those macros.
644 vtkWGPUUserStagingPixelData();
645 WGPUOrigin3D Origin;
646 WGPUExtent3D Extent;
647 WGPUTexelCopyBufferLayout Layout;
648 WGPUBuffer Buffer = nullptr; // for SetPixelData
649 };
650 vtkWGPUUserStagingPixelData StagingPixelData;
651
652 struct vtkWGPUFullScreenQuad
653 {
654 std::string Key;
655 WGPUBindGroup BindGroup = nullptr;
656 };
657 vtkWGPUFullScreenQuad ColorCopyRenderPipeline;
658
659 vtkSmartPointer<vtkWebGPUConfiguration> WGPUConfiguration;
660 vtkNew<vtkWebGPUShaderDatabase> WGPUShaderDatabase;
661 vtkNew<vtkWebGPURenderPipelineCache> WGPUPipelineCache;
662 vtkNew<vtkWebGPURenderTextureCache> WGPUTextureCache;
663
664 const WGPUSurfaceDescriptor* CustomSurfaceDescriptor = nullptr;
665
666 vtkSmartPointer<vtkWebGPUComputePipeline> DepthCopyPipeline;
667 vtkSmartPointer<vtkWebGPUComputePass> DepthCopyPass;
668 int DepthCopyBufferIndex = 0;
669 int DepthCopyTextureIndex = 0;
670
671 int ScreenSize[2];
672
673 // Render textures acquired by the user on this render window. They are kept here in case the
674 // render window is resized, in which case, we'll need to resize the render textures --> We need
675 // access to the textures
676 std::vector<vtkSmartPointer<vtkWebGPUComputeRenderTexture>> ComputeRenderTextures;
677};
678
679#define vtkWebGPURenderWindow_OVERRIDE_ATTRIBUTES vtkWebGPURenderWindow::CreateOverrideAttributes()
680VTK_ABI_NAMESPACE_END
681#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.
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.
Render textures are returned by calls to vtkWebGPUPolyDataMapper::AcquireXXXXRenderTexture() and repr...
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.
WGPUAdapter GetAdapter()
Get the webgpu adapter.
static vtkOverrideAttribute * CreateOverrideAttributes()
virtual void DestroyWindow()
Destroy a not-off-screen window.
WGPUCommandEncoder GetCommandEncoder()
Get the currently used command encoder.
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.
WGPUDevice GetDevice()
Get the webgpu device.
WGPURenderPassEncoder NewRenderPass(WGPURenderPassDescriptor &descriptor)
Create a new render pass encoder on the webgpu device.
bool EnsureDisplay() override
Ensure RenderWindow's display is opened.
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...
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.
void Finalize() override
Call DestroyWindow.
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
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.
void FlushCommandBuffers(vtkTypeUInt32 count, WGPUCommandBuffer *buffers)
Sends a given command buffer to the device queue.
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...
const WGPUSurfaceDescriptor * GetCustomSurfaceDescriptor() const
Set/Get a custom surface descriptor to use when creating the WebGPU surface.
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.
VTK_MAYSUSPEND void SetWGPUConfiguration(vtkWebGPUConfiguration *config)
Set/Get the webgpu configuration instance.
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...
WGPUTextureFormat GetDepthStencilFormat()
Get the texture format of the depth-stencil attachment.
vtkGetNewMacro(WGPUShaderDatabase, vtkWebGPUShaderDatabase)
Get a database of all WebGPU shader source codes in VTK.
void SetCustomSurfaceDescriptor(const WGPUSurfaceDescriptor *descriptor)
Set/Get a custom surface descriptor to use when creating the WebGPU surface.
void Initialize() override
Creates the WebGPU context, swapchain, depth buffer, color attachment, ...
WGPUTextureView GetHardwareSelectorAttachmentView()
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...
WGPUTextureView GetOffscreenColorAttachmentView()
Get a view of the color attachment used in the offscreen render target.
vtkSmartPointer< vtkWebGPUComputeRenderTexture > AcquireDepthBufferRenderTexture()
Returns a vtkWebGPUComputeRenderTexture ready to be added to a compute pipeline using vtkWebGPUComput...
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...
WGPUTextureView GetDepthStencilView()
Get a view of the depth-stencil attachment used in the offscreen render target.
WGPURenderBundleEncoder NewRenderBundleEncoder(WGPURenderBundleEncoderDescriptor &descriptor)
Create a new render bundle 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...
WGPUTextureFormat GetPreferredSurfaceTextureFormat()
Get the texture format preferred for the surface.
std::function< void(const void *mappedData, int bytesPerRow, void *userdata)> TextureMapCallback
VTK_MAYSUSPEND void GetIdsData(int x1, int y1, int x2, int y2, vtkTypeUInt32Array *data)
Get the Ids data from the last render.
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.
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.
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...
WGPUTextureFormat GetPreferredSelectorIdsTextureFormat()
Get the texture format preferred for selector IDs.
virtual void CreateAWindow()
Create a not-off-screen window.
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_MAYSUSPEND
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL
#define VTK_MARSHALAUTO
#define VTK_MARSHALEXCLUDE(reason)
#define VTK_NEWINSTANCE