Add LoadShaderBytecode API

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:03:13 +02:00
1 親 97bfa49d47
コミット 8ccf10a367
3ファイル変更
+174 -3

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

+78
ファイルの表示
@@ -0,0 +1,78 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.__Internals;
using ComputeSharp.Core.Extensions;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#pragma warning disable CS0618
namespace ComputeSharp.Shaders.Dispatching;
/// <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 ComPtr<ID3DBlob> GetResultingShaderBytecode(out ReadOnlySpan<byte> precompiledBytecode)
{
precompiledBytecode = new ReadOnlySpan<byte>(this.embeddedBytecodePtr, this.embeddedBytecodeSize);
return this.d3DBlob.Move();
}
/// <inheritdoc/>
public unsafe void LoadDynamicBytecode(IntPtr handle)
{
if (this.embeddedBytecodePtr is not null ||
this.d3DBlob.Get() is not null)
{
ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized.");
}
if (handle == IntPtr.Zero)
{
ThrowHelper.ThrowNotSupportedException("Runtime shader compilation is not supported by the current configuration.");
}
this.d3DBlob = new ComPtr<ID3DBlob>((ID3DBlob*)handle);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void LoadEmbeddedBytecode(ReadOnlySpan<byte> bytecode)
{
if (this.embeddedBytecodePtr is not null ||
this.d3DBlob.Get() is not null)
{
ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized.");
}
this.embeddedBytecodePtr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(bytecode));
this.embeddedBytecodeSize = bytecode.Length;
}
}
+57
ファイルの表示
@@ -0,0 +1,57 @@
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CommunityToolkit.Diagnostics;
namespace ComputeSharp.D2D1Interop.Shaders.Interop.Buffers;
/// <summary>
/// A custom <see cref="MemoryManager{T}"/> that wraps a pinned memory buffer.
/// </summary>
internal sealed unsafe class PinnedBufferMemoryManager : MemoryManager<byte>
{
/// <summary>
/// A pointer to the buffer.
/// </summary>
private readonly byte* buffer;
/// <summary>
/// The length of the buffer.
/// </summary>
private readonly int length;
/// <summary>
/// Initializes a new instance of the <see cref="PinnedBufferMemoryManager"/> class with the specified parameters.
/// </summary>
/// <param name="span">A <see cref="ReadOnlySpan{T}"/> wrapping the input pinned buffer.</param>
public PinnedBufferMemoryManager(ReadOnlySpan<byte> span)
{
this.buffer = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span));
this.length = span.Length;
}
/// <inheritdoc/>
public override Span<byte> GetSpan()
{
return new(this.buffer, this.length);
}
/// <inheritdoc/>
public override unsafe MemoryHandle Pin(int elementIndex = 0)
{
Guard.IsInRange(elementIndex, 0, this.length);
return new(this.buffer + elementIndex);
}
/// <inheritdoc/>
public override void Unpin()
{
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
}
+39 -3
ファイルの表示
@@ -1,7 +1,11 @@
using System.Runtime.CompilerServices;
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1Interop.__Internals;
using ComputeSharp.D2D1Interop.Shaders.Dispatching;
using ComputeSharp.D2D1Interop.Shaders.Interop.Buffers;
using ComputeSharp.Shaders.Dispatching;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#pragma warning disable CS0618
@@ -12,11 +16,43 @@ namespace ComputeSharp.D2D1Interop.Interop;
/// </summary>
public static class InteropServices
{
/// <summary>
/// Loads 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="shader">The input D2D1 pixel shader to load the bytecode for.</param>
/// <param name="bytecode">A <see cref="ReadOnlyMemory{T}"/> instance with the resulting shader bytecode.</param>
/// <remarks>
/// If the input shader was precompiled, <paramref name="bytecode"/> will wrap a pinned memory buffer (from the PE section).
/// If the shader was compiled at runtime, <paramref name="bytecode"/> will wrap a <see cref="byte"/> array with the bytecode.
/// </remarks>
public static unsafe void LoadShaderBytecode<T>(in T shader, out ReadOnlyMemory<byte> bytecode)
where T : struct, ID2D1Shader
{
D2D1ShaderBytecodeLoader bytecodeLoader = default;
Unsafe.AsRef(in shader).LoadBytecode(ref bytecodeLoader);
using ComPtr<ID3DBlob> dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan<byte> precompiledBytecode);
// If a precompiled shader is available, return it
if (!precompiledBytecode.IsEmpty)
{
bytecode = new PinnedBufferMemoryManager(precompiledBytecode).Memory;
}
// Otherwise, return the dynamic shader instead
byte* bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer();
int bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize();
bytecode = new ReadOnlySpan<byte>(bytecodePtr, bytecodeSize).ToArray();
}
/// <summary>
/// Sets the constant buffer from an input D2D1 pixel shader, by calling <c>ID2D1DrawInfo::SetPixelShaderConstantBuffer</c>.
/// </summary>
/// <typeparam name="T">The type of compute shader to retrieve info for.</typeparam>
/// <param name="shader">The input compute shader to retrieve info for.</param>
/// <typeparam name="T">The type of D2D1 pixel shader to retrieve info for.</typeparam>
/// <param name="shader">The input D2D1 pixel shader to retrieve info for.</param>
/// <param name="d2D1DrawInfo">A pointer to the <c>ID2D1DrawInfo</c> instance to use.</param>
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1effectauthor/nf-d2d1effectauthor-id2d1drawinfo-setpixelshaderconstantbuffer"/>.</remarks>
public static unsafe void SetPixelShaderConstantBufferForD2D1DrawInfo<T>(in T shader, void* d2D1DrawInfo)