forked from Misaki/GhostEngine
Refactor and optimize rendering pipeline
- Added `<IsTrimmable>` property in project files for trimming. - Replaced bindless texture types with non-bindless equivalents. - Refactored `ShaderDescriptor` and `ShaderPass` for better modularity. - Introduced `ShaderDescriptorExtensions` for property size calculations. - Simplified constant buffer handling in `Material.cs`. - Improved resource management in `D3D12` components. - Added support for static meshes and optimized resource barriers. - Refactored shader code generation and property merging in `SDLCompiler`. - Removed unused or redundant code (e.g., `IncludesBlock` parser). - Updated comments, documentation, and error handling for clarity.
This commit is contained in:
@@ -2,8 +2,6 @@ using Ghost.Core;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
@@ -12,29 +10,38 @@ internal struct CBufferCache : IResourceReleasable
|
||||
{
|
||||
private UnsafeArray<byte> _cpuData;
|
||||
private Handle<GraphicsBuffer> _gpuResource;
|
||||
private uint _size;
|
||||
private uint _alignedSize;
|
||||
|
||||
public readonly UnsafeArray<byte> CpuData => _cpuData;
|
||||
public readonly Handle<GraphicsBuffer> GpuResource => _gpuResource;
|
||||
public readonly uint Size => _size;
|
||||
public readonly uint AlignedSize => _alignedSize;
|
||||
|
||||
public readonly bool IsCreated => _gpuResource.IsValid && _cpuData.IsCreated;
|
||||
public readonly bool IsCreated => _size != 0 && _gpuResource.IsValid && _cpuData.IsCreated;
|
||||
|
||||
public CBufferCache(Handle<GraphicsBuffer> buffer, uint bufferSize)
|
||||
{
|
||||
_size = bufferSize;
|
||||
_alignedSize = (bufferSize + 255u) & ~255u;
|
||||
|
||||
_cpuData = new((int)AlignedSize, Allocator.Persistent);
|
||||
_cpuData = new UnsafeArray<byte>((int)AlignedSize, Allocator.Persistent);
|
||||
_gpuResource = buffer;
|
||||
}
|
||||
|
||||
public void ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
if (!IsCreated)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_cpuData.Dispose();
|
||||
|
||||
database.ReleaseResource(GpuResource.AsResource());
|
||||
_gpuResource = Handle<GraphicsBuffer>.Invalid;
|
||||
|
||||
_size = 0;
|
||||
_alignedSize = 0;
|
||||
}
|
||||
}
|
||||
@@ -42,183 +49,96 @@ internal struct CBufferCache : IResourceReleasable
|
||||
public struct Material : IResourceReleasable, IHandleType
|
||||
{
|
||||
private Identifier<Shader> _shader;
|
||||
private UnsafeArray<CBufferCache> _materialPropertiesCache; // One per shader pass
|
||||
private CBufferCache _cBufferCache;
|
||||
|
||||
internal readonly CBufferCache CBufferCache => _cBufferCache;
|
||||
|
||||
public readonly Identifier<Shader> Shader => _shader;
|
||||
|
||||
internal ref CBufferCache GetPassCache(int passIndex)
|
||||
{
|
||||
return ref _materialPropertiesCache[passIndex];
|
||||
}
|
||||
|
||||
public void SetShader(Identifier<Shader> shaderId, IResourceAllocator allocator, IResourceDatabase database)
|
||||
public Result SetShader(Identifier<Shader> shaderId, IResourceAllocator allocator, IResourceDatabase database)
|
||||
{
|
||||
if (!shaderId.IsValid)
|
||||
{
|
||||
throw new ArgumentException("Shader ID is invalid.");
|
||||
return Result.Failure("Shader ID is invalid.");
|
||||
}
|
||||
|
||||
_cBufferCache.ReleaseResource(database);
|
||||
_shader = shaderId;
|
||||
|
||||
var shader = database.GetShaderReference(shaderId);
|
||||
_materialPropertiesCache = new UnsafeArray<CBufferCache>(shader.PassCount, Allocator.Persistent);
|
||||
for (var i = 0; i < shader.PassCount; i++)
|
||||
if (shader.CBufferSize != 0)
|
||||
{
|
||||
var pass = database.GetShaderPass(shader.GetPassKey(i));
|
||||
var cbufferInfo = pass.CBuffer;
|
||||
|
||||
if (cbufferInfo.SizeInBytes == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var desc = new BufferDesc
|
||||
{
|
||||
Size = cbufferInfo.SizeInBytes,
|
||||
Size = shader.CBufferSize,
|
||||
Usage = BufferUsage.Constant,
|
||||
MemoryType = ResourceMemoryType.Default,
|
||||
};
|
||||
|
||||
var buffer = allocator.CreateBuffer(ref desc);
|
||||
_materialPropertiesCache[i] = new CBufferCache(buffer, cbufferInfo.SizeInBytes);
|
||||
}
|
||||
}
|
||||
|
||||
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
foreach (var cache in _materialPropertiesCache)
|
||||
{
|
||||
cache.ReleaseResource(database);
|
||||
_cBufferCache = new CBufferCache(buffer, shader.CBufferSize);
|
||||
}
|
||||
|
||||
_materialPropertiesCache.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public ref struct MaterialAccessor
|
||||
{
|
||||
private ref Material _materialData;
|
||||
private readonly Shader _shader;
|
||||
|
||||
private readonly IResourceDatabase _resourceDatabase;
|
||||
|
||||
public MaterialAccessor(Handle<Material> material, IResourceDatabase resourceDatabase)
|
||||
{
|
||||
_resourceDatabase = resourceDatabase;
|
||||
|
||||
_materialData = ref resourceDatabase.GetMaterialReference(material);
|
||||
_shader = resourceDatabase.GetShaderReference(_materialData.Shader);
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
private readonly unsafe void WriteToCache<T>(string propertyName, in T value)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly unsafe Result<T, ResultStatus> GetPropertyCache<T>()
|
||||
where T : unmanaged
|
||||
{
|
||||
foreach (var index in _shader.GetPropertyPassIndices(propertyName))
|
||||
if (sizeof(T) != _cBufferCache.Size)
|
||||
{
|
||||
var passKey = _shader.GetPassKey(index);
|
||||
var pass = _resourceDatabase.GetShaderPass(passKey);
|
||||
var propertyInfo = pass.GetPropertyInfo(propertyName);
|
||||
|
||||
if (propertyInfo.Size != sizeof(T))
|
||||
{
|
||||
throw new ArgumentException($"Property '{propertyName}' has a size mismatch. Expected {propertyInfo.Size} bytes, but got {sizeof(T)} bytes.");
|
||||
}
|
||||
|
||||
ref var cache = ref _materialData.GetPassCache(index);
|
||||
Unsafe.WriteUnaligned(ref cache.CpuData[propertyInfo.StartOffset], 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 readonly void SetFloat(string propertyName, in float value)
|
||||
{
|
||||
WriteToCache(propertyName, 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 readonly void SetUInt(string propertyName, in uint value)
|
||||
{
|
||||
WriteToCache(propertyName, 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 readonly void SetVector(string propertyName, in float4 value)
|
||||
{
|
||||
WriteToCache(propertyName, 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 readonly void SetMatrix(string propertyName, in float4x4 value)
|
||||
{
|
||||
WriteToCache(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>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetTextureBindless(string propertyName, Handle<Texture> texture)
|
||||
{
|
||||
var bindlessIndex = _resourceDatabase.GetBindlessIndex(texture.AsResource());
|
||||
if (bindlessIndex == -1)
|
||||
{
|
||||
throw new ArgumentException("The provided texture does not have a valid bindless index. Ensure the texture is created with bindless support.");
|
||||
return Result.Create(default(T), ResultStatus.InvalidArgument);
|
||||
}
|
||||
|
||||
SetUInt(propertyName, (uint)bindlessIndex);
|
||||
return Result.Create(*(T*)_cBufferCache.CpuData.GetUnsafePtr(), ResultStatus.Success);
|
||||
}
|
||||
|
||||
/// <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</param>
|
||||
/// <param name="indexBufferIndexProperty">The name of the index buffer index property</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetBufferBindless(string propertyName, Handle<GraphicsBuffer> buffer)
|
||||
public readonly Span<byte> GetRawPropertyCache()
|
||||
{
|
||||
var bindlessIndex = _resourceDatabase.GetBindlessIndex(buffer.AsResource());
|
||||
if (bindlessIndex == -1)
|
||||
if (_cBufferCache.Size == 0)
|
||||
{
|
||||
throw new ArgumentException("The provided buffer does not have a valid bindless index. Ensure the buffer is created with bindless support.");
|
||||
return Span<byte>.Empty;
|
||||
}
|
||||
|
||||
SetUInt(propertyName, (uint)bindlessIndex);
|
||||
return _cBufferCache.CpuData.AsSpan(0, (int)_cBufferCache.Size);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void UploadMaterialData(ICommandBuffer cmb)
|
||||
public readonly unsafe Result<ResultStatus> SetPropertyCache<T>(ref readonly T data)
|
||||
where T : unmanaged
|
||||
{
|
||||
for (var i = 0; i < _shader.PassCount; i++)
|
||||
if (sizeof(T) != _cBufferCache.Size)
|
||||
{
|
||||
ref var cache = ref _materialData.GetPassCache(i);
|
||||
cmb.UploadBuffer<byte>(cache.GpuResource, cache.CpuData.AsSpan());
|
||||
return new Result<ResultStatus>(false, ResultStatus.InvalidArgument);
|
||||
}
|
||||
|
||||
Unsafe.WriteUnaligned(_cBufferCache.CpuData.GetUnsafePtr(), data);
|
||||
return Result.Success(ResultStatus.Success);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly unsafe Result<ResultStatus> SetRawPropertyCache(ReadOnlySpan<byte> data)
|
||||
{
|
||||
if (data.Length != _cBufferCache.Size)
|
||||
{
|
||||
return new Result<ResultStatus>(false, ResultStatus.InvalidArgument);
|
||||
}
|
||||
|
||||
Unsafe.WriteUnaligned(_cBufferCache.CpuData.GetUnsafePtr(), data);
|
||||
return Result.Success(ResultStatus.Success);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void UploadData(ICommandBuffer cmb)
|
||||
{
|
||||
cmb.UploadBuffer(_cBufferCache.GpuResource, _cBufferCache.CpuData.AsSpan());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
_cBufferCache.ReleaseResource(database);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,9 @@ namespace Ghost.Graphics.Core;
|
||||
|
||||
public struct Mesh : IResourceReleasable, IHandleType
|
||||
{
|
||||
private UnsafeList<Vertex> _vertices;
|
||||
private UnsafeList<uint> _indices;
|
||||
|
||||
internal bool IsMeshDataDirty
|
||||
{
|
||||
get; private set;
|
||||
@@ -21,10 +24,10 @@ public struct Mesh : IResourceReleasable, IHandleType
|
||||
/// </summary>
|
||||
public UnsafeList<Vertex> Vertices
|
||||
{
|
||||
readonly get => field;
|
||||
readonly get => _vertices;
|
||||
set
|
||||
{
|
||||
field = value;
|
||||
_vertices = value;
|
||||
VertexCount = value.Count;
|
||||
IsMeshDataDirty = true;
|
||||
}
|
||||
@@ -35,10 +38,10 @@ public struct Mesh : IResourceReleasable, IHandleType
|
||||
/// </summary>
|
||||
public UnsafeList<uint> Indices
|
||||
{
|
||||
readonly get => field;
|
||||
readonly get => _indices;
|
||||
set
|
||||
{
|
||||
field = value;
|
||||
_indices = value;
|
||||
IndexCount = value.Count;
|
||||
IsMeshDataDirty = true;
|
||||
}
|
||||
@@ -100,8 +103,8 @@ public struct Mesh : IResourceReleasable, IHandleType
|
||||
|
||||
internal Mesh(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<uint> indices, Handle<GraphicsBuffer> vertexBuffer, Handle<GraphicsBuffer> indexBuffer)
|
||||
{
|
||||
Vertices = new(vertices.Length, Allocator.Persistent);
|
||||
Indices = new(indices.Length, Allocator.Persistent);
|
||||
Vertices = new UnsafeList<Vertex>(vertices.Length, Allocator.Persistent);
|
||||
Indices = new UnsafeList<uint>(indices.Length, Allocator.Persistent);
|
||||
Vertices.CopyFrom(vertices);
|
||||
Indices.CopyFrom(indices);
|
||||
VertexBuffer = vertexBuffer;
|
||||
@@ -112,8 +115,8 @@ public struct Mesh : IResourceReleasable, IHandleType
|
||||
|
||||
public readonly void ReleaseCpuResources()
|
||||
{
|
||||
Vertices.Dispose();
|
||||
Indices.Dispose();
|
||||
_vertices.Dispose();
|
||||
_indices.Dispose();
|
||||
}
|
||||
|
||||
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
||||
|
||||
@@ -56,13 +56,32 @@ public unsafe readonly ref struct RenderingContext
|
||||
queue.WaitIdle();
|
||||
}
|
||||
|
||||
public Handle<Mesh> CreateMesh(UnsafeList<Vertex> vertices, UnsafeList<uint> indices)
|
||||
public Handle<Mesh> CreateMesh(UnsafeList<Vertex> vertices, UnsafeList<uint> indices, bool staticMesh)
|
||||
{
|
||||
var mesh = ResourceAllocator.CreateMesh(vertices, indices);
|
||||
ref var meshData = ref ResourceDatabase.GetMeshReference(mesh);
|
||||
|
||||
var vertexHandle = meshData.VertexBuffer.AsResource();
|
||||
var indexHandle = meshData.IndexBuffer.AsResource();
|
||||
|
||||
_directCmd.ResourceBarrier(vertexHandle, ResourceState.Common, ResourceState.CopyDest);
|
||||
_directCmd.ResourceBarrier(indexHandle, ResourceState.Common, ResourceState.CopyDest);
|
||||
|
||||
_directCmd.UploadBuffer(meshData.VertexBuffer, meshData.Vertices.AsSpan());
|
||||
_directCmd.UploadBuffer(meshData.IndexBuffer, meshData.Indices.AsSpan());
|
||||
|
||||
_directCmd.ResourceBarrier(vertexHandle, ResourceState.CopyDest, ResourceState.VertexAndConstantBuffer);
|
||||
_directCmd.ResourceBarrier(indexHandle, ResourceState.CopyDest, ResourceState.IndexBuffer);
|
||||
|
||||
if (staticMesh)
|
||||
{
|
||||
meshData.ReleaseCpuResources();
|
||||
}
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
public Handle<Mesh> CreateMesh(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<uint> indices)
|
||||
public Handle<Mesh> CreateMesh(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<uint> indices, bool staticMesh)
|
||||
{
|
||||
var vertexList = new UnsafeList<Vertex>(vertices.Length, Allocator.Persistent);
|
||||
var indexList = new UnsafeList<uint>(indices.Length, Allocator.Persistent);
|
||||
@@ -70,12 +89,7 @@ public unsafe readonly ref struct RenderingContext
|
||||
vertexList.CopyFrom(vertices);
|
||||
indexList.CopyFrom(indices);
|
||||
|
||||
return CreateMesh(vertexList, indexList);
|
||||
}
|
||||
|
||||
public MaterialAccessor GetMaterialAccessor(Handle<Material> material)
|
||||
{
|
||||
return new MaterialAccessor(material, ResourceDatabase);
|
||||
return CreateMesh(vertexList, indexList, staticMesh);
|
||||
}
|
||||
|
||||
// TODO: Make one memory pool for upload.
|
||||
@@ -108,12 +122,12 @@ public unsafe readonly ref struct RenderingContext
|
||||
|
||||
if (needVertexTransition)
|
||||
{
|
||||
_directCmd.ResourceBarrier(meshData.VertexBuffer.AsResource(), ResourceState.CopyDest, ResourceState.VertexAndConstantBuffer);
|
||||
_directCmd.ResourceBarrier(meshData.VertexBuffer.AsResource(), ResourceState.CopyDest, vertexState);
|
||||
}
|
||||
|
||||
if (needIndexTransition)
|
||||
{
|
||||
_directCmd.ResourceBarrier(meshData.IndexBuffer.AsResource(), ResourceState.CopyDest, ResourceState.IndexBuffer);
|
||||
_directCmd.ResourceBarrier(meshData.IndexBuffer.AsResource(), ResourceState.CopyDest, indexState);
|
||||
}
|
||||
|
||||
if (markMeshStatic)
|
||||
@@ -177,7 +191,7 @@ public unsafe readonly ref struct RenderingContext
|
||||
slicePitch = slicePitch
|
||||
};
|
||||
|
||||
_directCmd.UploadTexture(texture, subresourceData);
|
||||
_directCmd.UploadTexture(texture, [subresourceData]);
|
||||
}
|
||||
|
||||
if (needTransition)
|
||||
@@ -194,10 +208,15 @@ public unsafe readonly ref struct RenderingContext
|
||||
ref var materialRef = ref ResourceDatabase.GetMaterialReference(material);
|
||||
var shader = ResourceDatabase.GetShaderReference(materialRef.Shader);
|
||||
|
||||
shader.TryGetPassKey(passName, out var passIndex, out var passKey);
|
||||
var keyResult = shader.TryGetPassKey(passName, out var passIndex);
|
||||
if (keyResult.Status != ResultStatus.Success)
|
||||
{
|
||||
throw new Exception(keyResult.ToString());
|
||||
}
|
||||
|
||||
var hash = new GraphicsPipelineHash
|
||||
{
|
||||
Id = passKey,
|
||||
Id = keyResult.Value.Identifier,
|
||||
RtvCount = 1,
|
||||
DsvFormat = TextureFormat.Unknown,
|
||||
};
|
||||
@@ -209,20 +228,13 @@ public unsafe readonly ref struct RenderingContext
|
||||
_directCmd.SetConstantBufferView(RootSignatureLayout.PER_OBJECT_BUFFER_SLOT, meshRef.ObjectDataBuffer);
|
||||
|
||||
// NOTE: We use fixed root signature layout for bindless rendering.
|
||||
ref var cache = ref materialRef.GetPassCache(passIndex);
|
||||
var cache = materialRef.CBufferCache;
|
||||
if (cache.IsCreated)
|
||||
{
|
||||
_directCmd.SetConstantBufferView(RootSignatureLayout.PER_MATERIAL_BUFFER_SLOT, cache.GpuResource);
|
||||
}
|
||||
|
||||
// NOTE: Since we are using true bindless resources, we only need to set the descriptor heaps, not individual tables.
|
||||
// TODO: Maybe handle the traditional bindless model?
|
||||
#if false
|
||||
var samplerGpuHandle = _descriptorAllocator.GetSamplerHeap()->GetGPUDescriptorHandleForHeapStart();
|
||||
_commandList.Get()->SetGraphicsRootDescriptorTable(rootParamIndex, samplerGpuHandle);
|
||||
#endif
|
||||
|
||||
//var threadGroupCountX = ((uint)meshRef.IndexCount + numThreadsX - 1) / numThreadsX;
|
||||
_directCmd.DispatchMesh(1, 1, 1);
|
||||
var threadGroupCountX = ((uint)meshRef.IndexCount + numThreadsX - 1) / numThreadsX;
|
||||
_directCmd.DispatchMesh(threadGroupCountX, 1, 1);
|
||||
}
|
||||
}
|
||||
@@ -3,45 +3,44 @@ using Ghost.Core.Graphics;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
public class ShaderPass : IResourceReleasable
|
||||
public struct ShaderPass : IResourceReleasable
|
||||
{
|
||||
private CBufferInfo _cbufferInfo;
|
||||
// NOTE: This is for per pass cbuffer only. Global, per view, and per mesh cbuffers are fixed.
|
||||
private readonly Dictionary<string, int> _propertyLookup;
|
||||
|
||||
public CBufferInfo CBuffer => _cbufferInfo;
|
||||
|
||||
public ShaderPass(CBufferInfo info)
|
||||
public ShaderPassKey Identifier
|
||||
{
|
||||
_cbufferInfo = info;
|
||||
|
||||
var capacity = info.Properties?.Count ?? 0;
|
||||
_propertyLookup = new Dictionary<string, int>(capacity);
|
||||
for (var i = 0; i < capacity; i++)
|
||||
{
|
||||
_propertyLookup[info.Properties![i].Name] = i;
|
||||
}
|
||||
get; init;
|
||||
}
|
||||
|
||||
public int GetPropertyId(string propertyName)
|
||||
public ZTestOptions ZTest
|
||||
{
|
||||
return _propertyLookup.TryGetValue(propertyName, out var id) ? id : -1;
|
||||
get; set;
|
||||
}
|
||||
|
||||
public CBufferPropertyInfo GetPropertyInfo(int id)
|
||||
public ZWriteOptions ZWrite
|
||||
{
|
||||
return _cbufferInfo.Properties[id];
|
||||
get; set;
|
||||
}
|
||||
|
||||
public CBufferPropertyInfo GetPropertyInfo(string propertyName)
|
||||
public CullOptions Cull
|
||||
{
|
||||
return _cbufferInfo.Properties[GetPropertyId(propertyName)];
|
||||
get; set;
|
||||
}
|
||||
|
||||
public BlendOptions Blend
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public uint ColorMask
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
// TODO: Shader variant.
|
||||
|
||||
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
}
|
||||
@@ -52,46 +51,43 @@ public class ShaderPass : IResourceReleasable
|
||||
/// </summary>
|
||||
public class Shader : IResourceReleasable, IIdentifierType
|
||||
{
|
||||
private UnsafeArray<ShaderPassKey> _passIDs;
|
||||
private readonly uint _cbufferSize;
|
||||
private UnsafeArray<ShaderPass> _passes;
|
||||
// TODO: Optmize lookups with a better data structure if needed
|
||||
private readonly Dictionary<string, int> _passLookup; // pass name to index
|
||||
private readonly Dictionary<string, List<int>> _propertyLookup; // property name to pass index (property name to list of pass indices that contain the property)
|
||||
|
||||
public int PassCount => _passIDs.Count;
|
||||
public int PassCount => _passes.Count;
|
||||
public uint CBufferSize => _cbufferSize;
|
||||
|
||||
internal Shader(ShaderDescriptor descriptor)
|
||||
{
|
||||
_passIDs = new UnsafeArray<ShaderPassKey>(descriptor.passes.Count, Allocator.Persistent);
|
||||
_passLookup = new(descriptor.passes.Count);
|
||||
_propertyLookup = new(descriptor.passes.Count);
|
||||
_cbufferSize = descriptor.cbufferSize;
|
||||
_passes = new UnsafeArray<ShaderPass>(descriptor.passes.Count, Allocator.Persistent);
|
||||
_passLookup = new Dictionary<string, int>(descriptor.passes.Count);
|
||||
|
||||
for (var i = 0; i < descriptor.passes.Count; i++)
|
||||
{
|
||||
var pass = descriptor.passes[i];
|
||||
|
||||
// TODO: Handle inherited passes
|
||||
if (pass is not FullPassDescriptor fullPass)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var passKey = new ShaderPassKey(pass.Identifier);
|
||||
|
||||
_passIDs[i] = passKey;
|
||||
_passLookup[pass.Name] = i;
|
||||
|
||||
if (pass is FullPassDescriptor fullPass)
|
||||
_passes[i] = new ShaderPass
|
||||
{
|
||||
if (fullPass.properties == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Identifier = passKey,
|
||||
ZTest = fullPass.localPipeline.zTest,
|
||||
ZWrite = fullPass.localPipeline.zWrite,
|
||||
Cull = fullPass.localPipeline.cull,
|
||||
Blend = fullPass.localPipeline.blend,
|
||||
ColorMask = fullPass.localPipeline.colorMask
|
||||
};
|
||||
|
||||
foreach (var property in fullPass.properties)
|
||||
{
|
||||
ref var passIndices = ref CollectionsMarshal.GetValueRefOrAddDefault(_propertyLookup, property.name, out var exists);
|
||||
if (!exists || passIndices == null)
|
||||
{
|
||||
passIndices = new List<int>();
|
||||
}
|
||||
|
||||
passIndices.Add(i);
|
||||
}
|
||||
}
|
||||
// TODO: handle inherited passes
|
||||
_passLookup[pass.Name] = i;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,38 +96,26 @@ public class Shader : IResourceReleasable, IIdentifierType
|
||||
return _passLookup.GetValueOrDefault(passName, -1);
|
||||
}
|
||||
|
||||
public ShaderPassKey GetPassKey(int index)
|
||||
public ref ShaderPass GetPassReference(int index)
|
||||
{
|
||||
return _passIDs[index];
|
||||
return ref _passes[index];
|
||||
}
|
||||
|
||||
public bool TryGetPassKey(string passName, out int passIndex, out ShaderPassKey passID)
|
||||
public RefResult<ShaderPass, ResultStatus> TryGetPassKey(string passName, out int passIndex)
|
||||
{
|
||||
var index = _passLookup.GetValueOrDefault(passName, -1);
|
||||
if (index == -1)
|
||||
{
|
||||
passIndex = -1;
|
||||
passID = new(0);
|
||||
return false;
|
||||
return Result.CreateRef(ref Unsafe.NullRef<ShaderPass>(), ResultStatus.NotFound);
|
||||
}
|
||||
|
||||
passIndex = index;
|
||||
passID = _passIDs[index];
|
||||
return true;
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<int> GetPropertyPassIndices(string propertyName)
|
||||
{
|
||||
if (_propertyLookup.TryGetValue(propertyName, out var passIndices))
|
||||
{
|
||||
return passIndices;
|
||||
}
|
||||
|
||||
return Array.Empty<int>();
|
||||
return Result.CreateRef(ref _passes[index], ResultStatus.Success);
|
||||
}
|
||||
|
||||
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
_passIDs.Dispose();
|
||||
_passes.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Ghost.Graphics.Core;
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Vertex
|
||||
{
|
||||
public unsafe static class Semantic
|
||||
public static class Semantic
|
||||
{
|
||||
public const DXGI_FORMAT ALIGNED_FORMAT = DXGI_FORMAT.DXGI_FORMAT_R32G32B32A32_FLOAT;
|
||||
public const int COUNT = 5;
|
||||
|
||||
Reference in New Issue
Block a user