diff --git a/src/DirectX12GameEngine.Graphics/Buffers/Abstract/GraphicsResource.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs similarity index 90% rename from src/DirectX12GameEngine.Graphics/Buffers/Abstract/GraphicsResource.cs rename to src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs index 1ffa6721..46f42eec 100644 --- a/src/DirectX12GameEngine.Graphics/Buffers/Abstract/GraphicsResource.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs @@ -1,7 +1,7 @@ using System; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics.Buffers.Abstract +namespace ComputeSharp.Graphics.Buffers.Abstract { /// /// An abstract that represents a graphics resource object @@ -35,12 +35,12 @@ namespace DirectX12GameEngine.Graphics.Buffers.Abstract /// /// Gets the instance for the current resource /// - protected internal CpuDescriptorHandle NativeCpuDescriptorHandle { get; protected set; } + protected internal CpuDescriptorHandle? NativeCpuDescriptorHandle { get; protected set; } /// /// Gets the instance for the current resource /// - protected internal GpuDescriptorHandle NativeGpuDescriptorHandle { get; protected set; } + protected internal GpuDescriptorHandle? NativeGpuDescriptorHandle { get; protected set; } /// /// Maps the current resource to a specified subresource diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.cs new file mode 100644 index 00000000..703bc324 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Buffer.cs @@ -0,0 +1,175 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Helpers; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A representing a typed buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public class Buffer : GraphicsResource where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + /// The heap type for the current buffer + protected internal Buffer(GraphicsDevice device, int size, HeapType heapType) : base(device) + { + Size = size; + ElementSizeInBytes = Unsafe.SizeOf(); + SizeInBytes = Size * ElementSizeInBytes; + HeapType = heapType; + + ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None; + ResourceDescription description = ResourceDescription.Buffer(SizeInBytes, flags); + ResourceStates resourceStates = heapType switch + { + HeapType.Upload => ResourceStates.GenericRead, + HeapType.Readback => ResourceStates.CopyDestination, + _ => ResourceStates.Common + }; + + NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, resourceStates); + + (NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = heapType switch + { + HeapType.Default => CreateUnorderedAccessView(), + HeapType.Upload => CreateConstantBufferView(), + _ => default + }; + } + + /// + /// Creates the descriptors for a constant buffer + /// + /// The CPU and GPU handles for a constant buffer + [Pure] + private (CpuDescriptorHandle, GpuDescriptorHandle) CreateConstantBufferView() + { + (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); + + int constantBufferSize = (SizeInBytes + 255) & ~255; + + ConstantBufferViewDescription description = new ConstantBufferViewDescription + { + BufferLocation = NativeResource!.GPUVirtualAddress, + SizeInBytes = constantBufferSize + }; + + GraphicsDevice.NativeDevice.CreateConstantBufferView(description, cpuHandle); + + return (cpuHandle, gpuHandle); + } + + /// + /// Creates the descriptors for a read write buffer + /// + /// The CPU and GPU handles for a read write buffer + [Pure] + private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView() + { + (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); + + UnorderedAccessViewDescription description = new UnorderedAccessViewDescription + { + Format = SharpDX.DXGI.Format.R32_Float, + Dimension = UnorderedAccessViewDimension.Buffer, + Buffer = { ElementCount = Size } + }; + + GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle); + + return (cpuHandle, gpuHandle); + } + + /// + /// Gets the size of the current buffer, as in the number of values it contains + /// + public int Size { get; } + + /// + /// Gets the size in bytes of the current buffer + /// + public int SizeInBytes { get; } + + /// + /// Gets the size in bytes of each value contained in the buffer + /// + public int ElementSizeInBytes { get; } + + /// + /// Gets the heap type being targeted by the current buffer + /// + public HeapType HeapType { get; } + + /// + /// Reads the contents of the current instance and returns an array + /// + /// A array with the contents of the current buffer + [Pure] + public T[] GetData() + { + T[] data = new T[Size]; + GetData(data); + + return data; + } + + /// + /// Reads the contents of the current instance and writes them into a target + /// + /// The input to write data to + public void GetData(Span span) + { + // Create a temporary readback buffer if the current buffer type is read write (can't be read from directly) + if (HeapType == HeapType.Default) + { + using Buffer readbackBaffer = new Buffer(GraphicsDevice, Size, HeapType.Readback); + using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); + + copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes); + copyCommandList.Flush(true); + + readbackBaffer.GetData(span); + } + else + { + // Map and read the memory back directly + Map(0); + MemoryHelper.Copy(MappedResource, span); + Unmap(0); + } + } + + /// + /// Writes the contents of a given to the current instance + /// + /// The input to read data from + public void SetData(Span span) + { + // Create a temporary upload buffer if the current buffer type is read write (can't be written to directly) + if (HeapType == HeapType.Default) + { + using Buffer uploadBuffer = new Buffer(GraphicsDevice, Size, HeapType.Upload); + using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); + + uploadBuffer.SetData(span); + copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes); + copyCommandList.Flush(true); + } + else + { + // Map and copy the memory directly + Map(0); + MemoryHelper.Copy(span, MappedResource); + Unmap(0); + } + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs new file mode 100644 index 00000000..70dacfb5 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs @@ -0,0 +1,60 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + +namespace ComputeSharp.Graphics.Buffers.Extensions +{ + /// + /// A with extension methods for the type to allocate buffers + /// + public static class BufferExtensions + { + /// + /// Allocates a new read write buffer with the specified parameters + /// + /// The type of items to store in the buffer + /// The instance to use to allocate the buffer + /// The size of the buffer to allocate + /// A zeroed instance of size + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int size) where T : unmanaged + { + return new ReadWriteBuffer(device, size); + } + + /// + /// Allocates a new read write buffer with the specified parameters + /// + /// The type of items to store in the buffer + /// The instance to use to allocate the buffer + /// The input with the data to copy on the allocated buffer + /// A read write instance with the contents of the input + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Span span) where T : unmanaged + { + ReadWriteBuffer buffer = new ReadWriteBuffer(device, span.Length); + buffer.SetData(span); + + return buffer; + } + + /// + /// Allocates a new constant buffer with the specified parameters + /// + /// The type of items to store in the buffer + /// The instance to use to allocate the buffer + /// The input with the data to copy on the allocated buffer + /// A constant instance with the contents of the input + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlyBuffer AllocateConstantBuffer(this GraphicsDevice device, Span span) where T : unmanaged + { + ReadOnlyBuffer buffer = new ReadOnlyBuffer(device, span.Length); + buffer.SetData(span); + + return buffer; + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs new file mode 100644 index 00000000..c86d4084 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -0,0 +1,26 @@ +using System; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A representing a typed read write buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class ReadOnlyBuffer : Buffer where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Upload) { } + + /// + /// Gets a single value from the current read write buffer + /// + /// The index of the value to get + /// This API can only be used from a compute shader, and will always throw if used anywhere else + public T this[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs new file mode 100644 index 00000000..ded6d1e9 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -0,0 +1,30 @@ +using System; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A representing a typed read write buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class ReadWriteBuffer : Buffer where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Default) { } + + /// + /// Gets or sets a single value from the current read write buffer + /// + /// The index of the value to get or set + /// This API can only be used from a compute shader, and will always throw if used anywhere else + public T this[uint i] + { + get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + } + } +} diff --git a/src/DirectX12GameEngine.Graphics/CommandAllocatorPool.cs b/src/ComputeSharp.Graphics/CommandAllocatorPool.cs similarity index 98% rename from src/DirectX12GameEngine.Graphics/CommandAllocatorPool.cs rename to src/ComputeSharp.Graphics/CommandAllocatorPool.cs index 1c130675..a14dccd0 100644 --- a/src/DirectX12GameEngine.Graphics/CommandAllocatorPool.cs +++ b/src/ComputeSharp.Graphics/CommandAllocatorPool.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { internal sealed class CommandAllocatorPool : IDisposable { diff --git a/src/DirectX12GameEngine.Graphics/CommandList.cs b/src/ComputeSharp.Graphics/CommandList.cs similarity index 94% rename from src/DirectX12GameEngine.Graphics/CommandList.cs rename to src/ComputeSharp.Graphics/CommandList.cs index 341aa967..355f1981 100644 --- a/src/DirectX12GameEngine.Graphics/CommandList.cs +++ b/src/ComputeSharp.Graphics/CommandList.cs @@ -1,8 +1,8 @@ using System; -using DirectX12GameEngine.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Abstract; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { public sealed class CommandList : IDisposable { @@ -110,7 +110,8 @@ namespace DirectX12GameEngine.Graphics public void SetGraphicsRootDescriptorTable(int rootParameterIndex, GraphicsResource resource) { - SetGraphicsRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle); + if (resource.NativeGpuDescriptorHandle == null) throw new InvalidOperationException("Invalid graphics resource GPU descriptor"); + SetGraphicsRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle.Value); } public void SetGraphicsRootDescriptorTable(int rootParameterIndex, GpuDescriptorHandle baseDescriptor) @@ -130,7 +131,8 @@ namespace DirectX12GameEngine.Graphics public void SetComputeRootDescriptorTable(int rootParameterIndex, GraphicsResource resource) { - SetComputeRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle); + if (resource.NativeGpuDescriptorHandle == null) throw new InvalidOperationException("Invalid graphics resource GPU descriptor"); + SetComputeRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle.Value); } public void SetComputeRootDescriptorTable(int rootParameterIndex, GpuDescriptorHandle baseDescriptor) diff --git a/src/DirectX12GameEngine.Graphics/CompiledCommandList.cs b/src/ComputeSharp.Graphics/CompiledCommandList.cs similarity index 93% rename from src/DirectX12GameEngine.Graphics/CompiledCommandList.cs rename to src/ComputeSharp.Graphics/CompiledCommandList.cs index a4bc4f07..57e2a7ff 100644 --- a/src/DirectX12GameEngine.Graphics/CompiledCommandList.cs +++ b/src/ComputeSharp.Graphics/CompiledCommandList.cs @@ -1,6 +1,6 @@ using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { public sealed class CompiledCommandList { diff --git a/src/DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj similarity index 51% rename from src/DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj rename to src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj index e2b2653d..497079a7 100644 --- a/src/DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj +++ b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj @@ -8,11 +8,11 @@ + - - - - + + + diff --git a/src/DirectX12GameEngine.Graphics/DescriptorAllocator.cs b/src/ComputeSharp.Graphics/DescriptorAllocator.cs similarity index 98% rename from src/DirectX12GameEngine.Graphics/DescriptorAllocator.cs rename to src/ComputeSharp.Graphics/DescriptorAllocator.cs index ff795e41..5a041bf2 100644 --- a/src/DirectX12GameEngine.Graphics/DescriptorAllocator.cs +++ b/src/ComputeSharp.Graphics/DescriptorAllocator.cs @@ -1,7 +1,7 @@ using System; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { internal sealed class DescriptorAllocator : IDisposable { diff --git a/src/DirectX12GameEngine.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs similarity index 99% rename from src/DirectX12GameEngine.Graphics/GraphicsDevice.cs rename to src/ComputeSharp.Graphics/GraphicsDevice.cs index 42be36df..893b4a2c 100644 --- a/src/DirectX12GameEngine.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -3,7 +3,7 @@ using System.Threading; using SharpDX.Direct3D; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { public sealed class GraphicsDevice : IDisposable { diff --git a/src/DirectX12GameEngine.Graphics/GraphicsResourceUsage.cs b/src/ComputeSharp.Graphics/GraphicsResourceUsage.cs similarity index 75% rename from src/DirectX12GameEngine.Graphics/GraphicsResourceUsage.cs rename to src/ComputeSharp.Graphics/GraphicsResourceUsage.cs index b4299070..64448684 100644 --- a/src/DirectX12GameEngine.Graphics/GraphicsResourceUsage.cs +++ b/src/ComputeSharp.Graphics/GraphicsResourceUsage.cs @@ -1,4 +1,4 @@ -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { public enum GraphicsHeapType { diff --git a/src/DirectX12GameEngine.Core/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs similarity index 97% rename from src/DirectX12GameEngine.Core/Helpers/MemoryHelper.cs rename to src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs index 559eabee..6e3a516b 100644 --- a/src/DirectX12GameEngine.Core/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.CompilerServices; -namespace DirectX12GameEngine.Core.Helpers +namespace ComputeSharp.Graphics.Helpers { /// /// A with some helper methods to copy data between memory areas diff --git a/src/DirectX12GameEngine.Graphics/PipelineState.cs b/src/ComputeSharp.Graphics/PipelineState.cs similarity index 97% rename from src/DirectX12GameEngine.Graphics/PipelineState.cs rename to src/ComputeSharp.Graphics/PipelineState.cs index c74c8967..c7dcdc5e 100644 --- a/src/DirectX12GameEngine.Graphics/PipelineState.cs +++ b/src/ComputeSharp.Graphics/PipelineState.cs @@ -1,6 +1,6 @@ using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { /// /// A representing a custom pipeline state for a compute operation diff --git a/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj b/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj similarity index 80% rename from src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj rename to src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj index 936e6672..086f0e66 100644 --- a/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj +++ b/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj @@ -18,8 +18,7 @@ - - + @@ -36,11 +35,7 @@ - - - - - + PreserveNewest diff --git a/src/DirectX12GameEngine.Shaders/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs similarity index 95% rename from src/DirectX12GameEngine.Shaders/Mappings/HlslKnownMethods.cs rename to src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs index 6e33493c..143e087e 100644 --- a/src/DirectX12GameEngine.Shaders/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using Microsoft.CodeAnalysis; -namespace DirectX12GameEngine.Shaders.Mappings +namespace ComputeSharp.Shaders.Mappings { /// /// A that contains and maps known HLSL method names to common .NET method @@ -30,9 +30,9 @@ namespace DirectX12GameEngine.Shaders.Mappings ["System.MathF.PI"] = "3.14159274f", // ThreadIds - ["DirectX12GameEngine.Shaders.Primitives.ThreadIds.X"] = ".x", - ["DirectX12GameEngine.Shaders.Primitives.ThreadIds.Y"] = ".y", - ["DirectX12GameEngine.Shaders.Primitives.ThreadIds.Z"] = ".z", + ["ComputeSharp.ThreadIds.X"] = ".x", + ["ComputeSharp.ThreadIds.Y"] = ".y", + ["ComputeSharp.ThreadIds.Z"] = ".z", // Vector3 ["System.Numerics.Vector3.X"] = ".x", diff --git a/src/DirectX12GameEngine.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs similarity index 93% rename from src/DirectX12GameEngine.Shaders/Mappings/HlslKnownTypes.cs rename to src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index c6038358..32bf790d 100644 --- a/src/DirectX12GameEngine.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Numerics; using System.Text.RegularExpressions; -using DirectX12GameEngine.Shaders.Primitives; +using ComputeSharp.Graphics.Buffers; -namespace DirectX12GameEngine.Shaders.Mappings +namespace ComputeSharp.Shaders.Mappings { /// /// A that contains and maps known HLSL type names to common .NET types @@ -27,7 +27,7 @@ namespace DirectX12GameEngine.Shaders.Mappings [typeof(Vector4).FullName] = "float4", [typeof(Matrix4x4).FullName] = "float4x4", [typeof(ThreadIds).FullName] = "uint3", - [typeof(RWBufferResource<>).FullName] = "RWBuffer" + [typeof(ReadWriteBuffer<>).FullName] = "RWBuffer" }; /// @@ -54,13 +54,13 @@ namespace DirectX12GameEngine.Shaders.Mappings type == typeof(Matrix4x4); /// - /// Checks whether or not the input type is a value + /// Checks whether or not the input type is a value /// /// The input instance to check - /// A indicating whether the input is in fact a instance + /// A indicating whether the input is in fact a instance [Pure] public static bool IsReadWriteBufferType(Type type) => type.IsGenericType && - type.GetGenericTypeDefinition() == typeof(RWBufferResource<>); + type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>); /// /// Checks whether or not a given is an HLSL known type diff --git a/src/DirectX12GameEngine.Shaders/MethodDecompiler.cs b/src/ComputeSharp.Shaders/MethodDecompiler.cs similarity index 99% rename from src/DirectX12GameEngine.Shaders/MethodDecompiler.cs rename to src/ComputeSharp.Shaders/MethodDecompiler.cs index 031f2e21..0c09ad67 100644 --- a/src/DirectX12GameEngine.Shaders/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/MethodDecompiler.cs @@ -12,7 +12,7 @@ using ICSharpCode.Decompiler.Metadata; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { /// /// A that is able to decompile arbitrary methods diff --git a/src/DirectX12GameEngine.Shaders/Primitives/ThreadIds.cs b/src/ComputeSharp.Shaders/Primitives/ThreadIds.cs similarity index 91% rename from src/DirectX12GameEngine.Shaders/Primitives/ThreadIds.cs rename to src/ComputeSharp.Shaders/Primitives/ThreadIds.cs index 08cf64b3..219f4ef9 100644 --- a/src/DirectX12GameEngine.Shaders/Primitives/ThreadIds.cs +++ b/src/ComputeSharp.Shaders/Primitives/ThreadIds.cs @@ -1,4 +1,4 @@ -namespace DirectX12GameEngine.Shaders.Primitives +namespace ComputeSharp { /// /// A that indicates the ids of a given GPU thread running a compute shader diff --git a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs similarity index 93% rename from src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs rename to src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs index ead23005..ff7cc533 100644 --- a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs @@ -1,4 +1,4 @@ -namespace DirectX12GameEngine.Shaders.Renderer.Models.Abstract +namespace ComputeSharp.Shaders.Renderer.Models.Fields.Abstract { /// /// A that contains info on a shader field diff --git a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs similarity index 90% rename from src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs rename to src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs index aa453dab..1f8c3160 100644 --- a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs @@ -1,6 +1,6 @@ -using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; -namespace DirectX12GameEngine.Shaders.Renderer.Models.Fields +namespace ComputeSharp.Shaders.Renderer.Models.Fields { /// /// A that contains info on a constant buffer field diff --git a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs similarity index 90% rename from src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs rename to src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs index abfb06c9..e7280bc2 100644 --- a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs @@ -1,6 +1,6 @@ -using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; -namespace DirectX12GameEngine.Shaders.Renderer.Models.Fields +namespace ComputeSharp.Shaders.Renderer.Models.Fields { /// /// A that contains info on a read write buffer field diff --git a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs similarity index 90% rename from src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs rename to src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs index ff41f95e..ab6ca039 100644 --- a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs @@ -1,6 +1,6 @@ -using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; -namespace DirectX12GameEngine.Shaders.Renderer.Models.Fields +namespace ComputeSharp.Shaders.Renderer.Models.Fields { /// /// A that contains info on a static scalar field diff --git a/src/DirectX12GameEngine.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs similarity index 91% rename from src/DirectX12GameEngine.Shaders/Renderer/Models/ShaderInfo.cs rename to src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 64ba1e8d..598568ef 100644 --- a/src/DirectX12GameEngine.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -1,9 +1,9 @@ using System.Collections.Generic; -using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; #pragma warning disable CS8618 // Non-nullable field is uninitialized -namespace DirectX12GameEngine.Shaders.Renderer.Models +namespace ComputeSharp.Shaders.Renderer.Models { /// /// A that contains info to render a shader source to compile diff --git a/src/DirectX12GameEngine.Shaders/Renderer/ShaderRenderer.cs b/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs similarity index 92% rename from src/DirectX12GameEngine.Shaders/Renderer/ShaderRenderer.cs rename to src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs index 5533346f..f9aeb680 100644 --- a/src/DirectX12GameEngine.Shaders/Renderer/ShaderRenderer.cs +++ b/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs @@ -2,9 +2,9 @@ using System.Diagnostics.Contracts; using System.IO; using System.Reflection; -using DirectX12GameEngine.Shaders.Renderer.Models; +using ComputeSharp.Shaders.Renderer.Models; -namespace DirectX12GameEngine.Shaders.Renderer +namespace ComputeSharp.Shaders.Renderer { /// /// A that uses the mustache templating system to render HLSL shaders @@ -24,7 +24,7 @@ namespace DirectX12GameEngine.Shaders.Renderer string assemblyPath = Assembly.GetExecutingAssembly().Location, assemblyDirectory = Path.GetDirectoryName(assemblyPath) ?? throw new InvalidOperationException("Can't find the assembly directory"), - templatePath = Path.Combine(assemblyDirectory, "Assets", "ShaderTemplate.mustache"); + templatePath = Path.Combine(assemblyDirectory, "Renderer", "Templates", "ShaderTemplate.mustache"); Template = File.ReadAllText(templatePath); } diff --git a/src/DirectX12GameEngine.Shaders/Assets/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache similarity index 100% rename from src/DirectX12GameEngine.Shaders/Assets/ShaderTemplate.mustache rename to src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache diff --git a/src/DirectX12GameEngine.Shaders/ShaderCompiler.cs b/src/ComputeSharp.Shaders/ShaderCompiler.cs similarity index 98% rename from src/DirectX12GameEngine.Shaders/ShaderCompiler.cs rename to src/ComputeSharp.Shaders/ShaderCompiler.cs index ea64c675..fc21b010 100644 --- a/src/DirectX12GameEngine.Shaders/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/ShaderCompiler.cs @@ -3,7 +3,7 @@ using System.Diagnostics.Contracts; using DotNetDxc; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { /// /// A that uses the APIs to compile compute shaders diff --git a/src/DirectX12GameEngine.Shaders/ShaderLoader.cs b/src/ComputeSharp.Shaders/ShaderLoader.cs similarity index 91% rename from src/DirectX12GameEngine.Shaders/ShaderLoader.cs rename to src/ComputeSharp.Shaders/ShaderLoader.cs index 4f49fe48..aabd5bb8 100644 --- a/src/DirectX12GameEngine.Shaders/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/ShaderLoader.cs @@ -4,18 +4,17 @@ using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; -using DirectX12GameEngine.Graphics.Buffers.Abstract; -using DirectX12GameEngine.Shaders.Mappings; -using DirectX12GameEngine.Shaders.Primitives; -using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; -using DirectX12GameEngine.Shaders.Renderer.Models.Fields; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Shaders.Mappings; +using ComputeSharp.Shaders.Renderer.Models.Fields; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using SharpDX.Direct3D12; #pragma warning disable CS8618 // Non-nullable field is uninitialized -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { /// /// A responsible for loading and processing instances @@ -123,9 +122,7 @@ namespace DirectX12GameEngine.Shaders descriptorRanges.Add(range); // Reference to the underlying buffer - object readWriteBuffer = fieldInfo.GetValue(ShaderInstance); - GraphicsResource resource = (GraphicsResource)fieldType.GetProperty(nameof(RWBufferResource.Buffer)).GetValue(readWriteBuffer); - readWriteBuffers.Add(resource); + readWriteBuffers.Add((GraphicsResource)fieldValue); string typeName = HlslKnownTypes.GetMappedName(fieldType); processedFieldInfo = new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++); diff --git a/src/DirectX12GameEngine.Shaders/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs similarity index 99% rename from src/DirectX12GameEngine.Shaders/ShaderSyntaxRewriter.cs rename to src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs index f2e9c018..65063848 100644 --- a/src/DirectX12GameEngine.Shaders/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs @@ -4,7 +4,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { public class ShaderSyntaxRewriter : CSharpSyntaxRewriter { diff --git a/src/DirectX12GameEngine.Shaders/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs similarity index 95% rename from src/DirectX12GameEngine.Shaders/SyntaxNodeExtensions.cs rename to src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs index b2e0f0fb..7660be33 100644 --- a/src/DirectX12GameEngine.Shaders/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs @@ -1,10 +1,10 @@ using System.Linq; -using DirectX12GameEngine.Shaders.Mappings; +using ComputeSharp.Shaders.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { public static class SyntaxNodeExtensions { diff --git a/src/ComputeSharp.sln b/src/ComputeSharp.sln index e28fc3f4..a8415a79 100644 --- a/src/ComputeSharp.sln +++ b/src/ComputeSharp.sln @@ -3,13 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.29201.188 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Graphics", "DirectX12GameEngine.Graphics\DirectX12GameEngine.Graphics.csproj", "{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Graphics", "ComputeSharp.Graphics\ComputeSharp.Graphics.csproj", "{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Shaders", "DirectX12GameEngine.Shaders\DirectX12GameEngine.Shaders.csproj", "{23AE6EED-79CD-4ADD-9A85-0148BD957D19}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Shaders", "ComputeSharp.Shaders\ComputeSharp.Shaders.csproj", "{23AE6EED-79CD-4ADD-9A85-0148BD957D19}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Core", "DirectX12GameEngine.Core\DirectX12GameEngine.Core.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp", "ComputeSharp\ComputeSharp.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DirectX12ComputeShaderSample", "DirectX12ComputeShaderSample\DirectX12ComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12ComputeShaderSample", "DirectX12ComputeShaderSample\DirectX12ComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/DirectX12GameEngine.Core/DirectX12GameEngine.Core.csproj b/src/ComputeSharp/ComputeSharp.csproj similarity index 100% rename from src/DirectX12GameEngine.Core/DirectX12GameEngine.Core.csproj rename to src/ComputeSharp/ComputeSharp.csproj diff --git a/src/DirectX12GameEngine.Core/Extensions/PointExtensions.cs b/src/ComputeSharp/Extensions/PointExtensions.cs similarity index 100% rename from src/DirectX12GameEngine.Core/Extensions/PointExtensions.cs rename to src/ComputeSharp/Extensions/PointExtensions.cs diff --git a/src/DirectX12GameEngine.Core/Extensions/RectExtensions.cs b/src/ComputeSharp/Extensions/RectExtensions.cs similarity index 100% rename from src/DirectX12GameEngine.Core/Extensions/RectExtensions.cs rename to src/ComputeSharp/Extensions/RectExtensions.cs diff --git a/src/DirectX12GameEngine.Core/Extensions/SizeExtensions.cs b/src/ComputeSharp/Extensions/SizeExtensions.cs similarity index 100% rename from src/DirectX12GameEngine.Core/Extensions/SizeExtensions.cs rename to src/ComputeSharp/Extensions/SizeExtensions.cs diff --git a/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj b/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj index 0823373d..a9e9535e 100644 --- a/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj +++ b/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/src/DirectX12ComputeShaderSample/Gpu.cs b/src/DirectX12ComputeShaderSample/Gpu.cs index 65f9f084..970ae4b7 100644 --- a/src/DirectX12ComputeShaderSample/Gpu.cs +++ b/src/DirectX12ComputeShaderSample/Gpu.cs @@ -1,13 +1,13 @@ using System; using System.Linq; -using DirectX12GameEngine.Graphics; -using DirectX12GameEngine.Shaders; -using DirectX12GameEngine.Shaders.Primitives; -using DirectX12GameEngine.Shaders.Renderer; -using DirectX12GameEngine.Shaders.Renderer.Models; +using ComputeSharp; +using ComputeSharp.Graphics; +using ComputeSharp.Shaders; +using ComputeSharp.Shaders.Renderer; +using ComputeSharp.Shaders.Renderer.Models; using SharpDX.Direct3D12; -using CommandList = DirectX12GameEngine.Graphics.CommandList; -using PipelineState = DirectX12GameEngine.Graphics.PipelineState; +using CommandList = ComputeSharp.Graphics.CommandList; +using PipelineState = ComputeSharp.Graphics.PipelineState; using ShaderBytecode = SharpDX.Direct3D12.ShaderBytecode; namespace DirectX12ComputeShaderSample diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 48be6ecd..813bd5b1 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -1,9 +1,8 @@ -using DirectX12GameEngine.Shaders; -using System; +using System; using System.Linq; -using DirectX12GameEngine.Graphics.Buffers; -using DirectX12GameEngine.Shaders.Primitives; -using Buffer = DirectX12GameEngine.Graphics.Buffers.Buffer; +using ComputeSharp; +using ComputeSharp.Graphics.Buffers; +using ComputeSharp.Graphics.Buffers.Extensions; namespace DirectX12ComputeShaderSample { @@ -20,16 +19,12 @@ namespace DirectX12ComputeShaderSample array[i] = i + 1; } - using Buffer gpuBuffer = Buffer.UnorderedAccess.New(Gpu.Default, array.AsSpan()); - - // Variables for closure - uint size = (uint)width; - var data = gpuBuffer.GetGpuResource(); + using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array.AsSpan()); // Shader body Action action = id => { - data[id.X + id.Y * size] *= 2; + gpuBuffer[id.X + id.Y * (uint)width] *= 2; }; // Run the shader diff --git a/src/DirectX12GameEngine.Graphics/BufferDescription.cs b/src/DirectX12GameEngine.Graphics/BufferDescription.cs deleted file mode 100644 index 89159df8..00000000 --- a/src/DirectX12GameEngine.Graphics/BufferDescription.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace DirectX12GameEngine.Graphics -{ - public struct BufferDescription - { - public BufferDescription(int sizeInBytes, BufferFlags bufferFlags, GraphicsHeapType heapType) - { - SizeInBytes = sizeInBytes; - Flags = bufferFlags; - HeapType = heapType; - } - - public int SizeInBytes; - - public BufferFlags Flags; - - public GraphicsHeapType HeapType; - } -} diff --git a/src/DirectX12GameEngine.Graphics/BufferFlags.cs b/src/DirectX12GameEngine.Graphics/BufferFlags.cs deleted file mode 100644 index 1c0a26b5..00000000 --- a/src/DirectX12GameEngine.Graphics/BufferFlags.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; - -namespace DirectX12GameEngine.Graphics -{ - [Flags] - public enum BufferFlags - { - None = 0, - ConstantBuffer = 1, - IndexBuffer = 2, - VertexBuffer = 4, - RenderTarget = 8, - ShaderResource = 16, - UnorderedAccess = 32, - StructuredBuffer = 64, - RawBuffer = 512, - ArgumentBuffer = 1024 - } -} diff --git a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.Constant.cs b/src/DirectX12GameEngine.Graphics/Buffers/Buffer.Constant.cs deleted file mode 100644 index 7745734e..00000000 --- a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.Constant.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; - -namespace DirectX12GameEngine.Graphics.Buffers -{ - public partial class Buffer - { - public static class Constant - { - public static unsafe Buffer New(GraphicsDevice device, int size, GraphicsHeapType heapType = GraphicsHeapType.Upload) - { - return Buffer.New(device, size, BufferFlags.ConstantBuffer, heapType); - } - - public static unsafe Buffer New(GraphicsDevice device, in T data, GraphicsHeapType heapType = GraphicsHeapType.Upload) where T : unmanaged - { - return Buffer.New(device, data, BufferFlags.ConstantBuffer, heapType); - } - - public static unsafe Buffer New(GraphicsDevice device, Span data, GraphicsHeapType heapType = GraphicsHeapType.Upload) where T : unmanaged - { - return Buffer.New(device, data, BufferFlags.ConstantBuffer, heapType); - } - } - } -} diff --git a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.UnorderedAccess.cs b/src/DirectX12GameEngine.Graphics/Buffers/Buffer.UnorderedAccess.cs deleted file mode 100644 index 20ec576d..00000000 --- a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.UnorderedAccess.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; - -namespace DirectX12GameEngine.Graphics.Buffers -{ - public partial class Buffer - { - public static class UnorderedAccess - { - public static unsafe Buffers.Buffer New(GraphicsDevice device, int size, GraphicsHeapType heapType = GraphicsHeapType.Default) - { - return Buffers.Buffer.New(device, size, BufferFlags.UnorderedAccess, heapType); - } - - public static unsafe Buffer New(GraphicsDevice device, in T data, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - return Buffers.Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType); - } - - public static unsafe Buffer New(GraphicsDevice device, Span data, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - return Buffers.Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType); - } - } - } -} \ No newline at end of file diff --git a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.cs b/src/DirectX12GameEngine.Graphics/Buffers/Buffer.cs deleted file mode 100644 index db4052e0..00000000 --- a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.cs +++ /dev/null @@ -1,232 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using DirectX12GameEngine.Core.Helpers; -using DirectX12GameEngine.Graphics.Buffers.Abstract; -using SharpDX.Direct3D12; - -namespace DirectX12GameEngine.Graphics.Buffers -{ - public partial class Buffer : GraphicsResource - { - protected Buffer(GraphicsDevice device) : base(device) - { - } - - public BufferDescription Description { get; private set; } - - public int SizeInBytes => Description.SizeInBytes; - - public BufferFlags Flags => Description.Flags; - - public GraphicsHeapType Usage => Description.HeapType; - - public int ElementCount { get; protected set; } - - public T[] GetData() where T : unmanaged - { - T[] data = new T[SizeInBytes / Unsafe.SizeOf()]; - GetData(data.AsSpan()); - - return data; - } - - public unsafe void GetData(ref T data, int offsetInBytes = 0) where T : unmanaged - { - fixed (T* pointer = &data) - { - GetData(new Span(pointer, 1), offsetInBytes); - } - } - - public void GetData(Span data, int offsetInBytes = 0) where T : unmanaged - { - if (Usage == GraphicsHeapType.Default) - { - using Buffer readbackBaffer = New(GraphicsDevice, data.Length, BufferFlags.None, GraphicsHeapType.Readback); - using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - - copyCommandList.CopyBufferRegion(this, offsetInBytes, readbackBaffer, 0, data.Length * Unsafe.SizeOf()); - copyCommandList.Flush(true); - - readbackBaffer.GetData(data); - } - else - { - Map(0); - MemoryHelper.Copy(MappedResource + offsetInBytes, data); - Unmap(0); - } - } - - public unsafe void SetData(in T data, int offsetInBytes = 0) where T : unmanaged - { - fixed (T* pointer = &data) - { - SetData(new Span(pointer, 1), offsetInBytes); - } - } - - public void SetData(Span data, int offsetInBytes = 0) where T : unmanaged - { - if (Usage == GraphicsHeapType.Default) - { - using Buffer uploadBuffer = New(GraphicsDevice, data, BufferFlags.None, GraphicsHeapType.Upload); - using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - - copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, offsetInBytes, data.Length * Unsafe.SizeOf()); - copyCommandList.Flush(true); - } - else - { - Map(0); - MemoryHelper.Copy(data, MappedResource + offsetInBytes); - Unmap(0); - } - } - - public Buffers.Buffer ToReadback() - { - BufferDescription description = Description; - description.HeapType = GraphicsHeapType.Readback; - description.Flags = BufferFlags.None; - - return New(GraphicsDevice, description); - } - - public static Buffers.Buffer New(GraphicsDevice device, BufferDescription description) - { - return new Buffers.Buffer(device).InitializeFrom(description); - } - - public static Buffers.Buffer New(GraphicsDevice device, int size, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) - { - return New(device, new BufferDescription(size, bufferFlags, heapType)); - } - - public static Buffer New(GraphicsDevice device, int elementCount, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - int bufferSize = elementCount * Unsafe.SizeOf(); - - return new Buffer(device, bufferSize, bufferFlags, heapType); - } - - public static unsafe Buffer New(GraphicsDevice device, in T data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - fixed (T* pointer = &data) - { - return New(device, new Span(pointer, 1), bufferFlags, heapType); - } - } - - public static Buffer New(GraphicsDevice device, Span data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - Buffer buffer = New(device, data.Length, bufferFlags, heapType); - buffer.SetData(data); - - return buffer; - } - - protected internal Buffers.Buffer InitializeFrom(BufferDescription description) - { - ResourceStates resourceStates = ResourceStates.Common; - - if (description.HeapType == GraphicsHeapType.Upload) - { - resourceStates = ResourceStates.GenericRead; - } - else if (description.HeapType == GraphicsHeapType.Readback) - { - resourceStates = ResourceStates.CopyDestination; - } - - NativeResource ??= GraphicsDevice.NativeDevice.CreateCommittedResource( - new HeapProperties((HeapType)description.HeapType), HeapFlags.None, - ConvertToNativeDescription(description), resourceStates); - - Description = description; - - (NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = description.Flags switch - { - BufferFlags.ConstantBuffer => CreateConstantBufferView(), - BufferFlags.ShaderResource => CreateShaderResourceView(), - BufferFlags.UnorderedAccess => CreateUnorderedAccessView(), - _ => default - }; - - return this; - } - - private static ResourceDescription ConvertToNativeDescription(BufferDescription description) - { - int size = description.SizeInBytes; - ResourceFlags flags = ResourceFlags.None; - - if (description.Flags.HasFlag(BufferFlags.UnorderedAccess)) - { - flags |= ResourceFlags.AllowUnorderedAccess; - //flags |= ResourceFlags.AllowSimultaneousAccess; - } - - return ResourceDescription.Buffer(size, flags); - } - - private (CpuDescriptorHandle, GpuDescriptorHandle) CreateConstantBufferView() - { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); - - int constantBufferSize = (SizeInBytes + 255) & ~255; - - ConstantBufferViewDescription cbvDescription = new ConstantBufferViewDescription - { - BufferLocation = NativeResource.GPUVirtualAddress, - SizeInBytes = constantBufferSize - }; - - GraphicsDevice.NativeDevice.CreateConstantBufferView(cbvDescription, cpuHandle); - - return (cpuHandle, gpuHandle); - } - - private (CpuDescriptorHandle, GpuDescriptorHandle) CreateShaderResourceView() - { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); - GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, null, cpuHandle); - - return (cpuHandle, gpuHandle); - } - - private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView() - { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); - - var description = new UnorderedAccessViewDescription - { - Format = SharpDX.DXGI.Format.R32_Float, - Dimension = UnorderedAccessViewDimension.Buffer, - Buffer = - { - ElementCount = ElementCount, - } - }; - - GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle); - - return (cpuHandle, gpuHandle); - } - } - - public class Buffer : Buffers.Buffer where T : unmanaged - { - protected internal Buffer(GraphicsDevice device, int size, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) : base(device) - { - ElementSize = Unsafe.SizeOf(); - ElementCount = size / ElementSize; - - InitializeFrom(new BufferDescription(size, bufferFlags, heapType)); - } - - public int ElementSize { get; } - - public T[] GetData() => GetData(); - } -} diff --git a/src/DirectX12GameEngine.Shaders/ShaderResource.cs b/src/DirectX12GameEngine.Shaders/ShaderResource.cs deleted file mode 100644 index 2cf469ec..00000000 --- a/src/DirectX12GameEngine.Shaders/ShaderResource.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Numerics; -using DirectX12GameEngine.Graphics.Buffers; -using Buffer = System.Buffer; - -namespace DirectX12GameEngine.Shaders -{ - public abstract class ShaderResource - { - } - - public sealed class RWBufferResource : ShaderResource where T : unmanaged - { - internal RWBufferResource(Buffer buffer) => Buffer = buffer; - - public Buffer Buffer { get; } - - public T this[uint index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - } - - public static class RWBufferResourceExtensions - { - public static RWBufferResource GetGpuResource(this Buffer buffer) where T : unmanaged - { - return new RWBufferResource(buffer); - } - } -}