From ddca0252e6a4596d93b252266f58267015a157ef Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 12 Oct 2026 06:14:44 +0200 Subject: [PATCH] CultMath: a Blender ramp table has 257 entries (size + 1 at i / 256); the oracle samples it so, and the oracle test leaves unread operands unread Builds. Fixtures and CultMath.Cycles.glsl are still to come. Co-Authored-By: Claude Sonnet 5.5 --- packages/cultmath/shaders/CultMath.Cycles.hlsl | 2 +- packages/cultmath/src/CultMath/math.Cycles.cs | 3 ++- packages/cultmath/tests/CultMath.Tests/CyclesOracleTests.cs | 3 ++- packages/cultmath/tests/CultMath.Tests/GlslMirrorTests.cs | 2 +- packages/cultmath/tools/cycles-oracle/oracle.py | 4 +++- .../unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl | 2 +- 6 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/cultmath/shaders/CultMath.Cycles.hlsl b/packages/cultmath/shaders/CultMath.Cycles.hlsl index 474f14a4..f2449e08 100644 --- a/packages/cultmath/shaders/CultMath.Cycles.hlsl +++ b/packages/cultmath/shaders/CultMath.Cycles.hlsl @@ -342,7 +342,7 @@ float cultmath_cycles_rgb_to_bw(float3 color) // Where a ramp of `size` table entries is read at t: (index, weight), the lower entry and the weight of the // next one. The colour is table[index] when weight is 0 and (1 - weight) table[index] + weight table[index + 1] -// otherwise. The ramp table itself is the caller's. +// otherwise. The ramp table itself is the caller's; Blender's holds 257 entries, the ramp sampled at i / 256. float2 cultmath_cycles_ramp_index(float t, int size) { float f = (t > 0.0 ? (t < 1.0 ? t : 1.0) : 0.0) * (float)(size - 1); diff --git a/packages/cultmath/src/CultMath/math.Cycles.cs b/packages/cultmath/src/CultMath/math.Cycles.cs index 57df82b9..3ebdb8f1 100644 --- a/packages/cultmath/src/CultMath/math.Cycles.cs +++ b/packages/cultmath/src/CultMath/math.Cycles.cs @@ -368,7 +368,8 @@ public static partial class math /// /// Where a Color Ramp of `size` table entries is read at t: (index, weight), the lower entry and the /// weight of the next one. The colour is table[index] when weight is 0 and - /// (1 - weight) table[index] + weight table[index + 1] otherwise. The ramp table itself is the caller's. + /// (1 - weight) table[index] + weight table[index + 1] otherwise. The ramp table itself is the caller's; + /// Blender's holds 257 entries, the ramp sampled at i / 256. /// public static float2 cycles_ramp_index(float t, int size) { diff --git a/packages/cultmath/tests/CultMath.Tests/CyclesOracleTests.cs b/packages/cultmath/tests/CultMath.Tests/CyclesOracleTests.cs index 94f457fb..cd167ab3 100644 --- a/packages/cultmath/tests/CultMath.Tests/CyclesOracleTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/CyclesOracleTests.cs @@ -53,7 +53,8 @@ public sealed class CyclesOracleTests throw new InvalidOperationException($"{method.Name}: parameter type {parameter.ParameterType.Name} is not a fixture operand"); } - Assert.Equal(tokens.Length, next); + // The operands a function does not take (Map Range's Steps, for the three kinds that ignore it) are left unread. + Assert.True(next <= tokens.Length, $"{method.Name}: {next} operands wanted, {tokens.Length} in the case"); return values.ToArray(); } diff --git a/packages/cultmath/tests/CultMath.Tests/GlslMirrorTests.cs b/packages/cultmath/tests/CultMath.Tests/GlslMirrorTests.cs index 8f5ee796..0419688b 100644 --- a/packages/cultmath/tests/CultMath.Tests/GlslMirrorTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/GlslMirrorTests.cs @@ -621,7 +621,7 @@ public sealed class GlslMirrorTests new("vec3 cultmath_cycles_rgb_to_hsv(vec3)", "ulp-bounded", r => new object[] { CyclesColor(r) }), new("vec3 cultmath_cycles_hsv_to_rgb(vec3)", "ulp-bounded", r => new object[] { new float3(r.NextSingle(), r.NextSingle(), Uniform(r, 0.0f, 2.0f)) }), new("float cultmath_cycles_rgb_to_bw(vec3)", "ulp-bounded", r => new object[] { CyclesColor(r) }), - new("vec2 cultmath_cycles_ramp_index(float, int)", "ulp-bounded", r => new object[] { Uniform(r, -0.25f, 1.25f), 256 }), + new("vec2 cultmath_cycles_ramp_index(float, int)", "ulp-bounded", r => new object[] { Uniform(r, -0.25f, 1.25f), 257 }), }.Concat(new[] { "blend", "add", "mul", "screen", "overlay", "sub", "div", "diff", "exclusion", "dark", "light", "dodge", "burn", "hue", "sat", "val", "color", "soft", "linear", diff --git a/packages/cultmath/tools/cycles-oracle/oracle.py b/packages/cultmath/tools/cycles-oracle/oracle.py index f85e6032..820dfed7 100644 --- a/packages/cultmath/tools/cycles-oracle/oracle.py +++ b/packages/cultmath/tools/cycles-oracle/oracle.py @@ -438,7 +438,9 @@ def family_rgb_to_bw(): return [{"function": "cycles_rgb_to_bw", "node": "ShaderNodeRGBToBW", "cases": encode_cases(columns, bake("rgb_to_bw", images, build, 1))}] -RAMP_SIZE = 256 +# A Blender ramp is sampled at 256 intervals into a table of 257 entries (colorramp_to_array: size + 1 entries +# at i / size), and the kernel reads that table with table_size 257. +RAMP_SIZE = 257 # A constant ramp whose 32 stops sit half an entry past an entry boundary: its table is the stops' values, so # a lookup's result names the entry it read. CONSTANT_STOPS = [((8 * j + 3.5) / (RAMP_SIZE - 1), (j + 1) / 32.0) for j in range(32)] diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl index 474f14a4..f2449e08 100644 --- a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl +++ b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl @@ -342,7 +342,7 @@ float cultmath_cycles_rgb_to_bw(float3 color) // Where a ramp of `size` table entries is read at t: (index, weight), the lower entry and the weight of the // next one. The colour is table[index] when weight is 0 and (1 - weight) table[index] + weight table[index + 1] -// otherwise. The ramp table itself is the caller's. +// otherwise. The ramp table itself is the caller's; Blender's holds 257 entries, the ramp sampled at i / 256. float2 cultmath_cycles_ramp_index(float t, int size) { float f = (t > 0.0 ? (t < 1.0 ? t : 1.0) : 0.0) * (float)(size - 1);