Improve the usability of Result<T, E> and add new job schedule method to EntityQuery.

Added implicate conversion to Result<T, E> and RefResult<T, E>;
Added new ScheduleChunkParallel in EntityQuery;
Remove Ghost.SparseEntity from solution file. It's now completlty replaced by Ghost.Entities;
This commit is contained in:
2025-12-09 21:43:12 +09:00
parent 97d1118caa
commit 99c1a1980e
29 changed files with 646 additions and 553 deletions

View File

@@ -130,7 +130,7 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
#if DEBUG
[DoesNotReturn]
#endif
private void RecordError(string cmdName, ResultStatus status)
private void RecordError(string cmdName, ErrorStatus status)
{
#if DEBUG
throw new InvalidOperationException($"Error at {cmdName} with {status}");
@@ -183,7 +183,7 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
_commandList.Get()->Close();
_isRecording = false;
if (_lastError.Status != ResultStatus.Success)
if (_lastError.Status != ErrorStatus.None)
{
return Result.Failure($"Command buffer ended with errors at {_lastError.CommandIndex}, command '{_lastError.CommandName}': {_lastError.Status}");
}
@@ -220,21 +220,21 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
if (!desc.Resource.IsValid)
{
RecordError(nameof(ResourceBarrier), ResultStatus.InvalidArgument);
RecordError(nameof(ResourceBarrier), ErrorStatus.InvalidArgument);
continue;
}
var recordResult = _resourceDatabase.GetResourceRecord(desc.Resource);
if (recordResult.Status != ResultStatus.Success)
if (recordResult.Error != ErrorStatus.None)
{
RecordError(nameof(ResourceBarrier), recordResult.Status);
RecordError(nameof(ResourceBarrier), recordResult.Error);
continue;
}
ref var record = ref recordResult.Value;
if (record.state != desc.StateBefore)
{
RecordError(nameof(ResourceBarrier), ResultStatus.InvalidState);
RecordError(nameof(ResourceBarrier), ErrorStatus.InvalidState);
continue;
}
@@ -263,9 +263,9 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
}
var recordResult = _resourceDatabase.GetResourceRecord(resource);
if (recordResult.Status != ResultStatus.Success)
if (recordResult.Error != ErrorStatus.None)
{
RecordError(nameof(ResourceBarrier), recordResult.Status);
RecordError(nameof(ResourceBarrier), recordResult.Error);
return;
}
@@ -284,9 +284,9 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
IncrementCommandCount();
var recordResult = _resourceDatabase.GetResourceRecord(resource);
if (recordResult.Status != ResultStatus.Success)
if (recordResult.Error != ErrorStatus.None)
{
RecordError(nameof(ResourceBarrier), recordResult.Status);
RecordError(nameof(ResourceBarrier), recordResult.Error);
return;
}
@@ -316,14 +316,14 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
var handle = renderTargets[i];
if (!handle.IsValid)
{
RecordError(nameof(SetRenderTargets), ResultStatus.InvalidArgument);
RecordError(nameof(SetRenderTargets), ErrorStatus.InvalidArgument);
continue;
}
var recordResult = _resourceDatabase.GetResourceRecord(handle.AsResource());
if (recordResult.Status != ResultStatus.Success)
if (recordResult.Error != ErrorStatus.None)
{
RecordError(nameof(SetRenderTargets), recordResult.Status);
RecordError(nameof(SetRenderTargets), recordResult.Error);
continue;
}
@@ -337,9 +337,9 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
if (pDsvHandle != null)
{
var recordResult = _resourceDatabase.GetResourceRecord(depthTarget.AsResource());
if (recordResult.Status != ResultStatus.Success)
if (recordResult.Error != ErrorStatus.None)
{
RecordError(nameof(SetRenderTargets), recordResult.Status);
RecordError(nameof(SetRenderTargets), recordResult.Error);
return;
}
@@ -362,14 +362,14 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
var rtDesc = rtDescs[i];
if (!rtDesc.Texture.IsValid)
{
RecordError(nameof(BeginRenderPass), ResultStatus.InvalidArgument);
RecordError(nameof(BeginRenderPass), ErrorStatus.InvalidArgument);
continue;
}
var recordResult = _resourceDatabase.GetResourceRecord(rtDesc.Texture.AsResource());
if (recordResult.Status != ResultStatus.Success)
if (recordResult.Error != ErrorStatus.None)
{
RecordError(nameof(BeginRenderPass), recordResult.Status);
RecordError(nameof(BeginRenderPass), recordResult.Error);
continue;
}
@@ -402,9 +402,9 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
if (pDsvDesc != null)
{
var recordResult = _resourceDatabase.GetResourceRecord(depthDesc.Texture.AsResource());
if (recordResult.Status != ResultStatus.Success)
if (recordResult.Error != ErrorStatus.None)
{
RecordError(nameof(BeginRenderPass), recordResult.Status);
RecordError(nameof(BeginRenderPass), recordResult.Error);
return;
}
@@ -458,10 +458,10 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
IncrementCommandCount();
var psor = _pipelineLibrary.GetGraphicsPSO(pipelineKey);
if (psor.Status != ResultStatus.Success)
if (psor.Error != ErrorStatus.None)
{
#if DEBUG || GHOST_EDITOR
Logger.LogError($"Failed to get graphics pipeline state object for key {pipelineKey}: {psor.Status}");
Logger.LogError($"Failed to get graphics pipeline state object for key {pipelineKey}: {psor.Error}");
#endif
return;
}
@@ -487,9 +487,9 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
IncrementCommandCount();
var recordResult = _resourceDatabase.GetResourceRecord(buffer.AsResource());
if (recordResult.Status != ResultStatus.Success)
if (recordResult.Error != ErrorStatus.None)
{
RecordError(nameof(BeginRenderPass), recordResult.Status);
RecordError(nameof(BeginRenderPass), recordResult.Error);
return;
}
@@ -657,7 +657,7 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
var pSrcResource = _resourceDatabase.GetResource(src.AsResource());
if (pSrcResource == null || pDestResource == null)
{
RecordError(nameof(CopyBuffer), ResultStatus.InvalidArgument);
RecordError(nameof(CopyBuffer), ErrorStatus.InvalidArgument);
return;
}

View File

@@ -161,7 +161,7 @@ internal unsafe class D3D12PipelineLibrary : IPipelineLibrary
if (File.Exists(filePath))
{
var fileBytes = File.ReadAllBytes(filePath!);
var fileBytes = File.ReadAllBytes(filePath);
fixed (byte* pFileBytes = fileBytes)
{
ThrowIfFailed(_device.NativeDevice.Get()->CreatePipelineLibrary(pFileBytes, (nuint)fileBytes.Length, __uuidof(pLibrary), (void**)&pLibrary));
@@ -224,7 +224,7 @@ internal unsafe class D3D12PipelineLibrary : IPipelineLibrary
RegisterSlot = info.BindPoint,
RegisterSpace = info.Space,
SizeInBytes = info.Size,
Properties = info.Properties ?? Array.Empty<CBufferPropertyInfo>(),
Properties = info.Properties ?? [],
};
}
}
@@ -401,14 +401,14 @@ internal unsafe class D3D12PipelineLibrary : IPipelineLibrary
return key;
}
public Result<SharedPtr<ID3D12PipelineState>, ResultStatus> GetGraphicsPSO(GraphicsPipelineKey key)
public Result<SharedPtr<ID3D12PipelineState>, ErrorStatus> GetGraphicsPSO(GraphicsPipelineKey key)
{
if (_pipelineCache.TryGetValue(key, out var cacheEntry))
{
return Result.Create(new SharedPtr<ID3D12PipelineState>(cacheEntry.pso.Get()), ResultStatus.Success);
return cacheEntry.pso.Share();
}
return Result.Create(default(SharedPtr<ID3D12PipelineState>), ResultStatus.NotFound);
return ErrorStatus.NotFound;
}
public void Dispose()

