diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
index 1f9759b3..cb8a3946 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
@@ -138,28 +138,16 @@ partial class ID2D1ShaderGenerator
///
/// Gets the instance for the input shader info.
///
- /// The input HLSL source code.
- /// The requested shader profile, if available.
- /// The requested compile options, if available.
- /// The effective shader profile.
- /// The effective compile options.
- /// Whether any errors were produced when analyzing the shader.
+ /// The instance for the shader to compile.
/// The used to cancel the operation, if needed.
///
- 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 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));
}
}
+
+ ///
+ /// Gets any diagnostics from a processed instance.
+ ///
+ /// The input instance to process.
+ /// The source instance.
+ /// The collection of produced instances.
+ public static void GetInfoDiagnostics(
+ INamedTypeSymbol structDeclarationSymbol,
+ HlslBytecodeInfo info,
+ ImmutableArrayBuilder 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);
+ }
+ }
}
}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
index 8351f5b1..ba63bd0c 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
@@ -93,6 +93,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
out ImmutableArray inputComplexIndices,
out ImmutableArray 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 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)!;
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs
index b67295c7..1540b7a0 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs
@@ -13,14 +13,15 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// The effect category for the shader type, if available.
/// The effect author for the shader type, if available.
/// The size of the shader constant buffer.
-/// The description on shader instance fields.
/// The gathered input types for the shader.
+/// The gathered input descriptions for the shader.
/// The gathered resource texture descriptions for the shader.
-/// The processed HLSL source for the shader.
+/// The description on shader instance fields.
/// The buffer precision for the resulting output buffer.
/// The channel depth for the resulting output buffer.
-/// The gathered input descriptions for the shader.
/// The pixel options used by the shader.
+/// The key with processed info on the shader.
+/// The value with processed info on the shader.
/// The discovered diagnostics, if any.
internal sealed record D2D1ShaderInfo(
HierarchyInfo Hierarchy,
@@ -30,12 +31,13 @@ internal sealed record D2D1ShaderInfo(
string? EffectCategory,
string? EffectAuthor,
int ConstantBufferSizeInBytes,
- EquatableArray Fields,
EquatableArray InputTypes,
+ EquatableArray InputDescriptions,
EquatableArray ResourceTextureDescriptions,
- HlslShaderSourceInfo HlslShaderSource,
+ EquatableArray Fields,
D2D1BufferPrecision BufferPrecision,
D2D1ChannelDepth ChannelDepth,
- EquatableArray InputDescriptions,
D2D1PixelOptions PixelOptions,
+ HlslBytecodeInfoKey HlslInfoKey,
+ HlslBytecodeInfo HlslInfo,
EquatableArray Diagnostcs);
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfo.cs
index 0116c5e3..fc547efe 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfo.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfo.cs
@@ -16,8 +16,9 @@ internal abstract record HlslBytecodeInfo
///
/// An HLSL shader that failed to compile due to a Win32 error.
///
+ /// The HRESULT for the error.
/// The error message from compiling the shader.
- public sealed record Win32Error(string Message) : HlslBytecodeInfo;
+ public sealed record Win32Error(int HResult, string Message) : HlslBytecodeInfo;
///
/// An HLSL shader that failed to compile due to an FXC compiler error.
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfoKey.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfoKey.cs
new file mode 100644
index 00000000..772e9aea
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfoKey.cs
@@ -0,0 +1,18 @@
+namespace ComputeSharp.D2D1.SourceGenerators.Models;
+
+///
+/// A model with info to be a unique key for instances.
+///
+/// The input HLSL source code.
+/// The requested shader profile, if available.
+/// The requested compile options, if available.
+/// The effective shader profile.
+/// The effective compile options.
+/// Whether any errors were produced when analyzing the shader.
+internal sealed record HlslBytecodeInfoKey(
+ string HlslSource,
+ D2D1ShaderProfile? RequestedShaderProfile,
+ D2D1CompileOptions? RequestedCompileOptions,
+ D2D1ShaderProfile EffectiveShaderProfile,
+ D2D1CompileOptions EffectiveCompileOptions,
+ bool HasErrors);
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs
deleted file mode 100644
index d6dea5d5..00000000
--- a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs
+++ /dev/null
@@ -1,16 +0,0 @@
-namespace ComputeSharp.D2D1.SourceGenerators.Models;
-
-///
-/// A model for extracted info on a processed HLSL shader.
-///
-/// The HLSL source.
-/// The shader profile to use to compile the shader, if requested.
-/// The compile options to use to compile the shader.
-/// Whether the shader supports linking.
-/// Whether any errors have been detected, and therefore the shader compilation should be skipped.
-internal sealed record HlslShaderSourceInfo(
- string HlslSource,
- D2D1ShaderProfile? ShaderProfile,
- D2D1CompileOptions? CompileOptions,
- bool IsLinkingSupported,
- bool HasErrors);
\ No newline at end of file