Update HLSL compilation method in the generator

このコミットが含まれているのは:
Sergio Pedri committed 2023-09-28 23:08:20 +02:00
1 親 d0421e52a7
コミット 50f3b74d3c
3ファイル変更
+81 -53

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

+58 -44
ファイルの表示
@@ -27,11 +27,11 @@ partial class ID2D1ShaderGenerator
internal static partial class LoadBytecode
{
/// <summary>
/// Extracts the shader profile for the current shader.
/// Extracts the requested shader profile for the current shader.
/// </summary>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <returns>The shader profile to use to compile the shader, if present.</returns>
public static D2D1ShaderProfile? GetShaderProfile(INamedTypeSymbol structDeclarationSymbol)
/// <returns>The requested shader profile to use to compile the shader, if present.</returns>
public static D2D1ShaderProfile? GetRequestedShaderProfile(INamedTypeSymbol structDeclarationSymbol)
{
if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out AttributeData? attributeData))
{
@@ -47,12 +47,12 @@ partial class ID2D1ShaderGenerator
}
/// <summary>
/// Extracts the compile options for the current shader.
/// Extracts the requested 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 compile options to use to compile the shader, if present.</returns>
public static D2D1CompileOptions? GetCompileOptions(ImmutableArrayBuilder<DiagnosticInfo> diagnostics, INamedTypeSymbol structDeclarationSymbol)
/// <returns>The requested compile options to use to compile the shader, if present.</returns>
public static D2D1CompileOptions? GetRequestedCompileOptions(ImmutableArrayBuilder<DiagnosticInfo> diagnostics, INamedTypeSymbol structDeclarationSymbol)
{
if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData))
{
@@ -79,6 +79,30 @@ partial class ID2D1ShaderGenerator
return null;
}
/// <summary>
/// Gets the effective shader profile to use.
/// </summary>
/// <param name="shaderProfile">The requested shader profile.</param>
/// <returns>The effective shader profile.</returns>
public static D2D1ShaderProfile GetEffectiveShaderProfile(D2D1ShaderProfile? shaderProfile)
{
// The effective shader profile is either be the requested one, or the default value (which maps to PS5.0)
return shaderProfile ?? D2D1ShaderProfile.PixelShader50;
}
/// <summary>
/// Gets the effective compile options to use.
/// </summary>
/// <param name="compileOptions">The requested compile options.</param>
/// <param name="isLinkingSupported">Whether the input shader supports linking.</param>
/// <returns>The effective compile options.</returns>
public static D2D1CompileOptions GetEffectiveCompileOptions(D2D1CompileOptions? compileOptions, bool isLinkingSupported)
{
// If an explicit set of compile options is provided, use that directly. Otherwise, use the default
// options plus the option to enable linking only if the shader can potentially support it.
return compileOptions ?? (D2D1CompileOptions.Default | (isLinkingSupported ? D2D1CompileOptions.EnableLinking : 0));
}
/// <summary>
/// Extracts the metadata definition for the current shader.
/// </summary>
@@ -113,49 +137,43 @@ partial class ID2D1ShaderGenerator
}
/// <summary>
/// Gets an <see cref="ImmutableArray{T}"/> instance with the compiled bytecode for the current shader.
/// Gets the <see cref="HlslBytecodeInfo"/> instance for the input shader info.
/// </summary>
/// <param name="sourceInfo">The source info for the shader to compile.</param>
/// <param name="hlslSource">The input HLSL source code.</param>
/// <param name="requestedShaderProfile">The requested shader profile, if available.</param>
/// <param name="requestedCompileOptions">The requested compile options, if available.</param>
/// <param name="effectiveShaderProfile">The effective shader profile.</param>
/// <param name="effectiveCompileOptions">The effective compile options.</param>
/// <param name="hasErrors">Whether any errors were produced when analyzing the shader.</param>
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
/// <param name="shaderProfile">The effective shader profile used to create the shader bytecode, or the default profile to use.</param>
/// <param name="compileOptions">The effective compile options used to create the shader bytecode, or the default options to use.</param>
/// <param name="diagnostic">The resulting diagnostic from the processing operation, if any.</param>
/// <returns>The <see cref="ImmutableArray{T}"/> instance with the compiled shader bytecode.</returns>
public static unsafe ImmutableArray<byte> GetBytecode(
HlslShaderSourceInfo sourceInfo,
CancellationToken token,
out D2D1ShaderProfile shaderProfile,
out D2D1CompileOptions compileOptions,
out DeferredDiagnosticInfo? diagnostic)
/// <returns></returns>
public static unsafe HlslBytecodeInfo GetInfo(
string hlslSource,
D2D1ShaderProfile? requestedShaderProfile,
D2D1CompileOptions? requestedCompileOptions,
D2D1ShaderProfile effectiveShaderProfile,
D2D1CompileOptions effectiveCompileOptions,
bool hasErrors,
CancellationToken token)
{
// Set the shader profile to either be the requested one, or the default value (which maps to PS5.0)
shaderProfile = sourceInfo.ShaderProfile ?? D2D1ShaderProfile.PixelShader50;
// If an explicit set of compile options is provided, use that directly. Otherwise, use the default
// options plus the option to enable linking only if the shader can potentially support it.
compileOptions = sourceInfo.CompileOptions ?? (D2D1CompileOptions.Default | (sourceInfo.IsLinkingSupported ? D2D1CompileOptions.EnableLinking : 0));
ImmutableArray<byte> bytecode = ImmutableArray<byte>.Empty;
// No embedded shader was requested, or there were some errors earlier in the pipeline.
// In this case, skip the compilation, as diagnostic will be emitted for those anyway.
// Compiling would just add overhead and result in more errors, as the HLSL would be invalid.
if (sourceInfo is { HasErrors: true } or { ShaderProfile: null })
// We also skip compilation if no shader profile has been requested (we never just assume one).
if (hasErrors || requestedShaderProfile is null)
{
diagnostic = null;
goto End;
return HlslBytecodeInfo.Missing.Instance;
}
try
{
token.ThrowIfCancellationRequested();
// Compile the shader bytecode using the requested parameters
// Compile the shader bytecode using the effective parameters
using ComPtr<ID3DBlob> dxcBlobBytecode = D3DCompiler.Compile(
sourceInfo.HlslSource.AsSpan(),
sourceInfo.ShaderProfile.Value,
compileOptions);
hlslSource.AsSpan(),
effectiveShaderProfile,
effectiveCompileOptions);
token.ThrowIfCancellationRequested();
@@ -164,22 +182,18 @@ partial class ID2D1ShaderGenerator
byte[] array = new ReadOnlySpan<byte>(buffer, length).ToArray();
bytecode = Unsafe.As<byte[], ImmutableArray<byte>>(ref array);
diagnostic = null;
ImmutableArray<byte> bytecode = Unsafe.As<byte[], ImmutableArray<byte>>(ref array);
return new HlslBytecodeInfo.Success(bytecode);
}
catch (Win32Exception e)
{
compileOptions = default;
diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message));
return new HlslBytecodeInfo.Win32Error(FixupExceptionMessage(e.Message));
}
catch (FxcCompilationException e)
{
compileOptions = default;
diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithFxcCompilationException, FixupExceptionMessage(e.Message));
return new HlslBytecodeInfo.FxcError(FixupExceptionMessage(e.Message));
}
End:
return bytecode;
}
/// <summary>
+7 -5
ファイルの表示
@@ -113,9 +113,11 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
token.ThrowIfCancellationRequested();
// Get the shader profile and linking info for LoadBytecode()
D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(typeSymbol);
D2D1CompileOptions? compileOptions = LoadBytecode.GetCompileOptions(diagnostics, typeSymbol);
bool isLinkingSupported = LoadBytecode.IsSimpleInputShader(typeSymbol, inputCount);
D2D1ShaderProfile? requestedShaderProfile = LoadBytecode.GetRequestedShaderProfile(typeSymbol);
D2D1CompileOptions? requestedCompileOptions = LoadBytecode.GetRequestedCompileOptions(diagnostics, typeSymbol);
D2D1ShaderProfile effectiveShaderProfile = LoadBytecode.GetEffectiveShaderProfile(requestedShaderProfile);
D2D1CompileOptions effectiveCompileOptions = LoadBytecode.GetEffectiveCompileOptions(requestedCompileOptions, isLinkingSupported);
token.ThrowIfCancellationRequested();
@@ -150,8 +152,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
ResourceTextureDescriptions: resourceTextureDescriptions,
HlslShaderSource: new HlslShaderSourceInfo(
hlslSource,
shaderProfile,
compileOptions,
requestedShaderProfile,
requestedCompileOptions,
isLinkingSupported,
HasErrors: diagnostics.Count > 0),
BufferPrecision: bufferPrecision,
@@ -322,7 +324,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
shaderBytecodeInfo
.Select(static (item, token) =>
{
ImmutableArray<byte> bytecode = LoadBytecode.GetBytecode(
ImmutableArray<byte> bytecode = LoadBytecode.GetInfo(
item.Source,
token,
out D2D1ShaderProfile shaderProfile,
+16 -4
ファイルの表示
@@ -14,13 +14,25 @@ internal abstract record HlslBytecodeInfo
public sealed record Success(EquatableArray<byte> Bytecode) : HlslBytecodeInfo;
/// <summary>
/// An HLSL shader that failed to compile.
/// An HLSL shader that failed to compile due to a Win32 error.
/// </summary>
/// <param name="Message">The error message from compiling the shader.</param>
public sealed record Error(string Message) : HlslBytecodeInfo;
public sealed record Win32Error(string Message) : HlslBytecodeInfo;
/// <summary>
/// No HLSL bytecode is available (ie. compilation was not requested).
/// An HLSL shader that failed to compile due to an FXC compiler error.
/// </summary>
public sealed record Missing : HlslBytecodeInfo;
/// <param name="Message">The error message from compiling the shader.</param>
public sealed record FxcError(string Message) : HlslBytecodeInfo;
/// <summary>
/// No HLSL bytecode is available (ie. compilation was not requested or some other error was detected).
/// </summary>
public sealed record Missing : HlslBytecodeInfo
{
/// <summary>
/// Gets the shared <see cref="Missing"/> instance.
/// </summary>
public static Missing Instance { get; } = new();
}
}