2022-11-02 17:17:19 -04:00
|
|
|
using Silk.NET.Vulkan;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Vulkan
|
|
|
|
{
|
|
|
|
public enum SpecConstType
|
|
|
|
{
|
|
|
|
Bool32,
|
|
|
|
Int16,
|
|
|
|
Int32,
|
|
|
|
Int64,
|
|
|
|
Float16,
|
|
|
|
Float32,
|
2023-07-01 06:31:42 -04:00
|
|
|
Float64,
|
2022-11-02 17:17:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sealed class SpecDescription
|
|
|
|
{
|
|
|
|
public readonly SpecializationInfo Info;
|
|
|
|
public readonly SpecializationMapEntry[] Map;
|
|
|
|
|
|
|
|
// For mapping a simple packed struct or single entry
|
|
|
|
public SpecDescription(params (uint Id, SpecConstType Type)[] description)
|
|
|
|
{
|
|
|
|
int count = description.Length;
|
|
|
|
Map = new SpecializationMapEntry[count];
|
|
|
|
|
|
|
|
uint structSize = 0;
|
|
|
|
|
2023-03-11 15:05:48 -05:00
|
|
|
for (int i = 0; i < Map.Length; ++i)
|
2022-11-02 17:17:19 -04:00
|
|
|
{
|
|
|
|
var typeSize = SizeOf(description[i].Type);
|
|
|
|
Map[i] = new SpecializationMapEntry(description[i].Id, structSize, typeSize);
|
|
|
|
structSize += typeSize;
|
|
|
|
}
|
|
|
|
|
2023-07-01 06:31:42 -04:00
|
|
|
Info = new SpecializationInfo
|
2022-11-02 17:17:19 -04:00
|
|
|
{
|
|
|
|
DataSize = structSize,
|
2023-07-01 06:31:42 -04:00
|
|
|
MapEntryCount = (uint)count,
|
2022-11-02 17:17:19 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// For advanced mapping with overlapping or staggered fields
|
|
|
|
public SpecDescription(SpecializationMapEntry[] map)
|
|
|
|
{
|
|
|
|
Map = map;
|
|
|
|
|
|
|
|
uint structSize = 0;
|
2023-03-11 15:05:48 -05:00
|
|
|
for (int i = 0; i < map.Length; ++i)
|
2022-11-02 17:17:19 -04:00
|
|
|
{
|
|
|
|
structSize = Math.Max(structSize, map[i].Offset + (uint)map[i].Size);
|
|
|
|
}
|
|
|
|
|
2023-07-01 06:31:42 -04:00
|
|
|
Info = new SpecializationInfo
|
2022-11-02 17:17:19 -04:00
|
|
|
{
|
|
|
|
DataSize = structSize,
|
2023-07-01 06:31:42 -04:00
|
|
|
MapEntryCount = (uint)map.Length,
|
2022-11-02 17:17:19 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private static uint SizeOf(SpecConstType type) => type switch
|
|
|
|
{
|
|
|
|
SpecConstType.Int16 or SpecConstType.Float16 => 2,
|
|
|
|
SpecConstType.Bool32 or SpecConstType.Int32 or SpecConstType.Float32 => 4,
|
|
|
|
SpecConstType.Int64 or SpecConstType.Float64 => 8,
|
2023-07-01 06:31:42 -04:00
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(type)),
|
2022-11-02 17:17:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
private SpecDescription()
|
|
|
|
{
|
|
|
|
Info = new();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly SpecDescription Empty = new();
|
|
|
|
}
|
|
|
|
|
|
|
|
readonly struct SpecData : IRefEquatable<SpecData>
|
|
|
|
{
|
|
|
|
private readonly byte[] _data;
|
|
|
|
private readonly int _hash;
|
|
|
|
|
|
|
|
public int Length => _data.Length;
|
|
|
|
public ReadOnlySpan<byte> Span => _data.AsSpan();
|
|
|
|
public override int GetHashCode() => _hash;
|
|
|
|
|
|
|
|
public SpecData(ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
_data = new byte[data.Length];
|
|
|
|
data.CopyTo(_data);
|
|
|
|
|
|
|
|
var hc = new HashCode();
|
|
|
|
hc.AddBytes(data);
|
|
|
|
_hash = hc.ToHashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals(object obj) => obj is SpecData other && Equals(other);
|
|
|
|
public bool Equals(ref SpecData other) => _data.AsSpan().SequenceEqual(other._data);
|
|
|
|
}
|
2023-07-01 06:31:42 -04:00
|
|
|
}
|