From 7f00437a483835a5eea1d9a1a3fe4317e2a85ded Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Oct 2023 01:17:00 +0200 Subject: [PATCH] Add HLSL bytecode to D2D1ShaderInfo --- .../Shaders/Interop/D2D1ReflectionServices.cs | 30 ++++--------------- .../Shaders/Interop/D2D1ShaderInfo.cs | 4 +++ .../D2D1ReflectionServicesTests.cs | 6 ++++ 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs index 7d478063..212b146f 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs @@ -2,7 +2,6 @@ using System; using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Descriptors; using ComputeSharp.D2D1.Extensions; -using ComputeSharp.D2D1.Shaders.Translation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -31,33 +30,15 @@ public static class D2D1ReflectionServices { Unsafe.SkipInit(out T shader); - string hlslSource = shader.HlslSource; + ReadOnlyMemory hlslBytecode = D2D1PixelShader.LoadBytecode(); using ComPtr d3D11ShaderReflection = default; - // If the shader already has embedded bytecode, use that directly - if (shader.HlslBytecode is ReadOnlyMemory { Length: > 0 } hlslBytecode) + fixed (byte* hlslBytecodePtr = hlslBytecode.Span) { - fixed (byte* hlslBytecodePtr = hlslBytecode.Span) - { - DirectX.D3DReflect( - pSrcData: hlslBytecodePtr, - SrcDataSize: (nuint)hlslBytecode.Length, - pInterface: Windows.__uuidof(), - ppReflector: (void**)d3D11ShaderReflection.GetAddressOf()).Assert(); - } - } - else - { - // Otherwise, compile it with default shader profile and compile options and use that. - // Note that we're intentionally retrieving an ID3DBlob instance here to avoid having - // to allocate a ReadOnlyMemory object to throw away immediately, as we don't - // actually need to keep the bytecode for later. - using ComPtr dynamicBytecode = D3DCompiler.Compile(hlslSource.AsSpan(), shader.ShaderProfile, shader.CompileOptions); - DirectX.D3DReflect( - pSrcData: (byte*)dynamicBytecode.Get()->GetBufferPointer(), - SrcDataSize: dynamicBytecode.Get()->GetBufferSize(), + pSrcData: hlslBytecodePtr, + SrcDataSize: (nuint)hlslBytecode.Length, pInterface: Windows.__uuidof(), ppReflector: (void**)d3D11ShaderReflection.GetAddressOf()).Assert(); } @@ -72,7 +53,8 @@ public static class D2D1ReflectionServices return new( CompilerVersion: new string(d3D11ShaderDescription.Creator), - HlslSource: hlslSource, + HlslSource: shader.HlslSource, + HlslBytecode: hlslBytecode, ConstantBufferCount: d3D11ShaderDescription.ConstantBuffers, BoundResourceCount: d3D11ShaderDescription.BoundResources, InstructionCount: d3D11ShaderDescription.InstructionCount, diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderInfo.cs index b909aa39..1106b56f 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderInfo.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderInfo.cs @@ -1,3 +1,5 @@ +using System; + namespace ComputeSharp.D2D1.Interop; /// @@ -5,6 +7,7 @@ namespace ComputeSharp.D2D1.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. @@ -32,6 +35,7 @@ namespace ComputeSharp.D2D1.Interop; public sealed record D2D1ShaderInfo( string CompilerVersion, string HlslSource, + ReadOnlyMemory HlslBytecode, uint ConstantBufferCount, uint BoundResourceCount, uint InstructionCount, diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs index df51cd07..3e3947d2 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs @@ -18,6 +18,7 @@ public partial class D2D1ReflectionServicesTests Assert.IsFalse(shaderInfo.RequiresDoublePrecisionSupport); Assert.AreEqual(D3DFeatureLevel.FeatureLevel10_1, shaderInfo.MinimumFeatureLevel); Assert.IsNotNull(shaderInfo.CompilerVersion); + Assert.AreEqual(""" // ================================================ // AUTO GENERATED @@ -52,6 +53,8 @@ public partial class D2D1ReflectionServicesTests return input0 + input1 + input2 + value3 + value4; } """.Replace("\r\n", "\n"), shaderInfo.HlslSource); + + CollectionAssert.AreEqual(D2D1PixelShader.LoadBytecode().ToArray(), shaderInfo.HlslBytecode.ToArray()); } [D2DInputCount(3)] @@ -98,6 +101,7 @@ public partial class D2D1ReflectionServicesTests Assert.IsTrue(shaderInfo.RequiresDoublePrecisionSupport); Assert.AreEqual(D3DFeatureLevel.FeatureLevel11_0, shaderInfo.MinimumFeatureLevel); Assert.IsNotNull(shaderInfo.CompilerVersion); + Assert.AreEqual(""" // ================================================ // AUTO GENERATED @@ -116,6 +120,8 @@ public partial class D2D1ReflectionServicesTests return (float4)(D2DGetInput(0) + (double4)amount); } """.Replace("\r\n", "\n"), shaderInfo.HlslSource); + + CollectionAssert.AreEqual(D2D1PixelShader.LoadBytecode().ToArray(), shaderInfo.HlslBytecode.ToArray()); } [D2DInputCount(1)]