Update library to use new HLSL bytecode properties

oops
このコミットが含まれているのは:
Sergio Pedri committed 2023-09-28 16:11:46 +02:00
1 親 ed24a3e4dc
コミット 55de6a88c5
9ファイル変更
+124 -188

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

-1
ファイルの表示
@@ -78,7 +78,6 @@
<Compile Include="..\ComputeSharp.D2D1\Extensions\ComPtrExtensions.cs" Link="ComputeSharp.D2D1\Extensions\ComPtrExtensions.cs" />
<Compile Include="..\ComputeSharp.D2D1\Extensions\HRESULTExtensions.cs" Link="ComputeSharp.D2D1\Extensions\HRESULTExtensions.cs" />
<Compile Include="..\ComputeSharp.D2D1\Interfaces\ID2D1PixelShader.cs" Link="ComputeSharp.D2D1\Interfaces\ID2D1PixelShader.cs" />
<Compile Include="..\ComputeSharp.D2D1\Interfaces\__Internals\ID2D1BytecodeLoader.cs" Link="ComputeSharp.D2D1\Interfaces\__Internals\ID2D1BytecodeLoader.cs" />
<Compile Include="..\ComputeSharp.D2D1\Interfaces\__Internals\ID2D1DispatchDataLoader.cs" Link="ComputeSharp.D2D1\Interfaces\__Internals\ID2D1DispatchDataLoader.cs" />
<Compile Include="..\ComputeSharp.D2D1\Interfaces\__Internals\ID2D1Shader.cs" Link="ComputeSharp.D2D1\Interfaces\__Internals\ID2D1Shader.cs" />
<Compile Include="..\ComputeSharp.D2D1\Intrinsics\D2D.cs" Link="ComputeSharp.D2D1\Intrinsics\D2D.cs" />
-26
ファイルの表示
@@ -1,26 +0,0 @@
using System;
using System.ComponentModel;
namespace ComputeSharp.D2D1.__Internals;
/// <summary>
/// A base <see langword="interface"/> representing a bytecode loader for a D2D1 shader.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This interface is not intended to be used directly by user code")]
public interface ID2D1BytecodeLoader
{
/// <summary>
/// Loads a dynamic shader bytecode.
/// </summary>
/// <param name="handle">An opaque handle to the shader bytecode.</param>
/// <exception cref="InvalidOperationException">Thrown if the shader has already been initialized or a precompiled bytecode was requested when not available.</exception>
void LoadDynamicBytecode(IntPtr handle);
/// <summary>
/// Loads the embedded shader bytecode for the shader.
/// </summary>
/// <param name="bytecode">The shader bytecode.</param>
/// <exception cref="InvalidOperationException">Thrown if the shader has already been initialized.</exception>
void LoadEmbeddedBytecode(ReadOnlySpan<byte> bytecode);
}
+52 -14
ファイルの表示
@@ -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<T>(shaderProfile, compileOptions | D2D1CompileOptions.PackMatrixRowMajor, out _, out _);
}
/// <summary>
/// Compiles the bytecode from an input D2D1 pixel shader.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to load the bytecode for.</typeparam>
/// <param name="requestedShaderProfile">The requested shader profile to use to get the shader bytecode.</param>
/// <param name="requestedCompileOptions">The requested compile options to use to get the shader bytecode.</param>
/// <param name="effectiveShaderProfile">The effective shader profile that was used to get the shader bytecode.</param>
/// <param name="effectiveCompileOptions">The effective compile options that were used to get the shader bytecode.</param>
/// <returns>A <see cref="ReadOnlyMemory{T}"/> instance with the resulting shader bytecode.</returns>
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe ReadOnlyMemory<byte> CompileBytecode<T>(
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<ID3DBlob> dynamicBytecode = D3DCompiler.Compile(hlslSource.AsSpan(), effectiveShaderProfile, effectiveCompileOptions);
byte* bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer();
int bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize();
return new ReadOnlySpan<byte>(bytecodePtr, bytecodeSize).ToArray();
}
/// <summary>
/// Loads or compiles the bytecode from an input D2D1 pixel shader.
/// </summary>
@@ -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<ID3DBlob> dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan<byte> 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<byte> { 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<ID3DBlob> dynamicBytecode = D3DCompiler.Compile(hlslSource.AsSpan(), effectiveShaderProfile, effectiveCompileOptions);
byte* bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer();
int bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize();
+23 -26
ファイルの表示
@@ -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<ID3D11ShaderReflection> d3D11ShaderReflection = default;
D2D1ShaderProfile? shaderProfile = null;
D2D1CompileOptions? compileOptions = null;
shader.LoadBytecode(ref bytecodeLoader, ref shaderProfile, ref compileOptions);
using ComPtr<ID3DBlob> dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan<byte> precompiledBytecode);
byte* bytecodePtr;
int bytecodeSize;
if (!precompiledBytecode.IsEmpty)
// If the shader already has embedded bytecode, use that directly
if (shader.HlslBytecode is ReadOnlyMemory<byte> { 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<ID3D11ShaderReflection>(),
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<byte> object to throw away immediately, as we don't
// actually need to keep the bytecode for later.
using ComPtr<ID3DBlob> dynamicBytecode = D3DCompiler.Compile(hlslSource.AsSpan(), shader.ShaderProfile, shader.CompileOptions);
DirectX.D3DReflect(
pSrcData: (byte*)dynamicBytecode.Get()->GetBufferPointer(),
SrcDataSize: dynamicBytecode.Get()->GetBufferSize(),
pInterface: Windows.__uuidof<ID3D11ShaderReflection>(),
ppReflector: d3D11ShaderReflection.GetVoidAddressOf()).Assert();
}
using ComPtr<ID3D11ShaderReflection> d3D11ShaderReflection = default;
DirectX.D3DReflect(
pSrcData: bytecodePtr,
SrcDataSize: (nuint)bytecodeSize,
pInterface: Windows.__uuidof<ID3D11ShaderReflection>(),
ppReflector: d3D11ShaderReflection.GetVoidAddressOf()).Assert();
D3D11_SHADER_DESC d3D11ShaderDescription;
d3D11ShaderReflection.Get()->GetDesc(&d3D11ShaderDescription).Assert();
+2 -2
ファイルの表示
@@ -74,7 +74,7 @@ public static class D2D1ShaderCompiler
options &= ~D2D1CompileOptions.EnableLinking;
// Compile the standalone D2D1 full shader
using ComPtr<ID3DBlob> d3DBlobFullShader = D3DCompiler.CompileShader(
using ComPtr<ID3DBlob> 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<ID3DBlob> d3DBlobFunction = D3DCompiler.CompileShader(
using ComPtr<ID3DBlob> d3DBlobFunction = D3DCompiler.Compile(
source: hlslSourceAscii,
macro: D3DCompiler.ASCII.D2D_FUNCTION,
d2DEntry: entryPointAscii,
-68
ファイルの表示
@@ -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;
/// <summary>
/// A bytecode loader for D2D1 shaders.
/// </summary>
internal unsafe struct D2D1ShaderBytecodeLoader : ID2D1BytecodeLoader
{
/// <summary>
/// A pointer to the precompiled shader, if available.
/// </summary>
private byte* embeddedBytecodePtr;
/// <summary>
/// The length of the precompiled shader buffer, if available.
/// </summary>
private int embeddedBytecodeSize;
/// <summary>
/// The dynamically compiled shader bytecode blob, if available.
/// </summary>
private ComPtr<ID3DBlob> d3DBlob;
/// <summary>
/// Gets the resulting shader bytecode that was available.
/// </summary>
/// <param name="precompiledBytecode">The precompiled bytecode, if available.</param>
/// <returns>An <see cref="ID3DBlob"/> with the shader bytecode, if dynamically compiled.</returns>
/// <remarks>Either <paramref name="precompiledBytecode"/> is empty, or the returned pointer is <see langword="null"/>.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ComPtr<ID3DBlob> GetResultingShaderBytecode(out ReadOnlySpan<byte> precompiledBytecode)
{
precompiledBytecode = new ReadOnlySpan<byte>(this.embeddedBytecodePtr, this.embeddedBytecodeSize);
return this.d3DBlob.Move();
}
/// <inheritdoc/>
[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>((ID3DBlob*)handle);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void ID2D1BytecodeLoader.LoadEmbeddedBytecode(ReadOnlySpan<byte> 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;
}
}
+4 -6
ファイルの表示
@@ -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;
/// <summary>
@@ -39,7 +37,7 @@ internal static unsafe partial class D3DCompiler
try
{
// Compile the standalone D2D1 full shader
using ComPtr<ID3DBlob> d3DBlobFullShader = CompileShader(
using ComPtr<ID3DBlob> 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<ID3DBlob> d3DBlobFunction = CompileShader(
using ComPtr<ID3DBlob> d3DBlobFunction = Compile(
source: buffer.AsSpan(0, writtenBytes),
macro: ASCII.D2D_FUNCTION,
d2DEntry: ASCII.Execute,
@@ -73,7 +71,7 @@ internal static unsafe partial class D3DCompiler
}
/// <summary>
/// Compiles a D2D1 pixel shader with the specified parameters.
/// Compiles an HLSL shader with the specified parameters.
/// </summary>
/// <param name="source">The HLSL source code to compile (in ASCII).</param>
/// <param name="macro">The target macro to define for the shader (either <c>D2D_FULL_SHADER</c> or <c>D2D_FUNCTION</c>).</param>
@@ -83,7 +81,7 @@ internal static unsafe partial class D3DCompiler
/// <param name="flags">The compilationm flag to use.</param>
/// <returns>The bytecode for the compiled shader.</returns>
/// <exception cref="FxcCompilationException">Thrown if the compilation fails.</exception>
public static ComPtr<ID3DBlob> CompileShader(
public static ComPtr<ID3DBlob> Compile(
ReadOnlySpan<byte> source,
ReadOnlySpan<byte> macro,
ReadOnlySpan<byte> d2DEntry,
-38
ファイルの表示
@@ -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;
/// <summary>
/// A <see langword="class"/> that uses the DXC APIs to compile compute shaders.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This type is not intended to be used directly by user code")]
public static class D2D1ShaderCompiler
{
/// <summary>
/// Compiles a new HLSL shader from the input source code.
/// </summary>
/// <typeparam name="TLoader">The type of bytecode loader being used.</typeparam>
/// <typeparam name="T">The type of D2D1 shader being dispatched.</typeparam>
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the bytecode.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the D2D1 shader to run.</param>
/// <param name="shaderProfile">The shader profile to use to compile the shader.</param>
/// <param name="options">The compiler options to use to compile the shader.</param>
public static unsafe void LoadDynamicBytecode<TLoader, T>(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<ID3DBlob> d3DBlobBytecode = Shaders.Translation.D3DCompiler.Compile(
hlslSource.AsSpan(),
shaderProfile,
options);
loader.LoadDynamicBytecode((IntPtr)d3DBlobBytecode.Get());
}
}
+43 -7
ファイルの表示
@@ -273,6 +273,26 @@ public partial class D2D1PixelShaderTests
}
}
[TestMethod]
public unsafe void GetBytecode_FromDynamicBytecode()
{
ReadOnlyMemory<byte> bytecode = D2D1PixelShader.LoadBytecode<ShaderWithoutEmbeddedBytecode>();
Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager<byte>? manager));
Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out ArraySegment<byte> 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<byte> bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecode>();
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager<byte>? manager));
Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name);
Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager"));
// Matching shader profile
bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecode>(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<ShaderWithEmbeddedBytecode>(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<ShaderWithEmbeddedBytecode>(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<ShaderWithEmbeddedBytecode>(D2D1ShaderProfile.PixelShader50);
Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out manager));
Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out ArraySegment<byte> segment));
Assert.AreEqual(0, segment.Offset);
Assert.IsTrue(segment.Count > 0);
// Incorrect options
bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecode>(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<byte> bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecodeAndCompileOptions>();
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager<byte>? manager));
Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name);
Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager"));
// Bytecode with all output parameters
ReadOnlyMemory<byte> bytecode2 = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecodeAndCompileOptions>(out D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions);
@@ -354,13 +390,13 @@ public partial class D2D1PixelShaderTests
bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecodeAndCompileOptions>(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<ShaderWithEmbeddedBytecodeAndCompileOptions>(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)]