Switch ShaderInfo to be a record

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-19 20:50:52 +01:00
1 親 45cc81c2d0
コミット 2b7758ffbf
5ファイル変更
+105 -181

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

+40 -40
ファイルの表示
@@ -25,16 +25,16 @@ public static class ReflectionServices
/// </para>
/// </summary>
/// <typeparam name="T">The type of compute shader to retrieve info for.</typeparam>
/// <param name="shaderInfo">The resulting <see cref="ShaderInfo"/> instance.</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 void GetShaderInfo<T>(out ShaderInfo shaderInfo)
public static ShaderInfo GetShaderInfo<T>()
where T : struct, IComputeShader
{
GetNonGenericShaderInfo(default(T), out shaderInfo);
return GetNonGenericShaderInfo(default(T));
}
/// <summary>
@@ -42,16 +42,16 @@ public static class ReflectionServices
/// </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>
/// <param name="shaderInfo">The resulting <see cref="ShaderInfo"/> instance.</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 void GetShaderInfo<T>(in T shader, out ShaderInfo shaderInfo)
public static unsafe ShaderInfo GetShaderInfo<T>(in T shader)
where T : struct, IComputeShader
{
GetNonGenericShaderInfo(in shader, out shaderInfo);
return GetNonGenericShaderInfo(in shader);
}
/// <summary>
@@ -63,17 +63,17 @@ public static class ReflectionServices
/// </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="shaderInfo">The resulting <see cref="ShaderInfo"/> instance.</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 void GetShaderInfo<T, TPixel>(out ShaderInfo shaderInfo)
public static ShaderInfo GetShaderInfo<T, TPixel>()
where T : struct, IPixelShader<TPixel>
where TPixel : unmanaged
{
GetNonGenericShaderInfo(default(T), out shaderInfo);
return GetNonGenericShaderInfo(default(T));
}
/// <summary>
@@ -82,17 +82,17 @@ public static class ReflectionServices
/// <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>
/// <param name="shaderInfo">The resulting <see cref="ShaderInfo"/> instance.</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 void GetShaderInfo<T, TPixel>(in T shader, out ShaderInfo shaderInfo)
public static unsafe ShaderInfo GetShaderInfo<T, TPixel>(in T shader)
where T : struct, IPixelShader<TPixel>
where TPixel : unmanaged
{
GetNonGenericShaderInfo(in shader, out shaderInfo);
return GetNonGenericShaderInfo(in shader);
}
/// <summary>
@@ -100,8 +100,8 @@ public static class ReflectionServices
/// </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>
private static unsafe void GetNonGenericShaderInfo<T>(in T shader, out ShaderInfo shaderInfo)
/// <returns>The resulting <see cref="ShaderInfo"/> instance.</returns>
private static unsafe ShaderInfo GetNonGenericShaderInfo<T>(in T shader)
where T : struct, IShader
{
Unsafe.AsRef(in shader).BuildHlslString(out ArrayPoolStringBuilder shaderSource, 1, 1, 1);
@@ -132,31 +132,31 @@ public static class ReflectionServices
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();
Unsafe.AsRef(shaderInfo.RequiresDoublePrecisionSupport) = (d3D12ShaderReflection.Get()->GetRequiresFlags() & (D3D.D3D_SHADER_REQUIRES_DOUBLES | D3D.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS)) != 0;
return new(
CompilerVersion: new string(d3D12ShaderDescription.Creator),
HlslSource: shaderSource.WrittenSpan.ToString(),
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);
}
}
+51 -127
ファイルの表示
@@ -3,130 +3,54 @@
/// <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;
/// <summary>
/// Indicates whether support for double precision floating point numbers is required.
/// </summary>
public readonly bool RequiresDoublePrecisionSupport;
}
/// <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="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>
/// <param name="TemporaryRegisterCount">The number of temporary registers in the compiled shader.</param>
/// <param name="TemporaryArrayCount">The number of temporary arrays used.</param>
/// <param name="ConstantDefineCount">The number of constant defines.</param>
/// <param name="DeclarationCount">The number of declarations (input + output).</param>
/// <param name="TextureNormalInstructions">The number of non-categorized texture instructions.</param>
/// <param name="TextureLoadInstructionCount"> The number of texture load instructions.</param>
/// <param name="TextureStoreInstructionCount">The number of texture write instructions.</param>
/// <param name="FloatInstructionCount">The number of floating point arithmetic instructions used.</param>
/// <param name="IntInstructionCount">The number of signed integer arithmetic instructions used.</param>
/// <param name="UIntInstructionCount">The number of unsigned integer arithmetic instructions used.</param>
/// <param name="StaticFlowControlInstructionCount">The number of static flow control instructions used.</param>
/// <param name="DynamicFlowControlInstructionCount">The number of dynamic flow control instructions used.</param>
/// <param name="EmitInstructionCount">The number of emit instructions used.</param>
/// <param name="BarrierInstructionCount">The number of barrier instructions used.</param>
/// <param name="InterlockedInstructionCount">The number of interlocked instructions used.</param>
/// <param name="BitwiseInstructionCount">The number of bitwise instructions used.</param>
/// <param name="MovcInstructionCount">The number of <c>movc</c> instructions used.</param>
/// <param name="MovInstructionCount">The number of <c>mov</c> instructions used.</param>
/// <param name="InterfaceSlotCount">The number of interface slots used.</param>
/// <param name="RequiresDoublePrecisionSupport">Indicates whether support for double precision floating point numbers is required.</param>
public sealed record ShaderInfo(
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);
+1 -1
ファイルの表示
@@ -237,7 +237,7 @@ public partial class DispatchTests
device.Get().For(11, 22, 3, new DispatchSizeShader(buffer));
ReflectionServices.GetShaderInfo<DispatchSizeShader>(out var info);
ShaderInfo info = ReflectionServices.GetShaderInfo<DispatchSizeShader>();
int[] data = buffer.ToArray();
+10 -10
ファイルの表示
@@ -12,7 +12,7 @@ namespace ComputeSharp.Tests
[TestMethod]
public void ReservedKeywords()
{
ReflectionServices.GetShaderInfo<ReservedKeywordsShader>(out _);
_ = ReflectionServices.GetShaderInfo<ReservedKeywordsShader>();
}
[AutoConstructor]
@@ -33,7 +33,7 @@ namespace ComputeSharp.Tests
[TestMethod]
public void SpecialTypeAsReturnType()
{
ReflectionServices.GetShaderInfo<SpecialTypeAsReturnTypeShader>(out _);
_ = ReflectionServices.GetShaderInfo<SpecialTypeAsReturnTypeShader>();
}
[AutoConstructor]
@@ -54,7 +54,7 @@ namespace ComputeSharp.Tests
[TestMethod]
public void LocalFunctionInExternalMethods()
{
ReflectionServices.GetShaderInfo<LocalFunctionInExternalMethodsShader>(out _);
_ = ReflectionServices.GetShaderInfo<LocalFunctionInExternalMethodsShader>();
}
[AutoConstructor]
@@ -78,7 +78,7 @@ namespace ComputeSharp.Tests
[TestMethod]
public void CapturedNestedStructType()
{
ReflectionServices.GetShaderInfo<CapturedNestedStructTypeShader>(out _);
_ = ReflectionServices.GetShaderInfo<CapturedNestedStructTypeShader>();
}
[AutoConstructor]
@@ -104,7 +104,7 @@ namespace ComputeSharp.Tests
[TestMethod]
public void ExternalStructType_Ok()
{
ReflectionServices.GetShaderInfo<ExternalStructTypeShader>(out var info);
_ = ReflectionServices.GetShaderInfo<ExternalStructTypeShader>();
}
[AutoConstructor]
@@ -125,7 +125,7 @@ namespace ComputeSharp.Tests
[TestMethod]
public void OutOfOrderMethods()
{
ReflectionServices.GetShaderInfo<OutOfOrderMethodsShader>(out var info);
_ = ReflectionServices.GetShaderInfo<OutOfOrderMethodsShader>();
}
[AutoConstructor]
@@ -158,7 +158,7 @@ namespace ComputeSharp.Tests
[TestMethod]
public void PixelShader()
{
ReflectionServices.GetShaderInfo<StatelessPixelShader, float4>(out var info);
ShaderInfo info = ReflectionServices.GetShaderInfo<StatelessPixelShader, float4>();
Assert.AreEqual(info.TextureStoreInstructionCount, 1u);
Assert.AreEqual(info.BoundResourceCount, 2u);
@@ -191,13 +191,13 @@ namespace ComputeSharp.Tests
[TestMethod]
public void LoopWithVarCounter()
{
ReflectionServices.GetShaderInfo<LoopWithVarCounterShader>(out var info);
_ = ReflectionServices.GetShaderInfo<LoopWithVarCounterShader>();
}
[TestMethod]
public void DoublePrecisionSupport()
{
ReflectionServices.GetShaderInfo<DoublePrecisionSupportShader>(out var info);
ShaderInfo info = ReflectionServices.GetShaderInfo<DoublePrecisionSupportShader>();
Assert.IsTrue(info.RequiresDoublePrecisionSupport);
}
@@ -241,7 +241,7 @@ namespace ExternalNamespace
[TestMethod]
public void UserDefinedType()
{
ReflectionServices.GetShaderInfo<UserDefinedTypeShader>(out var info);
_ = ReflectionServices.GetShaderInfo<UserDefinedTypeShader>();
}
}
}
+3 -3
ファイルの表示
@@ -95,7 +95,7 @@ public class ShadersTests
[Data(typeof(SwapChain.Shaders.Compute.ExtrudedTruchetPattern))]
public void ExtrudedTruchetPattern(Device device, Type shaderType)
{
ReflectionServices.GetShaderInfo<SwapChain.Shaders.Compute.ExtrudedTruchetPattern>(out var info);
ShaderInfo info = ReflectionServices.GetShaderInfo<SwapChain.Shaders.Compute.ExtrudedTruchetPattern>();
Assert.IsFalse(info.RequiresDoublePrecisionSupport);
@@ -161,7 +161,7 @@ public class ShadersTests
static void RunComputeShader<T>(ReadWriteTexture2D<Rgba32, float4> texture)
where T : struct, IComputeShader
{
ReflectionServices.GetShaderInfo<T>(out var info);
ShaderInfo info = ReflectionServices.GetShaderInfo<T>();
Assert.IsFalse(info.RequiresDoublePrecisionSupport);
@@ -230,7 +230,7 @@ public class ShadersTests
{
if (typeof(IComputeShader).IsAssignableFrom(shaderType))
{
ReflectionServices.GetShaderInfo<TCompute>(out var info);
ShaderInfo info = ReflectionServices.GetShaderInfo<TCompute>();
Assert.IsFalse(info.RequiresDoublePrecisionSupport);