VTK  9.4.20241112
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 "vtkWebGPUComputePipeline.h" // for the compute pipelines used by this renderer
11#include "vtk_wgpu.h" // for webgpu
12
13#include <string> // for ivar
14#include <unordered_map> // for ivar
15#include <unordered_set> // for the set of actors rendered last frame
16
18class vtkRenderState;
20
21VTK_ABI_NAMESPACE_BEGIN
22
24
25class VTKRENDERINGWEBGPU_EXPORT vtkWebGPURenderer : public vtkRenderer
26{
27public:
30 void PrintSelf(ostream& os, vtkIndent indent) override;
31
33 {
35 wgpu::RenderPipeline Pipeline;
36 };
37
38 // get the complexity of the current lights as a int
39 // 0 = no lighting
40 // 1 = headlight
41 // 2 = directional lights
42 // 3 = positional lights
44 {
45 NoLighting = 0,
46 Headlight = 1,
47 Directional = 2,
48 Positional = 3
49 };
50 vtkGetMacro(LightingComplexity, int);
51
53 {
58 RenderPostRasterization
59 };
60
64 void DeviceRender() override;
65
72
78 int UpdateGeometry(vtkFrameBufferObjectBase* fbo = nullptr) override;
79
88
94
96
100 const std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>>& GetSetupPreRenderComputePipelines();
101 const std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>>&
104
105 int UpdateLights() override;
106
107 void SetEnvironmentTexture(vtkTexture* texture, bool isSRGB = false) override;
108
110
111 inline wgpu::RenderPassEncoder GetRenderPassEncoder() { return this->WGPURenderEncoder; }
112 inline wgpu::RenderBundleEncoder GetRenderBundleEncoder() { return this->WGPUBundleEncoder; }
113 inline wgpu::BindGroup GetActorBindGroup() { return this->ActorBindGroup; }
114 inline wgpu::BindGroup GetSceneBindGroup() { return this->SceneBindGroup; }
115
116 inline void PopulateBindgroupLayouts(std::vector<wgpu::BindGroupLayout>& layouts)
117 {
118 layouts.emplace_back(this->SceneBindGroupLayout);
119 layouts.emplace_back(this->ActorBindGroupLayout);
120 }
121
123
130
134 std::unordered_set<vtkProp*> GetPropsRendered() { return this->PropsRendered; }
135
137
144
146
152 vtkSetMacro(UseRenderBundles, bool);
153 vtkBooleanMacro(UseRenderBundles, bool);
154 vtkGetMacro(UseRenderBundles, bool);
156
167
168protected:
171
180
181 // Setup scene and actor bindgroups. Actor has dynamic offsets.
183 // Create buffers for the bind groups.
185 // Create scene bind group.
187 // Create actor bind group.
189
190 // Start, finish recording commands with render pass encoder
193
194 std::size_t WriteLightsBuffer(std::size_t offset = 0);
195 std::size_t WriteSceneTransformsBuffer(std::size_t offset = 0);
196 std::size_t WriteActorBlocksBuffer(std::size_t offset = 0);
197
198 wgpu::RenderPassEncoder WGPURenderEncoder;
199 wgpu::RenderBundleEncoder WGPUBundleEncoder;
201 wgpu::Buffer SceneLightsBuffer;
202 wgpu::Buffer ActorBlocksBuffer;
203 std::size_t LastActorBufferSize = 0;
204 wgpu::BindGroup SceneBindGroup;
205 wgpu::BindGroupLayout SceneBindGroupLayout;
206
207 wgpu::BindGroup ActorBindGroup;
208 wgpu::BindGroupLayout ActorBindGroupLayout;
209
210#ifdef __EMSCRIPTEN__
211 bool UseRenderBundles = true;
212#else
213 bool UseRenderBundles = false;
214#endif
215 // one bundle per actor. bundle gets reused every frame.
216 // these bundles can be built in parallel with vtkSMPTools. holding off because not
217 // sure how to get emscripten to thread.
218 std::vector<wgpu::RenderBundle> Bundles;
220 {
221 wgpu::RenderBundle Bundle = nullptr;
222 vtkTypeUInt32 DynamicOffset = 0;
223 };
224 std::unordered_map<vtkProp*, vtkWGPUPropItem> PropWGPUItems;
225
226 int LightingComplexity = 0;
227 std::size_t NumberOfLightsUsed = 0;
228 std::vector<std::size_t> LightIDs;
229
232
233 struct
234 {
235 uint32_t Hits = 0;
236 uint32_t Misses = 0;
237 uint32_t TotalRequests = 0;
238 } BundleCacheStats;
239
244
245private:
247 // For the mapper to access 'AddPostRasterizationActor'
249 // For render window accessing PostRenderComputePipelines()
251
252 vtkWebGPURenderer(const vtkWebGPURenderer&) = delete;
253 void operator=(const vtkWebGPURenderer&) = delete;
254
258 void InitComputePipeline(vtkSmartPointer<vtkWebGPUComputePipeline> pipeline);
259
261
268 void PreRenderComputePipelines();
269 void PostRenderComputePipelines();
271
282 void PostRasterizationRender();
283
287 void AddPostRasterizationActor(vtkActor* actor);
288
293 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> SetupPreRenderComputePipelines;
294 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> SetupPostRenderComputePipelines;
295
299 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> NotSetupPreRenderComputePipelines;
300 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> NotSetupPostRenderComputePipelines;
301
309 std::vector<vtkActor*> PostRasterizationActors;
310
314 wgpu::CommandBuffer EncodePropListRenderCommand(vtkProp** propList, int listLength);
315
320 bool ComputeBuffersInitialized = false;
321
326 RenderStageEnum RenderStage = RenderStageEnum::AwaitingPreparation;
327
331 bool DoClearPass = true;
332
339 std::unordered_set<vtkProp*> PropsRendered;
340};
341
342VTK_ABI_NAMESPACE_END
343#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...
WebGPU rendering window.
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 and draw any opaque and translucent geometry.
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
wgpu::BindGroup GetActorBindGroup()
void PopulateBindgroupLayouts(std::vector< wgpu::BindGroupLayout > &layouts)
wgpu::Buffer SceneLightsBuffer
std::unordered_set< vtkProp * > GetPropsRendered()
Returns the list of the actors that were rendered last frame.
wgpu::RenderBundleEncoder WGPUBundleEncoder
vtkTransform * GetUserLightTransform()
Set the user light transform applied after the camera transform.
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...
wgpu::RenderBundleEncoder GetRenderBundleEncoder()
~vtkWebGPURenderer() override
std::vector< std::size_t > LightIDs
wgpu::RenderPassEncoder GetRenderPassEncoder()
void UpdateBuffers()
Updates / creates the various buffer necessary for the rendering of the props.
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
vtkGetEnumMacro(RenderStage, RenderStageEnum)
Query the stage in the rendering process.
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.
wgpu::BindGroupLayout ActorBindGroupLayout
vtkMTimeType LightingUpdateTime
void SetupBindGroupLayouts()
void SetupActorBindGroup()
window superclass for vtkRenderWindow
Definition vtkWindow.h:48
vtkSmartPointer< vtkPropCollection > Props
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:270