VTK  9.7.1
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
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 
33 VTK_ABI_NAMESPACE_BEGIN
34 
38 class vtkWebGPUTextureCache;
39 class vtkImageData;
40 class vtkTypeUInt32Array;
41 class VTKRENDERINGWEBGPU_EXPORT VTK_MARSHALAUTO vtkWebGPURenderWindow : public vtkRenderWindow
42 {
43 public:
47  static vtkWebGPURenderWindow* New();
49  static vtkOverrideAttribute* CreateOverrideAttributes();
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;
129  int GetPixelData(
130  int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right) override;
131  int SetPixelData(
132  int x, int y, int x2, int y2, unsigned char* data, int front, int right) override;
133  int SetPixelData(
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;
142  int GetRGBAPixelData(
143  int x, int y, int x2, int y2, int front, vtkFloatArray* data, int right = 0) override;
144  int SetRGBAPixelData(
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);
175  void GetIdsData(int x1, int y1, int x2, int y2, vtkTypeUInt32Array* data);
177 
182  int GetColorBufferSizes(int* rgba) override;
186  void WaitForCompletion() override;
187 
191  int SupportsOpenGL() override;
192 
196  const char* ReportCapabilities() override;
197 
202  bool InitializeFromCurrentContext() override;
203 
208  void ReleaseGraphicsResources(vtkWindow*) override;
209 
210  void SetWGPUConfiguration(vtkWebGPUConfiguration* config);
211  vtkGetSmartPointerMacro(WGPUConfiguration, vtkWebGPUConfiguration);
212 
218  vtkGetNewMacro(WGPUShaderDatabase, vtkWebGPUShaderDatabase);
219 
224  vtkGetNewMacro(WGPUPipelineCache, vtkWebGPURenderPipelineCache);
225 
230  vtkGetNewMacro(WGPUTextureCache, vtkWebGPURenderTextureCache);
231 
236  std::string PreprocessShaderSource(const std::string& source) const;
237 
241  wgpu::RenderPassEncoder NewRenderPass(wgpu::RenderPassDescriptor& descriptor);
242 
247  wgpu::RenderBundleEncoder NewRenderBundleEncoder(wgpu::RenderBundleEncoderDescriptor& descriptor);
248 
253  wgpu::CommandEncoder GetCommandEncoder();
254 
258  void CreateCommandEncoder();
259 
263  void FlushCommandBuffers(vtkTypeUInt32 count, wgpu::CommandBuffer* buffers);
264 
268  wgpu::TextureView GetOffscreenColorAttachmentView();
269  wgpu::TextureView GetHardwareSelectorAttachmentView();
270 
274  wgpu::TextureView GetDepthStencilView();
275 
279  wgpu::TextureFormat GetDepthStencilFormat();
280 
284  bool HasStencil();
285 
289  wgpu::Device GetDevice();
290 
294  wgpu::Adapter GetAdapter();
295 
299  wgpu::TextureFormat GetPreferredSurfaceTextureFormat();
300 
304  wgpu::TextureFormat GetPreferredSelectorIdsTextureFormat();
305 
307 
316  vtkSmartPointer<vtkWebGPUComputeRenderTexture> AcquireDepthBufferRenderTexture();
317  vtkSmartPointer<vtkWebGPUComputeRenderTexture> AcquireFramebufferRenderTexture();
319 
320  using TextureMapCallback =
321  std::function<void(const void* mappedData, int bytesPerRow, void* userdata)>;
322 
324  {
325  ColorRGBA,
326  ColorRGB,
327  Depth,
328  Ids,
329  };
330  vtkSmartPointer<vtkImageData> SaveAttachmentToVTI(AttachmentTypeForVTISnapshot type);
331 
335  bool EnsureDisplay() override;
336 
340  void* GetGenericDisplayId() override;
341 
342 protected:
344  ~vtkWebGPURenderWindow() override;
345 
354  virtual std::string MakeDefaultWindowNameWithBackend();
355 
356 private:
357  // For accessing SubmitCommandBuffer to submit custom prop render work
359 
361  void operator=(const vtkWebGPURenderWindow&) = delete;
362 
367  void InitializeRendererComputePipelines();
368 
373  void SubmitCommandBuffer(int count, wgpu::CommandBuffer* commandBuffer);
374 
378  void PostRenderComputePipelines();
379 
384  void PostRasterizationRender();
385 
386  struct ComponentMapping
387  {
388  int Map[4];
389  int InComponents;
390  int OutComponents;
391  bool Valid;
392  };
393 
409  ComponentMapping GetComponentMapping(wgpu::TextureFormat format, int desiredOutComponents);
410 
411  template <typename TOutput, typename TInput>
412  struct PixelReadbackCallbackData
413  {
414  TOutput* OutputValues;
415  uint32_t Width, Height;
416  ComponentMapping Mapping;
417  std::function<TOutput(TInput)> Converter;
418  };
419 
434  template <typename TOutput, typename TInput>
435  TOutput* GetTextureDataInternal(wgpu::Texture texture, wgpu::TextureFormat format, int x1, int y1,
436  int x2, int y2, const ComponentMapping& componentMapping,
437  std::function<TOutput(TInput)> converter = nullptr);
438 
444  std::uint32_t FlipY(std::uint32_t y);
445 
455  void ReadTextureFromGPU(wgpu::Texture& wgpuTexture, wgpu::TextureFormat format,
456  std::size_t mipLevel, wgpu::TextureAspect aspect, wgpu::Origin3D offsets,
457  wgpu::Extent3D extents, TextureMapCallback callback, void* userData);
458 
464  void ReadTextureFromGPU(wgpu::Texture& wgpuTexture, wgpu::TextureFormat format,
465  std::size_t mipLevel, wgpu::TextureAspect aspect, TextureMapCallback callback, void* userData);
466 
472  bool WGPUInit();
473 
481  void WGPUFinalize();
482 
488  void CreateSurface();
489 
493  void ConfigureSurface();
494 
500  void UnconfigureSurface();
501 
503 
508  void CreateOffscreenColorAttachment();
509  void DestroyOffscreenColorAttachment();
511 
513 
518  void CreateIdsAttachment();
519  void DestroyIdsAttachment();
521 
523 
528  void CreateDepthStencilAttachment();
529  void DestroyDepthStencilAttachment();
531 
533 
537  void CreateColorCopyPipeline();
538  void DestroyColorCopyPipeline();
540 
546  void RecreateComputeRenderTextures();
547 
554  void RenderOffscreenTexture();
555 
560  virtual void SyncWithHardware();
561 
562  bool RenderTexturesSetup = false;
563 
564  wgpu::Surface Surface;
565  wgpu::CommandEncoder CommandEncoder;
566  int SurfaceConfiguredSize[2];
567  wgpu::TextureFormat PreferredSurfaceTextureFormat = wgpu::TextureFormat::BGRA8Unorm;
568  wgpu::TextureFormat PreferredSelectorIdsTextureFormat = wgpu::TextureFormat::RGBA32Uint;
569  struct vtkWGPUDepthStencil
570  {
571  wgpu::Texture Texture;
572  wgpu::TextureView View;
573  wgpu::TextureFormat Format;
574  bool HasStencil;
575  };
576  vtkWGPUDepthStencil DepthStencilAttachment;
577 
578  struct vtkWGPUAttachment
579  {
580  wgpu::Texture Texture;
581  wgpu::TextureView View;
582  wgpu::TextureFormat Format;
583  };
584  vtkWGPUAttachment ColorAttachment;
585  vtkWGPUAttachment IdsAttachment;
586 
587  struct vtkWGPUUserStagingPixelData
588  {
589  wgpu::Origin3D Origin;
590  wgpu::Extent3D Extent;
591  wgpu::TexelCopyBufferLayout Layout;
592  wgpu::Buffer Buffer; // for SetPixelData
593  };
594  vtkWGPUUserStagingPixelData StagingPixelData;
595 
596  struct vtkWGPUFullScreenQuad
597  {
598  std::string Key;
599  wgpu::BindGroup BindGroup;
600  };
601  vtkWGPUFullScreenQuad ColorCopyRenderPipeline;
602 
603  vtkSmartPointer<vtkWebGPUConfiguration> WGPUConfiguration;
604  vtkNew<vtkWebGPUShaderDatabase> WGPUShaderDatabase;
605  vtkNew<vtkWebGPURenderPipelineCache> WGPUPipelineCache;
606  vtkNew<vtkWebGPURenderTextureCache> WGPUTextureCache;
607 
610  int DepthCopyBufferIndex = 0;
611  int DepthCopyTextureIndex = 0;
612 
613  int ScreenSize[2];
614 
615  // Render textures acquired by the user on this render window. They are kept here in case the
616  // render window is resized, in which case, we'll need to resize the render textures --> We need
617  // access to the textures
618  std::vector<vtkSmartPointer<vtkWebGPUComputeRenderTexture>> ComputeRenderTextures;
619 };
620 
621 #define vtkWebGPURenderWindow_OVERRIDE_ATTRIBUTES vtkWebGPURenderWindow::CreateOverrideAttributes()
622 VTK_ABI_NAMESPACE_END
623 #endif
virtual void Start()
Start the rendering process for a frame.
Attribute for vtkObjectFactory overrides.
virtual float * GetZbufferData(int, int, int, int)
Set/Get the zbuffer data from the frame buffer.
void * GetGenericDisplayId() override
Dummy stubs for vtkWindow API.
void * GetGenericContext() override
Dummy stubs for vtkWindow API.
virtual unsigned char * GetPixelData(int, int, int, int, int, int=0)
Get the pixel data of an image, transmitted as RGBRGBRGB.
Definition: vtkWindow.h:201
virtual int SetPixelData(int, int, int, int, unsigned char *, int, int=0)
Set/Get the pixel data of an image, transmitted as RGBRGBRGB.
virtual int GetColorBufferSizes(int *)
Get the size of the color buffer.
static vtkRenderWindow * New()
Construct an instance of vtkRenderWindow with its screen size set to 300x300, borders turned on...
Class to create and retrieve render Textures based on a given key.
dynamic, self-adjusting array of float
Definition: vtkFloatArray.h:31
Hold a reference to a vtkObjectBase instance.
Definition: vtkMeta.h:23
void Render() override
Ask each renderer owned by this RenderWindow to render its image and synchronize this process...
virtual void Finalize()
Finalize the rendering process.
window superclass for vtkRenderWindow
Definition: vtkWindow.h:41
virtual const char * ReportCapabilities()
Get report of capabilities for the render window.
virtual int SupportsOpenGL()
Does this render window support OpenGL? 0-false, 1-true.
virtual int SetZbufferData(int, int, int, int, float *)
Set/Get the zbuffer data from the frame buffer.
a simple class to control print indentation
Definition: vtkIndent.h:28
topologically and geometrically regular array of data
Definition: vtkImageData.h:45
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
virtual void WaitForCompletion()
Block the thread until the actual rendering is finished().
virtual bool EnsureDisplay()
These are window system independent methods that are used to help interface vtkWindow to native windo...
Definition: vtkWindow.h:62
virtual void ReleaseRGBAPixelData(float *)
Same as Get/SetPixelData except that the image also contains an alpha component.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_NEWINSTANCE
dynamic, self-adjusting array of unsigned char
vtkGetNewMacro(RenderTimer, vtkRenderTimerLog)
Get the render timer log for this window.
virtual int SetRGBAPixelData(int, int, int, int, float *, int, int=0, int=0)
Same as Get/SetPixelData except that the image also contains an alpha component.
create a window for renderers to draw into
virtual bool InitializeFromCurrentContext()
Initialize the render window from the information associated with the currently activated OpenGL cont...
Class to add and retrieve source code for shader files for a specified key.
virtual const char * GetRenderingBackend()
What rendering backend has the user requested.
std::function< void(const void *mappedData, int bytesPerRow, void *userdata)> TextureMapCallback
Create a webgpu device for use in rendering and compute pipelines.
virtual unsigned char * GetRGBACharPixelData(int, int, int, int, int, int=0)
Same as Get/SetPixelData except that the image also contains an alpha component.
virtual void StereoMidpoint()
Intermediate method performs operations required between the rendering of the left and right eye...
virtual void End()
Update the system, if needed, at end of render process.
virtual float * GetRGBAPixelData(int, int, int, int, int, int=0)
Same as Get/SetPixelData except that the image also contains an alpha component.
This culler does both frustum culling and occlusion culling.
#define VTK_MARSHALAUTO
WebGPU rendering window.
virtual void ReleaseGraphicsResources(vtkWindow *)
Release any graphics resources that are being consumed by this texture.
Definition: vtkWindow.h:186
Cell is in a good state.
virtual void Frame()
A termination method performed at the end of the rendering process to do things like swapping buffers...
Class to create and retrieve render pipelines based on a given key.
virtual int SetRGBACharPixelData(int, int, int, int, unsigned char *, int, int=0, int=0)
Same as Get/SetPixelData except that the image also contains an alpha component.
virtual void Initialize()
Initializes the rendering process.