diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs index e90e3eae..d1f19417 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs @@ -96,7 +96,7 @@ partial class D2DPixelShaderDescriptorGenerator /// /// The input instance with gathered shader info. /// The using directives needed by the generated code. - public static void AddAdditionalDataMemberUsingDirectives(D2D1ShaderInfo info, ImmutableHashSetBuilder usingDirectives) + private static void AddAdditionalDataMemberUsingDirectives(D2D1ShaderInfo info, ImmutableHashSetBuilder usingDirectives) { if (info.InputDescriptions.IsEmpty) { @@ -112,7 +112,7 @@ partial class D2DPixelShaderDescriptorGenerator /// /// The input instance with gathered shader info. /// The registered callbacks to generate additional data members. - public static void RegisterAdditionalDataMemberSyntax(D2D1ShaderInfo info, ImmutableArrayBuilder> callbacks) + private static void RegisterAdditionalDataMemberSyntax(D2D1ShaderInfo info, ImmutableArrayBuilder> callbacks) { // If there are no input descriptions, there is nothing to di if (info.InputDescriptions.IsEmpty) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.Syntax.cs index 0f1117e2..5e3a9eca 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.Syntax.cs @@ -1,4 +1,3 @@ -using System.Globalization; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGenerators.Models; @@ -36,7 +35,7 @@ partial class IShaderGenerator } /// - /// Writes the LoadDispatchMetadata method. + /// Writes the ResourceDescriptorRanges property. /// /// The input instance with gathered shader info. /// The instance to write into. @@ -44,32 +43,77 @@ partial class IShaderGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]"); - writer.WriteLine("readonly void global::ComputeSharp.__Internals.IShader.LoadDispatchMetadata(ref TLoader loader, out global::System.IntPtr result)"); + writer.Write($"readonly global::System.ReadOnlyMemory global::ComputeSharp.__Internals.IShader.ResourceDescriptorRanges => "); - using (writer.WriteBlock()) + // If there are no declared resources, just return an empty collection + if (info.ResourceDescriptors.IsEmpty) { - // Compute the total number of resources - int totalResourcesCount = info.ResourceDescriptors.Length; - - writer.WriteLine("global::System.Span span0 = stackalloc byte[5];"); - writer.WriteLine($"global::System.Span span1 = stackalloc global::ComputeSharp.__Internals.ResourceDescriptor[{totalResourcesCount}];"); - writer.WriteLine("ref byte r0 = ref span0[0];"); - writer.WriteLine("ref global::ComputeSharp.__Internals.ResourceDescriptor r1 = ref span1[0];"); - - // Write the serialized shader metadata - writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 0), {info.ConstantBufferSizeInBytes});"); - writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned(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.ResourceDescriptors) - { - writer.WriteLine($"global::ComputeSharp.__Internals.ResourceDescriptor.Create({descriptor.TypeId}, {descriptor.RegisterOffset}, out global::System.Runtime.CompilerServices.Unsafe.Add(ref r1, {descriptor.Offset}));"); - } - - // Invoke the value delegate to create the opaque root signature handle - writer.WriteLine("loader.LoadMetadataHandle(span0, span1, out result);"); + writer.WriteLine("default;"); } + else + { + writer.WriteLine("global::ComputeSharp.Generated.Data.ResourceDescriptorRanges;"); + } + } + + /// + /// Registers a callback to generate an additional data member, if needed. + /// + /// The input instance with gathered shader info. + /// The registered callbacks to generate additional data members. + /// The using directives needed by the generated code. + public static void RegisterAdditionalDataMemberSyntax( + ShaderInfo info, + ImmutableArrayBuilder> callbacks, + ImmutableHashSetBuilder usingDirectives) + { + // If there are no declared resources, there is nothing to di + if (info.ResourceDescriptors.IsEmpty) + { + return; + } + + usingDirectives.Add("global::System.CodeDom.Compiler"); + usingDirectives.Add("global::System.Diagnostics"); + usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); + usingDirectives.Add("global::ComputeSharp.Interop"); + + // Declare the additional data type and the shared array with resource descriptor ranges + static void Callback(ShaderInfo info, IndentedTextWriter writer) + { + writer.WriteLine($$"""/// """); + writer.WriteLine($$"""/// A container type for additional data needed by the shader."""); + writer.WriteLine($$"""/// """); + writer.WriteGeneratedAttributes(GeneratorName, useFullyQualifiedTypeNames: false); + writer.WriteLine($$"""file static class Data"""); + + using (writer.WriteBlock()) + { + writer.WriteLine("""/// The singleton array instance."""); + writer.WriteLine("""public static readonly ResourceDescriptorRange[] ResourceDescriptorRanges ="""); + writer.WriteLine("""{"""); + writer.IncreaseIndent(); + + // Initialize all resource descriptor ranges + writer.WriteInitializationExpressions(info.ResourceDescriptors.AsSpan(), static (description, writer) => + { + string rangeTypeName = description.TypeId switch + { + 0 => "ShaderResourceView", + 1 => "UnorderedAccessView", + _ => "ConstantBufferView" + }; + + writer.Write($"new ResourceDescriptorRange(ResourceDescriptorRangeType.{rangeTypeName}, {description.Register})"); + }); + + writer.DecreaseIndent(); + writer.WriteLine(); + writer.WriteLine("};"); + } + } + + callbacks.Add(Callback); } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs index 96fe4ebf..4e448bdc 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs @@ -32,12 +32,11 @@ partial class IShaderGenerator int constantBufferOffset = 1; int readOnlyResourceOffset = 0; int readWriteResourceOffset = 0; - int resourceOffset = 0; // Add the implicit texture descriptor, if needed if (isImplicitTextureUsed) { - descriptors.Add(new ResourceDescriptor(1, readWriteResourceOffset++, resourceOffset++)); + descriptors.Add(new ResourceDescriptor(1, readWriteResourceOffset++)); } // Populate the sequence of resource descriptors @@ -45,15 +44,15 @@ partial class IShaderGenerator { if (HlslKnownTypes.IsConstantBufferType(resource.TypeName)) { - descriptors.Add(new ResourceDescriptor(2, constantBufferOffset++, resourceOffset++)); + descriptors.Add(new ResourceDescriptor(2, constantBufferOffset++)); } else if (HlslKnownTypes.IsReadOnlyTypedResourceType(resource.TypeName)) { - descriptors.Add(new ResourceDescriptor(0, readOnlyResourceOffset++, resourceOffset++)); + descriptors.Add(new ResourceDescriptor(0, readOnlyResourceOffset++)); } else { - descriptors.Add(new ResourceDescriptor(1, readWriteResourceOffset++, resourceOffset++)); + descriptors.Add(new ResourceDescriptor(1, readWriteResourceOffset++)); } } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 511845f3..27f554a0 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -150,14 +150,15 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator 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.WriteSyntax); - declaredMembers.Add(LoadDispatchMetadata.WriteSyntax); using ImmutableArrayBuilder> additionalTypes = new(); using ImmutableHashSetBuilder usingDirectives = new(); + LoadDispatchMetadata.RegisterAdditionalDataMemberSyntax(item, additionalTypes, usingDirectives); LoadBytecode.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives); using IndentedTextWriter writer = new(); diff --git a/src/ComputeSharp.SourceGenerators/Models/ResourceDescriptor.cs b/src/ComputeSharp.SourceGenerators/Models/ResourceDescriptor.cs index 791af20a..805b537c 100644 --- a/src/ComputeSharp.SourceGenerators/Models/ResourceDescriptor.cs +++ b/src/ComputeSharp.SourceGenerators/Models/ResourceDescriptor.cs @@ -4,6 +4,5 @@ namespace ComputeSharp.SourceGenerators.Models; /// A resource descriptor for a captured resource in a shader. /// /// The type id for the resource. -/// The offset of the resource in its containing register. -/// The offset of the resource descriptor in the shader root signature. -internal sealed record ResourceDescriptor(int TypeId, int RegisterOffset, int Offset); \ No newline at end of file +/// The register offset for the resource descriptor. +internal sealed record ResourceDescriptor(int TypeId, int Register); \ No newline at end of file diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs index 089c3e94..2d00eb1b 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using ComputeSharp.Interop; namespace ComputeSharp.__Internals; @@ -43,6 +44,11 @@ public interface IShader /// bool IsStaticSamplerRequired { get; } + /// + /// Gets the resource descriptor ranges for all resources that should be bound to this shader. + /// + ReadOnlyMemory ResourceDescriptorRanges { get; } + /// /// Gets the HLSL source code for the current shader instance. ///