From 1734c09d1a276dfd350f6107c31f7aa51094d2b9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Oct 2023 01:28:49 +0200 Subject: [PATCH] Add HLSL bytecode to ShaderInfo, remove leftovers --- .../Shaders/Interop/ReflectionServices.cs | 88 +++++-------------- .../Shaders/Interop/ShaderInfo.cs | 4 + .../ComputeSharp.Tests/ShaderCompilerTests.cs | 9 ++ 3 files changed, 33 insertions(+), 68 deletions(-) diff --git a/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs b/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs index adfff47f..a6f0be37 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs @@ -17,97 +17,43 @@ public static class ReflectionServices { /// /// Gets the shader info associated with a given compute shader. - /// - /// This overload can be used for simplicity when the compute shader being inspected does not rely on captured - /// objects to be processed correctly. This is the case when it does not contain any -s. - /// /// /// The type of compute shader to retrieve info for. /// The resulting instance. - /// - /// The thread group sizes will always be set to (1, 1, 1) in the returned shader. This is done to - /// avoid having to compiler multiple shaders just to get reflection info for them. When using any of - /// the APIs to dispatch a shader, the thread sizes would actually be set to a proper value insead. - /// public static ShaderInfo GetShaderInfo() where T : struct, IComputeShader, IComputeShaderDescriptor { - return GetNonGenericShaderInfo(default(T)); - } - - /// - /// Gets the shader info associated with a given compute shader. - /// - /// The type of compute shader to retrieve info for. - /// The input compute shader to retrieve info for. - /// The resulting instance. - /// - /// The thread group sizes will always be set to (1, 1, 1) in the returned shader. This is done to - /// avoid having to compiler multiple shaders just to get reflection info for them. When using any of - /// the APIs to dispatch a shader, the thread sizes would actually be set to a proper value insead. - /// - public static unsafe ShaderInfo GetShaderInfo(in T shader) - where T : struct, IComputeShader, IComputeShaderDescriptor - { - return GetNonGenericShaderInfo(in shader); + return GetNonGenericShaderInfo(); } /// /// Gets the shader info associated with a given pixel shader. - /// - /// This overload can be used for simplicity when the pixel shader being inspected does not rely on captured - /// objects to be processed correctly. This is the case when it does not contain any -s. - /// /// /// The type of pixel shader to retrieve info for. /// The type of pixels being processed by the shader. /// The resulting instance. - /// - /// The thread group sizes will always be set to (1, 1, 1) in the returned shader. This is done to - /// avoid having to compiler multiple shaders just to get reflection info for them. When using any of - /// the APIs to dispatch a shader, the thread sizes would actually be set to a proper value insead. - /// public static ShaderInfo GetShaderInfo() where T : struct, IComputeShader, IComputeShaderDescriptor where TPixel : unmanaged { - return GetNonGenericShaderInfo(default(T)); - } - - /// - /// Gets the shader info associated with a given pixel shader. - /// - /// The type of pixel shader to retrieve info for. - /// The type of pixels being processed by the shader. - /// The input pixel shader to retrieve info for. - /// The resulting instance. - /// - /// The thread group sizes will always be set to (1, 1, 1) in the returned shader. This is done to - /// avoid having to compiler multiple shaders just to get reflection info for them. When using any of - /// the APIs to dispatch a shader, the thread sizes would actually be set to a proper value insead. - /// - public static unsafe ShaderInfo GetShaderInfo(in T shader) - where T : struct, IComputeShader, IComputeShaderDescriptor - where TPixel : unmanaged - { - return GetNonGenericShaderInfo(in shader); + return GetNonGenericShaderInfo(); } /// /// Gets the shader info associated with a given shader of any type. /// /// The type of shader to retrieve info for. - /// The input shader to retrieve info for. /// The resulting instance. - private static unsafe ShaderInfo GetNonGenericShaderInfo(in T shader) + private static unsafe ShaderInfo GetNonGenericShaderInfo() where T : struct, IComputeShaderDescriptor { - string hlslSource = Unsafe.AsRef(in shader).HlslSource; + Unsafe.SkipInit(out T shader); - using ComPtr dxcBlobBytecode = Shaders.Translation.ShaderCompiler.Instance.Compile(hlslSource); + ReadOnlyMemory hlslBytecode = shader.HlslBytecode; using ComPtr dxcUtils = default; + // Load the DXC compiler (the package will bundle the necessary .dll-s) DirectX.DxcCreateInstance( (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_DxcLibrary)), Windows.__uuidof(), @@ -115,14 +61,19 @@ public static class ReflectionServices using ComPtr d3D12ShaderReflection = default; - DxcBuffer dxcBuffer = default; - dxcBuffer.Ptr = dxcBlobBytecode.Get()->GetBufferPointer(); - dxcBuffer.Size = dxcBlobBytecode.Get()->GetBufferSize(); + // Reflect over the shader (this will fail if reflection metadata has been stripped). + // Since that's just opt-in from users, that is the expected behavior, so not an issue. + fixed (byte* hlslBytecodePtr = hlslBytecode.Span) + { + DxcBuffer dxcBuffer = default; + dxcBuffer.Ptr = hlslBytecodePtr; + dxcBuffer.Size = (uint)hlslBytecode.Length; - dxcUtils.Get()->CreateReflection( - &dxcBuffer, - Windows.__uuidof(), - (void**)d3D12ShaderReflection.GetAddressOf()).Assert(); + dxcUtils.Get()->CreateReflection( + &dxcBuffer, + Windows.__uuidof(), + (void**)d3D12ShaderReflection.GetAddressOf()).Assert(); + } D3D12_SHADER_DESC d3D12ShaderDescription; @@ -130,7 +81,8 @@ public static class ReflectionServices return new( CompilerVersion: new string(d3D12ShaderDescription.Creator), - HlslSource: hlslSource, + HlslSource: shader.HlslSource, + HlslBytecode: hlslBytecode, ConstantBufferCount: d3D12ShaderDescription.ConstantBuffers, BoundResourceCount: d3D12ShaderDescription.BoundResources, InstructionCount: d3D12ShaderDescription.InstructionCount, diff --git a/src/ComputeSharp.Dynamic/Shaders/Interop/ShaderInfo.cs b/src/ComputeSharp.Dynamic/Shaders/Interop/ShaderInfo.cs index 2b2064b7..4dba4231 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Interop/ShaderInfo.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Interop/ShaderInfo.cs @@ -1,3 +1,5 @@ +using System; + namespace ComputeSharp.Interop; /// @@ -5,6 +7,7 @@ namespace ComputeSharp.Interop; /// /// The compiler version used to build the shader. /// The HLSL source code the shader was built from. +/// The HLSL bytecode for the shader. /// The number of shader constant buffers. /// The number of resource (textures and buffers) bound to a shader. /// The number of intermediate-language instructions in the compiled shader. @@ -31,6 +34,7 @@ namespace ComputeSharp.Interop; public sealed record ShaderInfo( string CompilerVersion, string HlslSource, + ReadOnlyMemory HlslBytecode, uint ConstantBufferCount, uint BoundResourceCount, uint InstructionCount, diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index ca4f35b0..80ef6827 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -1,4 +1,5 @@ using ComputeSharp; +using ComputeSharp.Descriptors; using ComputeSharp.Interop; using ComputeSharp.Tests.Misc; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -11,6 +12,14 @@ namespace ComputeSharp.Tests [TestCategory("ShaderCompiler")] public partial class ShaderCompilerTests { + [TestMethod] + public void ReflectionBytecode() + { + ShaderInfo shaderInfo = ReflectionServices.GetShaderInfo(); + + CollectionAssert.AreEqual(((IComputeShaderDescriptor)default(ReservedKeywordsShader)).HlslBytecode.ToArray(), shaderInfo.HlslBytecode.ToArray()); + } + [TestMethod] public void ReservedKeywords() {