VTK: Rendering/WebGPU/vtkWebGPURenderer.h Source File

| 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;
18 class vtkFrameBufferObjectBase;
19
20 VTK_ABI_NAMESPACE_BEGIN
21 class VTKRENDERINGWEBGPU_EXPORT vtkWebGPURenderer : public vtkRenderer
22 {
23 public:
24 static vtkWebGPURenderer* New();
25 vtkTypeMacro(vtkWebGPURenderer, vtkRenderer);
26 void PrintSelf(ostream& os, vtkIndent indent) override;
27
28 struct RenderPipelineBatch
29 {
30 vtkSmartPointer<vtkPropCollection> Props;
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
39 enum LightingComplexityEnum
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:
104 vtkWebGPURenderer();
105 ~vtkWebGPURenderer() override;
106
112 int UpdateOpaquePolygonalGeometry() override;
113 int UpdateTranslucentPolygonalGeometry() override;
114
118 void DeviceRenderOpaqueGeometry(vtkFrameBufferObjectBase* fbo) override;
119 void DeviceRenderTranslucentPolygonalGeometry(vtkFrameBufferObjectBase* fbo) override;
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;
159 struct vtkWGPUPropItem
160 {
161 wgpu::RenderBundle Bundle = nullptr;
162 vtkSmartPointer<vtkTypeUInt32Array> DynamicOffsets;
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
172 vtkMTimeType LightingUpdateTime;
173 vtkTimeStamp LightingUploadTimestamp;
174
175 struct
176 {
177 uint32_t Hits = 0;
178 uint32_t Misses = 0;
179 uint32_t TotalRequests = 0;
180 } BundleCacheStats;
181
185 vtkSmartPointer<vtkTransform> UserLightTransform;
186
187 private:
188 vtkWebGPURenderer(const vtkWebGPURenderer&) = delete;
189 void operator=(const vtkWebGPURenderer&) = delete;
190 };
191
192 VTK_ABI_NAMESPACE_END
193 #endif
vtkAbstractMapper
abstract class specifies interface to map data
Definition: vtkAbstractMapper.h:46
vtkFrameBufferObjectBase
abstract interface to OpenGL FBOs
Definition: vtkFrameBufferObjectBase.h:22
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:38
vtkRenderState
Context in which a vtkRenderPass will render.
Definition: vtkRenderState.h:30
vtkRenderer
abstract specification for renderers
Definition: vtkRenderer.h:71
vtkSmartPointer< vtkPropCollection >
vtkTexture
handles properties associated with a texture map
Definition: vtkTexture.h:67
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:34
vtkTransform
describes linear transformations via a 4x4 matrix
Definition: vtkTransform.h:59
vtkWebGPURenderer
Definition: vtkWebGPURenderer.h:22
vtkWebGPURenderer::SetupSceneBindGroup
void SetupSceneBindGroup()
vtkWebGPURenderer::ReleaseGraphicsResources
void ReleaseGraphicsResources(vtkWindow *w) override
vtkWebGPURenderer::SceneTransformBuffer
wgpu::Buffer SceneTransformBuffer
Definition: vtkWebGPURenderer.h:141
vtkWebGPURenderer::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkWebGPURenderer::LightingUploadTimestamp
vtkTimeStamp LightingUploadTimestamp
Definition: vtkWebGPURenderer.h:173
vtkWebGPURenderer::EndEncoding
void EndEncoding()
vtkWebGPURenderer::CreateBuffers
void CreateBuffers()
vtkWebGPURenderer::LightingComplexityEnum
LightingComplexityEnum
Definition: vtkWebGPURenderer.h:40
vtkWebGPURenderer::GetSceneBindGroup
wgpu::BindGroup GetSceneBindGroup()
Definition: vtkWebGPURenderer.h:73
vtkWebGPURenderer::BeginEncoding
void BeginEncoding()
vtkWebGPURenderer::WriteSceneTransformsBuffer
std::size_t WriteSceneTransformsBuffer(std::size_t offset=0)
vtkWebGPURenderer::UpdateOpaquePolygonalGeometry
int UpdateOpaquePolygonalGeometry() override
Request mappers to run the vtkAlgorithm pipeline (if needed) and consequently update device buffers c...
vtkWebGPURenderer::WriteLightsBuffer
std::size_t WriteLightsBuffer(std::size_t offset=0)
vtkWebGPURenderer::WriteActorBlocksBuffer
std::size_t WriteActorBlocksBuffer(std::size_t offset=0)
vtkWebGPURenderer::PropWGPUItems
std::unordered_map< vtkProp *, vtkWGPUPropItem > PropWGPUItems
Definition: vtkWebGPURenderer.h:164
vtkWebGPURenderer::UpdateTranslucentPolygonalGeometry
int UpdateTranslucentPolygonalGeometry() override
Ask all props to update and draw any translucent polygonal geometry.
vtkWebGPURenderer::UpdateGeometry
int UpdateGeometry(vtkFrameBufferObjectBase *fbo=nullptr) override
Ask all props to update themselves.
vtkWebGPURenderer::GetUserLightTransform
vtkTransform * GetUserLightTransform()
Set the user light transform applied after the camera transform.
vtkWebGPURenderer::HasShaderCache
wgpu::ShaderModule HasShaderCache(const std::string &source)
vtkWebGPURenderer::New
static vtkWebGPURenderer * New()
vtkWebGPURenderer::UpdateLights
int UpdateLights() override
Ask all lights to load themselves into rendering pipeline.
vtkWebGPURenderer::SetUserLightTransform
void SetUserLightTransform(vtkTransform *transform)
Set the user light transform applied after the camera transform.
vtkWebGPURenderer::WGPURenderEncoder
wgpu::RenderPassEncoder WGPURenderEncoder
Definition: vtkWebGPURenderer.h:140
vtkWebGPURenderer::DeviceRenderOpaqueGeometry
void DeviceRenderOpaqueGeometry(vtkFrameBufferObjectBase *fbo) override
Request mappers to bind descriptor sets (bind groups) and encode draw commands.
vtkWebGPURenderer::GetActorBindGroup
wgpu::BindGroup GetActorBindGroup()
Definition: vtkWebGPURenderer.h:72
vtkWebGPURenderer::PopulateBindgroupLayouts
void PopulateBindgroupLayouts(std::vector< wgpu::BindGroupLayout > &layouts)
Definition: vtkWebGPURenderer.h:75
vtkWebGPURenderer::SceneLightsBuffer
wgpu::Buffer SceneLightsBuffer
Definition: vtkWebGPURenderer.h:142
vtkWebGPURenderer::UpdateBufferData
std::size_t UpdateBufferData()
vtkWebGPURenderer::Clear
void Clear() override
Clear the image to the background color.
vtkWebGPURenderer::DeviceRenderTranslucentPolygonalGeometry
void DeviceRenderTranslucentPolygonalGeometry(vtkFrameBufferObjectBase *fbo) override
Render translucent polygonal geometry.
vtkWebGPURenderer::RenderGeometry
int RenderGeometry()
Request props to encode render commands.
vtkWebGPURenderer::DeviceRender
void DeviceRender() override
Create an image.
vtkWebGPURenderer::ActorBlocksBuffer
wgpu::Buffer ActorBlocksBuffer
Definition: vtkWebGPURenderer.h:143
vtkWebGPURenderer::SceneBindGroup
wgpu::BindGroup SceneBindGroup
Definition: vtkWebGPURenderer.h:144
vtkWebGPURenderer::UserLightTransform
vtkSmartPointer< vtkTransform > UserLightTransform
Optional user transform for lights.
Definition: vtkWebGPURenderer.h:185
vtkWebGPURenderer::~vtkWebGPURenderer
~vtkWebGPURenderer() override
vtkWebGPURenderer::LightIDs
std::vector< std::size_t > LightIDs
Definition: vtkWebGPURenderer.h:170
vtkWebGPURenderer::GetRenderPassEncoder
wgpu::RenderPassEncoder GetRenderPassEncoder()
Definition: vtkWebGPURenderer.h:71
vtkWebGPURenderer::ActorBindGroup
wgpu::BindGroup ActorBindGroup
Definition: vtkWebGPURenderer.h:147
vtkWebGPURenderer::Bundles
std::vector< wgpu::RenderBundle > Bundles
Definition: vtkWebGPURenderer.h:158
vtkWebGPURenderer::SceneBindGroupLayout
wgpu::BindGroupLayout SceneBindGroupLayout
Definition: vtkWebGPURenderer.h:145
vtkWebGPURenderer::SetEnvironmentTexture
void SetEnvironmentTexture(vtkTexture *texture, bool isSRGB=false) override
Set/Get the environment texture used for image based lighting.
vtkWebGPURenderer::ShaderCache
std::unordered_map< std::string, wgpu::ShaderModule > ShaderCache
Definition: vtkWebGPURenderer.h:166
vtkWebGPURenderer::vtkWebGPURenderer
vtkWebGPURenderer()
vtkWebGPURenderer::InsertShader
void InsertShader(const std::string &source, wgpu::ShaderModule shader)
vtkWebGPURenderer::ActorBindGroupLayout
wgpu::BindGroupLayout ActorBindGroupLayout
Definition: vtkWebGPURenderer.h:148
vtkWebGPURenderer::LightingUpdateTime
vtkMTimeType LightingUpdateTime
Definition: vtkWebGPURenderer.h:172
vtkWebGPURenderer::SetupBindGroupLayouts
void SetupBindGroupLayouts()
vtkWebGPURenderer::SetupActorBindGroup
void SetupActorBindGroup()
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:37
vtkX3D::offset
@ offset
Definition: vtkX3D.h:438
vtkX3D::string
@ string
Definition: vtkX3D.h:490
vtkWebGPURenderer::RenderPipelineBatch
Definition: vtkWebGPURenderer.h:29
vtkWebGPURenderer::RenderPipelineBatch::Props
vtkSmartPointer< vtkPropCollection > Props
Definition: vtkWebGPURenderer.h:30
vtkWebGPURenderer::RenderPipelineBatch::Pipeline
wgpu::RenderPipeline Pipeline
Definition: vtkWebGPURenderer.h:31
vtkWebGPURenderer::vtkWGPUPropItem
Definition: vtkWebGPURenderer.h:160
vtkWebGPURenderer::vtkWGPUPropItem::DynamicOffsets
vtkSmartPointer< vtkTypeUInt32Array > DynamicOffsets
Definition: vtkWebGPURenderer.h:162
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:964
vtkRenderer.h
vtkSmartPointer.h
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270
vtk_wgpu.h