From 3aeb8430238cc3243420ac32bd42ec91d27586fb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 5 May 2021 18:29:06 +0200 Subject: [PATCH] Removed implicit __z field from pixel shaders --- .../IComputeShaderDataLoaderGenerator.cs | 4 +++- .../SyntaxRewriters/ExecuteMethodRewriter.cs | 15 +++++++++++++++ .../Shaders/Renderer/HlslShaderRenderer.cs | 6 +++++- src/ComputeSharp/Shaders/ShaderRunner{T}.cs | 14 +++++--------- .../Shaders/Translation/IShaderLoader.cs | 5 +++++ .../Translation/ShaderLoader{T}.DispatchData.cs | 8 ++++++-- .../Shaders/Translation/ShaderLoader{T}.cs | 7 +++++++ 7 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs index 3477badd..5dffbd96 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs @@ -154,9 +154,11 @@ namespace ComputeSharp.SourceGenerators { List statements = new(); + var pixelShaderSymbol = structDeclarationSymbol.AllInterfaces.FirstOrDefault(static interfaceSymbol => interfaceSymbol is { IsGenericType: true, Name: nameof(IPixelShader) }); + var isComputeShader = pixelShaderSymbol is null; int resourceOffset = 0, - rawDataOffset = sizeof(int) * 3; + rawDataOffset = sizeof(int) * (isComputeShader ? 3 : 2); AppendFields(structDeclarationSymbol, Array.Empty(), ref resourceOffset, ref rawDataOffset, statements); diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs index 3624a87e..a1943062 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs @@ -172,6 +172,21 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters return updatedNode.WithBody(Block(IfStatement(rangeCheckExpression, updatedNode.Body!))); } + + /// + public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node) + { + IdentifierNameSyntax updatedNode = (IdentifierNameSyntax)base.VisitIdentifierName(node)!; + + // Since pixel shaders are only ever executed on 2D textures, the special __z field can + // be removed and replaced with just numeric literal expressions, where the value is 1. + if (updatedNode.Identifier.Text == "__z") + { + return LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(1)); + } + + return updatedNode; + } } } } diff --git a/src/ComputeSharp/Shaders/Renderer/HlslShaderRenderer.cs b/src/ComputeSharp/Shaders/Renderer/HlslShaderRenderer.cs index 89fead55..3af11b8e 100644 --- a/src/ComputeSharp/Shaders/Renderer/HlslShaderRenderer.cs +++ b/src/ComputeSharp/Shaders/Renderer/HlslShaderRenderer.cs @@ -85,7 +85,11 @@ namespace ComputeSharp.Shaders.Renderer builder.AppendLine('{'); builder.AppendLine(" uint __x;"); builder.AppendLine(" uint __y;"); - builder.AppendLine(" uint __z;"); + + if (info.IsComputeShader) + { + builder.AppendLine(" uint __z;"); + } foreach (var local in info.FieldsInfo) { diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index c01dd702..908c0c67 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -193,20 +193,16 @@ namespace ComputeSharp.Shaders int x = texture.Width, y = texture.Height, - z = 1, threadsX = 8, threadsY = 8, - threadsZ = 1, groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1), - groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1), - groupsZ = Math.DivRem(z, threadsZ, out int modZ) + (modZ == 0 ? 0 : 1); + groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1); Guard.IsBetweenOrEqualTo(groupsX, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); Guard.IsBetweenOrEqualTo(groupsY, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); - Guard.IsBetweenOrEqualTo(groupsZ, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); // Create the shader key - ShaderKey key = new(ShaderHashCodeProvider.GetHashCode(in shader), threadsX, threadsY, threadsZ); + ShaderKey key = new(ShaderHashCodeProvider.GetHashCode(in shader), threadsX, threadsY, 1); CachedShader shaderData; PipelineData? pipelineData; @@ -215,7 +211,7 @@ namespace ComputeSharp.Shaders // Get or preload the shader if (!ShadersCache.TryGetValue(key, out shaderData)) { - LoadShader(threadsX, threadsY, threadsZ, in shader, out shaderData); + LoadShader(threadsX, threadsY, 1, in shader, out shaderData); // Cache for later use ShadersCache.Add(key, shaderData); @@ -235,7 +231,7 @@ namespace ComputeSharp.Shaders commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineData.D3D12PipelineState); // Extract the dispatch data for the shader invocation - using DispatchData dispatchData = shaderData.Loader.GetDispatchData(device, in shader, x, y, z); + using DispatchData dispatchData = shaderData.Loader.GetDispatchData(device, in shader, x, y, 1); // Initialize the loop targets and the captured values commandList.D3D12GraphicsCommandList->SetComputeRoot32BitConstants(dispatchData.Variables); @@ -254,7 +250,7 @@ namespace ComputeSharp.Shaders } // Dispatch and wait for completion - commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ); + commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, 1); commandList.ExecuteAndWaitForCompletion(); } diff --git a/src/ComputeSharp/Shaders/Translation/IShaderLoader.cs b/src/ComputeSharp/Shaders/Translation/IShaderLoader.cs index 23438eff..ccbdc33f 100644 --- a/src/ComputeSharp/Shaders/Translation/IShaderLoader.cs +++ b/src/ComputeSharp/Shaders/Translation/IShaderLoader.cs @@ -13,6 +13,11 @@ namespace ComputeSharp.Shaders.Translation /// string EntryPoint { get; } + /// + /// Gets whether or not the current shader is a compute shader (and not a pixel shader). + /// + bool IsComputeShader { get; } + /// /// Gets the collection of items for the shader fields. /// diff --git a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.DispatchData.cs b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.DispatchData.cs index 80b0d868..95c80c19 100644 --- a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.DispatchData.cs +++ b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.DispatchData.cs @@ -62,10 +62,14 @@ namespace ComputeSharp.Shaders.Translation ref ulong r0 = ref MemoryMarshal.GetArrayDataReference(resources); ref byte r1 = ref MemoryMarshal.GetArrayDataReference(variables); - // Set the x, y and z counters + // Set the x, y and (optionally) z counters Unsafe.As(ref r1) = x; Unsafe.Add(ref Unsafe.As(ref r1), 1) = y; - Unsafe.Add(ref Unsafe.As(ref r1), 2) = z; + + if (IsComputeShader) + { + Unsafe.Add(ref Unsafe.As(ref r1), 2) = z; + } // Invoke the dynamic method to extract the captured data int totalVariablesByteSize = this.dispatchDataLoader!(device, in shader, ref r0, ref r1); diff --git a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs index 392b6a17..d46ee25a 100644 --- a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs @@ -98,6 +98,9 @@ namespace ComputeSharp.Shaders.Translation /// public string EntryPoint { get; private set; } + /// + public bool IsComputeShader { get; private set; } + /// public IReadOnlyList HlslResourceInfo => this.hlslResourceInfo; @@ -193,6 +196,10 @@ namespace ComputeSharp.Shaders.Translation } } + // Set the current shader type + IsComputeShader = d3D12DescriptorRanges1.Count == 0; + + // Ensure the shader has a valid number of captured resources if (shaderFields.Count == 0 && d3D12DescriptorRanges1.Count == 0) { ThrowHelper.ThrowInvalidOperationException("The shader body must contain at least one field");