From 8afb6ba2e1caed6f67684eb6e5927e46f88cdaa1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 2 Apr 2022 15:49:31 +0200 Subject: [PATCH] Add D2D1ReflectionServices APIs --- .../Shaders/Interop/D2D1ReflectionServices.cs | 88 +++++++++++++++++++ .../Shaders/Interop/D2D1ShaderInfo.cs | 56 ++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs create mode 100644 src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ShaderInfo.cs diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs new file mode 100644 index 00000000..a7124848 --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs @@ -0,0 +1,88 @@ +using System; +using System.Runtime.CompilerServices; +using ComputeSharp.__Internals; +using ComputeSharp.Core.Extensions; +using ComputeSharp.D2D1Interop; +using ComputeSharp.D2D1Interop.Shaders.Translation; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Interop; + +/// +/// Provides methods to extract reflection info on D2D1 shaders generated using this library. +/// +public static class D2D1ReflectionServices +{ + /// + /// Gets the shader info associated with a given D2D1 shader. + /// + /// This overload can be used for simplicity when the D2D1 shader being inspected does not rely on captured + /// objects to be processed correctly. This is the case when it does not contain any -s. + /// + /// + /// The type of D2D1 shader to retrieve info for. + /// The resulting instance. + public static D2D1ShaderInfo GetShaderInfo() + where T : struct, ID2D1PixelShader + { + return GetShaderInfo(default(T)); + } + + /// + /// Gets the shader info associated with a given D2D1 shader. + /// + /// The type of D2D1 shader to retrieve info for. + /// The input D2D1 shader to retrieve info for. + /// The resulting instance. + public static unsafe D2D1ShaderInfo GetShaderInfo(in T shader) + where T : struct, ID2D1PixelShader + { + Unsafe.AsRef(in shader).BuildHlslString(out ArrayPoolStringBuilder shaderSource); + + using ComPtr d3DBlobBytecode = D2D1ShaderCompiler.Compile(shaderSource.WrittenSpan, enableLinkingSupport: false); + + shaderSource.Dispose(); + + using ComPtr d3D11ShaderReflection = default; + + DirectX.D3DReflect( + pSrcData: d3DBlobBytecode.Get()->GetBufferPointer(), + SrcDataSize: d3DBlobBytecode.Get()->GetBufferSize(), + pInterface: Windows.__uuidof(), + ppReflector: d3D11ShaderReflection.GetVoidAddressOf()).Assert(); + + D3D11_SHADER_DESC d3D11ShaderDescription; + + d3D11ShaderReflection.Get()->GetDesc(&d3D11ShaderDescription).Assert(); + + return new( + CompilerVersion: new string(d3D11ShaderDescription.Creator), + HlslSource: shaderSource.WrittenSpan.ToString(), + ConstantBufferCount: d3D11ShaderDescription.ConstantBuffers, + BoundResourceCount: d3D11ShaderDescription.BoundResources, + InstructionCount: d3D11ShaderDescription.InstructionCount, + TemporaryRegisterCount: d3D11ShaderDescription.TempRegisterCount, + TemporaryArrayCount: d3D11ShaderDescription.TempArrayCount, + ConstantDefineCount: d3D11ShaderDescription.DefCount, + DeclarationCount: d3D11ShaderDescription.DclCount, + TextureNormalInstructions: d3D11ShaderDescription.TextureNormalInstructions, + TextureLoadInstructionCount: d3D11ShaderDescription.TextureLoadInstructions, + TextureStoreInstructionCount: d3D11ShaderDescription.cTextureStoreInstructions, + FloatInstructionCount: d3D11ShaderDescription.FloatInstructionCount, + IntInstructionCount: d3D11ShaderDescription.IntInstructionCount, + UIntInstructionCount: d3D11ShaderDescription.UintInstructionCount, + StaticFlowControlInstructionCount: d3D11ShaderDescription.StaticFlowControlCount, + DynamicFlowControlInstructionCount: d3D11ShaderDescription.DynamicFlowControlCount, + EmitInstructionCount: d3D11ShaderDescription.EmitInstructionCount, + BarrierInstructionCount: d3D11ShaderDescription.cBarrierInstructions, + InterlockedInstructionCount: d3D11ShaderDescription.cInterlockedInstructions, + BitwiseInstructionCount: d3D11ShaderReflection.Get()->GetBitwiseInstructionCount(), + MovcInstructionCount: d3D11ShaderReflection.Get()->GetMovcInstructionCount(), + MovInstructionCount: d3D11ShaderReflection.Get()->GetMovInstructionCount(), + InterfaceSlotCount: d3D11ShaderReflection.Get()->GetNumInterfaceSlots(), + RequiresDoublePrecisionSupport: (d3D11ShaderReflection.Get()->GetRequiresFlags() & (D3D.D3D_SHADER_REQUIRES_DOUBLES | D3D.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS)) != 0); + } +} diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ShaderInfo.cs new file mode 100644 index 00000000..d8bafea7 --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ShaderInfo.cs @@ -0,0 +1,56 @@ +namespace ComputeSharp.Interop; + +/// +/// A model representing reflection info for a given D2D1 shader. +/// +/// The compiler version used to build the shader. +/// The HLSL source code the shader was built from. +/// The number of shader constant buffers. +/// The number of resource (textures and buffers) bound to a shader. +/// The number of intermediate-language instructions in the compiled shader. +/// The number of temporary registers in the compiled shader. +/// The number of temporary arrays used. +/// The number of constant defines. +/// The number of declarations (input + output). +/// The number of non-categorized texture instructions. +/// The number of texture load instructions. +/// The number of texture write instructions. +/// The number of floating point arithmetic instructions used. +/// The number of signed integer arithmetic instructions used. +/// The number of unsigned integer arithmetic instructions used. +/// The number of static flow control instructions used. +/// The number of dynamic flow control instructions used. +/// The number of emit instructions used. +/// The number of barrier instructions used. +/// The number of interlocked instructions used. +/// The number of bitwise instructions used. +/// The number of movc instructions used. +/// The number of mov instructions used. +/// The number of interface slots used. +/// Indicates whether support for double precision floating point numbers is required. +public sealed record D2D1ShaderInfo( + string CompilerVersion, + string HlslSource, + uint ConstantBufferCount, + uint BoundResourceCount, + uint InstructionCount, + uint TemporaryRegisterCount, + uint TemporaryArrayCount, + uint ConstantDefineCount, + uint DeclarationCount, + uint TextureNormalInstructions, + uint TextureLoadInstructionCount, + uint TextureStoreInstructionCount, + uint FloatInstructionCount, + uint IntInstructionCount, + uint UIntInstructionCount, + uint StaticFlowControlInstructionCount, + uint DynamicFlowControlInstructionCount, + uint EmitInstructionCount, + uint BarrierInstructionCount, + uint InterlockedInstructionCount, + uint BitwiseInstructionCount, + uint MovcInstructionCount, + uint MovInstructionCount, + uint InterfaceSlotCount, + bool RequiresDoublePrecisionSupport);