forked from Misaki/GhostEngine
- Introduced `Handle<T>` and `Identifier<T>` for lightweight, strongly-typed resource identifiers. - Replaced `BitSet` with `UnsafeBitSet` for improved performance and memory safety. - Refactored `Mesh` and `Material` into `MeshClass` and `MaterialClass` for better GPU resource handling. - Added `D3D12ResourceDatabase` to centralize GPU resource tracking and lifecycle management. - Updated `D3D12ShaderCompiler` to load shaders from disk and dynamically populate constant buffers and textures. - Enhanced `ICommandBuffer` with new upload operations for buffers and textures. - Refactored `Vertex` struct for simplified memory layout and better performance. - Updated `MeshBuilder` and rendering logic to align with new resource and shader structures. - Added `BindlessDescriptor` support to `TextureHandle` and `BufferHandle`. - Removed unused classes and performed general cleanup. - Updated unit tests and demos to reflect the new architecture.
148 lines
3.9 KiB
C#
148 lines
3.9 KiB
C#
using System.Runtime.CompilerServices;
|
|
using Win32.Graphics.D3D12MemoryAllocator;
|
|
|
|
namespace Ghost.Graphics.Data;
|
|
|
|
public readonly struct ResourceHandle : IEquatable<ResourceHandle>
|
|
{
|
|
private const int _INVALID_ID = -1;
|
|
|
|
public readonly int id;
|
|
public readonly int generation;
|
|
|
|
public static ResourceHandle Invalid => new(_INVALID_ID, _INVALID_ID);
|
|
|
|
internal ResourceHandle(int id, int generation)
|
|
{
|
|
this.id = id;
|
|
this.generation = generation;
|
|
}
|
|
|
|
public bool IsValid => id != _INVALID_ID && generation != _INVALID_ID;
|
|
|
|
public bool Equals(ResourceHandle other)
|
|
{
|
|
return id == other.id && generation == other.generation;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
unchecked
|
|
{
|
|
return (id * 397) ^ generation;
|
|
}
|
|
}
|
|
|
|
public override bool Equals(object? obj)
|
|
{
|
|
return obj is ResourceHandle handle && Equals(handle);
|
|
}
|
|
|
|
public static bool operator ==(ResourceHandle left, ResourceHandle right)
|
|
{
|
|
return left.Equals(right);
|
|
}
|
|
|
|
public static bool operator !=(ResourceHandle left, ResourceHandle right)
|
|
{
|
|
return !(left == right);
|
|
}
|
|
}
|
|
|
|
public readonly struct TextureHandle : IEquatable<TextureHandle>
|
|
{
|
|
private readonly ResourceHandle _resourceHandle;
|
|
private readonly BindlessDescriptor _bindlessDescriptor;
|
|
|
|
public ResourceHandle ResourceHandle => _resourceHandle;
|
|
public static TextureHandle Invalid => new(ResourceHandle.Invalid);
|
|
|
|
internal TextureHandle(ResourceHandle resourceHandle)
|
|
{
|
|
_resourceHandle = resourceHandle;
|
|
_bindlessDescriptor = BindlessDescriptor.Invalid;
|
|
}
|
|
|
|
internal TextureHandle(ResourceHandle resourceHandle, BindlessDescriptor descriptor)
|
|
{
|
|
_resourceHandle = resourceHandle;
|
|
_bindlessDescriptor = descriptor;
|
|
}
|
|
|
|
public bool IsValid => _resourceHandle.IsValid;
|
|
|
|
public bool Equals(TextureHandle other)
|
|
{
|
|
return _resourceHandle.Equals(other._resourceHandle) && _bindlessDescriptor.Equals(other._bindlessDescriptor);
|
|
}
|
|
|
|
public override bool Equals(object? obj)
|
|
{
|
|
return obj is TextureHandle other && Equals(other);
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return HashCode.Combine(_resourceHandle, _bindlessDescriptor);
|
|
}
|
|
|
|
public static bool operator ==(TextureHandle left, TextureHandle right)
|
|
{
|
|
return left.Equals(right);
|
|
}
|
|
|
|
public static bool operator !=(TextureHandle left, TextureHandle right)
|
|
{
|
|
return !(left == right);
|
|
}
|
|
}
|
|
|
|
public readonly struct BufferHandle : IEquatable<BufferHandle>
|
|
{
|
|
private readonly ResourceHandle _resourceHandle;
|
|
private readonly BindlessDescriptor _bindlessDescriptor;
|
|
|
|
public static BufferHandle Invalid => new(ResourceHandle.Invalid);
|
|
|
|
public ResourceHandle ResourceHandle => _resourceHandle;
|
|
public BindlessDescriptor BindlessDescriptor => _bindlessDescriptor;
|
|
|
|
internal BufferHandle(ResourceHandle resourceHandle)
|
|
{
|
|
_resourceHandle = resourceHandle;
|
|
_bindlessDescriptor = BindlessDescriptor.Invalid;
|
|
}
|
|
|
|
internal BufferHandle(ResourceHandle resourceHandle, BindlessDescriptor descriptor)
|
|
{
|
|
_resourceHandle = resourceHandle;
|
|
_bindlessDescriptor = descriptor;
|
|
}
|
|
|
|
public bool IsValid => _resourceHandle.IsValid;
|
|
|
|
public bool Equals(BufferHandle other)
|
|
{
|
|
return _resourceHandle.Equals(other._resourceHandle) && _bindlessDescriptor.Equals(other._bindlessDescriptor);
|
|
}
|
|
|
|
public override bool Equals(object? obj)
|
|
{
|
|
return obj is BufferHandle other && Equals(other);
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return HashCode.Combine(_resourceHandle, _bindlessDescriptor);
|
|
}
|
|
|
|
public static bool operator ==(BufferHandle left, BufferHandle right)
|
|
{
|
|
return left.Equals(right);
|
|
}
|
|
|
|
public static bool operator !=(BufferHandle left, BufferHandle right)
|
|
{
|
|
return !(left == right);
|
|
}
|
|
} |