diff --git a/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt index f4fdf2c2..a1c61091 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt +++ b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt @@ -1,5 +1,6 @@ D3DCompile D3DSetBlobPart +D3DStripShader D2D1_BUFFER_PRECISION D2D1_CHANNEL_DEPTH D2D1_FILTER @@ -19,6 +20,7 @@ D3DCOMPILE_OPTIMIZATION_LEVEL1 D3DCOMPILE_OPTIMIZATION_LEVEL2 D3DCOMPILE_OPTIMIZATION_LEVEL3 D3DCOMPILE_WARNINGS_ARE_ERRORS +D3DCOMPILER_STRIP_FLAGS IUnknown S_OK S_FALSE \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs index 9480ecee..8a8400b8 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs @@ -65,11 +65,13 @@ public static class D2D1ShaderCompiler D2D1ShaderProfile shaderProfile, D2D1CompileOptions options) { - // Check linking support + // Check the additional compile options (not provided by FXC directly) bool enableLinking = (options & D2D1CompileOptions.EnableLinking) == D2D1CompileOptions.EnableLinking; + bool stripReflectionData = (options & D2D1CompileOptions.StripReflectionData) == D2D1CompileOptions.StripReflectionData; - // Remove the linking flag to make the options blittable to flags + // Remove the additional options to make them blittable to flags options &= ~D2D1CompileOptions.EnableLinking; + options &= ~D2D1CompileOptions.StripReflectionData; // Compile the standalone D2D1 full shader using ComPtr d3DBlobFullShader = D3DCompiler.Compile( @@ -78,7 +80,8 @@ public static class D2D1ShaderCompiler d2DEntry: entryPointAscii, entryPoint: entryPointAscii, target: ASCII.GetPixelShaderProfile(shaderProfile), - flags: (uint)options); + flags: (uint)options, + stripReflectionData: stripReflectionData); if (!enableLinking) { @@ -95,7 +98,8 @@ public static class D2D1ShaderCompiler d2DEntry: entryPointAscii, entryPoint: default, target: ASCII.GetLibraryProfile(shaderProfile), - flags: (uint)options); + flags: (uint)options, + stripReflectionData: stripReflectionData); // Embed it as private data if requested using ComPtr d3DBlobLinked = D3DCompiler.SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get()); diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs index e550bad5..f1ab48c0 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs @@ -50,6 +50,12 @@ internal static unsafe partial class D3DCompiler writtenBytes = Encoding.ASCII.GetBytes(source, buffer); #endif + bool enableLinking = (options & D2D1CompileOptions.EnableLinking) == D2D1CompileOptions.EnableLinking; + bool stripReflectionData = (options & D2D1CompileOptions.StripReflectionData) == D2D1CompileOptions.StripReflectionData; + + options &= ~D2D1CompileOptions.EnableLinking; + options &= ~D2D1CompileOptions.StripReflectionData; + try { // Compile the standalone D2D1 full shader @@ -59,9 +65,10 @@ internal static unsafe partial class D3DCompiler d2DEntry: ASCII.Execute, entryPoint: ASCII.Execute, target: ASCII.GetPixelShaderProfile(shaderProfile), - flags: (uint)(options & ~D2D1CompileOptions.EnableLinking)); + flags: (uint)options, + stripReflectionData: stripReflectionData); - if ((options & D2D1CompileOptions.EnableLinking) == 0) + if (!enableLinking) { return d3DBlobFullShader.Move(); } @@ -73,7 +80,8 @@ internal static unsafe partial class D3DCompiler d2DEntry: ASCII.Execute, entryPoint: default, target: ASCII.GetLibraryProfile(shaderProfile), - flags: (uint)(options & ~D2D1CompileOptions.EnableLinking)); + flags: (uint)options, + stripReflectionData: stripReflectionData); // Embed it as private data if requested using ComPtr d3DBlobLinked = SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get()); @@ -95,6 +103,7 @@ internal static unsafe partial class D3DCompiler /// The entry point for the shader. /// The shader target to use to compile the input source. /// The compilationm flag to use. + /// Whether to strip reflection data from the shader bytecode. /// The bytecode for the compiled shader. /// Thrown if the compilation fails. public static ComPtr Compile( @@ -103,7 +112,8 @@ internal static unsafe partial class D3DCompiler ReadOnlySpan d2DEntry, ReadOnlySpan entryPoint, ReadOnlySpan target, - uint flags) + uint flags, + bool stripReflectionData) { using ComPtr d3DBlobBytecode = default; using ComPtr d3DBlobErrors = default; @@ -181,6 +191,20 @@ internal static unsafe partial class D3DCompiler // there would always be a message), but better safe than sorry. hResult.Assert(); + // If requested, strip the reflection data and return that bytecode blob + if (stripReflectionData) + { + using ComPtr d3DBlobBytecodeNoReflection = default; + + DirectX.D3DStripShader( + pShaderBytecode: d3DBlobBytecode.Get()->GetBufferPointer(), + BytecodeLength: d3DBlobBytecode.Get()->GetBufferSize(), + uStripFlags: (uint)D3DCOMPILER_STRIP_FLAGS.D3DCOMPILER_STRIP_REFLECTION_DATA, + ppStrippedBlob: d3DBlobBytecodeNoReflection.GetAddressOf()).Assert(); + + return d3DBlobBytecodeNoReflection.Move(); + } + return d3DBlobBytecode.Move(); }