using System; using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Core.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if !NET6_0_OR_GREATER using DirectX = TerraFX.Interop.DirectX.DirectX2; #endif #pragma warning disable CS0618 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 compute shader. /// /// 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 -s. /// /// /// The type of compute 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 ShaderInfo GetShaderInfo() where T : struct, IComputeShader { return GetNonGenericShaderInfo(default(T)); } /// /// Gets the shader info associated with a given compute shader. /// /// The type of compute shader to retrieve info for. /// The input compute 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 ShaderInfo GetShaderInfo(in T shader) where T : struct, IComputeShader { return GetNonGenericShaderInfo(in shader); } /// /// Gets the shader info associated with a given pixel shader. /// /// 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 -s. /// /// /// The type of pixel shader to retrieve info for. /// The type of pixels being processed by the shader. /// 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 ShaderInfo GetShaderInfo() where T : struct, IPixelShader where TPixel : unmanaged { return GetNonGenericShaderInfo(default(T)); } /// /// Gets the shader info associated with a given pixel shader. /// /// The type of pixel shader to retrieve info for. /// The type of pixels being processed by the shader. /// The input pixel 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 ShaderInfo GetShaderInfo(in T shader) where T : struct, IPixelShader where TPixel : unmanaged { return GetNonGenericShaderInfo(in shader); } /// /// Gets the shader info associated with a given shader of any type. /// /// The type of shader to retrieve info for. /// The input shader to retrieve info for. /// The resulting instance. private static unsafe ShaderInfo GetNonGenericShaderInfo(in T shader) where T : struct, IShader { Unsafe.AsRef(in shader).BuildHlslSource(out string hlslSource); using ComPtr dxcBlobBytecode = Shaders.Translation.ShaderCompiler.Instance.Compile(hlslSource); using ComPtr dxcUtils = default; DirectX.DxcCreateInstance( (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_DxcLibrary)), Windows.__uuidof(), (void**)dxcUtils.GetAddressOf()).Assert(); using ComPtr d3D12ShaderReflection = default; DxcBuffer dxcBuffer = default; dxcBuffer.Ptr = dxcBlobBytecode.Get()->GetBufferPointer(); dxcBuffer.Size = dxcBlobBytecode.Get()->GetBufferSize(); dxcUtils.Get()->CreateReflection( &dxcBuffer, Windows.__uuidof(), (void**)d3D12ShaderReflection.GetAddressOf()).Assert(); D3D12_SHADER_DESC d3D12ShaderDescription; d3D12ShaderReflection.Get()->GetDesc(&d3D12ShaderDescription).Assert(); return new( CompilerVersion: new string(d3D12ShaderDescription.Creator), HlslSource: hlslSource, ConstantBufferCount: d3D12ShaderDescription.ConstantBuffers, BoundResourceCount: d3D12ShaderDescription.BoundResources, InstructionCount: d3D12ShaderDescription.InstructionCount, TemporaryRegisterCount: d3D12ShaderDescription.TempRegisterCount, TemporaryArrayCount: d3D12ShaderDescription.TempArrayCount, ConstantDefineCount: d3D12ShaderDescription.DefCount, DeclarationCount: d3D12ShaderDescription.DclCount, TextureNormalInstructions: d3D12ShaderDescription.TextureNormalInstructions, TextureLoadInstructionCount: d3D12ShaderDescription.TextureLoadInstructions, TextureStoreInstructionCount: d3D12ShaderDescription.cTextureStoreInstructions, FloatInstructionCount: d3D12ShaderDescription.FloatInstructionCount, IntInstructionCount: d3D12ShaderDescription.IntInstructionCount, UIntInstructionCount: d3D12ShaderDescription.UintInstructionCount, StaticFlowControlInstructionCount: d3D12ShaderDescription.StaticFlowControlCount, DynamicFlowControlInstructionCount: d3D12ShaderDescription.DynamicFlowControlCount, EmitInstructionCount: d3D12ShaderDescription.EmitInstructionCount, BarrierInstructionCount: d3D12ShaderDescription.cBarrierInstructions, InterlockedInstructionCount: d3D12ShaderDescription.cInterlockedInstructions, BitwiseInstructionCount: d3D12ShaderReflection.Get()->GetBitwiseInstructionCount(), MovcInstructionCount: d3D12ShaderReflection.Get()->GetMovcInstructionCount(), MovInstructionCount: d3D12ShaderReflection.Get()->GetMovInstructionCount(), InterfaceSlotCount: d3D12ShaderReflection.Get()->GetNumInterfaceSlots(), RequiresDoublePrecisionSupport: (d3D12ShaderReflection.Get()->GetRequiresFlags() & (D3D.D3D_SHADER_REQUIRES_DOUBLES | D3D.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS)) != 0); } }