VTK  9.3.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
3 #ifndef vtkWebGPURenderWindow_h
4 #define vtkWebGPURenderWindow_h
5 
6 #include "vtkRenderWindow.h"
7 
8 #include "vtkRenderingWebGPUModule.h" // for export macro
9 #include "vtkTypeUInt8Array.h" // for ivar
10 #include "vtk_wgpu.h" // for webgpu
11 
12 VTK_ABI_NAMESPACE_BEGIN
13 
14 class VTKRENDERINGWEBGPU_EXPORT vtkWebGPURenderWindow : public vtkRenderWindow
15 {
16 public:
18  void PrintSelf(ostream& os, vtkIndent indent) override;
19 
20  // for high power preference, we shall request discrete GPU instead of integrated GPU or the CPU.
21  void SetPowerPreference(bool high = true)
22  {
23  this->PowerPreference =
24  high ? wgpu::PowerPreference::HighPerformance : wgpu::PowerPreference::LowPower;
25  this->Modified();
26  }
27 
32  virtual bool Initialize() = 0;
33 
37  virtual void CreateAWindow() = 0;
38 
42  virtual void DestroyWindow() = 0;
43 
44  void Start() override;
45 
49  void End() override;
50 
54  void Render() override;
55 
60  void StereoMidpoint() override;
61  void Frame() override;
62 
63  const char* GetRenderingBackend() override;
64 
68  void ReadPixels();
69 
71 
76  unsigned char* GetPixelData(int x, int y, int x2, int y2, int front, int right) override;
77  int GetPixelData(
78  int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right) override;
79  int SetPixelData(
80  int x, int y, int x2, int y2, unsigned char* data, int front, int right) override;
81  int SetPixelData(
82  int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front, int right) override;
84 
86 
89  float* GetRGBAPixelData(int x, int y, int x2, int y2, int front, int right = 0) override;
90  int GetRGBAPixelData(
91  int x, int y, int x2, int y2, int front, vtkFloatArray* data, int right = 0) override;
92  int SetRGBAPixelData(
93  int x, int y, int x2, int y2, float* data, int front, int blend = 0, int right = 0) override;
94  int SetRGBAPixelData(int x, int y, int x2, int y2, vtkFloatArray* data, int front, int blend = 0,
95  int right = 0) override;
96  void ReleaseRGBAPixelData(float* data) override;
97  unsigned char* GetRGBACharPixelData(
98  int x, int y, int x2, int y2, int front, int right = 0) override;
100  int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right = 0) override;
101  int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char* data, int front,
102  int blend = 0, int right = 0) override;
103  int SetRGBACharPixelData(int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front,
104  int blend = 0, int right = 0) override;
106 
108 
111  float* GetZbufferData(int x1, int y1, int x2, int y2) override;
112  int GetZbufferData(int x1, int y1, int x2, int y2, float* z) override;
113  int GetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
114  int SetZbufferData(int x1, int y1, int x2, int y2, float* buffer) override;
115  int SetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
117 
122  int GetColorBufferSizes(int* rgba) override;
126  void WaitForCompletion() override;
127 
131  int SupportsOpenGL() override;
132 
136  const char* ReportCapabilities() override;
137 
142  bool InitializeFromCurrentContext() override;
143 
148  void ReleaseGraphicsResources(vtkWindow*) override;
149 
150  inline wgpu::RenderPassEncoder NewRenderPass(wgpu::RenderPassDescriptor& descriptor)
151  {
152  return this->CommandEncoder.BeginRenderPass(&descriptor);
153  }
154 
155  inline wgpu::RenderBundleEncoder NewRenderBundleEncoder(
156  wgpu::RenderBundleEncoderDescriptor& descriptor)
157  {
158  return this->Device.CreateRenderBundleEncoder(&descriptor);
159  }
160 
161  inline wgpu::CommandEncoder GetCommandEncoder() { return this->CommandEncoder; }
162  inline wgpu::TextureView GetOffscreenColorAttachmentView() { return this->ColorAttachment.View; }
163  inline wgpu::TextureView GetDepthStencilView() { return this->DepthStencil.View; }
164  inline wgpu::TextureFormat GetDepthStencilFormat() { return this->DepthStencil.Format; }
165  inline bool HasStencil() { return this->DepthStencil.HasStencil; }
166  inline wgpu::Device GetDevice() { return this->Device; }
167 
168  wgpu::TextureFormat GetPreferredSwapChainTextureFormat();
169 
170 protected:
172  ~vtkWebGPURenderWindow() override;
173 
174  bool WGPUInit();
175  void WGPUFinalize();
176 
177  void CreateSwapChain();
178  void DestroySwapChain();
179 
180  void CreateOffscreenColorAttachments();
181  void DestroyOffscreenColorAttachments();
182 
183  void CreateDepthStencilTexture();
184  void DestroyDepthStencilTexture();
185 
186  void CreateFSQGraphicsPipeline();
187  void DestroyFSQGraphicsPipeline();
188 
189  void RenderOffscreenTexture();
190 
191  void FlushCommandBuffers(vtkTypeUInt32 count, wgpu::CommandBuffer* buffers);
192 
193  bool WGPUInitialized = false;
194  wgpu::PowerPreference PowerPreference = wgpu::PowerPreference::HighPerformance;
195  wgpu::Adapter Adapter;
196  wgpu::Device Device;
197  wgpu::Surface Surface;
198  wgpu::CommandEncoder CommandEncoder;
199 
201  {
202  wgpu::SwapChain Instance;
203  wgpu::TextureView Framebuffer;
204  wgpu::TextureFormat TexFormat;
205  wgpu::PresentMode PresentMode;
206  int Width = 0;
207  int Height = 0;
208  };
210 
212  {
213  wgpu::Texture Texture;
214  wgpu::TextureView View;
215  wgpu::TextureFormat Format;
217  };
219 
221  {
222  wgpu::Texture Texture;
223  wgpu::TextureView View;
224  wgpu::TextureFormat Format;
225  wgpu::Buffer OffscreenBuffer;
226  };
228 
230  {
231  wgpu::Origin3D Origin;
232  wgpu::Extent3D Extent;
233  wgpu::TextureDataLayout Layout;
234  wgpu::Buffer Buffer; // for SetPixelData
235  };
237 
239  {
240  wgpu::RenderPipeline Pipeline;
241  wgpu::BindGroup BindGroup;
242  };
244 
246  {
248  wgpu::Buffer src;
249  unsigned long size;
250  } BufferMapReadContext;
251 
253 
254 private:
256  void operator=(const vtkWebGPURenderWindow&) = delete;
257 };
258 
259 VTK_ABI_NAMESPACE_END
260 #endif
virtual void Start()
Start the rendering process for a frame.
wgpu::RenderBundleEncoder NewRenderBundleEncoder(wgpu::RenderBundleEncoderDescriptor &descriptor)
void SetPowerPreference(bool high=true)
virtual float * GetZbufferData(int, int, int, int)
Set/Get the zbuffer data from the frame buffer.
virtual unsigned char * GetPixelData(int, int, int, int, int, int=0)
Get the pixel data of an image, transmitted as RGBRGBRGB.
Definition: vtkWindow.h:184
virtual int SetPixelData(int, int, int, int, unsigned char *, int, int=0)
Set/Get the pixel data of an image, transmitted as RGBRGBRGB.
vtkWGPUUserStagingPixelData StagingPixelData
virtual int GetColorBufferSizes(int *)
Get the size of the color buffer.
dynamic, self-adjusting array of float
Definition: vtkFloatArray.h:30
void Render() override
Ask each renderer owned by this RenderWindow to render its image and synchronize this process...
vtkWGPUFullScreenQuad FSQ
vtkWGPUColorAttachment ColorAttachment
window superclass for vtkRenderWindow
Definition: vtkWindow.h:27
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.
vtkSmartPointer< vtkTypeUInt8Array > dst
wgpu::CommandEncoder GetCommandEncoder()
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
wgpu::TextureView GetOffscreenColorAttachmentView()
wgpu::TextureView GetDepthStencilView()
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 void Modified()
Update the modification time for this object.
virtual void ReleaseRGBAPixelData(float *)
Same as Get/SetPixelData except that the image also contains an alpha component.
vtkWGPUDeptStencil DepthStencil
dynamic, self-adjusting array of unsigned char
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...
virtual const char * GetRenderingBackend()
What rendering backend has the user requested.
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...
vtkNew< vtkTypeUInt8Array > CachedPixelBytes
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.
wgpu::RenderPassEncoder NewRenderPass(wgpu::RenderPassDescriptor &descriptor)
virtual void ReleaseGraphicsResources(vtkWindow *)
Release any graphics resources that are being consumed by this texture.
Definition: vtkWindow.h:169
wgpu::TextureFormat GetDepthStencilFormat()
wgpu::CommandEncoder CommandEncoder
virtual void Frame()
A termination method performed at the end of the rendering process to do things like swapping buffers...
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.