Restructure ComputeSharp to support dynamic plugin

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-19 16:45:12 +01:00
1 親 f9a4477b69
コミット b532a9cdfb
4ファイル変更
+94 -44

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

+26
ファイルの表示
@@ -0,0 +1,26 @@
using System;
using System.ComponentModel;
namespace ComputeSharp.__Internals;
/// <summary>
/// A base <see langword="interface"/> representing a bytecode loader for a shader being dispatched.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This interface is not intended to be used directly by user code")]
public interface IBytecodeLoader
{
/// <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.</exception>
void LoadDynamicBytecode(IntPtr handle);
/// <summary>
/// Loads the embedded shader bytecode for the shader being dispatched.
/// </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);
}
+5 -23
ファイルの表示
@@ -65,35 +65,17 @@ public interface IShader
#endif
/// <summary>
/// Builds the HLSL source code for the current shader instance.
/// Loads the bytecode for the current shader.
/// </summary>
/// <param name="builder">The resulting <see cref="ArrayPoolStringBuilder"/> instance containing the HLSL source.</param>
/// <typeparam name="TBytecodeLoader">The type of bytecode loader being used.</typeparam>
/// <param name="loader">The <typeparamref name="TBytecodeLoader"/> instance to use to load the bytecode.</param>
/// <param name="threadsX">The number of threads in each thread group for the X axis.</param>
/// <param name="threadsY">The number of threads in each thread group for the Y axis.</param>
/// <param name="threadsZ">The number of threads in each thread group for the Z axis.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be called directly by user code")]
void BuildHlslString(out ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ)
#if NET6_0_OR_GREATER
{
throw new NotImplementedException();
}
#else
;
#endif
/// <summary>
/// Tries to retrieve the precompiled bytecode for the current shader.
/// </summary>
/// <param name="threadsX">The number of threads in each thread group for the X axis.</param>
/// <param name="threadsY">The number of threads in each thread group for the Y axis.</param>
/// <param name="threadsZ">The number of threads in each thread group for the Z axis.</param>
/// <param name="bytecode">The resulting bytecode, if found.</param>
/// <returns>Whether or not a precompiled bytecode was found.</returns>
/// <remarks>The underlying buffer for <paramref name="bytecode"/>, if found, can be assumed to always be pinned.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be used directly by user code")]
bool TryGetBytecode(int threadsX, int threadsY, int threadsZ, out ReadOnlySpan<byte> bytecode)
void LoadBytecode<TBytecodeLoader>(ref TBytecodeLoader loader, int threadsX, int threadsY, int threadsZ)
where TBytecodeLoader : struct, IBytecodeLoader
#if NET6_0_OR_GREATER
{
throw new NotImplementedException();
+60
ファイルの表示
@@ -0,0 +1,60 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.__Internals;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop.DirectX;
using ComputeSharp.Shaders.Translation.Models;
#pragma warning disable CS0618
namespace ComputeSharp.Shaders.Dispatching;
/// <summary>
/// A bytecode loader for compute shaders.
/// </summary>
internal struct ShaderBytecodeLoader : IBytecodeLoader
{
/// <summary>
/// The current cached shader instance.
/// </summary>
private ICachedShader? cachedShader;
/// <summary>
/// Gets the current cached shader instance.
/// </summary>
/// <returns>The current <see cref="ICachedShader"/> instance.</returns>
/// <exception cref="InvalidOperationException">Thrown if the shader has not been initialized.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ICachedShader GetCachedShader()
{
if (this.cachedShader is not ICachedShader cachedShader)
{
return ThrowHelper.ThrowInvalidOperationException<ICachedShader>("The shader has not been initialized");
}
return cachedShader;
}
/// <inheritdoc/>
public unsafe void LoadDynamicBytecode(IntPtr handle)
{
if (this.cachedShader is not null)
{
ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized");
}
this.cachedShader = new ICachedShader.Dynamic((IDxcBlob*)handle);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void LoadEmbeddedBytecode(ReadOnlySpan<byte> bytecode)
{
if (this.cachedShader is not null)
{
ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized");
}
this.cachedShader = new ICachedShader.Embedded(bytecode);
}
}
+3 -21
ファイルの表示
@@ -4,9 +4,6 @@ using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Shaders.Dispatching;
using ComputeSharp.Shaders.Extensions;
#if !DISABLE_RUNTIME_SHADER_COMPILATION_SUPPORT
using ComputeSharp.Shaders.Translation;
#endif
using ComputeSharp.Shaders.Translation.Models;
using ComputeSharp.__Internals;
using Microsoft.Toolkit.Diagnostics;
@@ -228,26 +225,11 @@ internal static class ShaderRunner<T>
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, ref T shader, out ICachedShader shaderData)
{
if (Unsafe.NullRef<T>().TryGetBytecode(threadsX, threadsY, threadsZ, out ReadOnlySpan<byte> bytecode))
{
shaderData = new ICachedShader.Embedded(bytecode);
}
else
{
#if DISABLE_RUNTIME_SHADER_COMPILATION_SUPPORT
ThrowHelper.ThrowNotSupportedException("Runtime shader compilation is not supported by the current configuration.");
ShaderBytecodeLoader bytecodeLoader = default;
shaderData = null!;
#else
shader.BuildHlslString(out ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ);
shader.LoadBytecode(ref bytecodeLoader, threadsX, threadsY, threadsZ);
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(builder.WrittenSpan);
builder.Dispose();
shaderData = new ICachedShader.Dynamic(dxcBlobBytecode.Get());
#endif
}
shaderData = bytecodeLoader.GetCachedShader();
}
/// <summary>