Streamline ShaderInfo model, remove secondary models

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

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

@@ -26,7 +26,7 @@ partial class IShaderGenerator
writer.WriteLine("hlslSource =");
writer.IncreaseIndent();
writer.WriteLine("\"\"\"");
writer.Write(info.HlslShaderSource.HlslSource, isMultiline: true);
writer.Write(info.HlslSource, isMultiline: true);
writer.WriteLine("\"\"\";");
writer.DecreaseIndent();
}
+29 -27
ファイルの表示
@@ -7,7 +7,6 @@ using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGeneration.SyntaxRewriters;
using ComputeSharp.SourceGenerators.Models;
using ComputeSharp.SourceGenerators.SyntaxRewriters;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@@ -32,16 +31,23 @@ partial class IShaderGenerator
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> node to process.</param>
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for <paramref name="structDeclaration"/>.</param>
/// <param name="threadIds">The info on the shader thread count size to use.</param>
/// <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="isImplicitTextureUsed">Indicates whether the current shader uses an implicit texture.</param>
/// <returns>The resulting info on the processed shader.</returns>
public static HlslShaderSourceInfo GetInfo(
/// <param name="isSamplerUsed">Whether or not the static sampler is used.</param>
/// <param name="hlslSource">The resulting HLSL source for the current shader.</param>
public static void GetInfo(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
Compilation compilation,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
ThreadIdsInfo threadIds,
out bool isImplicitTextureUsed)
int threadsX,
int threadsY,
int threadsZ,
out bool isImplicitTextureUsed,
out bool isSamplerUsed,
out string hlslSource)
{
// Detect invalid properties
DetectAndReportInvalidPropertyDeclarations(diagnostics, structDeclarationSymbol);
@@ -65,7 +71,7 @@ partial class IShaderGenerator
string? implicitTextureType = isComputeShader ? null : HlslKnownTypes.GetMappedNameForPixelShaderType(pixelShaderSymbol!);
(ImmutableArray<(string MetadataName, string Name, string HlslType)> resourceFields, ImmutableArray<(string Name, string HlslType)> valueFields) = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes, isComputeShader);
ImmutableArray<(string Name, string Type, int? Count)> sharedBuffers = GetSharedBuffers(diagnostics, structDeclarationSymbol, discoveredTypes);
(string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods, bool isSamplerUsed) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, isComputeShader);
(string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods, isSamplerUsed) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, isComputeShader);
(string, string)? implicitSamplerField = isSamplerUsed ? ("SamplerState", "__sampler") : default((string, string)?);
ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
@@ -77,8 +83,10 @@ partial class IShaderGenerator
isImplicitTextureUsed = implicitTextureType is not null;
// Get the HLSL source data with the intermediate info
return GetHlslSourceInfo(
threadIds,
hlslSource = GetHlslSourceInfo(
threadsX,
threadsY,
threadsZ,
definedConstants,
declaredTypes,
resourceFields,
@@ -544,7 +552,9 @@ partial class IShaderGenerator
/// <summary>
/// Produces the series of statements to build the current HLSL source.
/// </summary>
/// <param name="threadIds">The info on the shader thread count size to use.</param>
/// <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="definedConstants">The sequence of defined constants for the shader.</param>
/// <param name="declaredTypes">The sequence of declared types used by the shader.</param>
/// <param name="resourceFields">The sequence of resource instance fields for the current shader.</param>
@@ -556,9 +566,11 @@ partial class IShaderGenerator
/// <param name="implicitTextureType">The type of the implicit target texture, if present.</param>
/// <param name="isSamplerUsed">Whether the static sampler is used by the shader.</param>
/// <param name="executeMethod">The body of the entry point of the shader.</param>
/// <returns>The series of statements to build the HLSL source to compile to execute the current shader.</returns>
private static HlslShaderSourceInfo GetHlslSourceInfo(
ThreadIdsInfo threadIds,
/// <returns>The HLSL source for the current shader.</returns>
private static string GetHlslSourceInfo(
int threadsX,
int threadsY,
int threadsZ,
ImmutableArray<(string Name, string Value)> definedConstants,
ImmutableArray<(string Name, string Definition)> declaredTypes,
ImmutableArray<(string MetadataName, string Name, string HlslType)> resourceFields,
@@ -578,11 +590,6 @@ partial class IShaderGenerator
hlslBuilder.Add('\n');
}
void AppendLine(string text)
{
hlslBuilder.AddRange(text.AsSpan());
}
void AppendLineAndLF(string text)
{
hlslBuilder.AddRange(text.AsSpan());
@@ -613,12 +620,9 @@ partial class IShaderGenerator
// Group size constants
AppendLF();
AppendLine("#define __GroupSize__get_X ");
AppendLineAndLF(threadIds.X.ToString());
AppendLine("#define __GroupSize__get_Y ");
AppendLineAndLF(threadIds.Y.ToString());
AppendLine("#define __GroupSize__get_Z ");
AppendLineAndLF(threadIds.Z.ToString());
AppendLineAndLF($"#define __GroupSize__get_X {threadsX}");
AppendLineAndLF($"#define __GroupSize__get_Y {threadsY}");
AppendLineAndLF($"#define __GroupSize__get_Z {threadsZ}");
// Define declarations
foreach ((string name, string value) in definedConstants)
@@ -740,9 +744,7 @@ partial class IShaderGenerator
AppendLineAndLF("[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]");
AppendLineAndLF(executeMethod);
string hlslSource = FlushText();
return new(hlslSource, isSamplerUsed);
return FlushText();
}
}
}
+1 -1
ファイルの表示
@@ -31,7 +31,7 @@ partial class IShaderGenerator
}
// Check that the [numthreads] values match
writer.WriteLine($"if (threadsX == {info.ThreadIds.X} && threadsY == {info.ThreadIds.Y} && threadsZ == {info.ThreadIds.Z})");
writer.WriteLine($"if (threadsX == {info.ThreadsX} && threadsY == {info.ThreadsY} && threadsZ == {info.ThreadsZ})");
using (writer.WriteBlock())
{
+19 -27
ファイルの表示
@@ -11,7 +11,6 @@ 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;
@@ -39,12 +38,15 @@ partial class IShaderGenerator
/// </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>
/// <param name="supportsDynamicShaders">Indicates whether or not dynamic shaders are supported.</param>
/// <returns>The thread ids for the precompiled shader, if available.</returns>
public static ThreadIdsInfo GetInfo(
/// <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>
public static void GetInfo(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
INamedTypeSymbol structDeclarationSymbol,
bool supportsDynamicShaders)
out int threadsX,
out int threadsY,
out int threadsZ)
{
// 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))
@@ -52,12 +54,10 @@ partial class IShaderGenerator
// Emit the diagnostics if dynamic shaders are not supported
diagnostics.Add(MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported, structDeclarationSymbol, structDeclarationSymbol);
return new(true, 0, 0, 0);
}
threadsX = threadsY = threadsZ = 0;
int threadsX;
int threadsY;
int threadsZ;
return;
}
// Check for a dispatch axis argument first
if (attribute.ConstructorArguments.Length == 1)
@@ -81,7 +81,9 @@ partial class IShaderGenerator
{
diagnostics.Add(InvalidEmbeddedBytecodeDispatchAxis, structDeclarationSymbol, structDeclarationSymbol);
return new(true, 0, 0, 0);
threadsX = threadsY = threadsZ = 0;
return;
}
}
else if (
@@ -96,7 +98,9 @@ partial class IShaderGenerator
// Failed to validate the thread number arguments
diagnostics.Add(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol);
return new(true, 0, 0, 0);
threadsX = threadsY = threadsZ = 0;
return;
}
else
{
@@ -104,31 +108,19 @@ partial class IShaderGenerator
threadsY = explicitThreadsY;
threadsZ = explicitThreadsZ;
}
// Ensure the bytecode generation is disabled if any errors are present. This step is done last to ensure that
// all available diagnostics are still emitted correctly before reaching this point, instead of being disabled.
if (diagnostics.Count > 0)
{
return new(true, 0, 0, 0);
}
return new(false, threadsX, threadsY, threadsZ);
}
/// <summary>
/// Gets the <see cref="HlslBytecodeInfo"/> instance for the input shader info.
/// </summary>
/// <param name="threadIds">The input <see cref="ThreadIdsInfo"/> instance to process.</param>
/// <param name="hlslSource">The generated HLSL source code (ignoring captured delegates, if present).</param>
/// <param name="isCompilationEnabled">Whether compilation is enabled.</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(
ThreadIdsInfo threadIds,
string hlslSource,
CancellationToken token)
public static unsafe HlslBytecodeInfo GetBytecode(string hlslSource, bool isCompilationEnabled, CancellationToken token)
{
// Skip even attempting to compile if compilation is disabled (see comments in D2D1 generator)
if (threadIds.IsDefault)
if (!isCompilationEnabled)
{
return HlslBytecodeInfo.Missing.Instance;
}
@@ -24,9 +24,9 @@ partial class IShaderGenerator
using (writer.WriteBlock())
{
int numberOfResources = info.DispatchMetadata.ResourceDescriptors.Length;
int numberOfResources = info.ResourceDescriptors.Length;
writer.WriteLine($"global::System.Span<uint> span0 = stackalloc uint[{info.DispatchData.Root32BitConstantCount}];");
writer.WriteLine($"global::System.Span<uint> span0 = stackalloc uint[{info.Root32BitConstantCount}];");
writer.WriteLineIf($"global::System.Span<ulong> span1 = stackalloc ulong[{numberOfResources}];", numberOfResources > 0);
writer.WriteLine("ref uint r0 = ref span0[0];");
writer.WriteLineIf("ref ulong r1 = ref span1[0];", numberOfResources > 0);
@@ -34,10 +34,10 @@ partial class IShaderGenerator
// Append the statements for the dispatch ranges
writer.WriteLine("span0[0] = (uint)x;");
writer.WriteLine("span0[1] = (uint)y;");
writer.WriteLineIf("span0[2] = (uint)z;", !info.DispatchData.IsPixelShaderLike);
writer.WriteLineIf("span0[2] = (uint)z;", !info.IsPixelShaderLike);
// Generate loading statements for each captured field
foreach (FieldInfo fieldInfo in info.DispatchData.FieldInfos)
foreach (FieldInfo fieldInfo in info.Fields)
{
switch (fieldInfo)
{
@@ -26,7 +26,7 @@ partial class IShaderGenerator
using (writer.WriteBlock())
{
// Compute the total number of resources
int totalResourcesCount = info.DispatchMetadata.ResourceDescriptors.Length;
int totalResourcesCount = info.ResourceDescriptors.Length;
writer.WriteLine("global::System.Span<byte> span0 = stackalloc byte[5];");
writer.WriteLine($"global::System.Span<global::ComputeSharp.__Internals.ResourceDescriptor> span1 = stackalloc global::ComputeSharp.__Internals.ResourceDescriptor[{totalResourcesCount}];");
@@ -34,11 +34,11 @@ partial class IShaderGenerator
writer.WriteLine("ref global::ComputeSharp.__Internals.ResourceDescriptor r1 = ref span1[0];");
// Write the serialized shader metadata
writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned<int>(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 0), {info.DispatchMetadata.Root32BitConstantCount});");
writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned<bool>(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 4), {info.DispatchMetadata.IsSamplerUsed.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()});");
writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned<int>(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 0), {info.Root32BitConstantCount});");
writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned<bool>(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 4), {info.IsSamplerUsed.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()});");
// Populate the sequence of resource descriptors
foreach (ResourceDescriptor descriptor in info.DispatchMetadata.ResourceDescriptors)
foreach (ResourceDescriptor descriptor in info.ResourceDescriptors)
{
writer.WriteLine($"global::ComputeSharp.__Internals.ResourceDescriptor.Create({descriptor.TypeId}, {descriptor.RegisterOffset}, out global::System.Runtime.CompilerServices.Unsafe.Add(ref r1, {descriptor.Offset}));");
}
+2 -5
ファイルの表示
@@ -21,7 +21,7 @@ partial class IShaderGenerator
/// <param name="isSamplerUsed">Whether the static sampler is used by the shader.</param>
/// <param name="capturedFields">The sequence of captured fields for the shader.</param>
/// <returns>The metadata info for the shader.</returns>
public static DispatchMetadataInfo GetInfo(
public static ImmutableArray<ResourceDescriptor> GetInfo(
int root32BitConstantCount,
bool isImplicitTextureUsed,
bool isSamplerUsed,
@@ -57,10 +57,7 @@ partial class IShaderGenerator
}
}
return new(
root32BitConstantCount,
isSamplerUsed,
descriptors.ToImmutable());
return descriptors.ToImmutable();
}
}
}
+26 -16
ファイルの表示
@@ -65,34 +65,43 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
token.ThrowIfCancellationRequested();
// TryGetBytecode() info
ThreadIdsInfo threadIds = LoadBytecode.GetInfo(
LoadBytecode.GetInfo(
diagnostics,
typeSymbol,
false);
out int threadsX,
out int threadsY,
out int threadsZ);
token.ThrowIfCancellationRequested();
// BuildHlslSource() info
HlslShaderSourceInfo hlslSourceInfo = BuildHlslSource.GetInfo(
BuildHlslSource.GetInfo(
diagnostics,
context.SemanticModel.Compilation,
typeDeclaration,
typeSymbol,
threadIds,
out bool isImplicitTextureUsed);
threadsX,
threadsY,
threadsZ,
out bool isImplicitTextureUsed,
out bool isSamplerUsed,
out string hlslSource);
token.ThrowIfCancellationRequested();
// GetDispatchMetadata() info
DispatchMetadataInfo dispatchMetadataInfo = LoadDispatchMetadata.GetInfo(
ImmutableArray<ResourceDescriptor> resourceDescriptors = LoadDispatchMetadata.GetInfo(
root32BitConstantCount,
isImplicitTextureUsed,
hlslSourceInfo.IsSamplerUsed,
isSamplerUsed,
fieldInfos);
token.ThrowIfCancellationRequested();
HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(threadIds, hlslSourceInfo.HlslSource, token);
// Like for D2D1 shaders, disable compilation if any errors are present
bool isCompilationEnabled = diagnostics.WrittenSpan.IsEmpty;
HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(hlslSource, isCompilationEnabled, token);
token.ThrowIfCancellationRequested();
@@ -107,15 +116,16 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
return new ShaderInfo(
Hierarchy: hierarchyInfo,
DispatchData: new DispatchDataInfo(
isPixelShaderLike,
fieldInfos,
resourceCount,
root32BitConstantCount),
DispatchMetadata: dispatchMetadataInfo,
HlslShaderSource: hlslSourceInfo,
ThreadsX: threadsX,
ThreadsY: threadsY,
ThreadsZ: threadsZ,
IsPixelShaderLike: isPixelShaderLike,
IsSamplerUsed: isSamplerUsed,
Fields: fieldInfos,
ResourceDescriptors: resourceDescriptors,
Root32BitConstantCount: root32BitConstantCount,
HlslSource: hlslSource,
HlslInfo: hlslInfo,
ThreadIds: threadIds,
Diagnostcs: diagnostics.ToImmutable());
})
.Where(static item => item is not null)!;
-16
ファイルの表示
@@ -1,16 +0,0 @@
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model representing gathered info on a shader dispatch data.
/// </summary>
/// <param name="IsPixelShaderLike">Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.</param>
/// <param name="FieldInfos">The description on shader instance fields.</param>
/// <param name="ResourceCount">The total number of captured resources.</param>
/// <param name="Root32BitConstantCount">The size of the shader root signature, in 32 bit constants.</param>
internal sealed record DispatchDataInfo(
bool IsPixelShaderLike,
EquatableArray<FieldInfo> FieldInfos,
int ResourceCount,
int Root32BitConstantCount);
-11
ファイルの表示
@@ -1,11 +0,0 @@
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model containing info on shader dispatch metadata.
/// </summary>
/// <param name="Root32BitConstantCount">The size of the shader root signature, in 32 bit constants.</param>
/// <param name="IsSamplerUsed">Whether or not the static sampler is used.</param>
/// <param name="ResourceDescriptors">The sequence of resource descriptors for the shader.</param>
internal sealed record DispatchMetadataInfo(int Root32BitConstantCount, bool IsSamplerUsed, EquatableArray<ResourceDescriptor> ResourceDescriptors);
-8
ファイルの表示
@@ -1,8 +0,0 @@
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model for extracted info on a processed HLSL shader.
/// </summary>
/// <param name="HlslSource">The HLSL source for the shader.</param>
/// <param name="IsSamplerUsed">Whether or not the static sampler is used.</param>
internal sealed record HlslShaderSourceInfo(string HlslSource, bool IsSamplerUsed);
+18 -8
ファイルの表示
@@ -7,17 +7,27 @@ namespace ComputeSharp.SourceGenerators.Models;
/// A model representing all necessary info for a full generation pass for a shader.
/// </summary>
/// <param name="Hierarchy">The hierarchy info for the shader type.</param>
/// <param name="DispatchData">The gathered shader dispatch data.</param>
/// <param name="DispatchMetadata">The gathered shader dispatch metadata.</param>
/// <param name="HlslShaderSource">The processed HLSL source for the shader.</param>
/// <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="IsPixelShaderLike">Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.</param>
/// <param name="IsSamplerUsed">Whether or not the static sampler is used.</param>
/// <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="HlslInfo">The value with processed info on the shader.</param>
/// <param name="ThreadIds">The thread ids for the shader, if compilation is requested.</param>
/// <param name="Diagnostcs">The discovered diagnostics, if any.</param>
internal sealed record ShaderInfo(
HierarchyInfo Hierarchy,
DispatchDataInfo DispatchData,
DispatchMetadataInfo DispatchMetadata,
HlslShaderSourceInfo HlslShaderSource,
int ThreadsX,
int ThreadsY,
int ThreadsZ,
bool IsPixelShaderLike,
bool IsSamplerUsed,
EquatableArray<FieldInfo> Fields,
EquatableArray<ResourceDescriptor> ResourceDescriptors,
int Root32BitConstantCount,
string HlslSource,
HlslBytecodeInfo HlslInfo,
ThreadIdsInfo ThreadIds,
EquatableArray<DiagnosticInfo> Diagnostcs);
-10
ファイルの表示
@@ -1,10 +0,0 @@
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model representing gathered info on a embedded thread ids.
/// </summary>
/// <param name="IsDefault">Whether the values are discarded.</param>
/// <param name="X">The thread ids value for the X axis.</param>
/// <param name="Y">The thread ids value for the Y axis.</param>
/// <param name="Z">The thread ids value for the Z axis.</param>
internal sealed record ThreadIdsInfo(bool IsDefault, int X, int Y, int Z);