diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs new file mode 100644 index 00000000..c1450498 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs @@ -0,0 +1,105 @@ +using System; +using System.Buffers; +using System.Text; +using ComputeSharp.D2D1.Exceptions; +#if !NET6_0_OR_GREATER +using ComputeSharp.D2D1.NetStandard.System.Text; +#endif +using ComputeSharp.D2D1.Shaders.Translation; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +namespace ComputeSharp.D2D1.Interop; + +/// +/// Provides methods to manually compile D2D1 shaders. +/// +public static class D2D1ShaderCompiler +{ + /// + /// Compiles a new HLSL shader from the input source code. + /// + /// The HLSL source code to compile. + /// The entry point of the shader being compiled. + /// The shader profile to use to compile the shader. + /// The compiler options to use to compile the shader. + /// The bytecode for the compiled shader. + /// Thrown if the compilation fails. + public static ReadOnlyMemory Compile( + ReadOnlySpan source, + ReadOnlySpan entryPoint, + D2D1ShaderProfile shaderProfile, + D2D1ShaderCompilerOptions options) + { + // Encode the HLSL source to ASCII + int maxSourceLength = Encoding.ASCII.GetMaxByteCount(source.Length); + byte[] sourceBuffer = ArrayPool.Shared.Rent(maxSourceLength); + int sourceWrittenBytes = Encoding.ASCII.GetBytes(source, sourceBuffer); + + // Encode the entry point to ASCII + int maxEntryPointLength = Encoding.ASCII.GetMaxByteCount(entryPoint.Length); + byte[] entryPointBuffer = ArrayPool.Shared.Rent(maxEntryPointLength); + int entryPointWrittenBytes = Encoding.ASCII.GetBytes(entryPoint, entryPointBuffer); + + ReadOnlySpan sourceAscii = sourceBuffer.AsSpan(0, sourceWrittenBytes); + ReadOnlySpan entryPointAscii = entryPointBuffer.AsSpan(0, entryPointWrittenBytes); + + return Compile(sourceAscii, entryPointAscii, shaderProfile, options); + } + + /// + /// Compiles a new HLSL shader from the input source code. + /// + /// The HLSL source code to compile (in ASCII). + /// The entry point of the shader being compiled (in ASCII). + /// The shader profile to use to compile the shader. + /// The compiler options to use to compile the shader. + /// The bytecode for the compiled shader. + /// Thrown if the compilation fails. + public static unsafe ReadOnlyMemory Compile( + ReadOnlySpan source, + ReadOnlySpan entryPoint, + D2D1ShaderProfile shaderProfile, + D2D1ShaderCompilerOptions options) + { + // Check linking support + bool enableLinking = (options & D2D1ShaderCompilerOptions.EnableLinking) == D2D1ShaderCompilerOptions.EnableLinking; + + // Remove the linking flag to make the options blittable to flags + options &= ~D2D1ShaderCompilerOptions.EnableLinking; + + // Compile the standalone D2D1 full shader + using ComPtr d3DBlobFullShader = D3DCompiler.CompileShader( + source: source, + macro: D3DCompiler.ASCII.D2D_FULL_SHADER, + d2DEntry: entryPoint, + entryPoint: entryPoint, + target: D3DCompiler.ASCII.GetPixelShaderProfile(shaderProfile), + flags: (uint)options); + + if (!enableLinking) + { + void* blobFullShaderPtr = d3DBlobFullShader.Get()->GetBufferPointer(); + nuint blobFullShaderSize = d3DBlobFullShader.Get()->GetBufferSize(); + + return new Span(blobFullShaderPtr, (int)blobFullShaderSize).ToArray(); + } + + // Compile the export function + using ComPtr d3DBlobFunction = D3DCompiler.CompileShader( + source: source, + macro: D3DCompiler.ASCII.D2D_FUNCTION, + d2DEntry: entryPoint, + entryPoint: default, + target: D3DCompiler.ASCII.GetLibraryProfile(shaderProfile), + flags: (uint)options); + + // Embed it as private data if requested + using ComPtr d3DBlobLinked = D3DCompiler.SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get()); + + void* blobLinkedPtr = d3DBlobLinked.Get()->GetBufferPointer(); + nuint blobLinkedSize = d3DBlobLinked.Get()->GetBufferSize(); + + return new Span(blobLinkedPtr, (int)blobLinkedSize).ToArray(); + } +} diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompilerOptions.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompilerOptions.cs new file mode 100644 index 00000000..a1cacc59 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompilerOptions.cs @@ -0,0 +1,172 @@ +using System; +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.D2D1.Interop; + +/// +/// Flags to control the options that can be used to compile a D2D1 shader. +/// +/// +/// For more info, see For more info, see . +/// +[Flags] +public enum D2D1ShaderCompilerOptions +{ + /// + /// Directs the compiler to insert debug file/line/type/symbol information into the output code. + /// + /// + /// This flag maps to D3DCOMPILE_DEBUG and /Zi. + /// + Debug = D3DCOMPILE.D3DCOMPILE_DEBUG, + + /// + /// Directs the compiler not to validate the generated code against known capabilities and constraints. + /// This option should only be used with shaders that have been successfully compiled in the past. + /// DirectX always validates shaders before it sets them to a device. + /// + /// + /// This flag maps to D3DCOMPILE_SKIP_VALIDATION and /Vd. + /// + SkipValidation = D3DCOMPILE.D3DCOMPILE_SKIP_VALIDATION, + + /// + /// Directs the compiler to skip optimization steps during code generation. + /// This option should only be used for debug purposes. + /// + /// + /// This flag maps to D3DCOMPILE_SKIP_OPTIMIZATION and /Od. + /// + SkipOptimization = D3DCOMPILE.D3DCOMPILE_SKIP_OPTIMIZATION, + + /// + /// Directs the compiler to pack matrices in row-major order on input and output from the shader. + /// + /// + /// This value is needed to have the constant buffer loading from generated shaders map to compiled shaders. + /// This flag maps to D3DCOMPILE_PACK_MATRIX_ROW_MAJOR and /Zpr. + /// + PackMatrixRowMajor = D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR, + + /// + /// Directs the compiler to pack matrices in column-major order on input and output from the shader. + /// This type of packing is generally more efficient because a series of dot-products can then + /// perform vector-matrix multiplication. + /// + /// + /// Using this flag makes shaders incompatible with the constant buffer loading code produced by ComputeSharp.D2D1. + /// That is, trying to load a constant buffer from a shader processed by the source generator is undefined behavior. + /// This flag maps to D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR and /Zpc. + /// + PackMatrixColumnMajor = D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR, + + /// + /// Directs the compiler to perform all computations with partial precision. + /// If this flag is set, the compiled code might run faster on some hardware. + /// + /// + /// This flag maps to D3DCOMPILE_PARTIAL_PRECISION and /Gpp. + /// + PartialPrecision = D3DCOMPILE.D3DCOMPILE_PARTIAL_PRECISION, + + /// + /// Directs the compiler to not use flow-control constructs where possible. + /// + /// + /// This flag maps to D3DCOMPILE_AVOID_FLOW_CONTROL and /Gfa. + /// + AvoidFlowControl = D3DCOMPILE.D3DCOMPILE_AVOID_FLOW_CONTROL, + + /// + /// Forces strict compile, which might not allow for legacy syntax. + /// By default, the compiler disables strictness on deprecated syntax. + /// + /// + /// This flag maps to D3DCOMPILE_ENABLE_STRICTNESS and /Ges. + /// + EnableStrictness = D3DCOMPILE.D3DCOMPILE_ENABLE_STRICTNESS, + + /// + /// Forces the IEEE strict compile which avoids optimizations that may break IEEE rules. + /// + /// + /// This flag maps to D3DCOMPILE_IEEE_STRICTNESS and /Gis. + /// + IeeeStrictness = D3DCOMPILE.D3DCOMPILE_IEEE_STRICTNESS, + + /// + /// Directs the compiler to enable older shaders to compile to 5_0 targets. + /// + /// + /// This flag maps to D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY and /Gec. + /// + EnableBackwardsCompatibility = D3DCOMPILE.D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY, + + /// + /// Directs the compiler to use the lowest optimization level. If this option is set, the compiler + /// might produce slower code but produces the code more quickly. This option should be set when + /// shaders are developed iteratively, as it can reduce compilation times. + /// + /// + /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL0 and /O0. + /// + OptimizationLevel0 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL0, + + /// + /// Directs the compiler to use the second lowest optimization level. + /// + /// + /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL1 and /O1. + /// + OptimizationLevel1 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL1, + + /// + /// Directs the compiler to use the second highest optimization level. + /// + /// + /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL2 and /O2. + /// + OptimizationLevel2 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL2, + + /// + /// Directs the compiler to use the highest optimization level. If this option is set, the compiler + /// produces the best possible code but might take significantly longer to do so. This option should + /// be set for final builds of an application when performance is the most important factor. + /// + /// + /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL3 and /O3. + /// + OptimizationLevel3 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3, + + /// + /// Directs the compiler to treat all warnings as errors when it compiles the shader code. This + /// option is recommended for new shader code, so that warnings can be resolved and the + /// number of hard-to-find code defects can be minimized. + /// + /// + /// This flag maps to D3DCOMPILE_WARNINGS_ARE_ERRORS and /WX. + /// + WarningsAreErrors = D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS, + + /// + /// This flag enables the support for shader linking. Specifically, when set, this flag causes the APIs from + /// to compile the input shader twice: once as a full D2D1 shader, and once + /// as an export function, which is then set as private blob data into the final bytecode that is returned. + /// + /// + /// For more info, see . + /// + EnableLinking = 1 << 31, + + /// + /// The default options for shaders compiled by ComputeSharp.D2D1. Specifically, this combination + /// of options is the one used by precompiled shaders (ie. when using ), + /// and when using or any of the overloads. + /// + /// + /// This option does not include , which is instead automatically enabled by shaders + /// compiled using any of the APIs mentioned above. When manually compiling shaders from source using the APIs + /// from instance, should be manually used when needed. + /// + Default = OptimizationLevel3 | WarningsAreErrors | PackMatrixRowMajor +} diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs index 21307121..e9d9617d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs @@ -32,9 +32,9 @@ internal static unsafe partial class D3DCompiler /// /// The HLSL source code to compile. /// The shader profile to use to compile the shader. - /// Whether to enable linking support for the shader. + /// Whether to enable linking for the shader. /// The bytecode for the compiled shader. - public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, bool enableLinkingSupport) + public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, bool enableLinking) { int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); byte[] buffer = ArrayPool.Shared.Rent(maxLength); @@ -51,7 +51,7 @@ internal static unsafe partial class D3DCompiler target: ASCII.GetPixelShaderProfile(shaderProfile), flags: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR); - if (!enableLinkingSupport) + if (!enableLinking) { return d3DBlobFullShader.Move(); } @@ -158,7 +158,7 @@ internal static unsafe partial class D3DCompiler /// The bytecode for the full shader. /// The bytecode for the shader function to export. /// An instance with the combined data of and . - private static ComPtr SetD3DPrivateData(ID3DBlob* shaderBlob, ID3DBlob* exportBlob) + public static ComPtr SetD3DPrivateData(ID3DBlob* shaderBlob, ID3DBlob* exportBlob) { void* shaderPtr = shaderBlob->GetBufferPointer(); nuint shaderSize = shaderBlob->GetBufferSize(); @@ -209,7 +209,7 @@ internal static unsafe partial class D3DCompiler /// /// A container for ASCII encoded, null-terminated constant strings. /// - private static class ASCII + public static class ASCII { /// /// Gets a with the "D2D_FUNCTION" ASCII text.