Added ReflectionServices type

このコミットが含まれているのは:
Sergio Pedri committed 2021-02-08 15:44:16 +01:00
1 親 5f6cedc8ab
コミット 63c0109128
2ファイル変更
+231

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

+103
ファイルの表示
@@ -0,0 +1,103 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Shaders.Renderer;
using ComputeSharp.Shaders.Translation;
using TerraFX.Interop;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Interop
{
/// <summary>
/// Provides methods to extract reflection info on compute shaders generated using this library.
/// </summary>
public static class ReflectionServices
{
/// <summary>
/// Gets the shader info associated with a given shader.
/// <para>
/// This overload can be used for simplicity when the shader being inspected does not rely on captured
/// objects to be processed correctly. This is the case when it does not contain any <see cref="Delegate"/>-s.
/// </para>
/// </summary>
/// <typeparam name="T">The type of shader to retrieve info for.</typeparam>
/// <param name="shaderInfo">The resulting <see cref="ShaderInfo"/> instance.</param>
/// <remarks>
/// The thread group sizes will always be set to (1, 1, 1) in the returned shader. This is done to
/// avoid having to compiler multiple shaders just to get reflection info for them. When using any of
/// the APIs to dispatch a shader, the thread sizes would actually be set to a proper value insead.
/// </remarks>
public static void GetShaderInfo<T>(out ShaderInfo shaderInfo)
where T : struct, IComputeShader
{
GetShaderInfo(default(T), out shaderInfo);
}
/// <summary>
/// Gets the shader info associated with a given shader.
/// </summary>
/// <typeparam name="T">The type of shader to retrieve info for.</typeparam>
/// <param name="shader">The input shader to retrieve info for.</param>
/// <param name="shaderInfo">The resulting <see cref="ShaderInfo"/> instance.</param>
/// <remarks>
/// The thread group sizes will always be set to (1, 1, 1) in the returned shader. This is done to
/// avoid having to compiler multiple shaders just to get reflection info for them. When using any of
/// the APIs to dispatch a shader, the thread sizes would actually be set to a proper value insead.
/// </remarks>
public static unsafe void GetShaderInfo<T>(in T shader, out ShaderInfo shaderInfo)
where T : struct, IComputeShader
{
ShaderLoader<T> shaderLoader = ShaderLoader<T>.Load(in shader);
using var shaderSource = HlslShaderRenderer.Render(1, 1, 1, shaderLoader);
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.CompileShader(shaderSource.WrittenSpan);
using ComPtr<IDxcUtils> dxcUtils = default;
Guid dxcLibraryClsid = FX.CLSID_DxcLibrary;
FX.DxcCreateInstance(&dxcLibraryClsid, FX.__uuidof<IDxcUtils>(), dxcUtils.GetVoidAddressOf()).Assert();
using ComPtr<ID3D12ShaderReflection> d3D12ShaderReflection = default;
DxcBuffer dxcBuffer = default;
dxcBuffer.Ptr = dxcBlobBytecode.Get()->GetBufferPointer();
dxcBuffer.Size = dxcBlobBytecode.Get()->GetBufferSize();
dxcUtils.Get()->CreateReflection(
&dxcBuffer,
FX.__uuidof<ID3D12ShaderReflection>(),
d3D12ShaderReflection.GetVoidAddressOf()).Assert();
D3D12_SHADER_DESC d3D12ShaderDescription;
d3D12ShaderReflection.Get()->GetDesc(&d3D12ShaderDescription).Assert();
shaderInfo = default;
Unsafe.AsRef(shaderInfo.CompilerVersion) = new string(d3D12ShaderDescription.Creator);
Unsafe.AsRef(shaderInfo.HlslSource) = shaderSource.WrittenSpan.ToString();
Unsafe.AsRef(shaderInfo.ConstantBufferCount) = d3D12ShaderDescription.ConstantBuffers;
Unsafe.AsRef(shaderInfo.BoundResourceCount) = d3D12ShaderDescription.BoundResources;
Unsafe.AsRef(shaderInfo.InstructionCount) = d3D12ShaderDescription.InstructionCount;
Unsafe.AsRef(shaderInfo.TemporaryRegisterCount) = d3D12ShaderDescription.TempRegisterCount;
Unsafe.AsRef(shaderInfo.TemporaryArrayCount) = d3D12ShaderDescription.TempArrayCount;
Unsafe.AsRef(shaderInfo.ConstantDefineCount) = d3D12ShaderDescription.DefCount;
Unsafe.AsRef(shaderInfo.DeclarationCount) = d3D12ShaderDescription.DclCount;
Unsafe.AsRef(shaderInfo.TextureNormalInstructions) = d3D12ShaderDescription.TextureNormalInstructions;
Unsafe.AsRef(shaderInfo.TextureLoadInstructionCount) = d3D12ShaderDescription.TextureLoadInstructions;
Unsafe.AsRef(shaderInfo.TextureStoreInstructionCount) = d3D12ShaderDescription.cTextureStoreInstructions;
Unsafe.AsRef(shaderInfo.FloatInstructionCount) = d3D12ShaderDescription.FloatInstructionCount;
Unsafe.AsRef(shaderInfo.IntInstructionCount) = d3D12ShaderDescription.IntInstructionCount;
Unsafe.AsRef(shaderInfo.UIntInstructionCount) = d3D12ShaderDescription.UintInstructionCount;
Unsafe.AsRef(shaderInfo.StaticFlowControlInstructionCount) = d3D12ShaderDescription.StaticFlowControlCount;
Unsafe.AsRef(shaderInfo.DynamicFlowControlInstructionCount) = d3D12ShaderDescription.DynamicFlowControlCount;
Unsafe.AsRef(shaderInfo.EmitInstructionCount) = d3D12ShaderDescription.EmitInstructionCount;
Unsafe.AsRef(shaderInfo.BarrierInstructionCount) = d3D12ShaderDescription.cBarrierInstructions;
Unsafe.AsRef(shaderInfo.InterlockedInstructionCount) = d3D12ShaderDescription.cInterlockedInstructions;
Unsafe.AsRef(shaderInfo.BitwiseInstructionCount) = d3D12ShaderReflection.Get()->GetBitwiseInstructionCount();
Unsafe.AsRef(shaderInfo.MovcInstructionCount) = d3D12ShaderReflection.Get()->GetMovcInstructionCount();
Unsafe.AsRef(shaderInfo.MovInstructionCount) = d3D12ShaderReflection.Get()->GetMovInstructionCount();
Unsafe.AsRef(shaderInfo.InterfaceSlotCount) = d3D12ShaderReflection.Get()->GetNumInterfaceSlots();
}
}
}
+128
ファイルの表示
@@ -0,0 +1,128 @@
namespace ComputeSharp.Interop
{
/// <summary>
/// A model representing reflection info for a given compute shader.
/// </summary>
public readonly struct ShaderInfo
{
/// <summary>
/// The compiler version used to build the shader.
/// </summary>
public readonly string CompilerVersion;
/// <summary>
/// The HLSL source code the shader was built from.
/// </summary>
public readonly string HlslSource;
/// <summary>
/// The number of shader constant buffers.
/// </summary>
public readonly uint ConstantBufferCount;
/// <summary>
/// The number of resource (textures and buffers) bound to a shader.
/// </summary>
public readonly uint BoundResourceCount;
/// <summary>
/// The number of intermediate-language instructions in the compiled shader.
/// </summary>
public readonly uint InstructionCount;
/// <summary>
/// The number of temporary registers in the compiled shader.
/// </summary>
public readonly uint TemporaryRegisterCount;
/// <summary>
/// The number of temporary arrays used.
/// </summary>
public readonly uint TemporaryArrayCount;
/// <summary>
/// The number of constant defines.
/// </summary>
public readonly uint ConstantDefineCount;
/// <summary>
/// The number of declarations (input + output).
/// </summary>
public readonly uint DeclarationCount;
/// <summary>
/// The number of non-categorized texture instructions.
/// </summary>
public readonly uint TextureNormalInstructions;
/// <summary>
/// The number of texture load instructions.
/// </summary>
public readonly uint TextureLoadInstructionCount;
/// <summary>
/// The number of texture write instructions.
/// </summary>
public readonly uint TextureStoreInstructionCount;
/// <summary>
/// The number of floating point arithmetic instructions used.
/// </summary>
public readonly uint FloatInstructionCount;
/// <summary>
/// The number of signed integer arithmetic instructions used.
/// </summary>
public readonly uint IntInstructionCount;
/// <summary>
/// The number of unsigned integer arithmetic instructions used.
/// </summary>
public readonly uint UIntInstructionCount;
/// <summary>
/// The number of static flow control instructions used.
/// </summary>
public readonly uint StaticFlowControlInstructionCount;
/// <summary>
/// The number of dynamic flow control instructions used.
/// </summary>
public readonly uint DynamicFlowControlInstructionCount;
/// <summary>
/// The number of emit instructions used.
/// </summary>
public readonly uint EmitInstructionCount;
/// <summary>
/// The number of barrier instructions used.
/// </summary>
public readonly uint BarrierInstructionCount;
/// <summary>
/// The number of interlocked instructions used.
/// </summary>
public readonly uint InterlockedInstructionCount;
/// <summary>
/// The number of bitwise instructions used.
/// </summary>
public readonly uint BitwiseInstructionCount;
/// <summary>
/// The number of <c>movc</c> instructions used.
/// </summary>
public readonly uint MovcInstructionCount;
/// <summary>
/// The number of <c>mov</c> instructions used.
/// </summary>
public readonly uint MovInstructionCount;
/// <summary>
/// The number of interface slots used.
/// </summary>
public readonly uint InterfaceSlotCount;
}
}