Cogs.Core
TexAtlasSystem.h
1#pragma once
2#include "Systems/ComponentSystem.h"
3#include "TexAtlasComponent.h"
4
5namespace Cogs::Core::TexAtlas
6{
7 using SlotIx = uint16_t;
8 static constexpr SlotIx NoSlotIx = static_cast<SlotIx>(~0u);
9
10 using TreeIx = uint64_t;
11 static constexpr TreeIx NoTreeIx = ~static_cast<TreeIx>(0u);
12
13 struct Cache;
14 struct Geometry;
15
16 struct Layout
17 {
18 glm::ivec2 offset;
19 glm::uvec2 size;
20 uint32_t tileScale;
21 uint32_t level;
22 uint32_t maxLevel;
23 };
24
25 struct Fetcher
26 {
27 struct LoadItem
28 {
29 size_t pathHash = 0;
31 TreeIx treeIx = NoTreeIx;
32 SlotIx slotIx = NoSlotIx;
33 };
34
36 {
37 enum struct Kind
38 {
39 InsertChars,
40 InsertX,
41 InsertY,
42 InsertZ,
43 InsertXMin,
44 InsertYMin,
45 InsertXMax,
46 InsertYMax
47 };
48 Kind kind;
49 std::string_view chars;
50 };
51
52 std::string url;
53 std::vector<UrlBuilderOp> urlBuilder;
54 std::vector<LoadItem> loaded; // Items that has finished loading and can be stored in atlas.
55 std::vector<LoadItem> loading; // Items that are currently loading.
56 std::vector<LoadItem> tmp; // Temp array used when filtering loaditems
57
58 uint32_t currentFrame = 0;
59 uint32_t currentTime = 0;
60 uint32_t timeout = 0;
61 uint32_t issuedThisFrame = 0;
62
63 uint32_t maxQueueSize = 10;
64 uint32_t maxIssuesPerFrame = 3;
65
66 uint32_t tileWidth = 0; // Set by the first tile that is succesfully fetched and will kept fixed thereafter.
67 uint32_t tileHeight = 0;
68
69 glm::dvec2 datasetExtentMin;
70 glm::dvec2 datasetExtentMax;
71
73 void update(Context* context, uint32_t currentFrame, uint32_t currentTime, uint32_t timeout, std::string_view urlTemplate);
74
76 void processLoadQueue(Context* context, Cache& cache);
77
79 bool canFetch() const;
80
82 void issueFetch(Context* context,
83 const glm::uvec3& tileId,
84 TreeIx treeIx,
85 SlotIx slotIx);
86 };
87
88 struct Cache
89 {
90 struct Slot
91 {
92 enum struct State
93 {
94 None,
95 Loading,
96 Failed,
97 Cancelled,
98 Loaded
99 };
100 State state = State::None;
101 TreeIx treePos = NoTreeIx;
102 uint32_t lastTouched = 0;
103 uint32_t stateChangeTime = 0;
104 uint8_t revision = 0;
105 uint8_t retries = 0;
106 };
107
108 uint32_t currentFrame = 0;
109 uint32_t currentTime = 0;
110 std::unordered_map<TreeIx, SlotIx> tree;
111 std::vector<SlotIx> lru;
112 std::vector<Slot> slots;
113
114 uint32_t lruPointer = 0;
115 uint32_t maxItemCount = 0;
116 uint32_t minRetryDelay = 0;
117
118 void setStateNone(Slot& slot) { slot.stateChangeTime = currentTime; slot.state = Slot::State::None; slot.retries = 0; }
119 void setStateLoading(Slot& slot) { slot.stateChangeTime = currentTime; slot.state = Slot::State::Loading; }
120 void setStateFailed(Slot& slot) { slot.stateChangeTime = currentTime; slot.state = Slot::State::Failed; }
121 void setStateCancelled(Slot& slot) { slot.stateChangeTime = currentTime; slot.state = Slot::State::Cancelled; slot.retries = 0; }
122 void setStateLoaded(Slot& slot) { slot.stateChangeTime = currentTime; slot.state = Slot::State::Loaded; slot.retries = 0; }
123
124 void update(uint32_t currentFrame, uint32_t currentTime, uint32_t minRetryDelay, uint32_t maxItemCount);
125
126 SlotIx getOrAllocCacheSlot(Context* context, Fetcher& fetcher, const glm::uvec3& tileId);
127 };
128
129 struct LodTree
130 {
131 struct Tile
132 {
133 float error;
134 int32_t x;
135 int32_t y;
136 uint8_t level;
137 uint16_t encodedIx;
138 SlotIx slotIx;
139 };
140
141 std::vector<Tile> candidates;
142 std::vector<Tile> tiles;
143 std::vector<uint16_t> encoded;
144
145 void update(Context* context, const Geometry& geo, Cache& cache, Fetcher& fetcher, const Layout& layout, float tolerance, bool restrictBetweenNearAndFar, bool lodFreeze);
146 };
147
148 struct Geometry
149 {
150 glm::dvec2 domain[2] = {};
151 glm::vec2 invDomainMapShift = glm::vec2(0.f);
152 glm::vec2 invDomainMapScale = glm::vec2(0.f);
153 glm::vec2 coefficients[4] = {};
154 float elevationMin = 0.f;
155 float elevationMax = 0.f;
156
157 void calcTileCorners(glm::vec4* corners, const LodTree::Tile& tile) const;
158 };
159
161 {
162 MaterialHandle material; // Handle to keep material alive.
163 uint32_t lastSeen = 0;
164 TexAtlasComponent* slots[4] = {};
165 };
166
168 {
169 std::string level;
170 std::string floatCoeffs;
171 std::string intCoeffs;
172 std::string tile;
173 std::string tree;
174 };
175
176}
177
178namespace Cogs::Core
179{
180 struct TexAtlasRenderer;
181
183 {
184 TextureHandle treeTex;
185 size_t treeHashValue = 0;
186 TextureHandle tilesTex;
187 TextureHandle tilesOldTex; // Previous texture when resizing
188
189 TexAtlas::Geometry geometry;
190 TexAtlas::Layout layout;
191 TexAtlas::Fetcher fetcher;
192 TexAtlas::Cache cache;
194
195 glm::mat4 floatCoefficients;
196 glm::ivec4 intCoefficients;
197 int32_t levels = 0;
198
199 bool inUse = false; // Hook for twin-visuals texatlas task to enable this outside of material use
200 };
201
202 struct TexAtlasSystem : public ComponentSystemWithDataPool<TexAtlasComponent, TexAtlasData>
203 {
205
206 TexAtlasSystem(Memory::Allocator* allocator, SizeType capacity) : ComponentSystemWithDataPool(allocator, capacity) {}
207
208 void initialize(Context* context) override;
209
211
212 void destroyComponent(ComponentHandle component) override;
213
214 void update(Context* context) override;
215
216 TexAtlasRenderer* renderer = nullptr;
217
218
219 std::unordered_map<Material*, TexAtlas::TrackedMaterial> trackedMaterials;
220
221 std::vector<TexAtlas::MaterialKeys> materialKeys;
222 };
223
224}
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
Base allocator implementation.
Definition: Allocator.h:30
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
@ Geometry
Store entity vector fields (vector<vec3>, vector<vec2>, vector<int>, vector<float>).
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
COGSFOUNDATION_API Time currentTime()
High resolution clock time (NTP / UTC time). Returns an implementation defined absolute timestamp,...
Handle to a Component instance.
Definition: Component.h:67
static const ResourceHandle_t NoHandle
Handle representing a default (or none if default not present) resource.
void destroyComponent(ComponentHandle component) override
void initialize(Context *context) override
Initialize the system.
ComponentHandle createComponent() override
std::vector< SlotIx > lru
Slots sorted s.t. the least recently used items is at end.
uint32_t maxItemCount
Number of tiles in cache.
uint32_t minRetryDelay
Initial delay for retrying a fetch. For subsequent retries this delay is doubled each time.
uint32_t lruPointer
Points to one past the least recently and recycable item, decrements and zero means nothing to recycl...
void issueFetch(Context *context, const glm::uvec3 &tileId, TreeIx treeIx, SlotIx slotIx)
void update(Context *context, uint32_t currentFrame, uint32_t currentTime, uint32_t timeout, std::string_view urlTemplate)
void processLoadQueue(Context *context, Cache &cache)
glm::vec2 coefficients[4]
Coefficients wrt engine origin.
uint32_t level
Base level.
uint32_t maxLevel
Maximum level.
uint32_t tileScale
1 << level.
glm::uvec2 size
Size of grid.
glm::ivec2 offset
Indices of the grid cell with smallest indices.