VTK  9.4.20250207
vtkWebGPUComputeOcclusionCuller.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
4#ifndef vtkWebGPUComputeOcclusionCuller_h
5#define vtkWebGPUComputeOcclusionCuller_h
6
7#include "vtkCallbackCommand.h" // for the bounds recomputed callback
8#include "vtkCuller.h"
9#include "vtkNew.h" // for new macro
10#include "vtkRenderingWebGPUModule.h" // For export macro
11#include "vtkSmartPointer.h" // for the pipeline smart pointer
12#include "vtkWebGPUComputePass.h" // for compute passes
13#include "vtkWebGPUComputePipeline.h" // for the member compute pipeline
14#include "vtkWebGPURenderWindow.h" // for the render window weak pointer member
15
16VTK_ABI_NAMESPACE_BEGIN
17
68class VTKRENDERINGWEBGPU_EXPORT vtkWebGPUComputeOcclusionCuller : public vtkCuller
69{
70public:
73
74 void PrintSelf(ostream& os, vtkIndent indent) override;
75
79 void SetRenderWindow(vtkWebGPURenderWindow* webGpuRenderWindow);
80
84 double Cull(
85 vtkRenderer* renderer, vtkProp** propList, int& listLength, int& initialized) override;
86
87protected:
90
91private:
93 void operator=(const vtkWebGPUComputeOcclusionCuller&) = delete;
98 void SetupDepthBufferCopyPass();
99
103 void SetupMipmapsPass();
104
108 void SetupCullingPass();
109
117 void FirstPassRender(vtkRenderer* renderer, vtkProp** propList, int listLength);
118
123 void CopyDepthBuffer();
124
128 void DepthMipmaps();
129
133 void PropCulling(vtkRenderer* renderer, vtkProp** propList, int& listLength);
134
138 void UpdateCameraMVPBuffer(vtkRenderer* renderer);
139
143 void UpdateBoundsBuffers(vtkProp** propList, int listLength);
144
149 void AddOcclusionCullingPipelineToRenderer(vtkRenderer* renderer);
150
154 void CreateHierarchicalZBuffer();
155
161 int ComputeMipLevelsSizes(int width, int height);
162
167 void ResizeHierarchicalZBuffer(uint32_t newWidth, uint32_t newHeight);
168
173 void ResizeHierarchicalZBufferMipmapsChain();
174
179 void FinishSetupDepthCopyPass();
180
185 void FinishSetupMipmapsPass();
186
190 void FinishSetupCullingPass();
191
195 static void ReadIndicesCountCallback(const void* mappedData, void* indicesCount);
196
206 static void FillObjectsToDrawCallback(const void* mappedData, void* data);
207
212 static void OutputIndicesCulledCallback(const void* mappedData, void* data);
213
218 static void WindowResizedCallback(
219 vtkObject* caller, unsigned long eid, void* clientdata, void* calldata);
220
221 // Occlusion culling pipeline
222 vtkSmartPointer<vtkWebGPUComputePipeline> OcclusionCullingPipeline;
223
224 // Pass that copies the depth buffer of the render window into the mip level 0 of the hierarchical
225 // z-buffer
226 vtkSmartPointer<vtkWebGPUComputePass> DepthBufferCopyPass;
227
228 // Index of the hierarchical z buffer in the depth buffer copy compute pass
229 int HierarchicalZBufferTextureIndexCopyPass = -1;
230 // Index of the hierarchical z buffer in the depth buffer mipmaps pass
231 int HierarchicalZBufferTextureIndexMipmapsPass = -1;
232 // Index of the hierarchical z buffer in the culling pass
233 int HierarchicalZBufferTextureIndexCullingPass = -1;
234 // All the views necessary for the computation of the depth buffer mipmaps
235 std::vector<vtkSmartPointer<vtkWebGPUComputeTextureView>> HierarchicalZBufferMipmapViews;
236 // Texture view indices within the DepthMipmapsPass compute pass
237 std::vector<int> HierarchicalZBufferMipmapViewsIndices;
238 // Total number of mipmaps of the hierarchical z buffer
239 int HierarchicalZBufferMipmapCount = -1;
240 // Widths of the successive mipmaps of the hierarchical z buffer
241 std::vector<int> MipmapWidths;
242 // Heights of the successive mipmaps of the hierarchical z buffer
243 std::vector<int> MipmapHeights;
244 // We need to keep the uniform buffer here because we can only create it once the RenderWindow has
245 // set its Device to the pipeline of this occlusion culler. We will add the buffer to the pipeline
246 // on the first frame, when we're sure that the Device has been set
247
248 // Pass that downsamples the mipmap level 0 of the depth buffer into as many mipmap levels as
249 // possible
251 // Pass that does the culling of the actors against the hierarchial z-buffer
253
254 // Index of the hierarchical z buffer texture view in the culling pass
255 int CullingPassHierarchicalZBufferView = -1;
256 // Index of the bounds buffer in the culling pass
257 int CullingPassBoundsBufferIndex = -1;
258 // Index of the buffer that contains the indices of the props that passed the culling test in the
259 // culling pass
260 int CullingPassOutputIndicesBufferIndex = -1;
261 // How many props that were sent to the culling shader passed the culling test
262 int CullingPassOutputIndicesCountBufferIndex = -1;
263 // Index of the buffer that contains the indices of the props that were culled. Needed to update
264 // the visibility of the props in the PropsRendered array of the WGPURenderer
265 int CullingPassOutputIndicesCulledBufferIndex = -1;
266 // How many props were culled by the culling pass
267 int CullingPassOutputIndicesCulledCountBufferIndex = -1;
268 // Index of the buffer that contains the number of bounds to cull in the culling pass
269 int CullingPassBoundsCountBufferIndex = -1;
270 // Index of the buffer that contains the view projection matrix in the culling pass
271 int CullingPassMVPMatrixBufferIndex = -1;
272
273 // Structure passed to the callbacks for reading the results of the culling pass
274 struct FillObjectsToDrawCallbackMapData
275 {
276 // How many props passed the culling test.
277 // This is a pointer to the 'listLength' parameter of the Cull() method
278 int* listLength = nullptr;
279 // Prop list of the renderer that needs to be updated
280 vtkProp** propList = nullptr;
281
282 // WebGPU renderer
283 // Used for accessing the 'rendered last frame' list
284 vtkWebGPURenderer* renderer = nullptr;
285 };
286
287 // Structure passed to the callbacks for reading the results of the culling pass
288 struct OutputIndicesCulledMapData
289 {
290 // Point to the renderer for removing the props that were culled from the list of "props
291 // rendered last frame"
292 vtkWebGPURenderer* renderer = nullptr;
293
294 // Prop list of the renderer
295 vtkProp** propList = nullptr;
296
297 // How many props were culled
298 int culledCount = -1;
299 };
300
301 // If this is the first frame, every object is going to be rendered b the FirstPassRender to fill
302 // the z-buffer
303 bool FirstFrame = true;
304 // Whether or not we're done initializing the compute passes of this culler
305 bool Initialized = false;
306 // Render window whose depth buffer we're going to use for the culling
307 vtkWeakPointer<vtkWebGPURenderWindow> WebGPURenderWindow = nullptr;
308
309 // Callback command for when the render window that this occlusion culler is attached to is
310 // resized
311 vtkSmartPointer<vtkCallbackCommand> WindowResizedCallbackCommand;
312};
313
314VTK_ABI_NAMESPACE_END
315
316#endif
a superclass for prop cullers
Definition vtkCuller.h:31
a simple class to control print indentation
Definition vtkIndent.h:108
abstract base class for most VTK objects
Definition vtkObject.h:162
abstract superclass for all actors, volumes and annotations
Definition vtkProp.h:66
abstract specification for renderers
Hold a reference to a vtkObjectBase instance.
a weak reference to a vtkObject.
This culler does both frustum culling and occlusion culling.
double Cull(vtkRenderer *renderer, vtkProp **propList, int &listLength, int &initialized) override
Culls props and returns the number of props that still need to be rendered after the culling.
void SetRenderWindow(vtkWebGPURenderWindow *webGpuRenderWindow)
Sets which render window this occlusion culler is going to work on.
static vtkWebGPUComputeOcclusionCuller * New()
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
WebGPU rendering window.