diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 09697080..9f52daa2 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -78,7 +78,6 @@ - diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1BytecodeLoader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1BytecodeLoader.cs deleted file mode 100644 index 3d4ccfa6..00000000 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1BytecodeLoader.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.ComponentModel; - -namespace ComputeSharp.D2D1.__Internals; - -/// -/// A base representing a bytecode loader for a D2D1 shader. -/// -[EditorBrowsable(EditorBrowsableState.Never)] -[Obsolete("This interface is not intended to be used directly by user code")] -public interface ID2D1BytecodeLoader -{ - /// - /// Loads a dynamic shader bytecode. - /// - /// An opaque handle to the shader bytecode. - /// Thrown if the shader has already been initialized or a precompiled bytecode was requested when not available. - void LoadDynamicBytecode(IntPtr handle); - - /// - /// Loads the embedded shader bytecode for the shader. - /// - /// The shader bytecode. - /// Thrown if the shader has already been initialized. - void LoadEmbeddedBytecode(ReadOnlySpan bytecode); -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 60f63619..ae24b737 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.CompilerServices; -using ComputeSharp.D2D1.Shaders.Interop.Buffers; using ComputeSharp.D2D1.Shaders.Loaders; +using ComputeSharp.D2D1.Shaders.Translation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -191,6 +191,40 @@ public static class D2D1PixelShader return LoadOrCompileBytecode(shaderProfile, compileOptions | D2D1CompileOptions.PackMatrixRowMajor, out _, out _); } + /// + /// Compiles the bytecode from an input D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to load the bytecode for. + /// The requested shader profile to use to get the shader bytecode. + /// The requested compile options to use to get the shader bytecode. + /// The effective shader profile that was used to get the shader bytecode. + /// The effective compile options that were used to get the shader bytecode. + /// A instance with the resulting shader bytecode. + [MethodImpl(MethodImplOptions.NoInlining)] + private static unsafe ReadOnlyMemory CompileBytecode( + D2D1ShaderProfile? requestedShaderProfile, + D2D1CompileOptions? requestedCompileOptions, + out D2D1ShaderProfile effectiveShaderProfile, + out D2D1CompileOptions effectiveCompileOptions) + where T : unmanaged, ID2D1PixelShader + { + Unsafe.SkipInit(out T shader); + + string hlslSource = shader.HlslSource; + + // Set the effective profile and option to the requested ones or the default values + effectiveShaderProfile = requestedShaderProfile ?? shader.ShaderProfile; + effectiveCompileOptions = requestedCompileOptions ?? shader.CompileOptions; + + // Compile the shader with the current settings + using ComPtr dynamicBytecode = D3DCompiler.Compile(hlslSource.AsSpan(), effectiveShaderProfile, effectiveCompileOptions); + + byte* bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer(); + int bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize(); + + return new ReadOnlySpan(bytecodePtr, bytecodeSize).ToArray(); + } + /// /// Loads or compiles the bytecode from an input D2D1 pixel shader. /// @@ -208,24 +242,28 @@ public static class D2D1PixelShader out D2D1CompileOptions effectiveCompileOptions) where T : unmanaged, ID2D1PixelShader { - D2D1ShaderBytecodeLoader bytecodeLoader = default; - Unsafe.SkipInit(out T shader); - shader.LoadBytecode(ref bytecodeLoader, ref requestedShaderProfile, ref requestedCompileOptions); - - effectiveShaderProfile = requestedShaderProfile.GetValueOrDefault(); - effectiveCompileOptions = requestedCompileOptions.GetValueOrDefault(); - - using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode); - - // If a precompiled shader is available, return it - if (!precompiledBytecode.IsEmpty) + // Check whether there is precompiled bytecode we can use + if (shader.HlslBytecode is ReadOnlyMemory { Length: > 0 } hlslBytecode && + (requestedShaderProfile is null || shader.ShaderProfile == requestedShaderProfile.GetValueOrDefault()) && + (requestedCompileOptions is null || shader.CompileOptions == requestedCompileOptions.GetValueOrDefault())) { - return new PinnedBufferMemoryManager(precompiledBytecode).Memory; + effectiveShaderProfile = shader.ShaderProfile; + effectiveCompileOptions = shader.CompileOptions; + + return hlslBytecode; } - // Otherwise, return the dynamic shader instead + string hlslSource = shader.HlslSource; + + // Set the effective profile and option to the requested ones or the default values + effectiveShaderProfile = requestedShaderProfile ?? shader.ShaderProfile; + effectiveCompileOptions = requestedCompileOptions ?? shader.CompileOptions; + + // Compile the shader with the current settings + using ComPtr dynamicBytecode = D3DCompiler.Compile(hlslSource.AsSpan(), effectiveShaderProfile, effectiveCompileOptions); + byte* bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer(); int bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize(); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs index 3ad440b7..8be0483b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs @@ -1,8 +1,7 @@ using System; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using ComputeSharp.D2D1.Extensions; -using ComputeSharp.D2D1.Shaders.Loaders; +using ComputeSharp.D2D1.Shaders.Translation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -35,37 +34,35 @@ public static class D2D1ReflectionServices string hlslSource = shader.HlslSource; - D2D1ShaderBytecodeLoader bytecodeLoader = default; + using ComPtr d3D11ShaderReflection = default; - D2D1ShaderProfile? shaderProfile = null; - D2D1CompileOptions? compileOptions = null; - - shader.LoadBytecode(ref bytecodeLoader, ref shaderProfile, ref compileOptions); - - using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode); - - byte* bytecodePtr; - int bytecodeSize; - - if (!precompiledBytecode.IsEmpty) + // If the shader already has embedded bytecode, use that directly + if (shader.HlslBytecode is ReadOnlyMemory { Length: > 0 } hlslBytecode) { - bytecodePtr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(precompiledBytecode)); - bytecodeSize = precompiledBytecode.Length; + fixed (byte* hlslBytecodePtr = hlslBytecode.Span) + { + DirectX.D3DReflect( + pSrcData: hlslBytecodePtr, + SrcDataSize: (nuint)hlslBytecode.Length, + pInterface: Windows.__uuidof(), + ppReflector: d3D11ShaderReflection.GetVoidAddressOf()).Assert(); + } } else { - bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer(); - bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize(); + // 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(), + pInterface: Windows.__uuidof(), + ppReflector: d3D11ShaderReflection.GetVoidAddressOf()).Assert(); } - using ComPtr d3D11ShaderReflection = default; - - DirectX.D3DReflect( - pSrcData: bytecodePtr, - SrcDataSize: (nuint)bytecodeSize, - pInterface: Windows.__uuidof(), - ppReflector: d3D11ShaderReflection.GetVoidAddressOf()).Assert(); - D3D11_SHADER_DESC d3D11ShaderDescription; d3D11ShaderReflection.Get()->GetDesc(&d3D11ShaderDescription).Assert(); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs index 9e635129..da2188c1 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs @@ -74,7 +74,7 @@ public static class D2D1ShaderCompiler options &= ~D2D1CompileOptions.EnableLinking; // Compile the standalone D2D1 full shader - using ComPtr d3DBlobFullShader = D3DCompiler.CompileShader( + using ComPtr d3DBlobFullShader = D3DCompiler.Compile( source: hlslSourceAscii, macro: D3DCompiler.ASCII.D2D_FULL_SHADER, d2DEntry: entryPointAscii, @@ -91,7 +91,7 @@ public static class D2D1ShaderCompiler } // Compile the export function - using ComPtr d3DBlobFunction = D3DCompiler.CompileShader( + using ComPtr d3DBlobFunction = D3DCompiler.Compile( source: hlslSourceAscii, macro: D3DCompiler.ASCII.D2D_FUNCTION, d2DEntry: entryPointAscii, diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs deleted file mode 100644 index 26592bff..00000000 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using ComputeSharp.D2D1.__Internals; -using ComputeSharp.D2D1.Extensions; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; - -#pragma warning disable CS0618 - -namespace ComputeSharp.D2D1.Shaders.Loaders; - -/// -/// A bytecode loader for D2D1 shaders. -/// -internal unsafe struct D2D1ShaderBytecodeLoader : ID2D1BytecodeLoader -{ - /// - /// A pointer to the precompiled shader, if available. - /// - private byte* embeddedBytecodePtr; - - /// - /// The length of the precompiled shader buffer, if available. - /// - private int embeddedBytecodeSize; - - /// - /// The dynamically compiled shader bytecode blob, if available. - /// - private ComPtr d3DBlob; - - /// - /// Gets the resulting shader bytecode that was available. - /// - /// The precompiled bytecode, if available. - /// An with the shader bytecode, if dynamically compiled. - /// Either is empty, or the returned pointer is . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly ComPtr GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode) - { - precompiledBytecode = new ReadOnlySpan(this.embeddedBytecodePtr, this.embeddedBytecodeSize); - - return this.d3DBlob.Move(); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - unsafe void ID2D1BytecodeLoader.LoadDynamicBytecode(IntPtr handle) - { - default(InvalidOperationException).ThrowIf(this.embeddedBytecodePtr is not null); - default(InvalidOperationException).ThrowIf(this.d3DBlob.Get() is not null); - default(NotSupportedException).ThrowIf(handle == IntPtr.Zero); - - this.d3DBlob = new ComPtr((ID3DBlob*)handle); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - void ID2D1BytecodeLoader.LoadEmbeddedBytecode(ReadOnlySpan bytecode) - { - default(InvalidOperationException).ThrowIf(this.embeddedBytecodePtr is not null); - default(InvalidOperationException).ThrowIf(this.d3DBlob.Get() is not null); - - this.embeddedBytecodePtr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(bytecode)); - this.embeddedBytecodeSize = bytecode.Length; - } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs index 8038b1a7..3159873f 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs @@ -9,8 +9,6 @@ using ComputeSharp.D2D1.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; -#pragma warning disable IDE1006 - namespace ComputeSharp.D2D1.Shaders.Translation; /// @@ -39,7 +37,7 @@ internal static unsafe partial class D3DCompiler try { // Compile the standalone D2D1 full shader - using ComPtr d3DBlobFullShader = CompileShader( + using ComPtr d3DBlobFullShader = Compile( source: buffer.AsSpan(0, writtenBytes), macro: ASCII.D2D_FULL_SHADER, d2DEntry: ASCII.Execute, @@ -53,7 +51,7 @@ internal static unsafe partial class D3DCompiler } // Compile the export function - using ComPtr d3DBlobFunction = CompileShader( + using ComPtr d3DBlobFunction = Compile( source: buffer.AsSpan(0, writtenBytes), macro: ASCII.D2D_FUNCTION, d2DEntry: ASCII.Execute, @@ -73,7 +71,7 @@ internal static unsafe partial class D3DCompiler } /// - /// Compiles a D2D1 pixel shader with the specified parameters. + /// Compiles an HLSL shader with the specified parameters. /// /// The HLSL source code to compile (in ASCII). /// The target macro to define for the shader (either D2D_FULL_SHADER or D2D_FUNCTION). @@ -83,7 +81,7 @@ internal static unsafe partial class D3DCompiler /// The compilationm flag to use. /// The bytecode for the compiled shader. /// Thrown if the compilation fails. - public static ComPtr CompileShader( + public static ComPtr Compile( ReadOnlySpan source, ReadOnlySpan macro, ReadOnlySpan d2DEntry, diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs deleted file mode 100644 index 95414a68..00000000 --- a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.ComponentModel; -using System.Runtime.CompilerServices; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; - -namespace ComputeSharp.D2D1.__Internals; - -/// -/// A that uses the DXC APIs to compile compute shaders. -/// -[EditorBrowsable(EditorBrowsableState.Never)] -[Obsolete("This type is not intended to be used directly by user code")] -public static class D2D1ShaderCompiler -{ - /// - /// Compiles a new HLSL shader from the input source code. - /// - /// The type of bytecode loader being used. - /// The type of D2D1 shader being dispatched. - /// The instance to use to load the bytecode. - /// The input instance representing the D2D1 shader to run. - /// The shader profile to use to compile the shader. - /// The compiler options to use to compile the shader. - public static unsafe void LoadDynamicBytecode(ref TLoader loader, in T shader, D2D1ShaderProfile shaderProfile, D2D1CompileOptions options) - where TLoader : struct, ID2D1BytecodeLoader - where T : struct, ID2D1Shader - { - string hlslSource = Unsafe.AsRef(in shader).HlslSource; - - using ComPtr d3DBlobBytecode = Shaders.Translation.D3DCompiler.Compile( - hlslSource.AsSpan(), - shaderProfile, - options); - - loader.LoadDynamicBytecode((IntPtr)d3DBlobBytecode.Get()); - } -} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index 689e3890..74cf6ffa 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -273,6 +273,26 @@ public partial class D2D1PixelShaderTests } } + [TestMethod] + public unsafe void GetBytecode_FromDynamicBytecode() + { + ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); + + Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? manager)); + Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out ArraySegment segment)); + Assert.AreEqual(0, segment.Offset); + Assert.IsTrue(segment.Count > 0); + } + + [D2DInputCount(0)] + private readonly partial struct ShaderWithoutEmbeddedBytecode : ID2D1PixelShader + { + public float4 Execute() + { + return 0; + } + } + [TestMethod] public unsafe void GetBytecode_FromEmbeddedBytecode() { @@ -280,25 +300,41 @@ public partial class D2D1PixelShaderTests ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? manager)); - Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); // Matching shader profile bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); - Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); // Matching compile options bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); - Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); // Matching shader profile and compile options bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); - Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); + + // Incorrect profile + bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader50); + + Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out ArraySegment segment)); + Assert.AreEqual(0, segment.Offset); + Assert.IsTrue(segment.Count > 0); + + // Incorrect options + bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.EnableStrictness); + + Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out segment)); + Assert.AreEqual(0, segment.Offset); + Assert.IsTrue(segment.Count > 0); } [TestMethod] @@ -340,7 +376,7 @@ public partial class D2D1PixelShaderTests ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? manager)); - Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); // Bytecode with all output parameters ReadOnlyMemory bytecode2 = D2D1PixelShader.LoadBytecode(out D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions); @@ -354,13 +390,13 @@ public partial class D2D1PixelShaderTests bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); - Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); // Matching shader profile and compile options bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); - Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); } [D2DInputCount(3)]