Add initial prototype to build a D2D1 pixel shader

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:03:12 +02:00
1 親 beeb6882b4
コミット a119df7658
4ファイル変更
+106

検索条件に一致するファイルはありません

+4
ファイルの表示
@@ -27,6 +27,10 @@ bld/
[Oo]bj/
[Ll]og/
# Include the bundled libraries
!**/Libraries/x64/
!**/Libraries/arm64/
# Debug view types
!**/Resources/Debug
+4
ファイルの表示
@@ -36,6 +36,10 @@
Link="dxil.dll"
Visible="False"
CopyToOutputDirectory="PreserveNewest" />
<None Include="Libraries\x64\d3dcompiler_47.dll"
Link="d3dcompiler_47.dll"
Visible="False"
CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</When>
<When Condition="'$(Platform)' == 'ARM64'">
バイナリファイルは表示されません。
+98
ファイルの表示
@@ -1,5 +1,8 @@
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Exceptions;
using TerraFX.Interop.DirectX;
@@ -98,6 +101,88 @@ internal sealed unsafe partial class ShaderCompiler
}
}
#if NET6_0_OR_GREATER
public static ComPtr<ID3DBlob> CompilePixelShader(ReadOnlySpan<char> source)
{
int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length);
byte[] buffer = ArrayPool<byte>.Shared.Rent(maxLength);
int writtenBytes = Encoding.ASCII.GetBytes(source, buffer);
using ComPtr<ID3DBlob> d3DBlobBytecode = default;
using ComPtr<ID3DBlob> d3DBlobErrors = default;
fixed (byte* bufferPtr = buffer)
{
// D2D_FULL_SHADER
ReadOnlySpan<byte> d2DFullShaderName = new[]
{
(byte)'D', (byte)'2', (byte)'D', (byte)'_',
(byte)'F', (byte)'U', (byte)'L', (byte)'L', (byte)'_',
(byte)'S', (byte)'H', (byte)'A', (byte)'D', (byte)'E', (byte)'R', (byte)'\0'
};
// (Empty)
ReadOnlySpan<byte> d2DFullShaderValue = new[] { (byte)'\0' };
// D2D_ENTRY
ReadOnlySpan<byte> d2DEntryName = new[]
{
(byte)'D', (byte)'2', (byte)'D', (byte)'_',
(byte)'E', (byte)'N', (byte)'T', (byte)'R', (byte)'Y', (byte)'\0'
};
// Execute
ReadOnlySpan<byte> 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(d2DFullShaderName)),
Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DFullShaderValue))
},
new()
{
Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DEntryName)),
Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DEntryValue))
},
new()
};
// ps_4_0
ReadOnlySpan<byte> shaderProfile = new[]
{
(byte)'p', (byte)'s', (byte)'_', (byte)'5', (byte)'_', (byte)'0', (byte)'\0'
};
DirectX.D3DCompile(
pSrcData: bufferPtr,
SrcDataSize: (nuint)writtenBytes,
pSourceName: null,
pDefines: macros,
pInclude: null,
pEntrypoint: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DEntryValue)),
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()).Assert();
}
ArrayPool<byte>.Shared.Return(buffer);
if (d3DBlobErrors.Get() is not null)
{
ThrowHslsCompilationException(d3DBlobErrors.Get());
}
return d3DBlobBytecode.Move();
}
#endif
/// <summary>
/// Compiles a new HLSL shader from the input source code.
/// </summary>
@@ -156,6 +241,19 @@ internal sealed unsafe partial class ShaderCompiler
return ThrowHslsCompilationException(dxcOperationResult);
}
/// <summary>
/// Throws an exception when a shader compilation fails.
/// </summary>
/// <param name="d3DOperationResult">The input (faulting) operation.</param>
/// <returns>This method always throws and never actually returs.</returns>
[MethodImpl(MethodImplOptions.NoInlining)]
private static ComPtr<IDxcBlob> ThrowHslsCompilationException(ID3DBlob* d3DOperationResult)
{
string message = new((sbyte*)d3DOperationResult->GetBufferPointer());
throw new HlslCompilationException(message);
}
/// <summary>
/// Throws an exception when a shader compilation fails.
/// </summary>