Nebula
Loading...
Searching...
No Matches
resourceloader.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
34//------------------------------------------------------------------------------
35#include "ids/id.h"
36#include "util/stringatom.h"
37#include "io/stream.h"
38#include "util/set.h"
39#include "resource.h"
40#include "threading/safequeue.h"
41#include "threading/threadid.h"
42#include "ids/idpool.h"
43#include <tuple>
44#include <functional>
45
46namespace Resources
47{
48
50{
51 None = 0x0,
52 Create = 0x1,
53 Update = 0x2
54};
56
57class Resource;
60{
62
63public:
67 virtual ~ResourceLoader();
68
70 virtual void Setup();
72 virtual void Discard();
73
75 virtual void LoadFallbackResources();
76
78 Resources::ResourceId CreateResource(const Resources::ResourceName& res, const void* loadInfo, SizeT loadInfoSize, const Util::StringAtom& tag, std::function<void(const Resources::ResourceId)> success, std::function<void(const Resources::ResourceId)> failed, bool immediate, bool stream);
80 void DiscardResource(const Resources::ResourceId id);
82 void DiscardByTag(const Util::StringAtom& tag);
84 void CreateListener(const Resources::ResourceId res, std::function<void(const Resources::ResourceId)> success, std::function<void(const Resources::ResourceId)> failed);
85
87 const Resources::ResourceName& GetName(const Resources::ResourceId id) const;
89 const uint32_t GetUsage(const Resources::ResourceId id) const;
91 const Util::StringAtom GetTag(const Resources::ResourceId id) const;
93 const Resource::State GetState(const Resources::ResourceId id) const;
95 const Resources::ResourceId GetId(const Resources::ResourceName& name) const;
99 const bool HasResource(const Resources::ResourceId id) const;
100
102 const int32_t& GetUniqueId() const;
103
105 void ReloadResource(const Resources::ResourceName& res, std::function<void(const Resources::ResourceId)> success, std::function<void(const Resources::ResourceId)> failed);
107 void ReloadResource(const Resources::ResourceId& id, std::function<void(const Resources::ResourceId)> success, std::function<void(const Resources::ResourceId)> failed);
108
110 void SetMinLod(const Resources::ResourceId& id, const float lod, bool immediate);
111
126
128 {
129 void* data;
131 };
132
134 {
136 void* data;
137 };
138
140 {
143
145 : id(InvalidResourceUnknownId)
146 {
147 }
148 };
149
155
162
164 {
168 ResourceId id;
173 float lod;
176
178 {
179 ResourceLoadJob job;
180 job.name = loader->names[load.entry].Value();
181 job.state = loader->states[load.entry];
182 job.id = loader->resources[load.entry];
183 job.loadState = loader->loadStates[load.entry];
184 job.streamData = loader->streamDatas[load.entry];
185 job.metadata = loader->metaData[load.entry];
186 job.tag = load.tag.Value();
187 job.immediate = load.immediate;
188 job.lod = load.lod;
190 job.flags = load.flags;
191 return job;
192 };
193 };
194
196 {
200 ResourceId id;
202
204 {
205 loader->streamDatas[id.loaderInstanceId] = this->streamData;
206 loader->loadStates[id.loaderInstanceId] = this->loadState;
207 loader->states[id.loaderInstanceId] = this->state;
208 loader->resources[id.loaderInstanceId] = this->id;
209 }
210 };
211
212
213protected:
214 friend class ResourceServer;
215
216 friend void ApplyLoadOutput(ResourceLoader* loader, const ResourceLoader::ResourceLoadOutput& output);
217 friend void DispatchJob(ResourceLoader* loader, const ResourceLoader::ResourceLoadJob& job);
219
221 virtual void UpdateLoaderSyncState();
222
225 {
226 Resources::ResourceId id;
227 float lod;
229
230 _PendingStreamLod() : id(ResourceId::Invalid()) {};
231 };
232
234 {
235 Resources::ResourceId resourceId;
236 };
237
240 {
241 std::function<void(const Resources::ResourceId)> success;
242 std::function<void(const Resources::ResourceId)> failed;
243 };
244
245
246 static const uint32_t ResourceIndexGrow = 512;
247
253 virtual Resource::State ReloadFromStream(const Resources::ResourceId id, const Ptr<IO::Stream>& stream);
254
256 virtual uint LodMask(const _StreamData& stream, float lod, bool async) const;
258 virtual void RequestLOD(const Ids::Id32 entry, float lod) const;
259
261 virtual void Unload(const Resources::ResourceId id) = 0;
263 virtual void Update(IndexT frameIndex);
264
266 void SetupIdFromEntry(const Ids::Id32 entry, ResourceId& cacheEntry);
267
269 void RunCallbacks(Resource::State status, const Resources::ResourceId id);
270
272 void EnqueueJob(const std::function<void()>& func);
273
280
282 Resources::ResourceId GetPlaceholder(const Resources::ResourceName& name);
283
287
288 Resources::ResourceId placeholderResourceId;
289 Resources::ResourceId failResourceId;
290
291 bool async;
292
294 std::function<void()> preJobFunc;
295 std::function<void()> postJobFunc;
297
302
305
308
320
322 int32_t uniqueId;
323
327};
328
329
330//------------------------------------------------------------------------------
333inline const Resources::ResourceName&
334ResourceLoader::GetName(const Resources::ResourceId id) const
335{
336 return this->names[id.loaderInstanceId];
337}
338
339//------------------------------------------------------------------------------
342inline const uint32_t
343ResourceLoader::GetUsage(const Resources::ResourceId id) const
344{
345 return this->usage[id.loaderInstanceId];
346}
347
348//------------------------------------------------------------------------------
351inline const Util::StringAtom
352ResourceLoader::GetTag(const Resources::ResourceId id) const
353{
354 return this->tags[id.loaderInstanceId];
355}
356
357//------------------------------------------------------------------------------
361ResourceLoader::GetState(const Resources::ResourceId id) const
362{
363 return this->states[id.loaderInstanceId];
364}
365
366//------------------------------------------------------------------------------
369inline const Resources::ResourceId
371{
372 IndexT i = this->ids.FindIndex(name);
373 if (i == InvalidIndex) return Resources::ResourceId::Invalid();
374 else return this->resources[this->ids.ValueAtIndex(i)];
375}
376
377//------------------------------------------------------------------------------
382{
383 return this->ids;
384}
385
386//------------------------------------------------------------------------------
389inline const bool
390ResourceLoader::HasResource(const Resources::ResourceId id) const
391{
392 return this->names.Size() > (SizeT)id.loaderInstanceId;
393}
394
395//------------------------------------------------------------------------------
398inline const int32_t&
400{
401 return this->uniqueId;
402}
403
404} // namespace Resources
The common base class of Nebula.
Definition refcounted.h:38
Definition idpool.h:29
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
Definition resource.h:22
State
Definition resource.h:26
Definition resourceloader.h:60
Ptr< ResourceLoaderThread > streamerThread
Definition resourceloader.h:293
Util::FixedArray< ResourceId > resources
Definition resourceloader.h:314
Util::FixedArray< Resources::ResourceName > names
Definition resourceloader.h:309
void EnqueueJob(const std::function< void()> &func)
Issue async job.
Definition resourceloader.cc:327
Util::Array< _PendingResourceUnload > pendingUnloads
Definition resourceloader.h:299
Util::Dictionary< Resources::ResourceName, uint32_t > ids
Definition resourceloader.h:306
__DeclareAbstractClass(ResourceLoader)
Util::FixedArray< Util::StringAtom > tags
Definition resourceloader.h:311
friend ResourceLoadOutput _LoadInternal(ResourceLoader *loader, ResourceLoadJob res)
Definition resourceloader.cc:346
std::function< void()> postJobFunc
Definition resourceloader.h:295
virtual void Setup()
setup resource loader, initiates the placeholder and error resources if valid, so don't forget to run...
Definition resourceloader.cc:42
Resources::ResourceId placeholderResourceId
Definition resourceloader.h:288
Util::StringAtom placeholderResourceName
these types need to be properly initiated in a subclass Setup function
Definition resourceloader.h:285
virtual Resource::State ReloadFromStream(const Resources::ResourceId id, const Ptr< IO::Stream > &stream)
perform a reload
Definition resourceloader.cc:117
Util::StringAtom streamerThreadName
Definition resourceloader.h:296
void DiscardByTag(const Util::StringAtom &tag)
discard all resources associated with a tag
Definition resourceloader.cc:651
void DiscardResource(const Resources::ResourceId id)
discard container
Util::FixedArray< _StreamData > streamDatas
Definition resourceloader.h:318
void RunCallbacks(Resource::State status, const Resources::ResourceId id)
run callbacks
Definition resourceloader.cc:307
Util::Array< ResourceLoadJob > dependentJobs
Definition resourceloader.h:304
uint32_t uniqueResourceId
Definition resourceloader.h:319
Threading::SafeQueue< _PendingStreamLod > pendingStreamQueue
Definition resourceloader.h:301
const Util::StringAtom GetTag(const Resources::ResourceId id) const
get resource tag was first registered with
Definition resourceloader.h:352
bool async
Definition resourceloader.h:291
int32_t uniqueId
id in resource manager
Definition resourceloader.h:322
ResourceLoader()
constructor
Definition resourceloader.cc:19
Util::FixedArray< Resource::State > states
Definition resourceloader.h:312
static const uint32_t ResourceIndexGrow
Definition resourceloader.h:246
const Util::Dictionary< Resources::ResourceName, Ids::Id32 > & GetResources() const
get the dictionary of all resource-id pairs
Definition resourceloader.h:381
virtual void RequestLOD(const Ids::Id32 entry, float lod) const
Set lod factor for resource.
Definition resourceloader.cc:136
Threading::SafeQueue< ResourceLoadOutput > loadOutputs
Definition resourceloader.h:303
Resources::ResourceId GetPlaceholder(const Resources::ResourceName &name)
get placeholder based on resource name
Definition resourceloader.cc:336
void CreateListener(const Resources::ResourceId res, std::function< void(const Resources::ResourceId)> success, std::function< void(const Resources::ResourceId)> failed)
Create new listener on resource.
Definition resourceloader.cc:670
Util::Array< IndexT > pendingLoads
Definition resourceloader.h:298
Util::FixedArray< LoadState > loadStates
Definition resourceloader.h:313
const int32_t & GetUniqueId() const
get the global identifier for this pool
Definition resourceloader.h:399
Threading::ThreadId creatorThread
Definition resourceloader.h:326
Resources::ResourceId failResourceId
Definition resourceloader.h:289
virtual ResourceInitOutput InitializeResource(const ResourceLoadJob &job, const Ptr< IO::Stream > &stream)=0
Initialize and create the resource, optionally load if no subresource management is necessary.
virtual void UpdateLoaderSyncState()
Update loader internal state.
Definition resourceloader.cc:98
friend void DispatchJob(ResourceLoader *loader, const ResourceLoader::ResourceLoadJob &job)
Definition resourceloader.cc:158
virtual void LoadFallbackResources()
load placeholder and error resources
Definition resourceloader.cc:70
Util::FixedArray< _PendingResourceLoad > loads
Definition resourceloader.h:316
void SetMinLod(const Resources::ResourceId &id, const float lod, bool immediate)
begin updating a resources lod
Definition resourceloader.cc:756
const Resources::ResourceId GetId(const Resources::ResourceName &name) const
get resource id by name, use with care
Definition resourceloader.h:370
Resources::ResourceId CreateResource(const Resources::ResourceName &res, const void *loadInfo, SizeT loadInfoSize, const Util::StringAtom &tag, std::function< void(const Resources::ResourceId)> success, std::function< void(const Resources::ResourceId)> failed, bool immediate, bool stream)
create a container with a tag associated with it, if no tag is provided, the resource will be untagge...
void SetupIdFromEntry(const Ids::Id32 entry, ResourceId &cacheEntry)
Construct resource ID based on loader entry.
Definition resourceloader.cc:281
Util::Array< _PendingStreamLod > pendingStreamLods
Definition resourceloader.h:300
virtual ResourceStreamOutput StreamResource(const ResourceLoadJob &job)
Stream resource.
Definition resourceloader.cc:107
const bool HasResource(const Resources::ResourceId id) const
returns true if pool has resource
Definition resourceloader.h:390
virtual void Discard()
discard resource loader
Definition resourceloader.cc:60
Util::FixedArray< _PlaceholderResource > placeholders
Definition resourceloader.h:279
virtual ~ResourceLoader()
destructor
Definition resourceloader.cc:32
const Resources::ResourceName & GetName(const Resources::ResourceId id) const
get resource name
Definition resourceloader.h:334
virtual uint LodMask(const _StreamData &stream, float lod, bool async) const
Create load mask based on LOD. This will be used to determine if the resoure is fully loaded.
Definition resourceloader.cc:127
Util::FixedArray< uint32_t > usage
Definition resourceloader.h:310
void ReloadResource(const Resources::ResourceName &res, std::function< void(const Resources::ResourceId)> success, std::function< void(const Resources::ResourceId)> failed)
reload resource using resource name
Definition resourceloader.cc:701
Ids::IdPool resourceInstanceIndexPool
Definition resourceloader.h:307
Util::FixedArray< Util::Array< _Callbacks > > callbacks
Definition resourceloader.h:315
const Resource::State GetState(const Resources::ResourceId id) const
get resource state
Definition resourceloader.h:361
Util::FixedArray< _LoadMetaData > metaData
Definition resourceloader.h:317
virtual void Unload(const Resources::ResourceId id)=0
unload resource (overload to implement resource deallocation)
Threading::CriticalSection asyncSection
async section to sync callbacks and pending list with thread
Definition resourceloader.h:325
std::function< void()> preJobFunc
Definition resourceloader.h:294
Util::StringAtom failResourceName
Definition resourceloader.h:286
const uint32_t GetUsage(const Resources::ResourceId id) const
get resource usage from resource id
Definition resourceloader.h:343
friend void ApplyLoadOutput(ResourceLoader *loader, const ResourceLoader::ResourceLoadOutput &output)
Definition resourceloader.cc:145
Definition resourceloaderthread.h:23
Definition resourceserver.h:22
Critical section objects are used to protect a portion of code from parallel execution.
Thread-safe version of Util::Queue.
Definition safequeue.h:27
Nebula's dynamic array class.
Definition array.h:60
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:34
SizeT Size() const
return number of key/value pairs in the dictionary
Definition dictionary.h:193
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
IndexT FindIndex(const TYPE &val) const
find index of identical element in unsorted array (slow)
Definition fixedarray.h:633
A StringAtom.
Definition stringatom.h:22
const char * Value() const
get contained string as char ptr (fast)
Definition stringatom.h:362
uint32_t Id32
Definition id.h:138
A resource is a container for some type of file which is loaded.
Definition resource.cc:9
LoadFlags
Definition resourceloader.h:50
@ Update
Definition resourceloader.h:53
@ None
Definition resourceloader.h:51
@ Create
Definition resourceloader.h:52
pthread_t ThreadId
Definition linuxthreadid.h:15
#define __ImplementEnumBitOperators(type)
callback functions to run when an associated resource is loaded (can be stacked)
Definition resourceloader.h:240
std::function< void(const Resources::ResourceId)> success
Definition resourceloader.h:241
std::function< void(const Resources::ResourceId)> failed
Definition resourceloader.h:242
Definition resourceloader.h:128
void * data
Definition resourceloader.h:129
SizeT size
Definition resourceloader.h:130
struct for pending resources which are about to be loaded
Definition resourceloader.h:114
Ids::Id32 entry
Definition resourceloader.h:115
bool inflight
Definition resourceloader.h:117
IndexT frame
Definition resourceloader.h:120
bool reload
Definition resourceloader.h:119
bool immediate
Definition resourceloader.h:118
float lod
Definition resourceloader.h:121
_PendingResourceLoad()
Definition resourceloader.h:124
LoadFlags flags
Definition resourceloader.h:122
Util::StringAtom tag
Definition resourceloader.h:116
Resources::ResourceId resourceId
Definition resourceloader.h:235
struct for pending stream
Definition resourceloader.h:225
_PendingStreamLod()
Definition resourceloader.h:230
bool immediate
Definition resourceloader.h:228
Resources::ResourceId id
Definition resourceloader.h:226
float lod
Definition resourceloader.h:227
Resources::ResourceName placeholderName
Definition resourceloader.h:276
Resources::ResourceId placeholderId
Definition resourceloader.h:277
Definition resourceloader.h:134
Ptr< IO::Stream > stream
Definition resourceloader.h:135
void * data
Definition resourceloader.h:136
Definition resourceloader.h:157
uint loadedBits
Definition resourceloader.h:160
uint requestedBits
Definition resourceloader.h:158
uint pendingBits
Definition resourceloader.h:159
Definition resourceloader.h:140
ResourceInitOutput()
Definition resourceloader.h:144
ResourceUnknownId id
Definition resourceloader.h:141
_StreamData loaderStreamData
Definition resourceloader.h:142
Definition resourceloader.h:164
LoadState loadState
Definition resourceloader.h:169
float lod
Definition resourceloader.h:173
_StreamData streamData
Definition resourceloader.h:170
bool immediate
Definition resourceloader.h:172
static ResourceLoadJob FromPending(ResourceLoader *loader, IndexT frameIndex, const _PendingResourceLoad &load)
Definition resourceloader.h:177
IndexT frameIndex
Definition resourceloader.h:174
LoadFlags flags
Definition resourceloader.h:175
Util::String name
Definition resourceloader.h:165
Resource::State state
Definition resourceloader.h:167
Util::String tag
Definition resourceloader.h:166
_LoadMetaData metadata
Definition resourceloader.h:171
ResourceId id
Definition resourceloader.h:168
Definition resourceloader.h:196
LoadState loadState
Definition resourceloader.h:198
void UpdateLoaderState(ResourceLoader *loader) const
Definition resourceloader.h:203
ResourceId id
Definition resourceloader.h:200
Resource::State state
Definition resourceloader.h:199
ResourceLoadJob remainderJob
Definition resourceloader.h:201
_StreamData streamData
Definition resourceloader.h:197
uint loadedBits
Definition resourceloader.h:152
uint pendingBits
Definition resourceloader.h:153
Definition resourceid.h:37
Nebula's universal string class.
Definition string.h:50
static const int InvalidIndex
Definition types.h:54
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31
int IndexT
Definition types.h:48