forked from Misaki/GhostEngine
Add sampler support and refactor resource handling
Enhanced shader and resource systems with `Sampler` support, including updates to `ShaderPropertyType`, HLSL code, and resource management. Refactored `Result` structs for better type safety and added new enums for texture and comparison settings. Improved `MeshRenderPass` to dynamically load textures and samplers. Updated SDL compiler and token lexicon for `Sampler` handling. Embedded debug info in project files and streamlined resource state tracking.
This commit is contained in:
@@ -234,6 +234,12 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
|
||||
resourceRecord.state = stateAfter;
|
||||
}
|
||||
|
||||
public void ResourceBarrier(Handle<GPUResource> resource, ResourceState stateAfter)
|
||||
{
|
||||
var stateBefore = _resourceDatabase.GetResourceState(resource);
|
||||
ResourceBarrier(resource, stateBefore, stateAfter);
|
||||
}
|
||||
|
||||
public void SetRenderTargets(ReadOnlySpan<Handle<Texture>> renderTargets, Handle<Texture> depthTarget)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
@@ -511,6 +517,8 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
|
||||
var pResource = _resourceDatabase.GetResource(buffer.AsResource());
|
||||
|
||||
_commandList.Get()->CopyBufferRegion(pResource, 0, uploadResource, 0, sizeInBytes);
|
||||
// D3D12 transition resource to COPY_DEST when copying
|
||||
_resourceDatabase.SetResourceState(buffer.AsResource(), ResourceState.CopyDest);
|
||||
}
|
||||
|
||||
public void UploadTexture(Handle<Texture> texture, ReadOnlySpan<SubResourceData> subresources)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Graphics.D3D12.Utilities;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TerraFX.Interop.DirectX;
|
||||
|
||||
@@ -34,7 +33,7 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
|
||||
#region RTV Methods
|
||||
|
||||
public Identifier<RTVDesc> AllocateRTV(bool dynamic = false)
|
||||
public Identifier<RTVDescriptor> AllocateRTV(bool dynamic = false)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -44,10 +43,10 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
throw new InvalidOperationException("Failed to allocate RTV descriptor");
|
||||
}
|
||||
|
||||
return new Identifier<RTVDesc>(index);
|
||||
return new Identifier<RTVDescriptor>(index);
|
||||
}
|
||||
|
||||
public Identifier<RTVDesc>[] AllocateRTVs(int count, bool dynamic = false)
|
||||
public Identifier<RTVDescriptor>[] AllocateRTVs(int count, bool dynamic = false)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -57,32 +56,32 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
throw new InvalidOperationException($"Failed to allocate {count} RTV descriptors");
|
||||
}
|
||||
|
||||
var descriptors = new Identifier<RTVDesc>[count];
|
||||
var descriptors = new Identifier<RTVDescriptor>[count];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var index = baseIndex + i;
|
||||
descriptors[i] = new Identifier<RTVDesc>(index);
|
||||
descriptors[i] = new Identifier<RTVDescriptor>(index);
|
||||
}
|
||||
|
||||
return descriptors;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(Identifier<RTVDesc> descriptor)
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(Identifier<RTVDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return _rtvHeap.GetCpuHandle(descriptor.value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Release(Identifier<RTVDesc> descriptor)
|
||||
public void Release(Identifier<RTVDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
_rtvHeap.ReleaseDescriptor(descriptor.value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Release(ReadOnlySpan<Identifier<RTVDesc>> descriptors)
|
||||
public void Release(ReadOnlySpan<Identifier<RTVDescriptor>> descriptors)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -93,14 +92,14 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void MakePersistent(Identifier<RTVDesc> descriptor)
|
||||
public void MakePersistent(Identifier<RTVDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
_rtvHeap.CopyToPersistentHeap(descriptor.value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void MakePersistent(ReadOnlySpan<Identifier<RTVDesc>> descriptors)
|
||||
public void MakePersistent(ReadOnlySpan<Identifier<RTVDescriptor>> descriptors)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
foreach (var descriptor in descriptors)
|
||||
@@ -120,7 +119,7 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
|
||||
#region DSV Methods
|
||||
|
||||
public Identifier<DSVDesc> AllocateDSV(bool dynamic = false)
|
||||
public Identifier<DSVDescriptor> AllocateDSV(bool dynamic = false)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -130,10 +129,10 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
throw new InvalidOperationException("Failed to allocate DSV descriptor");
|
||||
}
|
||||
|
||||
return new Identifier<DSVDesc>(index);
|
||||
return new Identifier<DSVDescriptor>(index);
|
||||
}
|
||||
|
||||
public Identifier<DSVDesc>[] AllocateDSVs(int count, bool dynamic = false)
|
||||
public Identifier<DSVDescriptor>[] AllocateDSVs(int count, bool dynamic = false)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -143,29 +142,29 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
throw new InvalidOperationException($"Failed to allocate {count} DSV descriptors");
|
||||
}
|
||||
|
||||
var descriptors = new Identifier<DSVDesc>[count];
|
||||
var descriptors = new Identifier<DSVDescriptor>[count];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var index = baseIndex + i;
|
||||
descriptors[i] = new Identifier<DSVDesc>(index);
|
||||
descriptors[i] = new Identifier<DSVDescriptor>(index);
|
||||
}
|
||||
|
||||
return descriptors;
|
||||
}
|
||||
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(Identifier<DSVDesc> descriptor)
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(Identifier<DSVDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return _dsvHeap.GetCpuHandle(descriptor.value);
|
||||
}
|
||||
|
||||
public void Release(Identifier<DSVDesc> descriptor)
|
||||
public void Release(Identifier<DSVDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
_dsvHeap.ReleaseDescriptor(descriptor.value);
|
||||
}
|
||||
|
||||
public void Release(ReadOnlySpan<Identifier<DSVDesc>> descriptors)
|
||||
public void Release(ReadOnlySpan<Identifier<DSVDescriptor>> descriptors)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -176,14 +175,14 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void MakePersistent(Identifier<DSVDesc> descriptor)
|
||||
public void MakePersistent(Identifier<DSVDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
_dsvHeap.CopyToPersistentHeap(descriptor.value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void MakePersistent(ReadOnlySpan<Identifier<DSVDesc>> descriptors)
|
||||
public void MakePersistent(ReadOnlySpan<Identifier<DSVDescriptor>> descriptors)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
foreach (var descriptor in descriptors)
|
||||
@@ -203,7 +202,7 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
|
||||
#region CBV_SRV_UAV Methods
|
||||
|
||||
public Identifier<CbvSrvUavDesc> AllocateCbvSrvUav(bool dynamic = false)
|
||||
public Identifier<CbvSrvUavDescriptor> AllocateCbvSrvUav(bool dynamic = false)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -214,10 +213,10 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
}
|
||||
|
||||
_cbvSrvUavHeap.CopyToShaderVisibleHeap(index);
|
||||
return new Identifier<CbvSrvUavDesc>(index);
|
||||
return new Identifier<CbvSrvUavDescriptor>(index);
|
||||
}
|
||||
|
||||
public Identifier<CbvSrvUavDesc>[] AllocateSRVs(int count, bool dynamic = false)
|
||||
public Identifier<CbvSrvUavDescriptor>[] AllocateSRVs(int count, bool dynamic = false)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -227,42 +226,42 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
throw new InvalidOperationException($"Failed to allocate {count} CBV/SRV/UAV descriptors");
|
||||
}
|
||||
|
||||
var descriptors = new Identifier<CbvSrvUavDesc>[count];
|
||||
var descriptors = new Identifier<CbvSrvUavDescriptor>[count];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var index = baseIndex + i;
|
||||
descriptors[i] = new Identifier<CbvSrvUavDesc>(index);
|
||||
descriptors[i] = new Identifier<CbvSrvUavDescriptor>(index);
|
||||
}
|
||||
|
||||
_cbvSrvUavHeap.CopyToShaderVisibleHeap(baseIndex, count);
|
||||
return descriptors;
|
||||
}
|
||||
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(Identifier<CbvSrvUavDesc> descriptor)
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(Identifier<CbvSrvUavDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return _cbvSrvUavHeap.GetCpuHandle(descriptor.value);
|
||||
}
|
||||
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandleShaderVisible(Identifier<CbvSrvUavDesc> descriptor)
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandleShaderVisible(Identifier<CbvSrvUavDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return _cbvSrvUavHeap.GetCpuHandleShaderVisible(descriptor.value);
|
||||
}
|
||||
|
||||
public D3D12_GPU_DESCRIPTOR_HANDLE GetGpuHandle(Identifier<CbvSrvUavDesc> descriptor)
|
||||
public D3D12_GPU_DESCRIPTOR_HANDLE GetGpuHandle(Identifier<CbvSrvUavDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return _cbvSrvUavHeap.GetGpuHandle(descriptor.value);
|
||||
}
|
||||
|
||||
public void Release(Identifier<CbvSrvUavDesc> descriptor)
|
||||
public void Release(Identifier<CbvSrvUavDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
_cbvSrvUavHeap.ReleaseDescriptor(descriptor.value);
|
||||
}
|
||||
|
||||
public void Release(ReadOnlySpan<Identifier<CbvSrvUavDesc>> descriptors)
|
||||
public void Release(ReadOnlySpan<Identifier<CbvSrvUavDescriptor>> descriptors)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -273,14 +272,14 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void MakePersistent(Identifier<CbvSrvUavDesc> descriptor)
|
||||
public void MakePersistent(Identifier<CbvSrvUavDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
_cbvSrvUavHeap.CopyToPersistentHeap(descriptor.value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void MakePersistent(ReadOnlySpan<Identifier<CbvSrvUavDesc>> descriptors)
|
||||
public void MakePersistent(ReadOnlySpan<Identifier<CbvSrvUavDescriptor>> descriptors)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
foreach (var descriptor in descriptors)
|
||||
@@ -300,7 +299,7 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
|
||||
#region Sampler Methods
|
||||
|
||||
public Identifier<SamplerDesc> AllocateSampler()
|
||||
public Identifier<SamplerDescriptor> AllocateSampler()
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -311,10 +310,10 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
}
|
||||
|
||||
_samplerHeap.CopyToShaderVisibleHeap(index);
|
||||
return new Identifier<SamplerDesc>(index);
|
||||
return new Identifier<SamplerDescriptor>(index);
|
||||
}
|
||||
|
||||
public Identifier<SamplerDesc>[] AllocateSamplers(int count)
|
||||
public Identifier<SamplerDescriptor>[] AllocateSamplers(int count)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -324,42 +323,42 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
|
||||
throw new InvalidOperationException($"Failed to allocate {count} Sampler descriptors");
|
||||
}
|
||||
|
||||
var descriptors = new Identifier<SamplerDesc>[count];
|
||||
var descriptors = new Identifier<SamplerDescriptor>[count];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var index = baseIndex + i;
|
||||
descriptors[i] = new Identifier<SamplerDesc>(index);
|
||||
descriptors[i] = new Identifier<SamplerDescriptor>(index);
|
||||
}
|
||||
|
||||
_samplerHeap.CopyToShaderVisibleHeap(baseIndex, count);
|
||||
return descriptors;
|
||||
}
|
||||
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(Identifier<SamplerDesc> descriptor)
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(Identifier<SamplerDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return _samplerHeap.GetCpuHandle(descriptor.value);
|
||||
}
|
||||
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandleShaderVisible(Identifier<SamplerDesc> descriptor)
|
||||
public D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandleShaderVisible(Identifier<SamplerDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return _samplerHeap.GetCpuHandleShaderVisible(descriptor.value);
|
||||
}
|
||||
|
||||
public D3D12_GPU_DESCRIPTOR_HANDLE GetGpuHandle(Identifier<SamplerDesc> descriptor)
|
||||
public D3D12_GPU_DESCRIPTOR_HANDLE GetGpuHandle(Identifier<SamplerDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return _samplerHeap.GetGpuHandle(descriptor.value);
|
||||
}
|
||||
|
||||
public void Release(Identifier<SamplerDesc> descriptor)
|
||||
public void Release(Identifier<SamplerDescriptor> descriptor)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
_samplerHeap.ReleaseDescriptor(descriptor.value);
|
||||
}
|
||||
|
||||
public void Release(ReadOnlySpan<Identifier<SamplerDesc>> descriptors)
|
||||
public void Release(ReadOnlySpan<Identifier<SamplerDescriptor>> descriptors)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using TerraFX.Interop.DirectX;
|
||||
|
||||
using static TerraFX.Aliases.D3D12_Alias;
|
||||
|
||||
namespace Ghost.Graphics.D3D12.Utilities;
|
||||
namespace Ghost.Graphics.D3D12;
|
||||
|
||||
internal unsafe struct D3D12DescriptorHeap : IDisposable
|
||||
{
|
||||
@@ -2,6 +2,7 @@ using Ghost.Core;
|
||||
using Ghost.Core.Graphics;
|
||||
using Ghost.Core.Utilities;
|
||||
using Ghost.Graphics.Core;
|
||||
using Ghost.Graphics.D3D12.Utilities;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
@@ -851,6 +852,35 @@ internal sealed unsafe partial class D3D12ResourceAllocator : IResourceAllocator
|
||||
return CreateBuffer(in desc, isTemp);
|
||||
}
|
||||
|
||||
public Identifier<Sampler> CreateSampler(ref readonly SamplerDesc desc)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
if (_resourceDatabase.TryGetSampler(in desc, out var id))
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
var samplerDesc = new D3D12_SAMPLER_DESC
|
||||
{
|
||||
Filter = desc.FilterMode.ToD3D12Filter(),
|
||||
AddressU = desc.AddressU.ToD3D12TextureAddressMode(),
|
||||
AddressV = desc.AddressV.ToD3D12TextureAddressMode(),
|
||||
AddressW = desc.AddressW.ToD3D12TextureAddressMode(),
|
||||
MipLODBias = desc.MipLODBias,
|
||||
MaxAnisotropy = desc.MaxAnisotropy,
|
||||
ComparisonFunc = desc.ComparisonFunc.ToD3D12ComparisonFunc(),
|
||||
MinLOD = desc.MinLOD,
|
||||
MaxLOD = desc.MaxLOD,
|
||||
};
|
||||
|
||||
var samplerDescriptor = _descriptorAllocator.AllocateSampler();
|
||||
var cpuHandle = _descriptorAllocator.GetCpuHandleShaderVisible(samplerDescriptor);
|
||||
_device.NativeDevice.Get()->CreateSampler(&samplerDesc, cpuHandle);
|
||||
|
||||
return _resourceDatabase.CreateSampler(in desc, samplerDescriptor.value);
|
||||
}
|
||||
|
||||
public Handle<Mesh> CreateMesh(UnsafeList<Vertex> vertices, UnsafeList<uint> indices)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
@@ -97,26 +97,30 @@ internal class D3D12ResourceDatabase : IResourceDatabase
|
||||
private readonly Dictionary<Handle<GPUResource>, string> _resourceName;
|
||||
#endif
|
||||
|
||||
private UnsafeHashMap<SamplerDesc, Identifier<Sampler>> _samplers;
|
||||
private UnsafeSlotMap<Mesh> _meshes;
|
||||
private UnsafeSlotMap<Material> _materials;
|
||||
private readonly DynamicArray<Shader?> _shaders; // NOTE: We use a simple list since shader is not frequently added/removed. This can save 4 bytes for each ecs component.
|
||||
private readonly Dictionary<ShaderPassKey, ShaderPass> _shaderPasses; // NOTE: The reason we use Dictionary here is that ShaderPassKey is a presistence identifier across multiple application sessions.
|
||||
|
||||
private int _lastSamplerId;
|
||||
private bool _disposed;
|
||||
|
||||
public D3D12ResourceDatabase(D3D12DescriptorAllocator descriptorAllocator)
|
||||
{
|
||||
_descriptorAllocator = descriptorAllocator;
|
||||
|
||||
_resources = new UnsafeSlotMap<ResourceRecord>(64, Allocator.Persistent, AllocationOption.Clear);
|
||||
#if DEBUG || GHOST_EDITOR
|
||||
_resourceName = new Dictionary<Handle<GPUResource>, string>(64);
|
||||
#endif
|
||||
|
||||
_samplers = new UnsafeHashMap<SamplerDesc, Identifier<Sampler>>(32, Allocator.Persistent);
|
||||
_meshes = new UnsafeSlotMap<Mesh>(64, Allocator.Persistent, AllocationOption.Clear);
|
||||
_materials = new UnsafeSlotMap<Material>(16, Allocator.Persistent, AllocationOption.Clear);
|
||||
_shaders = new DynamicArray<Shader?>(16);
|
||||
_shaderPasses = new Dictionary<ShaderPassKey, ShaderPass>(16);
|
||||
|
||||
_descriptorAllocator = descriptorAllocator;
|
||||
_lastSamplerId = -1;
|
||||
}
|
||||
|
||||
~D3D12ResourceDatabase()
|
||||
@@ -224,17 +228,17 @@ internal class D3D12ResourceDatabase : IResourceDatabase
|
||||
return GetResourceRecord(handle).desc;
|
||||
}
|
||||
|
||||
public int GetBindlessIndex(Handle<GPUResource> handle)
|
||||
public Result<uint, ResultStatus> GetBindlessIndex(Handle<GPUResource> handle)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
ref var info = ref GetResourceRecord(handle, out var exist);
|
||||
if (!exist || !info.Allocated)
|
||||
{
|
||||
return -1;
|
||||
return Result.Create(0u, ResultStatus.NotFound);
|
||||
}
|
||||
|
||||
return info.viewGroup.srv.value;
|
||||
return Result.Create((uint)info.viewGroup.srv.value, ResultStatus.NotFound);
|
||||
}
|
||||
|
||||
public string? GetResourceName(Handle<GPUResource> handle)
|
||||
@@ -266,7 +270,7 @@ internal class D3D12ResourceDatabase : IResourceDatabase
|
||||
}
|
||||
|
||||
info.Release(_descriptorAllocator);
|
||||
//Debug.Assert(info.Release(_descriptorAllocator) == 0);
|
||||
//System.Diagnostics.Debug.Assert(info.Release(_descriptorAllocator) == 0);
|
||||
#if DEBUG || GHOST_EDITOR
|
||||
_resourceName.Remove(handle, out var name);
|
||||
#endif
|
||||
@@ -274,6 +278,27 @@ internal class D3D12ResourceDatabase : IResourceDatabase
|
||||
_resources.Remove(handle.id, handle.generation);
|
||||
}
|
||||
|
||||
public Identifier<Sampler> CreateSampler(ref readonly SamplerDesc desc, int id)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
if (_samplers.ContainsKey(desc))
|
||||
{
|
||||
throw new InvalidOperationException("Sampler already exists.");
|
||||
}
|
||||
|
||||
var identifier = new Identifier<Sampler>(id);
|
||||
_samplers.Add(desc, identifier);
|
||||
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public bool TryGetSampler(ref readonly SamplerDesc desc, out Identifier<Sampler> id)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return _samplers.TryGetValue(desc, out id);
|
||||
}
|
||||
|
||||
public Handle<Mesh> AddMesh(ref readonly Mesh mesh)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
@@ -444,6 +469,7 @@ internal class D3D12ResourceDatabase : IResourceDatabase
|
||||
}
|
||||
|
||||
_resources.Dispose();
|
||||
_samplers.Dispose();
|
||||
_meshes.Dispose();
|
||||
_materials.Dispose();
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ internal unsafe class D3D12SwapChain : ISwapChain
|
||||
Format = desc.Format.ToDXGIFormat(),
|
||||
SampleDesc = new DXGI_SAMPLE_DESC(1, 0),
|
||||
BufferUsage = DXGI_USAGE_BACK_BUFFER | DXGI_USAGE_RENDER_TARGET_OUTPUT,
|
||||
BufferCount = D3D12PipelineResource.BACK_BUFFER_COUNT,
|
||||
BufferCount = desc.BufferCount,
|
||||
Scaling = DXGI_SCALING_STRETCH,
|
||||
SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD,
|
||||
AlphaMode = DXGI_ALPHA_MODE_IGNORE,
|
||||
|
||||
@@ -2,27 +2,27 @@ using Ghost.Core;
|
||||
|
||||
namespace Ghost.Graphics.D3D12;
|
||||
|
||||
internal readonly struct RTVDesc : IIdentifierType;
|
||||
internal readonly struct DSVDesc : IIdentifierType;
|
||||
internal readonly struct CbvSrvUavDesc : IIdentifierType;
|
||||
internal readonly struct SamplerDesc : IIdentifierType;
|
||||
internal readonly struct RTVDescriptor : IIdentifierType;
|
||||
internal readonly struct DSVDescriptor : IIdentifierType;
|
||||
internal readonly struct CbvSrvUavDescriptor : IIdentifierType;
|
||||
internal readonly struct SamplerDescriptor : IIdentifierType;
|
||||
|
||||
internal struct ResourceViewGroup
|
||||
{
|
||||
public Identifier<RTVDesc> rtv;
|
||||
public Identifier<DSVDesc> dsv;
|
||||
public Identifier<CbvSrvUavDesc> srv;
|
||||
public Identifier<CbvSrvUavDesc> cbv;
|
||||
public Identifier<CbvSrvUavDesc> uav;
|
||||
public Identifier<SamplerDesc> sampler;
|
||||
public Identifier<RTVDescriptor> rtv;
|
||||
public Identifier<DSVDescriptor> dsv;
|
||||
public Identifier<CbvSrvUavDescriptor> srv;
|
||||
public Identifier<CbvSrvUavDescriptor> cbv;
|
||||
public Identifier<CbvSrvUavDescriptor> uav;
|
||||
public Identifier<SamplerDescriptor> sampler;
|
||||
|
||||
public static ResourceViewGroup Invalid => new()
|
||||
{
|
||||
rtv = Identifier<RTVDesc>.Invalid,
|
||||
dsv = Identifier<DSVDesc>.Invalid,
|
||||
srv = Identifier<CbvSrvUavDesc>.Invalid,
|
||||
cbv = Identifier<CbvSrvUavDesc>.Invalid,
|
||||
uav = Identifier<CbvSrvUavDesc>.Invalid,
|
||||
sampler = Identifier<SamplerDesc>.Invalid,
|
||||
rtv = Identifier<RTVDescriptor>.Invalid,
|
||||
dsv = Identifier<DSVDescriptor>.Invalid,
|
||||
srv = Identifier<CbvSrvUavDescriptor>.Invalid,
|
||||
cbv = Identifier<CbvSrvUavDescriptor>.Invalid,
|
||||
uav = Identifier<CbvSrvUavDescriptor>.Invalid,
|
||||
sampler = Identifier<SamplerDescriptor>.Invalid,
|
||||
};
|
||||
}
|
||||
@@ -88,6 +88,64 @@ internal unsafe static class D3D12Utility
|
||||
};
|
||||
}
|
||||
|
||||
public static ResourceState ToResourceState(this D3D12_RESOURCE_STATES states)
|
||||
{
|
||||
return states switch
|
||||
{
|
||||
D3D12_RESOURCE_STATE_COMMON => ResourceState.Common,
|
||||
D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER => ResourceState.VertexAndConstantBuffer,
|
||||
D3D12_RESOURCE_STATE_INDEX_BUFFER => ResourceState.IndexBuffer,
|
||||
D3D12_RESOURCE_STATE_RENDER_TARGET => ResourceState.RenderTarget,
|
||||
D3D12_RESOURCE_STATE_UNORDERED_ACCESS => ResourceState.UnorderedAccess,
|
||||
D3D12_RESOURCE_STATE_DEPTH_WRITE => ResourceState.DepthWrite,
|
||||
D3D12_RESOURCE_STATE_DEPTH_READ => ResourceState.DepthRead,
|
||||
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE => ResourceState.PixelShaderResource,
|
||||
D3D12_RESOURCE_STATE_COPY_DEST => ResourceState.CopyDest,
|
||||
D3D12_RESOURCE_STATE_COPY_SOURCE => ResourceState.CopySource,
|
||||
_ => throw new ArgumentException($"Unknown D3D12 resource state: {states}")
|
||||
};
|
||||
}
|
||||
|
||||
public static D3D12_FILTER ToD3D12Filter(this TextureFilterMode filterMode)
|
||||
{
|
||||
return filterMode switch
|
||||
{
|
||||
TextureFilterMode.Point => D3D12_FILTER_MIN_MAG_MIP_POINT,
|
||||
TextureFilterMode.Bilinear => D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT,
|
||||
TextureFilterMode.Trilinear => D3D12_FILTER_MIN_MAG_MIP_LINEAR,
|
||||
TextureFilterMode.Anisotropic => D3D12_FILTER_ANISOTROPIC,
|
||||
_ => throw new ArgumentException($"Unknown texture filter mode: {filterMode}")
|
||||
};
|
||||
}
|
||||
|
||||
public static D3D12_TEXTURE_ADDRESS_MODE ToD3D12TextureAddressMode(this TextureAddressMode addressMode)
|
||||
{
|
||||
return addressMode switch
|
||||
{
|
||||
TextureAddressMode.Repeat => D3D12_TEXTURE_ADDRESS_MODE_WRAP,
|
||||
TextureAddressMode.Mirror => D3D12_TEXTURE_ADDRESS_MODE_MIRROR,
|
||||
TextureAddressMode.Clamp => D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
|
||||
TextureAddressMode.Border => D3D12_TEXTURE_ADDRESS_MODE_BORDER,
|
||||
TextureAddressMode.MirrorOnce => D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE,
|
||||
_ => throw new ArgumentException($"Unknown texture address mode: {addressMode}")
|
||||
};
|
||||
}
|
||||
|
||||
public static D3D12_COMPARISON_FUNC ToD3D12ComparisonFunc(this ComparisonFunction func)
|
||||
{
|
||||
return func switch
|
||||
{
|
||||
ComparisonFunction.Never => D3D12_COMPARISON_FUNC_NEVER,
|
||||
ComparisonFunction.Less => D3D12_COMPARISON_FUNC_LESS,
|
||||
ComparisonFunction.Equal => D3D12_COMPARISON_FUNC_EQUAL,
|
||||
ComparisonFunction.LessEqual => D3D12_COMPARISON_FUNC_LESS_EQUAL,
|
||||
ComparisonFunction.Greater => D3D12_COMPARISON_FUNC_GREATER,
|
||||
ComparisonFunction.NotEqual => D3D12_COMPARISON_FUNC_NOT_EQUAL,
|
||||
ComparisonFunction.GreaterEqual => D3D12_COMPARISON_FUNC_GREATER_EQUAL,
|
||||
ComparisonFunction.Always => D3D12_COMPARISON_FUNC_ALWAYS,
|
||||
_ => throw new ArgumentException($"Unknown comparison function: {func}")
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static D3D12_RASTERIZER_DESC D3D12_RASTERIZER_DESC_CREATE(
|
||||
|
||||
Reference in New Issue
Block a user