VTK  9.4.20250310
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
200
205
209 static wgpu::BindGroupLayout CreateMeshAttributeBindGroupLayout(
210 const wgpu::Device& device, const std::string& label);
211
215 static wgpu::BindGroupLayout CreateTopologyBindGroupLayout(
216 const wgpu::Device& device, const std::string& label);
217
221 static wgpu::RenderPipeline CreateRenderPipeline(const wgpu::Device& device,
222 wgpu::RenderPipelineDescriptor* pipelineDescriptor, const wgpu::ShaderModule& shaderModule,
223 wgpu::PrimitiveTopology primitiveTopology);
224
241 const wgpu::Device& device, const std::string& label);
242
254 wgpu::BindGroup CreateTopologyBindGroup(const wgpu::Device& device, const std::string& label,
256
261
266
273
279
285
291
297 unsigned long GetExactPointBufferSize();
298
304 unsigned long GetExactCellBufferSize();
305
307
318
322 const char* GetGraphicsPipelineTypeAsString(GraphicsPipelineType graphicsPipelineType);
323
328 void SetupGraphicsPipelines(const wgpu::Device& device, vtkRenderer* renderer, vtkActor* actor);
329
336
338 {
339 struct
340 {
341 // point attributes.
342 wgpu::Buffer Buffer;
343 uint64_t Size = 0;
344 } Point;
345
346 struct
347 {
348 // cell attributes.
349 wgpu::Buffer Buffer;
350 uint64_t Size = 0;
351 } Cell;
352 };
354
356 {
357 vtkTypeUInt32 Start = 0;
358 vtkTypeUInt32 NumTuples = 0;
359 vtkTypeUInt32 NumComponents = 0;
360 };
362 {
370 vtkTypeUInt32 ApplyOverrideColors = 0;
371 vtkTypeFloat32 Opacity = 0;
372 vtkTypeUInt32 CompositeId = 0;
373 vtkTypeFloat32 Ambient[3] = {};
374 vtkTypeUInt32 ProcessId = 0;
375 vtkTypeFloat32 Diffuse[3] = {};
376 vtkTypeUInt32 Pickable = false;
377 };
379
381 vtkTimeStamp CellAttributesBuildTimestamp[CELL_NB_ATTRIBUTES];
382 vtkTimeStamp PointAttributesBuildTimestamp[POINT_NB_ATTRIBUTES];
386
387 bool HasPointAttributes[POINT_NB_ATTRIBUTES];
388 bool HasCellAttributes[CELL_NB_ATTRIBUTES];
389 bool RebuildGraphicsPipelines = true;
390 // used by RenderPiece and functions it calls to reduce
391 // calls to get the input and allow for rendering of
392 // other polydata (not the input)
393 vtkPolyData* CurrentInput = nullptr;
394 // vtkRenderer culls props to frustum. At that point, it requests
395 // mappers for bounds of the geometry. We cache the vtkAlgorithm output
396 // so that `UpdateMeshGeometryBuffers` can reuse it without climbing up
397 // vtkAlgorithm pipeline.
398 vtkPolyData* CachedInput = nullptr;
399
400 // 1 bind group for this polydata mesh
401 wgpu::BindGroup MeshAttributeBindGroup;
402
404 {
405 // buffer for the primitive cell ids and point ids.
406 wgpu::Buffer TopologyBuffer;
407 // buffer for edge array. this lets fragment shader hide internal edges of a polygon
408 // when edge visibility is turned on.
409 wgpu::Buffer EdgeArrayBuffer;
410 // // buffer for indirect draw command
411 // wgpu::Buffer IndirectDrawBuffer;
412 // bind group for the primitive size uniform.
413 wgpu::BindGroup BindGroup;
414 // vertexCount for draw call.
415 vtkTypeUInt32 VertexCount = 0;
416 };
417
421 std::string GraphicsPipelineKeys[GFX_PIPELINE_NB_TYPES] = {};
422
423 // Cache these so that subsequent executions of UpdateMeshGeometryBuffers() do not unnecessarily
424 // invoke MapScalars().
425 int LastScalarMode = -1;
426 bool LastScalarVisibility = false;
428 {
429 bool LastActorBackfaceCulling = false;
430 bool LastActorFrontfaceCulling = false;
431 bool LastVertexVisibility = false;
432 int LastRepresentation = VTK_SURFACE;
433 bool LastHasRenderingTranslucentGeometry = false;
434 };
435
436private:
438 friend class vtkWebGPURenderer;
439
443 wgpu::Buffer GetPointDataWGPUBuffer() { return this->MeshSSBO.Point.Buffer; }
444
448 wgpu::Buffer GetCellDataWGPUBuffer() { return this->MeshSSBO.Cell.Buffer; }
449
456 std::vector<vtkSmartPointer<vtkWebGPUComputeRenderBuffer>> SetupComputeRenderBuffers;
457
461 std::unordered_set<vtkSmartPointer<vtkWebGPUComputeRenderBuffer>> NotSetupComputeRenderBuffers;
462
466 const PointDataAttributes PointDataAttributesOrder[PointDataAttributes::POINT_NB_ATTRIBUTES] = {
467 PointDataAttributes::POINT_POSITIONS, PointDataAttributes::POINT_COLORS,
468 PointDataAttributes::POINT_NORMALS, PointDataAttributes::POINT_TANGENTS,
469 PointDataAttributes::POINT_UVS
470 };
471
475 const CellDataAttributes CellDataAttributesOrder[CellDataAttributes::CELL_NB_ATTRIBUTES] = {
476 CellDataAttributes::CELL_COLORS, CellDataAttributes::CELL_NORMALS
477 };
478
479 std::map<std::pair<vtkActor*, vtkRenderer*>, ActorState> CachedActorRendererProperties;
480
482 void operator=(const vtkWebGPUPolyDataMapper&) = delete;
483};
484VTK_ABI_NAMESPACE_END
485#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
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.
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().
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:332