Promote shader math primitives

This commit is contained in:
Meta Vens 2026-06-10 21:22:43 +01:00
commit f535801cd0
7 changed files with 604 additions and 3 deletions

View file

@ -0,0 +1,63 @@
using Xunit;
namespace CultMath.Tests;
public sealed class HlslMirrorTests
{
[Fact]
public void HlslMirrorPublishesCultMathPrimitives()
{
var include = File.ReadAllText(Path.Combine(GetRepositoryRoot(), "shaders", "CultMath.hlsl"));
var requiredSymbols = new[]
{
"cultmath_radians",
"cultmath_degrees",
"cultmath_frac",
"cultmath_clamp",
"cultmath_saturate",
"cultmath_lerp",
"cultmath_step",
"cultmath_smoothstep",
"cultmath_smootherstep",
"cultmath_lengthsq",
"cultmath_distance",
"cultmath_normalize",
"cultmath_reflect",
"cultmath_csum",
"cultmath_decay",
"cultmath_damp",
"cultmath_catmullrom",
"cultmath_quadratic_bezier",
"cultmath_cubic_bezier",
"cultmath_hash",
"cultmath_value_noise",
"cultmath_value_noise_bicubic",
"cultmath_value_noise_texture",
"cultmath_value_noise_texture_bicubic",
};
foreach (var symbol in requiredSymbols)
{
Assert.Contains(symbol, include);
}
Assert.Contains("CULTMATH_NORMALIZE_EPSILON", include);
Assert.Contains("max(length(value), CULTMATH_NORMALIZE_EPSILON)", include);
}
private static string GetRepositoryRoot()
{
var directory = new DirectoryInfo(AppContext.BaseDirectory);
while (directory is not null)
{
if (File.Exists(Path.Combine(directory.FullName, "CultMath.slnx")))
{
return directory.FullName;
}
directory = directory.Parent;
}
throw new InvalidOperationException("Could not locate CultMath repository root.");
}
}

View file

@ -23,6 +23,9 @@ public sealed class MathTests
[Fact]
public void VectorConstructorsAcceptLowerDimensionVectors()
{
Assert.Equal(new float2(1.0f, 2.0f), math.float2(1.0f, 2.0f));
Assert.Equal(new float3(1.0f, 1.0f, 1.0f), math.float3(1.0f));
Assert.Equal(new float4(1.0f, 2.0f, 3.0f, 4.0f), math.float4(new float3(1.0f, 2.0f, 3.0f), 4.0f));
Assert.Equal(new float3(1.0f, 2.0f, 3.0f), new float3(new float2(1.0f, 2.0f), 3.0f));
Assert.Equal(new float4(1.0f, 2.0f, 3.0f, 4.0f), new float4(new float2(1.0f, 2.0f), 3.0f, 4.0f));
Assert.Equal(new float4(1.0f, 2.0f, 3.0f, 4.0f), new float4(new float3(1.0f, 2.0f, 3.0f), 4.0f));
@ -42,6 +45,7 @@ public sealed class MathTests
Assert.Equal(0.0f, math.smoothstep(0.0f, 1.0f, -1.0f));
Assert.Equal(0.5f, math.smoothstep(0.0f, 1.0f, 0.5f), precision: 5);
Assert.Equal(1.0f, math.smoothstep(0.0f, 1.0f, 2.0f));
Assert.Equal(0.5f, math.smootherstep(0.5f), precision: 5);
}
[Fact]
@ -53,6 +57,50 @@ public sealed class MathTests
Assert.Equal(math.HALF_PI, math.atan2(1.0f, 0.0f), precision: 5);
}
[Fact]
public void AetheriaPrimitivesRemainSmallAndDeterministic()
{
Assert.Equal(6.0f, math.csum(new float3(1.0f, 2.0f, 3.0f)));
Assert.Equal(MathF.Exp(-2.0f), math.decay(1.0f, 2.0f, 1.0f), precision: 5);
Assert.Equal(1.0f - MathF.Exp(-2.0f), math.damp(0.0f, 1.0f, 2.0f, 1.0f), precision: 5);
Assert.Equal(15.0f, math.catmullrom(0.0f, 10.0f, 20.0f, 30.0f, 0.5f), precision: 5);
Assert.Equal(0.25f, math.quadratic_bezier(0.0f, 0.0f, 1.0f, 0.5f), precision: 5);
Assert.Equal(0.5f, math.cubic_bezier(0.0f, 0.0f, 1.0f, 1.0f, 0.5f), precision: 5);
}
[Fact]
public void SegmentDistanceReportsClosestPoint()
{
var distance = math.distance_to_segment(new float2(2.0f, 3.0f), new float2(0.0f, 0.0f), new float2(4.0f, 0.0f), out var closest);
Assert.Equal(3.0f, distance, precision: 5);
Assert.Equal(new float2(2.0f, 0.0f), closest);
}
[Fact]
public void FirstOrderInterceptSolvesSimpleClosingTarget()
{
var time = math.first_order_intercept_time(
2.0f,
new float3(10.0f, 0.0f, 0.0f),
new float3(-1.0f, 0.0f, 0.0f));
Assert.Equal(10.0f / 3.0f, time, precision: 5);
}
[Fact]
public void ValueNoiseIsDeterministicAndSmoothAtCellCorners()
{
var position = new float2(12.25f, -4.5f);
Assert.Equal(math.value_noise(position), math.value_noise(position));
Assert.Equal(math.hash(new float2(3.0f, 5.0f)), math.value_noise(new float2(3.0f, 5.0f)), precision: 5);
var bicubic = math.value_noise_bicubic(position);
Assert.True(bicubic > -0.25f && bicubic < 1.25f);
}
[Fact]
public void VoronoiBatchProducesToneMappedColors()
{