From 91f78969b12265290d9f082eda3550a19c402890 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 18 Oct 2023 19:39:10 +0200 Subject: [PATCH] Adopt static abstracts in ComputeSharp (and related projects) --- .../Interop/ReflectionServices.cs | 6 +- ...torGenerator.DispatchDataLoading.Syntax.cs | 4 +- ...DescriptorGenerator.HlslBytecode.Syntax.cs | 38 +------- ...erDescriptorGenerator.HlslSource.Syntax.cs | 2 +- ...ptorGenerator.MetadataProperties.Syntax.cs | 4 +- ...erDescriptorGenerator.NumThreads.Syntax.cs | 6 +- ...nerator.ResourceDescriptorRanges.Syntax.cs | 2 +- .../ComputeShaderDescriptorGenerator.cs | 6 +- .../IComputeShaderDescriptor{T}.cs | 26 +++--- src/ComputeSharp/Shaders/ComputeContext.cs | 18 ++-- .../Shaders/Loading/PipelineDataLoader{T}.cs | 14 ++- .../ShaderDataLoaderTests.cs | 88 +++++++++++++------ .../ComputeSharp.Tests/ShaderCompilerTests.cs | 9 +- .../ThreadGroupSizeTests.cs | 23 ++--- 14 files changed, 119 insertions(+), 127 deletions(-) diff --git a/src/ComputeSharp.Dxc/Interop/ReflectionServices.cs b/src/ComputeSharp.Dxc/Interop/ReflectionServices.cs index 297a8ba0..60bb70a5 100644 --- a/src/ComputeSharp.Dxc/Interop/ReflectionServices.cs +++ b/src/ComputeSharp.Dxc/Interop/ReflectionServices.cs @@ -43,9 +43,7 @@ public static partial class ReflectionServices private static unsafe ShaderInfo GetNonGenericShaderInfo() where T : struct, IComputeShaderDescriptor { - Unsafe.SkipInit(out T shader); - - ReadOnlyMemory hlslBytecode = shader.HlslBytecode; + ReadOnlyMemory hlslBytecode = T.HlslBytecode; using ComPtr dxcUtils = default; @@ -77,7 +75,7 @@ public static partial class ReflectionServices return new( CompilerVersion: new string(d3D12ShaderDescription.Creator), - HlslSource: shader.HlslSource, + HlslSource: T.HlslSource, HlslBytecode: hlslBytecode, ConstantBufferCount: d3D12ShaderDescription.ConstantBuffers, BoundResourceCount: d3D12ShaderDescription.BoundResources, diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs index 7a8669d7..87ed9c4d 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs @@ -23,7 +23,7 @@ partial class ComputeShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]"); - writer.WriteLine($"readonly void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader, int x, int y, int z)"); + writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader, int x, int y, int z)"); using (writer.WriteBlock()) { @@ -99,7 +99,7 @@ partial class ComputeShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadGraphicsResources(in {typeName} shader, ref TLoader loader)"); + writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadGraphicsResources(in {typeName} shader, ref TLoader loader)"); using (writer.WriteBlock()) { diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs index dcee6b1c..15b1ee5c 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs @@ -20,7 +20,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.Write($"readonly global::System.ReadOnlyMemory global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslBytecode => "); + writer.Write($"static global::System.ReadOnlyMemory global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslBytecode => "); // Just like with the D2D1 generator, only return the MemoryManager if we do have HLSL bytecode if (info.HlslInfo is not HlslBytecodeInfo.Success) @@ -119,41 +119,5 @@ partial class ComputeShaderDescriptorGenerator callbacks.Add(Callback); } - - /// - /// Writes the ThreadsX property. - /// - /// The input instance with gathered shader info. - /// The instance to write into. - public static void WriteThreadsXSyntax(ShaderInfo info, IndentedTextWriter writer) - { - writer.WriteLine("/// "); - writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsX => {info.ThreadsX};"); - } - - /// - /// Writes the ThreadsY property. - /// - /// The input instance with gathered shader info. - /// The instance to write into. - public static void WriteThreadsYSyntax(ShaderInfo info, IndentedTextWriter writer) - { - writer.WriteLine("/// "); - writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsY => {info.ThreadsY};"); - } - - /// - /// Writes the ThreadsZ property. - /// - /// The input instance with gathered shader info. - /// The instance to write into. - public static void WriteThreadsZSyntax(ShaderInfo info, IndentedTextWriter writer) - { - writer.WriteLine("/// "); - writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsZ => {info.ThreadsZ};"); - } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs index fdb68dae..1dd1c909 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs @@ -19,7 +19,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly string global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslSource =>"); + writer.WriteLine($"static string global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslSource =>"); writer.IncreaseIndent(); writer.WriteLine("\"\"\""); writer.Write(info.HlslInfoKey.HlslSource, isMultiline: true); diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.MetadataProperties.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.MetadataProperties.Syntax.cs index c13050d1..a4eabb67 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.MetadataProperties.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.MetadataProperties.Syntax.cs @@ -21,7 +21,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ConstantBufferSize => {info.ConstantBufferSizeInBytes};"); + writer.WriteLine($"static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ConstantBufferSize => {info.ConstantBufferSizeInBytes};"); } /// @@ -33,7 +33,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly bool global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.IsStaticSamplerRequired => {info.IsSamplerUsed.ToString().ToLowerInvariant()};"); + writer.WriteLine($"static bool global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.IsStaticSamplerRequired => {info.IsSamplerUsed.ToString().ToLowerInvariant()};"); } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.Syntax.cs index deafb48c..d5ede4da 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.Syntax.cs @@ -19,7 +19,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsX => {info.ThreadsX};"); + writer.WriteLine($"static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsX => {info.ThreadsX};"); } /// @@ -31,7 +31,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsY => {info.ThreadsY};"); + writer.WriteLine($"static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsY => {info.ThreadsY};"); } /// @@ -43,7 +43,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsZ => {info.ThreadsZ};"); + writer.WriteLine($"static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsZ => {info.ThreadsZ};"); } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs index 4a86db94..56e237c1 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs @@ -19,7 +19,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.Write($"readonly global::System.ReadOnlyMemory global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ResourceDescriptorRanges => "); + writer.Write($"static global::System.ReadOnlyMemory global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ResourceDescriptorRanges => "); // If there are no declared resources, just return an empty collection if (info.ResourceDescriptors.IsEmpty) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs index ce59ce78..c4a64b2c 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs @@ -144,9 +144,9 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener { using ImmutableArrayBuilder> declaredMembers = new(); - declaredMembers.Add(HlslBytecode.WriteThreadsXSyntax); - declaredMembers.Add(HlslBytecode.WriteThreadsYSyntax); - declaredMembers.Add(HlslBytecode.WriteThreadsZSyntax); + declaredMembers.Add(NumThreads.WriteThreadsXSyntax); + declaredMembers.Add(NumThreads.WriteThreadsYSyntax); + declaredMembers.Add(NumThreads.WriteThreadsZSyntax); declaredMembers.Add(MetadataProperties.WriteConstantBufferSizeSyntax); declaredMembers.Add(MetadataProperties.WriteIsStaticSamplerRequiredSyntax); declaredMembers.Add(ResourceDescriptorRanges.WriteSyntax); diff --git a/src/ComputeSharp/Core/Interfaces/Descriptors/IComputeShaderDescriptor{T}.cs b/src/ComputeSharp/Core/Interfaces/Descriptors/IComputeShaderDescriptor{T}.cs index 55a550b6..2f1ae452 100644 --- a/src/ComputeSharp/Core/Interfaces/Descriptors/IComputeShaderDescriptor{T}.cs +++ b/src/ComputeSharp/Core/Interfaces/Descriptors/IComputeShaderDescriptor{T}.cs @@ -13,19 +13,19 @@ public interface IComputeShaderDescriptor where T : struct { /// - /// The number of threads in each thread group for the X axis. + /// Gets the number of threads in each thread group for the X axis. /// - int ThreadsX { get; } + static abstract int ThreadsX { get; } /// - /// The number of threads in each thread group for the Y axis. + /// Gets the number of threads in each thread group for the Y axis. /// - int ThreadsY { get; } + static abstract int ThreadsY { get; } /// - /// The number of threads in each thread group for the Z axis. + /// Gets the number of threads in each thread group for the Z axis. /// - int ThreadsZ { get; } + static abstract int ThreadsZ { get; } /// /// Gets the size in bytes of the constant buffer for the current shader. @@ -35,7 +35,7 @@ public interface IComputeShaderDescriptor /// ID3D12GraphicsCommandList::SetComputeRoot32BitConstants, /// so the size must be a multiple of the size of a DWORD value (ie. 4 bytes). /// - int ConstantBufferSize { get; } + static abstract int ConstantBufferSize { get; } /// /// Gets whether the shader requires a static sampler being declared and initialized. @@ -43,22 +43,22 @@ public interface IComputeShaderDescriptor /// /// When requested, a static sampler with linear sampling and mirror addressing will be bound to register(s). /// - bool IsStaticSamplerRequired { get; } + static abstract bool IsStaticSamplerRequired { get; } /// /// Gets the resource descriptor ranges for all resources that should be bound to this shader. /// - ReadOnlyMemory ResourceDescriptorRanges { get; } + static abstract ReadOnlyMemory ResourceDescriptorRanges { get; } /// /// Gets the HLSL source code for the current shader instance. /// - string HlslSource { get; } + static abstract string HlslSource { get; } /// /// Gets the HLSL bytecode for the current shader. /// - ReadOnlyMemory HlslBytecode { get; } + static abstract ReadOnlyMemory HlslBytecode { get; } /// /// Loads the constant buffer of a given input shader. @@ -69,7 +69,7 @@ public interface IComputeShaderDescriptor /// 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. - void LoadConstantBuffer(in T shader, ref TLoader loader, int x, int y, int z) + static abstract void LoadConstantBuffer(in T shader, ref TLoader loader, int x, int y, int z) where TLoader : struct, IConstantBufferLoader; /// @@ -78,6 +78,6 @@ public interface IComputeShaderDescriptor /// The type of graphics resource loader being used. /// The input shader to load the graphics resources for. /// The instance to use to load the data. - void LoadGraphicsResources(in T shader, ref TLoader loader) + static abstract void LoadGraphicsResources(in T shader, ref TLoader loader) where TLoader : struct, IGraphicsResourceLoader; } \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 468a8995..cc50e76b 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -157,9 +157,9 @@ public struct ComputeContext : IDisposable, IAsyncDisposable default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(y); default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(z); - int groupsX = Math.DivRem(x, shader.ThreadsX, out int modX) + (modX == 0 ? 0 : 1); - int groupsY = Math.DivRem(y, shader.ThreadsY, out int modY) + (modY == 0 ? 0 : 1); - int groupsZ = Math.DivRem(z, shader.ThreadsZ, out int modZ) + (modZ == 0 ? 0 : 1); + int groupsX = Math.DivRem(x, T.ThreadsX, out int modX) + (modX == 0 ? 0 : 1); + int groupsY = Math.DivRem(y, T.ThreadsY, out int modY) + (modY == 0 ? 0 : 1); + int groupsZ = Math.DivRem(z, T.ThreadsZ, out int modZ) + (modZ == 0 ? 0 : 1); default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION); default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); @@ -173,11 +173,11 @@ public struct ComputeContext : IDisposable, IAsyncDisposable D3D12GraphicsCommandListConstantBufferLoader dataLoader = new(commandList.D3D12GraphicsCommandList); - shader.LoadConstantBuffer(in shader, ref dataLoader, x, y, z); + T.LoadConstantBuffer(in shader, ref dataLoader, x, y, z); D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 1); - shader.LoadGraphicsResources(in shader, ref graphicsResourceLoader); + T.LoadGraphicsResources(in shader, ref graphicsResourceLoader); commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ); } @@ -197,8 +197,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable int x = texture.Width; int y = texture.Height; - int groupsX = Math.DivRem(x, shader.ThreadsX, out int modX) + (modX == 0 ? 0 : 1); - int groupsY = Math.DivRem(y, shader.ThreadsY, out int modY) + (modY == 0 ? 0 : 1); + int groupsX = Math.DivRem(x, T.ThreadsX, out int modX) + (modX == 0 ? 0 : 1); + int groupsY = Math.DivRem(y, T.ThreadsY, out int modY) + (modY == 0 ? 0 : 1); default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION); default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); @@ -211,11 +211,11 @@ public struct ComputeContext : IDisposable, IAsyncDisposable D3D12GraphicsCommandListConstantBufferLoader constantBufferLoader = new(commandList.D3D12GraphicsCommandList); - shader.LoadConstantBuffer(in shader, ref constantBufferLoader, x, y, 1); + T.LoadConstantBuffer(in shader, ref constantBufferLoader, x, y, 1); D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 2); - shader.LoadGraphicsResources(in shader, ref graphicsResourceLoader); + T.LoadGraphicsResources(in shader, ref graphicsResourceLoader); // Load the implicit output texture commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable( diff --git a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs index 0a2d8c94..72438276 100644 --- a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs +++ b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs @@ -65,9 +65,7 @@ internal static unsafe class PipelineDataLoader /// The allocated instance. private static void CreateD3D12RootSignature(ID3D12Device* d3D12Device, ID3D12RootSignature** d3D12RootSignature) { - Unsafe.SkipInit(out T shader); - - ReadOnlySpan resourceDescriptorRanges = shader.ResourceDescriptorRanges.Span; + ReadOnlySpan resourceDescriptorRanges = T.ResourceDescriptorRanges.Span; D3D12_DESCRIPTOR_RANGE1[]? d3D12DescriptorRangesArray = null; @@ -94,13 +92,13 @@ internal static unsafe class PipelineDataLoader } // Calculate the actual size of the constant buffer in DWORD values - int root32BitConstantCount = shader.ConstantBufferSize / sizeof(int); + int root32BitConstantCount = T.ConstantBufferSize / sizeof(int); // Create the D3D12 root signature d3D12Device->CreateRootSignature( root32BitConstantCount, d3D12DescriptorRanges, - shader.IsStaticSamplerRequired, + T.IsStaticSamplerRequired, d3D12RootSignature); // Return the array to the pool if no exceptions have been thrown @@ -118,11 +116,9 @@ internal static unsafe class PipelineDataLoader /// The resulting instance. private static void CreateD3D12PipelineState(ID3D12Device* d3D12Device, ID3D12RootSignature* d3D12RootSignature, ID3D12PipelineState** d3D12PipelineState) { - Unsafe.SkipInit(out T shader); - - fixed (byte* hlslBytecodePtr = shader.HlslBytecode.Span) + fixed (byte* hlslBytecodePtr = T.HlslBytecode.Span) { - D3D12_SHADER_BYTECODE d3D12ShaderBytecode = new(hlslBytecodePtr, (nuint)shader.HlslBytecode.Length); + D3D12_SHADER_BYTECODE d3D12ShaderBytecode = new(hlslBytecodePtr, (nuint)T.HlslBytecode.Length); d3D12Device->CreateComputePipelineState(d3D12RootSignature, d3D12ShaderBytecode, d3D12PipelineState); } diff --git a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs index 105203c4..0b57e733 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs @@ -1,5 +1,7 @@ +using System; using System.Runtime.CompilerServices; using ComputeSharp.Descriptors; +using ComputeSharp.Interop; using ComputeSharp.Tests.Internals.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -29,18 +31,18 @@ public partial class ShaderDataLoaderTests CapturedResourceShader shader = new(buffer); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(12, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(12, 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])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(1, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(1, ResourceDescriptorRanges().Length); Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } @@ -70,9 +72,9 @@ public partial class ShaderDataLoaderTests MultipleResourcesAndPrimitivesShader shader = new(buffer0, buffer1, 1, 22, 77); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(24, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(24, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(24, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -82,9 +84,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(22, Unsafe.As(ref dataLoader.ConstantBuffer[16])); Assert.AreEqual(77, Unsafe.As(ref dataLoader.ConstantBuffer[20])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(2, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(2, ResourceDescriptorRanges().Length); Assert.AreSame(buffer0, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); Assert.AreSame(buffer1, dataLoader.GraphicsResources[1].Resource); @@ -117,9 +119,9 @@ public partial class ShaderDataLoaderTests ScalarAndVectorTypesShader shader = new(buffer, new(55, 44, 888), 22, 77, new(3.14, 6.28), 42, 9999); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(72, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(72, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(72, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -135,9 +137,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(42, Unsafe.As(ref dataLoader.ConstantBuffer[64])); Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[68])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(1, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(1, ResourceDescriptorRanges().Length); Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } @@ -179,9 +181,9 @@ public partial class ShaderDataLoaderTests d: 9999); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(124, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(124, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(124, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -208,9 +210,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[116])); Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[120])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(1, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(1, ResourceDescriptorRanges().Length); Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } @@ -259,9 +261,9 @@ public partial class ShaderDataLoaderTests d: 9999)); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(124, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(124, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(124, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -288,9 +290,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[116])); Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[120])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(1, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(1, ResourceDescriptorRanges().Length); Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } @@ -363,9 +365,9 @@ public partial class ShaderDataLoaderTests b: new(333.3f, 444.4f)))); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(188, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(188, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(188, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -405,9 +407,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(333.3f, Unsafe.As(ref dataLoader.ConstantBuffer[180])); Assert.AreEqual(444.4f, Unsafe.As(ref dataLoader.ConstantBuffer[184])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(1, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(1, ResourceDescriptorRanges().Length); Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } @@ -445,9 +447,9 @@ public partial class ShaderDataLoaderTests AmbiguousNamesShader shader = new(a, b, c, x, y, z, 7777, 8888, 9999); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(24, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(24, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(24, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -457,9 +459,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(8888, Unsafe.As(ref dataLoader.ConstantBuffer[16])); Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[20])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(6, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(6, ResourceDescriptorRanges().Length); Assert.AreSame(a, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); Assert.AreSame(b, dataLoader.GraphicsResources[1].Resource); @@ -473,4 +475,34 @@ public partial class ShaderDataLoaderTests Assert.AreSame(z, dataLoader.GraphicsResources[5].Resource); Assert.AreEqual(5u, dataLoader.GraphicsResources[5].Index); } + + /// + private static int ConstantBufferSize() + where T : struct, IComputeShaderDescriptor + { + return T.ConstantBufferSize; + } + + /// + private static ReadOnlyMemory ResourceDescriptorRanges() + where T : struct, IComputeShaderDescriptor + { + return T.ResourceDescriptorRanges; + } + + /// + private static void LoadConstantBuffer(in T shader, ref TLoader loader, int x, int y, int z) + where T : struct, IComputeShaderDescriptor + where TLoader : struct, IConstantBufferLoader + { + T.LoadConstantBuffer(in shader, ref loader, x, y, z); + } + + /// + private static void LoadGraphicsResources(in T shader, ref TLoader loader) + where T : struct, IComputeShaderDescriptor + where TLoader : struct, IGraphicsResourceLoader + { + T.LoadGraphicsResources(in shader, ref loader); + } } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 80ef6827..79a8b2a8 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -1,3 +1,4 @@ +using System; using ComputeSharp; using ComputeSharp.Descriptors; using ComputeSharp.Interop; @@ -15,9 +16,15 @@ namespace ComputeSharp.Tests [TestMethod] public void ReflectionBytecode() { + static ReadOnlyMemory GetHlslBytecode() + where T : struct, IComputeShaderDescriptor + { + return T.HlslBytecode; + } + ShaderInfo shaderInfo = ReflectionServices.GetShaderInfo(); - CollectionAssert.AreEqual(((IComputeShaderDescriptor)default(ReservedKeywordsShader)).HlslBytecode.ToArray(), shaderInfo.HlslBytecode.ToArray()); + CollectionAssert.AreEqual(GetHlslBytecode().ToArray(), shaderInfo.HlslBytecode.ToArray()); } [TestMethod] diff --git a/tests/ComputeSharp.Tests/ThreadGroupSizeTests.cs b/tests/ComputeSharp.Tests/ThreadGroupSizeTests.cs index 58aa2a45..8f135864 100644 --- a/tests/ComputeSharp.Tests/ThreadGroupSizeTests.cs +++ b/tests/ComputeSharp.Tests/ThreadGroupSizeTests.cs @@ -10,21 +10,16 @@ public partial class ThreadGroupSizeTests [TestMethod] public unsafe void Verify_ThreadGroupSize() { - Assert.AreEqual(64, ((IComputeShaderDescriptor)default(DispatchXShader)).ThreadsX); - Assert.AreEqual(1, ((IComputeShaderDescriptor)default(DispatchXShader)).ThreadsY); - Assert.AreEqual(1, ((IComputeShaderDescriptor)default(DispatchXShader)).ThreadsZ); + static (int X, int Y, int Z) GetNumThreads() + where T : struct, IComputeShaderDescriptor + { + return (T.ThreadsX, T.ThreadsY, T.ThreadsZ); + } - Assert.AreEqual(8, ((IComputeShaderDescriptor)default(DispatchXYShader)).ThreadsX); - Assert.AreEqual(8, ((IComputeShaderDescriptor)default(DispatchXYShader)).ThreadsY); - Assert.AreEqual(1, ((IComputeShaderDescriptor)default(DispatchXYShader)).ThreadsZ); - - Assert.AreEqual(4, ((IComputeShaderDescriptor)default(DispatchXYZShader)).ThreadsX); - Assert.AreEqual(4, ((IComputeShaderDescriptor)default(DispatchXYZShader)).ThreadsY); - Assert.AreEqual(4, ((IComputeShaderDescriptor)default(DispatchXYZShader)).ThreadsZ); - - Assert.AreEqual(11, ((IComputeShaderDescriptor)default(DispatchCustomShader)).ThreadsX); - Assert.AreEqual(14, ((IComputeShaderDescriptor)default(DispatchCustomShader)).ThreadsY); - Assert.AreEqual(6, ((IComputeShaderDescriptor)default(DispatchCustomShader)).ThreadsZ); + Assert.AreEqual((64, 1, 1), GetNumThreads()); + Assert.AreEqual((8, 8, 1), GetNumThreads()); + Assert.AreEqual((4, 4, 4), GetNumThreads()); + Assert.AreEqual((11, 14, 6), GetNumThreads()); } [AutoConstructor]