using Ghost.Core; using Ghost.Graphics.Core; namespace Ghost.Graphics.RHI; public interface IResourceReleasable { /// /// A method to release GPU resources. /// void ReleaseResource(IResourceDatabase database); } public struct ResourceBarrierData { public BarrierLayout layout; public BarrierAccess access; public BarrierSync sync; public ResourceBarrierData(BarrierLayout layout, BarrierAccess access, BarrierSync sync) { this.layout = layout; this.access = access; this.sync = sync; } } public enum BindlessAccess { ShaderResource, ConstantBuffer, UnorderedAccess, } // TODO: Consider adding methods for resource enumeration, statistics, and bulk operations. // TODO: Consider adding async resource loading and streaming support. // TODO: Mesh, Material, Shader management could be separated into their own interfaces for better modularity because they are not bound to specific graphics API. public interface IResourceDatabase : IDisposable { /* /// /// Imports an external unmanaged resource and returns a handle for use within the resource management system. /// /// The space of the unmanaged resource pointer to import. /// A pointer to the external unmanaged resource to be imported. Must remain valid for the duration of the resource's usage. /// The initial state to assign to the imported resource. /// A handle representing the imported resource, which can be used for subsequent operations. unsafe Handle ImportExternalResource(T resourcePtr, ResourceState initialState, string? name = null) where T : unmanaged; */ /// /// Checks if a resource with the specified handle exists in the database. /// /// The handle of the resource to check for existence. bool HasResource(Handle handle); /// /// Retrieves the current barrier data of the specified resource. /// /// The handle that uniquely identifies the resource. /// A ResourceBarrierData value representing the current barrier state. Result GetResourceBarrierData(Handle handle); /// /// Sets the barrier data of the specified resource handle. /// /// The handle that identifies the resource. /// The new barrier data. /// An Error indicating the success or failure of the operation. Error SetResourceBarrierData(Handle handle, ResourceBarrierData data); /// /// Retrieves the description of a GPU resource associated with the specified handle. /// /// A handle that identifies the GPU resource for which to obtain the description. Must reference a valid resource. /// A ResourceDesc structure containing details about the specified GPU resource. Result GetResourceDescription(Handle handle); /// /// Retrieves the bindless index associated with the specified GPU resource handle. /// /// A handle to the GPU resource for which to obtain the bindless index. Must reference a valid, currently registered resource. /// The type of bindless access for which to obtain the index. /// The bindless index corresponding to the specified GPU resource handle. ~0 if the resource does not support bindless access or is not found. uint GetBindlessIndex(Handle handle, BindlessAccess access = BindlessAccess.ShaderResource); /// /// Retrieves the name of the GPU resource associated with the specified handle. /// /// /// You should only use this method in debug builds or inside engine editor. /// /// A handle to the GPU resource for which to obtain the name. Must reference a valid resource. /// The name of the GPU resource associated with the specified handle, or null if the resource does not have a name. string? GetResourceName(Handle handle); /// /// Removes a resource from the database using its handle. /// /// The handle of the resource to be removed. void ReleaseResource(Handle handle); /// /// Retrieves an existing sampler identifier that matches the specified description, or creates a new one if none /// exists. /// /// A read-only reference to a structure that defines the properties of the sampler to retrieve or create. /// An integer identifier to associate with the sampler. /// An representing the sampler that matches the specified description. /// If a matching sampler does not exist, a new sampler is created and its identifier is returned. Identifier CreateSampler(ref readonly SamplerDesc desc, int id); /// /// Determines whether a sampler with the specified identifier exists. /// /// The identifier of the sampler to check for existence. /// true if a sampler with the given identifier exists; otherwise, false. bool TryGetSampler(ref readonly SamplerDesc desc, out Identifier id); /// /// Releases the sampler associated with the specified identifier and frees any resources allocated to it. /// /// The identifier of the sampler to release. Must reference a valid, existing sampler. void ReleaseSampler(Identifier id); /// /// Adds a mesh to the resource database and returns its handle. /// /// The mesh data to be added to the database. /// The representing the newly added mesh."/> Handle AddMesh(ref readonly Mesh mesh); /// /// Determines whether a mesh with the specified Handle exists. /// /// The handle of the mesh to check for existence. Cannot be null. /// true if a mesh with the specified Handle exists; otherwise, false. bool HasMesh(Handle handle); /// /// Returns a reference to the mesh associated with the specified handle. /// /// The handle of the mesh to retrieve. Must refer to a valid mesh; otherwise, the behavior is undefined. /// A result containing a reference to the mesh corresponding to the specified handle, or an error status if the handle is invalid. RefResult GetMeshReference(Handle handle); /// /// Releases the mesh resource associated with the specified handle, freeing any resources held by it. Includes both CPU and GPU resources. /// /// The handle of the mesh to release. Must refer to a mesh that was previously created and not already released. void ReleaseMesh(Handle handle); /// /// Adds a new material to the collection and returns its unique handle. /// /// The material to add. The material must be fully initialized before calling this method. /// The representing the newly added material. Handle AddMaterial(ref readonly Material material); /// /// Determines whether a material with the specified handle exists in the collection. /// /// The handle of the material to check for existence. /// true if a material with the specified handle exists; otherwise, false. bool HasMaterial(Handle handle); /// /// Gets a reference to the material associated with the specified handle. /// /// The handle of the material to retrieve. Must refer to a valid material. /// A result containing a reference to the material corresponding to the specified handle, or an error status if the handle is invalid. RefResult GetMaterialReference(Handle handle); /// /// Releases the material associated with the specified handle, making it available for reuse or disposal. /// /// The handle of the material to release. Must refer to a material that has been previously acquired. void ReleaseMaterial(Handle handle); /// /// Adds the specified shader to the collection and returns its unique identifier. /// /// The shader to add. The shader is passed by read-only reference and will not be modified. /// The representing the newly added shader. Identifier AddShader(Shader shader); /// /// Determines whether a shader with the specified identifier exists in the collection. /// /// The identifier of the shader to check for existence. /// true if a shader with the specified identifier exists; otherwise, false. bool HasShader(Identifier id); /// /// Returns a reference to the shader associated with the specified identifier. /// /// The identifier of the shader to retrieve. Must refer to a valid shader. /// A result containing a reference to the shader corresponding to the specified identifier, or an error status if the identifier is invalid. RefResult GetShaderReference(Identifier id); /// /// Releases the shader associated with the specified identifier, freeing any resources allocated to it. /// /// The identifier of the shader to release. Must refer to a valid, previously created shader. void ReleaseShader(Identifier id); }