3#include "IResourceManager.h"
4#include "Utilities/Strings.h"
6#include "Foundation/Platform/Atomic.h"
10 struct RenderResource;
52 FailedActivation = 1 << 15,
78 enum class ResourceError : uint32_t
92 ResourceId
id = NoResourceId;
98 ResourceError error = ResourceError::None;
109 const static uint32_t NoAttachment = 0xFFFFFFFF;
148 assert(!attachment &&
"Attachment still present when destructing resource.");
151 void setInitialized() { setFlag(ResourceFlags::Initialized); }
155 if (hasChanged())
return;
158 incrementGeneration();
160 flags |=
static_cast<uint32_t
>(ResourceFlags::Changed);
163 owner->resourceChanged(
this);
167 bool hasChanged()
const {
return (flags &
static_cast<uint32_t
>(ResourceFlags::Changed)) != 0; }
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); }
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); }
186 [[nodiscard]]
bool hasFailedLoad()
const {
return isSet(ResourceFlags::FailedLoad); }
187 [[nodiscard]]
bool hasFailedActivation()
const {
return isSet(ResourceFlags::FailedActivation); }
189 [[nodiscard]]
bool keepStorage()
const {
return isSet(ResourceFlags::KeepStorage); }
205 assert(info->id == NoResourceId || resourceId == info->id);
207 info->id = resourceId;
215 ResourceId
getId()
const {
return info ? info->id : NoResourceId; }
226 void setFlags(
ResourceFlags flags) { this->flags |=
static_cast<uint32_t
>(flags); }
245 bool isSet(
ResourceFlags flag)
const {
return (this->flags &
static_cast<uint32_t
>(flag)) ==
static_cast<uint32_t
>(flag); }
309 void setSource(
const StringView & source) {
if (!info)
return; info->source = Strings::add(source); }
325 if (++usage == 1 && isSet(ResourceFlags::Orphaned)) {
326 unsetFlag(ResourceFlags::Orphaned);
344 assert(usage.load() > 0 &&
"Reference count already at zero. Cannot decrement.");
347 setFlag(ResourceFlags::Orphaned);
350 owner->resourceDeleted(
this);
383 void setSlot(uint32_t slot) { this->slot = slot; }
389 void setInfo(
ResourceInfo * info) { this->info = info; }
392 Atomic<uint32_t> usage{ 0 };
395 Atomic<uint32_t> flags =
static_cast<uint32_t
>(ResourceFlags::None);
407 uint32_t slot = 0xFFFFFFFF;
410 uint32_t generation = 0;
413 friend struct ResourcePool;
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.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
ResourceFlags
Flags for describing resource attributes.
@ 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.
@ Success
Resource activated successfully.
@ Postponed
Resource activation postponed, retry later.
@ Failure
Resource activation failed.
ResourceTypes
Resource types.
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.
Effect resources contain data to control the shader stages of the GPU pipeline.
Font resources are used to render text with a specific font and size.
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...
Meshes contain streams of vertex data in addition to index data and options defining geometry used fo...
Model resources define a template for a set of connected entities, with resources such as meshes,...
Base class for engine resources.
uint32_t getGeneration() const
Get the generation count.
ResourceId getId() const
Get the resource id of this instance.
void incrementGeneration()
Increment the generation count.
ResourceFlags getFlags() const
Get the current flags of the resource.
ResourceBase & operator=(const ResourceBase &other)=delete
Disable copy-assignment of resources.
StringView getName() const
Get the name of the resource.
void unsetFlag(ResourceFlags flag)
Unset the given flag.
void setFlag(ResourceFlags flags)
Set the given resource flag.
uint32_t getSlot() const
Gets the slot where the resource is tracked internally.
void attachResource(RenderResource *attachment)
Attach the given GPU resource to the resource.
bool isSet(ResourceFlags flag) const
Check if the given flag is currently set.
void decrement()
Decrement the reference count of the resource.
ResourceBase(const ResourceBase &other)=delete
Disable trivial copies of resources.
void setName(const StringView &name)
Set the user friendly name of the resource.
void setType(ResourceTypes type)
Set the type enumeration of the resource.
ResourceTypes getType() const
Gets the type enumeration of the resource.
void setId(ResourceId resourceId)
Set the resource id of the resource.
void increment()
Increments the reference count of the resource.
~ResourceBase()
Destructs the resource.
ResourceBase()=default
Constructs a new resource base.
RenderResource * getAttachedResource() const
Get the attached resource.
void setSlot(uint32_t slot)
Sets the slot at which the resource is internally tracked.
bool hasAttachedResource() const
Check if the resource has an attachment.
uint32_t referenceCount() const
Get the current reference count.
void setOwner(IResourceManager *owner)
Sets the owner of this resource instance.
Texture resources contain raster bitmap data to use for texturing.