Cogs.Core
OctSystem.h
1#pragma once
2
3#include "Systems/ComponentSystem.h"
4
5#include "../Components/OctComponent.h"
6#include "../Renderers/OctAtlas.h"
7
8#include "Rendering/Common.h"
9
10#include "Foundation/Collections/Pool.h"
11
12#include <glm/glm.hpp>
13
14#include <unordered_map>
15#include <set>
16#include <memory.h>
17
18namespace Cogs
19{
20 namespace Core
21 {
22 class CameraComponent;
23 class TransformComponent;
24 struct CameraData;
25
26 namespace Volumetric
27 {
28 struct OctSystem;
29
32 typedef uint64_t BaseBlockKey;
33
34 inline BaseBlockKey createBaseBlockKey(int x, int y, int z)
35 {
36 return uint64_t(uint16_t(z)) << 32 | uint64_t(uint16_t(y)) << 16 | uint64_t(uint16_t(x));
37 }
38
41 {
42 uint32_t timestamp;
43 glm::i16vec3 ix3;
44 std::set<RegionKey> regionKeys;
45 };
46
48 struct NodeBlock
49 {
50 uint64_t ix;
51 uint32_t timestamp;
52 glm::u16vec4 ix4;
53 glm::u16vec3 extentMin;
54 glm::u16vec3 extentMax;
55 union {
56 uint32_t children[8];
57 OctBaseBlock* baseBlock;
58 };
59 };
60
62 {
63 RegionKey regionKey;
64 glm::vec3 min;
65 glm::vec3 max;
66 std::vector<uint64_t> baseBlocks;
67 };
68
69 struct OctData
70 {
71 ~OctData();
72
73 OctComponent* comp;
74
75 std::unordered_map<BaseBlockKey, OctBaseBlock*> baseBlocks;
77
78 std::unordered_map<uint64_t, OctRegionData*> knownRegions;
80
81 std::vector<TileResponse*> tileResponses;
82 std::vector<TileResponse*> tileResponsesStash;
83
84 std::vector<NodeBlock> nodes;
85
86 glm::i16vec3 alignMinToZeroShift;
87
88 std::vector<uint32_t> stack;
89
90 std::vector<uint32_t> front;
91 std::vector<uint32_t> frontTmp;
92
93 uint32_t currentTimestamp = 0;
94
95 float tolerance = 0.1f;
96 unsigned maxFrontSize = 1; // Updated by OctTreeRenderSystem::preUpdate.
97
98 //std::unordered_map<uint64_t, std::set<uint64_t>> baseBlocksByRegion;
99
100 size_t layoutHash = 0;
101
102 bool gpuCacheWipe = true;
103
104 VertexBufferHandle instanceBufferHandle;
105 MaterialInstanceHandle materialInstance;
106
107 OctSource source = OctSource::Value;
108
109 unsigned instanceCount = 0;
110
111 OctAtlas atlas;
112 };
113
115 : public ComponentSystemWithDataPool<OctComponent, OctData>
116 {
117 static const unsigned maxResponses = 5;
118 static const unsigned skirtSize = 1;
119 public:
120 OctSystem(Memory::Allocator * allocator, SizeType capacity) : ComponentSystemWithDataPool(allocator, capacity) {}
121
122 void initialize(Context * context) override;
123 void update(Context * context) override;
124
126
127 static glm::mat4 LocalFromIndexSpaceTransform(const OctComponent& octComp, const OctData& octData);
128 static glm::mat4 IndexSpaceFromLocalTransform(const OctComponent& octComp, const OctData& octData);
129
130 struct OctBounds* bounds = nullptr;
131 struct OctRenderer* renderer = nullptr;
132
133 MaterialHandle material;
134 VariableKey transferTexKey;
135 VariableKey volumeTexKey;
136
137 Cogs::EffectHandle effectHandle;
138 Cogs::InputLayoutHandle inputLayoutHandle;
139
140 void buildTree(Context * context, OctComponent& octComp, OctData& octData);
141
142 void adaptiveSubset(Context * context, OctComponent& octComp, OctData& octData, const TransformComponent& transComp, const CameraComponent& camComp, const CameraData& camData);
143 };
144
145 }
146 }
147}
Context * context
Pointer to the Context instance the system lives in.
void update()
Updates the system state to that of the current frame.
Component system with parallel data per component stored in a pool similar to how the components them...
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Defines a 4x4 transformation matrix for the entity and a global offset for root entities.
Base allocator implementation.
Definition: Allocator.h:30
uint16_t VariableKey
Used to lookup material properties.
Definition: Resources.h:46
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
Pool used to store elements of ElementType.
Definition: Pool.h:17
Handle to a Component instance.
Definition: Component.h:67
Contains data describing a Camera instance and its derived data structured such as matrix data and vi...
Definition: CameraSystem.h:67
Representing a block or node in the oct-tree.
Definition: OctSystem.h:49
uint32_t timestamp
Timestamp of the most recent adding of a region in the tree below.
Definition: OctSystem.h:51
glm::u16vec4 ix4
Oct-tree index, base level is baseblock ix3 minus alignMinToZeroShift.
Definition: OctSystem.h:52
Represent the blocks at the oct-tree base level. Independent on current the particular oct-tree.
Definition: OctSystem.h:41
std::set< RegionKey > regionKeys
Regions intersecting this block.
Definition: OctSystem.h:44
glm::i16vec3 ix3
Index without block shift.
Definition: OctSystem.h:43
uint32_t timestamp
Timestamp last time a region was added to block.
Definition: OctSystem.h:42
glm::i16vec3 alignMinToZeroShift
Shift value for baseBlock ix3 to get them non-negative.
Definition: OctSystem.h:86
std::vector< uint64_t > baseBlocks
Base blocks that intersects with this region.
Definition: OctSystem.h:66
glm::vec3 min
Object space min corner of region bounding box.
Definition: OctSystem.h:64
glm::vec3 max
Object space max corner of region bounding box.
Definition: OctSystem.h:65
void initialize(Context *context) override
Initialize the system.
Definition: OctSystem.cpp:162
ComponentModel::ComponentHandle createComponent() override
Definition: OctSystem.cpp:204