using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.SourceGenerators.Models;
///
/// A base type for a model describing a given captured field in a shader.
///
internal abstract record FieldInfo
{
///
/// A captured resource (either a buffer or a texture).
///
/// The name of the resource field.
/// The full metadata name for the resource type.
/// The offset for the resource within the root signature.
public sealed record Resource(string FieldName, string TypeName, int Offset) : FieldInfo
{
///
public bool Equals(Resource? obj) => Comparer.Default.Equals(this, obj);
///
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
///
/// An implementation for .
///
private sealed class Comparer : Comparer
{
///
protected override void AddToHashCode(ref HashCode hashCode, Resource obj)
{
hashCode.Add(obj.FieldName);
hashCode.Add(obj.TypeName);
hashCode.Add(obj.Offset);
}
///
protected override bool AreEqual(Resource x, Resource y)
{
return
x.FieldName == y.FieldName &&
x.TypeName == y.TypeName &&
x.Offset == y.Offset;
}
}
}
///
/// A captured primitive value (either a scalar, a vector, or a linear matrix).
///
/// The path of the field with respect to the shader instance.
/// The full metadata name for the primitive type.
/// The byte offset of the value within the root signature.
public sealed record Primitive(ImmutableArray FieldPath, string TypeName, int Offset) : FieldInfo
{
///
public bool Equals(Primitive? obj) => Comparer.Default.Equals(this, obj);
///
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
///
/// An implementation for .
///
private sealed class Comparer : Comparer
{
///
protected override void AddToHashCode(ref HashCode hashCode, Primitive obj)
{
hashCode.AddRange(obj.FieldPath);
hashCode.Add(obj.TypeName);
hashCode.Add(obj.Offset);
}
///
protected override bool AreEqual(Primitive x, Primitive y)
{
return
x.FieldPath.SequenceEqual(y.FieldPath) &&
x.TypeName == y.TypeName &&
x.Offset == y.Offset;
}
}
}
///
/// A captured non linear matrix value.
///
/// The path of the field with respect to the shader instance.
/// The full metadata name for the matrix type.
/// The name of each element in the matrix type.
/// The number of rows in the matrix.
/// The number of columns in the matrix.
/// The sequence of byte offsets for each row within the root signature.
public sealed record NonLinearMatrix(
ImmutableArray FieldPath,
string TypeName,
string ElementName,
int Rows,
int Columns,
ImmutableArray Offsets) : FieldInfo
{
///
public bool Equals(NonLinearMatrix? obj) => Comparer.Default.Equals(this, obj);
///
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
///
/// An implementation for .
///
private sealed class Comparer : Comparer
{
///
protected override void AddToHashCode(ref HashCode hashCode, NonLinearMatrix obj)
{
hashCode.AddRange(obj.FieldPath);
hashCode.Add(obj.TypeName);
hashCode.Add(obj.ElementName);
hashCode.Add(obj.Rows);
hashCode.Add(obj.Columns);
hashCode.AddRange(obj.Offsets);
}
///
protected override bool AreEqual(NonLinearMatrix x, NonLinearMatrix y)
{
return
x.FieldPath.SequenceEqual(y.FieldPath) &&
x.TypeName == y.TypeName &&
x.ElementName == y.ElementName &&
x.Rows == y.Rows &&
x.Columns == y.Columns &&
x.Offsets.SequenceEqual(y.Offsets);
}
}
}
}