Add and adopt HlslBytecodeInfoKey model

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-14 00:35:04 +02:00
1 親 8c3b9be08f
コミット af9338053b
5ファイル変更
+34 -14

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

@@ -26,7 +26,7 @@ partial class IShaderGenerator
writer.WriteLine("hlslSource =");
writer.IncreaseIndent();
writer.WriteLine("\"\"\"");
writer.Write(info.HlslSource, isMultiline: true);
writer.Write(info.HlslInfoKey.HlslSource, isMultiline: true);
writer.WriteLine("\"\"\";");
writer.DecreaseIndent();
}
+14 -6
ファイルの表示
@@ -11,6 +11,7 @@ using ComputeSharp.Shaders.Translation;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
@@ -41,12 +42,14 @@ partial class IShaderGenerator
/// <param name="threadsX">The thread ids value for the X axis.</param>
/// <param name="threadsY">The thread ids value for the Y axis.</param>
/// <param name="threadsZ">The thread ids value for the Z axis.</param>
/// <param name="isCompilationEnabled">Whether compilation should be attempted for the current info.</param>
public static void GetInfo(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
INamedTypeSymbol structDeclarationSymbol,
out int threadsX,
out int threadsY,
out int threadsZ)
out int threadsZ,
out bool isCompilationEnabled)
{
// Try to get the attribute that controls shader precompilation (has to be present when ComputeSharp.Dynamic is not referenced)
if (!structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName(typeof(EmbeddedBytecodeAttribute).FullName, out AttributeData? attribute))
@@ -55,6 +58,7 @@ partial class IShaderGenerator
diagnostics.Add(MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported, structDeclarationSymbol, structDeclarationSymbol);
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
return;
}
@@ -82,9 +86,12 @@ partial class IShaderGenerator
diagnostics.Add(InvalidEmbeddedBytecodeDispatchAxis, structDeclarationSymbol, structDeclarationSymbol);
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
return;
}
isCompilationEnabled = true;
}
else if (
attribute.ConstructorArguments.Length != 3 ||
@@ -99,6 +106,7 @@ partial class IShaderGenerator
diagnostics.Add(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol);
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
return;
}
@@ -107,20 +115,20 @@ partial class IShaderGenerator
threadsX = explicitThreadsX;
threadsY = explicitThreadsY;
threadsZ = explicitThreadsZ;
isCompilationEnabled = true;
}
}
/// <summary>
/// Gets the <see cref="HlslBytecodeInfo"/> instance for the input shader info.
/// </summary>
/// <param name="hlslSource">The generated HLSL source code (ignoring captured delegates, if present).</param>
/// <param name="isCompilationEnabled">Whether compilation is enabled.</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>The <see cref="HlslBytecodeInfo"/> instance for the current shader.</returns>
public static unsafe HlslBytecodeInfo GetBytecode(string hlslSource, bool isCompilationEnabled, CancellationToken token)
public static unsafe HlslBytecodeInfo GetBytecode(HlslBytecodeInfoKey key, CancellationToken token)
{
// Skip even attempting to compile if compilation is disabled (see comments in D2D1 generator)
if (!isCompilationEnabled)
if (!key.IsCompilationEnabled)
{
return HlslBytecodeInfo.Missing.Instance;
}
@@ -133,7 +141,7 @@ partial class IShaderGenerator
token.ThrowIfCancellationRequested();
// Compile the shader bytecode
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.Instance.Compile(hlslSource.AsSpan());
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.Instance.Compile(key.HlslSource.AsSpan());
token.ThrowIfCancellationRequested();
+7 -5
ファイルの表示
@@ -70,7 +70,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
typeSymbol,
out int threadsX,
out int threadsY,
out int threadsZ);
out int threadsZ,
out bool isCompilationEnabled);
token.ThrowIfCancellationRequested();
@@ -98,10 +99,11 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
token.ThrowIfCancellationRequested();
// Like for D2D1 shaders, disable compilation if any errors are present
bool isCompilationEnabled = diagnostics.WrittenSpan.IsEmpty;
// Prepare the lookup key for the HLSL shader bytecode
HlslBytecodeInfoKey hlslInfoKey = new(hlslSource, isCompilationEnabled);
HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(hlslSource, isCompilationEnabled, token);
// Try to get the HLSL bytecode
HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(hlslInfoKey, token);
token.ThrowIfCancellationRequested();
@@ -124,7 +126,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
Fields: fieldInfos,
ResourceDescriptors: resourceDescriptors,
Root32BitConstantCount: root32BitConstantCount,
HlslSource: hlslSource,
HlslInfoKey: hlslInfoKey,
HlslInfo: hlslInfo,
Diagnostcs: diagnostics.ToImmutable());
})
+10
ファイルの表示
@@ -0,0 +1,10 @@
using ComputeSharp.SourceGeneration.Models;
namespace ComputeSharp.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="IsCompilationEnabled">Whether compilation should be attempted for the current info.</param>
internal sealed record HlslBytecodeInfoKey(string HlslSource, bool IsCompilationEnabled);
+2 -2
ファイルの表示
@@ -15,7 +15,7 @@ namespace ComputeSharp.SourceGenerators.Models;
/// <param name="Fields">The description on shader instance fields.</param>
/// <param name="ResourceDescriptors">The sequence of resource descriptors for the shader.</param>
/// <param name="Root32BitConstantCount">The size of the shader root signature, in 32 bit constants.</param>
/// <param name="HlslSource">The HLSL source for 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 ShaderInfo(
@@ -28,6 +28,6 @@ internal sealed record ShaderInfo(
EquatableArray<FieldInfo> Fields,
EquatableArray<ResourceDescriptor> ResourceDescriptors,
int Root32BitConstantCount,
string HlslSource,
HlslBytecodeInfoKey HlslInfoKey,
HlslBytecodeInfo HlslInfo,
EquatableArray<DiagnosticInfo> Diagnostcs);