Add support for [EmbeddedBytecode] in shader runner

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-06 17:43:44 +01:00
1 親 92b2934229
コミット 767c73aa04
4ファイル変更
+133 -54

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

+20
ファイルの表示
@@ -81,4 +81,24 @@ public interface IShader
#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)
#if NET6_0_OR_GREATER
{
throw new NotImplementedException();
}
#else
;
#endif
}
+18 -11
ファイルの表示
@@ -25,7 +25,7 @@ internal static class ShaderRunner<T>
/// <summary>
/// The mapping used to cache and reuse compiled shaders.
/// </summary>
private static readonly Dictionary<ShaderKey, CachedShader> ShadersCache = new();
private static readonly Dictionary<ShaderKey, ICachedShader> ShadersCache = new();
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
@@ -129,7 +129,7 @@ internal static class ShaderRunner<T>
lock (ShadersCache)
{
// Get or preload the shader
if (!ShadersCache.TryGetValue(key, out CachedShader? shaderData))
if (!ShadersCache.TryGetValue(key, out ICachedShader? shaderData))
{
LoadShader(threadsX, threadsY, threadsZ, ref shader, out shaderData);
@@ -183,7 +183,7 @@ internal static class ShaderRunner<T>
lock (ShadersCache)
{
// Get or preload the shader
if (!ShadersCache.TryGetValue(key, out CachedShader? shaderData))
if (!ShadersCache.TryGetValue(key, out ICachedShader? shaderData))
{
LoadShader(threadsX, threadsY, 1, ref shader, out shaderData);
@@ -222,27 +222,34 @@ internal static class ShaderRunner<T>
/// <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="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
/// <param name="shaderData">The <see cref="CachedShader"/> instance to return with the cached shader data.</param>
/// <param name="shaderData">The <see cref="ICachedShader"/> instance to return with the cached shader data.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, ref T shader, out CachedShader shaderData)
private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, ref T shader, out ICachedShader shaderData)
{
shader.BuildHlslString(out ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ);
if (shader.TryGetBytecode(threadsX, threadsY, threadsZ, out ReadOnlySpan<byte> bytecode))
{
shaderData = new ICachedShader.Embedded(bytecode);
}
else
{
shader.BuildHlslString(out ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ);
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(builder.WrittenSpan);
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(builder.WrittenSpan);
builder.Dispose();
builder.Dispose();
shaderData = new CachedShader(dxcBlobBytecode.Get());
shaderData = new ICachedShader.Dynamic(dxcBlobBytecode.Get());
}
}
/// <summary>
/// Creates and caches a <see cref="PipelineData"/> instance for a given shader.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
/// <param name="shaderData">The <see cref="CachedShader"/> instance with the data on the loaded shader.</param>
/// <param name="shaderData">The <see cref="ICachedShader"/> instance with the data on the loaded shader.</param>
/// <param name="pipelineData">The resulting <see cref="PipelineData"/> instance to use to run the shader.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe void CreatePipelineData(GraphicsDevice device, CachedShader shaderData, out PipelineData pipelineData)
private static unsafe void CreatePipelineData(GraphicsDevice device, ICachedShader shaderData, out PipelineData pipelineData)
{
using ComPtr<ID3D12RootSignature> d3D12RootSignature = default;
-43
ファイルの表示
@@ -1,43 +0,0 @@
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
namespace ComputeSharp.Shaders.Translation.Models;
/// <summary>
/// A class that contains info on a cached shader.
/// </summary>
internal sealed unsafe class CachedShader : NativeObject
{
/// <summary>
/// The <see cref="IDxcBlob"/> instance currently in use.
/// </summary>
private ComPtr<IDxcBlob> dxcBlobBytecode;
/// <summary>
/// Creates a new <see cref="CachedShader"/> instance with the specified parameters.
/// </summary>
/// <param name="dxcBlobBytecode">The <see cref="IDxcBlob"/> bytecode instance to wrap.</param>
public CachedShader(IDxcBlob* dxcBlobBytecode)
{
this.dxcBlobBytecode = new ComPtr<IDxcBlob>(dxcBlobBytecode);
}
/// <summary>
/// Gets the map of cached <see cref="PipelineData"/> instances for each GPU in use.
/// </summary>
public ConditionalWeakTable<GraphicsDevice, PipelineData> CachedPipelines { get; } = new();
/// <summary>
/// Gets a raw pointer to the <see cref="IDxcBlob"/> instance in use.
/// </summary>
public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new((ID3DBlob*)this.dxcBlobBytecode.Get());
/// <inheritdoc/>
protected override void OnDispose()
{
this.dxcBlobBytecode.Dispose();
}
}
+95
ファイルの表示
@@ -0,0 +1,95 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
namespace ComputeSharp.Shaders.Translation.Models;
/// <summary>
/// An interface that contains info on a cached shader.
/// </summary>
internal interface ICachedShader
{
/// <summary>
/// Gets the map of cached <see cref="PipelineData"/> instances for each GPU in use.
/// </summary>
ConditionalWeakTable<GraphicsDevice, PipelineData> CachedPipelines { get; }
/// <summary>
/// Gets a <see cref="D3D12_SHADER_BYTECODE"/> value pointing shader bytecode to use.
/// </summary>
D3D12_SHADER_BYTECODE D3D12ShaderBytecode { get; }
/// <summary>
/// An <see cref="ICachedShader"/> implementation for an embedded shader.
/// </summary>
public sealed unsafe class Embedded : ICachedShader
{
/// <summary>
/// The bytecode buffer, if a precompiled shader is used.
/// </summary>
private readonly byte* precompiledBytecodeBuffer;
/// <summary>
/// The buffer size for <see cref="precompiledBytecodeBuffer"/>, if present.
/// </summary>
private readonly uint precompiledBytecodeSize;
/// <summary>
/// Creates a new <see cref="Embedded"/> instance with the specified parameters.
/// </summary>
/// <param name="precompiledBytecode">The shader bytecode buffer to wrap.</param>
/// <remarks>
/// It is assumed that <paramref name="precompiledBytecode"/> is a static binary block in the <c>.text</c> segment of the
/// PE file, which is what Roslyn generates when directly assigning a new array to a <see cref="ReadOnlySpan{T}"/>. As such,
/// the underlying buffer is assumed to be pinned, and it will not be properly tracked for GC compactions. If an invalid
/// buffer is passed and its contents are moved, the behavior is undefined (access violations will most definitely occurr).
/// </remarks>
public Embedded(ReadOnlySpan<byte> precompiledBytecode)
{
this.precompiledBytecodeBuffer = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(precompiledBytecode));
this.precompiledBytecodeSize = (uint)precompiledBytecode.Length;
}
/// <inheritdoc/>
public ConditionalWeakTable<GraphicsDevice, PipelineData> CachedPipelines { get; } = new();
/// <inheritdoc/>
public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new(this.precompiledBytecodeBuffer, this.precompiledBytecodeSize);
}
/// <summary>
/// An <see cref="ICachedShader"/> implementation for a dynamically compiled shader.
/// </summary>
public sealed unsafe class Dynamic : NativeObject, ICachedShader
{
/// <summary>
/// The <see cref="IDxcBlob"/> instance currently in use.
/// </summary>
private ComPtr<IDxcBlob> dxcBlobBytecode;
/// <summary>
/// Creates a new <see cref="Dynamic"/> instance with the specified parameters.
/// </summary>
/// <param name="dxcBlobBytecode">The <see cref="IDxcBlob"/> bytecode instance to wrap.</param>
public Dynamic(IDxcBlob* dxcBlobBytecode)
{
this.dxcBlobBytecode = new ComPtr<IDxcBlob>(dxcBlobBytecode);
}
/// <inheritdoc/>
public ConditionalWeakTable<GraphicsDevice, PipelineData> CachedPipelines { get; } = new();
/// <inheritdoc/>
public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new ((ID3DBlob*)this.dxcBlobBytecode.Get());
/// <inheritdoc/>
protected override void OnDispose()
{
this.dxcBlobBytecode.Dispose();
}
}
}