Cogs.Core
ResourceBase.h
1#pragma once
2
3#include "IResourceManager.h"
4#include "Utilities/Strings.h"
5
6#include "Foundation/Platform/Atomic.h"
7
8namespace Cogs::Core
9{
10 struct RenderResource;
11
13 enum class ActivationResult : uint16_t
14 {
16 Success = 0x0001,
18 Failure = 0x0002,
20 Postponed = 0x0004,
22 Delayed = 0x0008,
23 };
24
26 enum class ResourceFlags : uint32_t
27 {
29 None = 0,
30 Initialized = 1 << 0,
31 Queued = 1 << 1,
32 Loading = 1 << 2,
33 Loaded = 1 << 3,
34 Changed = 1 << 4,
36 Active = 1 << 5,
38 Resident = 1 << 6,
39 Static = 1 << 7,
41 Orphaned = 1 << 8,
42 Deleted = 1 << 9,
44 Locked = 1 << 10,
46 Dependency = 1 << 11,
48 Watched = 1 << 12,
50 Error = 1 << 13,
51 FailedLoad = 1 << 14,
52 FailedActivation = 1 << 15,
53 Proxy = 1 << 16,
55 KeepStorage = 1 << 17,
56 };
57
58 ENABLE_ENUM_FLAGS(ResourceFlags);
59
63 enum class ResourceTypes : uint32_t
64 {
65 Unknown = 0,
66 Asset,
67 Model,
68 Texture,
69 Effect,
72 Mesh,
73 Buffer,
74 Font,
75 Gui
76 };
77
78 enum class ResourceError : uint32_t
79 {
80 None = 0,
81 };
82
83 struct COGSCORE_DLL_API ResourceInfo
84 {
86 StringRef name = NoString;
87
89 StringRef source = NoString;
90
92 ResourceId id = NoResourceId;
93
95 ResourceTypes type = ResourceTypes::Unknown;
96
98 ResourceError error = ResourceError::None;
99 };
100
106 struct COGSCORE_DLL_API ResourceBase
107 {
109 const static uint32_t NoAttachment = 0xFFFFFFFF;
110
117 ResourceBase() = default;
118
120 ResourceBase(const ResourceBase & other) = delete;
121
123 ResourceBase & operator=(const ResourceBase & other) = delete;
124
130 ResourceBase(ResourceBase && other);
131
140 ResourceBase & operator=(ResourceBase && other);
141
146 {
147 // To ensure we will not leak rendering resources, make sure any attachments are removed.
148 assert(!attachment && "Attachment still present when destructing resource.");
149 }
150
151 void setInitialized() { setFlag(ResourceFlags::Initialized); }
152
153 void setChanged()
154 {
155 if (hasChanged()) return;
156
157 setInitialized();
158 incrementGeneration();
159
160 flags |= static_cast<uint32_t>(ResourceFlags::Changed);
161
162 if (owner) {
163 owner->resourceChanged(this);
164 }
165 }
166
167 bool hasChanged() const { return (flags & static_cast<uint32_t>(ResourceFlags::Changed)) != 0; }
168
169 void setLoading() { setFlag(ResourceFlags::Loading); }
170 void setLoaded() { setFlag(ResourceFlags::Loaded); }
171 void setFailedLoad() { setFlag(ResourceFlags::FailedLoad); }
172 void setActive() { setFlag(ResourceFlags::Active); }
173 void setResident() { setFlag(ResourceFlags::Active | ResourceFlags::Resident); }
174 void setProxy() { setFlag(ResourceFlags::Proxy); }
175 void setKeepStorage() { setFlag(ResourceFlags::KeepStorage); }
176
177 [[nodiscard]] bool isInitialized() const { return isSet(ResourceFlags::Initialized); }
178 [[nodiscard]] bool isLoaded() const { return isSet(ResourceFlags::Loaded); }
179 [[nodiscard]] bool isActive() const { return isSet(ResourceFlags::Active); }
180 [[nodiscard]] bool isResident() const { return isSet(ResourceFlags::Resident); }
181 [[nodiscard]] bool isDependency() const { return isSet(ResourceFlags::Dependency); }
182 [[nodiscard]] bool isDeleted() const { return isSet(ResourceFlags::Deleted) && isSet(ResourceFlags::Orphaned); }
183 [[nodiscard]] bool isOrphaned() const { return isSet(ResourceFlags::Orphaned); }
184 [[nodiscard]] bool isProxy() const { return isSet(ResourceFlags::Proxy); }
185
186 [[nodiscard]] bool hasFailedLoad() const { return isSet(ResourceFlags::FailedLoad); }
187 [[nodiscard]] bool hasFailedActivation() const { return isSet(ResourceFlags::FailedActivation); }
188
189 [[nodiscard]] bool keepStorage() const { return isSet(ResourceFlags::KeepStorage); }
190
192 void setType(ResourceTypes type) { info->type = type; }
193
195 [[nodiscard]] ResourceTypes getType() const { return info ? info->type : ResourceTypes::Unknown; }
196
202 void setId(ResourceId resourceId)
203 {
204 assert(info);
205 assert(info->id == NoResourceId || resourceId == info->id);
206
207 info->id = resourceId;
208 }
209
215 ResourceId getId() const { return info ? info->id : NoResourceId; }
216
225 void setFlag(ResourceFlags flags) { this->flags |= static_cast<uint32_t>(flags); }
226 void setFlags(ResourceFlags flags) { this->flags |= static_cast<uint32_t>(flags); }
227
236 void unsetFlag(ResourceFlags flag) { this->flags &= static_cast<uint32_t>(~flag); }
237
245 bool isSet(ResourceFlags flag) const { return (this->flags & static_cast<uint32_t>(flag)) == static_cast<uint32_t>(flag); }
246
252 ResourceFlags getFlags() const { return static_cast<ResourceFlags>(this->flags.load()); }
253
261 void attachResource(RenderResource * attachment) { this->attachment = attachment; }
262
268 bool hasAttachedResource() const { return this->attachment != nullptr; }
269
275 RenderResource * getAttachedResource() const { return this->attachment; }
276
287 void setOwner(IResourceManager * owner) { this->owner = owner; }
288
289 IResourceManager * getOwner() const { return owner; }
290
298 void setName(const StringView & name) { if (!info) return; info->name = Strings::add(name); }
299
307 StringView getName() const { return info ? Strings::get(info->name) : StringView{}; }
308
309 void setSource(const StringView & source) { if (!info) return; info->source = Strings::add(source); }
310 StringView getSource() const { return info ? Strings::get(info->source) : StringView{}; }
311
324 {
325 if (++usage == 1 && isSet(ResourceFlags::Orphaned)) {
326 unsetFlag(ResourceFlags::Orphaned);
327 }
328 }
329
343 {
344 assert(usage.load() > 0 && "Reference count already at zero. Cannot decrement.");
345
346 if (!--usage) {
347 setFlag(ResourceFlags::Orphaned);
348
349 if (owner) {
350 owner->resourceDeleted(this);
351 }
352 }
353 }
354
360 uint32_t referenceCount() const { return usage.load(); }
361
368 {
369 ++generation;
370 }
371
380 uint32_t getGeneration() const { return generation; }
381
383 void setSlot(uint32_t slot) { this->slot = slot; }
384
386 uint32_t getSlot() const { return slot; }
387
388 private:
389 void setInfo(ResourceInfo * info) { this->info = info; }
390
392 Atomic<uint32_t> usage{ 0 };
393
395 Atomic<uint32_t> flags = static_cast<uint32_t>(ResourceFlags::None);
396
398 struct RenderResource * attachment = nullptr;
399
401 ResourceInfo * info = nullptr;
402
404 IResourceManager * owner = nullptr;
405
407 uint32_t slot = 0xFFFFFFFF;
408
410 uint32_t generation = 0;
411
412 template<typename U>
413 friend struct ResourcePool;
414
415 friend class ResourceManagerBase;
416 };
417}
Defines common resource manager interface shared by resource managers for all types of resources.
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
ResourceFlags
Flags for describing resource attributes.
Definition: ResourceBase.h:27
@ Watched
The resource on disk is being watched for changes.
@ Dependency
The resource is a dependency that should trigger subsequent updates if changed.
@ Orphaned
The resource has no alive handles, bringing the reference count to zero.
@ Resident
The resource is loaded onto the GPU.
@ Locked
The resource is locked for outside manipulations by a thread performing work on the data.
@ KeepStorage
Keep resource cpu storage after upload to the GPU.
ActivationResult
Defines results for resource activation.
Definition: ResourceBase.h:14
@ Success
Resource activated successfully.
@ Postponed
Resource activation postponed, retry later.
@ Failure
Resource activation failed.
ResourceTypes
Resource types.
Definition: ResourceBase.h:64
void setChanged(Cogs::Core::Context *context, Cogs::ComponentModel::Component *component, Reflection::FieldId fieldId)
Must be Called after changing a Component field. Mark field changed. Request engine update.
Definition: FieldSetter.h:25
Effect resources contain data to control the shader stages of the GPU pipeline.
Definition: Effect.h:24
Font resources are used to render text with a specific font and size.
Definition: Font.h:22
Material instances represent a specialized Material combined with state for all its buffers and prope...
Material resources define the how of geometry rendering (the what is defined by Mesh and Texture reso...
Definition: Material.h:82
Meshes contain streams of vertex data in addition to index data and options defining geometry used fo...
Definition: Mesh.h:265
Model resources define a template for a set of connected entities, with resources such as meshes,...
Definition: Model.h:56
Base class for engine resources.
Definition: ResourceBase.h:107
uint32_t getGeneration() const
Get the generation count.
Definition: ResourceBase.h:380
ResourceId getId() const
Get the resource id of this instance.
Definition: ResourceBase.h:215
void incrementGeneration()
Increment the generation count.
Definition: ResourceBase.h:367
ResourceFlags getFlags() const
Get the current flags of the resource.
Definition: ResourceBase.h:252
ResourceBase & operator=(const ResourceBase &other)=delete
Disable copy-assignment of resources.
StringView getName() const
Get the name of the resource.
Definition: ResourceBase.h:307
void unsetFlag(ResourceFlags flag)
Unset the given flag.
Definition: ResourceBase.h:236
void setFlag(ResourceFlags flags)
Set the given resource flag.
Definition: ResourceBase.h:225
uint32_t getSlot() const
Gets the slot where the resource is tracked internally.
Definition: ResourceBase.h:386
void attachResource(RenderResource *attachment)
Attach the given GPU resource to the resource.
Definition: ResourceBase.h:261
bool isSet(ResourceFlags flag) const
Check if the given flag is currently set.
Definition: ResourceBase.h:245
void decrement()
Decrement the reference count of the resource.
Definition: ResourceBase.h:342
ResourceBase(const ResourceBase &other)=delete
Disable trivial copies of resources.
void setName(const StringView &name)
Set the user friendly name of the resource.
Definition: ResourceBase.h:298
void setType(ResourceTypes type)
Set the type enumeration of the resource.
Definition: ResourceBase.h:192
ResourceTypes getType() const
Gets the type enumeration of the resource.
Definition: ResourceBase.h:195
void setId(ResourceId resourceId)
Set the resource id of the resource.
Definition: ResourceBase.h:202
void increment()
Increments the reference count of the resource.
Definition: ResourceBase.h:323
~ResourceBase()
Destructs the resource.
Definition: ResourceBase.h:145
ResourceBase()=default
Constructs a new resource base.
RenderResource * getAttachedResource() const
Get the attached resource.
Definition: ResourceBase.h:275
void setSlot(uint32_t slot)
Sets the slot at which the resource is internally tracked.
Definition: ResourceBase.h:383
bool hasAttachedResource() const
Check if the resource has an attachment.
Definition: ResourceBase.h:268
uint32_t referenceCount() const
Get the current reference count.
Definition: ResourceBase.h:360
void setOwner(IResourceManager *owner)
Sets the owner of this resource instance.
Definition: ResourceBase.h:287
Texture resources contain raster bitmap data to use for texturing.
Definition: Texture.h:91