VTK  9.4.20250206
vtkWebGPUPolyDataMapper.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 vtkWebGPUPolyDataMapper_h
4#define vtkWebGPUPolyDataMapper_h
5
6#include "vtkPolyDataMapper.h"
7
8#include "vtkProperty.h" // for VTK_SURFACE constants
9#include "vtkRenderingWebGPUModule.h" // for export macro
10#include "vtkWeakPointer.h" // for vtkWeakPointer
11#include "vtkWebGPUCellToPrimitiveConverter.h" // for TopologySourceType
12#include "vtkWebGPUComputePipeline.h" // for ivar
13#include "vtk_wgpu.h" // for webgpu
14
15#include <unordered_set> // for the not set compute render buffers
16
17VTK_ABI_NAMESPACE_BEGIN
18class vtkCellArray;
23
24class VTKRENDERINGWEBGPU_EXPORT vtkWebGPUPolyDataMapper : public vtkPolyDataMapper
25{
26public:
29 void PrintSelf(ostream& os, vtkIndent indent) override;
30
35 {
36 POINT_POSITIONS = 0,
42 POINT_UNDEFINED
43 };
44
49 {
50 CELL_COLORS = 0,
53 CELL_UNDEFINED
54 };
55
62 {
63 // Pipeline that renders points
64 GFX_PIPELINE_POINTS = 0,
65 // Pipeline that renders lines with rounded caps and rounded joins.
67 // Pipeline that renders lines with miter joins.
69 // Pipeline that renders triangles
71 GFX_PIPELINE_NB_TYPES
72 };
73
75
79 void RenderPiece(vtkRenderer* renderer, vtkActor* act) override;
80
87
94 bool GetSupportsSelection() override { return false; }
95
107 void MapDataArrayToVertexAttribute(const char* vertexAttributeName, const char* dataArrayName,
108 int fieldAssociation, int componentno = -1) override;
109
117 void MapDataArrayToMultiTextureAttribute(const char* tname, const char* dataArrayName,
118 int fieldAssociation, int componentno = -1) override;
119
123 void RemoveVertexAttributeMapping(const char* vertexAttributeName) override;
124
129
135 vtkHardwareSelector* sel, std::vector<unsigned int>& pixeloffsets, vtkProp* prop) override;
136
153 PointDataAttributes attribute, int bufferGroup, int bufferBinding, int uniformsGroup,
154 int uniformsBinding);
155
160 CellDataAttributes attribute, int bufferGroup, int bufferBinding, int uniformsGroup,
161 int uniformsBinding);
162
163protected:
166
172 void ComputeBounds() override;
173
185
191 vtkRenderer* renderer, vtkActor* actor, const wgpu::RenderPassEncoder& passEncoder);
193 vtkRenderer* renderer, vtkActor* actor, const wgpu::RenderBundleEncoder& bundleEncoder);
194
201
207
212
216 static wgpu::BindGroupLayout CreateMeshAttributeBindGroupLayout(
217 const wgpu::Device& device, const std::string& label);
218
222 static wgpu::BindGroupLayout CreateTopologyBindGroupLayout(
223 const wgpu::Device& device, const std::string& label);
224
228 static wgpu::RenderPipeline CreateRenderPipeline(const wgpu::Device& device,
229 wgpu::RenderPipelineDescriptor* pipelineDescriptor, const wgpu::ShaderModule& shaderModule,
230 wgpu::PrimitiveTopology primitiveTopology);
231
248 const wgpu::Device& device, const std::string& label);
249
261 wgpu::BindGroup CreateTopologyBindGroup(const wgpu::Device& device, const std::string& label,
263
268
273
280
286
292
298
304 unsigned long GetExactPointBufferSize();
305
311 unsigned long GetExactCellBufferSize();
312
314
325
329 const char* GetGraphicsPipelineTypeAsString(GraphicsPipelineType graphicsPipelineType);
330
335 void SetupGraphicsPipelines(const wgpu::Device& device, vtkRenderer* renderer, vtkActor* actor);
336
343
345 {
346 struct
347 {
348 // point attributes.
349 wgpu::Buffer Buffer;
350 uint64_t Size = 0;
351 } Point;
352
353 struct
354 {
355 // cell attributes.
356 wgpu::Buffer Buffer;
357 uint64_t Size = 0;
358 } Cell;
359 };
361
363 {
364 vtkTypeUInt32 Start = 0;
365 vtkTypeUInt32 NumTuples = 0;
366 vtkTypeUInt32 NumComponents = 0;
367 };
369 {
370 vtkTypeUInt32 ApplyOverrideColors = 0;
371 vtkTypeFloat32 Opacity = 0;
372 vtkTypeUInt8 Padding1[8];
373 vtkTypeFloat32 Ambient[4] = {}; // last component is padding for 16-byte alignment
374 vtkTypeFloat32 Diffuse[4] = {}; // last component is padding for 16-byte alignment
375 };
377 {
385 vtkTypeUInt8 Padding[12];
387 };
389
391 vtkTimeStamp CellAttributesBuildTimestamp[CELL_NB_ATTRIBUTES];
392 vtkTimeStamp PointAttributesBuildTimestamp[POINT_NB_ATTRIBUTES];
396
397 bool HasPointAttributes[POINT_NB_ATTRIBUTES];
398 bool HasCellAttributes[CELL_NB_ATTRIBUTES];
399 bool RebuildGraphicsPipelines = true;
400 // used by RenderPiece and functions it calls to reduce
401 // calls to get the input and allow for rendering of
402 // other polydata (not the input)
403 vtkPolyData* CurrentInput = nullptr;
404 // vtkRenderer culls props to frustum. At that point, it requests
405 // mappers for bounds of the geometry. We cache the vtkAlgorithm output
406 // so that `UpdateMeshGeometryBuffers` can reuse it without climbing up
407 // vtkAlgorithm pipeline.
408 vtkPolyData* CachedInput = nullptr;
409
410 // 1 bind group for this polydata mesh
411 wgpu::BindGroup MeshAttributeBindGroup;
412
414 {
415 // buffer for the primitive cell ids and point ids.
416 wgpu::Buffer TopologyBuffer;
417 // buffer for edge array. this lets fragment shader hide internal edges of a polygon
418 // when edge visibility is turned on.
419 wgpu::Buffer EdgeArrayBuffer;
420 // // buffer for indirect draw command
421 // wgpu::Buffer IndirectDrawBuffer;
422 // bind group for the primitive size uniform.
423 wgpu::BindGroup BindGroup;
424 // vertexCount for draw call.
425 vtkTypeUInt32 VertexCount = 0;
426 };
427
431 std::string GraphicsPipelineKeys[GFX_PIPELINE_NB_TYPES] = {};
432
433 // Cache these so that subsequent executions of UpdateMeshGeometryBuffers() do not unnecessarily
434 // invoke MapScalars().
435 int LastScalarMode = -1;
436 bool LastScalarVisibility = false;
438 {
439 bool LastActorBackfaceCulling = false;
440 bool LastActorFrontfaceCulling = false;
441 bool LastVertexVisibility = false;
442 int LastRepresentation = VTK_SURFACE;
443 bool LastHasRenderingTranslucentGeometry = false;
444 };
445 std::map<std::pair<vtkWeakPointer<vtkActor>, vtkWeakPointer<vtkRenderer>>, ActorState>
447
448private:
450 friend class vtkWebGPURenderer;
451
455 wgpu::Buffer GetPointDataWGPUBuffer() { return this->MeshSSBO.Point.Buffer; }
456
460 wgpu::Buffer GetCellDataWGPUBuffer() { return this->MeshSSBO.Cell.Buffer; }
461
468 std::vector<vtkSmartPointer<vtkWebGPUComputeRenderBuffer>> SetupComputeRenderBuffers;
469
473 std::unordered_set<vtkSmartPointer<vtkWebGPUComputeRenderBuffer>> NotSetupComputeRenderBuffers;
474
478 const PointDataAttributes PointDataAttributesOrder[PointDataAttributes::POINT_NB_ATTRIBUTES] = {
479 PointDataAttributes::POINT_POSITIONS, PointDataAttributes::POINT_COLORS,
480 PointDataAttributes::POINT_NORMALS, PointDataAttributes::POINT_TANGENTS,
481 PointDataAttributes::POINT_UVS
482 };
483
487 const CellDataAttributes CellDataAttributesOrder[CellDataAttributes::CELL_NB_ATTRIBUTES] = {
488 CellDataAttributes::CELL_COLORS, CellDataAttributes::CELL_NORMALS
489 };
490
492 void operator=(const vtkWebGPUPolyDataMapper&) = delete;
493};
494VTK_ABI_NAMESPACE_END
495#endif
represents an object (geometry & properties) in a rendered scene
Definition vtkActor.h:151
object to represent cell connectivity
a simple class to control print indentation
Definition vtkIndent.h:108
Allocate and hold a VTK object.
Definition vtkNew.h:167
map vtkPolyData to graphics primitives
std::uintptr_t MapperHashType
concrete dataset represents vertices, lines, polygons, and triangle strips
abstract superclass for all actors, volumes and annotations
Definition vtkProp.h:66
abstract specification for renderers
Hold a reference to a vtkObjectBase instance.
record modification and/or execution time
a weak reference to a vtkObject.
TopologySourceType
All supported types of topology.
Render buffers are returned by calls to vtkWebGPUPolyDataMapper::AcquirePointAttributeComputeRenderBu...
Create a webgpu device for use in rendering and compute pipelines.
PointDataAttributes
All the attributes supported by the point data buffer.
unsigned long GetPointAttributeByteSize(vtkWebGPUPolyDataMapper::PointDataAttributes attribute)
Returns the size of the 'sub-buffer' within the whole point data SSBO for the given attribute.
static wgpu::BindGroupLayout CreateMeshAttributeBindGroupLayout(const wgpu::Device &device, const std::string &label)
Create a bind group layout for the mesh attribute bind group.
void ProcessSelectorPixelBuffers(vtkHardwareSelector *sel, std::vector< unsigned int > &pixeloffsets, vtkProp *prop) override
allows a mapper to update a selections color buffers Called from a prop which in turn is called from ...
static wgpu::RenderPipeline CreateRenderPipeline(const wgpu::Device &device, wgpu::RenderPipelineDescriptor *pipelineDescriptor, const wgpu::ShaderModule &shaderModule, wgpu::PrimitiveTopology primitiveTopology)
Create a render pipeline.
bool GetNeedToRemapScalars(vtkPolyData *mesh)
Determines whether this mapper should re-map scalars on the mesh to colors.
void ReleaseGraphicsResources(vtkWindow *) override
Release any graphics resources that are being consumed by this mapper.
vtkIdType GetPointAttributeByteOffset(PointDataAttributes attribute)
Returns the offset at which the 'sub-buffer' of 'attribute' starts within the mesh SSBO point data bu...
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
unsigned long GetCellAttributeElementSize(vtkWebGPUPolyDataMapper::CellDataAttributes attribute)
Returns the size in bytes of one element of the given attribute.
void MapDataArrayToVertexAttribute(const char *vertexAttributeName, const char *dataArrayName, int fieldAssociation, int componentno=-1) override
Select a data array from the point/cell data and map it to a generic vertex attribute.
void RemoveAllVertexAttributeMappings() override
Remove all vertex attributes.
void DeducePointCellAttributeAvailability(vtkPolyData *mesh)
Looks at the point/cell data of vtkPolyData object and determines which attributes are available.
vtkPolyDataMapper::MapperHashType GenerateHash(vtkPolyData *polydata) override
This hash integer is computed by concrete graphics implementation of this class.
void ResetPointCellAttributeState()
Reset the internal Has{Point,Cell}Attribute booleans to false.
vtkSmartPointer< vtkWebGPUComputeRenderBuffer > AcquirePointAttributeComputeRenderBuffer(PointDataAttributes attribute, int bufferGroup, int bufferBinding, int uniformsGroup, int uniformsBinding)
Returns an already configured (ready to be added to a vtkWebGPUComputePipeline) buffer bound to the g...
vtkSmartPointer< vtkWebGPUComputeRenderBuffer > AcquireCellAttributeComputeRenderBuffer(CellDataAttributes attribute, int bufferGroup, int bufferBinding, int uniformsGroup, int uniformsBinding)
Same as AcquirePointAttributeComputeRenderBuffer but for cell data attributes.
unsigned long GetExactPointBufferSize()
Calculates the size of a buffer that is large enough to contain all the values from the point attribu...
vtkIdType GetCellAttributeByteOffset(CellDataAttributes attribute)
Returns the offset at which the 'sub-buffer' of 'attribute' starts within the mesh SSBO cell data buf...
wgpu::BindGroup CreateTopologyBindGroup(const wgpu::Device &device, const std::string &label, vtkWebGPUCellToPrimitiveConverter::TopologySourceType topologySourceType)
Create a bind group for the primitives of a mesh.
void SetupGraphicsPipelines(const wgpu::Device &device, vtkRenderer *renderer, vtkActor *actor)
Creates the graphics pipeline.
void RecordDrawCommands(vtkRenderer *renderer, vtkActor *actor, const wgpu::RenderPassEncoder &passEncoder)
Record draw calls in the render pass encoder.
void RemoveVertexAttributeMapping(const char *vertexAttributeName) override
Remove a vertex attribute mapping.
static vtkWebGPUPolyDataMapper * New()
~vtkWebGPUPolyDataMapper() override
static wgpu::BindGroupLayout CreateTopologyBindGroupLayout(const wgpu::Device &device, const std::string &label)
Create a bind group layout for the TopologyRenderInfo::BindGroup
void RecordDrawCommands(vtkRenderer *renderer, vtkActor *actor, const wgpu::RenderBundleEncoder &bundleEncoder)
const char * GetGraphicsPipelineTypeAsString(GraphicsPipelineType graphicsPipelineType)
Get the name of the graphics pipeline type as a string.
vtkNew< vtkWebGPUCellToPrimitiveConverter > CellConverter
void MapDataArrayToMultiTextureAttribute(const char *tname, const char *dataArrayName, int fieldAssociation, int componentno=-1) override
This method will Map the specified data array for use as a texture coordinate for texture tname.
unsigned long GetCellAttributeByteSize(vtkWebGPUPolyDataMapper::CellDataAttributes attribute)
Returns the size of the 'sub-buffer' within the whole cell data SSBO for the given attribute.
void RenderPiece(vtkRenderer *renderer, vtkActor *act) override
Implemented by sub classes.
GraphicsPipelineType
This mapper uses different wgpu::RenderPipeline to render a list of primitives.
void UpdateMeshGeometryBuffers(vtkWebGPURenderWindow *wgpuRenderWindow)
Creates buffers as needed and updates them with point/cell attributes, topology, draw parameters.
bool GetNeedToRebuildGraphicsPipelines(vtkActor *actor, vtkRenderer *renderer)
Get whether the graphics pipeline needs rebuilt.
unsigned long GetExactCellBufferSize()
Calculates the size of a buffer that is large enough to contain all the values from the cell attribut...
unsigned long GetPointAttributeElementSize(vtkWebGPUPolyDataMapper::PointDataAttributes attribute)
Returns the size in bytes of one element of the given attribute.
wgpu::BindGroup CreateMeshAttributeBindGroup(const wgpu::Device &device, const std::string &label)
Create a bind group for the point and cell attributes of a mesh.
bool CacheActorRendererProperties(vtkActor *actor, vtkRenderer *renderer)
This method keeps track of few properties of the actor which when changed, require rebuilding a rende...
CellDataAttributes
All the attributes supported by the cell data buffer.
void ComputeBounds() override
Called in GetBounds().
std::map< std::pair< vtkWeakPointer< vtkActor >, vtkWeakPointer< vtkRenderer > >, ActorState > CachedActorRendererProperties
WebGPU rendering window.
window superclass for vtkRenderWindow
Definition vtkWindow.h:48
struct vtkWebGPUPolyDataMapper::MeshAttributeBuffers::@201 Cell
struct vtkWebGPUPolyDataMapper::MeshAttributeBuffers::@200 Point
#define VTK_SURFACE
int vtkIdType
Definition vtkType.h:315