forked from Misaki/GhostEngine
Refactor GPU resource management and rendering pipeline
- 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.
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
using Ghost.Graphics.D3D12;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Helpers;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Ghost.Graphics.Data;
|
||||
|
||||
internal struct CBufferCache : IDisposable
|
||||
internal struct CBufferCache
|
||||
{
|
||||
public UnsafeArray<byte> CpuData
|
||||
{
|
||||
@@ -14,33 +11,18 @@ internal struct CBufferCache : IDisposable
|
||||
set;
|
||||
}
|
||||
|
||||
public GraphicsBuffer GpuResource
|
||||
public BufferHandle GpuResource
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
private readonly uint _alignedSize;
|
||||
|
||||
internal unsafe CBufferCache(uint bufferSize)
|
||||
internal unsafe CBufferCache(BufferHandle buffer, uint bufferSize)
|
||||
{
|
||||
_alignedSize = (bufferSize + 255u) & ~255u;
|
||||
|
||||
CpuData = new((int)_alignedSize, Allocator.Persistent);
|
||||
GpuResource = GraphicsBuffer.Create(_alignedSize, GraphicsBuffer.Usage.Constant);
|
||||
GpuResource.Name = "Material_CBufferCache";
|
||||
|
||||
UploadToGpu();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void UploadToGpu()
|
||||
{
|
||||
GpuResource.SetData(CpuData.AsSpan(), 0);
|
||||
}
|
||||
|
||||
public readonly void Dispose()
|
||||
{
|
||||
CpuData.Dispose();
|
||||
GpuResource.Dispose();
|
||||
GpuResource = buffer;
|
||||
}
|
||||
}
|
||||
@@ -7,14 +7,14 @@ namespace Ghost.Graphics.Data;
|
||||
/// Represents a color with 4 bytes components.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Size = 4)]
|
||||
public struct Color4 : IEquatable<Color4>
|
||||
public struct Color32 : IEquatable<Color32>
|
||||
{
|
||||
public byte r;
|
||||
public byte g;
|
||||
public byte b;
|
||||
public byte a;
|
||||
|
||||
public Color4(byte r, byte g, byte b, byte a)
|
||||
public Color32(byte r, byte g, byte b, byte a)
|
||||
{
|
||||
this.r = r;
|
||||
this.g = g;
|
||||
@@ -22,24 +22,24 @@ public struct Color4 : IEquatable<Color4>
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public Color4(Color color)
|
||||
public Color32(Color color)
|
||||
: this(color.R, color.G, color.B, color.A)
|
||||
{
|
||||
}
|
||||
|
||||
public Color4(Color16 color128)
|
||||
public Color32(Color128 color128)
|
||||
: this((byte)(color128.r * 255.0f), (byte)(color128.g * 255.0f), (byte)(color128.b * 255.0f), (byte)(color128.a * 255.0f))
|
||||
{
|
||||
}
|
||||
|
||||
public readonly bool Equals(Color4 other)
|
||||
public readonly bool Equals(Color32 other)
|
||||
{
|
||||
return r == other.r && g == other.g && b == other.b && a == other.a;
|
||||
}
|
||||
|
||||
public override readonly bool Equals(object? obj)
|
||||
{
|
||||
return obj is Color4 color && Equals(color);
|
||||
return obj is Color32 color && Equals(color);
|
||||
}
|
||||
|
||||
public override readonly int GetHashCode()
|
||||
@@ -47,12 +47,12 @@ public struct Color4 : IEquatable<Color4>
|
||||
return HashCode.Combine(r, g, b, a);
|
||||
}
|
||||
|
||||
public static bool operator ==(Color4 left, Color4 right)
|
||||
public static bool operator ==(Color32 left, Color32 right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(Color4 left, Color4 right)
|
||||
public static bool operator !=(Color32 left, Color32 right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
@@ -62,14 +62,14 @@ public struct Color4 : IEquatable<Color4>
|
||||
/// Represents a color with 16 bytes components.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Size = 16)]
|
||||
public struct Color16 : IEquatable<Color16>
|
||||
public struct Color128 : IEquatable<Color128>
|
||||
{
|
||||
public float r;
|
||||
public float g;
|
||||
public float b;
|
||||
public float a;
|
||||
|
||||
public Color16(float r, float g, float b, float a)
|
||||
public Color128(float r, float g, float b, float a)
|
||||
{
|
||||
this.r = r;
|
||||
this.g = g;
|
||||
@@ -77,24 +77,24 @@ public struct Color16 : IEquatable<Color16>
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public Color16(Color color)
|
||||
public Color128(Color color)
|
||||
: this(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f)
|
||||
{
|
||||
}
|
||||
|
||||
public Color16(Color4 color32)
|
||||
public Color128(Color32 color32)
|
||||
: this(color32.r / 255.0f, color32.g / 255.0f, color32.b / 255.0f, color32.a / 255.0f)
|
||||
{
|
||||
}
|
||||
|
||||
public readonly bool Equals(Color16 other)
|
||||
public readonly bool Equals(Color128 other)
|
||||
{
|
||||
return r.Equals(other.r) && g.Equals(other.g) && b.Equals(other.b) && a.Equals(other.a);
|
||||
}
|
||||
|
||||
public override readonly bool Equals(object? obj)
|
||||
{
|
||||
return obj is Color16 color && Equals(color);
|
||||
return obj is Color128 color && Equals(color);
|
||||
}
|
||||
|
||||
public readonly override int GetHashCode()
|
||||
@@ -102,12 +102,12 @@ public struct Color16 : IEquatable<Color16>
|
||||
return HashCode.Combine(r, g, b, a);
|
||||
}
|
||||
|
||||
public static bool operator ==(Color16 left, Color16 right)
|
||||
public static bool operator ==(Color128 left, Color128 right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(Color16 left, Color16 right)
|
||||
public static bool operator !=(Color128 left, Color128 right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
namespace Ghost.Graphics.Data;
|
||||
|
||||
public struct BatchMaterialID : IEquatable<BatchMaterialID>
|
||||
{
|
||||
public uint value;
|
||||
|
||||
public static BatchMaterialID Null => new() { value = 0 };
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return value.GetHashCode();
|
||||
}
|
||||
|
||||
public readonly override bool Equals(object? obj)
|
||||
{
|
||||
return obj is BatchMaterialID id && Equals(id);
|
||||
}
|
||||
|
||||
public readonly bool Equals(BatchMaterialID other)
|
||||
{
|
||||
return value == other.value;
|
||||
}
|
||||
|
||||
public readonly int CompareTo(BatchMaterialID other)
|
||||
{
|
||||
return value.CompareTo(other.value);
|
||||
}
|
||||
|
||||
public static bool operator ==(BatchMaterialID a, BatchMaterialID b)
|
||||
{
|
||||
return a.Equals(b);
|
||||
}
|
||||
|
||||
public static bool operator !=(BatchMaterialID a, BatchMaterialID b)
|
||||
{
|
||||
return !a.Equals(b);
|
||||
}
|
||||
}
|
||||
|
||||
internal class GraphicsResourceManager
|
||||
{
|
||||
private readonly Dictionary<BatchMaterialID, Material> _materials = new();
|
||||
private readonly Dictionary<BatchMeshID, MeshHandle> _meshes = new();
|
||||
|
||||
public Material GetMaterial(BatchMaterialID id)
|
||||
{
|
||||
return _materials.TryGetValue(id, out var material) ? material : Material.Null;
|
||||
}
|
||||
|
||||
public MeshHandle GetMesh(BatchMeshID id)
|
||||
{
|
||||
return _meshes.TryGetValue(id, out var mesh) ? mesh : MeshHandle.Invalid;
|
||||
}
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
using Ghost.Graphics.D3D12;
|
||||
using Ghost.Graphics.RHI;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Win32.Graphics.Direct3D12;
|
||||
|
||||
namespace Ghost.Graphics.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Material implementation for bindless rendering with SM 6.6 support
|
||||
/// </summary>
|
||||
public unsafe class Material : IDisposable
|
||||
{
|
||||
private readonly CBufferCache[] _cbufferCaches;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
internal ReadOnlySpan<CBufferCache> CBufferCaches => _cbufferCaches;
|
||||
|
||||
public Shader Shader
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public Material(Shader shader)
|
||||
{
|
||||
Shader = shader;
|
||||
|
||||
if (shader.ConstantBuffers.Count > 0)
|
||||
{
|
||||
var maxSlot = shader.ConstantBuffers.Max(cb => cb.RegisterSlot);
|
||||
_cbufferCaches = new CBufferCache[maxSlot + 1];
|
||||
|
||||
foreach (var cbufferInfo in shader.ConstantBuffers)
|
||||
{
|
||||
_cbufferCaches[cbufferInfo.RegisterSlot] = new CBufferCache(cbufferInfo.Size);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_cbufferCaches = Array.Empty<CBufferCache>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a float property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetFloat(int propertyId, in float value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a float property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetFloat(string propertyName, in float value)
|
||||
{
|
||||
SetFloat(Shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a uint property in the material's constant buffer (useful for texture indices).
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetUInt(int propertyId, in uint value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a uint property in the material's constant buffer (useful for texture indices).
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetUInt(string propertyName, in uint value)
|
||||
{
|
||||
SetUInt(Shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Vector property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetVector(int propertyId, in Vector4 value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Vector property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetVector(string propertyName, in Vector4 value)
|
||||
{
|
||||
SetVector(Shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Matrix property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetMatrix(int propertyId, in Matrix4x4 value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Matrix property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetMatrix(string propertyName, in Matrix4x4 value)
|
||||
{
|
||||
SetMatrix(Shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a texture index for a shader property (for bindless texture access)
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the shader property (e.g., "_TextureIndex1")</param>
|
||||
/// <param name="texture">The bindless texture to reference</param>
|
||||
public void SetTexture(string propertyName, Texture2D texture)
|
||||
{
|
||||
SetUInt(propertyName, texture.DescriptorIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the mesh buffer indices for bindless vertex and index buffer access
|
||||
/// </summary>
|
||||
/// <param name="mesh">The mesh whose buffer indices to set</param>
|
||||
/// <param name="vertexBufferIndexProperty">The name of the vertex buffer index property (e.g., "_VertexBufferIndex")</param>
|
||||
/// <param name="indexBufferIndexProperty">The name of the index buffer index property (e.g., "_IndexBufferIndex")</param>
|
||||
public void SetMeshBuffer(Mesh mesh, string vertexBufferIndexProperty = "_VertexBufferIndex", string indexBufferIndexProperty = "_IndexBufferIndex")
|
||||
{
|
||||
SetUInt(vertexBufferIndexProperty, mesh.VertexBufferDescriptorIndex);
|
||||
SetUInt(indexBufferIndexProperty, mesh.IndexBufferDescriptorIndex);
|
||||
}
|
||||
|
||||
private unsafe void WriteToCache<T>(int propertyId, in T value)
|
||||
where T : unmanaged
|
||||
{
|
||||
if (propertyId == -1)
|
||||
{
|
||||
throw new ArgumentException("Property ID is invalid.");
|
||||
}
|
||||
|
||||
var propInfo = Shader.Properties[propertyId];
|
||||
if (propInfo.Size != sizeof(T))
|
||||
{
|
||||
throw new ArgumentException($"Property '{propInfo.Name}' has a size mismatch. Expected {propInfo.Size} bytes, but got {sizeof(T)} bytes.");
|
||||
}
|
||||
|
||||
var cache = _cbufferCaches[propInfo.CBufferIndex];
|
||||
|
||||
Unsafe.WriteUnaligned(ref cache.CpuData[(int)propInfo.ByteOffset], value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uploads the material data to the GPU.
|
||||
/// </summary>
|
||||
public void UploadMaterialData()
|
||||
{
|
||||
foreach (var cache in _cbufferCaches)
|
||||
{
|
||||
cache.UploadToGpu();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var cache in _cbufferCaches)
|
||||
{
|
||||
cache.Dispose();
|
||||
}
|
||||
|
||||
// NOTE: We don't dispose the textures here as they might be shared
|
||||
// The user is responsible for disposing BindlessTexture2D instances
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
376
Ghost.Graphics/Data/MaterialClass.cs
Normal file
376
Ghost.Graphics/Data/MaterialClass.cs
Normal file
@@ -0,0 +1,376 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Ghost.Graphics.Data;
|
||||
|
||||
public struct Material
|
||||
{
|
||||
internal UnsafeArray<CBufferCache> _cBufferCaches;
|
||||
|
||||
public Identifier<Shader> Shader
|
||||
{
|
||||
get; internal set;
|
||||
}
|
||||
}
|
||||
|
||||
public ref struct MaterialAccessor
|
||||
{
|
||||
private ref Material _materialData;
|
||||
private ref Shader _shader;
|
||||
|
||||
public MaterialAccessor(ref Material materialData, IResourceDatabase resourceDatabase)
|
||||
{
|
||||
_materialData = ref materialData;
|
||||
_shader = ref resourceDatabase.GetShaderReference(materialData.Shader);
|
||||
}
|
||||
|
||||
private readonly unsafe void WriteToCache<T>(int propertyId, in T value)
|
||||
where T : unmanaged
|
||||
{
|
||||
if (propertyId == -1)
|
||||
{
|
||||
throw new ArgumentException("Property ID is invalid.");
|
||||
}
|
||||
|
||||
var propInfo = _shader.Properties[propertyId];
|
||||
if (propInfo.Size != sizeof(T))
|
||||
{
|
||||
throw new ArgumentException($"Property '{propertyId}' has a size mismatch. Expected {propInfo.Size} bytes, but got {sizeof(T)} bytes.");
|
||||
}
|
||||
|
||||
var cache = _materialData._cBufferCaches[propInfo.CBufferIndex];
|
||||
|
||||
Unsafe.WriteUnaligned(ref cache.CpuData[(int)propInfo.ByteOffset], value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a float property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetFloat(int propertyId, in float value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a float property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetFloat(string propertyName, in float value)
|
||||
{
|
||||
SetFloat(_shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a uint property in the material's constant buffer (useful for texture indices).
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetUInt(int propertyId, in uint value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a uint property in the material's constant buffer (useful for texture indices).
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetUInt(string propertyName, in uint value)
|
||||
{
|
||||
SetUInt(_shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Vector property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetVector(int propertyId, in Vector4 value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Vector property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetVector(string propertyName, in Vector4 value)
|
||||
{
|
||||
SetVector(_shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Matrix property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetMatrix(int propertyId, in Matrix4x4 value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Matrix property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetMatrix(string propertyName, in Matrix4x4 value)
|
||||
{
|
||||
SetMatrix(_shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a texture index for a shader property (for bindless texture access)
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the shader property (e.g., "_TextureIndex1")</param>
|
||||
/// <param name="texture">The bindless texture to reference</param>
|
||||
public void SetTexture(string propertyName, Texture2D texture)
|
||||
{
|
||||
SetUInt(propertyName, texture.DescriptorIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the mesh buffer indices for bindless vertex and index buffer access
|
||||
/// </summary>
|
||||
/// <param name="mesh">The mesh whose buffer indices to set</param>
|
||||
/// <param name="vertexBufferIndexProperty">The name of the vertex buffer index property (e.g., "_VertexBufferIndex")</param>
|
||||
/// <param name="indexBufferIndexProperty">The name of the index buffer index property (e.g., "_IndexBufferIndex")</param>
|
||||
public void SetMeshBuffer(MeshClass mesh, string vertexBufferIndexProperty = "_VertexBufferIndex", string indexBufferIndexProperty = "_IndexBufferIndex")
|
||||
{
|
||||
SetUInt(vertexBufferIndexProperty, mesh.VertexBufferDescriptorIndex);
|
||||
SetUInt(indexBufferIndexProperty, mesh.IndexBufferDescriptorIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uploads all cached material data to the GPU using the specified command buffer.
|
||||
/// </summary>
|
||||
/// <param name="cmb">The command buffer used to perform the upload operations to the GPU. Cannot be null.</param>
|
||||
public readonly void UploadMaterialData(ICommandBuffer cmb)
|
||||
{
|
||||
foreach (var cache in _materialData._cBufferCaches)
|
||||
{
|
||||
cmb.Upload(cache.GpuResource, cache.CpuData.AsSpan());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Material implementation for bindless rendering with SM 6.6 support
|
||||
/// </summary>
|
||||
public unsafe class MaterialClass : IDisposable
|
||||
{
|
||||
private readonly UnsafeArray<CBufferCache> _cbufferCaches;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
internal ReadOnlySpan<CBufferCache> CBufferCaches => _cbufferCaches.AsSpan();
|
||||
|
||||
public Identifier<Shader> Shader
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public MaterialClass(Identifier<Shader> shader, IResourceAllocator resourceAllocator, IResourceDatabase resourceDatabase)
|
||||
{
|
||||
Shader = shader;
|
||||
|
||||
var shaderResource = resourceDatabase.GetShader(shader);
|
||||
|
||||
if (shaderResource.ConstantBuffers.Count > 0)
|
||||
{
|
||||
var maxSlot = shaderResource.ConstantBuffers.Max(cb => cb.RegisterSlot);
|
||||
_cbufferCaches = new UnsafeArray<CBufferCache>((int)maxSlot + 1, Allocator.Persistent);
|
||||
|
||||
foreach (var cbufferInfo in shaderResource.ConstantBuffers)
|
||||
{
|
||||
var desc = new BufferDesc
|
||||
{
|
||||
Size = cbufferInfo.Size,
|
||||
Usage = BufferUsage.Constant,
|
||||
MemoryType = MemoryType.Default,
|
||||
};
|
||||
|
||||
var buffer = resourceAllocator.CreateBuffer(in desc);
|
||||
|
||||
_cbufferCaches[cbufferInfo.RegisterSlot] = new CBufferCache(buffer, cbufferInfo.Size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a float property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetFloat(int propertyId, in float value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a float property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetFloat(string propertyName, in float value)
|
||||
{
|
||||
SetFloat(Shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a uint property in the material's constant buffer (useful for texture indices).
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetUInt(int propertyId, in uint value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a uint property in the material's constant buffer (useful for texture indices).
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetUInt(string propertyName, in uint value)
|
||||
{
|
||||
SetUInt(Shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Vector property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetVector(int propertyId, in Vector4 value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Vector property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetVector(string propertyName, in Vector4 value)
|
||||
{
|
||||
SetVector(Shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Matrix property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyId">The ID of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetMatrix(int propertyId, in Matrix4x4 value)
|
||||
{
|
||||
WriteToCache(propertyId, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Matrix property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetMatrix(string propertyName, in Matrix4x4 value)
|
||||
{
|
||||
SetMatrix(Shader.GetPropertyId(propertyName), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a texture index for a shader property (for bindless texture access)
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the shader property (e.g., "_TextureIndex1")</param>
|
||||
/// <param name="texture">The bindless texture to reference</param>
|
||||
public void SetTexture(string propertyName, Texture2D texture)
|
||||
{
|
||||
SetUInt(propertyName, texture.DescriptorIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the mesh buffer indices for bindless vertex and index buffer access
|
||||
/// </summary>
|
||||
/// <param name="mesh">The mesh whose buffer indices to set</param>
|
||||
/// <param name="vertexBufferIndexProperty">The name of the vertex buffer index property (e.g., "_VertexBufferIndex")</param>
|
||||
/// <param name="indexBufferIndexProperty">The name of the index buffer index property (e.g., "_IndexBufferIndex")</param>
|
||||
public void SetMeshBuffer(MeshClass mesh, string vertexBufferIndexProperty = "_VertexBufferIndex", string indexBufferIndexProperty = "_IndexBufferIndex")
|
||||
{
|
||||
SetUInt(vertexBufferIndexProperty, mesh.VertexBufferDescriptorIndex);
|
||||
SetUInt(indexBufferIndexProperty, mesh.IndexBufferDescriptorIndex);
|
||||
}
|
||||
|
||||
private unsafe void WriteToCache<T>(int propertyId, in T value)
|
||||
where T : unmanaged
|
||||
{
|
||||
if (propertyId == -1)
|
||||
{
|
||||
throw new ArgumentException("Property ID is invalid.");
|
||||
}
|
||||
|
||||
var propInfo = Shader.Properties[propertyId];
|
||||
if (propInfo.Size != sizeof(T))
|
||||
{
|
||||
throw new ArgumentException($"Property '{propInfo.Name}' has a size mismatch. Expected {propInfo.Size} bytes, but got {sizeof(T)} bytes.");
|
||||
}
|
||||
|
||||
var cache = _cbufferCaches[propInfo.CBufferIndex];
|
||||
|
||||
Unsafe.WriteUnaligned(ref cache.CpuData[(int)propInfo.ByteOffset], value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uploads the material data to the GPU.
|
||||
/// </summary>
|
||||
public void UploadMaterialData()
|
||||
{
|
||||
foreach (var cache in _cbufferCaches)
|
||||
{
|
||||
cache.UploadToGpu();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var cache in _cbufferCaches)
|
||||
{
|
||||
cache.Dispose();
|
||||
}
|
||||
|
||||
// NOTE: We don't dispose the textures here as they might be shared
|
||||
// The user is responsible for disposing BindlessTexture2D instances
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
@@ -2,82 +2,68 @@
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Helpers;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using Misaki.HighPerformance.Mathematics.Geometry;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Win32.Graphics.Direct3D12;
|
||||
using Win32.Graphics.Dxgi.Common;
|
||||
|
||||
namespace Ghost.Graphics.Data;
|
||||
|
||||
/// <summary>
|
||||
/// The CPU-side mesh data structure.
|
||||
/// </summary>
|
||||
public struct MeshData
|
||||
public struct Mesh
|
||||
{
|
||||
public UnsafeList<Vertex> vertices;
|
||||
public UnsafeList<int> indices;
|
||||
public UnsafeList<uint> indices;
|
||||
public AABB boundingBox;
|
||||
public MeshHandle handle;
|
||||
|
||||
public MeshData()
|
||||
{
|
||||
handle = MeshHandle.Invalid;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The GPU-side mesh handle containing buffer references.
|
||||
/// </summary>
|
||||
public struct MeshHandle
|
||||
{
|
||||
public BufferHandle vertexBuffer;
|
||||
public BufferHandle indexBuffer;
|
||||
|
||||
public static MeshHandle Invalid => new() { vertexBuffer = BufferHandle.Invalid, indexBuffer = BufferHandle.Invalid };
|
||||
|
||||
public readonly bool IsValid => vertexBuffer.IsValid && indexBuffer.IsValid;
|
||||
}
|
||||
|
||||
public struct BatchMeshID : IEquatable<BatchMeshID>
|
||||
{
|
||||
public int value;
|
||||
|
||||
public static BatchMeshID Null => new() { value = -1 };
|
||||
|
||||
public readonly override int GetHashCode()
|
||||
public Mesh()
|
||||
{
|
||||
return value.GetHashCode();
|
||||
vertexBuffer = BufferHandle.Invalid;
|
||||
indexBuffer = BufferHandle.Invalid;
|
||||
}
|
||||
|
||||
public readonly override bool Equals(object? obj)
|
||||
internal Mesh(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<uint> indices, BufferHandle vertexBuffer, BufferHandle indexBuffer)
|
||||
{
|
||||
return obj is BatchMeshID id && Equals(id);
|
||||
this.vertices = new(vertices.Length, Allocator.Persistent);
|
||||
this.indices = new(indices.Length, Allocator.Persistent);
|
||||
this.vertices.CopyFrom(vertices);
|
||||
this.indices.CopyFrom(indices);
|
||||
this.vertexBuffer = vertexBuffer;
|
||||
this.indexBuffer = indexBuffer;
|
||||
|
||||
ComputeBounds();
|
||||
}
|
||||
|
||||
public readonly bool Equals(BatchMeshID other)
|
||||
public void ComputeBounds()
|
||||
{
|
||||
return value == other.value;
|
||||
if (vertices.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var min = new float3(float.MaxValue);
|
||||
var max = new float3(float.MinValue);
|
||||
foreach (var vertex in vertices)
|
||||
{
|
||||
var pos = vertex.position.xyz;
|
||||
min = math.min(min, pos);
|
||||
max = math.max(max, pos);
|
||||
}
|
||||
|
||||
boundingBox = new AABB(min, max);
|
||||
}
|
||||
|
||||
public readonly int CompareTo(BatchMeshID other)
|
||||
public void ReleaseCpuResources()
|
||||
{
|
||||
return value.CompareTo(other.value);
|
||||
}
|
||||
|
||||
public static bool operator ==(BatchMeshID a, BatchMeshID b)
|
||||
{
|
||||
return a.Equals(b);
|
||||
}
|
||||
|
||||
public static bool operator !=(BatchMeshID a, BatchMeshID b)
|
||||
{
|
||||
return !a.Equals(b);
|
||||
vertices.Dispose();
|
||||
indices.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe sealed class Mesh : IDisposable
|
||||
public unsafe sealed class MeshClass : IDisposable
|
||||
{
|
||||
private UnsafeList<Vertex> _vertices;
|
||||
private UnsafeList<int> _indices;
|
||||
@@ -132,13 +118,13 @@ public unsafe sealed class Mesh : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
public Mesh(int initialVertexCapacity = 256, int initialIndexCapacity = 512)
|
||||
public MeshClass(int initialVertexCapacity = 256, int initialIndexCapacity = 512)
|
||||
{
|
||||
_vertices = new(initialVertexCapacity, Allocator.Persistent);
|
||||
_indices = new(initialIndexCapacity, Allocator.Persistent);
|
||||
}
|
||||
|
||||
public Mesh(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<int> indices)
|
||||
public MeshClass(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<int> indices)
|
||||
: this(vertices.Length, indices.Length)
|
||||
{
|
||||
_vertices = new(vertices.Length, Allocator.Persistent);
|
||||
@@ -148,7 +134,7 @@ public unsafe sealed class Mesh : IDisposable
|
||||
_indices.CopyFrom(indices);
|
||||
}
|
||||
|
||||
~Mesh()
|
||||
~MeshClass()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
@@ -233,18 +219,18 @@ public unsafe sealed class Mesh : IDisposable
|
||||
var v1 = _vertices[i1];
|
||||
var v2 = _vertices[i2];
|
||||
|
||||
var edge1 = v1.Position - v0.Position;
|
||||
var edge2 = v2.Position - v0.Position;
|
||||
var faceNormal = Vector3.Cross(edge1.AsVector3(), edge2.AsVector3());
|
||||
var edge1 = v1.position - v0.position;
|
||||
var edge2 = v2.position - v0.position;
|
||||
var faceNormal = math.cross(edge1.xyz, edge2.xyz);
|
||||
|
||||
_vertices[i0].Normal += faceNormal.AsVector4();
|
||||
_vertices[i1].Normal += faceNormal.AsVector4();
|
||||
_vertices[i2].Normal += faceNormal.AsVector4();
|
||||
_vertices[i0].normal.xyz += faceNormal;
|
||||
_vertices[i1].normal.xyz += faceNormal;
|
||||
_vertices[i2].normal.xyz += faceNormal;
|
||||
}
|
||||
|
||||
for (var i = 0; i < _vertices.Count; i++)
|
||||
{
|
||||
_vertices[i].Normal = Vector4.Normalize(_vertices[i].Normal);
|
||||
_vertices[i].normal = math.normalize(_vertices[i].normal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,7 +242,7 @@ public unsafe sealed class Mesh : IDisposable
|
||||
/// </remarks>
|
||||
public void ComputeTangents()
|
||||
{
|
||||
var bitangents = new Vector4[_vertices.Count];
|
||||
var bitangents = new float4[_vertices.Count];
|
||||
|
||||
for (var i = 0; i < _indices.Count; i += 3)
|
||||
{
|
||||
@@ -268,29 +254,24 @@ public unsafe sealed class Mesh : IDisposable
|
||||
var v1 = _vertices[i1];
|
||||
var v2 = _vertices[i2];
|
||||
|
||||
var uv0 = _vertices[i0].UV;
|
||||
var uv1 = _vertices[i1].UV;
|
||||
var uv2 = _vertices[i2].UV;
|
||||
var uv0 = _vertices[i0].uv;
|
||||
var uv1 = _vertices[i1].uv;
|
||||
var uv2 = _vertices[i2].uv;
|
||||
|
||||
var deltaPos1 = v1.Position - v0.Position;
|
||||
var deltaPos2 = v2.Position - v0.Position;
|
||||
var deltaPos1 = v1.position - v0.position;
|
||||
var deltaPos2 = v2.position - v0.position;
|
||||
var deltaUV1 = uv1 - uv0;
|
||||
var deltaUV2 = uv2 - uv0;
|
||||
|
||||
var r = 1.0f / (deltaUV1.X * deltaUV2.Y - deltaUV1.Y * deltaUV2.X);
|
||||
var tangent = (deltaPos1 * deltaUV2.Y - deltaPos2 * deltaUV1.Y) * r;
|
||||
var bitangent = (deltaPos2 * deltaUV1.X - deltaPos1 * deltaUV2.X) * r;
|
||||
var r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
|
||||
var tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
|
||||
var bitangent = (deltaPos2 * deltaUV1.x - deltaPos1 * deltaUV2.x) * r;
|
||||
|
||||
for (var j = 0; j < 3; j++)
|
||||
{
|
||||
var idx = _indices[i + j];
|
||||
var t = _vertices[idx].Tangent;
|
||||
_vertices[idx].Tangent = new Vector4(
|
||||
t.X + tangent.X,
|
||||
t.Y + tangent.Y,
|
||||
t.Z + tangent.Z,
|
||||
0.0f // we’ll fill w later
|
||||
);
|
||||
var t = _vertices[idx].tangent;
|
||||
_vertices[idx].tangent.xyz = t.xyz + tangent.xyz;
|
||||
|
||||
bitangents[idx] += bitangent;
|
||||
}
|
||||
@@ -298,18 +279,16 @@ public unsafe sealed class Mesh : IDisposable
|
||||
|
||||
for (var i = 0; i < _vertices.Count; i++)
|
||||
{
|
||||
var n = _vertices[i].Normal;
|
||||
var t = _vertices[i].Tangent;
|
||||
var n3 = n.AsVector3();
|
||||
var t3 = t.AsVector3();
|
||||
var n = _vertices[i].normal;
|
||||
var t = _vertices[i].tangent;
|
||||
|
||||
var proj = n3 * Vector3.Dot(n3, t3);
|
||||
t3 = Vector3.Normalize(t3 - proj);
|
||||
var proj = n * math.dot(n, t);
|
||||
t = math.normalize(t - proj);
|
||||
|
||||
var b = bitangents[i];
|
||||
var w = Vector3.Dot(Vector3.Cross(n3, t3), b.AsVector3()) < 0.0f ? -1.0f : 1.0f;
|
||||
var w = math.dot(math.cross(n.xyz, t.xyz), b.xyz) < 0.0f ? -1.0f : 1.0f;
|
||||
|
||||
_vertices[i].Tangent = new Vector4(t3.X, t3.Y, t3.Z, w);
|
||||
_vertices[i].tangent = new float4(t.x, t.y, t.z, w);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -320,20 +299,20 @@ public unsafe sealed class Mesh : IDisposable
|
||||
{
|
||||
if (_vertices.Count == 0)
|
||||
{
|
||||
_boundingBox = Bounds.Zero;
|
||||
_boundingBox = AABB.Zero;
|
||||
return;
|
||||
}
|
||||
|
||||
var min = new Vector3(float.MaxValue);
|
||||
var max = new Vector3(float.MinValue);
|
||||
var min = new float3(float.MaxValue);
|
||||
var max = new float3(float.MinValue);
|
||||
foreach (var vertex in _vertices)
|
||||
{
|
||||
var pos = vertex.Position.AsVector3();
|
||||
min = Vector3.Min(min, pos);
|
||||
max = Vector3.Max(max, pos);
|
||||
var pos = vertex.position.xyz;
|
||||
min = math.min(min, pos);
|
||||
max = math.max(max, pos);
|
||||
}
|
||||
|
||||
_boundingBox = new Bounds(min, max);
|
||||
_boundingBox = new AABB(min, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -206,7 +206,7 @@ public unsafe class RenderTexture : Texture
|
||||
/// </summary>
|
||||
/// <param name="commandList">Command list to record clear commands</param>
|
||||
/// <param name="clearColor">Color to clear to</param>
|
||||
public void ClearColor(CommandList commandList, Color16 clearColor)
|
||||
public void ClearColor(CommandList commandList, Color128 clearColor)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ public readonly struct ResourceHandle : IEquatable<ResourceHandle>
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
return (id * 397) ^ (int)generation;
|
||||
return (id * 397) ^ generation;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ public readonly struct ResourceHandle : IEquatable<ResourceHandle>
|
||||
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);
|
||||
@@ -59,13 +60,20 @@ public readonly struct TextureHandle : IEquatable<TextureHandle>
|
||||
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);
|
||||
return _resourceHandle.Equals(other._resourceHandle) && _bindlessDescriptor.Equals(other._bindlessDescriptor);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
@@ -75,7 +83,7 @@ public readonly struct TextureHandle : IEquatable<TextureHandle>
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _resourceHandle.GetHashCode();
|
||||
return HashCode.Combine(_resourceHandle, _bindlessDescriptor);
|
||||
}
|
||||
|
||||
public static bool operator ==(TextureHandle left, TextureHandle right)
|
||||
@@ -115,7 +123,7 @@ public readonly struct BufferHandle : IEquatable<BufferHandle>
|
||||
|
||||
public bool Equals(BufferHandle other)
|
||||
{
|
||||
return _resourceHandle.Equals(other._resourceHandle);
|
||||
return _resourceHandle.Equals(other._resourceHandle) && _bindlessDescriptor.Equals(other._bindlessDescriptor);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
@@ -125,7 +133,7 @@ public readonly struct BufferHandle : IEquatable<BufferHandle>
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _resourceHandle.GetHashCode();
|
||||
return HashCode.Combine(_resourceHandle, _bindlessDescriptor);
|
||||
}
|
||||
|
||||
public static bool operator ==(BufferHandle left, BufferHandle right)
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
|
||||
namespace Ghost.Graphics.Data;
|
||||
|
||||
internal readonly struct TextureInfo
|
||||
{
|
||||
public required string Name
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
public uint RegisterSlot
|
||||
{
|
||||
get; init;
|
||||
@@ -20,11 +18,6 @@ internal readonly struct TextureInfo
|
||||
|
||||
internal readonly struct PropertyInfo
|
||||
{
|
||||
public required string Name
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
public uint CBufferIndex
|
||||
{
|
||||
get; init;
|
||||
@@ -43,11 +36,6 @@ internal readonly struct PropertyInfo
|
||||
|
||||
internal readonly struct CBufferInfo
|
||||
{
|
||||
public required string Name
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
public uint Size
|
||||
{
|
||||
get; init;
|
||||
@@ -64,36 +52,44 @@ internal readonly struct CBufferInfo
|
||||
/// </summary>
|
||||
|
||||
// TODO: Multi pass and keyword support
|
||||
public unsafe class Shader : IDisposable
|
||||
public struct Shader : IDisposable
|
||||
{
|
||||
private readonly string _source;
|
||||
|
||||
private readonly List<CBufferInfo> _constantBuffers = new();
|
||||
private readonly List<PropertyInfo> _properties = new();
|
||||
private readonly List<TextureInfo> _regularTextures = new(); // Add regular texture support
|
||||
private readonly Dictionary<string, int> _propertyNameToIdMap = new();
|
||||
private UnsafeList<CBufferInfo> _constantBuffers;
|
||||
private UnsafeList<PropertyInfo> _properties;
|
||||
private UnsafeList<TextureInfo> _regularTextures; // Add regular texture support
|
||||
// TODO: Possible to move this to unmanaged heap?
|
||||
private Dictionary<string, int> _propertyNameToIdMap;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
internal string Source => _source;
|
||||
internal readonly UnsafeList<CBufferInfo> ConstantBuffers => _constantBuffers;
|
||||
internal readonly UnsafeList<PropertyInfo> Properties => _properties;
|
||||
internal readonly UnsafeList<TextureInfo> RegularTextures => _regularTextures;
|
||||
internal readonly Dictionary<string, int> PropertyNameToIdMap => _propertyNameToIdMap;
|
||||
|
||||
internal List<CBufferInfo> ConstantBuffers => _constantBuffers;
|
||||
internal List<PropertyInfo> Properties => _properties;
|
||||
internal List<TextureInfo> RegularTextures => _regularTextures;
|
||||
internal Dictionary<string, int> PropertyNameToIdMap => _propertyNameToIdMap;
|
||||
|
||||
// TODO: In real production, we should not load the shader source code directly.
|
||||
internal Shader(string shaderCode)
|
||||
public Shader()
|
||||
{
|
||||
_source = shaderCode;
|
||||
_constantBuffers = new(8, Allocator.Persistent);
|
||||
_properties = new(8, Allocator.Persistent);
|
||||
_regularTextures = new(8, Allocator.Persistent);
|
||||
_propertyNameToIdMap = new(8);
|
||||
|
||||
_disposed = false;
|
||||
}
|
||||
|
||||
internal void AddProperty(string name, PropertyInfo propertyInfo)
|
||||
{
|
||||
var id = _properties.Count;
|
||||
_properties.Add(propertyInfo);
|
||||
_propertyNameToIdMap[name] = id;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a unique, stable ID for a shader property.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property (e.g., "_Color").</param>
|
||||
/// <param name="propertyName">The name of the shader property.</param>
|
||||
/// <returns>The integer ID of the property, or -1 if not found.</returns>
|
||||
public int GetPropertyId(string propertyName)
|
||||
public readonly int GetPropertyId(string propertyName)
|
||||
{
|
||||
return _propertyNameToIdMap.TryGetValue(propertyName, out var id) ? id : -1;
|
||||
}
|
||||
@@ -105,12 +101,12 @@ public unsafe class Shader : IDisposable
|
||||
return;
|
||||
}
|
||||
|
||||
_constantBuffers.Clear();
|
||||
_properties.Clear();
|
||||
_propertyNameToIdMap.Clear();
|
||||
_regularTextures.Clear();
|
||||
_constantBuffers.Dispose();
|
||||
_properties.Dispose();
|
||||
_regularTextures.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
_propertyNameToIdMap.Clear();
|
||||
_propertyNameToIdMap = null!;
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Win32.Graphics.Dxgi.Common;
|
||||
|
||||
namespace Ghost.Graphics.Data;
|
||||
@@ -9,59 +8,21 @@ namespace Ghost.Graphics.Data;
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Vertex
|
||||
{
|
||||
public unsafe struct Semantic
|
||||
public unsafe static class Semantic
|
||||
{
|
||||
public const Format ALIGNED_FORMAT = Format.R32G32B32A32Float;
|
||||
public const int COUNT = 5;
|
||||
|
||||
private static readonly byte[] s_positionBytes = Encoding.UTF8.GetBytes("POSITION");
|
||||
private static readonly byte[] s_normalBytes = Encoding.UTF8.GetBytes("NORMAL");
|
||||
private static readonly byte[] s_tangentBytes = Encoding.UTF8.GetBytes("TANGENT");
|
||||
private static readonly byte[] s_colorBytes = Encoding.UTF8.GetBytes("COLOR");
|
||||
private static readonly byte[] s_uvBytes = Encoding.UTF8.GetBytes("TEXCOORD");
|
||||
|
||||
public static byte* pPositionName => (byte*)Unsafe.AsPointer(ref s_positionBytes[0]);
|
||||
public static byte* pNormalName => (byte*)Unsafe.AsPointer(ref s_normalBytes[0]);
|
||||
public static byte* pTangentName => (byte*)Unsafe.AsPointer(ref s_tangentBytes[0]);
|
||||
public static byte* pColorName => (byte*)Unsafe.AsPointer(ref s_colorBytes[0]);
|
||||
public static byte* pUVName => (byte*)Unsafe.AsPointer(ref s_uvBytes[0]);
|
||||
public static readonly FixedString32 position = new("POSITION");
|
||||
public static readonly FixedString32 normal = new("NORMAL");
|
||||
public static readonly FixedString32 tangent = new("TANGENT");
|
||||
public static readonly FixedString32 uv = new("TEXCOORD");
|
||||
public static readonly FixedString32 color = new("COLOR");
|
||||
}
|
||||
|
||||
public float4 Position
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public float4 Normal
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public float4 Tangent
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public Color16 Color
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public float4 UV
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public Vertex(float4 position, float4 normal, float4 tangent, Color16 color, float4 uv)
|
||||
{
|
||||
Position = position;
|
||||
Normal = normal;
|
||||
Tangent = tangent;
|
||||
Color = color;
|
||||
UV = uv;
|
||||
}
|
||||
public float4 position;
|
||||
public float4 normal;
|
||||
public float4 tangent;
|
||||
public float4 uv;
|
||||
public Color128 color;
|
||||
}
|
||||
Reference in New Issue
Block a user