Add HLSL bytecode to ShaderInfo, remove leftovers

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-17 01:55:16 +02:00
1 親 7f00437a48
コミット 1734c09d1a
3ファイル変更
+33 -68

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

+20 -68
ファイルの表示
@@ -17,97 +17,43 @@ public static class ReflectionServices
{
/// <summary>
/// Gets the shader info associated with a given compute shader.
/// <para>
/// This overload can be used for simplicity when the compute 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 compute shader to retrieve info for.</typeparam>
/// <returns>The resulting <see cref="ShaderInfo"/> instance.</returns>
/// <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 ShaderInfo GetShaderInfo<T>()
where T : struct, IComputeShader, IComputeShaderDescriptor<T>
{
return GetNonGenericShaderInfo(default(T));
}
/// <summary>
/// Gets the shader info associated with a given compute shader.
/// </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>
/// <returns>The resulting <see cref="ShaderInfo"/> instance.</returns>
/// <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 ShaderInfo GetShaderInfo<T>(in T shader)
where T : struct, IComputeShader, IComputeShaderDescriptor<T>
{
return GetNonGenericShaderInfo(in shader);
return GetNonGenericShaderInfo<T>();
}
/// <summary>
/// Gets the shader info associated with a given pixel shader.
/// <para>
/// This overload can be used for simplicity when the pixel 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 pixel shader to retrieve info for.</typeparam>
/// <typeparam name="TPixel">The type of pixels being processed by the shader.</typeparam>
/// <returns>The resulting <see cref="ShaderInfo"/> instance.</returns>
/// <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 ShaderInfo GetShaderInfo<T, TPixel>()
where T : struct, IComputeShader<TPixel>, IComputeShaderDescriptor<T>
where TPixel : unmanaged
{
return GetNonGenericShaderInfo(default(T));
}
/// <summary>
/// Gets the shader info associated with a given pixel shader.
/// </summary>
/// <typeparam name="T">The type of pixel shader to retrieve info for.</typeparam>
/// <typeparam name="TPixel">The type of pixels being processed by the shader.</typeparam>
/// <param name="shader">The input pixel shader to retrieve info for.</param>
/// <returns>The resulting <see cref="ShaderInfo"/> instance.</returns>
/// <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 ShaderInfo GetShaderInfo<T, TPixel>(in T shader)
where T : struct, IComputeShader<TPixel>, IComputeShaderDescriptor<T>
where TPixel : unmanaged
{
return GetNonGenericShaderInfo(in shader);
return GetNonGenericShaderInfo<T>();
}
/// <summary>
/// Gets the shader info associated with a given shader of any type.
/// </summary>
/// <typeparam name="T">The type of shader to retrieve info for.</typeparam>
/// <param name="shader">The input shader to retrieve info for.</param>
/// <returns>The resulting <see cref="ShaderInfo"/> instance.</returns>
private static unsafe ShaderInfo GetNonGenericShaderInfo<T>(in T shader)
private static unsafe ShaderInfo GetNonGenericShaderInfo<T>()
where T : struct, IComputeShaderDescriptor<T>
{
string hlslSource = Unsafe.AsRef(in shader).HlslSource;
Unsafe.SkipInit(out T shader);
using ComPtr<IDxcBlob> dxcBlobBytecode = Shaders.Translation.ShaderCompiler.Instance.Compile(hlslSource);
ReadOnlyMemory<byte> hlslBytecode = shader.HlslBytecode;
using ComPtr<IDxcUtils> dxcUtils = default;
// Load the DXC compiler (the package will bundle the necessary .dll-s)
DirectX.DxcCreateInstance(
(Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_DxcLibrary)),
Windows.__uuidof<IDxcUtils>(),
@@ -115,14 +61,19 @@ public static class ReflectionServices
using ComPtr<ID3D12ShaderReflection> d3D12ShaderReflection = default;
DxcBuffer dxcBuffer = default;
dxcBuffer.Ptr = dxcBlobBytecode.Get()->GetBufferPointer();
dxcBuffer.Size = dxcBlobBytecode.Get()->GetBufferSize();
// Reflect over the shader (this will fail if reflection metadata has been stripped).
// Since that's just opt-in from users, that is the expected behavior, so not an issue.
fixed (byte* hlslBytecodePtr = hlslBytecode.Span)
{
DxcBuffer dxcBuffer = default;
dxcBuffer.Ptr = hlslBytecodePtr;
dxcBuffer.Size = (uint)hlslBytecode.Length;
dxcUtils.Get()->CreateReflection(
&dxcBuffer,
Windows.__uuidof<ID3D12ShaderReflection>(),
(void**)d3D12ShaderReflection.GetAddressOf()).Assert();
dxcUtils.Get()->CreateReflection(
&dxcBuffer,
Windows.__uuidof<ID3D12ShaderReflection>(),
(void**)d3D12ShaderReflection.GetAddressOf()).Assert();
}
D3D12_SHADER_DESC d3D12ShaderDescription;
@@ -130,7 +81,8 @@ public static class ReflectionServices
return new(
CompilerVersion: new string(d3D12ShaderDescription.Creator),
HlslSource: hlslSource,
HlslSource: shader.HlslSource,
HlslBytecode: hlslBytecode,
ConstantBufferCount: d3D12ShaderDescription.ConstantBuffers,
BoundResourceCount: d3D12ShaderDescription.BoundResources,
InstructionCount: d3D12ShaderDescription.InstructionCount,
+4
ファイルの表示
@@ -1,3 +1,5 @@
using System;
namespace ComputeSharp.Interop;
/// <summary>
@@ -5,6 +7,7 @@ namespace ComputeSharp.Interop;
/// </summary>
/// <param name="CompilerVersion">The compiler version used to build the shader.</param>
/// <param name="HlslSource">The HLSL source code the shader was built from.</param>
/// <param name="HlslBytecode">The HLSL bytecode for the shader.</param>
/// <param name="ConstantBufferCount">The number of shader constant buffers.</param>
/// <param name="BoundResourceCount">The number of resource (textures and buffers) bound to a shader.</param>
/// <param name="InstructionCount">The number of intermediate-language instructions in the compiled shader.</param>
@@ -31,6 +34,7 @@ namespace ComputeSharp.Interop;
public sealed record ShaderInfo(
string CompilerVersion,
string HlslSource,
ReadOnlyMemory<byte> HlslBytecode,
uint ConstantBufferCount,
uint BoundResourceCount,
uint InstructionCount,
+9
ファイルの表示
@@ -1,4 +1,5 @@
using ComputeSharp;
using ComputeSharp.Descriptors;
using ComputeSharp.Interop;
using ComputeSharp.Tests.Misc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -11,6 +12,14 @@ namespace ComputeSharp.Tests
[TestCategory("ShaderCompiler")]
public partial class ShaderCompilerTests
{
[TestMethod]
public void ReflectionBytecode()
{
ShaderInfo shaderInfo = ReflectionServices.GetShaderInfo<ReservedKeywordsShader>();
CollectionAssert.AreEqual(((IComputeShaderDescriptor<ReservedKeywordsShader>)default(ReservedKeywordsShader)).HlslBytecode.ToArray(), shaderInfo.HlslBytecode.ToArray());
}
[TestMethod]
public void ReservedKeywords()
{