Refactor D3D12 generator files and helper types

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-15 21:28:03 +02:00
1 親 0f9b975336
コミット 2545c6c391
13ファイル変更
+227 -145

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

@@ -8,7 +8,7 @@ namespace ComputeSharp.SourceGenerators;
partial class ComputeShaderDescriptorGenerator
{
/// <inheritdoc/>
partial class LoadDispatchData
partial class DispatchDataLoading
{
/// <summary>
/// Writes the <c>LoadConstantBuffer</c> method.
@@ -18,9 +18,9 @@ namespace ComputeSharp.SourceGenerators;
partial class ComputeShaderDescriptorGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>LoadDispatchData</c> method.
/// A helper with all logic to generate the dispatch data loading methods.
/// </summary>
private static partial class LoadDispatchData
private static partial class DispatchDataLoading
{
/// <summary>
/// Explores a given type hierarchy and generates statements to load fields.
@@ -9,7 +9,7 @@ namespace ComputeSharp.SourceGenerators;
partial class ComputeShaderDescriptorGenerator
{
/// <inheritdoc/>
partial class LoadBytecode
partial class HlslBytecode
{
/// <summary>
/// Writes the <c>HlslBytecode</c> property.
@@ -25,9 +25,9 @@ namespace ComputeSharp.SourceGenerators;
partial class ComputeShaderDescriptorGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>LoadBytecode</c> method.
/// A helper with all logic to generate the <c>HlslBytecode</c> property.
/// </summary>
private static partial class LoadBytecode
private static partial class HlslBytecode
{
/// <summary>
/// The shared cache of <see cref="HlslBytecodeInfo"/> values.
@@ -39,91 +39,6 @@ partial class ComputeShaderDescriptorGenerator
/// </summary>
private static volatile bool areDxcLibrariesLoaded;
/// <summary>
/// Gets the thread ids values for a given shader type, if available.
/// </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="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 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))
{
// Emit the diagnostics if dynamic shaders are not supported
diagnostics.Add(MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported, structDeclarationSymbol, structDeclarationSymbol);
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
return;
}
// Check for a dispatch axis argument first
if (attribute.ConstructorArguments.Length == 1)
{
int dispatchAxis = (int)attribute.ConstructorArguments[0].Value!;
(threadsX, threadsY, threadsZ) = (DispatchAxis)dispatchAxis switch
{
DispatchAxis.X => (64, 1, 1),
DispatchAxis.Y => (1, 64, 1),
DispatchAxis.Z => (1, 1, 64),
DispatchAxis.XY => (8, 8, 1),
DispatchAxis.XZ => (8, 1, 8),
DispatchAxis.YZ => (1, 8, 8),
DispatchAxis.XYZ => (4, 4, 4),
_ => (0, 0, 0)
};
// Validate the dispatch axis argument
if ((threadsX, threadsY, threadsZ) is (0, 0, 0))
{
diagnostics.Add(InvalidEmbeddedBytecodeDispatchAxis, structDeclarationSymbol, structDeclarationSymbol);
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
return;
}
isCompilationEnabled = true;
}
else if (
attribute.ConstructorArguments.Length != 3 ||
attribute.ConstructorArguments[0].Value is not int explicitThreadsX ||
attribute.ConstructorArguments[1].Value is not int explicitThreadsY ||
attribute.ConstructorArguments[2].Value is not int explicitThreadsZ ||
explicitThreadsX is < 1 or > 1024 ||
explicitThreadsY is < 1 or > 1024 ||
explicitThreadsZ is < 1 or > 64)
{
// Failed to validate the thread number arguments
diagnostics.Add(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol);
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
return;
}
else
{
threadsX = explicitThreadsX;
threadsY = explicitThreadsY;
threadsZ = explicitThreadsZ;
isCompilationEnabled = true;
}
}
/// <summary>
/// Gets the <see cref="HlslBytecodeInfo"/> instance for the input shader info.
/// </summary>
@@ -8,10 +8,10 @@ namespace ComputeSharp.SourceGenerators;
partial class ComputeShaderDescriptorGenerator
{
/// <inheritdoc/>
partial class BuildHlslSource
partial class HlslSource
{
/// <summary>
/// Writes the <c>BuildHlslSource</c> method.
/// Writes the <c>HlslSource</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
@@ -20,9 +20,9 @@ namespace ComputeSharp.SourceGenerators;
partial class ComputeShaderDescriptorGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>BuildHlslSource</c> method.
/// A helper with all logic to generate the <c>HlslSource</c> property.
/// </summary>
internal static partial class BuildHlslSource
internal static partial class HlslSource
{
/// <summary>
/// Gathers all necessary information on a transpiled HLSL source for a given shader type.
@@ -0,0 +1,39 @@
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGenerators.Models;
namespace ComputeSharp.SourceGenerators;
/// <inheritdoc/>
partial class ComputeShaderDescriptorGenerator
{
/// <summary>
/// A helper with all logic to generate additional metadata properties
/// </summary>
private static class MetadataProperties
{
/// <summary>
/// Writes the <c>ConstantBufferSize</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteConstantBufferSizeSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ConstantBufferSize => {info.ConstantBufferSizeInBytes};");
}
/// <summary>
/// Writes the <c>IsStaticSamplerRequired</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteIsStaticSamplerRequiredSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly bool global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.IsStaticSamplerRequired => {info.IsSamplerUsed.ToString().ToLowerInvariant()};");
}
}
}
@@ -0,0 +1,49 @@
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGenerators.Models;
namespace ComputeSharp.SourceGenerators;
/// <inheritdoc/>
partial class ComputeShaderDescriptorGenerator
{
/// <inheritdoc/>
partial class NumThreads
{
/// <summary>
/// Writes the <c>ThreadsX</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteThreadsXSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsX => {info.ThreadsX};");
}
/// <summary>
/// Writes the <c>ThreadsY</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteThreadsYSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsY => {info.ThreadsY};");
}
/// <summary>
/// Writes the <c>ThreadsZ</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteThreadsZSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsZ => {info.ThreadsZ};");
}
}
}
+103
ファイルの表示
@@ -0,0 +1,103 @@
using System;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using Microsoft.CodeAnalysis;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.SourceGenerators;
/// <inheritdoc/>
partial class ComputeShaderDescriptorGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>[numthreads]</c> properties.
/// </summary>
private static partial class NumThreads
{
/// <summary>
/// Gets the thread ids values for a given shader type, if available.
/// </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="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 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))
{
// Emit the diagnostics if dynamic shaders are not supported
diagnostics.Add(MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported, structDeclarationSymbol, structDeclarationSymbol);
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
return;
}
// Check for a dispatch axis argument first
if (attribute.ConstructorArguments.Length == 1)
{
int dispatchAxis = (int)attribute.ConstructorArguments[0].Value!;
(threadsX, threadsY, threadsZ) = (DispatchAxis)dispatchAxis switch
{
DispatchAxis.X => (64, 1, 1),
DispatchAxis.Y => (1, 64, 1),
DispatchAxis.Z => (1, 1, 64),
DispatchAxis.XY => (8, 8, 1),
DispatchAxis.XZ => (8, 1, 8),
DispatchAxis.YZ => (1, 8, 8),
DispatchAxis.XYZ => (4, 4, 4),
_ => (0, 0, 0)
};
// Validate the dispatch axis argument
if ((threadsX, threadsY, threadsZ) is (0, 0, 0))
{
diagnostics.Add(InvalidEmbeddedBytecodeDispatchAxis, structDeclarationSymbol, structDeclarationSymbol);
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
return;
}
isCompilationEnabled = true;
}
else if (
attribute.ConstructorArguments.Length != 3 ||
attribute.ConstructorArguments[0].Value is not int explicitThreadsX ||
attribute.ConstructorArguments[1].Value is not int explicitThreadsY ||
attribute.ConstructorArguments[2].Value is not int explicitThreadsZ ||
explicitThreadsX is < 1 or > 1024 ||
explicitThreadsY is < 1 or > 1024 ||
explicitThreadsZ is < 1 or > 64)
{
// Failed to validate the thread number arguments
diagnostics.Add(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol);
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
return;
}
else
{
threadsX = explicitThreadsX;
threadsY = explicitThreadsY;
threadsZ = explicitThreadsZ;
isCompilationEnabled = true;
}
}
}
}
@@ -8,32 +8,8 @@ namespace ComputeSharp.SourceGenerators;
partial class ComputeShaderDescriptorGenerator
{
/// <inheritdoc/>
partial class LoadDispatchMetadata
partial class ResourceDescriptorRanges
{
/// <summary>
/// Writes the <c>ConstantBufferSize</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteConstantBufferSizeSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ConstantBufferSize => {info.ConstantBufferSizeInBytes};");
}
/// <summary>
/// Writes the <c>IsStaticSamplerRequired</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteIsStaticSamplerRequiredSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly bool global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.IsStaticSamplerRequired => {info.IsSamplerUsed.ToString().ToLowerInvariant()};");
}
/// <summary>
/// Writes the <c>ResourceDescriptorRanges</c> property.
/// </summary>
@@ -9,9 +9,9 @@ namespace ComputeSharp.SourceGenerators;
partial class ComputeShaderDescriptorGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>LoadDispatchMetadata</c> method.
/// A helper with all logic to generate the <c>ResourceDescriptorRanges</c> property.
/// </summary>
private static partial class LoadDispatchMetadata
private static partial class ResourceDescriptorRanges
{
/// <summary>
/// Gets the data related to the shader metadata for a given shader type.
+22 -22
ファイルの表示
@@ -53,8 +53,8 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
using ImmutableArrayBuilder<DiagnosticInfo> diagnostics = new();
// LoadDispatchData() info
ImmutableArray<FieldInfo> fieldInfos = LoadDispatchData.GetInfo(
// Dispatch data loading info
ImmutableArray<FieldInfo> fieldInfos = DispatchDataLoading.GetInfo(
diagnostics,
typeSymbol,
isPixelShaderLike,
@@ -63,8 +63,8 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
token.ThrowIfCancellationRequested();
// TryGetBytecode() info
LoadBytecode.GetInfo(
// Thread group size info
NumThreads.GetInfo(
diagnostics,
typeSymbol,
out int threadsX,
@@ -74,8 +74,8 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
token.ThrowIfCancellationRequested();
// BuildHlslSource() info
BuildHlslSource.GetInfo(
// Transpiled HLSL source info
HlslSource.GetInfo(
diagnostics,
context.SemanticModel.Compilation,
typeDeclaration,
@@ -89,8 +89,8 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
token.ThrowIfCancellationRequested();
// GetDispatchMetadata() info
ImmutableArray<ResourceDescriptor> resourceDescriptors = LoadDispatchMetadata.GetInfo(
// Resource descriptor ranges info
ImmutableArray<ResourceDescriptor> resourceDescriptors = ResourceDescriptorRanges.GetInfo(
isImplicitTextureUsed,
isSamplerUsed,
fieldInfos);
@@ -101,11 +101,11 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
HlslBytecodeInfoKey hlslInfoKey = new(hlslSource, isCompilationEnabled);
// Try to get the HLSL bytecode
HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(ref hlslInfoKey, token);
HlslBytecodeInfo hlslInfo = HlslBytecode.GetBytecode(ref hlslInfoKey, token);
token.ThrowIfCancellationRequested();
LoadBytecode.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics);
HlslBytecode.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics);
token.ThrowIfCancellationRequested();
@@ -143,22 +143,22 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
{
using ImmutableArrayBuilder<IndentedTextWriter.Callback<ShaderInfo>> declaredMembers = new();
declaredMembers.Add(LoadBytecode.WriteThreadsXSyntax);
declaredMembers.Add(LoadBytecode.WriteThreadsYSyntax);
declaredMembers.Add(LoadBytecode.WriteThreadsZSyntax);
declaredMembers.Add(LoadDispatchMetadata.WriteConstantBufferSizeSyntax);
declaredMembers.Add(LoadDispatchMetadata.WriteIsStaticSamplerRequiredSyntax);
declaredMembers.Add(LoadDispatchMetadata.WriteSyntax);
declaredMembers.Add(BuildHlslSource.WriteSyntax);
declaredMembers.Add(LoadBytecode.WriteHlslBytecodeSyntax);
declaredMembers.Add(LoadDispatchData.WriteLoadConstantBufferSyntax);
declaredMembers.Add(LoadDispatchData.WriteLoadGraphicsResourcesSyntax);
declaredMembers.Add(HlslBytecode.WriteThreadsXSyntax);
declaredMembers.Add(HlslBytecode.WriteThreadsYSyntax);
declaredMembers.Add(HlslBytecode.WriteThreadsZSyntax);
declaredMembers.Add(MetadataProperties.WriteConstantBufferSizeSyntax);
declaredMembers.Add(MetadataProperties.WriteIsStaticSamplerRequiredSyntax);
declaredMembers.Add(ResourceDescriptorRanges.WriteSyntax);
declaredMembers.Add(HlslSource.WriteSyntax);
declaredMembers.Add(HlslBytecode.WriteHlslBytecodeSyntax);
declaredMembers.Add(DispatchDataLoading.WriteLoadConstantBufferSyntax);
declaredMembers.Add(DispatchDataLoading.WriteLoadGraphicsResourcesSyntax);
using ImmutableArrayBuilder<IndentedTextWriter.Callback<ShaderInfo>> additionalTypes = new();
using ImmutableHashSetBuilder<string> usingDirectives = new();
LoadDispatchMetadata.RegisterAdditionalDataMemberSyntax(item, additionalTypes, usingDirectives);
LoadBytecode.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);
ResourceDescriptorRanges.RegisterAdditionalDataMemberSyntax(item, additionalTypes, usingDirectives);
HlslBytecode.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);
using IndentedTextWriter writer = new();