VTK  9.7.1
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 #include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
16 
17 VTK_ABI_NAMESPACE_BEGIN
18 
69 class VTKRENDERINGWEBGPU_EXPORT VTK_MARSHALAUTO vtkWebGPUComputeOcclusionCuller : public vtkCuller
70 {
71 public:
74 
75  void PrintSelf(ostream& os, vtkIndent indent) override;
76 
80  void SetRenderWindow(vtkWebGPURenderWindow* webGpuRenderWindow);
81 
85  double Cull(
86  vtkRenderer* renderer, vtkProp** propList, int& listLength, int& initialized) override;
87 
88 protected:
91 
92 private:
94  void operator=(const vtkWebGPUComputeOcclusionCuller&) = delete;
99  void SetupDepthBufferCopyPass();
100 
104  void SetupMipmapsPass();
105 
109  void SetupCullingPass();
110 
118  void FirstPassRender(vtkRenderer* renderer, vtkProp** propList, int listLength);
119 
124  void CopyDepthBuffer();
125 
129  void DepthMipmaps();
130 
134  void PropCulling(vtkRenderer* renderer, vtkProp** propList, int& listLength);
135 
139  void UpdateCameraMVPBuffer(vtkRenderer* renderer);
140 
144  void UpdateBoundsBuffers(vtkProp** propList, int listLength);
145 
150  void AddOcclusionCullingPipelineToRenderer(vtkRenderer* renderer);
151 
155  void CreateHierarchicalZBuffer();
156 
162  int ComputeMipLevelsSizes(int width, int height);
163 
168  void ResizeHierarchicalZBuffer(uint32_t newWidth, uint32_t newHeight);
169 
174  void ResizeHierarchicalZBufferMipmapsChain();
175 
180  void FinishSetupDepthCopyPass();
181 
186  void FinishSetupMipmapsPass();
187 
191  void FinishSetupCullingPass();
192 
196  static void ReadIndicesCountCallback(const void* mappedData, void* indicesCount);
197 
207  static void FillObjectsToDrawCallback(const void* mappedData, void* data);
208 
213  static void OutputIndicesCulledCallback(const void* mappedData, void* data);
214 
219  static void WindowResizedCallback(
220  vtkObject* caller, unsigned long eid, void* clientdata, void* calldata);
221 
222  // Occlusion culling pipeline
223  vtkSmartPointer<vtkWebGPUComputePipeline> OcclusionCullingPipeline;
224 
225  // Pass that copies the depth buffer of the render window into the mip level 0 of the hierarchical
226  // z-buffer
227  vtkSmartPointer<vtkWebGPUComputePass> DepthBufferCopyPass;
228 
229  // Index of the hierarchical z buffer in the depth buffer copy compute pass
230  int HierarchicalZBufferTextureIndexCopyPass = -1;
231  // Index of the hierarchical z buffer in the depth buffer mipmaps pass
232  int HierarchicalZBufferTextureIndexMipmapsPass = -1;
233  // Index of the hierarchical z buffer in the culling pass
234  int HierarchicalZBufferTextureIndexCullingPass = -1;
235  // All the views necessary for the computation of the depth buffer mipmaps
236  std::vector<vtkSmartPointer<vtkWebGPUComputeTextureView>> HierarchicalZBufferMipmapViews;
237  // Texture view indices within the DepthMipmapsPass compute pass
238  std::vector<int> HierarchicalZBufferMipmapViewsIndices;
239  // Total number of mipmaps of the hierarchical z buffer
240  int HierarchicalZBufferMipmapCount = -1;
241  // Widths of the successive mipmaps of the hierarchical z buffer
242  std::vector<int> MipmapWidths;
243  // Heights of the successive mipmaps of the hierarchical z buffer
244  std::vector<int> MipmapHeights;
245  // We need to keep the uniform buffer here because we can only create it once the RenderWindow has
246  // set its Device to the pipeline of this occlusion culler. We will add the buffer to the pipeline
247  // on the first frame, when we're sure that the Device has been set
248 
249  // Pass that downsamples the mipmap level 0 of the depth buffer into as many mipmap levels as
250  // possible
251  vtkSmartPointer<vtkWebGPUComputePass> DepthMipmapsPass;
252  // Pass that does the culling of the actors against the hierarchial z-buffer
254 
255  // Index of the hierarchical z buffer texture view in the culling pass
256  int CullingPassHierarchicalZBufferView = -1;
257  // Index of the bounds buffer in the culling pass
258  int CullingPassBoundsBufferIndex = -1;
259  // Index of the buffer that contains the indices of the props that passed the culling test in the
260  // culling pass
261  int CullingPassOutputIndicesBufferIndex = -1;
262  // How many props that were sent to the culling shader passed the culling test
263  int CullingPassOutputIndicesCountBufferIndex = -1;
264  // Index of the buffer that contains the indices of the props that were culled. Needed to update
265  // the visibility of the props in the PropsRendered array of the WGPURenderer
266  int CullingPassOutputIndicesCulledBufferIndex = -1;
267  // How many props were culled by the culling pass
268  int CullingPassOutputIndicesCulledCountBufferIndex = -1;
269  // Index of the buffer that contains the number of bounds to cull in the culling pass
270  int CullingPassBoundsCountBufferIndex = -1;
271  // Index of the buffer that contains the view projection matrix in the culling pass
272  int CullingPassMVPMatrixBufferIndex = -1;
273 
274  // Structure passed to the callbacks for reading the results of the culling pass
275  struct FillObjectsToDrawCallbackMapData
276  {
277  // How many props passed the culling test.
278  // This is a pointer to the 'listLength' parameter of the Cull() method
279  int* listLength = nullptr;
280  // Prop list of the renderer that needs to be updated
281  vtkProp** propList = nullptr;
282 
283  // WebGPU renderer
284  // Used for accessing the 'rendered last frame' list
285  vtkWebGPURenderer* renderer = nullptr;
286  };
287 
288  // Structure passed to the callbacks for reading the results of the culling pass
289  struct OutputIndicesCulledMapData
290  {
291  // Point to the renderer for removing the props that were culled from the list of "props
292  // rendered last frame"
293  vtkWebGPURenderer* renderer = nullptr;
294 
295  // Prop list of the renderer
296  vtkProp** propList = nullptr;
297 
298  // How many props were culled
299  int culledCount = -1;
300  };
301 
302  // If this is the first frame, every object is going to be rendered b the FirstPassRender to fill
303  // the z-buffer
304  bool FirstFrame = true;
305  // Whether or not we're done initializing the compute passes of this culler
306  bool Initialized = false;
307  // Render window whose depth buffer we're going to use for the culling
308  vtkWeakPointer<vtkWebGPURenderWindow> WebGPURenderWindow = nullptr;
309 
310  // Callback command for when the render window that this occlusion culler is attached to is
311  // resized
312  vtkSmartPointer<vtkCallbackCommand> WindowResizedCallbackCommand;
313 };
314 
315 VTK_ABI_NAMESPACE_END
316 
317 #endif
abstract superclass for all actors, volumes and annotations
Definition: vtkProp.h:49
abstract base class for most VTK objects
Definition: vtkObject.h:52
abstract specification for renderers
Definition: vtkRenderer.h:63
a superclass for prop cullers
Definition: vtkCuller.h:30
a simple class to control print indentation
Definition: vtkIndent.h:28
This culler does both frustum culling and occlusion culling.
#define VTK_MARSHALAUTO
WebGPU rendering window.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
virtual double Cull(vtkRenderer *ren, vtkProp **propList, int &listLength, int &initialized)=0
This is called outside the render loop by vtkRenderer.