VTK  9.4.2
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 "vtkWebGPUActor.h" // for the actors rendered last frame
12#include "vtkWebGPUComputePipeline.h" // for the compute pipelines used by this renderer
13#include "vtk_wgpu.h" // for webgpu
14
15#include <string> // for ivar
16#include <unordered_map> // for ivar
17#include <unordered_set> // for the set of actors rendered last frame
18
20class vtkRenderState;
22
23VTK_ABI_NAMESPACE_BEGIN
24
26
27class VTKRENDERINGWEBGPU_EXPORT vtkWebGPURenderer : public vtkRenderer
28{
29public:
32 void PrintSelf(ostream& os, vtkIndent indent) override;
33
35 {
37 wgpu::RenderPipeline Pipeline;
38 };
39
40 // get the complexity of the current lights as a int
41 // 0 = no lighting
42 // 1 = headlight
43 // 2 = directional lights
44 // 3 = positional lights
46 {
47 NoLighting = 0,
48 Headlight = 1,
49 Directional = 2,
50 Positional = 3
51 };
52 vtkGetMacro(LightingComplexity, int);
53
54 void DeviceRender() override;
55
60
67 int UpdateGeometry(vtkFrameBufferObjectBase* fbo = nullptr) override;
68
77
83
85
89 const std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>>& GetSetupPreRenderComputePipelines();
90 const std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>>&
93
98
99 int UpdateLights() override;
100
101 void SetEnvironmentTexture(vtkTexture* texture, bool isSRGB = false) override;
102
104
105 inline wgpu::RenderPassEncoder GetRenderPassEncoder() { return this->WGPURenderEncoder; }
106 inline wgpu::BindGroup GetActorBindGroup() { return this->ActorBindGroup; }
107 inline wgpu::BindGroup GetSceneBindGroup() { return this->SceneBindGroup; }
108
109 inline void PopulateBindgroupLayouts(std::vector<wgpu::BindGroupLayout>& layouts)
110 {
111 layouts.emplace_back(this->SceneBindGroupLayout);
112 layouts.emplace_back(this->ActorBindGroupLayout);
113 }
114
115 wgpu::ShaderModule HasShaderCache(const std::string& source);
116 void InsertShader(const std::string& source, wgpu::ShaderModule shader);
117
119
126
130 std::unordered_set<vtkProp*> GetPropsRendered() { return this->PropsRendered; }
131
133
140
142
146 vtkSetMacro(UseRenderBundles, bool);
147 vtkBooleanMacro(UseRenderBundles, bool);
148 vtkGetMacro(UseRenderBundles, bool);
150
151protected:
154
162
168
169 // Setup scene and actor bindgroups. Actor has dynamic offsets.
171 // Create buffers for the bind groups.
173 // Update the bound buffers with data.
174 std::size_t UpdateBufferData();
175 // Create scene bind group.
177 // Create actor bind group.
179
180 // Start, finish recording commands with render pass encoder
183
189
190 std::size_t WriteLightsBuffer(std::size_t offset = 0);
191 std::size_t WriteSceneTransformsBuffer(std::size_t offset = 0);
192 std::size_t WriteActorBlocksBuffer(std::size_t offset = 0);
193
194 wgpu::RenderPassEncoder WGPURenderEncoder;
196 wgpu::Buffer SceneLightsBuffer;
197 wgpu::Buffer ActorBlocksBuffer;
198 wgpu::BindGroup SceneBindGroup;
199 wgpu::BindGroupLayout SceneBindGroupLayout;
200
201 wgpu::BindGroup ActorBindGroup;
202 wgpu::BindGroupLayout ActorBindGroupLayout;
203
204#ifdef __EMSCRIPTEN__
205 bool UseRenderBundles = true;
206#else
207 bool UseRenderBundles = false;
208#endif
209 // one bundle per actor. bundle gets reused every frame.
210 // these bundles can be built in parallel with vtkSMPTools. holding off because not
211 // sure how to get emscripten to thread.
212 std::vector<wgpu::RenderBundle> Bundles;
214 {
215 wgpu::RenderBundle Bundle = nullptr;
217 };
218 std::unordered_map<vtkProp*, vtkWGPUPropItem> PropWGPUItems;
219
220 std::unordered_map<std::string, wgpu::ShaderModule> ShaderCache;
221 std::size_t NumberOfPropsUpdated = 0;
222 int LightingComplexity = 0;
223 std::size_t NumberOfLightsUsed = 0;
224 std::vector<std::size_t> LightIDs;
225
228
229 struct
230 {
231 uint32_t Hits = 0;
232 uint32_t Misses = 0;
233 uint32_t TotalRequests = 0;
234 } BundleCacheStats;
235
240
241private:
243 // For the mapper to access 'AddPostRasterizationActor'
245 // For render window accessing PostRenderComputePipelines()
247
248 vtkWebGPURenderer(const vtkWebGPURenderer&) = delete;
249 void operator=(const vtkWebGPURenderer&) = delete;
250
254 void InitComputePipeline(vtkSmartPointer<vtkWebGPUComputePipeline> pipeline);
255
257
264 void PreRenderComputePipelines();
265 void PostRenderComputePipelines();
267
278 void PostRasterizationRender();
279
283 void AddPostRasterizationActor(vtkActor* actor);
284
289 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> SetupPreRenderComputePipelines;
290 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> SetupPostRenderComputePipelines;
291
295 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> NotSetupPreRenderComputePipelines;
296 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> NotSetupPostRenderComputePipelines;
297
305 std::vector<vtkActor*> PostRasterizationActors;
306
310 wgpu::CommandBuffer EncodePropListRenderCommand(vtkProp** propList, int listLength);
311
316 bool ComputeBuffersInitialized = false;
317
322 bool PrepareRenderDone = false;
323
327 bool DoClearPass = true;
328
335 std::unordered_set<vtkProp*> PropsRendered;
336};
337
338VTK_ABI_NAMESPACE_END
339#endif
abstract class specifies interface to map data
represents an object (geometry & properties) in a rendered scene
Definition vtkActor.h:151
abstract interface to OpenGL FBOs
a simple class to control print indentation
Definition vtkIndent.h:108
abstract superclass for all actors, volumes and annotations
Definition vtkProp.h:66
Context in which a vtkRenderPass will render.
abstract specification for renderers
Hold a reference to a vtkObjectBase instance.
handles properties associated with a texture map
Definition vtkTexture.h:168
record modification and/or execution time
describes linear transformations via a 4x4 matrix
This culler does both frustum culling and occlusion culling.
The point cloud renderer uses WebGPU compute shaders to render the point cells of a polydata onto the...
void ConfigureComputeRenderBuffers(vtkSmartPointer< vtkWebGPUComputePipeline > computePipeline)
Set up the buffers of a given vtkWebGPUComputePass.
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.
void ConfigureComputePipelines()
Sets the adapter and the device of the render window of this renderer to the compute pipelines of thi...
vtkTimeStamp LightingUploadTimestamp
void AddPostRenderComputePipeline(vtkSmartPointer< vtkWebGPUComputePipeline > pipeline)
Adds a compute pipeline to the renderer that will be executed each frame before/after the rendering p...
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 RenderProps()
Encodes the draw commands for the this->PropArrayCount props currently contained in this->PropArray.
void PopulateBindgroupLayouts(std::vector< wgpu::BindGroupLayout > &layouts)
wgpu::Buffer SceneLightsBuffer
std::size_t UpdateBufferData()
std::unordered_set< vtkProp * > GetPropsRendered()
Returns the list of the actors that were rendered last frame.
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 PrepareRender()
Updates / creates the various buffer necessary for the rendering of the props.
void DeviceRender() override
Create an image.
wgpu::Buffer ActorBlocksBuffer
wgpu::BindGroup SceneBindGroup
vtkSmartPointer< vtkTransform > UserLightTransform
Optional user transform for lights.
void AddPreRenderComputePipeline(vtkSmartPointer< vtkWebGPUComputePipeline > pipeline)
Adds a compute pipeline to the renderer that will be executed each frame before/after the rendering p...
~vtkWebGPURenderer() override
std::vector< std::size_t > LightIDs
wgpu::RenderPassEncoder GetRenderPassEncoder()
const std::vector< vtkSmartPointer< vtkWebGPUComputePipeline > > & GetSetupPostRenderComputePipelines()
Returns the list of compute pipelines of this renderer that have been setup for execution before/afte...
wgpu::BindGroup ActorBindGroup
std::vector< wgpu::RenderBundle > Bundles
const std::vector< vtkSmartPointer< vtkWebGPUComputePipeline > > & GetSetupPreRenderComputePipelines()
Returns the list of compute pipelines of this renderer that have been setup for execution before/afte...
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:48
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