diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs index a6a3cd5b..652ee3ba 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs @@ -11,53 +11,43 @@ partial class IShaderGenerator partial class LoadDispatchData { /// - /// Writes the LoadDispatchData method. + /// Writes the LoadConstantBuffer method. /// /// The input instance with gathered shader info. /// The instance to write into. - public static void WriteSyntax(ShaderInfo info, IndentedTextWriter writer) + public static void WriteLoadConstantBufferSyntax(ShaderInfo info, IndentedTextWriter writer) { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]"); - writer.WriteLine("readonly void global::ComputeSharp.__Internals.IShader.LoadDispatchData(ref TLoader loader, global::ComputeSharp.GraphicsDevice device, int x, int y, int z)"); + writer.WriteLine("readonly void global::ComputeSharp.__Internals.IShader.LoadConstantBuffer(ref TLoader loader, int x, int y, int z)"); using (writer.WriteBlock()) { - writer.WriteLine($"global::System.Span span0 = stackalloc uint[{info.ConstantBufferSizeInBytes}];"); - writer.WriteLineIf($"global::System.Span span1 = stackalloc ulong[{info.ResourceCount}];", info.ResourceCount > 0); - writer.WriteLine("ref uint r0 = ref span0[0];"); - writer.WriteLineIf("ref ulong r1 = ref span1[0];", info.ResourceCount > 0); + writer.WriteLine($"global::System.Span span = stackalloc byte[{info.ConstantBufferSizeInBytes}];"); // Append the statements for the dispatch ranges - writer.WriteLine("span0[0] = (uint)x;"); - writer.WriteLine("span0[1] = (uint)y;"); - writer.WriteLineIf("span0[2] = (uint)z;", !info.IsPixelShaderLike); + writer.WriteLine("global::System.Runtime.CompilerServices.Unsafe.As(ref span[0]) = (uint)x;"); + writer.WriteLine("global::System.Runtime.CompilerServices.Unsafe.As(ref span[4]) = (uint)y;"); + writer.WriteLineIf("global::System.Runtime.CompilerServices.Unsafe.As(ref span[8]) = (uint)z;", !info.IsPixelShaderLike); // Generate loading statements for each captured field foreach (FieldInfo fieldInfo in info.Fields) { switch (fieldInfo) { - case FieldInfo.Resource resource: - - // Validate the resource and get a handle for it - writer.WriteLine( - $"global::System.Runtime.CompilerServices.Unsafe.Add(ref r1, {resource.Offset}) = " + - $"global::ComputeSharp.__Internals.GraphicsResourceHelper.ValidateAndGetGpuDescriptorHandle({resource.FieldName}, device);"); - break; case FieldInfo.Primitive { TypeName: "System.Boolean" } primitive: // Read a boolean value and cast it to Bool first, which will apply the correct size expansion writer.WriteLine( - $"global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){primitive.Offset})) = " + + $"global::System.Runtime.CompilerServices.Unsafe.As(ref span[{primitive.Offset}]) = " + $"(global::ComputeSharp.Bool){string.Join(".", primitive.FieldPath)};"); break; case FieldInfo.Primitive primitive: // Read a primitive value and serialize it into the target buffer writer.WriteLine( - $"global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){primitive.Offset})) = " + + $"global::System.Runtime.CompilerServices.Unsafe.As(ref span[{primitive.Offset}]) = " + $"{string.Join(".", primitive.FieldPath)};"); break; @@ -82,7 +72,7 @@ partial class IShaderGenerator for (int j = 0; j < matrix.Rows; j++) { writer.WriteLine( - $"global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){matrix.Offsets[j]})) = " + + $"global::System.Runtime.CompilerServices.Unsafe.As(ref span[{matrix.Offsets[j]}]) = " + $"global::System.Runtime.CompilerServices.Unsafe.Add(ref {rowLocalName}, {j});"); } @@ -90,9 +80,32 @@ partial class IShaderGenerator } } - // Load the prepared buffers - writer.WriteLine("loader.LoadCapturedValues(span0);"); - writer.WriteLineIf("loader.LoadCapturedResources(span1);", info.ResourceCount > 0); + // Load the prepared buffer + writer.WriteLine("loader.LoadConstantBuffer(span);"); + } + } + + /// + /// Writes the LoadGraphicsResources method. + /// + /// The input instance with gathered shader info. + /// The instance to write into. + public static void WriteLoadGraphicsResourcesSyntax(ShaderInfo info, IndentedTextWriter writer) + { + writer.WriteLine("/// "); + writer.WriteGeneratedAttributes(GeneratorName); + writer.WriteLine("readonly void global::ComputeSharp.__Internals.IShader.LoadGraphicsResources(ref TLoader loader)"); + + using (writer.WriteBlock()) + { + // Generate loading statements for each captured resource + foreach (FieldInfo fieldInfo in info.Fields) + { + if (fieldInfo is FieldInfo.Resource resource) + { + writer.WriteLine($"loader.LoadGraphicsResource({resource.FieldName}, {resource.Offset});"); + } + } } } } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs index a026863c..6048c79b 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -27,8 +27,8 @@ partial class IShaderGenerator /// /// The collection of produced instances. /// The current shader type being explored. - /// The size of the shader constant buffer. /// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture. + /// The size of the shader constant buffer. /// The total number of captured resources in the shader. /// The sequence of instances for all captured resources and values. public static ImmutableArray GetInfo( @@ -108,7 +108,7 @@ partial class IShaderGenerator // the local variables is padded to a multiple of a 32 bit value. This is necessary to // enable loading all the dispatch data after reinterpreting it to a sequence of values // of size 32 bits (via SetComputeRoot32BitConstants), without reading out of bounds. - constantBufferSizeInBytes = AlignmentHelper.Pad(rawDataOffsetAsBox.Value, sizeof(int)) / sizeof(int); + constantBufferSizeInBytes = AlignmentHelper.Pad(rawDataOffsetAsBox.Value, sizeof(int)); // A shader root signature has a maximum size of 64 DWORDs, so 256 bytes. // Loaded values in the root signature have the following costs: @@ -118,9 +118,9 @@ partial class IShaderGenerator // So here we check whether the current signature respects that constraint, // and emit a build error otherwise. For more info on this, see the docs here: // https://docs.microsoft.com/windows/win32/direct3d12/root-signature-limits. - int rootSignatureDwordSize = constantBufferSizeInBytes + resourceCount; + int root32BitConstantCount = (constantBufferSizeInBytes / sizeof(int)) + resourceCount; - if (rootSignatureDwordSize > 64) + if (root32BitConstantCount > 64) { diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol); } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs index 4e448bdc..55741a13 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs @@ -16,13 +16,11 @@ partial class IShaderGenerator /// /// Gets the data related to the shader metadata for a given shader type. /// - /// The total number of needed 32 bit constants in the shader root signature. /// Indicates whether the current shader uses an implicit texture. /// Whether the static sampler is used by the shader. /// The sequence of captured fields for the shader. /// The metadata info for the shader. public static ImmutableArray GetInfo( - int root32BitConstantCount, bool isImplicitTextureUsed, bool isSamplerUsed, ImmutableArray capturedFields) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 27f554a0..f1d5f259 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -59,7 +59,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator diagnostics, typeSymbol, isPixelShaderLike, - out int root32BitConstantCount, + out int constantBufferSizeInBytes, out int resourceCount); token.ThrowIfCancellationRequested(); @@ -92,7 +92,6 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator // GetDispatchMetadata() info ImmutableArray resourceDescriptors = LoadDispatchMetadata.GetInfo( - root32BitConstantCount, isImplicitTextureUsed, isSamplerUsed, fieldInfos); @@ -123,7 +122,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator ThreadsZ: threadsZ, IsPixelShaderLike: isPixelShaderLike, IsSamplerUsed: isSamplerUsed, - ConstantBufferSizeInBytes: root32BitConstantCount, + ConstantBufferSizeInBytes: constantBufferSizeInBytes, ResourceCount: resourceCount, Fields: fieldInfos, ResourceDescriptors: resourceDescriptors, @@ -153,7 +152,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator declaredMembers.Add(LoadDispatchMetadata.WriteSyntax); declaredMembers.Add(BuildHlslSource.WriteSyntax); declaredMembers.Add(LoadBytecode.WriteHlslBytecodeSyntax); - declaredMembers.Add(LoadDispatchData.WriteSyntax); + declaredMembers.Add(LoadDispatchData.WriteLoadConstantBufferSyntax); + declaredMembers.Add(LoadDispatchData.WriteLoadGraphicsResourcesSyntax); using ImmutableArrayBuilder> additionalTypes = new(); using ImmutableHashSetBuilder usingDirectives = new(); diff --git a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs index 70370257..4cf56167 100644 --- a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs @@ -12,7 +12,7 @@ namespace ComputeSharp.SourceGenerators.Models; /// The thread ids value for the Z axis. /// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture. /// Whether or not the static sampler is used. -/// /// The size of the shader constant buffer. +/// The size of the shader constant buffer. /// The total number of captured resources in the shader. /// The description on shader instance fields. /// The sequence of resource descriptors for the shader. diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IConstantBufferLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IConstantBufferLoader.cs new file mode 100644 index 00000000..76c584af --- /dev/null +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IConstantBufferLoader.cs @@ -0,0 +1,16 @@ +using System; + +namespace ComputeSharp.__Internals; + +/// +/// A type representing a data loader for the constant buffer of a compute shader. +/// +public interface IConstantBufferLoader +{ + /// + /// Loads the constant buffer of a compute shader. + /// + /// The constant buffer for the compute shader (the size must be a multiple of the size of a DWORD value). + /// Thrown if the size of is not a multiple of the size of a DWORD value). + void LoadConstantBuffer(ReadOnlySpan data); +} \ No newline at end of file diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchDataLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchDataLoader.cs deleted file mode 100644 index 9fddceaf..00000000 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchDataLoader.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.ComponentModel; - -namespace ComputeSharp.__Internals; - -/// -/// A base representing a data loader for a shader being dispatched. -/// -[EditorBrowsable(EditorBrowsableState.Never)] -[Obsolete("This interface is not intended to be used directly by user code")] -public interface IDispatchDataLoader -{ - /// - /// Loads the captured values used by the shader to be dispatched. - /// - /// The sequence of serialized captured values. - void LoadCapturedValues(ReadOnlySpan data); - - /// - /// Loads the captured resources used by the shader to be dispatched. - /// - /// The sequence of serialized resources. - void LoadCapturedResources(ReadOnlySpan data); -} \ No newline at end of file diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IGraphicsResourceLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IGraphicsResourceLoader.cs new file mode 100644 index 00000000..d5edd8c8 --- /dev/null +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IGraphicsResourceLoader.cs @@ -0,0 +1,19 @@ +using System; + +namespace ComputeSharp.__Internals; + +/// +/// A type representing a loader object for values used in compute shaders. +/// +public interface IGraphicsResourceLoader +{ + /// + /// Loads a resource used by the shader to be dispatched. + /// + /// The input resource to be loaded. + /// The index to use to bind the resource to the shader. + /// Thrown if is . + /// Thrown if is not a valid resource object. + /// Thrown if the resource isn't associated with the same device used to dispatch the shader. + void LoadGraphicsResource(IGraphicsResource resource, uint index); +} \ No newline at end of file diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs index df79288c..1669dcc5 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -32,7 +32,7 @@ public interface IShader /// /// Constant buffer data is bound to shaders via 32 bit root constants, and loaded before dispatching via /// ID3D12GraphicsCommandList::SetComputeRoot32BitConstants, - /// so the size must be a multiple of 4. + /// so the size must be a multiple of the size of a DWORD value (ie. 4 bytes). /// int ConstantBufferSize { get; } @@ -60,16 +60,21 @@ public interface IShader ReadOnlyMemory HlslBytecode { get; } /// - /// Loads the dispatch data for the shader. + /// Loads the constant buffer of a given input shader. /// /// The type of data loader being used. /// The instance to use to load the data. - /// The device the shader is being dispatched on. /// The number of iterations to run on the X axis. /// The number of iterations to run on the Y axis. /// The number of iterations to run on the Z axis. - [EditorBrowsable(EditorBrowsableState.Never)] - [Obsolete("This method is not intended to be called directly by user code")] - void LoadDispatchData(ref TLoader loader, GraphicsDevice device, int x, int y, int z) - where TLoader : struct, IDispatchDataLoader; + void LoadConstantBuffer(ref TLoader loader, int x, int y, int z) + where TLoader : struct, IConstantBufferLoader; + + /// + /// Loads the graphics resources for the shader. + /// + /// The type of graphics resource loader being used. + /// The instance to use to load the data. + void LoadGraphicsResources(ref TLoader loader) + where TLoader : struct, IGraphicsResourceLoader; } \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs index b8f66857..b403881d 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -288,11 +288,11 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions /// /// The instance in use. /// The input buffer with the constant data to bind. - public static void SetComputeRoot32BitConstants(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ReadOnlySpan data) + public static void SetComputeRoot32BitConstants(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ReadOnlySpan data) { - fixed (uint* p = data) + fixed (byte* p = data) { - d3D12GraphicsCommandList.SetComputeRoot32BitConstants(0, (uint)data.Length, p, 0); + d3D12GraphicsCommandList.SetComputeRoot32BitConstants(0, (uint)data.Length / sizeof(uint), p, 0); } } } \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs deleted file mode 100644 index 5884f50e..00000000 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ /dev/null @@ -1,416 +0,0 @@ -using System; -using System.ComponentModel; -using System.Runtime.CompilerServices; -using ComputeSharp.Interop; -using ComputeSharp.Resources; -using TerraFX.Interop.DirectX; - -namespace ComputeSharp.__Internals; - -/// -/// A helper class with some proxy methods to expose to generated code in external projects. -/// -[EditorBrowsable(EditorBrowsableState.Never)] -[Obsolete("This type is not intended to be used directly by user code")] -public static class GraphicsResourceHelper -{ - /// - /// An interface for non-generic graphics resource types. - /// - internal unsafe interface IGraphicsResource - { - /// - /// Validates the given resource for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The target instance in use. - /// The GPU descriptor handle for the resource. - D3D12_GPU_DESCRIPTOR_HANDLE ValidateAndGetGpuDescriptorHandle(GraphicsDevice device); - - /// - /// Validates the given resource for usage with a specified device, and retrieves its GPU and CPU descriptor handles for a clear operation. - /// - /// The target instance in use. - /// Indicates whether the current resource uses a normalized format. - /// The GPU and CPU descriptor handles for the resource. - (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized); - - /// - /// Validates the given resource for usage with a specified device, and retrieves the underlying object. - /// - /// The target instance in use. - /// The value for the returned object. - /// The the underlying object. - ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease); - - /// - /// Validates the given resource for usage with a specified device, and retrieves the underlying object, along with the transition states. - /// - /// The target instance in use. - /// The target state to transition the resource to. - /// The the underlying object. - /// The value for the returned object. - /// The resource states for , before and after the transition. - (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease); - } - - /// - /// Validates the given buffer for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of values stored in the input buffer. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the buffer. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(Buffer buffer, GraphicsDevice device) - where T : unmanaged - { - default(ArgumentNullException).ThrowIfNull(buffer); - - using ReferenceTracker.Lease _0 = buffer.GetReferenceTracker().GetLease(); - - buffer.ThrowIfDeviceMismatch(device); - - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = buffer.D3D12GpuDescriptorHandle; - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture1D texture, GraphicsDevice device) - where T : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); - - texture.ThrowIfDeviceMismatch(device); - - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture1D texture, GraphicsDevice device) - where T : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); - - texture.ThrowIfDeviceMismatch(device); - - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyTexture1D texture, GraphicsDevice device) - where T : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - if (texture is IGraphicsResource resource) - { - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - return default(ArgumentException).Throw(nameof(texture)); - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of normalized values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyNormalizedTexture1D texture, GraphicsDevice device) - where TPixel : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - if (texture is IGraphicsResource resource) - { - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - return default(ArgumentException).Throw(nameof(texture)); - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of normalized values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadWriteNormalizedTexture1D texture, GraphicsDevice device) - where TPixel : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - if (texture is IGraphicsResource resource) - { - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - return default(ArgumentException).Throw(nameof(texture)); - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture2D texture, GraphicsDevice device) - where T : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); - - texture.ThrowIfDeviceMismatch(device); - - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture2D texture, GraphicsDevice device) - where T : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); - - texture.ThrowIfDeviceMismatch(device); - - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyTexture2D texture, GraphicsDevice device) - where T : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - if (texture is IGraphicsResource resource) - { - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - return default(ArgumentException).Throw(nameof(texture)); - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of normalized values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyNormalizedTexture2D texture, GraphicsDevice device) - where TPixel : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - if (texture is IGraphicsResource resource) - { - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - return default(ArgumentException).Throw(nameof(texture)); - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of normalized values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadWriteNormalizedTexture2D texture, GraphicsDevice device) - where TPixel : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - if (texture is IGraphicsResource resource) - { - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - return default(ArgumentException).Throw(nameof(texture)); - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture3D texture, GraphicsDevice device) - where T : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); - - texture.ThrowIfDeviceMismatch(device); - - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture3D texture, GraphicsDevice device) - where T : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); - - texture.ThrowIfDeviceMismatch(device); - - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyTexture3D texture, GraphicsDevice device) - where T : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - if (texture is IGraphicsResource resource) - { - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - return default(ArgumentException).Throw(nameof(texture)); - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of normalized values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyNormalizedTexture3D texture, GraphicsDevice device) - where TPixel : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - if (texture is IGraphicsResource resource) - { - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - return default(ArgumentException).Throw(nameof(texture)); - } - - /// - /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. - /// - /// The type of normalized values stored in the input texture. - /// The input instance to check. - /// The target instance in use. - /// The GPU descriptor handle for the texture. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadWriteNormalizedTexture3D texture, GraphicsDevice device) - where TPixel : unmanaged - { - default(ArgumentNullException).ThrowIfNull(texture); - - if (texture is IGraphicsResource resource) - { - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); - - return *(ulong*)&d3D12GpuDescriptorHandle; - } - - return default(ArgumentException).Throw(nameof(texture)); - } -} \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 4cf4dfee..4583f59b 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -7,25 +7,28 @@ using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Interop.Allocation; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_FEATURE; using static TerraFX.Interop.DirectX.DXGI_FORMAT; using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; -using ComputeSharp.Interop.Allocation; #if NET6_0_OR_GREATER using MemoryMarshal = System.Runtime.InteropServices.MemoryMarshal; #else using MemoryMarshal = ComputeSharp.NetStandard.MemoryMarshal; #endif +#pragma warning disable CA1063 + namespace ComputeSharp.Resources; /// /// A representing a typed buffer stored on GPU memory. /// /// The type of items stored on the buffer. -public abstract unsafe partial class Buffer : IReferenceTrackedObject, IGraphicsResource +public abstract unsafe partial class Buffer : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadOnlyResource where T : unmanaged { /// @@ -232,7 +235,7 @@ public abstract unsafe partial class Buffer : IReferenceTrackedObject, IGraph } } - /// + /// internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); @@ -244,7 +247,7 @@ public abstract unsafe partial class Buffer : IReferenceTrackedObject, IGraph this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible); } - /// + /// internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) { lease = GetReferenceTracker().GetLease(); @@ -253,4 +256,20 @@ public abstract unsafe partial class Buffer : IReferenceTrackedObject, IGraph return D3D12Resource; } + + /// + D3D12_GPU_DESCRIPTOR_HANDLE ID3D12ReadOnlyResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + ThrowIfDeviceMismatch(device); + + return D3D12GpuDescriptorHandle; + } + + /// + ID3D12Resource* ID3D12ReadOnlyResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + { + return ValidateAndGetID3D12Resource(device, out lease); + } } \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs index e9f025da..bbe0e983 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; @@ -9,6 +8,7 @@ using ComputeSharp.Graphics.Resources.Helpers; using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Interop; using ComputeSharp.Interop.Allocation; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE; @@ -17,7 +17,7 @@ using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; using static TerraFX.Interop.DirectX.D3D12_UAV_DIMENSION; using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; -#pragma warning disable CS0618, CA1063 +#pragma warning disable CA1063 namespace ComputeSharp.Resources; @@ -25,7 +25,7 @@ namespace ComputeSharp.Resources; /// A representing a typed 1D texture stored on GPU memory. /// /// The type of items stored on the texture. -public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource +public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadWriteResource where T : unmanaged { /// @@ -511,7 +511,27 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr } } - /// + /// + D3D12_GPU_DESCRIPTOR_HANDLE ID3D12ReadOnlyResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + ThrowIfDeviceMismatch(device); + + return D3D12GpuDescriptorHandle; + } + + /// + internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + { + lease = GetReferenceTracker().GetLease(); + + ThrowIfDeviceMismatch(device); + + return D3D12Resource; + } + + /// internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); @@ -523,17 +543,7 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); } - /// - internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) - { - lease = GetReferenceTracker().GetLease(); - - ThrowIfDeviceMismatch(device); - - return D3D12Resource; - } - - /// + /// internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) { lease = GetReferenceTracker().GetLease(); @@ -551,29 +561,19 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr } /// - D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) - { - using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); - - ThrowIfDeviceMismatch(device); - - return D3D12GpuDescriptorHandle; - } - - /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) - { - return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); - } - - /// - ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + ID3D12Resource* ID3D12ReadOnlyResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) { return ValidateAndGetID3D12Resource(device, out lease); } /// - (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) ID3D12ReadWriteResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) + { + return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); + } + + /// + (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) ID3D12ReadWriteResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) { return ValidateAndGetID3D12ResourceAndTransitionStates(device, resourceState, out d3D12Resource, out lease); } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index e95ff83f..f191f21a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; @@ -9,6 +8,7 @@ using ComputeSharp.Graphics.Resources.Helpers; using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Interop; using ComputeSharp.Interop.Allocation; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE; @@ -17,7 +17,7 @@ using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; using static TerraFX.Interop.DirectX.D3D12_UAV_DIMENSION; using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; -#pragma warning disable CS0618, CA1063 +#pragma warning disable CA1063 namespace ComputeSharp.Resources; @@ -25,7 +25,7 @@ namespace ComputeSharp.Resources; /// A representing a typed 2D texture stored on GPU memory. /// /// The type of items stored on the texture. -public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource +public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadWriteResource where T : unmanaged { /// @@ -561,7 +561,17 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr } } - /// + /// + internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + { + lease = GetReferenceTracker().GetLease(); + + ThrowIfDeviceMismatch(device); + + return D3D12Resource; + } + + /// internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); @@ -573,17 +583,7 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); } - /// - internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) - { - lease = GetReferenceTracker().GetLease(); - - ThrowIfDeviceMismatch(device); - - return D3D12Resource; - } - - /// + /// internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) { lease = GetReferenceTracker().GetLease(); @@ -601,7 +601,7 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr } /// - D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + D3D12_GPU_DESCRIPTOR_HANDLE ID3D12ReadOnlyResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); @@ -611,19 +611,19 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr } /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) - { - return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); - } - - /// - ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + ID3D12Resource* ID3D12ReadOnlyResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) { return ValidateAndGetID3D12Resource(device, out lease); } /// - (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) ID3D12ReadWriteResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) + { + return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); + } + + /// + (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) ID3D12ReadWriteResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) { return ValidateAndGetID3D12ResourceAndTransitionStates(device, resourceState, out d3D12Resource, out lease); } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 18077312..268f1563 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; @@ -9,6 +8,7 @@ using ComputeSharp.Graphics.Resources.Helpers; using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Interop; using ComputeSharp.Interop.Allocation; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE; @@ -17,7 +17,7 @@ using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; using static TerraFX.Interop.DirectX.D3D12_UAV_DIMENSION; using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; -#pragma warning disable CS0618, CA1063 +#pragma warning disable CA1063 namespace ComputeSharp.Resources; @@ -25,7 +25,7 @@ namespace ComputeSharp.Resources; /// A representing a typed 3D texture stored on GPU memory. /// /// The type of items stored on the texture. -public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource +public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadWriteResource where T : unmanaged { /// @@ -613,7 +613,17 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr } } - /// + /// + internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + { + lease = GetReferenceTracker().GetLease(); + + ThrowIfDeviceMismatch(device); + + return D3D12Resource; + } + + /// internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); @@ -625,17 +635,7 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); } - /// - internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) - { - lease = GetReferenceTracker().GetLease(); - - ThrowIfDeviceMismatch(device); - - return D3D12Resource; - } - - /// + /// internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) { lease = GetReferenceTracker().GetLease(); @@ -653,7 +653,7 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr } /// - D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + D3D12_GPU_DESCRIPTOR_HANDLE ID3D12ReadOnlyResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); @@ -663,19 +663,19 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr } /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) - { - return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); - } - - /// - ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + ID3D12Resource* ID3D12ReadOnlyResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) { return ValidateAndGetID3D12Resource(device, out lease); } /// - (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) ID3D12ReadWriteResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) + { + return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); + } + + /// + (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) ID3D12ReadWriteResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) { return ValidateAndGetID3D12ResourceAndTransitionStates(device, resourceState, out d3D12Resource, out lease); } diff --git a/src/ComputeSharp/Graphics/Resources/Interfaces/Interop/ID3D12ReadOnlyResource.cs b/src/ComputeSharp/Graphics/Resources/Interfaces/Interop/ID3D12ReadOnlyResource.cs new file mode 100644 index 00000000..0424857a --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/Interfaces/Interop/ID3D12ReadOnlyResource.cs @@ -0,0 +1,25 @@ +using ComputeSharp.Interop; +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.Resources.Interop; + +/// +/// An interface for a readonly, non-generic readonly graphics resource types. +/// +internal unsafe interface ID3D12ReadOnlyResource +{ + /// + /// Validates the given resource for usage with a specified device, and retrieves its GPU descriptor handle. + /// + /// The target instance in use. + /// The GPU descriptor handle for the resource. + D3D12_GPU_DESCRIPTOR_HANDLE ValidateAndGetGpuDescriptorHandle(GraphicsDevice device); + + /// + /// Validates the given resource for usage with a specified device, and retrieves the underlying object. + /// + /// The target instance in use. + /// The value for the returned object. + /// The the underlying object. + ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease); +} diff --git a/src/ComputeSharp/Graphics/Resources/Interfaces/Interop/ID3D12ReadWriteResource.cs b/src/ComputeSharp/Graphics/Resources/Interfaces/Interop/ID3D12ReadWriteResource.cs new file mode 100644 index 00000000..6ca55e18 --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/Interfaces/Interop/ID3D12ReadWriteResource.cs @@ -0,0 +1,32 @@ +using ComputeSharp.Interop; +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.Resources.Interop; + +/// +/// An interface for a writeable, non-generic graphics resource types. +/// +internal unsafe interface ID3D12ReadWriteResource : ID3D12ReadOnlyResource +{ + /// + /// Validates the given resource for usage with a specified device, and retrieves its GPU and CPU descriptor handles for a clear operation. + /// + /// The target instance in use. + /// Indicates whether the current resource uses a normalized format. + /// The GPU and CPU descriptor handles for the resource. + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized); + + /// + /// Validates the given resource for usage with a specified device, and retrieves the underlying object, along with the transition states. + /// + /// The target instance in use. + /// The target state to transition the resource to. + /// The the underlying object. + /// The value for the returned object. + /// The resource states for , before and after the transition. + (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates( + GraphicsDevice device, + ResourceState resourceState, + out ID3D12Resource* d3D12Resource, + out ReferenceTracker.Lease lease); +} diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T,TPixel}.ReadOnly.cs index a510e30f..8487497a 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T,TPixel}.ReadOnly.cs @@ -1,14 +1,13 @@ -using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; -#pragma warning disable CS0618, IDE0022 +#pragma warning disable IDE0022 namespace ComputeSharp; @@ -57,7 +56,7 @@ partial class ReadWriteTexture1D /// /// A wrapper for a resource that has been temporarily transitioned to readonly. /// - private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture1D, GraphicsResourceHelper.IGraphicsResource + private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture1D, ID3D12ReadOnlyResource { /// /// The owning instance being wrapped. @@ -99,7 +98,7 @@ partial class ReadWriteTexture1D public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice; /// - D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + D3D12_GPU_DESCRIPTOR_HANDLE ID3D12ReadOnlyResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = this.owner.GetReferenceTracker().GetLease(); @@ -110,13 +109,7 @@ partial class ReadWriteTexture1D } /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - - /// - ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + ID3D12Resource* ID3D12ReadOnlyResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) { lease = GetReferenceTracker().GetLease(); @@ -127,12 +120,6 @@ partial class ReadWriteTexture1D return this.owner.D3D12Resource; } - /// - (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - /// protected override void DangerousOnDispose() { diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T}.ReadOnly.cs index 00221eae..d670d741 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T}.ReadOnly.cs @@ -1,14 +1,13 @@ -using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; -#pragma warning disable CS0618, IDE0022 +#pragma warning disable IDE0022 namespace ComputeSharp; @@ -57,7 +56,7 @@ partial class ReadWriteTexture1D /// /// A wrapper for a resource that has been temporarily transitioned to readonly. /// - private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture1D, GraphicsResourceHelper.IGraphicsResource + private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture1D, ID3D12ReadOnlyResource { /// /// The owning instance being wrapped. @@ -99,7 +98,7 @@ partial class ReadWriteTexture1D public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice; /// - D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + D3D12_GPU_DESCRIPTOR_HANDLE ID3D12ReadOnlyResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = this.owner.GetReferenceTracker().GetLease(); @@ -110,13 +109,7 @@ partial class ReadWriteTexture1D } /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - - /// - ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + ID3D12Resource* ID3D12ReadOnlyResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) { lease = GetReferenceTracker().GetLease(); @@ -127,12 +120,6 @@ partial class ReadWriteTexture1D return this.owner.D3D12Resource; } - /// - (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - /// protected override void DangerousOnDispose() { diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs index 993de87a..efa46b81 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs @@ -1,14 +1,13 @@ -using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; -#pragma warning disable CS0618, IDE0022 +#pragma warning disable IDE0022 namespace ComputeSharp; @@ -57,7 +56,7 @@ partial class ReadWriteTexture2D /// /// A wrapper for a resource that has been temporarily transitioned to readonly. /// - private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture2D, GraphicsResourceHelper.IGraphicsResource + private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture2D, ID3D12ReadOnlyResource { /// /// The owning instance being wrapped. @@ -111,7 +110,7 @@ partial class ReadWriteTexture2D public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice; /// - D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + D3D12_GPU_DESCRIPTOR_HANDLE ID3D12ReadOnlyResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = this.owner.GetReferenceTracker().GetLease(); @@ -122,13 +121,7 @@ partial class ReadWriteTexture2D } /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - - /// - ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + ID3D12Resource* ID3D12ReadOnlyResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) { lease = GetReferenceTracker().GetLease(); @@ -139,12 +132,6 @@ partial class ReadWriteTexture2D return this.owner.D3D12Resource; } - /// - (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - /// protected override void DangerousOnDispose() { diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs index 428d9b90..92b41d1f 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs @@ -1,14 +1,13 @@ -using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; -#pragma warning disable CS0618, IDE0022 +#pragma warning disable IDE0022 namespace ComputeSharp; @@ -60,7 +59,7 @@ partial class ReadWriteTexture2D /// /// A wrapper for a resource that has been temporarily transitioned to readonly. /// - private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture2D, GraphicsResourceHelper.IGraphicsResource + private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture2D, ID3D12ReadOnlyResource { /// /// The owning instance being wrapped. @@ -111,7 +110,7 @@ partial class ReadWriteTexture2D public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice; /// - D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + D3D12_GPU_DESCRIPTOR_HANDLE ID3D12ReadOnlyResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = this.owner.GetReferenceTracker().GetLease(); @@ -122,13 +121,7 @@ partial class ReadWriteTexture2D } /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - - /// - ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + ID3D12Resource* ID3D12ReadOnlyResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) { lease = GetReferenceTracker().GetLease(); @@ -139,12 +132,6 @@ partial class ReadWriteTexture2D return this.owner.D3D12Resource; } - /// - (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - /// protected override void DangerousOnDispose() { diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs index abe8e1c3..b91de2a9 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs @@ -1,14 +1,13 @@ -using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; -#pragma warning disable CS0618, IDE0022 +#pragma warning disable IDE0022 namespace ComputeSharp; @@ -57,7 +56,7 @@ partial class ReadWriteTexture3D /// /// A wrapper for a resource that has been temporarily transitioned to readonly. /// - private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture3D, GraphicsResourceHelper.IGraphicsResource + private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture3D, ID3D12ReadOnlyResource { /// /// The owning instance being wrapped. @@ -111,7 +110,7 @@ partial class ReadWriteTexture3D public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice; /// - D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + D3D12_GPU_DESCRIPTOR_HANDLE ID3D12ReadOnlyResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = this.owner.GetReferenceTracker().GetLease(); @@ -122,13 +121,7 @@ partial class ReadWriteTexture3D } /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - - /// - ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + ID3D12Resource* ID3D12ReadOnlyResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) { lease = GetReferenceTracker().GetLease(); @@ -139,12 +132,6 @@ partial class ReadWriteTexture3D return this.owner.D3D12Resource; } - /// - (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - /// protected override void DangerousOnDispose() { diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs index 7ea10fae..3c28e717 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs @@ -1,14 +1,13 @@ -using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; -#pragma warning disable CS0618, IDE0022 +#pragma warning disable IDE0022 namespace ComputeSharp; @@ -57,7 +56,7 @@ partial class ReadWriteTexture3D /// /// A wrapper for a resource that has been temporarily transitioned to readonly. /// - private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture3D, GraphicsResourceHelper.IGraphicsResource + private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture3D, ID3D12ReadOnlyResource { /// /// The owning instance being wrapped. @@ -111,7 +110,7 @@ partial class ReadWriteTexture3D public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice; /// - D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + D3D12_GPU_DESCRIPTOR_HANDLE ID3D12ReadOnlyResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = this.owner.GetReferenceTracker().GetLease(); @@ -122,13 +121,7 @@ partial class ReadWriteTexture3D } /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - - /// - ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) + ID3D12Resource* ID3D12ReadOnlyResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out ReferenceTracker.Lease lease) { lease = GetReferenceTracker().GetLease(); @@ -139,12 +132,6 @@ partial class ReadWriteTexture3D return this.owner.D3D12Resource; } - /// - (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out ReferenceTracker.Lease lease) - { - throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); - } - /// protected override void DangerousOnDispose() { diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 71108959..0f92e29f 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -2,9 +2,9 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Threading.Tasks; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Resources.Interop; using ComputeSharp.Shaders.Dispatching; using ComputeSharp.Shaders.Loading; using TerraFX.Interop.DirectX; @@ -172,9 +172,13 @@ public struct ComputeContext : IDisposable, IAsyncDisposable commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature); - ComputeShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList); + D3D12GraphicsCommandListConstantBufferLoader dataLoader = new(commandList.D3D12GraphicsCommandList); - shader.LoadDispatchData(ref dataLoader, this.device, x, y, z); + shader.LoadConstantBuffer(ref dataLoader, x, y, z); + + D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 1); + + shader.LoadGraphicsResources(ref graphicsResourceLoader); commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ); } @@ -206,14 +210,18 @@ public struct ComputeContext : IDisposable, IAsyncDisposable commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature); - PixelShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList); + D3D12GraphicsCommandListConstantBufferLoader constantBufferLoader = new(commandList.D3D12GraphicsCommandList); - shader.LoadDispatchData(ref dataLoader, this.device, x, y, 1); + shader.LoadConstantBuffer(ref constantBufferLoader, x, y, 1); + + D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 2); + + shader.LoadGraphicsResources(ref graphicsResourceLoader); // Load the implicit output texture commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable( 1, - ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuDescriptorHandle(this.device)); + ((ID3D12ReadOnlyResource)texture).ValidateAndGetGpuDescriptorHandle(this.device)); commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, 1); } diff --git a/src/ComputeSharp/Shaders/Dispatching/ComputeShaderDispatchDataLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ComputeShaderDispatchDataLoader.cs deleted file mode 100644 index 8f131c65..00000000 --- a/src/ComputeSharp/Shaders/Dispatching/ComputeShaderDispatchDataLoader.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using ComputeSharp.Graphics.Extensions; -using ComputeSharp.__Internals; -using TerraFX.Interop.DirectX; -using System.Runtime.InteropServices; - -#pragma warning disable CS0618 - -namespace ComputeSharp.Shaders.Dispatching; - -/// -/// A data loader for compute shaders. -/// -internal readonly unsafe struct ComputeShaderDispatchDataLoader : IDispatchDataLoader -{ - /// - /// The object in use. - /// - private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList; - - /// - /// Creates a new instance. - /// - /// The object to use. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal ComputeShaderDispatchDataLoader(ID3D12GraphicsCommandList* d3D12GraphicsCommandList) - { - this.d3D12GraphicsCommandList = d3D12GraphicsCommandList; - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void LoadCapturedValues(ReadOnlySpan data) - { - this.d3D12GraphicsCommandList->SetComputeRoot32BitConstants(data); - } - - /// - public void LoadCapturedResources(ReadOnlySpan data) - { - ID3D12GraphicsCommandList* d3D12GraphicsCommandList = this.d3D12GraphicsCommandList; - ref ulong resourcesRef = ref MemoryMarshal.GetReference(data); - ref D3D12_GPU_DESCRIPTOR_HANDLE startRef = ref Unsafe.As(ref resourcesRef); - ref D3D12_GPU_DESCRIPTOR_HANDLE endRef = ref Unsafe.Add(ref startRef, data.Length); - uint offset = 1; - - while (Unsafe.IsAddressLessThan(ref startRef, ref endRef)) - { - d3D12GraphicsCommandList->SetComputeRootDescriptorTable(offset, startRef); - - startRef = ref Unsafe.Add(ref startRef, 1); - offset++; - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListConstantBufferLoader.cs b/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListConstantBufferLoader.cs new file mode 100644 index 00000000..5d8056a5 --- /dev/null +++ b/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListConstantBufferLoader.cs @@ -0,0 +1,37 @@ +using System; +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.__Internals; +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.Shaders.Dispatching; + +/// +/// An implementation for compute shaders dispatched via . +/// +internal readonly unsafe struct D3D12GraphicsCommandListConstantBufferLoader : IConstantBufferLoader +{ + /// + /// The object in use. + /// + private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList; + + /// + /// Creates a new instance. + /// + /// The object to use. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal D3D12GraphicsCommandListConstantBufferLoader(ID3D12GraphicsCommandList* d3D12GraphicsCommandList) + { + this.d3D12GraphicsCommandList = d3D12GraphicsCommandList; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void LoadConstantBuffer(ReadOnlySpan data) + { + default(ArgumentException).ThrowIf((data.Length & 3) != 0, nameof(data)); + + this.d3D12GraphicsCommandList->SetComputeRoot32BitConstants(data); + } +} \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs b/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs new file mode 100644 index 00000000..4858ee7e --- /dev/null +++ b/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs @@ -0,0 +1,75 @@ +using System; +using System.Runtime.CompilerServices; +using ComputeSharp.__Internals; +using TerraFX.Interop.DirectX; +using ComputeSharp.Resources.Interop; + +namespace ComputeSharp.Shaders.Dispatching; + +/// +/// An loader for compute shaders dispatched via . +/// +internal readonly unsafe struct D3D12GraphicsCommandListGraphicsResourceLoader : IGraphicsResourceLoader +{ + /// + /// The object in use. + /// + private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList; + + /// + /// The object in use. + /// + private readonly GraphicsDevice graphicsDevice; + + /// + /// The offset into the compute root descriptor table for loaded resources. + /// + /// + /// + /// This is used to correctly align the explicit resources captured by shaders, with the implicit ones + /// that are injected by ComputeSharp. For instance, the constant buffer is always added automatically. + /// + /// + /// For compute shaders capturing an implicit texture, this is used to skip the first target resource, + /// as that is loaded explicitly by the shader runner using the target texture passed in by the user, + /// which is not actually captured by the shader itself. The target root descriptor table index is therefore + /// 2 in this case, as it needs to skip not only the this implicit texture to draw on, but also the constant + /// buffer with capture values. + /// + /// + private readonly uint rootParameterOffset; + + /// + /// Creates a new instance. + /// + /// The object to use. + /// The instance that is tied to. + /// The offset into the compute root descriptor table for loaded resources. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal D3D12GraphicsCommandListGraphicsResourceLoader( + ID3D12GraphicsCommandList* d3D12GraphicsCommandList, + GraphicsDevice graphicsDevice, + uint rootParameterOffset) + { + this.d3D12GraphicsCommandList = d3D12GraphicsCommandList; + this.graphicsDevice = graphicsDevice; + this.rootParameterOffset = rootParameterOffset; + } + + /// + void IGraphicsResourceLoader.LoadGraphicsResource(IGraphicsResource resource, uint index) + { + default(ArgumentNullException).ThrowIfNull(resource); + + if (resource is not ID3D12ReadOnlyResource readOnlyResource) + { + default(ArgumentException).Throw(nameof(resource)); + + return; + } + + this.d3D12GraphicsCommandList->SetComputeRootDescriptorTable( + RootParameterIndex: this.rootParameterOffset + index, + BaseDescriptor: readOnlyResource.ValidateAndGetGpuDescriptorHandle(this.graphicsDevice)); + } +} \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Dispatching/PixelShaderDispatchDataLoader.cs b/src/ComputeSharp/Shaders/Dispatching/PixelShaderDispatchDataLoader.cs deleted file mode 100644 index c43bf1f1..00000000 --- a/src/ComputeSharp/Shaders/Dispatching/PixelShaderDispatchDataLoader.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using ComputeSharp.Graphics.Extensions; -using ComputeSharp.__Internals; -using TerraFX.Interop.DirectX; - -#pragma warning disable CS0618 - -namespace ComputeSharp.Shaders.Dispatching; - -/// -/// A data loader for pixel shaders. -/// -internal readonly unsafe struct PixelShaderDispatchDataLoader : IDispatchDataLoader -{ - /// - /// The object in use. - /// - private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList; - - /// - /// Creates a new instance. - /// - /// The object to use. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal PixelShaderDispatchDataLoader(ID3D12GraphicsCommandList* d3D12GraphicsCommandList) - { - this.d3D12GraphicsCommandList = d3D12GraphicsCommandList; - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void LoadCapturedValues(ReadOnlySpan data) - { - this.d3D12GraphicsCommandList->SetComputeRoot32BitConstants(data); - } - - /// - public void LoadCapturedResources(ReadOnlySpan data) - { - ID3D12GraphicsCommandList* d3D12GraphicsCommandList = this.d3D12GraphicsCommandList; - ref ulong resourcesRef = ref MemoryMarshal.GetReference(data); - ref D3D12_GPU_DESCRIPTOR_HANDLE startRef = ref Unsafe.As(ref resourcesRef); - ref D3D12_GPU_DESCRIPTOR_HANDLE endRef = ref Unsafe.Add(ref startRef, data.Length); - uint offset = 2; - - // Skip the first target resource, as that is loaded explicitly by the shader runner using the - // target texture passed in by the user, which is not actually captured by the shader itself. - // The target root descriptor table index is therefore 2 in this case, as it needs to skip not - // only the this implicit texture to draw on, but also the constant buffer with capture values. - while (Unsafe.IsAddressLessThan(ref startRef, ref endRef)) - { - d3D12GraphicsCommandList->SetComputeRootDescriptorTable(offset, startRef); - - startRef = ref Unsafe.Add(ref startRef, 1); - offset++; - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 2262ca07..163037a3 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -1,12 +1,10 @@ using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Resources.Interop; using TerraFX.Interop.DirectX; -#pragma warning disable CS0618 - namespace ComputeSharp; /// @@ -145,7 +143,7 @@ public static class ComputeContextExtensions using ReferenceTracker.Lease lease = default; - context.Barrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease))); + context.Barrier(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease))); } /// @@ -161,7 +159,7 @@ public static class ComputeContextExtensions using ReferenceTracker.Lease lease = default; - context.Barrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease))); + context.Barrier(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease))); } /// @@ -177,7 +175,7 @@ public static class ComputeContextExtensions using ReferenceTracker.Lease lease = default; - context.Barrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease))); + context.Barrier(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease))); } /// @@ -309,11 +307,11 @@ public static class ComputeContextExtensions { default(ArgumentNullException).ThrowIfNull(texture); - (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((ID3D12ReadWriteResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); using ReferenceTracker.Lease lease = default; - context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, true); + context.Clear(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, true); } /// @@ -327,11 +325,11 @@ public static class ComputeContextExtensions { default(ArgumentNullException).ThrowIfNull(texture); - (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((ID3D12ReadWriteResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); using ReferenceTracker.Lease lease = default; - context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, true); + context.Clear(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, true); } /// @@ -345,11 +343,11 @@ public static class ComputeContextExtensions { default(ArgumentNullException).ThrowIfNull(texture); - (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((ID3D12ReadWriteResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); using ReferenceTracker.Lease lease = default; - context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, true); + context.Clear(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, true); } /// @@ -421,12 +419,12 @@ public static class ComputeContextExtensions { default(ArgumentNullException).ThrowIfNull(texture); - (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((ID3D12ReadWriteResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); using ReferenceTracker.Lease lease = default; context.Fill( - ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), + ((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value)); @@ -444,12 +442,12 @@ public static class ComputeContextExtensions { default(ArgumentNullException).ThrowIfNull(texture); - (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((ID3D12ReadWriteResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); using ReferenceTracker.Lease lease = default; context.Fill( - ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), + ((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value)); @@ -467,12 +465,12 @@ public static class ComputeContextExtensions { default(ArgumentNullException).ThrowIfNull(texture); - (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((ID3D12ReadWriteResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); using ReferenceTracker.Lease lease = default; context.Fill( - ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), + ((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value)); diff --git a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs index aa24d6ff..3366a4d6 100644 --- a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs +++ b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs @@ -96,9 +96,12 @@ internal static unsafe class PipelineDataLoader d3D12DescriptorRange.OffsetInDescriptorsFromTableStart = D3D12.D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND; } + // Calculate the actual size of the constant buffer in DWORD values + int root32BitConstantCount = shader.ConstantBufferSize / sizeof(int); + // Create the D3D12 root signature *d3D12RootSignature = d3D12Device->CreateRootSignature( - shader.ConstantBufferSize, + root32BitConstantCount, d3D12DescriptorRanges, shader.IsStaticSamplerRequired).Get(); diff --git a/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs b/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs index 9dc41823..820ee39e 100644 --- a/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs +++ b/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs @@ -1,53 +1,50 @@ using System; -using System.Runtime.CompilerServices; +using System.Collections.Generic; using ComputeSharp.__Internals; -#pragma warning disable CS0618 - namespace ComputeSharp.Tests.Internals.Helpers; /// /// A debug data loader for generic shaders. /// -internal readonly unsafe struct DebugDispatchDataLoader : IDispatchDataLoader +internal struct DebugDispatchDataLoader : IConstantBufferLoader, IGraphicsResourceLoader { /// - /// The backing, mutable data for and . + /// The constant buffer, if set. /// - private readonly StrongBox<(uint[] Values, ulong[] Resources)> data; + private byte[]? constantBuffer; /// - /// Gets the captured values. + /// The list of loaded resources. /// - public uint[] Values => this.data.Value.Values; - - /// - /// Gets the captured resources. - /// - public ulong[] Resources => this.data.Value.Resources; + private readonly List<(IGraphicsResource Resource, uint Index)> graphicsResources = new(); /// /// Creates a new instance. /// - /// A new instance to use. - public static DebugDispatchDataLoader Create() + public DebugDispatchDataLoader() { - DebugDispatchDataLoader @this = default; + } - Unsafe.AsRef(in @this.data) = new StrongBox<(uint[], ulong[])>((Array.Empty(), Array.Empty())); + /// + /// Gets the constant buffer. + /// + public byte[]? ConstantBuffer => this.constantBuffer; - return @this; + /// + /// Gets the loaded resources. + /// + public IReadOnlyList<(IGraphicsResource Resource, uint Index)> GraphicsResources => this.graphicsResources; + + /// + void IConstantBufferLoader.LoadConstantBuffer(ReadOnlySpan data) + { + this.constantBuffer = data.ToArray(); } /// - public void LoadCapturedValues(ReadOnlySpan data) + readonly void IGraphicsResourceLoader.LoadGraphicsResource(IGraphicsResource resource, uint index) { - this.data.Value.Values = data.ToArray(); - } - - /// - public void LoadCapturedResources(ReadOnlySpan data) - { - this.data.Value.Resources = data.ToArray(); + this.graphicsResources.Add((resource, index)); } } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs index 847a95c8..66eb2daf 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs @@ -1,9 +1,8 @@ +using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Tests.Internals.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; -#pragma warning disable CS0618 - namespace ComputeSharp.Tests.Internals; [TestClass] @@ -22,21 +21,27 @@ public partial class ShaderDataLoaderTests } [TestMethod] - public unsafe void CapturedResource() + public void CapturedResource() { using ReadWriteBuffer buffer = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); - DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); + CapturedResourceShader shader = new(buffer); + DebugDispatchDataLoader dataLoader = new(); - ((IShader)new CapturedResourceShader(buffer)).LoadDispatchData(ref dataLoader, GraphicsDevice.GetDefault(), 111, 222, 333); + ((IShader)shader).LoadConstantBuffer(ref dataLoader, 111, 222, 333); - Assert.AreEqual(3, dataLoader.Values.Length); - Assert.AreEqual(1, dataLoader.Resources.Length); + Assert.AreEqual(12, ((IShader)shader).ConstantBufferSize); + Assert.IsNotNull(dataLoader.ConstantBuffer); + Assert.AreEqual(12, dataLoader.ConstantBuffer.Length); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[4])); + Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[8])); - Assert.AreEqual(111, (int)dataLoader.Values[0]); - Assert.AreEqual(222, (int)dataLoader.Values[1]); - Assert.AreEqual(333, (int)dataLoader.Values[2]); - Assert.AreEqual(buffer.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); + ((IShader)shader).LoadGraphicsResources(ref dataLoader); + + Assert.AreEqual(1, ((IShader)shader).ResourceDescriptorRanges.Length); + Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); + Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } [AutoConstructor] @@ -55,26 +60,33 @@ public partial class ShaderDataLoaderTests } [TestMethod] - public unsafe void LoadMultipleResourcesAndPrimitives() + public void LoadMultipleResourcesAndPrimitives() { using ReadWriteBuffer buffer0 = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); using ReadWriteBuffer buffer1 = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); - DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); + MultipleResourcesAndPrimitivesShader shader = new(buffer0, buffer1, 1, 22, 77); + DebugDispatchDataLoader dataLoader = new(); - ((IShader)new MultipleResourcesAndPrimitivesShader(buffer0, buffer1, 1, 22, 77)).LoadDispatchData(ref dataLoader, GraphicsDevice.GetDefault(), 111, 222, 333); + ((IShader)shader).LoadConstantBuffer(ref dataLoader, 111, 222, 333); - Assert.AreEqual(6, dataLoader.Values.Length); - Assert.AreEqual(2, dataLoader.Resources.Length); + Assert.AreEqual(24, ((IShader)shader).ConstantBufferSize); + Assert.IsNotNull(dataLoader.ConstantBuffer); + Assert.AreEqual(24, dataLoader.ConstantBuffer.Length); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[4])); + Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[8])); + Assert.AreEqual(1, Unsafe.As(ref dataLoader.ConstantBuffer[12])); + Assert.AreEqual(22, Unsafe.As(ref dataLoader.ConstantBuffer[16])); + Assert.AreEqual(77, Unsafe.As(ref dataLoader.ConstantBuffer[20])); - Assert.AreEqual(111, (int)dataLoader.Values[0]); - Assert.AreEqual(222, (int)dataLoader.Values[1]); - Assert.AreEqual(333, (int)dataLoader.Values[2]); - Assert.AreEqual(1, (int)dataLoader.Values[3]); - Assert.AreEqual(22, (int)dataLoader.Values[4]); - Assert.AreEqual(77, (int)dataLoader.Values[5]); - Assert.AreEqual(buffer0.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); - Assert.AreEqual(buffer1.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[1]); + ((IShader)shader).LoadGraphicsResources(ref dataLoader); + + Assert.AreEqual(2, ((IShader)shader).ResourceDescriptorRanges.Length); + Assert.AreSame(buffer0, dataLoader.GraphicsResources[0].Resource); + Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); + Assert.AreSame(buffer1, dataLoader.GraphicsResources[1].Resource); + Assert.AreEqual(1u, dataLoader.GraphicsResources[1].Index); } [AutoConstructor] @@ -95,38 +107,36 @@ public partial class ShaderDataLoaderTests } [TestMethod] - public unsafe void LoadScalarAndVectorTypes() + public void LoadScalarAndVectorTypes() { using ReadWriteBuffer buffer = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); - DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); ScalarAndVectorTypesShader shader = new(buffer, new(55, 44, 888), 22, 77, new(3.14, 6.28), 42, 9999); + DebugDispatchDataLoader dataLoader = new(); - ((IShader)shader).LoadDispatchData(ref dataLoader, GraphicsDevice.GetDefault(), 111, 222, 333); + ((IShader)shader).LoadConstantBuffer(ref dataLoader, 111, 222, 333); - Assert.AreEqual(18, dataLoader.Values.Length); - Assert.AreEqual(1, dataLoader.Resources.Length); + Assert.AreEqual(72, ((IShader)shader).ConstantBufferSize); + Assert.IsNotNull(dataLoader.ConstantBuffer); + Assert.AreEqual(72, dataLoader.ConstantBuffer.Length); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[4])); + Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[8])); + Assert.AreEqual(55, Unsafe.As(ref dataLoader.ConstantBuffer[16])); + Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[20])); + Assert.AreEqual(888, Unsafe.As(ref dataLoader.ConstantBuffer[24])); + Assert.AreEqual(22, Unsafe.As(ref dataLoader.ConstantBuffer[28])); + Assert.AreEqual(77, Unsafe.As(ref dataLoader.ConstantBuffer[32])); + Assert.AreEqual(3.14, Unsafe.As(ref dataLoader.ConstantBuffer[48])); + Assert.AreEqual(6.28, Unsafe.As(ref dataLoader.ConstantBuffer[56])); + Assert.AreEqual(42, Unsafe.As(ref dataLoader.ConstantBuffer[64])); + Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[68])); - Assert.AreEqual(111, (int)dataLoader.Values[0]); - Assert.AreEqual(222, (int)dataLoader.Values[1]); - Assert.AreEqual(333, (int)dataLoader.Values[2]); + ((IShader)shader).LoadGraphicsResources(ref dataLoader); - Assert.AreEqual(buffer.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); - - fixed (void* p0 = &dataLoader.Values[0]) - { - byte* p1 = (byte*)p0; - - Assert.AreEqual(55, *(float*)&p1[16]); - Assert.AreEqual(44, *(float*)&p1[20]); - Assert.AreEqual(888, *(float*)&p1[24]); - Assert.AreEqual(22, *(int*)&p1[28]); - Assert.AreEqual(77, *(int*)&p1[32]); - Assert.AreEqual(3.14, *(double*)&p1[48]); - Assert.AreEqual(6.28, *(double*)&p1[56]); - Assert.AreEqual(42, *(int*)&p1[64]); - Assert.AreEqual(9999, *(int*)&p1[68]); - } + Assert.AreEqual(1, ((IShader)shader).ResourceDescriptorRanges.Length); + Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); + Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } [AutoConstructor] @@ -149,11 +159,10 @@ public partial class ShaderDataLoaderTests } [TestMethod] - public unsafe void LoadScalarVectorAndMatrixTypes() + public void LoadScalarVectorAndMatrixTypes() { using ReadWriteBuffer buffer = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); - DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); ScalarVectorAndMatrixTypesShader shader = new( buffer, f2x3: new(55, 44, 888, 111, 222, 333), @@ -164,43 +173,42 @@ public partial class ShaderDataLoaderTests i1x2: new(111, 222), i2x2: new(11, 22, 33, 44), d: 9999); + DebugDispatchDataLoader dataLoader = new(); - ((IShader)shader).LoadDispatchData(ref dataLoader, GraphicsDevice.GetDefault(), 111, 222, 333); + ((IShader)shader).LoadConstantBuffer(ref dataLoader, 111, 222, 333); - Assert.AreEqual(31, dataLoader.Values.Length); - Assert.AreEqual(1, dataLoader.Resources.Length); + Assert.AreEqual(124, ((IShader)shader).ConstantBufferSize); + Assert.IsNotNull(dataLoader.ConstantBuffer); + Assert.AreEqual(124, dataLoader.ConstantBuffer.Length); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[4])); + Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[8])); + Assert.AreEqual(55, Unsafe.As(ref dataLoader.ConstantBuffer[16])); + Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[20])); + Assert.AreEqual(888, Unsafe.As(ref dataLoader.ConstantBuffer[24])); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[32])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[36])); + Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[40])); + Assert.AreEqual(22, Unsafe.As(ref dataLoader.ConstantBuffer[44])); + Assert.AreEqual(1, Unsafe.As(ref dataLoader.ConstantBuffer[48])); + Assert.AreEqual(2, Unsafe.As(ref dataLoader.ConstantBuffer[52])); + Assert.AreEqual(3, Unsafe.As(ref dataLoader.ConstantBuffer[56])); + Assert.AreEqual(3.14, Unsafe.As(ref dataLoader.ConstantBuffer[64])); + Assert.AreEqual(6.28, Unsafe.As(ref dataLoader.ConstantBuffer[72])); + Assert.AreEqual(42, Unsafe.As(ref dataLoader.ConstantBuffer[80])); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[84])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[88])); + Assert.AreEqual(11, Unsafe.As(ref dataLoader.ConstantBuffer[96])); + Assert.AreEqual(22, Unsafe.As(ref dataLoader.ConstantBuffer[100])); + Assert.AreEqual(33, Unsafe.As(ref dataLoader.ConstantBuffer[112])); + Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[116])); + Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[120])); - Assert.AreEqual(111, (int)dataLoader.Values[0]); - Assert.AreEqual(222, (int)dataLoader.Values[1]); - Assert.AreEqual(333, (int)dataLoader.Values[2]); + ((IShader)shader).LoadGraphicsResources(ref dataLoader); - Assert.AreEqual(buffer.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); - - fixed (void* p0 = &dataLoader.Values[0]) - { - byte* p1 = (byte*)p0; - - Assert.AreEqual(55, *(float*)&p1[16]); - Assert.AreEqual(44, *(float*)&p1[20]); - Assert.AreEqual(888, *(float*)&p1[24]); - Assert.AreEqual(111, *(float*)&p1[32]); - Assert.AreEqual(222, *(float*)&p1[36]); - Assert.AreEqual(333, *(float*)&p1[40]); - Assert.AreEqual(22, *(int*)&p1[44]); - Assert.AreEqual(1, *(int*)&p1[48]); - Assert.AreEqual(2, *(int*)&p1[52]); - Assert.AreEqual(3, *(int*)&p1[56]); - Assert.AreEqual(3.14, *(double*)&p1[64]); - Assert.AreEqual(6.28, *(double*)&p1[72]); - Assert.AreEqual(42, *(int*)&p1[80]); - Assert.AreEqual(111, *(int*)&p1[84]); - Assert.AreEqual(222, *(int*)&p1[88]); - Assert.AreEqual(11, *(int*)&p1[96]); - Assert.AreEqual(22, *(int*)&p1[100]); - Assert.AreEqual(33, *(int*)&p1[112]); - Assert.AreEqual(44, *(int*)&p1[116]); - Assert.AreEqual(9999, *(int*)&p1[120]); - } + Assert.AreEqual(1, ((IShader)shader).ResourceDescriptorRanges.Length); + Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); + Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } [AutoConstructor] @@ -229,11 +237,10 @@ public partial class ShaderDataLoaderTests } [TestMethod] - public unsafe void LoadFlatCustomTypeShader() + public void LoadFlatCustomTypeShader() { using ReadWriteBuffer buffer = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); - DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); FlatCustomTypeShader shader = new( buffer, new SimpleTypes( @@ -245,43 +252,42 @@ public partial class ShaderDataLoaderTests i1x2: new(111, 222), i2x2: new(11, 22, 33, 44), d: 9999)); + DebugDispatchDataLoader dataLoader = new(); - ((IShader)shader).LoadDispatchData(ref dataLoader, GraphicsDevice.GetDefault(), 111, 222, 333); + ((IShader)shader).LoadConstantBuffer(ref dataLoader, 111, 222, 333); - Assert.AreEqual(31, dataLoader.Values.Length); - Assert.AreEqual(1, dataLoader.Resources.Length); + Assert.AreEqual(124, ((IShader)shader).ConstantBufferSize); + Assert.IsNotNull(dataLoader.ConstantBuffer); + Assert.AreEqual(124, dataLoader.ConstantBuffer.Length); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[4])); + Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[8])); + Assert.AreEqual(55, Unsafe.As(ref dataLoader.ConstantBuffer[16])); + Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[20])); + Assert.AreEqual(888, Unsafe.As(ref dataLoader.ConstantBuffer[24])); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[32])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[36])); + Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[40])); + Assert.AreEqual(22, Unsafe.As(ref dataLoader.ConstantBuffer[44])); + Assert.AreEqual(1, Unsafe.As(ref dataLoader.ConstantBuffer[48])); + Assert.AreEqual(2, Unsafe.As(ref dataLoader.ConstantBuffer[52])); + Assert.AreEqual(3, Unsafe.As(ref dataLoader.ConstantBuffer[56])); + Assert.AreEqual(3.14, Unsafe.As(ref dataLoader.ConstantBuffer[64])); + Assert.AreEqual(6.28, Unsafe.As(ref dataLoader.ConstantBuffer[72])); + Assert.AreEqual(42, Unsafe.As(ref dataLoader.ConstantBuffer[80])); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[84])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[88])); + Assert.AreEqual(11, Unsafe.As(ref dataLoader.ConstantBuffer[96])); + Assert.AreEqual(22, Unsafe.As(ref dataLoader.ConstantBuffer[100])); + Assert.AreEqual(33, Unsafe.As(ref dataLoader.ConstantBuffer[112])); + Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[116])); + Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[120])); - Assert.AreEqual(111, (int)dataLoader.Values[0]); - Assert.AreEqual(222, (int)dataLoader.Values[1]); - Assert.AreEqual(333, (int)dataLoader.Values[2]); + ((IShader)shader).LoadGraphicsResources(ref dataLoader); - Assert.AreEqual(buffer.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); - - fixed (void* p0 = &dataLoader.Values[0]) - { - byte* p1 = (byte*)p0; - - Assert.AreEqual(55, *(float*)&p1[16]); - Assert.AreEqual(44, *(float*)&p1[20]); - Assert.AreEqual(888, *(float*)&p1[24]); - Assert.AreEqual(111, *(float*)&p1[32]); - Assert.AreEqual(222, *(float*)&p1[36]); - Assert.AreEqual(333, *(float*)&p1[40]); - Assert.AreEqual(22, *(int*)&p1[44]); - Assert.AreEqual(1, *(int*)&p1[48]); - Assert.AreEqual(2, *(int*)&p1[52]); - Assert.AreEqual(3, *(int*)&p1[56]); - Assert.AreEqual(3.14, *(double*)&p1[64]); - Assert.AreEqual(6.28, *(double*)&p1[72]); - Assert.AreEqual(42, *(int*)&p1[80]); - Assert.AreEqual(111, *(int*)&p1[84]); - Assert.AreEqual(222, *(int*)&p1[88]); - Assert.AreEqual(11, *(int*)&p1[96]); - Assert.AreEqual(22, *(int*)&p1[100]); - Assert.AreEqual(33, *(int*)&p1[112]); - Assert.AreEqual(44, *(int*)&p1[116]); - Assert.AreEqual(9999, *(int*)&p1[120]); - } + Assert.AreEqual(1, ((IShader)shader).ResourceDescriptorRanges.Length); + Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); + Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } [AutoConstructor] @@ -322,11 +328,10 @@ public partial class ShaderDataLoaderTests } [TestMethod] - public unsafe void LoadNestedCustomTypes() + public void LoadNestedCustomTypes() { using ReadWriteBuffer buffer = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); - DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); NestedCustomTypesShader shader = new( buffer, new CustomType1( @@ -350,56 +355,55 @@ public partial class ShaderDataLoaderTests e: new CustomType3( a: 888888, b: new(333.3f, 444.4f)))); + DebugDispatchDataLoader dataLoader = new(); - ((IShader)shader).LoadDispatchData(ref dataLoader, GraphicsDevice.GetDefault(), 111, 222, 333); + ((IShader)shader).LoadConstantBuffer(ref dataLoader, 111, 222, 333); - Assert.AreEqual(47, dataLoader.Values.Length); - Assert.AreEqual(1, dataLoader.Resources.Length); + Assert.AreEqual(188, ((IShader)shader).ConstantBufferSize); + Assert.IsNotNull(dataLoader.ConstantBuffer); + Assert.AreEqual(188, dataLoader.ConstantBuffer.Length); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[4])); + Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[8])); + Assert.AreEqual(3.14f, Unsafe.As(ref dataLoader.ConstantBuffer[16])); + Assert.AreEqual(6.28f, Unsafe.As(ref dataLoader.ConstantBuffer[20])); + Assert.AreEqual(123.4f, Unsafe.As(ref dataLoader.ConstantBuffer[24])); + Assert.AreEqual(55, Unsafe.As(ref dataLoader.ConstantBuffer[32])); + Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[36])); + Assert.AreEqual(888, Unsafe.As(ref dataLoader.ConstantBuffer[40])); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[48])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[52])); + Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[56])); + Assert.AreEqual(22, Unsafe.As(ref dataLoader.ConstantBuffer[60])); + Assert.AreEqual(1, Unsafe.As(ref dataLoader.ConstantBuffer[64])); + Assert.AreEqual(2, Unsafe.As(ref dataLoader.ConstantBuffer[68])); + Assert.AreEqual(3, Unsafe.As(ref dataLoader.ConstantBuffer[72])); + Assert.AreEqual(3.14, Unsafe.As(ref dataLoader.ConstantBuffer[80])); + Assert.AreEqual(6.28, Unsafe.As(ref dataLoader.ConstantBuffer[88])); + Assert.AreEqual(42, Unsafe.As(ref dataLoader.ConstantBuffer[96])); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[100])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[104])); + Assert.AreEqual(11, Unsafe.As(ref dataLoader.ConstantBuffer[112])); + Assert.AreEqual(22, Unsafe.As(ref dataLoader.ConstantBuffer[116])); + Assert.AreEqual(33, Unsafe.As(ref dataLoader.ConstantBuffer[128])); + Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[132])); + Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[136])); + Assert.AreEqual(42, Unsafe.As(ref dataLoader.ConstantBuffer[140])); + Assert.AreEqual(1234567, Unsafe.As(ref dataLoader.ConstantBuffer[144])); + Assert.AreEqual(44.4f, Unsafe.As(ref dataLoader.ConstantBuffer[148])); + Assert.AreEqual(55.5f, Unsafe.As(ref dataLoader.ConstantBuffer[152])); + Assert.AreEqual(7654321, Unsafe.As(ref dataLoader.ConstantBuffer[160])); + Assert.AreEqual(111.1f, Unsafe.As(ref dataLoader.ConstantBuffer[164])); + Assert.AreEqual(222.2f, Unsafe.As(ref dataLoader.ConstantBuffer[168])); + Assert.AreEqual(888888, Unsafe.As(ref dataLoader.ConstantBuffer[176])); + Assert.AreEqual(333.3f, Unsafe.As(ref dataLoader.ConstantBuffer[180])); + Assert.AreEqual(444.4f, Unsafe.As(ref dataLoader.ConstantBuffer[184])); - Assert.AreEqual(111, (int)dataLoader.Values[0]); - Assert.AreEqual(222, (int)dataLoader.Values[1]); - Assert.AreEqual(333, (int)dataLoader.Values[2]); + ((IShader)shader).LoadGraphicsResources(ref dataLoader); - Assert.AreEqual(buffer.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); - - fixed (void* p0 = &dataLoader.Values[0]) - { - byte* p1 = (byte*)p0; - - Assert.AreEqual(3.14f, *(float*)&p1[16]); - Assert.AreEqual(6.28f, *(float*)&p1[20]); - Assert.AreEqual(123.4f, *(float*)&p1[24]); - Assert.AreEqual(55, *(float*)&p1[32]); - Assert.AreEqual(44, *(float*)&p1[36]); - Assert.AreEqual(888, *(float*)&p1[40]); - Assert.AreEqual(111, *(float*)&p1[48]); - Assert.AreEqual(222, *(float*)&p1[52]); - Assert.AreEqual(333, *(float*)&p1[56]); - Assert.AreEqual(22, *(int*)&p1[60]); - Assert.AreEqual(1, *(int*)&p1[64]); - Assert.AreEqual(2, *(int*)&p1[68]); - Assert.AreEqual(3, *(int*)&p1[72]); - Assert.AreEqual(3.14, *(double*)&p1[80]); - Assert.AreEqual(6.28, *(double*)&p1[88]); - Assert.AreEqual(42, *(int*)&p1[96]); - Assert.AreEqual(111, *(int*)&p1[100]); - Assert.AreEqual(222, *(int*)&p1[104]); - Assert.AreEqual(11, *(int*)&p1[112]); - Assert.AreEqual(22, *(int*)&p1[116]); - Assert.AreEqual(33, *(int*)&p1[128]); - Assert.AreEqual(44, *(int*)&p1[132]); - Assert.AreEqual(9999, *(int*)&p1[136]); - Assert.AreEqual(42, *(int*)&p1[140]); - Assert.AreEqual(1234567, *(int*)&p1[144]); - Assert.AreEqual(44.4f, *(float*)&p1[148]); - Assert.AreEqual(55.5f, *(float*)&p1[152]); - Assert.AreEqual(7654321, *(int*)&p1[160]); - Assert.AreEqual(111.1f, *(float*)&p1[164]); - Assert.AreEqual(222.2f, *(float*)&p1[168]); - Assert.AreEqual(888888, *(int*)&p1[176]); - Assert.AreEqual(333.3f, *(float*)&p1[180]); - Assert.AreEqual(444.4f, *(float*)&p1[184]); - } + Assert.AreEqual(1, ((IShader)shader).ResourceDescriptorRanges.Length); + Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); + Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } [AutoConstructor] @@ -422,7 +426,7 @@ public partial class ShaderDataLoaderTests } [TestMethod] - public unsafe void AmbiguousNames() + public void AmbiguousNames() { using ReadWriteBuffer a = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); using ReadWriteBuffer b = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); @@ -431,24 +435,35 @@ public partial class ShaderDataLoaderTests using ReadWriteBuffer y = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); using ReadWriteBuffer z = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(16); - DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); + AmbiguousNamesShader shader = new(a, b, c, x, y, z, 7777, 8888, 9999); + DebugDispatchDataLoader dataLoader = new(); - ((IShader)new AmbiguousNamesShader(a, b, c, x, y, z, 7777, 8888, 9999)).LoadDispatchData(ref dataLoader, GraphicsDevice.GetDefault(), 111, 222, 333); + ((IShader)shader).LoadConstantBuffer(ref dataLoader, 111, 222, 333); - Assert.AreEqual(6, dataLoader.Values.Length); - Assert.AreEqual(6, dataLoader.Resources.Length); + Assert.AreEqual(24, ((IShader)shader).ConstantBufferSize); + Assert.IsNotNull(dataLoader.ConstantBuffer); + Assert.AreEqual(24, dataLoader.ConstantBuffer.Length); + Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); + Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[4])); + Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[8])); + Assert.AreEqual(7777, Unsafe.As(ref dataLoader.ConstantBuffer[12])); + Assert.AreEqual(8888, Unsafe.As(ref dataLoader.ConstantBuffer[16])); + Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[20])); - Assert.AreEqual(111, (int)dataLoader.Values[0]); - Assert.AreEqual(222, (int)dataLoader.Values[1]); - Assert.AreEqual(333, (int)dataLoader.Values[2]); - Assert.AreEqual(7777, (int)dataLoader.Values[3]); - Assert.AreEqual(8888, (int)dataLoader.Values[4]); - Assert.AreEqual(9999, (int)dataLoader.Values[5]); - Assert.AreEqual(a.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); - Assert.AreEqual(b.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[1]); - Assert.AreEqual(c.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[2]); - Assert.AreEqual(x.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[3]); - Assert.AreEqual(y.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[4]); - Assert.AreEqual(z.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[5]); + ((IShader)shader).LoadGraphicsResources(ref dataLoader); + + Assert.AreEqual(6, ((IShader)shader).ResourceDescriptorRanges.Length); + Assert.AreSame(a, dataLoader.GraphicsResources[0].Resource); + Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); + Assert.AreSame(b, dataLoader.GraphicsResources[1].Resource); + Assert.AreEqual(1u, dataLoader.GraphicsResources[1].Index); + Assert.AreSame(c, dataLoader.GraphicsResources[2].Resource); + Assert.AreEqual(2u, dataLoader.GraphicsResources[2].Index); + Assert.AreSame(x, dataLoader.GraphicsResources[3].Resource); + Assert.AreEqual(3u, dataLoader.GraphicsResources[3].Index); + Assert.AreSame(y, dataLoader.GraphicsResources[4].Resource); + Assert.AreEqual(4u, dataLoader.GraphicsResources[4].Index); + Assert.AreSame(z, dataLoader.GraphicsResources[5].Resource); + Assert.AreEqual(5u, dataLoader.GraphicsResources[5].Index); } } \ No newline at end of file