Use forward declarations wherever possible

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-13 14:22:25 +01:00
1 親 bd8e881055
コミット fb3439556c
4ファイル変更
+99 -32

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

+25 -9
ファイルの表示
@@ -97,12 +97,14 @@ partial class D2DPixelShaderDescriptorGenerator
token.ThrowIfCancellationRequested();
// Process the discovered types and constants
ImmutableArray<HlslUserType> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes(
HlslDefinitionsSyntaxProcessor.GetDeclaredTypes(
diagnostics,
structDeclarationSymbol,
discoveredTypes,
instanceMethods,
constructors);
constructors,
out ImmutableArray<HlslUserType> typeDeclarations,
out ImmutableArray<string> typeMethodDeclarations);
token.ThrowIfCancellationRequested();
@@ -125,11 +127,12 @@ partial class D2DPixelShaderDescriptorGenerator
// Get the HLSL source
return GetHlslSource(
definedConstants,
declaredTypes,
valueFields,
resourceTextureFields,
staticFields,
processedMethods,
typeDeclarations,
typeMethodDeclarations,
entryPoint,
inputCount,
inputSimpleIndices,
@@ -449,11 +452,12 @@ partial class D2DPixelShaderDescriptorGenerator
/// Produces the series of statements to build the current HLSL source.
/// </summary>
/// <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="valueFields">The sequence of value instance fields for the current shader.</param>
/// <param name="resourceTextureFields">The sequence of captured resource textures for the current shader.</param>
/// <param name="staticFields">The sequence of static fields referenced by the shader.</param>
/// <param name="processedMethods">The sequence of processed methods used by the shader.</param>
/// <param name="typeDeclarations">The collection of declarations of all custom types.</param>
/// <param name="typeMethodDeclarations">The collection of implementations of all methods in all custom types.</param>
/// <param name="executeMethod">The body of the entry point of the shader.</param>
/// <param name="inputCount">The number of shader inputs to declare.</param>
/// <param name="inputSimpleIndices">The indicess of the simple shader inputs.</param>
@@ -462,11 +466,12 @@ partial class D2DPixelShaderDescriptorGenerator
/// <returns>The series of statements to build the HLSL source to compile to execute the current shader.</returns>
private static string GetHlslSource(
ImmutableArray<HlslConstant> definedConstants,
ImmutableArray<HlslUserType> declaredTypes,
ImmutableArray<HlslValueField> valueFields,
ImmutableArray<HlslResourceTextureField> resourceTextureFields,
ImmutableArray<HlslStaticField> staticFields,
ImmutableArray<HlslMethod> processedMethods,
ImmutableArray<HlslUserType> typeDeclarations,
ImmutableArray<string> typeMethodDeclarations,
string executeMethod,
int inputCount,
ImmutableArray<int> inputSimpleIndices,
@@ -513,6 +518,15 @@ partial class D2DPixelShaderDescriptorGenerator
writer.WriteLine(skipIfPresent: true);
// Forward declarations of shader/static methods
foreach (HlslMethod method in processedMethods)
{
writer.WriteLine(skipIfPresent: true);
writer.WriteLine(method.Signature);
}
writer.WriteLine(skipIfPresent: true);
// Static fields
foreach (HlslStaticField field in staticFields)
{
@@ -526,8 +540,10 @@ partial class D2DPixelShaderDescriptorGenerator
}
}
writer.WriteLine(skipIfPresent: true);
// Declared types
foreach (HlslUserType userType in declaredTypes)
foreach (HlslUserType userType in typeDeclarations)
{
writer.WriteLine(skipIfPresent: true);
writer.WriteLine(userType.Definition);
@@ -549,11 +565,11 @@ partial class D2DPixelShaderDescriptorGenerator
writer.WriteLine($"SamplerState __sampler__{field.Name} : register(s{field.Index});");
}
// Forward declarations
foreach (HlslMethod method in processedMethods)
// Method declarations for discovered types
foreach (string method in typeMethodDeclarations)
{
writer.WriteLine(skipIfPresent: true);
writer.WriteLine(method.Signature);
writer.WriteLine(method);
}
// Captured methods
@@ -143,15 +143,19 @@ internal static class HlslDefinitionsSyntaxProcessor
/// <param name="types">The sequence of discovered custom types.</param>
/// <param name="instanceMethods">The collection of discovered instance methods for custom struct types.</param>
/// <param name="constructors">The collection of discovered constructors for custom struct types.</param>
/// <returns>A sequence of custom type definitions to add to the shader source.</returns>
public static ImmutableArray<HlslUserType> GetDeclaredTypes(
/// <param name="typeDeclarations">The collection of declarations of all custom types.</param>
/// <param name="methodDeclarations">The collection of implementations of all methods in all custom types.</param>
public static void GetDeclaredTypes(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
INamedTypeSymbol structDeclarationSymbol,
IEnumerable<INamedTypeSymbol> types,
IReadOnlyDictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods,
IReadOnlyDictionary<IMethodSymbol, (MethodDeclarationSyntax, MethodDeclarationSyntax)> constructors)
IReadOnlyDictionary<IMethodSymbol, (MethodDeclarationSyntax, MethodDeclarationSyntax)> constructors,
out ImmutableArray<HlslUserType> typeDeclarations,
out ImmutableArray<string> methodDeclarations)
{
using ImmutableArrayBuilder<HlslUserType> builder = new();
using ImmutableArrayBuilder<HlslUserType> typeDeclarationsBuilder = new();
using ImmutableArrayBuilder<string> methodDeclarationsBuilder = new();
IReadOnlyCollection<INamedTypeSymbol> invalidTypes;
@@ -223,11 +227,33 @@ internal static class HlslDefinitionsSyntaxProcessor
}
}
// Declare the methods of the current type
structDeclaration = structDeclaration.WithMembers(structDeclaration.Members.AddRange(GatherInstanceMethods(type, instanceMethods, constructors)));
using (ImmutableArrayBuilder<MethodDeclarationSyntax> methodDefinitions = new())
{
// Forward declarations for all methods, and implementations.
// We need these to ensure things work with arbitrary ordering.
foreach (MethodDeclarationSyntax methodDeclaration in GatherInstanceMethods(type, instanceMethods, constructors))
{
methodDefinitions.Add(methodDeclaration.AsDefinition());
// We need to normalize the whitespaces here, as this methhod declaration will not be added to
// the list of members for the struct declaration, but rather it will be written directly into
// the resulting HLSL source (as the implementation of this forward declaration). For the same
// reason, we also need to change the identifier to also include the containing type with '::'.
string methodImplementation =
methodDeclaration
.WithIdentifier(Identifier($"{structType}::{methodDeclaration.Identifier.Text}"))
.NormalizeWhitespace(eol: "\n")
.ToFullString();
methodDeclarationsBuilder.Add(methodImplementation);
}
// Add all method forward declarations to the current type
structDeclaration = structDeclaration.WithMembers(structDeclaration.Members.AddRange(methodDefinitions.AsEnumerable()));
}
// Insert the trailing ; right after the closing bracket (after normalization)
builder.Add((
typeDeclarationsBuilder.Add((
structType,
structDeclaration
.NormalizeWhitespace(eol: "\n")
@@ -241,7 +267,8 @@ internal static class HlslDefinitionsSyntaxProcessor
diagnostics.Add(InvalidDiscoveredType, structDeclarationSymbol, structDeclarationSymbol, invalidType);
}
return builder.ToImmutable();
typeDeclarations = typeDeclarationsBuilder.ToImmutable();
methodDeclarations = methodDeclarationsBuilder.ToImmutable();
}
/// <summary>
+33 -9
ファイルの表示
@@ -114,12 +114,14 @@ partial class ComputeShaderDescriptorGenerator
token.ThrowIfCancellationRequested();
// Process the discovered types and constants
ImmutableArray<HlslUserType> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes(
HlslDefinitionsSyntaxProcessor.GetDeclaredTypes(
diagnostics,
structDeclarationSymbol,
discoveredTypes,
instanceMethods,
constructors);
constructors,
out ImmutableArray<HlslUserType> typeDeclarations,
out ImmutableArray<string> typeMethodDeclarations);
token.ThrowIfCancellationRequested();
@@ -136,12 +138,13 @@ partial class ComputeShaderDescriptorGenerator
threadsY,
threadsZ,
definedConstants,
declaredTypes,
resourceFields,
valueFields,
staticFields,
sharedBuffers,
processedMethods,
typeDeclarations,
typeMethodDeclarations,
isComputeShader,
implicitTextureType,
isSamplerUsed,
@@ -487,12 +490,13 @@ partial class ComputeShaderDescriptorGenerator
/// <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>
/// <param name="valueFields">The sequence of value instance fields for the current shader.</param>
/// <param name="staticFields">The sequence of static fields referenced by the shader.</param>
/// <param name="sharedBuffers">The sequence of shared buffers declared by the shader.</param>
/// <param name="processedMethods">The sequence of processed methods used by the shader.</param>
/// <param name="typeDeclarations">The collection of declarations of all custom types.</param>
/// <param name="typeMethodDeclarations">The collection of implementations of all methods in all custom types.</param>
/// <param name="isComputeShader">Whether or not the current shader type is a compute shader.</param>
/// <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>
@@ -503,12 +507,13 @@ partial class ComputeShaderDescriptorGenerator
int threadsY,
int threadsZ,
ImmutableArray<HlslConstant> definedConstants,
ImmutableArray<HlslUserType> declaredTypes,
ImmutableArray<HlslResourceField> resourceFields,
ImmutableArray<HlslValueField> valueFields,
ImmutableArray<HlslStaticField> staticFields,
ImmutableArray<HlslSharedBuffer> sharedBuffers,
ImmutableArray<HlslMethod> processedMethods,
ImmutableArray<HlslUserType> typeDeclarations,
ImmutableArray<string> typeMethodDeclarations,
bool isComputeShader,
string? implicitTextureType,
bool isSamplerUsed,
@@ -539,6 +544,23 @@ partial class ComputeShaderDescriptorGenerator
writer.WriteLine(skipIfPresent: true);
// Forward declarations of discovered types
foreach (HlslUserType userType in typeDeclarations)
{
writer.WriteLine($"struct {userType.Name};");
}
writer.WriteLine(skipIfPresent: true);
// Forward declarations of shader/static methods
foreach (HlslMethod method in processedMethods)
{
writer.WriteLine(skipIfPresent: true);
writer.WriteLine(method.Signature);
}
writer.WriteLine(skipIfPresent: true);
// Static fields
foreach (HlslStaticField field in staticFields)
{
@@ -552,8 +574,10 @@ partial class ComputeShaderDescriptorGenerator
}
}
writer.WriteLine(skipIfPresent: true);
// Declared types
foreach (HlslUserType userType in declaredTypes)
foreach (HlslUserType userType in typeDeclarations)
{
writer.WriteLine(skipIfPresent: true);
writer.WriteLine(userType.Definition);
@@ -621,11 +645,11 @@ partial class ComputeShaderDescriptorGenerator
writer.WriteLine($"groupshared {buffer.Type} {buffer.Name} [{count}];");
}
// Forward declarations
foreach (HlslMethod method in processedMethods)
// Method declarations for discovered types
foreach (string method in typeMethodDeclarations)
{
writer.WriteLine(skipIfPresent: true);
writer.WriteLine(method.Signature);
writer.WriteLine(method);
}
// Captured methods
+6 -6
ファイルの表示
@@ -722,6 +722,10 @@ namespace ComputeSharp.Tests
#define __GroupSize__get_Y 1
#define __GroupSize__get_Z 1
static void Foo(in int a, in int b, inout int c, out int d);
static void Bar(in int a, in int b, inout int c, out int d);
cbuffer _ : register(b0)
{
uint __x;
@@ -731,10 +735,6 @@ namespace ComputeSharp.Tests
RWStructuredBuffer<float> __reserved__buffer : register(u0);
static void Foo(in int a, in int b, inout int c, out int d);
static void Bar(in int a, in int b, inout int c, out int d);
static void Foo(in int a, in int b, inout int c, out int d)
{
d = 0;
@@ -805,6 +805,8 @@ namespace ComputeSharp.Tests
#define __GroupSize__get_Z 1
#define __ComputeSharp_Tests_ShaderCompilerTests_ShaderWithPartialDeclarations__a 2
static int Sum(int x, int y);
static const int b = 4;
cbuffer _ : register(b0)
@@ -816,8 +818,6 @@ namespace ComputeSharp.Tests
RWStructuredBuffer<float> __reserved__buffer : register(u0);
static int Sum(int x, int y);
static int Sum(int x, int y)
{
return x + y;