Refactor error handling and improve type safety

Refactored error handling across the codebase by replacing exceptions with `Result`-based error handling for better robustness and consistency.

- Updated `ResultExtensions` to use `EqualityComparer` for comparisons.
- Enhanced `RenderingContext` with `GetValueOrThrow` for resource validation and added type constraints for texture methods.
- Introduced `CommandError` and `RecordError` in `D3D12CommandBuffer` for improved error tracking.
- Refactored `D3D12ResourceDatabase` to use `Result` objects for resource queries.
- Updated `ICommandBuffer` and `IResourceDatabase` interfaces to return `Result` objects.
- Improved type safety by replacing `int` with `uint` where appropriate.
- Simplified texture handling in `MeshRenderPass` with new `CreateTexture` logic.
- Cleaned up project files by removing unused and redundant entries.

These changes enhance code maintainability, improve error reporting, and ensure type safety throughout the project.
This commit is contained in:
2025-11-30 19:06:31 +09:00
parent 0ec318a9ab
commit 85280c746d
15 changed files with 244 additions and 126 deletions

View File

@@ -263,8 +263,8 @@ public struct RectDesc
public struct SubResourceData
{
public unsafe void* pData;
public nint rowPitch;
public nint slicePitch;
public uint rowPitch;
public uint slicePitch;
}
public struct PassRenderTargetDesc
@@ -303,7 +303,7 @@ public struct PassDepthStencilDesc
public struct BarrierDesc
{
public Handle<GraphicsBuffer> Resource
public Handle<GPUResource> Resource
{
get; set;
}
@@ -681,6 +681,24 @@ public struct BufferDesc
}
}
public struct CommandError
{
public int CommandIndex
{
get; set;
}
public string CommandName
{
get; set;
}
public ResultStatus Status
{
get; set;
}
}
/// <summary>
/// Swap chain description
/// </summary>

View File

@@ -38,7 +38,7 @@ public interface ICommandBuffer : IDisposable
/// <summary>
/// Ends recording commands and prepares for submission
/// </summary>
void End();
Result End();
/// <summary>
/// Sets the viewport for rendering

View File

@@ -39,7 +39,7 @@ public interface IResourceDatabase : IDisposable
/// </summary>
/// <param name="handle">The handle that uniquely identifies the resource whose state is to be retrieved.</param>
/// <returns>A ResourceState Value representing the current state of the resource associated with the specified handle.</returns>
ResourceState GetResourceState(Handle<GPUResource> handle);
Result<ResourceState, ResultStatus> GetResourceState(Handle<GPUResource> handle);
/// <summary>
/// Sets the state of the specified resource handle to the given Value.
@@ -53,7 +53,7 @@ public interface IResourceDatabase : IDisposable
/// </summary>
/// <param name="handle">A handle that identifies the GPU resource for which to obtain the description. Must reference a valid resource.</param>
/// <returns>A ResourceDesc structure containing details about the specified GPU resource.</returns>
ResourceDesc GetResourceDescription(Handle<GPUResource> handle);
Result<ResourceDesc, ResultStatus> GetResourceDescription(Handle<GPUResource> handle);
/// <summary>
/// Retrieves the bindless index associated with the specified GPU resource handle.

View File

@@ -2,7 +2,7 @@ namespace Ghost.Graphics.RHI;
internal static class RHIUtility
{
public static int GetBytesPerPixel(this TextureFormat format)
public static uint GetBytesPerPixel(this TextureFormat format)
{
return format switch
{
@@ -16,12 +16,17 @@ internal static class RHIUtility
};
}
public static void GetSurfaceInfo(this TextureFormat format, int width, int height, out int rowPitch, out int slicePitch, out int rowCount)
public static uint GetTotalBytes(this TextureDesc desc)
{
return desc.Format.GetBytesPerPixel() * desc.Width * desc.Height * desc.Slice;
}
public static void GetSurfaceInfo(this TextureFormat format, uint width, uint height, out uint rowPitch, out uint slicePitch, out uint rowCount)
{
var bc = false;
var packed = false;
var planar = false;
var bpe = 0;
var bpe = 0u;
//switch (Format)
//{
@@ -86,31 +91,33 @@ internal static class RHIUtility
if (bc)
{
var numBlocksWide = 0;
var numBlocksWide = 0u;
if (width > 0)
{
numBlocksWide = Math.Max(1, (width + 3) / 4);
numBlocksWide = Math.Max(1u, (width + 3) / 4u);
}
var numBlocksHigh = 0;
var numBlocksHigh = 0u;
if (height > 0)
{
numBlocksHigh = Math.Max(1, (height + 3) / 4);
numBlocksHigh = Math.Max(1u, (height + 3) / 4u);
}
rowPitch = numBlocksWide * bpe;
rowCount = numBlocksHigh;
slicePitch = rowPitch * numBlocksHigh;
}
else if (packed)
{
rowPitch = ((width + 1) >> 1) * bpe;
rowPitch = ((width + 1u) >> 1) * bpe;
rowCount = height;
slicePitch = rowPitch * height;
}
else if (planar)
{
rowPitch = ((width + 1) >> 1) * bpe;
rowPitch = ((width + 1u) >> 1) * bpe;
slicePitch = (rowPitch * height) + ((rowPitch * height + 1) >> 1);
rowCount = (int)(height + ((height + 1u) >> 1));
rowCount = height + ((height + 1u) >> 1);
}
else
{