VTK  9.7.20261101
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 "vtkWeakPtr.h" // For vtkWeakPtr
11#include "vtkWebGPUComputePipeline.h" // for the compute pipelines used by this renderer
12#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
13#include "vtk_wgpu.h" // for webgpu C API
14
15#include <unordered_set> // for the set of actors rendered last frame
16#include <vector> // for the list of visible props
17
19class vtkCuller;
20class vtkRenderState;
23VTK_ABI_NAMESPACE_BEGIN
24
27
28class VTKRENDERINGWEBGPU_EXPORT VTK_MARSHALAUTO vtkWebGPURenderer : public vtkRenderer
29{
30public:
35 void PrintSelf(ostream& os, vtkIndent indent) override;
36
37 // get the complexity of the current lights as a int
38 // 0 = no lighting
39 // 1 = headlight
40 // 2 = directional lights
41 // 3 = positional lights
49 vtkGetMacro(LightingComplexity, int);
50
59
63 void Clear() override;
64
68 void DeviceRender() override;
69
76
82 int UpdateGeometry(vtkFrameBufferObjectBase* fbo = nullptr) override;
83
92
98
100
104 const std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>>& GetSetupPreRenderComputePipelines();
105 const std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>>&
108
109 int UpdateLights() override;
110
111 void SetEnvironmentTexture(vtkTexture* texture, bool isSRGB = false) override;
112
114
115 WGPURenderPassEncoder GetRenderPassEncoder() { return this->WGPURenderEncoder; }
116 WGPURenderBundleEncoder GetRenderBundleEncoder() { return this->WGPUBundleEncoder; }
117 WGPUBindGroup GetSceneBindGroup() { return this->SceneBindGroup; }
118
119 void PopulateBindgroupLayouts(std::vector<WGPUBindGroupLayout>& layouts)
120 {
121 layouts.emplace_back(this->SceneBindGroupLayout);
122 }
123
125
132
136 std::unordered_set<vtkProp*> GetPropsRendered() { return this->PropsRendered; }
137
139
155 vtkSetMacro(UseRenderBundles, bool);
156 vtkBooleanMacro(UseRenderBundles, bool);
157 vtkGetMacro(UseRenderBundles, bool);
159
169 vtkSetMacro(UseDeepFramebufferTransparency, bool);
170 vtkBooleanMacro(UseDeepFramebufferTransparency, bool);
171 vtkGetMacro(UseDeepFramebufferTransparency, bool);
172
178
185
191
202
214 vtkProp* GetPropWithId(vtkTypeUInt32 id);
215
222 vtkGetMacro(RecordingTranslucentPass, bool);
223
248 {
249 this->RebuildRenderBundle = true;
250 this->Bundle = nullptr;
251 }
252
256 vtkGetMacro(RebuildRenderBundle, bool);
257
258protected:
261
270
271private:
273 // For the mapper to access 'AddPostRasterizationActor'
275 // For render window accessing PostRenderComputePipelines()
277 // For the actor to access RenderBundleBuildTime
278 friend class vtkWebGPUActor;
279
280 vtkWebGPURenderer(const vtkWebGPURenderer&) = delete;
281 void operator=(const vtkWebGPURenderer&) = delete;
282
283 // Setup scene and actor bindgroups. Actor has dynamic offsets.
284 void SetupBindGroupLayouts();
285 void CreateBuffers();
286 void SetupSceneBindGroup();
287
288 void BeginRecording();
289 void EndRecording();
290
291 std::size_t WriteLightsBuffer(std::size_t offset = 0);
292 std::size_t WriteSceneTransformsBuffer(std::size_t offset = 0);
293
297 void InitComputePipeline(vtkSmartPointer<vtkWebGPUComputePipeline> pipeline);
298
300
307 void PreRenderComputePipelines();
308 void PostRenderComputePipelines();
310
321 void PostRasterizationRender();
322
326 void AddPostRasterizationActor(vtkActor* actor);
327
332 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> SetupPreRenderComputePipelines;
333 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> SetupPostRenderComputePipelines;
334
338 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> NotSetupPreRenderComputePipelines;
339 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> NotSetupPostRenderComputePipelines;
340
348 std::vector<vtkActor*> PostRasterizationActors;
349
353 WGPUCommandBuffer EncodePropListRenderCommand(vtkProp** propList, int listLength);
354
360 void RecordRenderCommands();
361
366 void ClearGradientBackground();
367
368 WGPURenderPassEncoder WGPURenderEncoder = nullptr;
369 WGPURenderBundleEncoder WGPUBundleEncoder = nullptr;
370 WGPUBuffer SceneTransformBuffer = nullptr;
371 WGPUBuffer SceneLightsBuffer = nullptr;
372 // Holds the two gradient stop colors, the gradient mode and the dither flag
373 // for the background quad drawn by Clear(). Only allocated when a gradient
374 // background is requested.
375 WGPUBuffer BackgroundGradientBuffer = nullptr;
376
377 WGPUBindGroup SceneBindGroup = nullptr;
378 WGPUBindGroupLayout SceneBindGroupLayout = nullptr;
379
380 bool UseDeepFramebufferTransparency = true;
381 bool DeepFramebufferPopulated = false;
383 WGPUBuffer DeepFramebufferPlaceholders[3] = { nullptr, nullptr, nullptr };
384
389 void ResolveDeepFramebuffer(WGPURenderPassEncoder encoder);
390
391 std::string DeepFramebufferResolveKey;
392
393 // Render bundles enable faster rendering.
394 bool UseRenderBundles = true;
395 bool RebuildRenderBundle = false;
396 // the commands in bundle get reused every frame.
397 WGPURenderBundle Bundle = nullptr;
398
399 int LightingComplexity = 0;
400 std::size_t NumberOfLightsUsed = 0;
401 std::vector<std::size_t> LightIDs;
402 std::size_t AllocatedLightsBufferSize = 0;
403 std::size_t PreviousLightCount = 0;
404
405 vtkMTimeType LightingUpdateTime;
406 vtkTimeStamp LightingUploadTimestamp;
407
412 bool ComputeBuffersInitialized = false;
413
418 RenderStageEnum RenderStage = RenderStageEnum::AwaitingPreparation;
419 bool RecordingTranslucentPass = false;
420
424 bool DrawBackgroundInClearPass = true;
425
432 std::unordered_set<vtkProp*> PropsRendered;
433
444 std::vector<vtkProp*> LastVisibleProps;
445
451 std::vector<vtkProp*> VisibleProps;
452
457 bool VisiblePropSetChanged();
458
464 bool CanReuseRenderBundle() const
465 {
466 return this->UseRenderBundles && !this->RebuildRenderBundle && this->Bundle != nullptr;
467 }
468
474 bool PropSupportsReusingRenderBundle(vtkProp*) const;
475
481 void WarnIfCullingWithRenderBundles();
482
484 vtkTimeStamp RenderBundleBuildTime;
485
487 vtkWeakPtr<vtkCuller> DefaultCuller;
488 bool WarnedAboutCullingWithRenderBundles = false;
489};
490
491#define vtkWebGPURenderer_OVERRIDE_ATTRIBUTES vtkWebGPURenderer::CreateOverrideAttributes()
492VTK_ABI_NAMESPACE_END
493#endif
abstract class specifies interface to map data
represents an object (geometry & properties) in a rendered scene
Definition vtkActor.h:151
a superclass for prop cullers
Definition vtkCuller.h:31
abstract interface to OpenGL FBOs
a simple class to control print indentation
Definition vtkIndent.h:108
Attribute for vtkObjectFactory overrides.
abstract superclass for all actors, volumes and annotations
Definition vtkProp.h:68
Context in which a vtkRenderPass will render.
Hold a reference to a vtkObjectBase instance.
handles properties associated with a texture map
Definition vtkTexture.h:168
record modification and/or execution time
This culler does both frustum culling and occlusion culling.
Per-pixel fragment lists for order independent transparency.
void ConfigureComputeRenderBuffers(vtkSmartPointer< vtkWebGPUComputePipeline > computePipeline)
Set up the buffers of a given vtkWebGPUComputePass.
friend class vtkWebGPUComputePointCloudMapper
WGPURenderBundleEncoder GetRenderBundleEncoder()
void ReleaseGraphicsResources(vtkWindow *w) override
static vtkWebGPURenderer * New()
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...
void AddPostRenderComputePipeline(vtkSmartPointer< vtkWebGPUComputePipeline > pipeline)
Adds a compute pipeline to the renderer that will be executed each frame before/after the rendering p...
friend class vtkWebGPUActor
int UpdateOpaquePolygonalGeometry() override
Request mappers to run the vtkAlgorithm pipeline (if needed) and consequently update device buffers c...
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.
vtkProp * GetPropWithId(vtkTypeUInt32 id)
The prop that was drawn with id in the ids attachment, or nullptr when the id names no prop.
friend class vtkWebGPUComputeOcclusionCuller
int UpdateLights() override
Ask all lights to load themselves into rendering pipeline.
void PopulateBindgroupLayouts(std::vector< WGPUBindGroupLayout > &layouts)
std::unordered_set< vtkProp * > GetPropsRendered()
Returns the list of the actors that were rendered last frame.
void Clear() override
Clear the image to the background color.
vtkWebGPUDeepFramebuffer * GetDeepFramebuffer()
The per-pixel fragment lists used when UseDeepFramebufferTransparency is on.
static vtkOverrideAttribute * CreateOverrideAttributes()
void DeviceRender() override
Create an image.
void InvalidateBundle()
Forces the renderer to re-record draw commands into a render bundle.
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
bool PrepareDeepFramebuffer()
Ensure the deep framebuffer and its bind group exist and match the current viewport.
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...
friend class vtkWebGPURenderWindow
WGPUBindGroup GetSceneBindGroup()
vtkGetEnumMacro(RenderStage, RenderStageEnum)
Layout and bind group holding the deep framebuffer's storage buffers.
const std::vector< vtkSmartPointer< vtkWebGPUComputePipeline > > & GetSetupPreRenderComputePipelines()
Returns the list of compute pipelines of this renderer that have been setup for execution before/afte...
void SetEnvironmentTexture(vtkTexture *texture, bool isSRGB=false) override
Set/Get the environment texture used for image based lighting.
WGPURenderPassEncoder GetRenderPassEncoder()
window superclass for vtkRenderWindow
Definition vtkWindow.h:62
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:318
#define VTK_MARSHALAUTO
#define VTK_NEWINSTANCE