Update generator to apply custom compile options

このコミットが含まれているのは:
Sergio Pedri committed 2023-11-21 16:01:09 +01:00
1 親 9580730e93
コミット fb0a1a441a
6ファイル変更
+158 -36

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

+19 -1
ファイルの表示
@@ -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>
+6 -1
ファイルの表示
@@ -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);
+1
ファイルの表示
@@ -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" />
+123 -32
ファイルの表示
@@ -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();
+5 -1
ファイルの表示
@@ -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);
+4 -1
ファイルの表示
@@ -56,7 +56,7 @@ public enum CompileOptions
/// By default, the compiler disables strictness on deprecated syntax.
/// </summary>
/// <remarks>
/// This flag maps to <c>-Ges</c>.W
/// This flag maps to <c>-Ges</c>.
/// </remarks>
EnableStrictness = 1 << 5,
@@ -134,6 +134,9 @@ public enum CompileOptions
/// 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 fail at runtime. Recommend if not reflection is done.
/// </summary>
/// <remarks>
/// This flag maps to <c>-Qstrip_reflect</c>.
/// </remarks>
StripReflectionData = 1 << 14,
/// <summary>