From fd9d8a6f658dc4e379bd3da2064a1647dc40bd5a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 16 Apr 2022 13:59:08 +0200 Subject: [PATCH] Disable shader compilation if any errors are present --- .../ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs | 7 +++++-- .../ID2D1ShaderGenerator.cs | 4 +++- .../Models/HlslShaderSourceInfo.cs | 3 ++- src/ComputeSharp.SourceGenerators/IShaderGenerator.cs | 7 +++++++ src/ComputeSharp.SourceGenerators/Models/ThreadIdsInfo.cs | 2 +- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index 6260c545..0682d904 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -78,8 +78,11 @@ partial class ID2D1ShaderGenerator { ImmutableArray bytecode = ImmutableArray.Empty; - // No embedded shader was requested - if (sourceInfo.ShaderProfile is null) + // 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.HasErrors || + sourceInfo.ShaderProfile is null) { diagnostic = null; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index fa125b74..5d7dd509 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -78,11 +78,13 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Get the shader profile and linking info for LoadBytecode() D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(item.Left.Symbol); bool isLinkingSupported = LoadBytecode.IsSimpleInputShader(item.Left.Symbol); + bool hasErrors = !dispatchDataDiagnostics.IsDefaultOrEmpty || !hlslSourceDiagnostics.IsDefaultOrEmpty; HlslShaderSourceInfo sourceInfo = new( hlslSource, shaderProfile, - isLinkingSupported); + isLinkingSupported, + hasErrors); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs index ff513d26..76f7c60d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs @@ -6,4 +6,5 @@ /// The HLSL source. /// The shader profile to use to compile the shader, if requested. /// Whether the shader supports linking. -internal sealed record HlslShaderSourceInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, bool IsLinkingSupported); +/// Whether any errors have been detected, and therefore the shader compilation should be skipped. +internal sealed record HlslShaderSourceInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, bool IsLinkingSupported, bool HasErrors); diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 569a931b..4a93c255 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -119,6 +119,13 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); + // Ensure the bytecode generation is disabled if any errors are present + if (!dispatchDataDiagnostics.IsDefaultOrEmpty || + !hlslSourceDiagnostics.IsDefaultOrEmpty) + { + threadIds = new ThreadIdsInfo(true, 0, 0, 0); + } + return ( new Result(dispatchDataInfo, dispatchDataDiagnostics), new Result(hlslSourceInfo, hlslSourceDiagnostics), diff --git a/src/ComputeSharp.SourceGenerators/Models/ThreadIdsInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ThreadIdsInfo.cs index 08b05eee..4bca599b 100644 --- a/src/ComputeSharp.SourceGenerators/Models/ThreadIdsInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/ThreadIdsInfo.cs @@ -3,7 +3,7 @@ /// /// A model representing gathered info on a embedded thread ids. /// -/// Whether the values are diascarded. +/// Whether the values are discarded. /// The thread ids value for the X axis. /// The thread ids value for the Y axis. /// The thread ids value for the Z axis.