Cogs.Core
Image360.h
1#pragma once
2#include "Resources/Resources.h"
3#include "Resources/DataFetcherManager.h"
4
5namespace Cogs::Core
6{
7 class Context;
8 struct Image360Component;
9}
10
11namespace Cogs::Core::Image360
12{
13 typedef int16_t SlotIx;
14
15 // Encoded slot index:
16 // - Value >= 0 => index in cache with tile data
17 // - Value == -1 => tile without data
18 // - Value < -1 => -Value is the offset to the 4 slots that contain the children
19 // If less than
20
21 typedef int16_t EncodedSlotIx;
22 typedef size_t MapIx;
23 struct Cache;
24
25
26 struct Config {
27
28 struct Channel {
29 enum struct DataType {
30 None,
32 SRGB8_PNG,
34 U16,
35 U16_ZST
36 };
37 std::string prefix;
38 DataType dataType = DataType::None;
39 float scale = 1.f;
40 float bias = 0.f;
41 };
42
43 std::string rootPath;
44 uint32_t instanceId = 0;
45 uint32_t treeDepth = 0;
46 uint32_t baseSize = 0;
47 uint32_t noDataDepth = 0;
48 float extent = 1.f;
49 uint8_t valueChannel = uint8_t(0);
50 uint8_t depthChannel = static_cast<uint8_t>(~0u);
51 std::vector<Channel> channels;
52
53 // limits
54 uint32_t cacheMaxCount = 0;
55 uint32_t treeMaxSize = 0;
56 uint32_t maxConcurrent = 0;
57
58 bool hasDepth = false;
59 bool discardNoDepth = true;
60
61 bool parseConfigJson(Cogs::FileContents* contents);
62 };
63
64
65 struct Fetcher
66 {
67
68 struct LoadItem
69 {
71 std::unique_ptr<Memory::MemoryBuffer> buffer;
72 DataFetcherManager::FetchId fetchId = DataFetcherManager::NoFetchId;
73 MapIx mapIx = ~0u;
74 SlotIx slotIx = -2;
75 uint8_t channelIx = 0;
76 uint8_t revision = 0;
77 bool failed = false;
78 };
79
80 std::vector<LoadItem> itemsLoading;
81 std::vector<LoadItem> itemsUploading;
82
83 bool canLoadAnyItems(const Config& config) const;
84
85 void issueChannelFetch(Context* context,
86 const Image360Component& im360Comp,
87 const Config& config,
88 const MapIx mapIx,
89 const SlotIx slotIx,
90 const uint8_t channelIx,
91 const uint8_t revision,
92 const size_t cacheLevel,
93 const size_t cacheLevelIndex);
94
95 void processLoadItems(Context* context,
96 const Image360Component& im360Comp,
97 Cache& cache,
98 const Config& config,
99 const uint32_t currentFrame);
100
101 private:
102 std::vector<LoadItem> itemsLoadingNext;
103 };
104
105
106 struct Cache {
107
108 struct Item
109 {
110 enum struct State {
111 None,
112 Loading,
113 Failed,
114 Loaded,
116 };
117
118 struct {
119 State state = State::None;
120 } value, depth;
121 MapIx mapIx = ~0u;
122 uint32_t lastTouched = 0;
123 uint8_t revision = 0;
124 };
125
126 std::vector<SlotIx> map;
127 std::vector<SlotIx> lru;
128 std::vector<Item> items;
129 size_t lruPointer = 0;
130 size_t maxItemCount = 512;
131
132 void init(const Config& config);
133
134 SlotIx isQuadInCache(Context* context,
135 Fetcher& fetcher,
136 const Image360Component& im360Comp,
137 const Config& config,
138 const uint32_t currentFrame,
139 const size_t cacheLevel,
140 const size_t cacheLevelIndex);
141
142 void updateLeastRecentlyUsedList(const uint32_t currentFrame);
143
144 };
145
147 {
148 uint32_t cols = 0;
149 uint32_t rows = 0;
150 uint32_t layers = 0;
151 };
152
154 {
155 glm::mat3 worldFromLocal; // Camera position must be at image origin, so we don't track position
156 glm::mat3 localFromWorld;
157 Cogs::BufferHandle constants;
158 Cogs::TextureHandle encodedTreesTex;
159 struct {
160 Cogs::TextureHandle tilesTex;
161 float scale = 1.f;
162 float bias = 0.f;
163 uint32_t sampler = 0;
164 } value;
165 struct {
166 Cogs::TextureHandle tilesTex;
167 std::vector<std::unique_ptr<Memory::MemoryBuffer>> tilesData;
168 float scale = 1.f;
169 float bias = 0.f;
170 uint32_t sampler = 0;
171 } depth;
172 CacheLayout cacheLayout;
173 uint32_t baseSize = 0;
176 uint32_t currentTreeSize = 0;
177 uint32_t noDataDepth = 0;
178 bool discardNoDepth = true;
179 };
180
181
183 {
184 struct Quad
185 {
186 glm::vec3 o;
187 glm::vec3 u;
188 glm::vec3 v;
189 SlotIx slot = -1;
190 size_t cacheLevelIndex = 0;
191 size_t encodedTreeIndex = 0;
192 size_t level = 0;
193 };
194
195 std::vector<Image360::EncodedSlotIx> data;
196
197 void update(Context* context,
198 RendererExtensionData& rendererData,
199 Fetcher& fetcher,
200 Cache& cache,
201 const Image360Component& im360Comp,
202 const Config& config,
203 const uint32_t currentFrame,
204 const Image360Component& comp);
205
206 private:
207 std::vector<Quad> quads, quadsNext;
208 };
209
210
211}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
@ Resident
The resource is loaded onto the GPU.
size_t maxItemCount
Number of tiles in cache.
Definition: Image360.h:130
size_t lruPointer
Points to one past the least recently and recycable item, decrements and zero means nothing to recycl...
Definition: Image360.h:129
std::vector< SlotIx > lru
Slots sorted s.t. the least recently used items is at end.
Definition: Image360.h:127
@ SRGB8_JPEG
8-bit colors in SRGB color space stored as JPEG (.jpg).
@ U16_ZST
16-bit unsigned values stored as little endian raw values that are subsequently zstd compressed.
@ SRGBA8_PNG
8-bit colors in SRGB color space and a alpha channel (zero is transparent) stored as PNG (....
@ SRGB8_PNG
8-bit colors in SRGB color space stored as PNG (.png).
@ U16
16-bit unsigned values stored as little endian raw values.
uint32_t noDataDepth
Depth value that corresponds to noData.
Definition: Image360.h:47
uint32_t instanceId
Component instance id.
Definition: Image360.h:44
uint32_t baseSize
Base image size of a cached tile. From json.
Definition: Image360.h:46
uint8_t valueChannel
Data channel to be used as value data. From component.
Definition: Image360.h:49
uint32_t treeDepth
Depth of tile hierarchy. From json.
Definition: Image360.h:45
uint8_t depthChannel
Data channel that contains depth data. From json.
Definition: Image360.h:50
std::vector< Channel > channels
Data channels to use. From json.
Definition: Image360.h:51
bool discardNoDepth
Discard pixels with no depth data.
Definition: Image360.h:59
bool hasDepth
If data has depth and component wants depth.
Definition: Image360.h:58
float extent
Size in model space units, used for bounds queries.
Definition: Image360.h:48
std::vector< Quad > quadsNext
Scratch buffers kept here to avoid excessive reallocations.
Definition: Image360.h:207
std::vector< LoadItem > itemsLoadingNext
Scratch buffer.
Definition: Image360.h:102
uint32_t gpuAllocatedCacheItemCount
Number of tiles allocated in current GPU cache texture.
Definition: Image360.h:175
uint32_t gpuActiveCacheItemCount
Number of tiles in current GPU cache that contain data.
Definition: Image360.h:174
static const ResourceHandle_t NoHandle
Handle representing a default (or none if default not present) resource.
Abstract base class storing data read from a file.
Definition: FileContents.h:20