VTK  9.4.2
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(vtkRenderer* ren, vtkProp** propList, int& listLength, int& initialized) override;
85
86protected:
91
92private:
97 void SetupDepthBufferCopyPass();
98
102 void SetupMipmapsPass();
103
107 void SetupCullingPass();
108
116 void FirstPassRender(vtkRenderer* ren, vtkProp** propList, int listLength);
117
122 void CopyDepthBuffer();
123
127 void DepthMipmaps();
128
132 void PropCulling(vtkRenderer* ren, vtkProp** propList, int& listLength);
133
137 void UpdateCameraMVPBuffer(vtkRenderer* ren);
138
142 void UpdateBoundsBuffers(vtkProp** propList, int listLength);
143
148 void AddOcclusionCullingPipelineToRenderer(vtkRenderer* ren);
149
153 void CreateHierarchicalZBuffer();
154
160 int ComputeMipLevelsSizes(int width, int height);
161
166 void ResizeHierarchicalZBuffer(uint32_t newWidth, uint32_t newHeight);
167
172 void ResizeHierarchicalZBufferMipmapsChain();
173
178 void FinishSetupDepthCopyPass();
179
184 void FinishSetupMipmapsPass();
185
189 void FinishSetupCullingPass();
190
194 static void ReadIndicesCountCallback(const void* mappedData, void* indicesCount);
195
205 static void FillObjectsToDrawCallback(const void* mappedData, void* data);
206
211 static void OutputIndicesCulledCallback(const void* mappedData, void* data);
212
217 static void WindowResizedCallback(
218 vtkObject* caller, unsigned long eid, void* clientdata, void* calldata);
219
220 // Occlusion culling pipeline
221 vtkSmartPointer<vtkWebGPUComputePipeline> OcclusionCullingPipeline;
222
223 // Pass that copies the depth buffer of the render window into the mip level 0 of the hierarchical
224 // z-buffer
225 vtkSmartPointer<vtkWebGPUComputePass> DepthBufferCopyPass;
226
227 // Index of the hierarchical z buffer in the depth buffer copy compute pass
228 int HierarchicalZBufferTextureIndexCopyPass = -1;
229 // Index of the hierarchical z buffer in the depth buffer mipmaps pass
230 int HierarchicalZBufferTextureIndexMipmapsPass = -1;
231 // Index of the hierarchical z buffer in the culling pass
232 int HierarchicalZBufferTextureIndexCullingPass = -1;
233 // All the views necessary for the computation of the depth buffer mipmaps
234 std::vector<vtkSmartPointer<vtkWebGPUComputeTextureView>> HierarchicalZBufferMipmapViews;
235 // Texture view indices within the DepthMipmapsPass compute pass
236 std::vector<int> HierarchicalZBufferMipmapViewsIndices;
237 // Total number of mipmaps of the hierarchical z buffer
238 int HierarchicalZBufferMipmapCount = -1;
239 // Widths of the successive mipmaps of the hierarchical z buffer
240 std::vector<int> MipmapWidths;
241 // Heights of the successive mipmaps of the hierarchical z buffer
242 std::vector<int> MipmapHeights;
243 // We need to keep the uniform buffer here because we can only create it once the RenderWindow has
244 // set its Device to the pipeline of this occlusion culler. We will add the buffer to the pipeline
245 // on the first frame, when we're sure that the Device has been set
246
247 // Pass that downsamples the mipmap level 0 of the depth buffer into as many mipmap levels as
248 // possible
250 // Pass that does the culling of the actors against the hierarchial z-buffer
252
253 // Index of the hierarchical z buffer texture view in the culling pass
254 int CullingPassHierarchicalZBufferView = -1;
255 // Index of the bounds buffer in the culling pass
256 int CullingPassBoundsBufferIndex = -1;
257 // Index of the buffer that contains the indices of the props that passed the culling test in the
258 // culling pass
259 int CullingPassOutputIndicesBufferIndex = -1;
260 // How many props that were sent to the culling shader passed the culling test
261 int CullingPassOutputIndicesCountBufferIndex = -1;
262 // Index of the buffer that contains the indices of the props that were culled. Needed to update
263 // the visibility of the props in the PropsRendered array of the WGPURenderer
264 int CullingPassOutputIndicesCulledBufferIndex = -1;
265 // How many props were culled by the culling pass
266 int CullingPassOutputIndicesCulledCountBufferIndex = -1;
267 // Index of the buffer that contains the number of bounds to cull in the culling pass
268 int CullingPassBoundsCountBufferIndex = -1;
269 // Index of the buffer that contains the view projection matrix in the culling pass
270 int CullingPassMVPMatrixBufferIndex = -1;
271
272 // Structure passed to the callbacks for reading the results of the culling pass
273 struct FillObjectsToDrawCallbackMapData
274 {
275 // How many props passed the culling test.
276 // This is a pointer to the 'listLength' parameter of the Cull() method
277 int* listLength = nullptr;
278 // Prop list of the renderer that needs to be updated
279 vtkProp** propList = nullptr;
280
281 // WebGPU renderer
282 // Used for accessing the 'rendered last frame' list
283 vtkWebGPURenderer* renderer = nullptr;
284 };
285
286 // Structure passed to the callbacks for reading the results of the culling pass
287 struct OutputIndicesCulledMapData
288 {
289 // Point to the renderer for removing the props that were culled from the list of "props
290 // rendered last frame"
291 vtkWebGPURenderer* renderer = nullptr;
292
293 // Prop list of the renderer
294 vtkProp** propList = nullptr;
295
296 // How many props were culled
297 int culledCount = -1;
298 };
299
300 // If this is the first frame, every object is going to be rendered b the FirstPassRender to fill
301 // the z-buffer
302 bool FirstFrame = true;
303 // Whether or not we're done initializing the compute passes of this culler
304 bool Initialized = false;
305 // Render window whose depth buffer we're going to use for the culling
306 vtkWeakPointer<vtkWebGPURenderWindow> WebGPURenderWindow = nullptr;
307
308 // Callback command for when the render window that this occlusion culler is attached to is
309 // resized
310 vtkSmartPointer<vtkCallbackCommand> WindowResizedCallbackCommand;
311};
312
313VTK_ABI_NAMESPACE_END
314
315#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.
vtkWebGPUComputeOcclusionCuller(const vtkWebGPUComputeOcclusionCuller &)=delete
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.
void operator=(const vtkWebGPUComputeOcclusionCuller &)=delete
double Cull(vtkRenderer *ren, vtkProp **propList, int &listLength, int &initialized) override
Culls props and returns the number of props that still need to be rendered after the culling.