View File

@@ -178,17 +178,17 @@ internal class D3D12ResourceDatabase : IResourceDatabase
return _resources.Contains(handle.id, handle.generation);
}
public RefResult<ResourceRecord, ResultStatus> GetResourceRecord(Handle<GPUResource> handle)
public RefResult<ResourceRecord, ErrorStatus> GetResourceRecord(Handle<GPUResource> handle)
{
ObjectDisposedException.ThrowIf(_disposed, this);
ref var info = ref _resources.GetElementReferenceAt(handle.id, handle.generation, out var exist);
if (!exist)
{
return Result.CreateRef(ref Unsafe.NullRef<ResourceRecord>(), ResultStatus.NotFound);
return ErrorStatus.NotFound;
}
return Result.CreateRef(ref info, ResultStatus.Success);
return RefResult<ResourceRecord, ErrorStatus>.Success(ref info);
}
public ref ResourceRecord GetResourceRecord(Handle<GPUResource> handle, out bool exist)
@@ -202,7 +202,7 @@ internal class D3D12ResourceDatabase : IResourceDatabase
ObjectDisposedException.ThrowIf(_disposed, this);
var r = GetResourceRecord(handle);
if (r.Status != ResultStatus.Success)
if (r.Error != ErrorStatus.None)
{
return null;
}
@@ -210,17 +210,17 @@ internal class D3D12ResourceDatabase : IResourceDatabase
return r.Value.ResourcePtr;
}
public Result<ResourceState, ResultStatus> GetResourceState(Handle<GPUResource> handle)
public Result<ResourceState, ErrorStatus> GetResourceState(Handle<GPUResource> handle)
{
ObjectDisposedException.ThrowIf(_disposed, this);
var r = GetResourceRecord(handle);
if (r.Status != ResultStatus.Success)
if (!r)
{
return Result.Create(ResourceState.Common, r.Status);
return r.Error;
}
return Result.Create(r.Value.state, ResultStatus.Success);
return r.Value.state;
}
public void SetResourceState(Handle<GPUResource> handle, ResourceState state)
@@ -236,31 +236,30 @@ internal class D3D12ResourceDatabase : IResourceDatabase
info.state = state;
}
public Result<ResourceDesc, ResultStatus> GetResourceDescription(Handle<GPUResource> handle)
public Result<ResourceDesc, ErrorStatus> GetResourceDescription(Handle<GPUResource> handle)
{
ObjectDisposedException.ThrowIf(_disposed, this);
var r = GetResourceRecord(handle);
if (r.Status != ResultStatus.Success)
if (!r)
{
return Result.Create(default(ResourceDesc), r.Status);
return r.Error;
}
return Result.Create(r.Value.desc, ResultStatus.Success);
return r.Value.desc;
}
public Result<uint, ResultStatus> GetBindlessIndex(Handle<GPUResource> handle)
public Result<uint, ErrorStatus> GetBindlessIndex(Handle<GPUResource> handle)
{
ObjectDisposedException.ThrowIf(_disposed, this);
ref var info = ref GetResourceRecord(handle, out var exist);
if (!exist || !info.Allocated)
{
return Result.Create(0u, ResultStatus.NotFound);
return ErrorStatus.NotFound;
}
return Result.Create((uint)info.viewGroup.srv.value, ResultStatus.Success);
return (uint)info.viewGroup.srv.value;
}
public string? GetResourceName(Handle<GPUResource> handle)