VTK  9.3.1
vtkWebGPURenderer.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 vtkWebGPURenderer_h
4 #define vtkWebGPURenderer_h
5 
6 #include "vtkRenderer.h"
7 
8 #include "vtkRenderingWebGPUModule.h" // for export macro
9 #include "vtkSmartPointer.h" // for ivar
10 #include "vtkTypeUInt32Array.h" // for ivar
11 #include "vtk_wgpu.h" // for webgpu
12 
13 #include <string> // for ivar
14 #include <unordered_map> // for ivar
15 
16 class vtkAbstractMapper;
17 class vtkRenderState;
19 
20 VTK_ABI_NAMESPACE_BEGIN
21 class VTKRENDERINGWEBGPU_EXPORT vtkWebGPURenderer : public vtkRenderer
22 {
23 public:
24  static vtkWebGPURenderer* New();
26  void PrintSelf(ostream& os, vtkIndent indent) override;
27 
29  {
31  wgpu::RenderPipeline Pipeline;
32  };
33 
34  // get the complexity of the current lights as a int
35  // 0 = no lighting
36  // 1 = headlight
37  // 2 = directional lights
38  // 3 = positional lights
40  {
41  NoLighting = 0,
42  Headlight = 1,
43  Directional = 2,
44  Positional = 3
45  };
46  vtkGetMacro(LightingComplexity, int);
47 
48  void DeviceRender() override;
49 
50  void Clear() override;
51 
58  int UpdateGeometry(vtkFrameBufferObjectBase* fbo = nullptr) override;
59 
63  int RenderGeometry();
64 
65  int UpdateLights() override;
66 
67  void SetEnvironmentTexture(vtkTexture* texture, bool isSRGB = false) override;
68 
69  void ReleaseGraphicsResources(vtkWindow* w) override;
70 
71  inline wgpu::RenderPassEncoder GetRenderPassEncoder() { return this->WGPURenderEncoder; }
72  inline wgpu::BindGroup GetActorBindGroup() { return this->ActorBindGroup; }
73  inline wgpu::BindGroup GetSceneBindGroup() { return this->SceneBindGroup; }
74 
75  inline void PopulateBindgroupLayouts(std::vector<wgpu::BindGroupLayout>& layouts)
76  {
77  layouts.emplace_back(this->SceneBindGroupLayout);
78  layouts.emplace_back(this->ActorBindGroupLayout);
79  }
80 
81  wgpu::ShaderModule HasShaderCache(const std::string& source);
82  void InsertShader(const std::string& source, wgpu::ShaderModule shader);
83 
85 
89  void SetUserLightTransform(vtkTransform* transform);
90  vtkTransform* GetUserLightTransform();
92 
94 
98  vtkSetMacro(UseRenderBundles, bool);
99  vtkBooleanMacro(UseRenderBundles, bool);
100  vtkGetMacro(UseRenderBundles, bool);
102 
103 protected:
105  ~vtkWebGPURenderer() override;
106 
112  int UpdateOpaquePolygonalGeometry() override;
113  int UpdateTranslucentPolygonalGeometry() override;
114 
120 
121  // Setup scene and actor bindgroups. Actor has dynamic offsets.
122  void SetupBindGroupLayouts();
123  // Creates buffers for the bind groups.
124  void CreateBuffers();
125  // Updates the bound buffers with data.
126  std::size_t UpdateBufferData();
127  // Creates scene bind group.
128  void SetupSceneBindGroup();
129  // Creates actor bind group.
130  void SetupActorBindGroup();
131 
132  // Start, finish recording commands with render pass encoder
133  void BeginEncoding();
134  void EndEncoding();
135 
136  std::size_t WriteLightsBuffer(std::size_t offset = 0);
137  std::size_t WriteSceneTransformsBuffer(std::size_t offset = 0);
138  std::size_t WriteActorBlocksBuffer(std::size_t offset = 0);
139 
140  wgpu::RenderPassEncoder WGPURenderEncoder;
141  wgpu::Buffer SceneTransformBuffer;
142  wgpu::Buffer SceneLightsBuffer;
143  wgpu::Buffer ActorBlocksBuffer;
144  wgpu::BindGroup SceneBindGroup;
145  wgpu::BindGroupLayout SceneBindGroupLayout;
146 
147  wgpu::BindGroup ActorBindGroup;
148  wgpu::BindGroupLayout ActorBindGroupLayout;
149 
150 #ifdef __EMSCRIPTEN__
151  bool UseRenderBundles = true;
152 #else
153  bool UseRenderBundles = false;
154 #endif
155  // one bundle per actor. bundle gets reused every frame.
156  // these bundles can be built in parallel with vtkSMPTools. holding off because not
157  // sure how to get emscripten to thread.
158  std::vector<wgpu::RenderBundle> Bundles;
160  {
161  wgpu::RenderBundle Bundle = nullptr;
163  };
164  std::unordered_map<vtkProp*, vtkWGPUPropItem> PropWGPUItems;
165 
166  std::unordered_map<std::string, wgpu::ShaderModule> ShaderCache;
167  std::size_t NumberOfPropsUpdated = 0;
168  int LightingComplexity = 0;
169  std::size_t NumberOfLightsUsed = 0;
170  std::vector<std::size_t> LightIDs;
171 
174 
175  struct
176  {
177  uint32_t Hits = 0;
178  uint32_t Misses = 0;
179  uint32_t TotalRequests = 0;
180  } BundleCacheStats;
181 
186 
187 private:
188  vtkWebGPURenderer(const vtkWebGPURenderer&) = delete;
189  void operator=(const vtkWebGPURenderer&) = delete;
190 };
191 
192 VTK_ABI_NAMESPACE_END
193 #endif
wgpu::BindGroup ActorBindGroup
wgpu::Buffer SceneTransformBuffer
void PopulateBindgroupLayouts(std::vector< wgpu::BindGroupLayout > &layouts)
virtual void ReleaseGraphicsResources(vtkWindow *)
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270
std::unordered_map< vtkProp *, vtkWGPUPropItem > PropWGPUItems
vtkSmartPointer< vtkTypeUInt32Array > DynamicOffsets
record modification and/or execution time
Definition: vtkTimeStamp.h:24
vtkMTimeType LightingUpdateTime
wgpu::Buffer SceneLightsBuffer
abstract specification for renderers
Definition: vtkRenderer.h:61
describes linear transformations via a 4x4 matrix
Definition: vtkTransform.h:49
virtual void SetEnvironmentTexture(vtkTexture *texture, bool isSRGB=false)
Set/Get the environment texture used for image based lighting.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
window superclass for vtkRenderWindow
Definition: vtkWindow.h:27
vtkTimeStamp LightingUploadTimestamp
vtkSmartPointer< vtkPropCollection > Props
Context in which a vtkRenderPass will render.
abstract interface to OpenGL FBOs
a simple class to control print indentation
Definition: vtkIndent.h:28
wgpu::BindGroup GetSceneBindGroup()
virtual int UpdateLights()
Ask all lights to load themselves into rendering pipeline.
Definition: vtkRenderer.h:1051
handles properties associated with a texture map
Definition: vtkTexture.h:57
wgpu::RenderPassEncoder GetRenderPassEncoder()
wgpu::BindGroup SceneBindGroup
virtual int UpdateGeometry(vtkFrameBufferObjectBase *fbo=nullptr)
Ask all props to update and draw any opaque and translucent geometry.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
wgpu::Buffer ActorBlocksBuffer
abstract class specifies interface to map data
std::vector< wgpu::RenderBundle > Bundles
wgpu::BindGroupLayout ActorBindGroupLayout
wgpu::BindGroupLayout SceneBindGroupLayout
virtual void DeviceRender()
Create an image.
Definition: vtkRenderer.h:294
virtual int UpdateTranslucentPolygonalGeometry()
Ask all props to update and draw any translucent polygonal geometry.
wgpu::RenderPassEncoder WGPURenderEncoder
virtual void DeviceRenderTranslucentPolygonalGeometry(vtkFrameBufferObjectBase *fbo=nullptr)
Render translucent polygonal geometry.
static vtkRenderer * New()
Create a vtkRenderer with a black background, a white ambient light, two-sided lighting turned on...
vtkSmartPointer< vtkTransform > UserLightTransform
Optional user transform for lights.
virtual int UpdateOpaquePolygonalGeometry()
Ask all props to update and draw any opaque polygonal geometry.
std::unordered_map< std::string, wgpu::ShaderModule > ShaderCache
wgpu::BindGroup GetActorBindGroup()
virtual void DeviceRenderOpaqueGeometry(vtkFrameBufferObjectBase *fbo=nullptr)
Render opaque polygonal geometry.
std::vector< std::size_t > LightIDs
virtual void Clear()
Clear the image to the background color.
Definition: vtkRenderer.h:324