Cogs.Core
UniformGridSystem.h
1#pragma once
2
3#include "EntityDefinition.h"
4#include "Resources/Resources.h"
5#include "Systems/ComponentSystem.h"
6#include "Services/TaskManager.h"
7
8#include "../Components/UniformGridComponent.h"
9
10#include <atomic>
11#include <mutex>
12#include <memory>
13#include <unordered_map>
14
15namespace Cogs::Core::EchoSounder
16{
17 struct UniformGridSampleData;
18 struct UniformGridSystem;
19
21 uint32_t configId = 0;
22 uint32_t coordSys = 0;
23 uint32_t majorCount = 0;
24 uint32_t minorCount = 0;
25 uint32_t sampleCount = 0;
26 float depthOffset = 0.0f;
27 float depthStep = 0.0f;
28 glm::vec3 transducerAlpha;
29 glm::vec3 transducerOffset;
30 std::vector<float> directionX;
31 std::vector<float> directionY;
32 std::vector<float> beamWidthX;
33 std::vector<float> beamWidthY;
34 };
36 uint32_t configId = 0;
37 int64_t timestamp = 0;
38 glm::vec3 position;
39 glm::quat orientation;
41 };
42 typedef uint64_t TileKey;
43 inline TileKey encodeTileKey(glm::uvec3 pos)
44 {
45 return (((TileKey)(pos.x&0xffff))<<0 |
46 ((TileKey)(pos.y&0xffff))<<16 |
47 ((TileKey)(pos.z&0xffff))<<32);
48 }
49 inline glm::uvec3 decodeTileKey(TileKey key)
50 {
51 return glm::uvec3((key>>0)&0xffff, (key>>16)&0xffff, (key>>32)&0xffff);
52 }
53 struct Tile
54 {
55 float *data = nullptr;
56 // TODO: Age
57 uint64_t ping_number = 0;
58 float minValue = 0.0f;
59 float maxValue = 0.0f;
60 bool update = false;
61 bool update_iso = false;
62 EntityPtr visualizer;
63 MeshHandle mesh;
64 };
66 UniformGridSystem *system = nullptr;
67 UniformGridComponent *component = nullptr;
68 std::unordered_map<uint32_t ,std::shared_ptr<UniformGridConfig>> config;
69
70 std::mutex mu;
71 std::condition_variable cv;
72
73 bool shutdown = false;
74 bool change_sv = false;
75 bool update_iso = false;
76
77 std::mutex tiles_mu;
78 std::unordered_map<TileKey, Tile> tiles;
79 std::vector<Entity*> destroy;
80
81 glm::uvec3 size;
82 glm::vec3 scale;
83
84 float decibelMin = 0.0f;
85 float decibelMax = 0.0f;
86
87 UniformGridSampleData *sample_data = nullptr;
88 UniformGridSampleData *sample_data_backbuffer = nullptr;
89
90 // Iso Surfaces
91 ComputeType compute = ComputeType::Reference; // TODO Mutex
92 std::vector<float> thresholds; // TODO Mutex
93 std::vector<MaterialInstanceHandle> layerMaterialInstances;
94 EntityPtr visualizer_group;
95
96 // Outline
97 glm::quat vesselOrientationGlobal;
98 glm::vec3 vesselPositionGlobal;
99 std::unordered_map<uint32_t, EntityPtr> boundingFrustum;
100
101 uint64_t ping_number = 0;
102 uint64_t pings_handled = 0;
103 uint64_t pings_droped = 0;
104
105 uint64_t evict_number = 0;
106
107 unsigned runs = 0;
108 struct {
109 std::atomic<uint32_t> us = 0;
110 std::atomic<uint32_t> N = 0;
111 } sample_intersecting;
112
113 struct {
114 std::atomic<uint32_t> us = 0;
115 std::atomic<uint32_t> N = 0;
116 } sample_inside;
117
118 struct {
119 std::atomic<uint32_t> analyze = 0;
120 std::atomic<uint32_t> vtx = 0;
121 std::atomic<uint32_t> idx = 0;
122 std::atomic<uint32_t> N = 0; // number of non-empty cells.
123 } isoSurf;
124
125 bool update_isosurface();
126 MeshHandle createIsoSurfaces(Tile & tile, Cogs::Memory::MemoryBuffer& scratch, const glm::vec3 minCorner);
127 };
129 UniformGridSampleData(Context *context, UniformGridSystem *system, UniformGridComponent *component, UniformGridData &data, UniformGridPing &ping, uint64_t ping_number);
130 void cull_tiles();
131 void evict_tiles();
132 Tile GenTile();
133 void gen_tiles();
134 void sample_intersecting(size_t start, size_t end);
135 void sample_inside(size_t start, size_t end);
136 void filter_ping();
137 void sample_ref(float *data, const float *v, uint32_t z, uint32_t y, uint32_t x, float r2, glm::vec3 q);
138 void sample_sn90_ref(float *data, const float *v, uint32_t z, uint32_t y, uint32_t x, float r2, glm::vec3 q);
139
140 Context* context;
141 UniformGridSystem *system;
142 UniformGridComponent *component;
143 UniformGridData &data;
144 std::shared_ptr<UniformGridConfig> config;
145
146 TaskId linear_group;
147 ComputeType compute;
148
149 float average_alpha;
150
151 float filter_width;
152 float filter_falloff;
153
154 // Ping Data
155 uint64_t ping_number;
156
157 glm::quat vesselOrientationGlobal;
158 glm::vec3 vesselPositionGlobal;
159 glm::quat arrayOrientationGlobal;
160 glm::vec3 arrayPositionGlobal;
161 glm::quat inverseOrientation;
162
163 float minDistance;
164 float maxDistance;
165 float minDistanceSquared;
166 float maxDistanceSquared;
167
168 glm::vec4 frustum[6]; // TODO 4
169
170 glm::uvec3 maxIndices;
171 glm::vec3 polarShift;
172 glm::vec3 polarScale;
173
174 // Group Data
175 uint32_t majorCount;
176 uint32_t minorCount;
177
178 std::vector<glm::ivec3> tiles_intersecting;
179 std::vector<glm::ivec3> tiles_inside;
180 Memory::MemoryBuffer decibel;
182 };
183 struct UniformGridSystem: public ComponentSystemWithDataPool<UniformGridComponent, UniformGridData>
184 {
185 UniformGridSystem(Memory::Allocator * allocator, SizeType capacity) : ComponentSystemWithDataPool(allocator, capacity) {}
186
187 void initialize(Context * context) override;
188 //void cleanup(Context * context) override;
189
190 void preUpdate(Context * context) override;
191
193 void destroyComponent(ComponentHandle component) override;
194
195 void dispatch(Context *context, UniformGridComponent *component);
196
197 void config(UniformGridComponent *component, UniformGridConfig &config);
198 void clear(UniformGridComponent *component);
199 void addPing(Context * context, UniformGridComponent *component, UniformGridPing &ping);
200
201 void update_outline(Context * context, UniformGridComponent *component);
202
203 bool has_sse41;
204 bool has_avx2;
205
206 MaterialHandle point_material;
207 MaterialInstanceHandle point_instance;
208
209 TaskQueueId Queue;
210
211 // Reduce point visualization count
212 int viz_x_div = 2;
213 int viz_y_div = 2;
214 int viz_z_div = 2;
215 };
216}
Context * context
Pointer to the Context instance the system lives in.
void preUpdate()
Run the pre-update method of the system.
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
Base allocator implementation.
Definition: Allocator.h:30
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
Definition: EntityPtr.h:12
uint16_t TaskQueueId
Unique id for a task queue.
Definition: TaskManager.h:14
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
Handle to a Component instance.
Definition: Component.h:67
void initialize(Context *context) override
Initialize the system.
void destroyComponent(ComponentHandle component) override
Task id struct used to identify unique Task instances.
Definition: TaskManager.h:20