diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs index 26f439bc..31b529c9 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs @@ -102,6 +102,107 @@ internal sealed unsafe partial class ShaderCompiler } #if NET6_0_OR_GREATER + public static ComPtr SetPrivateData(ID3DBlob* shaderBlob, ID3DBlob* exportBlob) + { + void* shaderPtr = shaderBlob->GetBufferPointer(); + nuint shaderSize = shaderBlob->GetBufferSize(); + + void* exportPtr = exportBlob->GetBufferPointer(); + nuint exportSize = exportBlob->GetBufferSize(); + + using ComPtr resultBlob = default; + + DirectX.D3DSetBlobPart( + pSrcData: shaderPtr, + SrcDataSize: shaderSize, + Part: D3D_BLOB_PART.D3D_BLOB_PRIVATE_DATA, + Flags: 0, + pPart: exportPtr, + PartSize: exportSize, + ppNewShader: resultBlob.GetAddressOf()).Assert(); + + return resultBlob.Move(); + } + + public static ComPtr CompilePixelShaderFunction(ReadOnlySpan source) + { + int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); + byte[] buffer = ArrayPool.Shared.Rent(maxLength); + int writtenBytes = Encoding.ASCII.GetBytes(source, buffer); + + using ComPtr d3DBlobBytecode = default; + using ComPtr d3DBlobErrors = default; + + fixed (byte* bufferPtr = buffer) + { + // D2D_FUNCTION + ReadOnlySpan d2DFunctionName = new[] + { + (byte)'D', (byte)'2', (byte)'D', (byte)'_', + (byte)'F', (byte)'U', (byte)'N', (byte)'C', (byte)'T', (byte)'I', (byte)'O', (byte)'N', (byte)'\0' + }; + + // (Empty) + ReadOnlySpan d2DFunctionValue = new[] { (byte)'\0' }; + + // D2D_ENTRY + ReadOnlySpan d2DEntryName = new[] + { + (byte)'D', (byte)'2', (byte)'D', (byte)'_', + (byte)'E', (byte)'N', (byte)'T', (byte)'R', (byte)'Y', (byte)'\0' + }; + + // Execute + ReadOnlySpan d2DEntryValue = new[] + { + (byte)'E', (byte)'x', (byte)'e', (byte)'c', (byte)'u', (byte)'t', (byte)'e', (byte)'\0' + }; + + D3D_SHADER_MACRO* macros = stackalloc D3D_SHADER_MACRO[] + { + new() + { + Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DFunctionName)), + Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DFunctionValue)) + }, + new() + { + Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DEntryName)), + Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DEntryValue)) + }, + new() + }; + + // ps_4_0 + ReadOnlySpan shaderProfile = new[] + { + (byte)'l', (byte)'i', (byte)'b', (byte)'_', (byte)'5', (byte)'_', (byte)'0', (byte)'\0' + }; + + _ = DirectX.D3DCompile( + pSrcData: bufferPtr, + SrcDataSize: (nuint)writtenBytes, + pSourceName: null, + pDefines: macros, + pInclude: null, + pEntrypoint: null, + pTarget: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(shaderProfile)), + Flags1: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS, + Flags2: 0, + ppCode: d3DBlobBytecode.GetAddressOf(), + ppErrorMsgs: d3DBlobErrors.GetAddressOf()); + } + + ArrayPool.Shared.Return(buffer); + + if (d3DBlobErrors.Get() is not null) + { + ThrowHslsCompilationException(d3DBlobErrors.Get()); + } + + return d3DBlobBytecode.Move(); + } + public static ComPtr CompilePixelShader(ReadOnlySpan source) { int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); @@ -152,7 +253,7 @@ internal sealed unsafe partial class ShaderCompiler new() }; - // ps_4_0 + // ps_5_0 ReadOnlySpan shaderProfile = new[] { (byte)'p', (byte)'s', (byte)'_', (byte)'5', (byte)'_', (byte)'0', (byte)'\0'