using System.Collections.Immutable;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
using ComputeSharp.SourceGenerators.Models;
namespace ComputeSharp.SourceGenerators;
///
partial class ComputeShaderDescriptorGenerator
{
///
/// A helper with all logic to generate the ResourceDescriptorRanges property.
///
private static partial class ResourceDescriptorRanges
{
///
/// Gets the data related to the shader metadata for a given shader type.
///
/// Indicates whether the current shader uses an implicit texture.
/// Whether the static sampler is used by the shader.
/// The sequence of captured fields for the shader.
/// The metadata info for the shader.
public static ImmutableArray GetInfo(
bool isImplicitTextureUsed,
bool isSamplerUsed,
ImmutableArray capturedFields)
{
using ImmutableArrayBuilder descriptors = new();
int constantBufferOffset = 1;
int readOnlyResourceOffset = 0;
int readWriteResourceOffset = 0;
// Add the implicit texture descriptor, if needed
if (isImplicitTextureUsed)
{
descriptors.Add(new ResourceDescriptor(1, readWriteResourceOffset++));
}
// Populate the sequence of resource descriptors
foreach (FieldInfo.Resource resource in capturedFields.OfType())
{
if (HlslKnownTypes.IsConstantBufferType(resource.TypeName))
{
descriptors.Add(new ResourceDescriptor(2, constantBufferOffset++));
}
else if (HlslKnownTypes.IsReadOnlyTypedResourceType(resource.TypeName))
{
descriptors.Add(new ResourceDescriptor(0, readOnlyResourceOffset++));
}
else
{
descriptors.Add(new ResourceDescriptor(1, readWriteResourceOffset++));
}
}
return descriptors.ToImmutable();
}
}
}