Refactor D3D12 Resource Management

Refactored and renamed components related to D3D12 graphics programming, replacing "descriptor" with "viewGroup" to improve resource grouping and management. Updated `D3D12CommandBuffer`, `D3D12DescriptorAllocator`, and `D3D12PipelineLibrary` to reflect these changes. Simplified material and shader creation in `D3D12ResourceAllocator`. Enhanced `D3D12ResourceDatabase` with resource naming for debugging and improved management. Refactored `Shader` and `ShaderPass` to use modern C# features and `IResourceReleasable` interface. Introduced `D3D12Utility` for centralized utility methods. Updated `Material` class for efficient buffer creation. Renamed `ShaderCompiler` to `SDLCompiler` with improved error handling. Updated `MeshRenderPass` to use new shader compilation process. Various improvements in error handling, code readability, and utility methods.
This commit is contained in:
2025-10-23 14:42:53 +09:00
parent d2d9f5feb7
commit 28c386b0bb
28 changed files with 393 additions and 306 deletions

View File

@@ -18,20 +18,12 @@ internal struct CBufferCache : IResourceReleasable
public readonly Handle<GraphicsBuffer> GpuResource => _gpuResource;
public readonly uint AlignedSize => _alignedSize;
public unsafe CBufferCache(IResourceAllocator allocator, uint bufferSize)
public unsafe CBufferCache(Handle<GraphicsBuffer> buffer, uint bufferSize)
{
_alignedSize = (bufferSize + 255u) & ~255u;
_cpuData = new((int)AlignedSize, Allocator.Persistent);
var desc = new BufferDesc
{
Size = bufferSize,
Usage = BufferUsage.Constant,
MemoryType = ResourceMemoryType.Default,
};
_gpuResource = allocator.CreateBuffer(ref desc);
_gpuResource = buffer;
}
public void ReleaseResource(IResourceDatabase database)
@@ -52,11 +44,6 @@ public struct Material : IResourceReleasable, IHandleType
public readonly Identifier<Shader> Shader => _shader;
public Material(Identifier<Shader> shader, IResourceAllocator allocator, IResourceDatabase database)
{
SetShader(shader, allocator, database);
}
internal ref CBufferCache GetPassCache(int passIndex)
{
return ref _materialPropertiesCache[passIndex];
@@ -77,7 +64,16 @@ public struct Material : IResourceReleasable, IHandleType
{
var pass = database.GetShaderPass(shader.GetPassKey(i));
var cbufferInfo = pass.PassPropertyInfo;
_materialPropertiesCache[i] = new CBufferCache(allocator, cbufferInfo.Size);
var desc = new BufferDesc
{
Size = cbufferInfo.Size,
Usage = BufferUsage.Constant,
MemoryType = ResourceMemoryType.Default,
};
var buffer = allocator.CreateBuffer(ref desc);
_materialPropertiesCache[i] = new CBufferCache(buffer, cbufferInfo.Size);
}
}
@@ -95,7 +91,7 @@ public struct Material : IResourceReleasable, IHandleType
public ref struct MaterialAccessor
{
private ref Material _materialData;
private readonly ref Shader _shader;
private Shader _shader;
private readonly IResourceDatabase _resourceDatabase;
@@ -104,7 +100,7 @@ public ref struct MaterialAccessor
_resourceDatabase = resourceDatabase;
_materialData = ref resourceDatabase.GetMaterialReference(material);
_shader = ref resourceDatabase.GetShaderReference(_materialData.Shader);
_shader = resourceDatabase.GetShaderReference(_materialData.Shader);
}
private readonly unsafe void WriteToCache<T>(string propertyName, in T value)

View File

@@ -1,7 +1,7 @@
using Ghost.Core;
using Ghost.Core.Contracts;
using Ghost.Core.Graphics;
using Ghost.Graphics.RHI;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.InteropServices;
@@ -51,13 +51,13 @@ public readonly struct CBufferInfo
}
}
public readonly unsafe struct ShaderPass
public unsafe class ShaderPass : IResourceReleasable
{
// NOTE: This is for per pass cbuffer only. Global, per view, and per mesh cbuffers are fixed.
private readonly Dictionary<string, int> _propertyLookup;
private readonly UnsafeList<PropertyInfo> _properties;
internal readonly CBufferInfo PassPropertyInfo
internal CBufferInfo PassPropertyInfo
{
get;
}
@@ -69,36 +69,41 @@ public readonly unsafe struct ShaderPass
_propertyLookup = propertyNameToIdMap;
}
public readonly int GetPropertyId(string propertyName)
public int GetPropertyId(string propertyName)
{
return _propertyLookup.TryGetValue(propertyName, out var id) ? id : -1;
}
public readonly PropertyInfo GetPropertyInfo(int id)
public PropertyInfo GetPropertyInfo(int id)
{
return _properties[id];
}
public readonly PropertyInfo GetPropertyInfo(string propertyName)
public PropertyInfo GetPropertyInfo(string propertyName)
{
return _properties[GetPropertyId(propertyName)];
}
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
{
_properties.Dispose();
}
}
/// <summary>
/// A representation of a GPU shader, including all the passes it contains.
/// </summary>
public readonly struct Shader : IResourceReleasable, IIdentifierType
public class Shader : IResourceReleasable, IIdentifierType
{
private readonly ShaderPassKey[] _passIDs;
private UnsafeArray<ShaderPassKey> _passIDs;
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.Length;
public int PassCount => _passIDs.Count;
internal Shader(ShaderDescriptor descriptor)
{
_passIDs = new ShaderPassKey[descriptor.passes.Count];
_passIDs = new UnsafeArray<ShaderPassKey>(descriptor.passes.Count, Allocator.Persistent);
_passLookup = new(descriptor.passes.Count);
_propertyLookup = new(descriptor.passes.Count);
@@ -132,12 +137,12 @@ public readonly struct Shader : IResourceReleasable, IIdentifierType
}
}
public readonly ShaderPassKey GetPassKey(int index)
public ShaderPassKey GetPassKey(int index)
{
return _passIDs[index];
}
public readonly bool TryGetPassKey(string passName, out ShaderPassKey? passID)
public bool TryGetPassKey(string passName, out ShaderPassKey? passID)
{
var index = _passLookup.GetValueOrDefault(passName, -1);
if (index == -1)
@@ -150,7 +155,7 @@ public readonly struct Shader : IResourceReleasable, IIdentifierType
return true;
}
public readonly IReadOnlyCollection<int> GetPropertyPassIndices(string propertyName)
public IReadOnlyCollection<int> GetPropertyPassIndices(string propertyName)
{
if (_propertyLookup.TryGetValue(propertyName, out var passIndices))
{
@@ -162,6 +167,6 @@ public readonly struct Shader : IResourceReleasable, IIdentifierType
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
{
// Should we do something here?
_passIDs.Dispose();
}
}