VTK  9.7.20260918
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
26
27class VTKRENDERINGWEBGPU_EXPORT VTK_MARSHALAUTO vtkWebGPURenderer : public vtkRenderer
28{
29public:
34 void PrintSelf(ostream& os, vtkIndent indent) override;
35
36 // get the complexity of the current lights as a int
37 // 0 = no lighting
38 // 1 = headlight
39 // 2 = directional lights
40 // 3 = positional lights
48 vtkGetMacro(LightingComplexity, int);
49
58
62 void Clear() override;
63
67 void DeviceRender() override;
68
75
81 int UpdateGeometry(vtkFrameBufferObjectBase* fbo = nullptr) override;
82
91
97
99
103 const std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>>& GetSetupPreRenderComputePipelines();
104 const std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>>&
107
108 int UpdateLights() override;
109
110 void SetEnvironmentTexture(vtkTexture* texture, bool isSRGB = false) override;
111
113
114 WGPURenderPassEncoder GetRenderPassEncoder() { return this->WGPURenderEncoder; }
115 WGPURenderBundleEncoder GetRenderBundleEncoder() { return this->WGPUBundleEncoder; }
116 WGPUBindGroup GetSceneBindGroup() { return this->SceneBindGroup; }
117
118 void PopulateBindgroupLayouts(std::vector<WGPUBindGroupLayout>& layouts)
119 {
120 layouts.emplace_back(this->SceneBindGroupLayout);
121 }
122
124
131
135 std::unordered_set<vtkProp*> GetPropsRendered() { return this->PropsRendered; }
136
138
154 vtkSetMacro(UseRenderBundles, bool);
155 vtkBooleanMacro(UseRenderBundles, bool);
156 vtkGetMacro(UseRenderBundles, bool);
158
169
181 vtkProp* GetPropWithId(vtkTypeUInt32 id);
182
207 {
208 this->RebuildRenderBundle = true;
209 this->Bundle = nullptr;
210 }
211
215 vtkGetMacro(RebuildRenderBundle, bool);
216
217protected:
220
229
230private:
232 // For the mapper to access 'AddPostRasterizationActor'
234 // For render window accessing PostRenderComputePipelines()
236
237 vtkWebGPURenderer(const vtkWebGPURenderer&) = delete;
238 void operator=(const vtkWebGPURenderer&) = delete;
239
240 // Setup scene and actor bindgroups. Actor has dynamic offsets.
241 void SetupBindGroupLayouts();
242 // Create buffers for the bind groups.
243 void CreateBuffers();
244 // Create scene bind group.
245 void SetupSceneBindGroup();
246
247 // Start, finish recording commands.
248 void BeginRecording();
249 void EndRecording();
250
251 std::size_t WriteLightsBuffer(std::size_t offset = 0);
252 std::size_t WriteSceneTransformsBuffer(std::size_t offset = 0);
253
257 void InitComputePipeline(vtkSmartPointer<vtkWebGPUComputePipeline> pipeline);
258
260
267 void PreRenderComputePipelines();
268 void PostRenderComputePipelines();
270
281 void PostRasterizationRender();
282
286 void AddPostRasterizationActor(vtkActor* actor);
287
292 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> SetupPreRenderComputePipelines;
293 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> SetupPostRenderComputePipelines;
294
298 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> NotSetupPreRenderComputePipelines;
299 std::vector<vtkSmartPointer<vtkWebGPUComputePipeline>> NotSetupPostRenderComputePipelines;
300
308 std::vector<vtkActor*> PostRasterizationActors;
309
313 WGPUCommandBuffer EncodePropListRenderCommand(vtkProp** propList, int listLength);
314
320 void RecordRenderCommands();
321
326 void ClearGradientBackground();
327
328 WGPURenderPassEncoder WGPURenderEncoder = nullptr;
329 WGPURenderBundleEncoder WGPUBundleEncoder = nullptr;
330 WGPUBuffer SceneTransformBuffer = nullptr;
331 WGPUBuffer SceneLightsBuffer = nullptr;
332 // Holds the two gradient stop colors, the gradient mode and the dither flag
333 // for the background quad drawn by Clear(). Only allocated when a gradient
334 // background is requested.
335 WGPUBuffer BackgroundGradientBuffer = nullptr;
336
337 WGPUBindGroup SceneBindGroup = nullptr;
338 WGPUBindGroupLayout SceneBindGroupLayout = nullptr;
339
340 // Render bundles enable faster rendering.
341 bool UseRenderBundles = true;
342 bool RebuildRenderBundle = false;
343 // the commands in bundle get reused every frame.
344 WGPURenderBundle Bundle = nullptr;
345
346 int LightingComplexity = 0;
347 std::size_t NumberOfLightsUsed = 0;
348 std::vector<std::size_t> LightIDs;
349 std::size_t AllocatedLightsBufferSize = 0;
350 std::size_t PreviousLightCount = 0;
351
352 vtkMTimeType LightingUpdateTime;
353 vtkTimeStamp LightingUploadTimestamp;
354
359 bool ComputeBuffersInitialized = false;
360
366
370 bool DrawBackgroundInClearPass = true;
371
378 std::unordered_set<vtkProp*> PropsRendered;
379
390 std::vector<vtkProp*> LastVisibleProps;
391
397 std::vector<vtkProp*> VisibleProps;
398
403 bool VisiblePropSetChanged();
404
410 bool CanReuseRenderBundle() const
411 {
412 return this->UseRenderBundles && !this->RebuildRenderBundle && this->Bundle != nullptr;
413 }
414
420 bool PropSupportsReusingRenderBundle(vtkProp*) const;
421
427 void WarnIfCullingWithRenderBundles();
428
430 vtkWeakPtr<vtkCuller> DefaultCuller;
431 bool WarnedAboutCullingWithRenderBundles = false;
432};
433
434#define vtkWebGPURenderer_OVERRIDE_ATTRIBUTES vtkWebGPURenderer::CreateOverrideAttributes()
435VTK_ABI_NAMESPACE_END
436#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
a weak reference to a vtkObjectBase.
Definition vtkWeakPtr.h:52
This culler does both frustum culling and occlusion culling.
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...
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.
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
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)
Query the stage in the rendering process.
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