diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs index 5a5020f7..358a4a73 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs @@ -48,7 +48,7 @@ partial class ID2D1ShaderGenerator token.ThrowIfCancellationRequested(); // Compile the shader bytecode - using ComPtr dxcBlobBytecode = D2D1ShaderCompiler.Compile(hlslSource.AsSpan(), false); + using ComPtr dxcBlobBytecode = D2D1ShaderCompiler.Compile(hlslSource.AsSpan(), shaderProfile.Value, false); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.cs index 0450f179..ca0bc77c 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.cs @@ -30,12 +30,13 @@ internal static unsafe partial class D2D1ShaderCompiler /// Compiles a new HLSL shader from the input source code. /// /// The HLSL source code to compile. + /// The shader profile to use to compile the shader. /// Whether to enable linking support for the shader. /// The bytecode for the compiled shader. - public static ComPtr Compile(ReadOnlySpan source, bool enableLinkingSupport) + public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, bool enableLinkingSupport) { // Compile the standalone D2D1 full shader - using ComPtr d3DBlobFullShader = CompileD2DFullShader(source); + using ComPtr d3DBlobFullShader = CompileD2DFullShader(source, shaderProfile); if (!enableLinkingSupport) { @@ -43,7 +44,7 @@ internal static unsafe partial class D2D1ShaderCompiler } // Compile the export function and embed it as private data if requested - using ComPtr d3DBlobFunction = CompileD2DFunction(source); + using ComPtr d3DBlobFunction = CompileD2DFunction(source, shaderProfile); using ComPtr d3DBlobLinked = EmbedD2DFunctionPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get()); return d3DBlobLinked.Move(); @@ -52,9 +53,10 @@ internal static unsafe partial class D2D1ShaderCompiler /// /// Compiles a D2D1 pixel shader with D2D_FULL_SHADER. /// - ///The HLSL source code to compile. + /// The HLSL source code to compile. + /// The shader profile to use to compile the shader. /// The bytecode for the compiled shader. - private static ComPtr CompileD2DFullShader(ReadOnlySpan source) + private static ComPtr CompileD2DFullShader(ReadOnlySpan source, D2D1ShaderProfile shaderProfile) { // Encode the HLSL source to ASCII int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); @@ -95,7 +97,7 @@ internal static unsafe partial class D2D1ShaderCompiler pDefines: macros, pInclude: D3DIncludeForD2D1EffectHelpers, pEntrypoint: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.Execute)), - pTarget: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.ps_5_0)), + pTarget: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.GetPixelShaderProfile(shaderProfile))), Flags1: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS, Flags2: 0, ppCode: d3DBlobBytecode.GetAddressOf(), @@ -118,9 +120,10 @@ internal static unsafe partial class D2D1ShaderCompiler /// /// Compiles a D2D1 pixel shader with D2D_FUNCTION. /// - ///The HLSL source code to compile. + /// The HLSL source code to compile. + /// The shader profile to use to compile the shader. /// The bytecode for the compiled shader. - private static ComPtr CompileD2DFunction(ReadOnlySpan source) + private static ComPtr CompileD2DFunction(ReadOnlySpan source, D2D1ShaderProfile shaderProfile) { int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); byte[] buffer = ArrayPool.Shared.Rent(maxLength); @@ -160,7 +163,7 @@ internal static unsafe partial class D2D1ShaderCompiler pDefines: macros, pInclude: D3DIncludeForD2D1EffectHelpers, pEntrypoint: null, - pTarget: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.lib_5_0)), + pTarget: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.GetLibraryProfile(shaderProfile))), Flags1: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS, Flags2: 0, ppCode: d3DBlobBytecode.GetAddressOf(), @@ -183,8 +186,8 @@ internal static unsafe partial class D2D1ShaderCompiler /// /// Embeds the bytecode for an exported shader as private data into another shader bytecode. /// - /// The bytecode produced by . - /// The bytecode produced by . + /// The bytecode produced by . + /// The bytecode produced by . /// An instance with the combined data of and . private static ComPtr EmbedD2DFunctionPrivateData(ID3DBlob* shaderBlob, ID3DBlob* exportBlob) { @@ -252,14 +255,88 @@ internal static unsafe partial class D2D1ShaderCompiler /// public static ReadOnlySpan Execute => new[] { (byte)'E', (byte)'x', (byte)'e', (byte)'c', (byte)'u', (byte)'t', (byte)'e', (byte)'\0' }; + /// + /// Gets a with the "lib_4_0_level_9_1" ASCII text. + /// + private static ReadOnlySpan lib_4_0_level_9_1 => new[] { (byte)'l', (byte)'i', (byte)'b', (byte)'_', (byte)'4', (byte)'_', (byte)'0', (byte)'_', (byte)'l', (byte)'e', (byte)'v', (byte)'e', (byte)'l', (byte)'_', (byte)'9', (byte)'_', (byte)'1', (byte)'\0' }; + + /// + /// Gets a with the "lib_4_0_level_9_3" ASCII text. + /// + private static ReadOnlySpan lib_4_0_level_9_3 => new[] { (byte)'l', (byte)'i', (byte)'b', (byte)'_', (byte)'4', (byte)'_', (byte)'0', (byte)'_', (byte)'l', (byte)'e', (byte)'v', (byte)'e', (byte)'l', (byte)'_', (byte)'9', (byte)'_', (byte)'3', (byte)'\0' }; + + /// + /// Gets a with the "lib_4_0" ASCII text. + /// + private static ReadOnlySpan lib_4_0 => new[] { (byte)'l', (byte)'i', (byte)'b', (byte)'_', (byte)'4', (byte)'_', (byte)'0', (byte)'\0' }; + + /// + /// Gets a with the "lib_4_1" ASCII text. + /// + private static ReadOnlySpan lib_4_1 => new[] { (byte)'l', (byte)'i', (byte)'b', (byte)'_', (byte)'4', (byte)'_', (byte)'1', (byte)'\0' }; + /// /// Gets a with the "lib_5_0" ASCII text. /// - public static ReadOnlySpan lib_5_0 => new[] { (byte)'l', (byte)'i', (byte)'b', (byte)'_', (byte)'5', (byte)'_', (byte)'0', (byte)'\0' }; + private static ReadOnlySpan lib_5_0 => new[] { (byte)'l', (byte)'i', (byte)'b', (byte)'_', (byte)'5', (byte)'_', (byte)'0', (byte)'\0' }; + + /// + /// Gets a with the "ps_4_0_level_9_1" ASCII text. + /// + private static ReadOnlySpan ps_4_0_level_9_1 => new[] { (byte)'p', (byte)'s', (byte)'_', (byte)'4', (byte)'_', (byte)'0', (byte)'_', (byte)'l', (byte)'e', (byte)'v', (byte)'e', (byte)'l', (byte)'_', (byte)'9', (byte)'_', (byte)'1', (byte)'\0' }; + + /// + /// Gets a with the "ps_4_0_level_9_3" ASCII text. + /// + private static ReadOnlySpan ps_4_0_level_9_3 => new[] { (byte)'p', (byte)'s', (byte)'_', (byte)'4', (byte)'_', (byte)'0', (byte)'_', (byte)'l', (byte)'e', (byte)'v', (byte)'e', (byte)'l', (byte)'_', (byte)'9', (byte)'_', (byte)'3', (byte)'\0' }; + + /// + /// Gets a with the "ps_4_0" ASCII text. + /// + private static ReadOnlySpan ps_4_0 => new[] { (byte)'p', (byte)'s', (byte)'_', (byte)'4', (byte)'_', (byte)'0', (byte)'\0' }; + + /// + /// Gets a with the "ps_4_1" ASCII text. + /// + private static ReadOnlySpan ps_4_1 => new[] { (byte)'p', (byte)'s', (byte)'_', (byte)'4', (byte)'_', (byte)'1', (byte)'\0' }; /// /// Gets a with the "ps_5_0" ASCII text. /// - public static ReadOnlySpan ps_5_0 => new[] { (byte)'p', (byte)'s', (byte)'_', (byte)'5', (byte)'_', (byte)'0', (byte)'\0' }; + private static ReadOnlySpan ps_5_0 => new[] { (byte)'p', (byte)'s', (byte)'_', (byte)'5', (byte)'_', (byte)'0', (byte)'\0' }; + + /// + /// Gets a for a given shader profile, for a library. + /// + /// The input shaader profile to use. + /// A for a given shader profile, for a library. + public static ReadOnlySpan GetLibraryProfile(D2D1ShaderProfile shaderProfile) + { + return shaderProfile switch + { + D2D1ShaderProfile.PixelShader40Level91 => lib_4_0_level_9_1, + D2D1ShaderProfile.PixelShader40Level93 => lib_4_0_level_9_3, + D2D1ShaderProfile.PixelShader40 => lib_4_0, + D2D1ShaderProfile.PixelShader41 => lib_4_1, + _ => lib_5_0 + }; + } + + /// + /// Gets a for a given shader profile, for a full shader. + /// + /// The input shaader profile to use. + /// A for a given shader profile, for a full shader. + public static ReadOnlySpan GetPixelShaderProfile(D2D1ShaderProfile shaderProfile) + { + return shaderProfile switch + { + D2D1ShaderProfile.PixelShader40Level91 => ps_4_0_level_9_1, + D2D1ShaderProfile.PixelShader40Level93 => ps_4_0_level_9_3, + D2D1ShaderProfile.PixelShader40 => ps_4_0, + D2D1ShaderProfile.PixelShader41 => ps_4_1, + _ => ps_5_0 + }; + } } }