diff --git a/src/ComputeSharp.Shaders/Interop/ReflectionServices.cs b/src/ComputeSharp.Shaders/Interop/ReflectionServices.cs new file mode 100644 index 00000000..2b2d79cf --- /dev/null +++ b/src/ComputeSharp.Shaders/Interop/ReflectionServices.cs @@ -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 +{ + /// + /// Provides methods to extract reflection info on compute shaders generated using this library. + /// + public static class ReflectionServices + { + /// + /// Gets the shader info associated with a given shader. + /// + /// 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 -s. + /// + /// + /// The type of shader to retrieve info for. + /// The resulting instance. + /// + /// 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. + /// + public static void GetShaderInfo(out ShaderInfo shaderInfo) + where T : struct, IComputeShader + { + GetShaderInfo(default(T), out shaderInfo); + } + + /// + /// Gets the shader info associated with a given shader. + /// + /// The type of shader to retrieve info for. + /// The input shader to retrieve info for. + /// The resulting instance. + /// + /// 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. + /// + public static unsafe void GetShaderInfo(in T shader, out ShaderInfo shaderInfo) + where T : struct, IComputeShader + { + ShaderLoader shaderLoader = ShaderLoader.Load(in shader); + + using var shaderSource = HlslShaderRenderer.Render(1, 1, 1, shaderLoader); + + using ComPtr dxcBlobBytecode = ShaderCompiler.CompileShader(shaderSource.WrittenSpan); + + using ComPtr dxcUtils = default; + Guid dxcLibraryClsid = FX.CLSID_DxcLibrary; + + FX.DxcCreateInstance(&dxcLibraryClsid, FX.__uuidof(), dxcUtils.GetVoidAddressOf()).Assert(); + + using ComPtr d3D12ShaderReflection = default; + + DxcBuffer dxcBuffer = default; + dxcBuffer.Ptr = dxcBlobBytecode.Get()->GetBufferPointer(); + dxcBuffer.Size = dxcBlobBytecode.Get()->GetBufferSize(); + + dxcUtils.Get()->CreateReflection( + &dxcBuffer, + FX.__uuidof(), + 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(); + } + } +} diff --git a/src/ComputeSharp.Shaders/Interop/ShaderInfo.cs b/src/ComputeSharp.Shaders/Interop/ShaderInfo.cs new file mode 100644 index 00000000..fe962fbb --- /dev/null +++ b/src/ComputeSharp.Shaders/Interop/ShaderInfo.cs @@ -0,0 +1,128 @@ +namespace ComputeSharp.Interop +{ + /// + /// A model representing reflection info for a given compute shader. + /// + public readonly struct ShaderInfo + { + /// + /// The compiler version used to build the shader. + /// + public readonly string CompilerVersion; + + /// + /// The HLSL source code the shader was built from. + /// + public readonly string HlslSource; + + /// + /// The number of shader constant buffers. + /// + public readonly uint ConstantBufferCount; + + /// + /// The number of resource (textures and buffers) bound to a shader. + /// + public readonly uint BoundResourceCount; + + /// + /// The number of intermediate-language instructions in the compiled shader. + /// + public readonly uint InstructionCount; + + /// + /// The number of temporary registers in the compiled shader. + /// + public readonly uint TemporaryRegisterCount; + + /// + /// The number of temporary arrays used. + /// + public readonly uint TemporaryArrayCount; + + /// + /// The number of constant defines. + /// + public readonly uint ConstantDefineCount; + + /// + /// The number of declarations (input + output). + /// + public readonly uint DeclarationCount; + + /// + /// The number of non-categorized texture instructions. + /// + public readonly uint TextureNormalInstructions; + + /// + /// The number of texture load instructions. + /// + public readonly uint TextureLoadInstructionCount; + + /// + /// The number of texture write instructions. + /// + public readonly uint TextureStoreInstructionCount; + + /// + /// The number of floating point arithmetic instructions used. + /// + public readonly uint FloatInstructionCount; + + /// + /// The number of signed integer arithmetic instructions used. + /// + public readonly uint IntInstructionCount; + + /// + /// The number of unsigned integer arithmetic instructions used. + /// + public readonly uint UIntInstructionCount; + + /// + /// The number of static flow control instructions used. + /// + public readonly uint StaticFlowControlInstructionCount; + + /// + /// The number of dynamic flow control instructions used. + /// + public readonly uint DynamicFlowControlInstructionCount; + + /// + /// The number of emit instructions used. + /// + public readonly uint EmitInstructionCount; + + /// + /// The number of barrier instructions used. + /// + public readonly uint BarrierInstructionCount; + + /// + /// The number of interlocked instructions used. + /// + public readonly uint InterlockedInstructionCount; + + /// + /// The number of bitwise instructions used. + /// + public readonly uint BitwiseInstructionCount; + + /// + /// The number of movc instructions used. + /// + public readonly uint MovcInstructionCount; + + /// + /// The number of mov instructions used. + /// + public readonly uint MovInstructionCount; + + /// + /// The number of interface slots used. + /// + public readonly uint InterfaceSlotCount; + } +}