VTK  9.3.0
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
17class vtkRenderState;
19
20VTK_ABI_NAMESPACE_BEGIN
21class VTKRENDERINGWEBGPU_EXPORT vtkWebGPURenderer : public vtkRenderer
22{
23public:
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
64
65 int UpdateLights() override;
66
67 void SetEnvironmentTexture(vtkTexture* texture, bool isSRGB = false) override;
68
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
92
94
98 vtkSetMacro(UseRenderBundles, bool);
99 vtkBooleanMacro(UseRenderBundles, bool);
100 vtkGetMacro(UseRenderBundles, bool);
102
103protected:
106
114
120
121 // Setup scene and actor bindgroups. Actor has dynamic offsets.
123 // Creates buffers for the bind groups.
125 // Updates the bound buffers with data.
126 std::size_t UpdateBufferData();
127 // Creates scene bind group.
129 // Creates actor bind group.
131
132 // Start, finish recording commands with render pass encoder
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;
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
187private:
188 vtkWebGPURenderer(const vtkWebGPURenderer&) = delete;
189 void operator=(const vtkWebGPURenderer&) = delete;
190};
191
192VTK_ABI_NAMESPACE_END
193#endif
abstract class specifies interface to map data
abstract interface to OpenGL FBOs
a simple class to control print indentation
Definition vtkIndent.h:29
Context in which a vtkRenderPass will render.
abstract specification for renderers
Definition vtkRenderer.h:59
Hold a reference to a vtkObjectBase instance.
handles properties associated with a texture map
Definition vtkTexture.h:58
record modification and/or execution time
describes linear transformations via a 4x4 matrix
void SetupSceneBindGroup()
void ReleaseGraphicsResources(vtkWindow *w) override
static vtkWebGPURenderer * New()
wgpu::Buffer SceneTransformBuffer
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkTimeStamp LightingUploadTimestamp
wgpu::BindGroup GetSceneBindGroup()
std::size_t WriteSceneTransformsBuffer(std::size_t offset=0)
int UpdateOpaquePolygonalGeometry() override
Request mappers to run the vtkAlgorithm pipeline (if needed) and consequently update device buffers c...
std::size_t WriteLightsBuffer(std::size_t offset=0)
std::size_t WriteActorBlocksBuffer(std::size_t offset=0)
std::unordered_map< vtkProp *, vtkWGPUPropItem > PropWGPUItems
int UpdateTranslucentPolygonalGeometry() override
Ask all props to update and draw any translucent polygonal geometry.
int UpdateGeometry(vtkFrameBufferObjectBase *fbo=nullptr) override
Ask all props to update themselves.
wgpu::ShaderModule HasShaderCache(const std::string &source)
int UpdateLights() override
Ask all lights to load themselves into rendering pipeline.
void SetUserLightTransform(vtkTransform *transform)
Set the user light transform applied after the camera transform.
wgpu::RenderPassEncoder WGPURenderEncoder
void DeviceRenderOpaqueGeometry(vtkFrameBufferObjectBase *fbo) override
Request mappers to bind descriptor sets (bind groups) and encode draw commands.
wgpu::BindGroup GetActorBindGroup()
void PopulateBindgroupLayouts(std::vector< wgpu::BindGroupLayout > &layouts)
wgpu::Buffer SceneLightsBuffer
std::size_t UpdateBufferData()
void Clear() override
Clear the image to the background color.
void DeviceRenderTranslucentPolygonalGeometry(vtkFrameBufferObjectBase *fbo) override
Render translucent polygonal geometry.
vtkTransform * GetUserLightTransform()
Set the user light transform applied after the camera transform.
int RenderGeometry()
Request props to encode render commands.
void DeviceRender() override
Create an image.
wgpu::Buffer ActorBlocksBuffer
wgpu::BindGroup SceneBindGroup
vtkSmartPointer< vtkTransform > UserLightTransform
Optional user transform for lights.
~vtkWebGPURenderer() override
std::vector< std::size_t > LightIDs
wgpu::RenderPassEncoder GetRenderPassEncoder()
wgpu::BindGroup ActorBindGroup
std::vector< wgpu::RenderBundle > Bundles
wgpu::BindGroupLayout SceneBindGroupLayout
void SetEnvironmentTexture(vtkTexture *texture, bool isSRGB=false) override
Set/Get the environment texture used for image based lighting.
std::unordered_map< std::string, wgpu::ShaderModule > ShaderCache
void InsertShader(const std::string &source, wgpu::ShaderModule shader)
wgpu::BindGroupLayout ActorBindGroupLayout
vtkMTimeType LightingUpdateTime
void SetupBindGroupLayouts()
void SetupActorBindGroup()
window superclass for vtkRenderWindow
Definition vtkWindow.h:25
vtkSmartPointer< vtkPropCollection > Props
vtkSmartPointer< vtkTypeUInt32Array > DynamicOffsets
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:270