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:
2025-11-28 18:58:50 +09:00
parent 0720444c2c
commit bd97d233cb
49 changed files with 842 additions and 1025 deletions

View File

@@ -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);
}
}