Add HlslBytecodeInfoKey and initial setup

このコミットが含まれているのは:
Sergio Pedri committed 2023-09-28 23:56:33 +02:00
1 親 44849d5d3e
コミット c3b64a90cd
6ファイル変更
+107 -59

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

+41 -19
ファイルの表示
@@ -138,28 +138,16 @@ partial class ID2D1ShaderGenerator
/// <summary>
/// Gets the <see cref="HlslBytecodeInfo"/> instance for the input shader info.
/// </summary>
/// <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="key">The <see cref="HlslBytecodeInfoKey"/> instance for the shader to compile.</param>
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
/// <returns></returns>
public static unsafe HlslBytecodeInfo GetInfo(
string hlslSource,
D2D1ShaderProfile? requestedShaderProfile,
D2D1CompileOptions? requestedCompileOptions,
D2D1ShaderProfile effectiveShaderProfile,
D2D1CompileOptions effectiveCompileOptions,
bool hasErrors,
CancellationToken token)
public static unsafe HlslBytecodeInfo GetInfo(HlslBytecodeInfoKey key, CancellationToken token)
{
// 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.
// We also skip compilation if no shader profile has been requested (we never just assume one).
if (hasErrors || requestedShaderProfile is null)
if (key.HasErrors || key.RequestedShaderProfile is null)
{
return HlslBytecodeInfo.Missing.Instance;
}
@@ -170,9 +158,9 @@ partial class ID2D1ShaderGenerator
// Compile the shader bytecode using the effective parameters
using ComPtr<ID3DBlob> dxcBlobBytecode = D3DCompiler.Compile(
hlslSource.AsSpan(),
effectiveShaderProfile,
effectiveCompileOptions);
key.HlslSource.AsSpan(),
key.EffectiveShaderProfile,
key.EffectiveCompileOptions);
token.ThrowIfCancellationRequested();
@@ -187,12 +175,46 @@ partial class ID2D1ShaderGenerator
}
catch (Win32Exception e)
{
return new HlslBytecodeInfo.Win32Error(D3DCompiler.PrettifyFxcErrorMessage(e.Message));
return new HlslBytecodeInfo.Win32Error(e.NativeErrorCode, D3DCompiler.PrettifyFxcErrorMessage(e.Message));
}
catch (FxcCompilationException e)
{
return new HlslBytecodeInfo.FxcError(D3DCompiler.PrettifyFxcErrorMessage(e.Message));
}
}
/// <summary>
/// Gets any diagnostics from a processed <see cref="HlslBytecodeInfo"/> instance.
/// </summary>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="info">The source <see cref="HlslBytecodeInfo"/> instance.</param>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
public static void GetInfoDiagnostics(
INamedTypeSymbol structDeclarationSymbol,
HlslBytecodeInfo info,
ImmutableArrayBuilder<DiagnosticInfo> diagnostics)
{
DiagnosticInfo? diagnostic = null;
if (info is HlslBytecodeInfo.Win32Error win32Error)
{
diagnostic = DiagnosticInfo.Create(
EmbeddedBytecodeFailedWithWin32Exception,
structDeclarationSymbol,
new object[] { structDeclarationSymbol, win32Error.HResult, win32Error.Message });
}
else if (info is HlslBytecodeInfo.FxcError fxcError)
{
diagnostic = DiagnosticInfo.Create(
EmbeddedBytecodeFailedWithFxcCompilationException,
structDeclarationSymbol,
new object[] { structDeclarationSymbol, fxcError.Message });
}
if (diagnostic is not null)
{
diagnostics.Add(diagnostic);
}
}
}
}
+38 -17
ファイルの表示
@@ -93,6 +93,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
out ImmutableArray<int> inputComplexIndices,
out ImmutableArray<uint> inputTypes);
token.ThrowIfCancellationRequested();
// Get the resource texture info for ResourceTextureDescriptions
ResourceTextureDescriptions.GetInfo(
diagnostics,
@@ -100,6 +102,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
inputCount,
out ImmutableArray<ResourceTextureDescription> resourceTextureDescriptions);
token.ThrowIfCancellationRequested();
// Get HLSL source for HlslSource
string hlslSource = HlslSource.GetHlslSource(
diagnostics,
@@ -112,15 +116,6 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
token.ThrowIfCancellationRequested();
// Get the shader profile and linking info for LoadBytecode()
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();
// Get the info for the output buffer properties
OutputBuffer.GetInfo(typeSymbol, out D2D1BufferPrecision bufferPrecision, out D2D1ChannelDepth channelDepth);
@@ -139,6 +134,36 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
token.ThrowIfCancellationRequested();
// Get the shader profile and linking info for LoadBytecode()
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);
bool hasErrors = diagnostics.Count > 0;
token.ThrowIfCancellationRequested();
// As the last steps in the pipeline, try to compile the shader if needed.
// This is done last so that it can be skipped if any errors happened before.
HlslBytecodeInfoKey hlslInfoKey = new(
hlslSource,
requestedShaderProfile,
requestedCompileOptions,
effectiveShaderProfile,
effectiveCompileOptions,
hasErrors);
// TODO: cache this across transform runs
HlslBytecodeInfo hlslInfo = LoadBytecode.GetInfo(hlslInfoKey, token);
token.ThrowIfCancellationRequested();
// Append any diagnostic for the shader compilation
LoadBytecode.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics);
token.ThrowIfCancellationRequested();
return new D2D1ShaderInfo(
Hierarchy: HierarchyInfo.From(typeSymbol),
EffectId: effectId,
@@ -147,19 +172,15 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
EffectCategory: effectCategory,
EffectAuthor: effectAuthor,
ConstantBufferSizeInBytes: constantBufferSizeInBytes,
Fields: fieldInfos,
InputTypes: inputTypes,
InputDescriptions: inputDescriptions,
ResourceTextureDescriptions: resourceTextureDescriptions,
HlslShaderSource: new HlslShaderSourceInfo(
hlslSource,
requestedShaderProfile,
requestedCompileOptions,
isLinkingSupported,
HasErrors: diagnostics.Count > 0),
Fields: fieldInfos,
BufferPrecision: bufferPrecision,
ChannelDepth: channelDepth,
InputDescriptions: inputDescriptions,
PixelOptions: pixelOptions,
HlslInfoKey: hlslInfoKey,
HlslInfo: hlslInfo,
Diagnostcs: diagnostics.ToImmutable());
})
.Where(static item => item is not null)!;
+8 -6
ファイルの表示
@@ -13,14 +13,15 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// <param name="EffectCategory">The effect category for the shader type, if available.</param>
/// <param name="EffectAuthor">The effect author for the shader type, if available.</param>
/// <param name="ConstantBufferSizeInBytes">The size of the shader constant buffer.</param>
/// <param name="Fields">The description on shader instance fields.</param>
/// <param name="InputTypes">The gathered input types for the shader.</param>
/// <param name="InputDescriptions">The gathered input descriptions for the shader.</param>
/// <param name="ResourceTextureDescriptions">The gathered resource texture descriptions for the shader.</param>
/// <param name="HlslShaderSource">The processed HLSL source for the shader.</param>
/// <param name="Fields">The description on shader instance fields.</param>
/// <param name="BufferPrecision">The buffer precision for the resulting output buffer.</param>
/// <param name="ChannelDepth">The channel depth for the resulting output buffer.</param>
/// <param name="InputDescriptions">The gathered input descriptions for the shader.</param>
/// <param name="PixelOptions">The pixel options used by the shader.</param>
/// <param name="HlslInfoKey">The key with processed info on the shader.</param>
/// <param name="HlslInfo">The value with processed info on the shader.</param>
/// <param name="Diagnostcs">The discovered diagnostics, if any.</param>
internal sealed record D2D1ShaderInfo(
HierarchyInfo Hierarchy,
@@ -30,12 +31,13 @@ internal sealed record D2D1ShaderInfo(
string? EffectCategory,
string? EffectAuthor,
int ConstantBufferSizeInBytes,
EquatableArray<FieldInfo> Fields,
EquatableArray<uint> InputTypes,
EquatableArray<InputDescription> InputDescriptions,
EquatableArray<ResourceTextureDescription> ResourceTextureDescriptions,
HlslShaderSourceInfo HlslShaderSource,
EquatableArray<FieldInfo> Fields,
D2D1BufferPrecision BufferPrecision,
D2D1ChannelDepth ChannelDepth,
EquatableArray<InputDescription> InputDescriptions,
D2D1PixelOptions PixelOptions,
HlslBytecodeInfoKey HlslInfoKey,
HlslBytecodeInfo HlslInfo,
EquatableArray<DiagnosticInfo> Diagnostcs);
+2 -1
ファイルの表示
@@ -16,8 +16,9 @@ internal abstract record HlslBytecodeInfo
/// <summary>
/// An HLSL shader that failed to compile due to a Win32 error.
/// </summary>
/// <param name="HResult">The HRESULT for the error.</param>
/// <param name="Message">The error message from compiling the shader.</param>
public sealed record Win32Error(string Message) : HlslBytecodeInfo;
public sealed record Win32Error(int HResult, string Message) : HlslBytecodeInfo;
/// <summary>
/// An HLSL shader that failed to compile due to an FXC compiler error.
+18
ファイルの表示
@@ -0,0 +1,18 @@
namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// <summary>
/// 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="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>
internal sealed record HlslBytecodeInfoKey(
string HlslSource,
D2D1ShaderProfile? RequestedShaderProfile,
D2D1CompileOptions? RequestedCompileOptions,
D2D1ShaderProfile EffectiveShaderProfile,
D2D1CompileOptions EffectiveCompileOptions,
bool HasErrors);
-16
ファイルの表示
@@ -1,16 +0,0 @@
namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// <summary>
/// A model for extracted info on a processed HLSL shader.
/// </summary>
/// <param name="HlslSource">The HLSL source.</param>
/// <param name="ShaderProfile">The shader profile to use to compile the shader, if requested.</param>
/// <param name="CompileOptions">The compile options to use to compile the shader.</param>
/// <param name="IsLinkingSupported">Whether the shader supports linking.</param>
/// <param name="HasErrors">Whether any errors have been detected, and therefore the shader compilation should be skipped.</param>
internal sealed record HlslShaderSourceInfo(
string HlslSource,
D2D1ShaderProfile? ShaderProfile,
D2D1CompileOptions? CompileOptions,
bool IsLinkingSupported,
bool HasErrors);