Merge pull request #658 from Sergio0694/dev/dxc-compile-options
Add [CompileOptions] attribute for DX12 shaders
このコミットが含まれているのは:
8ファイル変更
+380
-35
検索条件に一致するファイルはありません
@@ -53,7 +53,7 @@ partial class ComputeShaderDescriptorGenerator
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Compile the shader bytecode
|
||||
byte[] bytecode = DxcShaderCompiler.Instance.Compile(key.HlslSource.AsSpan(), token);
|
||||
byte[] bytecode = DxcShaderCompiler.Instance.Compile(key.HlslSource.AsSpan(), key.CompileOptions, token);
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
@@ -72,6 +72,24 @@ partial class ComputeShaderDescriptorGenerator
|
||||
return HlslBytecodeCache.GetOrCreate(ref key, GetInfo, token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the compile options for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
|
||||
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
|
||||
/// <returns>The requested compile options to use to compile the shader, if present.</returns>
|
||||
public static CompileOptions GetCompileOptions(ImmutableArrayBuilder<DiagnosticInfo> diagnostics, INamedTypeSymbol structDeclarationSymbol)
|
||||
{
|
||||
// If a [CompileOptions] annotation is present, return the explicit options
|
||||
if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.CompileOptionsAttribute", out AttributeData? attributeData) ||
|
||||
structDeclarationSymbol.ContainingAssembly.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.CompileOptionsAttribute", out attributeData))
|
||||
{
|
||||
return (CompileOptions)attributeData.ConstructorArguments[0].Value!;
|
||||
}
|
||||
|
||||
return CompileOptions.Default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets any diagnostics from a processed <see cref="HlslBytecodeInfo"/> instance.
|
||||
/// </summary>
|
||||
|
||||
@@ -107,8 +107,13 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Get the compile options as well
|
||||
CompileOptions compileOptions = HlslBytecode.GetCompileOptions(diagnostics, typeSymbol);
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Prepare the lookup key for the HLSL shader bytecode
|
||||
HlslBytecodeInfoKey hlslInfoKey = new(hlslSource, isCompilationEnabled);
|
||||
HlslBytecodeInfoKey hlslInfoKey = new(hlslSource, compileOptions, isCompilationEnabled);
|
||||
|
||||
// Try to get the HLSL bytecode
|
||||
HlslBytecodeInfo hlslInfo = HlslBytecode.GetBytecode(ref hlslInfoKey, token);
|
||||
|
||||
@@ -55,6 +55,7 @@
|
||||
|
||||
<!-- ComputeSharp APIs -->
|
||||
<Compile Include="..\ComputeSharp\Core\Attributes\EmbeddedBytecodeAttribute.cs" Link="ComputeSharp\Core\Attributes\EmbeddedBytecodeAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Attributes\Enums\CompileOptions.cs" Link="ComputeSharp\Core\Attributes\Enums\CompileOptions.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Attributes\GroupSharedAttribute.cs" Link="ComputeSharp\Core\Attributes\GroupSharedAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Dispatch\DispatchAxis.cs" Link="ComputeSharp\Core\Dispatch\DispatchAxis.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Dispatch\DispatchSize.cs" Link="ComputeSharp\Core\Dispatch\DispatchSize.cs" />
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
@@ -71,42 +70,136 @@ internal sealed unsafe class DxcShaderCompiler
|
||||
/// Compiles a new HLSL shader from the input source code.
|
||||
/// </summary>
|
||||
/// <param name="source">The HLSL source code to compile.</param>
|
||||
/// <param name="compileOptions">The compile options to use.</param>
|
||||
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
|
||||
/// <returns>The bytecode for the compiled shader.</returns>
|
||||
public byte[] Compile(ReadOnlySpan<char> source, CancellationToken token)
|
||||
public byte[] Compile(ReadOnlySpan<char> source, CompileOptions compileOptions, CancellationToken token)
|
||||
{
|
||||
using ComPtr<IDxcResult> dxcResult = default;
|
||||
|
||||
// We can't use stackalloc here, because the portable Span<T> type has a bug that causes
|
||||
// the constructor taking a pointer to throw if T is a type that contains a pointer field.
|
||||
// So we can just rent a buffer from the pool and use that instead, at least for now.
|
||||
PCWSTR[] arguments = ArrayPool<PCWSTR>.Shared.Rent(3);
|
||||
|
||||
// Try to compile the new compute shader
|
||||
fixed (char* pSourceName = "")
|
||||
fixed (char* pEntryPoint = "Execute")
|
||||
fixed (char* pTargetProfile = "cs_6_0")
|
||||
fixed (char* optimization = "-O3")
|
||||
fixed (char* rowMajor = "-Zpr")
|
||||
fixed (char* warningsAsErrors = "-Werror")
|
||||
fixed (PCWSTR* pArguments = arguments)
|
||||
using (ComPtr<IDxcCompilerArgs> dxcCompilerArgs = default)
|
||||
{
|
||||
pArguments[0] = optimization;
|
||||
pArguments[1] = rowMajor;
|
||||
pArguments[2] = warningsAsErrors;
|
||||
|
||||
using ComPtr<IDxcCompilerArgs> dxcCompilerArgs = default;
|
||||
|
||||
// Create the arguments to append others to
|
||||
this.dxcUtils.Get()->BuildArguments(
|
||||
pSourceName: pSourceName,
|
||||
pEntryPoint: pEntryPoint,
|
||||
pTargetProfile: pTargetProfile,
|
||||
pArguments: pArguments,
|
||||
argCount: 3,
|
||||
pDefines: null,
|
||||
defineCount: 0,
|
||||
ppArgs: dxcCompilerArgs.GetAddressOf()).Assert();
|
||||
fixed (char* pSourceName = "")
|
||||
fixed (char* pEntryPoint = "Execute")
|
||||
fixed (char* pTargetProfile = "cs_6_0")
|
||||
fixed (char* pRowMajor = "-Zpr")
|
||||
{
|
||||
// The row major argument is the only one always set automatically.
|
||||
// All the others are dynamically added based on the selected options.
|
||||
PCWSTR rowMajorArgument = pRowMajor;
|
||||
|
||||
this.dxcUtils.Get()->BuildArguments(
|
||||
pSourceName: pSourceName,
|
||||
pEntryPoint: pEntryPoint,
|
||||
pTargetProfile: pTargetProfile,
|
||||
pArguments: &rowMajorArgument,
|
||||
argCount: 1,
|
||||
pDefines: null,
|
||||
defineCount: 0,
|
||||
ppArgs: dxcCompilerArgs.GetAddressOf()).Assert();
|
||||
}
|
||||
|
||||
// Add any other optional arguments
|
||||
fixed (char* pAllResourcesBound = "-all-resources-bound")
|
||||
fixed (char* pDisableValidation = "-Vd")
|
||||
fixed (char* pDisableOptimization = "-Od")
|
||||
fixed (char* pAvoidFlowControl = "-Gfa")
|
||||
fixed (char* pPreferFlowControl = "-Gfp")
|
||||
fixed (char* pEnableStrictness = "-Ges")
|
||||
fixed (char* pIeeeStrictness = "-Gis")
|
||||
fixed (char* pEnableBackwardsCompatibility = "-Gec")
|
||||
fixed (char* pOptimizationLevel0 = "-O0")
|
||||
fixed (char* pOptimizationLevel1 = "-O1")
|
||||
fixed (char* pOptimizationLevel2 = "-O2")
|
||||
fixed (char* pOptimizationLevel3 = "-O3")
|
||||
fixed (char* pWarningsAreErrors = "-WX")
|
||||
fixed (char* pResourcesMayAlias = "-res-may-alias")
|
||||
fixed (char* pStripReflectionData = "-Qstrip_reflect")
|
||||
{
|
||||
// Helper to add a new argument
|
||||
static void AddArgument(IDxcCompilerArgs* args, char* pArgument)
|
||||
{
|
||||
PCWSTR argument = pArgument;
|
||||
|
||||
args->AddArguments(&argument, 1).Assert();
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.AllResourcesBound))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pAllResourcesBound);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.DisableValidation))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pDisableValidation);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.DisableOptimization))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pDisableOptimization);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.AvoidFlowControl))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pAvoidFlowControl);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.PreferFlowControl))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pPreferFlowControl);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.EnableStrictness))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pEnableStrictness);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.IeeeStrictness))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pIeeeStrictness);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.EnableBackwardsCompatibility))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pEnableBackwardsCompatibility);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.OptimizationLevel0))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pOptimizationLevel0);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.OptimizationLevel1))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pOptimizationLevel1);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.OptimizationLevel2))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pOptimizationLevel2);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.OptimizationLevel3))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pOptimizationLevel3);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.WarningsAreErrors))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pWarningsAreErrors);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.ResourcesMayAlias))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pResourcesMayAlias);
|
||||
}
|
||||
|
||||
if (compileOptions.HasFlag(CompileOptions.StripReflectionData))
|
||||
{
|
||||
AddArgument(dxcCompilerArgs.Get(), pStripReflectionData);
|
||||
}
|
||||
}
|
||||
|
||||
fixed (char* pSource = source)
|
||||
{
|
||||
@@ -127,8 +220,6 @@ internal sealed unsafe class DxcShaderCompiler
|
||||
riid: &dxcResultIid,
|
||||
ppResult: (void**)dxcResult.GetAddressOf());
|
||||
|
||||
ArrayPool<PCWSTR>.Shared.Return(arguments);
|
||||
|
||||
// Only assert the HRESULT after returning the array from the pool. Since the method itself
|
||||
// can return a failure but doesn't actually throw, there's no need to throw the buffer away.
|
||||
hresult.Assert();
|
||||
|
||||
@@ -6,5 +6,9 @@ namespace ComputeSharp.SourceGenerators.Models;
|
||||
/// A model with info to be a unique key for <see cref="HlslBytecodeInfo"/> instances.
|
||||
/// </summary>
|
||||
/// <param name="HlslSource">The input HLSL source code.</param>
|
||||
/// <param name="CompileOptions">The compile options.</param>
|
||||
/// <param name="IsCompilationEnabled">Whether compilation should be attempted for the current info.</param>
|
||||
internal sealed record HlslBytecodeInfoKey(string HlslSource, bool IsCompilationEnabled);
|
||||
internal sealed record HlslBytecodeInfoKey(
|
||||
string HlslSource,
|
||||
CompileOptions CompileOptions,
|
||||
bool IsCompilationEnabled);
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp;
|
||||
|
||||
/// <summary>
|
||||
/// An attribute that indicates the compile options that should be used when compiling a given compute shader.
|
||||
/// <para>
|
||||
/// This attribute can be used to annotate shader types as follows:
|
||||
/// <code>
|
||||
/// [CompileOptions(CompileOptions.IeeeStrictness | CompileOptions.OptimizationLevel3)]
|
||||
/// struct MyShader : IComputeShader
|
||||
/// {
|
||||
/// }
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This attribute can also be added to a whole assembly, and will be used by default if not overridden by a shader type.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="options">The compiler options to use to compile the shader.</param>
|
||||
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)]
|
||||
public sealed class CompileOptionsAttribute(CompileOptions options) : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the compile options to use to compile the shader.
|
||||
/// </summary>
|
||||
public CompileOptions Options { get; } = options;
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp;
|
||||
|
||||
/// <summary>
|
||||
/// Flags to control the options that can be used to compile a compute shader.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum CompileOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Enables agressive flattening.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-all-resources-bound</c>.
|
||||
/// </remarks>
|
||||
AllResourcesBound = 1 << 0,
|
||||
|
||||
/// <summary>
|
||||
/// Directs the compiler not to validate the generated code against known capabilities and constraints.
|
||||
/// This option should only be used with shaders that have been successfully compiled in the past.
|
||||
/// DirectX always validates shaders before it sets them to a device.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-Vd</c>.
|
||||
/// </remarks>
|
||||
DisableValidation = 1 << 1,
|
||||
|
||||
/// <summary>
|
||||
/// Directs the compiler to skip optimization steps during code generation.
|
||||
/// This option should only be used for debug purposes.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-Od</c>.
|
||||
/// </remarks>
|
||||
DisableOptimization = 1 << 2,
|
||||
|
||||
/// <summary>
|
||||
/// Directs the compiler to not use flow-control constructs where possible.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-Gfa</c>.
|
||||
/// </remarks>
|
||||
AvoidFlowControl = 1 << 3,
|
||||
|
||||
/// <summary>
|
||||
/// Directs the compiler to prefer using flow-control constructs where possible.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-Gfp</c>.
|
||||
/// </remarks>
|
||||
PreferFlowControl = 1 << 4,
|
||||
|
||||
/// <summary>
|
||||
/// Forces strict compile, which might not allow for legacy syntax.
|
||||
/// By default, the compiler disables strictness on deprecated syntax.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-Ges</c>.
|
||||
/// </remarks>
|
||||
EnableStrictness = 1 << 5,
|
||||
|
||||
/// <summary>
|
||||
/// Forces the IEEE strict compile which avoids optimizations that may break IEEE rules.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-Gis</c>.
|
||||
/// </remarks>
|
||||
IeeeStrictness = 1 << 6,
|
||||
|
||||
/// <summary>
|
||||
/// Enables backward compatibility mode.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-Gec</c>.
|
||||
/// </remarks>
|
||||
EnableBackwardsCompatibility = 1 << 7,
|
||||
|
||||
/// <summary>
|
||||
/// Directs the compiler to use the lowest optimization level. If this option is set, the compiler
|
||||
/// might produce slower code but produces the code more quickly. This option should be set when
|
||||
/// shaders are developed iteratively, as it can reduce compilation times.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-O0</c>.
|
||||
/// </remarks>
|
||||
OptimizationLevel0 = 1 << 8,
|
||||
|
||||
/// <summary>
|
||||
/// Directs the compiler to use the second lowest optimization level.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-O1</c>.
|
||||
/// </remarks>
|
||||
OptimizationLevel1 = OptimizationLevel0 | (1 << 9),
|
||||
|
||||
/// <summary>
|
||||
/// Directs the compiler to use the second highest optimization level.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-O2</c>.
|
||||
/// </remarks>
|
||||
OptimizationLevel2 = OptimizationLevel1 | (1 << 10),
|
||||
|
||||
/// <summary>
|
||||
/// Directs the compiler to use the highest optimization level. If this option is set, the compiler
|
||||
/// produces the best possible code but might take significantly longer to do so. This option should
|
||||
/// be set for final builds of an application when performance is the most important factor.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-O3</c>.
|
||||
/// </remarks>
|
||||
OptimizationLevel3 = OptimizationLevel2 | (1 << 11),
|
||||
|
||||
/// <summary>
|
||||
/// Directs the compiler to treat all warnings as errors when it compiles the shader code. This
|
||||
/// option is recommended for new shader code, so that warnings can be resolved and the
|
||||
/// number of hard-to-find code defects can be minimized.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-WX</c>.
|
||||
/// </remarks>
|
||||
WarningsAreErrors = 1 << 12,
|
||||
|
||||
/// <summary>
|
||||
/// Directs the compiler that UAVs (uniform access views) and SRVs (shader resource views) may alias.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-res-may-alias</c>.
|
||||
/// </remarks>
|
||||
ResourcesMayAlias = 1 << 13,
|
||||
|
||||
/// <summary>
|
||||
/// Strips the reflection data from the generated shader bytecode. The bytecode size will be significantly lower, but
|
||||
/// trying to perform reflection on the shader will return inaccurate results. Recommend if not reflection is done.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This flag maps to <c>-Qstrip_reflect</c>.
|
||||
/// </remarks>
|
||||
StripReflectionData = 1 << 14,
|
||||
|
||||
/// <summary>
|
||||
/// The default options for shaders compiled by ComputeSharp.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This option does not include a flag to pack matrices in column-major order on input and output from the shader,
|
||||
/// as that is always enabled by default (it is necessary to ensure the constant buffer matches the managed layout).
|
||||
/// </remarks>
|
||||
Default = OptimizationLevel3 | WarningsAreErrors
|
||||
}
|
||||
@@ -559,6 +559,54 @@ namespace ComputeSharp.Tests
|
||||
this.buffer[ThreadIds.X] *= x;
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShaderWithStrippedReflectionData()
|
||||
{
|
||||
ShaderInfo info1 = ReflectionServices.GetShaderInfo<ShaderWithStrippedReflectionData1>();
|
||||
|
||||
// With no reflection data available, the instruction count is just 0
|
||||
Assert.AreEqual(0u, info1.InstructionCount);
|
||||
|
||||
ShaderInfo info2 = ReflectionServices.GetShaderInfo<ShaderWithStrippedReflectionData2>();
|
||||
|
||||
// Sanity check, here instead we should have some valid count
|
||||
Assert.AreNotEqual(0u, info2.InstructionCount);
|
||||
|
||||
// Verify that the bytecode with stripped reflection is much smaller
|
||||
Assert.IsTrue(info1.HlslBytecode.Length < 1800);
|
||||
Assert.IsTrue(info2.HlslBytecode.Length > 3300);
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
[EmbeddedBytecode(DispatchAxis.X)]
|
||||
[CompileOptions(CompileOptions.Default | CompileOptions.StripReflectionData)]
|
||||
[GeneratedComputeShaderDescriptor]
|
||||
internal readonly partial struct ShaderWithStrippedReflectionData1 : IComputeShader
|
||||
{
|
||||
public readonly ReadWriteBuffer<float> buffer;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
this.buffer[ThreadIds.X] = ThreadIds.X;
|
||||
}
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
[EmbeddedBytecode(DispatchAxis.X)]
|
||||
[CompileOptions(CompileOptions.Default)]
|
||||
[GeneratedComputeShaderDescriptor]
|
||||
internal readonly partial struct ShaderWithStrippedReflectionData2 : IComputeShader
|
||||
{
|
||||
public readonly ReadWriteBuffer<float> buffer;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
this.buffer[ThreadIds.X] = ThreadIds.X;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする