Always generate hardcoded HLSL source

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-13 12:11:55 +02:00
1 親 249c708466
コミット 8e0aa437bc
5ファイル変更
+43 -167

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

+11 -68
ファイルの表示
@@ -1,5 +1,4 @@
using System.Collections.Immutable;
using System.Text;
using ComputeSharp.__Internals;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGenerators.Models;
@@ -83,73 +82,8 @@ partial class IShaderGenerator
{
using ImmutableArrayBuilder<StatementSyntax> statements = new();
StringBuilder textBuilder = new();
int prologueStatements = 0;
int sizeHint = 64;
void AppendLine(string text)
{
_ = textBuilder.Append(text);
}
void AppendParsedStatement(string text)
{
FlushText();
statements.Add(ParseStatement(text));
}
void FlushText()
{
if (textBuilder.Length > 0)
{
string hlslSource = textBuilder.ToString();
_ = textBuilder.Append(hlslSource);
sizeHint += textBuilder.Length;
// The indentation level is the current depth + 2 (the method, and the return expression)
SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth + 2);
statements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("builder"), IdentifierName("Append")))
.AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralExpression)))));
_ = textBuilder.Clear();
}
}
// Header and thread ids
AppendLine(hlslSourceInfo.HeaderAndThreadsX);
AppendParsedStatement("builder.Append(threadsX);");
AppendLine(hlslSourceInfo.ThreadsY);
AppendParsedStatement("builder.Append(threadsY);");
AppendLine(hlslSourceInfo.ThreadsZ);
AppendParsedStatement("builder.Append(threadsZ);");
// Define declarations
AppendLine(hlslSourceInfo.Defines);
// Static fields and declared types
AppendLine(hlslSourceInfo.StaticFieldsAndDeclaredTypes);
// Captured variables
AppendLine(hlslSourceInfo.CapturedFieldsAndResourcesAndForwardDeclarations);
// Captured methods
AppendLine(hlslSourceInfo.CapturedMethods);
// Entry point
AppendLine(hlslSourceInfo.EntryPoint);
FlushText();
// builder = global::ComputeSharp.__Internals.ArrayPoolStringBuilder.Create(<SIZE_HINT>);
statements.Insert(
prologueStatements,
statements.Add(
ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
@@ -162,7 +96,16 @@ partial class IShaderGenerator
.AddArgumentListArguments(
Argument(LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(sizeHint)))))));
Literal(hlslSourceInfo.HlslSource.Length)))))));
// The indentation level is the current depth + 2 (the method, and the return expression)
SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSourceInfo.HlslSource, hierarchyDepth + 2);
statements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("builder"), IdentifierName("Append")))
.AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralExpression)))));
return statements.ToImmutable();
}
+16 -56
ファイルの表示
@@ -15,8 +15,6 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
#pragma warning disable CS0618
namespace ComputeSharp.SourceGenerators;
/// <inheritdoc/>
@@ -34,12 +32,16 @@ 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="isImplicitTextureUsed">Indicates whether the current shader uses an implicit texture.</param>
/// <returns>The resulting info on the processed shader.</returns>
public static HlslShaderSourceInfo GetInfo(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
Compilation compilation,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol)
INamedTypeSymbol structDeclarationSymbol,
ThreadIdsInfo threadIds,
out bool isImplicitTextureUsed)
{
// Detect invalid properties
DetectAndReportInvalidPropertyDeclarations(diagnostics, structDeclarationSymbol);
@@ -71,8 +73,12 @@ partial class IShaderGenerator
ImmutableArray<(string Name, string Definition)> declaredTypes = GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes, instanceMethods);
ImmutableArray<(string Name, string Value)> definedConstants = GetDefinedConstants(constantDefinitions);
// Check whether an implicit texture is used in the shader
isImplicitTextureUsed = implicitTextureType is not null;
// Get the HLSL source data with the intermediate info
return GetHlslSourceInfo(
threadIds,
definedConstants,
declaredTypes,
resourceFields,
@@ -538,6 +544,7 @@ 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="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>
@@ -551,6 +558,7 @@ partial class IShaderGenerator
/// <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,
ImmutableArray<(string Name, string Value)> definedConstants,
ImmutableArray<(string Name, string Definition)> declaredTypes,
ImmutableArray<(string MetadataName, string Name, string HlslType)> resourceFields,
@@ -606,20 +614,11 @@ partial class IShaderGenerator
// Group size constants
AppendLF();
AppendLine("#define __GroupSize__get_X ");
string headerAndThreadsX = FlushText();
AppendLF();
AppendLineAndLF(threadIds.X.ToString());
AppendLine("#define __GroupSize__get_Y ");
string threadsY = FlushText();
AppendLF();
AppendLineAndLF(threadIds.Y.ToString());
AppendLine("#define __GroupSize__get_Z ");
string threadsZ = FlushText();
AppendLF();
AppendLineAndLF(threadIds.Z.ToString());
// Define declarations
foreach ((string name, string value) in definedConstants)
@@ -627,8 +626,6 @@ partial class IShaderGenerator
AppendLineAndLF($"#define {name} {value}");
}
string defines = FlushText();
// Static fields
if (staticFields.Any())
{
@@ -654,8 +651,6 @@ partial class IShaderGenerator
AppendLineAndLF(typeDefinition);
}
string staticFieldsAndDeclaredTypes = FlushText();
// Captured variables
AppendLF();
AppendLineAndLF("cbuffer _ : register(b0)");
@@ -733,8 +728,6 @@ partial class IShaderGenerator
AppendLineAndLF(forwardDeclaration);
}
string capturedFieldsAndResourcesAndForwardDeclarations = FlushText();
// Captured methods
foreach ((string _, string method) in processedMethods)
{
@@ -742,47 +735,14 @@ partial class IShaderGenerator
AppendLineAndLF(method);
}
string capturedMethods = FlushText();
// Entry point
AppendLF();
AppendLineAndLF("[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]");
AppendLineAndLF(executeMethod);
string entryPoint = FlushText();
string hlslSource = FlushText();
return new(
headerAndThreadsX,
threadsY,
threadsZ,
defines,
staticFieldsAndDeclaredTypes,
capturedFieldsAndResourcesAndForwardDeclarations,
capturedMethods,
entryPoint,
implicitTextureType,
isSamplerUsed,
declaredTypes.Select(static t => t.Name).ToImmutableArray(),
definedConstants.Select(static c => c.Name).ToImmutableArray(),
processedMethods.Select(static m => m.Signature).ToImmutableArray());
}
/// <summary>
/// Produces the non dynamic HLSL source (concatenating all generated HLSL source chunks).
/// </summary>
/// <param name="hlslSourceInfo">The input <see cref="HlslShaderSourceInfo"/> instance to use.</param>
/// <returns>The non dynamic HLSL source code.</returns>
public static string GetNonDynamicHlslSource(HlslShaderSourceInfo hlslSourceInfo)
{
return
hlslSourceInfo.HeaderAndThreadsX + "<THREADSX>" +
hlslSourceInfo.ThreadsY + "<THREADSY>" +
hlslSourceInfo.ThreadsZ + "<THREADSZ>" +
hlslSourceInfo.Defines +
hlslSourceInfo.StaticFieldsAndDeclaredTypes +
hlslSourceInfo.CapturedFieldsAndResourcesAndForwardDeclarations +
hlslSourceInfo.CapturedMethods +
hlslSourceInfo.EntryPoint;
return new(hlslSource, isSamplerUsed);
}
}
}
+1 -4
ファイルの表示
@@ -47,10 +47,7 @@ partial class IShaderGenerator
if (!structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName(typeof(EmbeddedBytecodeAttribute).FullName, out AttributeData? attribute))
{
// Emit the diagnostics if dynamic shaders are not supported
if (!supportsDynamicShaders)
{
diagnostics.Add(MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported, structDeclarationSymbol, structDeclarationSymbol);
}
diagnostics.Add(MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported, structDeclarationSymbol, structDeclarationSymbol);
return new(true, 0, 0, 0);
}
+13 -11
ファイルの表示
@@ -61,32 +61,34 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
token.ThrowIfCancellationRequested();
// TryGetBytecode() info
ThreadIdsInfo threadIds = LoadBytecode.GetInfo(
diagnostics,
typeSymbol,
false);
token.ThrowIfCancellationRequested();
// BuildHlslSource() info
HlslShaderSourceInfo hlslSourceInfo = BuildHlslSource.GetInfo(
diagnostics,
context.SemanticModel.Compilation,
typeDeclaration,
typeSymbol);
typeSymbol,
threadIds,
out bool isImplicitTextureUsed);
token.ThrowIfCancellationRequested();
// GetDispatchMetadata() info
DispatchMetadataInfo dispatchMetadataInfo = LoadDispatchMetadata.GetInfo(
root32BitConstantCount,
hlslSourceInfo.ImplicitTextureType is not null,
isImplicitTextureUsed,
hlslSourceInfo.IsSamplerUsed,
fieldInfos);
token.ThrowIfCancellationRequested();
// TryGetBytecode() info
ThreadIdsInfo threadIds = LoadBytecode.GetInfo(
diagnostics,
typeSymbol,
false);
token.ThrowIfCancellationRequested();
return new ShaderInfo(
Hierarchy: HierarchyInfo.From(typeSymbol),
DispatchData: new DispatchDataInfo(
@@ -155,7 +157,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
// Transform the raw HLSL source to compile (this step aggregates the HLSL source to ensure compilation is only done for actual HLSL changes)
IncrementalValuesProvider<(HierarchyInfo Hierarchy, string Hlsl, ThreadIdsInfo ThreadIds)> shaderBytecodeInfo =
shaderInfoWithErrors
.Select(static (item, token) => (item.Hierarchy, BuildHlslSource.GetNonDynamicHlslSource(item.HlslShaderSource), item.ThreadIds));
.Select(static (item, token) => (item.Hierarchy, item.HlslShaderSource.HlslSource, item.ThreadIds));
// Compile the requested shader bytecodes
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo EmbeddedBytecode, DeferredDiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors =
+2 -28
ファイルの表示
@@ -1,34 +1,8 @@
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model for extracted info on a processed HLSL shader.
/// </summary>
/// <param name="HeaderAndThreadsX">The shader generated header and <c>threadsX</c> count declaration.</param>
/// <param name="ThreadsY">The <c>threadsY</c> count declaration.</param>
/// <param name="ThreadsZ">The <c>threadsZ</c> count declaration.</param>
/// <param name="Defines">The define statements, if any.</param>
/// <param name="StaticFieldsAndDeclaredTypes">The static fields and declared types, if any.</param>
/// <param name="CapturedFieldsAndResourcesAndForwardDeclarations">The captured fields, and method forward declarations.</param>
/// <param name="CapturedMethods">The captured method implementations.</param>
/// <param name="EntryPoint">The shader entry point.</param>
/// <param name="ImplicitTextureType">The type of the implicit texture type, if any.</param>
/// <param name="HlslSource">The HLSL source for the shader.</param>
/// <param name="IsSamplerUsed">Whether or not the static sampler is used.</param>
/// <param name="DefinedTypes">The names of declared types.</param>
/// <param name="DefinedConstants">The names of defined constants.</param>
/// <param name="MethodSignatures">The signatures for captured methods.</param>
internal sealed record HlslShaderSourceInfo(
string HeaderAndThreadsX,
string ThreadsY,
string ThreadsZ,
string Defines,
string StaticFieldsAndDeclaredTypes,
string CapturedFieldsAndResourcesAndForwardDeclarations,
string CapturedMethods,
string EntryPoint,
string? ImplicitTextureType,
bool IsSamplerUsed,
EquatableArray<string> DefinedTypes,
EquatableArray<string> DefinedConstants,
EquatableArray<string> MethodSignatures);
internal sealed record HlslShaderSourceInfo(string HlslSource, bool IsSamplerUsed);