VTK  9.7.20261114
vtkWebGPUContextDevice2DPrivate.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
8
9#ifndef vtkWebGPUContextDevice2DPrivate_h
10#define vtkWebGPUContextDevice2DPrivate_h
11
13#include "vtkWebGPUHandle.h"
15#include <unordered_map>
16#include <webgpu/webgpu.h>
17
18VTK_ABI_NAMESPACE_BEGIN
19
24
26{
27public:
28 vtkWebGPULinearAllocator(const std::string& label)
29 : Label(label)
30 {
31 }
32
34 {
35 if (this->CurrentBuffer)
36 wgpuBufferRelease(this->CurrentBuffer);
37 for (WGPUBuffer buf : this->StaleBuffers)
39 }
40
44 void Reset()
45 {
46 this->CurrentOffset = 0;
47
48 for (WGPUBuffer buf : this->StaleBuffers)
49 {
51 }
52 this->StaleBuffers.clear();
53 }
54
66 uint64_t Allocate(vtkWebGPUConfiguration* config, uint64_t sizeInBytes, WGPUBufferUsage usage)
67 {
68 uint64_t alignedSize = (sizeInBytes + 3) & ~3ull;
69
70 if (!this->CurrentBuffer || this->CurrentOffset + alignedSize > this->Capacity)
71 {
72 if (this->CurrentBuffer)
73 {
74 this->StaleBuffers.push_back(this->CurrentBuffer);
75 }
76
77 this->Capacity =
78 std::max({ this->Capacity * 2, alignedSize, static_cast<uint64_t>(4194304) });
79
80 this->CurrentBuffer = config->CreateBuffer(
81 this->Capacity, usage | WGPUBufferUsage_CopyDst, false, this->Label.c_str());
82
83 this->CurrentOffset = 0;
84 }
85
86 uint64_t allocatedOffset = this->CurrentOffset;
87 this->CurrentOffset += alignedSize;
88 return allocatedOffset;
89 }
90
95 WGPUBuffer GetBuffer() const { return this->CurrentBuffer; }
96
97private:
98 std::string Label;
99 WGPUBuffer CurrentBuffer = nullptr;
100 uint64_t Capacity = 0;
101 uint64_t CurrentOffset = 0;
102 std::vector<WGPUBuffer> StaleBuffers;
103};
104
110{
111public:
114
120 {
121 WGPUPrimitiveTopology topology;
124 double haveLines;
125 vtkWebGPU::RenderPipeline* outPipeline;
126 };
127
133 {
134 WGPUTexture Texture = nullptr;
135 WGPUTextureView View = nullptr;
136 WGPUBindGroup BindGroup = nullptr;
137 WGPUSampler Sampler = nullptr;
139 int Properties = 0;
140 int Width = 0;
141 int Height = 0;
142 };
143
153
154 WGPURenderPipeline TrianglePipeline = nullptr;
155 WGPURenderPipeline LinePipeline = nullptr;
156 WGPURenderPipeline TexturedPipeline = nullptr;
157
158 vtkWebGPULinearAllocator VertexAllocator{ "Context2D Dynamic VBO" };
159 WGPUBuffer ColorBuffer = nullptr;
160 WGPUBuffer MatrixUniformBuffer = nullptr;
161 WGPUBindGroup TransformBindGroup;
162 WGPUPipelineLayout PipelineLayout = nullptr;
163 WGPUVertexBufferLayout VertexBufferLayout;
164
165 WGPUTexture DummyTexture = nullptr;
166 WGPUTextureView DummyTextureView = nullptr;
167 WGPUSampler DefaultSampler = nullptr;
168 WGPUBindGroup ActiveTextureBindGroup = nullptr;
169 WGPUBindGroupLayout BindGroupLayout = nullptr;
170
171 std::unordered_map<vtkImageData*, CachedTexture> ImageCache;
172 std::unordered_map<std::string, CachedTexture> TextCache;
173 std::unordered_map<std::string, CachedTextData> TextImageCache;
174
175 std::vector<float> BatchedVertices;
176 WGPURenderPipeline CurrentBatchPipeline = nullptr;
177 WGPUBindGroup CurrentBatchBindGroup = nullptr;
178
179 bool ClippingEnabled = false;
180 int ClipRect[4] = { 0, 0, 0, 0 };
181
182 bool MatricesDirty = true;
183};
184
185VTK_ABI_NAMESPACE_END
186
187#endif // vtkWebGPUContextDevice2DPrivate_h
Hold a reference to a vtkObjectBase instance.
Create a webgpu device for use in rendering and compute pipelines.
WGPUBuffer CreateBuffer(std::uint64_t sizeBytes, WGPUBufferUsage usage, bool mappedAtCreation=false, const char *label=nullptr)
Convenient methods used to create webgpu buffers.
Container for vtkWebGPUContextDevice2D internal state.
std::unordered_map< vtkImageData *, CachedTexture > ImageCache
std::unordered_map< std::string, CachedTextData > TextImageCache
std::unordered_map< std::string, CachedTexture > TextCache
Linear allocator for dynamic WebGPU buffers.
void Reset()
Resets the internal offset and releases all stale buffers.
uint64_t Allocate(vtkWebGPUConfiguration *config, uint64_t sizeInBytes, WGPUBufferUsage usage)
Reserves a contiguous block of memory in the current WebGPU buffer.
WGPUBuffer GetBuffer() const
Gets the active WebGPU buffer object.
vtkWebGPULinearAllocator(const std::string &label)
Class to create and retrieve render pipelines based on a given key.
Stores rasterized text images and their bounding box dimensions.
Holds WebGPU texture resources and metadata for caching images and text.
Configuration parameters for creating or retrieving WebGPU render pipelines.
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:321
Reference-counted ownership for WebGPU C API handles.
#define wgpuBufferRelease