using System.Globalization; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using static SpectrumColorShift.SpectrumColorShiftCustomEffect; namespace SpectrumColorShift.Tests; public sealed partial class SpectralShaderContractTests { static readonly Dictionary Fields = new() { ["inputBounds"] = "Header.InputBounds", ["direction"] = "Header.Direction", ["center"] = "Header.Center", ["shiftDistance"] = "Header.Distance", ["radius"] = "Header.Radius", ["strength"] = "Header.Strength", ["steps"] = "Header.Steps", ["spreadScale"] = "SpreadScale", ["filterOrigin"] = "FilterOrigin", ["filterVertical"] = "FilterVertical", ["samples"] = "Samples", ["taps"] = "Taps", }; static string Source { get; } = ShaderSource("Physical.hlsli"); static string SrgbSource { get; } = ShaderSource("Srgb.hlsli"); static string ShaderSource(string name) => File.ReadAllText(Path.Combine(Path.GetDirectoryName(Here())!, "..", "SpectrumColorShift.Shader", name)); static string Here([CallerFilePath] string path = "") => path; static Type ConstantBuffer => typeof(EffectImpl).GetNestedType("PhysicalConstantBuffer", BindingFlags.NonPublic)!; [GeneratedRegex(@"static const (?:int|float) (?\w+) = (?[0-9.]+);")] private static partial Regex ConstantPattern(); [GeneratedRegex(@"Texture2D\s+LightTexture\s*:\s*register\(t(?\d+)\)")] private static partial Regex TexturePattern(); [GeneratedRegex(@"float4\s+\w+\s*:\s*TEXCOORD(?\d+)")] private static partial Regex CoordinatePattern(); static float Constant(string name) => Constant(Source, name); static float Constant(string source, string name) => float.Parse(ConstantPattern().Matches(source).Single(match => match.Groups["name"].Value == name).Groups["value"].Value, CultureInfo.InvariantCulture); [Fact] public void TheShaderAndTheEffectAgreeOnTheConstants() { Assert.Equal(Spectrum.SpectralTableLength, Constant("TableSize")); Assert.Equal(SpectralFilter.TableLength, Constant("FilterTableSize")); Assert.Equal((int)ShiftMode.Radial, Constant("ModeRadial")); Assert.Equal((int)ShiftMode.Rotational, Constant("ModeRotational")); Assert.Equal(ShiftGeometry.MaxRadialShift, Constant("MaxRadialShift")); } [Fact] public void TheShaderReadsTheTextureOfTheLinearizedInput() { Assert.Equal(SpectrumColorShiftCustomEffect.LightInput, int.Parse(TexturePattern().Match(Source).Groups["register"].Value, CultureInfo.InvariantCulture)); } [Fact] public void TheShaderDeclaresTheTextureCoordinateOfEveryInputUpToTheOneItReads() { var declared = CoordinatePattern().Matches(Source).Select(match => int.Parse(match.Groups["index"].Value, CultureInfo.InvariantCulture)); Assert.Equal(Enumerable.Range(0, SpectrumColorShiftCustomEffect.LightInput + 1), declared.Order()); } [Theory] [InlineData("SrgbDecodeThreshold", 0.04045f)] [InlineData("SrgbEncodeThreshold", 0.0031308f)] [InlineData("SrgbLowSlope", 12.92f)] [InlineData("SrgbOffset", 0.055f)] [InlineData("SrgbScale", 1.055f)] [InlineData("SrgbExponent", 2.4f)] public void TheSrgbHeaderHoldsTheStandardTransferConstants(string name, float expected) => Assert.Equal(expected, Constant(SrgbSource, name)); [Fact] public void TheSrgbCurvesMeetAtTheThresholds() { var slope = Constant(SrgbSource, "SrgbLowSlope"); var linearEnd = Constant(SrgbSource, "SrgbEncodeThreshold"); var power = Constant(SrgbSource, "SrgbScale") * MathF.Pow(linearEnd, 1f / Constant(SrgbSource, "SrgbExponent")) - Constant(SrgbSource, "SrgbOffset"); Assert.Equal(Constant(SrgbSource, "SrgbDecodeThreshold"), slope * linearEnd, 1e-5f); Assert.Equal(slope * linearEnd, power, 1e-5f); } [Fact] public void TheConstantBufferMatchesTheShaderLayout() { var members = ConstantBufferContract.Declared(Source); Assert.Equal(Fields.Keys.Order(), members.Keys.Order()); Assert.All(members, member => Assert.Equal(member.Value, ConstantBufferContract.OffsetOf(ConstantBuffer, Fields[member.Key]))); } [Fact] public void TheConstantBufferFillsWholeRegistersAndHoldsTheTable() { Assert.Equal(16 * (4 + Spectrum.SpectralTableLength + SpectralFilter.TableLength), ConstantBufferContract.SizeOf(ConstantBuffer)); } }