VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtBallisticComputeComponent.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2026 MAK Technologies, Inc.
3  * All rights reserved.
4  *****************************************************************************/
5 
9 
10 #pragma once
11 
13 #include <vrvOsg/vrvOsg.h>
14 #include <vrvOsg/DtGpuOperation.h>
15 
18 
19 #include <vrvMath/DtVector3.h>
20 #include <vrvMath/DtVector4.h>
21 #include <vrvMath/DtMatrix4.h>
22 
23 #include <atomic>
24 #include <functional>
25 #include <mutex>
26 #include <string>
27 #include <vector>
28 
29 namespace osg
30 {
31  class RenderInfo;
32  class Camera;
33  class State;
34 }
35 
36 namespace makVrv
37 {
38 #define CPU_SIDE_COMPILATION 1
39 #include <../data/shaders/indirect/DrawElementsIndirectCommand.h>
40 #include <../data/shaders/ballistics/bullet_spawn.h>
41 #include <../data/shaders/ballistics/observer.h>
42 #include <../data/shaders/ballistics/query.h>
43 #include <../data/shaders/ballistics/tracer_definition_buffer.h>
44 #undef CPU_SIDE_COMPILATION
45 
46  class DtDe;
47  class DtGlBuffer;
48  class DtGlProgram;
49  class DtBallisticGeometry;
50  class DtIndirectShaderManager;
51  class DtBallisticDrawComponent;
52  class DtBallisticRunTimeSettings;
53  class DtTracerDefinition;
54  class DtOsgBallisticManager;
55 
58 
61 
63  template <>
64  inline const char* creatorTypeName<DtBallisticComputeComponent>(void) { return "DtBallisticComputeComponentCreator"; }
65 
72  {
73  public:
75  enum class CullMode
76  {
77  Octree,
78  BruteForce
79  };
80 
82  enum class QueryMode
83  {
84  Octree,
85  BruteForce
86  };
87 
88  public:
89  // Configuration
90 
92  static constexpr uint32_t MAX_BULLETS = 16384;
93 
98  virtual std::string buildShaderPreamble() const;
99 
101  using SlotReleasedCallback = std::function<void(int slotIndex)>;
102 
103  public:
105 
106  protected:
111 
112  private:
114  DtBallisticComputeComponent() = delete;
115 
118 
120  DtBallisticComputeComponent& operator=(const DtBallisticComputeComponent& rhs) = delete;
121 
122  public:
124  virtual ~DtBallisticComputeComponent();
125 
127  virtual void slot_reloadShaders();
128 
132  virtual void slot_windSpeedChanged(float metersPerSecond);
133 
137  virtual void slot_windDirectionChanged(float radians);
138 
140  virtual void execute();
141 
143  virtual void update(osg::RenderInfo& renderInfo);
144 
146  virtual void apply(osg::RenderInfo& renderInfo);
147 
149  virtual void releaseGLObjects(osg::State* state);
150 
152  virtual bool shouldDraw(bool isShadowPass);
153 
159  virtual void drainReleasedSlots(SlotReleasedCallback callback);
160 
166  virtual void setSimulationActive(bool active);
167 
171  virtual void beginFrame();
172 
176  virtual void insertFrameFence();
177 
182  virtual void pollReadback();
183 
187  virtual void pollQueryReadback();
188 
193  virtual void writeSpawnData(int slotIndex, const BulletSpawn& spawn);
194 
206  virtual uint32_t registerTracerDefinition(const DtTracerDefinition& def, const float otherData0[4]);
207 
215  virtual void unregisterTracerDefinition(uint32_t index);
216 
221  virtual void setSlotActive(int slotIndex, uint32_t active);
222 
229  virtual void deactivateSlot(int slotIndex);
230 
238  virtual void updateEnvironment(float gravity, float windSpeed, float windDirection,
239  float deltaTime, double currentTime);
240 
244  virtual void dispatchTrajectoryUpdate();
245 
247  virtual void dispatchOctreeBuild();
248 
252  virtual void dispatchSoundQuery(DtOsgBallisticManager* mgr);
253 
258  virtual void bindGlobalCullBuffers();
259 
263  virtual void bindGlobalDrawBuffers();
264 
272  virtual void drainSoundTriggers(
273  std::function<void(const std::vector<std::vector<DtBallisticSoundTrigger>>&)> callback);
274 
276  virtual void unbindCullBuffers();
277 
282  virtual void unbindDrawBuffers();
283 
289  virtual void initIndirectDrawCommands(GLuint indexCount);
290 
292  virtual void setFrustumCullMode(CullMode mode);
293 
295  virtual CullMode frustumCullMode() const;
296 
298  virtual void setProximityQueryMode(QueryMode mode);
299 
301  virtual QueryMode proximityQueryMode() const;
302 
304  virtual void setGravity(float gravity);
305 
307  virtual float gravity() const;
308 
316  virtual void setMaxQueryResults(int n);
317 
322  virtual void dispatchFrustumCull();
323 
328  virtual void dispatchOctreeCull();
329 
331  virtual void dispatchGatherActiveFlags();
332 
336  virtual void bindProjectileBuffer(unsigned int bindingPoint);
337 
339  virtual DtBallisticDrawComponent* drawComponent() const;
340 
344  virtual void debugDumpBuffers();
345 
353  virtual void debugDrawCullResults(DtGlBuffer* visibleSSBO);
354 
364  virtual void debugDrawPositions();
365 
371  virtual void debugDumpCullBuffers(DtGlBuffer* visibleSSBO);
376  virtual void initializeBuffers();
377 
379  virtual void doReloadShaders();
380 
381  protected:
384  virtual void recomputeOctreeConstants();
385 
386  protected:
392  float myGravity;
394  float myWindSpeed;
397 
399  uint32_t myOctreeDepth;
400 
401  // Derived octree constants -- recomputed by recomputeOctreeConstants().
403  uint32_t myLeafCount;
410 
413 
416 
417  // Compute shader programs
438 
443 
444  // GPU buffers
463 
465  DtGlBuffer* myTracerDefinitionSSBO = nullptr;
466 
468  std::vector<TracerDefinition> myTracerDefTable;
469 
471  std::vector<int32_t> myTracerDefRefCounts;
472 
474  std::vector<uint32_t> myFreeTracerSlots;
475 
476  // Triple-buffer readback
478  DtGlBuffer* myReadbackBuffer[3];
480  GLsync myFenceSync[3];
483 
489  std::vector<uint64_t> myLifeTracker;
490 
493 
498  uint64_t myReadbackFrameNumber[3];
499 
501  std::vector<int> myPendingReleasedSlots;
503 
505  DtGlBuffer* myQueryInputSSBO[3];
507  DtGlBuffer* myQueryResultSSBO[3];
509  DtGlBuffer* myQueryReadbackSSBO[3];
511  GLsync myQueryFence[3];
520  std::vector<std::vector<DtBallisticSoundTrigger>> myPendingSoundTriggers;
523 
525  std::atomic<bool> myPendingShaderReload;
526 
529  std::atomic<bool> mySimulationActive;
530 
537 
540 
543 
546 
549 
552  };
553 }
bool myMutableShadersFailedToCompile
True if the most recent shader compilation failed.
Definition: DtBallisticComputeComponent.h:415
This manages the draw components (shader vp wrappers) for combinations of DtIndirectEffectTexturesTem...
Definition: DtIndirectShaderManager.h:50
int myFrameIndex
Current triple-buffer frame slot (cycles 0, 1, 2).
Definition: DtBallisticComputeComponent.h:482
float myWindDirection
Current wind direction (radians, clockwise from north).
Definition: DtBallisticComputeComponent.h:396
std::vector< TracerDefinition > myTracerDefTable
CPU-side table of registered tracer types; mirrored to myTracerDefinitionSSBO each frame...
Definition: DtBallisticComputeComponent.h:468
DtGlBuffer * myBoundsSSBO
BoundsBuffer SSBO (world AABB for octree root).
Definition: DtBallisticComputeComponent.h:456
DtBallisticDrawComponent * mySharedDrawComponent
Shared draw component; all ballistic geometries reference this single instance.
Definition: DtBallisticComputeComponent.h:548
GLuint myMeshIndexCount
Mesh index count set by initIndirectDrawCommands(); cached for reference.
Definition: DtBallisticComputeComponent.h:551
Abstract base for any operation that runs on the GPU via DtGpuArena.
Definition: DtGpuOperation.h:30
DtGlProgram * myOctreeMarkProgram
Compute program for octree internal-node frustum mark pass.
Definition: DtBallisticComputeComponent.h:431
float myGravity
Current gravity magnitude (m/s^2).
Definition: DtBallisticComputeComponent.h:392
#define DT_DLL_VRVOSG
Definition: vrvOsg.h:23
DtGlProgram * myOctreeLeafProgram
Compute program for octree leaf frustum cull pass.
Definition: DtBallisticComputeComponent.h:433
std::function< void(int slotIndex)> SlotReleasedCallback
Callback type invoked on the CPU thread for each pool slot the GPU has killed.
Definition: DtBallisticComputeComponent.h:101
Visual and physical appearance parameters for a single tracer round type.
Definition: DtTracerDefinition.h:32
Plain data struct for cross-thread transfer of a ballistic flyby sound trigger.Written by the render ...
std::vector< uint32_t > myFreeTracerSlots
Indices of free slots in myTracerDefTable (reference count == 0), available for reuse.
Definition: DtBallisticComputeComponent.h:474
unsigned int GLuint
Definition: DtGlTextureBufferObject.h:18
DtGlProgram * myOctreeBoundsProgram
Compute program for octree bounds calculation.
Definition: DtBallisticComputeComponent.h:421
DtGlProgram * myTrajectoryProgram
Compute program for trajectory integration.
Definition: DtBallisticComputeComponent.h:419
uint32_t myLeafCount
8^myOctreeDepth leaf nodes.
Definition: DtBallisticComputeComponent.h:403
double myLastFrameTime
Simulation time of the previous execute() call, used to compute deltaTime.
Definition: DtBallisticComputeComponent.h:539
DtGlProgram * myOctreeStructureProgram
Compute program for octree structure build.
Definition: DtBallisticComputeComponent.h:423
4 dimensional vector
DtGlProgram * myProximityQueryOctreeProgram
Compute program for octree-accelerated proximity queries.
Definition: DtBallisticComputeComponent.h:437
CullMode
Selects whether octree or brute-force frustum culling is used.
Definition: DtBallisticComputeComponent.h:75
std::mutex myPendingSoundTriggersMutex
Guards myPendingSoundTriggers for render/sim cross-thread access.
Definition: DtBallisticComputeComponent.h:522
std::atomic< bool > mySimulationActive
True when the manager has active bullets or pending burst rounds. Written by the sim thread via setSi...
Definition: DtBallisticComputeComponent.h:529
A convenience class to represent an OpenGL Shader Program.
Definition: DtGlProgram.h:66
float myWindSpeed
Current wind speed (m/s).
Definition: DtBallisticComputeComponent.h:394
DtGlProgram * myProximityQueryProgram
Compute program for brute-force proximity queries.
Definition: DtBallisticComputeComponent.h:435
3 dimensional vector
DtGlProgram * myActiveFlagProgram
Compute program for gathering active-flags.
Definition: DtBallisticComputeComponent.h:427
Contains the DtGpuOperation class declaration.
Variadic templated creator class for defining a DtVirtualBaseClass creator that is automatically regi...
Definition: DtVirtualBaseClassCreator.h:23
DtSignalConnectionManager myConnections
Manages signal connections for wind change notifications.
Definition: DtBallisticComputeComponent.h:390
DtGlBuffer * myEnvironmentUBO
BallisticEnvironment UBO (gravity, wind, deltaTime, currentTime).
Definition: DtBallisticComputeComponent.h:446
QueryMode
Selects whether octree or brute-force proximity queries are used.
Definition: DtBallisticComputeComponent.h:82
DtGlProgram * myFrustumCullProgram
Compute program for brute-force frustum cull.
Definition: DtBallisticComputeComponent.h:429
DtGlProgram * myOctreeInsertProgram
Compute program for octree bullet insertion.
Definition: DtBallisticComputeComponent.h:425
const char * creatorTypeName< DtBallisticComputeComponent >(void)
template specialization for the creator (pass the class being created to the creatorTypeName template...
Definition: DtBallisticComputeComponent.h:64
DtDe & myDe
Reference to the simulation environment.
Definition: DtBallisticComputeComponent.h:388
DtGlProgram * myQuerySortProgram
Sort shader (sort_query_results.cs.glsl).
Definition: DtBallisticComputeComponent.h:513
const DtBallisticRunTimeSettings & myBallisticRunTimeSettings
Reference to the shared ballistic runtime settings.
Definition: DtBallisticComputeComponent.h:412
bool myBuffersInitialized
True after initializeBuffers() has completed; reset by releaseGLObjects().
Definition: DtBallisticComputeComponent.h:545
uint32_t myOctreeDepth
Current octree depth; all derived octree constants are recomputed when this changes.
Definition: DtBallisticComputeComponent.h:399
DtGlBuffer * myOctreeNodeFlagsSSBO
OctreeNodeFlags SSBO (per-node frustum visibility from the mark pass).
Definition: DtBallisticComputeComponent.h:462
std::mutex myPendingReleasedSlotsMutex
Guards myPendingReleasedSlots for cross-thread access.
Definition: DtBallisticComputeComponent.h:502
Class to manage disconnection of boost connections on deletion.
Definition: DtSignalConnectionManager.h:20
uint32_t myOctreeTotalNodes
Total nodes in the uniform octree (breadth-first).
Definition: DtBallisticComputeComponent.h:405
std::vector< int > myPendingReleasedSlots
Slot indices confirmed dead by GPU readback, pending CPU-thread processing.
Definition: DtBallisticComputeComponent.h:501
Interface for the ballistic rendering draw pass.
Definition: DtBallisticDrawComponent.h:52
Interface for GPU compute operations driving the ballistic simulation pipeline.
Definition: DtBallisticComputeComponent.h:71
DtGlBuffer * myNodeCounterSSBO
NodeCounter SSBO (total node and bullet-list counts).
Definition: DtBallisticComputeComponent.h:458
uint32_t myInternalNodeCount
Internal (non-leaf) node count; used to size the node-flags SSBO.
Definition: DtBallisticComputeComponent.h:407
std::vector< uint64_t > myLifeTracker
Per-slot CPU liveness tracker; 0 = free, non-zero = frame number when spawned.
Definition: DtBallisticComputeComponent.h:489
A generic OpenGL buffer. This can be: -&gt; a vertex buffer -&gt; an index buffer -&gt; a shader storage buffe...
Definition: DtGlBuffer.h:29
std::vector< std::vector< DtBallisticSoundTrigger > > myPendingSoundTriggers
Pending raw sound trigger frame batches enqueued by pollQueryReadback(); drained each tick...
Definition: DtBallisticComputeComponent.h:520
DtGlBuffer * myOctreeBulletListSSBO
OctreeBulletLists SSBO (per-leaf bullet index arrays).
Definition: DtBallisticComputeComponent.h:454
Base class to provide boost signal/slot management.
uint64_t myGlobalFrameCount
Monotonically increasing frame counter, incremented once per execute().
Definition: DtBallisticComputeComponent.h:492
Singleton holding runtime-tweakable settings for the ballistic system.
Definition: DtBallisticRunTimeSettings.h:43
const char int mode
Definition: ioapi.h:38
QueryMode myQueryMode
Active proximity query algorithm.
Definition: DtBallisticComputeComponent.h:442
Contains DLL export macros.
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
CullMode myCullMode
Active frustum cull algorithm.
Definition: DtBallisticComputeComponent.h:440
int myMaxQueryResults
Maximum result entries to read back per frame; immutable after init.
Definition: DtBallisticComputeComponent.h:515
Ballistic manager that owns the projectile pool, compute component, and spatial grid.
Definition: DtOsgBallisticManager.h:58
DtGlBuffer * myActiveFlagSSBO
Dense active-flag output buffer used for CPU readback.
Definition: DtBallisticComputeComponent.h:460
friend DtBallisticComputeComponentCreator
Definition: DtBallisticComputeComponent.h:104
DtGlBuffer * mySpawnSSBO
SpawnBuffer SSBO (immutable per-bullet spawn data).
Definition: DtBallisticComputeComponent.h:448
DtIndirectShaderManager & myIndirectShaderManager
Shared indirect shader manager for loading/caching compute shaders.
Definition: DtBallisticComputeComponent.h:542
std::atomic< bool > myPendingShaderReload
Set from any thread by slot_reloadShaders(); consumed on the GL thread at the top of execute()...
Definition: DtBallisticComputeComponent.h:525
int myActiveSlotCount
Number of slots currently non-zero in myLifeTracker.
Definition: DtBallisticComputeComponent.h:536
std::vector< int32_t > myTracerDefRefCounts
Reference count per entry in myTracerDefTable; zero means the slot is free and available for reuse...
Definition: DtBallisticComputeComponent.h:471
uint32_t myBulletsPerLeaf
Bullet slots per leaf = MAX_BULLETS / myLeafCount.
Definition: DtBallisticComputeComponent.h:409
4 dimensional matrix
DtGlBuffer * myStateSSBO
StateBuffer SSBO (GPU-updated per-bullet state).
Definition: DtBallisticComputeComponent.h:450
DtGlBuffer * myOctreeSSBO
OctreeBuffer SSBO (octree node array).
Definition: DtBallisticComputeComponent.h:452
DtVirtualBaseClassCreator< DtBallisticComputeComponent > DtBallisticComputeComponentCreator
creator
Definition: DtBallisticComputeComponent.h:57
bool myWarnedSoundProgramNull
One-shot warning flag for dispatchSoundQuery() early-return conditions.
Definition: DtBallisticComputeComponent.h:517

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)