Refactoring Rendering backend
This commit is contained in:
@@ -1,148 +1,30 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using Win32.Graphics.D3D12MemoryAllocator;
|
||||
using Ghost.Core;
|
||||
|
||||
namespace Ghost.Graphics.Data;
|
||||
|
||||
public readonly struct ResourceHandle : IEquatable<ResourceHandle>
|
||||
public readonly struct GPUResource : IHandleType;
|
||||
public readonly struct Texture : IHandleType;
|
||||
public readonly struct GraphicsBuffer : IHandleType;
|
||||
|
||||
public static class ResourceHandleExtensions
|
||||
{
|
||||
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)
|
||||
public static Handle<GPUResource> AsResource(this Handle<Texture> texture)
|
||||
{
|
||||
this.id = id;
|
||||
this.generation = generation;
|
||||
return new Handle<GPUResource>(texture.id, texture.generation);
|
||||
}
|
||||
|
||||
public bool IsValid => id != _INVALID_ID && generation != _INVALID_ID;
|
||||
|
||||
public bool Equals(ResourceHandle other)
|
||||
public static Handle<GPUResource> AsResource(this Handle<GraphicsBuffer> buffer)
|
||||
{
|
||||
return id == other.id && generation == other.generation;
|
||||
return new Handle<GPUResource>(buffer.id, buffer.generation);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
internal static Handle<Texture> AsTexture(this Handle<GPUResource> resource)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
return (id * 397) ^ generation;
|
||||
}
|
||||
return new Handle<Texture>(resource.id, resource.generation);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
internal static Handle<GraphicsBuffer> AsGraphicsBuffer(this Handle<GPUResource> resource)
|
||||
{
|
||||
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);
|
||||
return new Handle<GraphicsBuffer>(resource.id, resource.generation);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user