VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtGpuCulling.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2025 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <osg/FrameStamp>
13 #include <osg/Camera>
14 
15 #include <vrvOsg/vrvOsg.h>
16 
23 
24 
25 #include <vrvMath/DtCommonTypes.h>
26 #include <vrvMath/DtVector4.h>
27 #define CPU_SIDE_COMPILATION 1
28 #include <../data/shaders/indirect/GeometrySpecification.h>
29 
33 
35 
36 #include <map>
37 #include <unordered_map>
38 #include <unordered_set>
39 #include <vector>
40 #include <thread>
41 
42 namespace osg
43 {
44  class State;
45  class RenderInfo;
46 };
47 
48 namespace makVrv
49 {
50  class DtGlBuffer;
51  class DtGpuArenaUploadState;
52  class DtIndirectGeometryRegistrar;
53  class DtInstanceContainer;
54  class DtOsgShaderFilesCache;
55  class DtIndirectCullProgram;
56  class DtCpuCullingHelper;
57  class DtOsgLightPointManager;
58  class DtGlDeleteObjectManager;
59  class DtCoreGlBufferCreator;
60  class DtIndirectClearBufferProgram;
61  class DtIndirectModelInfo;
62  class DtIndirectGlTextureObject;
63  class DtIndirectGlTextureObjectManager;
64  class DtGpuCullingTcsUserData;
65  class DtThreadCameraStreamDataManager;
66 
68  class DtGpuCulling;
69 
72 
74  template <>
75  inline const char* creatorTypeName<DtGpuCulling>() { return "DtGpuCullingCreator"; }
76 
92  {
93  public:
97  {
98  int myOffset;
100 
102  {
103  return myOffset == rhs.myOffset && myNumOfInstances == rhs.myNumOfInstances;
104  }
105  };
106 
110  {
111  int myOffset;
113  };
114 
115  using VectorStateIdOffsetAndMaxCommands = std::vector<StateIdOffsetAndMaxCommands>;
116 
118  using CameraSet = std::set<osg::observer_ptr<const osg::Camera> >;
119 
121  using MapStateToGeometrySpecIndices = std::map<int, VectorInts>;
122 
123  public:
124  //friend class so that it can be created only by its creator
126 
128  virtual ~DtGpuCulling();
129 
130  protected:
132  DtGpuCulling(DtDe& de, DtInstanceContainer& container, const DtCullShaderConfig&);
133 
134  private:
136  DtGpuCulling() = delete;
138  DtGpuCulling(const DtGpuCulling& other) = delete;
140  DtGpuCulling& operator=(const DtGpuCulling& other) = delete;
141 
142  public:
144  virtual void setRegistrar(DtIndirectGeometryRegistrar* registrar);
145 
147  virtual void setRegistrarInvalid();
148 
150  virtual void setDebug(bool debug);
151 
153  virtual const DtCullShaderConfig& cullShaderConfig() const;
154 
157  virtual bool updateGpuBuffers(DtGpuArenaUploadState& uploadState);
158 
161  virtual void modelIdsToNumInstancesChanged();
162 
166  virtual unsigned int updatePerInstancePerGeomBuffers(const MapIntToInt& modelIdsToNumInstances,
167  const osg::Camera* camera, unsigned int currentFrame);
168 
180  virtual void doCulling(const DtIndirectViewParameters& viewParameters
181  , const osg::FrameStamp* frameStamp
182  , const DtGlBuffer* gpuLodVisibleBuffer
183  , const DtGlBuffer* gpuDofTransformBuffer
184  , const DtGlBuffer* gpuSwitchStateBuffer
185  , const DtGlBuffer* gpuPerInstanceDataBuffer
186  , const osg::Camera* camera
187  , bool& outputInstanceBufferChanged);
188 
190  static bool setUpOutputFormatAndBindings();
191 
193  static void unsetUpOutputFormatAndBindings();
194 
199  virtual bool bindGpuOutputInstanceBuffersWithAddressRange(const osg::Camera* camera) const;
200 
206  virtual bool bindCommandBufferAndSsbos(const DtGlBuffer* gpuDofTransformBuffer
207  , const DtGlBuffer* gpuDofModelScaleBuffer
208  , const DtGlBuffer* gpuUvOffsetBuffer
209  , const DtGlBuffer* gpuPerInstanceDataBuffer
210  , const osg::Camera* camera);
211 
213  virtual void unbindCommandBufferAndSsbos(const osg::Camera* camera);
214 
218  // It is an instanced buffer. It advances once per instance.
219  virtual void bindGpuOutputInstanceBufferDebug(const osg::Camera* camera) const;
220 
223  virtual void unbindGpuOutputInstanceBufferDebug(const osg::Camera* camera) const;
224 
228  virtual bool bindIndirectThingsDebug(const DtGlBuffer* gpuDofTransformBuffer, const osg::Camera* camera);
229 
231  virtual void unbindIndirectThingsDebug(const osg::Camera* camera);
232 
235  virtual bool useInstanceCount() const;
236 
239  virtual int numDrawCommands(const osg::Camera* camera) const;
240 
242  virtual void releaseGLObjects();
243 
245  virtual void reloadShaders() const;
246 
248  virtual bool buffersInitializedForCamera(const osg::Camera* camera) const;
249 
251  virtual unsigned int maxNumInstances() const;
252 
254  virtual unsigned int numGeometrySpecifications() const;
255 
257  virtual const VectorStateIdOffsetAndMaxCommands& stateIdOffsetAndMaxCommands() const;
258 
260  virtual const std::vector<InstancesOffsetAndNumOfInstances>& instancesOffsetAndNumInstances() const;
261 
263  virtual bool readyToUpdateAndGpuBuffersNeedUpdate() const;
264 
266  virtual bool gpuBuffersNeedUpdate() const;
267 
269  virtual bool textureUpdateDirty() const;
270 
272  virtual void setTextureUpdateDirty(bool flag);
273 
275  virtual bool geometryAndMaterialsDirty() const;
276 
278  virtual void setGeometryAndMaterialsDirty(bool flag);
279 
281  virtual bool instancesInvalidatedDirty() const;
282 
284  virtual void invalidateInstance(int instanceId);
285 
287  virtual const DtGlBuffer* gpuInputPerGeomInstancesBuffer() const;
288 
290  virtual const DtGlBuffer* gpuMaterialsBuffer() const;
291 
293  virtual void fillOutputInstances(std::vector< DtVector4<int> >& outputInstances, const osg::Camera* camera) const;
294 
296  virtual const DtIndirectEffectTexturesTemplate& currentEffectTexturesState() const;
297 
299  virtual void setName(const std::string& name);
300 
302  virtual const std::string& name() const;
303 
304  protected:
305 
307 
308 
315  virtual void updateGpuInputData(unsigned int currentFrame);
316 
319  virtual void updateGpuInputPerGeomInstancesBuffer();
320 
321  virtual bool updateMaterialsAndGeometrySpecifications(DtGpuArenaUploadState& uploadState);
322  virtual void transferMaterialsAndGeometrySpecificationsToGpuBuffers(size_t numUniqueGeometries, DtGpuArenaUploadState& uploadState);
323  virtual void updateGpuLightPointBuffers();
324  virtual void updateGpuIndirectBuffersInstanceCount(const MapIntToInt& modelIdsToNumInstances, DtGpuCullingTcsUserData* userData);
325  virtual void updateGpuIndirectBuffersNonInstanceCount(const MapIntToInt& modelIdsToNumInstances, DtGpuCullingTcsUserData* userData);
326  virtual void clearGpuIndirectBuffer(DtGpuCullingTcsUserData* userData);
327  virtual void clearStateIdCommandCountBuffer();
328  virtual void clearLightSourceIndexOutputBuffer(unsigned int maxNumLightSources, DtGpuCullingTcsUserData* userData);
329 
331 
334  virtual void buildStateToGeometrySpecIndicesMap(const std::vector<DtGeometrySpecification>& geometrySpecifications,
335  const std::vector<InstancesOffsetAndNumOfInstances>& instancesOffsetAndNumInstances,
336  MapStateToGeometrySpecIndices& outIndices, int& outMaxStateId) const;
337 
339  virtual void populateIndirectDrawBuffersByState(const int numGeometries,
340  MapStateToGeometrySpecIndices& mapStateToSpecIndices, DtGpuCullingTcsUserData* userData);
341 
344  virtual void updateStateIdToMaxNumCommands(const MapStateToGeometrySpecIndices& mapStateToSpecIndices, int maxStateId);
345 
348  void accumulateStateIdToMaxCommands(const DtGeometrySpecVector& locations, const int numInstances,
349  std::unordered_map<int, int>& stateIdToMaxCommands, int& maxStateId) const;
350 
354  virtual bool processGpuBufferUploadsForModelInfo(DtIndirectModelInfo* info, DtGpuArenaUploadState& uploadState);
355 
359  virtual void updateGpuTexturesBuffer(DtGpuArenaUploadState& uploadState);
360 
364  virtual bool processModelTextures(const DtIndirectModelInfo* modelInfo, GLuint64* stagingBuffer,
365  int& texturesIndex, int numTextures, const std::vector<int>& offsetLookup, DtGpuArenaUploadState& uploadState,
366  int sizeOfGpuTextureStructure, int sizeGpuTextureBuffersRequired, int& numTexturesStillDownloading);
367 
371  virtual bool processEffectTexturesForGeometry(
372  const DtIndirectGeometryTextures& texturesForThisGeometry, GLuint64* stagingBuffer,
373  int texturesIndex, int numTextures, const std::vector<int>& offsetLookup,
374  DtGpuArenaUploadState& uploadState, int& numTexturesStillDownloading);
375 
379  virtual int updateEffectTextureState();
380 
385  virtual int updateGeometryCountState();
386 
391  virtual void buildEffectTextureOffsetLookup(std::vector<int>& offsetLookup) const;
392 
394  virtual void updateInstancesOffsetAndInstances(const MapIntToInt& modelIdsToNumInstances);
395 
397  virtual void updateInstancesOffsetAndInstancesBuffer();
398 
400  virtual void updateStateIdToMaxNumCommands(const MapIntToInt& modelIdsToNumInstances);
401 
403  virtual void updateStateIdToMaxNumCommandsBuffer();
404 
406  virtual void deleteBuffers();
407 
409  virtual void flushSingleCameraSet(CameraSet& cameraSet);
410 
413  virtual void incrementUsers(DtIndirectModelInfo* info);
414 
416  virtual void releaseGlObjectsDecrementUsersAndUnLoadIfNecessary();
417 
419  virtual DtGpuCullingTcsUserData* getOrCreate(const osg::Camera* camera);
420 
422  virtual bool debugCullingActive() const;
423 
424  protected:
428 
432 
434 
436  std::vector<DtGeometrySpecification> myGeometrySpecifications;
437 
441 
443  std::vector<DrawElementsIndirectBindlessCommandNV> myBindlessIndexAndVertexBuffers;
444 
447 
451 
454 
456 
458 
460 
461  std::vector<InstancesOffsetAndNumOfInstances> myInstancesOffsetAndNumInstances;
463 
465  unsigned int myMaxNumInstances;
466 
467  unsigned int myMaxNumLightSources;
468 
470 
471  // For when instance count is false or true
473 
474  // For when instance count if false
477 
478  // For when instance count is true
481 
483 
485 
486  //texture state
492 
494 
496 
499 
500  std::string myName;
501 
505 
507 
508  std::unordered_set<DtIndirectModelInfo*> myModelsBeingUsed;
509 
511 
512  std::unordered_set<DtIndirectGlTextureObject*> myIndirectTexturesBeingUsed;
513 
515 
516  static std::thread::id theMainThreadId;
517 
519 
521  };
522 }
523 
unsigned int myMaxNumInstancesPreCulling
Definition: DtGpuCulling.h:464
Contains the DtCullShaderConfig class declaration.
DtCoreGlBufferCreator & myDtCoreGlBufferCreator
Definition: DtGpuCulling.h:497
static const int theGpuOutputInstanceOffsetInPerInstanceDataBufferIndex
Definition: DtGpuCulling.h:503
Manager for DtThreadCameraStreamData Holds them as cameras come in. Deletes them as cameras go out (v...
Definition: DtThreadCameraStreamDataManager.h:86
DtIndirectGeometryRegistrar * myRegistrar
Definition: DtGpuCulling.h:425
DtMaterial class declaration.
DtGlBuffer * myGpuInstancesOffsetAndNumInstancesBuffer
Definition: DtGpuCulling.h:462
std::map< int, VectorInts > MapStateToGeometrySpecIndices
map of state id to vector of geometry spec indices
Definition: DtGpuCulling.h:121
const DtInstanceContainer & myInstanceContainer
Definition: DtGpuCulling.h:427
unsigned int myLastGpuInputDataFrame
Definition: DtGpuCulling.h:469
Contains makVrv::DtIndirectInstancesInvalidated class.
DtOsgLightPointManager & myLightPointManager
Definition: DtGpuCulling.h:482
std::vector< InstancesOffsetAndNumOfInstances > myInstancesOffsetAndNumInstances
Definition: DtGpuCulling.h:461
static const int theGpuOutputInstanceBufferDebugIndex
Definition: DtGpuCulling.h:504
const char * creatorTypeName< DtGpuCulling >()
template specialization for the creator (pass the class being created to the creatorTypeName template...
Definition: DtGpuCulling.h:75
DtIndirectClearBufferProgram * myClearBufferProgram
Definition: DtGpuCulling.h:506
#define DT_DLL_VRVOSG
Definition: vrvOsg.h:23
std::vector< DrawElementsIndirectBindlessCommandNV > myBindlessIndexAndVertexBuffers
the bindless vertex and index buffers
Definition: DtGpuCulling.h:443
creator for DtCoreGlBuffer
Definition: DtCoreGlBuffer.h:66
This manager manages the lifetime of the actual gl texture objects (gl unsigned ints) in the indirect...
Definition: DtIndirectGlTextureObjectManager.h:61
This class is a component of the DtIndirectGeometry It inputs are: -&gt; the instances registered with Dt...
Definition: DtGpuCulling.h:91
int myOffset
Definition: DtGpuCulling.h:111
Indirect geometry can have multiple models and their corresponding ids registered in it...
Definition: DtIndirectGeometryRegistrar.h:111
Contains makVrv::DtFirstTextureStreamedModelAndIndexTracker class.
DtGlBuffer * myGpuStateIdToMaxNumCommandsBuffer
Definition: DtGpuCulling.h:475
DtGlDeleteObjectManager & myGlDeleteObjectManager
Definition: DtGpuCulling.h:510
Contains the DtGpuCullingTcsUserData class declaration.
bool operator==(const InstancesOffsetAndNumOfInstances &rhs) const
Definition: DtGpuCulling.h:101
4 dimensional vector
DtGlBuffer * myBindlessIndexAndVertexBuffersBuffer
The gpu buffer holding the same above info.
Definition: DtGpuCulling.h:446
VectorInts myGeomSpecIndexToCommandIndex
Definition: DtGpuCulling.h:479
DtIndirectInstancesInvalidated myInstancesInvalidated
Definition: DtGpuCulling.h:431
shader configuration storage class. Has 3 available options which are described in the constructor&#39;s ...
Definition: DtCullShaderConfig.h:21
DtModelIdAndStreamedBytesTracker mySpecBufferTracker
Definition: DtGpuCulling.h:493
DtOsgLightPointManager registers LightPoint nodes so their TBO can be created and updated in a single...
Definition: DtOsgLightPointManager.h:128
std::map< int, int > MapIntToInt
Definition: DtCommonTypes.h:25
DtGlBuffer * myGpuInputPerGeomInstancesBuffer
contains switch states/ lod info
Definition: DtGpuCulling.h:430
DtFirstTextureStreamedModelAndIndexTracker myFirstTextureStreamedModelAndIndexTracker
Keep track of the first model and what index to start using bindless textures.
Definition: DtGpuCulling.h:488
This represents per camera (tcs) specific data for DtGpuCulling.
Definition: DtGpuCullingTcsUserData.h:23
bool myGeometryAndMaterialsDirty
Definition: DtGpuCulling.h:433
DtThreadCameraStreamDataManager & myTcsDataManager
Definition: DtGpuCulling.h:518
Variadic templated creator class for defining a DtVirtualBaseClass creator that is automatically regi...
Definition: DtVirtualBaseClassCreator.h:23
Contains the DtIndirectEffectTexturesTemplate class declaration.
DtGlBuffer * myGpuSpecIndexToCommandIndexBuffer
Definition: DtGpuCulling.h:480
DtIndirectEffectTexturesTemplate myCurrentEffectTextureState
Definition: DtGpuCulling.h:498
holds a set of the instances that need to be invalidated in the instance buffers
Definition: DtIndirectInstancesInvalidated.h:19
Definition: DtModelIdAndStreamedBytesTracker.h:18
unsigned int myLastNumUniqueGeometriesForGpuTextures
Definition: DtGpuCulling.h:490
unsigned int myMaxNumInstances
Definition: DtGpuCulling.h:465
unsigned int myLastNumEffectTextures
Definition: DtGpuCulling.h:489
std::vector< int > VectorInts
Definition: DtCommonTypes.h:20
handles the running of the compute shader pass for indirect culling, LODing and switches ...
Definition: DtIndirectCullProgram.h:54
DtGpuCullingTcsUserDataCreator & myUserDataCreator
Definition: DtGpuCulling.h:520
DtGlBuffer * myGpuMaterialsBuffer
The spec buffer containing Materials for all the models/lods of the passed in instances.
Definition: DtGpuCulling.h:450
static std::thread::id theMainThreadId
Definition: DtGpuCulling.h:516
This class represents the list of DtIndirectTexturePrototypes belonging to a particular sub-geometry...
Definition: DtIndirectGeometryTextures.h:38
Contains makVrv::DtIndirectModelSubGeometry class.
structure for holding an offset of an instance type (model id), and number of those instances ...
Definition: DtGpuCulling.h:96
Manages and tracks memory usage statistics for GPU resource upload operations.
Definition: DtGpuArenaUploadState.h:39
DtIndirectCullProgram * myCullProgram
Definition: DtGpuCulling.h:457
unsigned int myMaxNumLightSources
Definition: DtGpuCulling.h:467
A generic OpenGL buffer. This can be: -&gt; a vertex buffer -&gt; an index buffer -&gt; a shader storage buffe...
Definition: DtGlBuffer.h:29
int myNumOfInstances
Definition: DtGpuCulling.h:99
DtGlBuffer * myGpuStateIdToCommandCountBuffer
Definition: DtGpuCulling.h:476
const DtCullShaderConfig myCullShaderConfig
Definition: DtGpuCulling.h:459
DtVirtualBaseClassCreator< DtGpuCulling, DtInstanceContainer &, const DtCullShaderConfig & > DtGpuCullingCreator
creator
Definition: DtGpuCulling.h:68
The class that is used to communicate the camera viewing parameters to the DtICullingAndLOD interface...
Definition: DtIndirectViewParameters.h:26
std::vector< DtGeometrySpecification > myGeometrySpecifications
c++ buffer that holds the same info
Definition: DtGpuCulling.h:436
std::string myName
Definition: DtGpuCulling.h:500
DtGlBuffer * myGpuGeometrySpecificationsBuffer
The spec buffer containing GeometrySpecifications for all the models/lods of the passed in instances...
Definition: DtGpuCulling.h:440
DtIndirectMaterialVector myMaterials
c++ buffer that holds the same info
Definition: DtGpuCulling.h:453
Vaos are OpenGL context specific. They need to be deleted in the context they are created in...
Definition: DtGlDeleteObjectManager.h:58
friend DtGpuCullingCreator
Definition: DtGpuCulling.h:125
std::unordered_set< DtIndirectModelInfo * > myModelsBeingUsed
Definition: DtGpuCulling.h:508
This class clears buffers using compute shader Theoretically, there are performance gains to be achie...
Definition: DtIndirectClearBufferProgram.h:33
DtGlBuffer * myGpuTexturesBuffer
Definition: DtGpuCulling.h:455
bool myTextureUpdateDirty
Definition: DtGpuCulling.h:491
Contains DLL export macros.
DtDe & myDe
Definition: DtGpuCulling.h:484
DtIndirectGlTextureObjectManager & myIndirectGlTextureObjectManager
Definition: DtGpuCulling.h:514
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer, DtDeCommunicator, as well as a DtDisplay and other things.
Definition: DtDe.h:76
This is a helper class to manage sets of perGeomInstance objects associated with a model instance...
Definition: DtInstanceContainer.h:39
static const int theGpuOutputInstanceBufferIndex
Definition: DtGpuCulling.h:502
Definition: DtFirstTextureStreamedModelAndIndexTracker.h:16
CameraSet myModelIdsToNumInstancesChanged
Definition: DtGpuCulling.h:495
std::unordered_set< DtIndirectGlTextureObject * > myIndirectTexturesBeingUsed
Definition: DtGpuCulling.h:512
Contains makVrv::DtIndirectViewParameters class.
std::set< osg::observer_ptr< const osg::Camera > > CameraSet
All the culling output buffers need to be per camera so shadows passes work.
Definition: DtGpuCulling.h:118
bool myRegistrarValid
Definition: DtGpuCulling.h:426
Class that allows you to abstractly register effect texture types the effect texture types are client...
Definition: DtIndirectEffectTexturesTemplate.h:40
VectorStateIdOffsetAndMaxCommands myStateIdToMaxNumCommands
Definition: DtGpuCulling.h:472
Contains the per Model info which the indirect module needs These objects are owned by the Indirect g...
Definition: DtIndirectModelInfo.h:68
std::vector< Material > DtIndirectMaterialVector
Each model type can have a number of geometries, each with an own material.
Definition: DtMaterial.h:46
int myMaxCommands
Definition: DtGpuCulling.h:112
Contains makVrv::DtModelIdAndStreamedBytesTracker class.
std::vector< StateIdOffsetAndMaxCommands > VectorStateIdOffsetAndMaxCommands
Definition: DtGpuCulling.h:115
structure for holding an offset of a command of a given type (state id) and the number of commands of...
Definition: DtGpuCulling.h:109

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)