Refactor error handling: use Error enum, update APIs

Replaces ErrorStatus with Error across all systems for consistency.
Renames ResourceBarrierData fields to camelCase.
Adds BindlessAccess enum and updates GetBindlessIndex API.
Updates method signatures, result types, and error checks.
Modernizes HLSL mesh shader syntax and fixes naming.
Improves code style and updates comments for clarity.
This commit is contained in:
2026-01-25 16:34:28 +09:00
parent e11a9ebb52
commit 364fbf9208
28 changed files with 282 additions and 252 deletions

View File

@@ -89,14 +89,14 @@ internal sealed partial class DxcShaderCompiler
return argsArray;
}
private static Result<string, ErrorStatus> GetFinalShaderCode(string shaderPath, ReadOnlySpan<string> includes, string? injectedCode)
private static Result<string, Error> GetFinalShaderCode(string shaderPath, ReadOnlySpan<string> includes, string? injectedCode)
{
string shaderCode;
if (shaderPath == "hlsl_block")
{
if (string.IsNullOrEmpty(injectedCode))
{
return ErrorStatus.InvalidArgument;
return Error.InvalidArgument;
}
shaderCode = string.Empty;
@@ -105,7 +105,7 @@ internal sealed partial class DxcShaderCompiler
{
if (!File.Exists(shaderPath))
{
return ErrorStatus.NotFound;
return Error.NotFound;
}
shaderCode = File.ReadAllText(shaderPath);
@@ -487,7 +487,7 @@ internal sealed unsafe partial class DxcShaderCompiler : IShaderCompiler
return compiled;
}
public Result<GraphicsCompiledResult, ErrorStatus> LoadCompiledCache(Key64<ShaderVariant> key)
public Result<GraphicsCompiledResult, Error> LoadCompiledCache(Key64<ShaderVariant> key)
{
ObjectDisposedException.ThrowIf(_disposed, this);
@@ -496,7 +496,7 @@ internal sealed unsafe partial class DxcShaderCompiler : IShaderCompiler
return compiledResult;
}
return ErrorStatus.NotFound;
return Error.NotFound;
}
public void Dispose()

View File

@@ -3,7 +3,9 @@ using Ghost.Core.Graphics;
using Ghost.Graphics.RHI;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ghost.Graphics.Core;
@@ -66,17 +68,11 @@ public struct Material : IResourceReleasable
get; set;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetDirty()
{
_isDirty = true;
}
public ErrorStatus SetShader(Identifier<Shader> shaderId, IResourceAllocator allocator, IResourceDatabase database)
public Error SetShader(Identifier<Shader> shaderId, IResourceAllocator allocator, IResourceDatabase database)
{
if (!shaderId.IsValid)
{
return ErrorStatus.InvalidArgument;
return Error.InvalidArgument;
}
_cBufferCache.ReleaseResource(database);
@@ -125,16 +121,16 @@ public struct Material : IResourceReleasable
_cBufferCache = new CBufferCache(buffer, shader.CBufferSize);
}
return ErrorStatus.None;
return Error.None;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly unsafe Result<T, ErrorStatus> GetPropertyCache<T>()
public readonly unsafe Result<T, Error> GetPropertyCache<T>()
where T : unmanaged
{
if (sizeof(T) != _cBufferCache.Size)
{
return ErrorStatus.InvalidArgument;
return Error.InvalidArgument;
}
return *(T*)_cBufferCache.CpuData.GetUnsafePtr();
@@ -152,32 +148,45 @@ public struct Material : IResourceReleasable
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe ErrorStatus SetPropertyCache<T>(ref readonly T data)
public unsafe Error SetPropertyCache<T>(ref readonly T data)
where T : unmanaged
{
if (sizeof(T) != _cBufferCache.Size)
{
return ErrorStatus.InvalidArgument;
return Error.InvalidArgument;
}
Unsafe.WriteUnaligned(_cBufferCache.CpuData.GetUnsafePtr(), data);
SetDirty();
var dataSpan = MemoryMarshal.AsBytes(new ReadOnlySpan<T>(in data));
var cacheSpan = _cBufferCache.CpuData.AsSpan();
if (cacheSpan.SequenceEqual(dataSpan))
{
return Error.None;
}
return ErrorStatus.None;
dataSpan.CopyTo(cacheSpan);
_isDirty = true;
return Error.None;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe ErrorStatus SetRawPropertyCache(ReadOnlySpan<byte> data)
public Error SetRawPropertyCache(ReadOnlySpan<byte> data)
{
if (data.Length != _cBufferCache.Size)
{
return ErrorStatus.InvalidArgument;
return Error.InvalidArgument;
}
Unsafe.WriteUnaligned(_cBufferCache.CpuData.GetUnsafePtr(), data);
SetDirty();
var cacheSpan = _cBufferCache.CpuData.AsSpan();
if (cacheSpan.SequenceEqual(data))
{
return Error.None;
}
return ErrorStatus.None;
data.CopyTo(cacheSpan);
_isDirty = true;
return Error.None;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -191,11 +200,11 @@ public struct Material : IResourceReleasable
{
ref var pipelineOverride = ref _passPipelineOverride[passIndex];
pipelineOverride.options = options;
SetDirty();
_isDirty = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ErrorStatus SetKeyword(IResourceDatabase resourceDatabase, int keywordId, bool enabled)
public Error SetKeyword(IResourceDatabase resourceDatabase, int keywordId, bool enabled)
{
var r = resourceDatabase.GetShaderReference(_shader);
if (r.IsFailure)
@@ -207,13 +216,13 @@ public struct Material : IResourceReleasable
var localIndex = shader.GetLocalKeywordIndex(keywordId);
if (localIndex == -1)
{
return ErrorStatus.NotFound;
return Error.NotFound;
}
_keywordMask.SetKeyword(localIndex, enabled);
SetDirty();
_isDirty = true;
return ErrorStatus.None;
return Error.None;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -252,14 +261,14 @@ public struct Material : IResourceReleasable
var barrierData = r.Value;
var desc = BarrierDesc.Buffer(
cbufferResource,
barrierData.Sync,
barrierData.sync,
BarrierSync.Copy,
barrierData.Access,
barrierData.access,
BarrierAccess.CopyDest);
cmd.ResourceBarrier(desc);
cmd.UploadBuffer(_cBufferCache.GpuResource, _cBufferCache.CpuData.AsSpan());
desc = BarrierDesc.Buffer(
cbufferResource,
BarrierSync.Copy,

View File

@@ -55,23 +55,27 @@ public readonly unsafe ref struct RenderingContext
}
var data = r.Value;
if (data.Layout == newLayout && data.Access == newAccess && data.Sync == newSync)
if (data.layout == newLayout && data.access == newAccess && data.sync == newSync)
{
return;
}
// For buffers, layout is usually Undefined/Common and doesn't change, but Access/Sync do.
// For textures, layout changes matter.
var desc = isTexture ?
BarrierDesc.Texture(
BarrierDesc desc;
if (isTexture)
{
desc = BarrierDesc.Texture(
resource,
data.Sync, newSync,
data.Access, newAccess,
data.Layout, newLayout)
: BarrierDesc.Buffer(
data.sync, newSync,
data.access, newAccess,
data.layout, newLayout);
}
else
{
desc = BarrierDesc.Buffer(
resource,
data.Sync, newSync,
data.Access, newAccess);
data.sync, newSync,
data.access, newAccess);
}
_directCmd.ResourceBarrier(new ReadOnlySpan<BarrierDesc>(in desc));
ResourceDatabase.SetResourceBarrierData(resource, new ResourceBarrierData(newLayout, newAccess, newSync));
@@ -188,13 +192,13 @@ public readonly unsafe ref struct RenderingContext
public void UploadTexture<T>(Handle<Texture> texture, ReadOnlySpan<T> data)
where T : unmanaged
{
var desc = ResourceDatabase.GetResourceDescription(texture.AsResource())
.GetValueOrThrow();
if (data.Length * sizeof(T) != desc.TextureDescription.GetTotalBytes())
{
throw new ArgumentException("Data size does not match texture size.");
}
var desc = ResourceDatabase.GetResourceDescription(texture.AsResource()).GetValueOrThrow();
//var size = ResourceAllocator.GetSizeInfo(desc).Size;
//if ((ulong)(data.Length * sizeof(T)) != ResourceAllocator.GetSizeInfo(desc).Size)
//{
// throw new ArgumentException("Data size does not match texture size.");
//}
desc.TextureDescription.Format.GetSurfaceInfo(desc.TextureDescription.Width, desc.TextureDescription.Height, out var rowPitch, out var slicePitch, out _);
@@ -209,7 +213,7 @@ public readonly unsafe ref struct RenderingContext
slicePitch = slicePitch
};
_directCmd.UploadTexture(texture, [subresourceData]);
_directCmd.UploadTexture(texture, subresourceData);
}
}
}

View File

@@ -186,12 +186,12 @@ public partial struct Shader : IResourceReleasable
return ref _shaderPasses[index];
}
public readonly Result<ShaderPass, ErrorStatus> TryGetPass(Identifier<ShaderPass> passID, out int passIndex)
public readonly Result<ShaderPass, Error> TryGetPass(Identifier<ShaderPass> passID, out int passIndex)
{
if (_passIDToLocal.TryGetValue(passID.Value, out var index))
{
passIndex = -1;
return ErrorStatus.NotFound;
return Error.NotFound;
}
passIndex = index;