CultMath Cycles: an infinite exponent is even in cycles_power; the oracle's infinite family and the pins for infinite operands
cycles_power read b - 2 floor(0.5 b) != 0 as odd, which is NaN for b = +-inf, so a negative base got a negative result where Cycles' powf(a, +-inf) is never negative. Odd now means the remainder is exactly 1 (C#, HLSL, Unity copy). oracle.py gains the infinite family (operands made in-kernel as +-exp(100), results read through comparisons with 0) for power, modulo and Map Range Smooth Step; the infinite.json fixture and the regenerated GLSL follow in the next commit. This commit builds; InfiniteOperandsFollowBlender and CommittedGlslEqualsLowering fail until it lands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d8f38057a1
commit
77a60d2910
5 changed files with 167 additions and 3 deletions
|
|
@ -42,7 +42,8 @@ float cultmath_cycles_power(float a, float b)
|
|||
if (a < 0.0)
|
||||
{
|
||||
float magnitude = pow(-a, b);
|
||||
return b - 2.0 * floor(0.5 * b) != 0.0 ? -magnitude : magnitude;
|
||||
// Odd exactly when the remainder is 1: an infinite exponent leaves NaN here and is even, as powf(a, +-inf) is never negative.
|
||||
return b - 2.0 * floor(0.5 * b) == 1.0 ? -magnitude : magnitude;
|
||||
}
|
||||
return pow(a, b);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,8 @@ public static partial class math
|
|||
if (a < 0.0f)
|
||||
{
|
||||
var magnitude = pow(-a, b);
|
||||
return b - 2.0f * floor(0.5f * b) != 0.0f ? -magnitude : magnitude;
|
||||
// Odd exactly when the remainder is 1: an infinite exponent leaves NaN here and is even, as powf(a, +-inf) is never negative.
|
||||
return b - 2.0f * floor(0.5f * b) == 1.0f ? -magnitude : magnitude;
|
||||
}
|
||||
|
||||
return pow(a, b);
|
||||
|
|
|
|||
|
|
@ -233,6 +233,63 @@ public sealed class CyclesOracleTests
|
|||
Assert.Equal(0.0f, One("cycles_floored_modulo", 1.0f, 0.0f));
|
||||
}
|
||||
|
||||
private static float OperandOrInfinity(string token) => token == "inf" ? float.PositiveInfinity : token == "-inf" ? float.NegativeInfinity : Float(token);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Targets))]
|
||||
public void InfiniteOperandsFollowBlender(string target)
|
||||
{
|
||||
// infinite.json: operands that are infinite, made inside Blender's kernel (the film writes 0 for an infinity), and
|
||||
// results read back through comparisons with 0 as "inf", "-inf" or "nan" where the film showed 0.
|
||||
var functions = Load("infinite")["functions"]!.AsArray();
|
||||
Assert.Equal(new[] { "cycles_power", "cycles_modulo", "cycles_map_range_smoothstep" }, functions.Select(f => (string)f!["function"]!).ToArray());
|
||||
var cases = 0;
|
||||
foreach (var entry in functions)
|
||||
{
|
||||
var name = (string)entry!["function"]!;
|
||||
foreach (var line in entry["cases"]!.AsArray())
|
||||
{
|
||||
var sides = ((string)line!).Split(" -> ");
|
||||
var operands = sides[0].Split(' ').Select(OperandOrInfinity).Cast<object>().ToArray();
|
||||
var have = (float)Call(target, name, operands);
|
||||
var ok = sides[1] switch
|
||||
{
|
||||
"nan" => float.IsNaN(have),
|
||||
"inf" => float.IsPositiveInfinity(have),
|
||||
"-inf" => float.IsNegativeInfinity(have),
|
||||
var token => Distance(Float(token), have) == 0,
|
||||
};
|
||||
Assert.True(ok, $"{target} {name} {sides[0]}: Blender {sides[1]}, here {have}");
|
||||
cases++;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(cases >= 45, $"{cases} infinite-operand cases");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Targets))]
|
||||
public void InfiniteOperandsFollowTheCyclesSource(string target)
|
||||
{
|
||||
// Blender 5.2.2 (infinite.json rows named beside each pin). powf(a, +-inf) is never negative, so an infinite
|
||||
// exponent is even; modulo and smoothstep of an infinity are NaN where inf - inf or inf / inf forms.
|
||||
float One(string function, params float[] arguments) => (float)Call(target, function, arguments.Cast<object>().ToArray());
|
||||
const float inf = float.PositiveInfinity;
|
||||
Assert.Equal(0x3f800000, BitConverter.SingleToInt32Bits(One("cycles_power", -1.0f, inf)));
|
||||
Assert.Equal(0x3f800000, BitConverter.SingleToInt32Bits(One("cycles_power", -1.0f, -inf)));
|
||||
Assert.Equal(inf, One("cycles_power", -2.0f, inf));
|
||||
Assert.Equal(inf, One("cycles_power", -0.5f, -inf));
|
||||
Assert.Equal(0, BitConverter.SingleToInt32Bits(One("cycles_power", -2.0f, -inf)));
|
||||
Assert.Equal(-8.0f, One("cycles_power", -2.0f, 3.0f));
|
||||
Assert.Equal(0x3f800000, BitConverter.SingleToInt32Bits(One("cycles_power", -1.0f, 2.0f)));
|
||||
Assert.True(float.IsNaN(One("cycles_modulo", inf, 1.0f)));
|
||||
Assert.True(float.IsNaN(One("cycles_modulo", -inf, 3.0f)));
|
||||
Assert.True(float.IsNaN(One("cycles_modulo", inf, 0.1f)));
|
||||
Assert.True(float.IsNaN(One("cycles_map_range_smoothstep", -inf, -inf, 1.0f, 2.0f, 3.0f)));
|
||||
Assert.True(float.IsNaN(One("cycles_map_range_smoothstep", -inf, 1.0f, -inf, 2.0f, 3.0f)));
|
||||
Assert.Equal(2.0f, One("cycles_map_range_smoothstep", -inf, 1.0f, 5.0f, 2.0f, 3.0f));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Targets))]
|
||||
public void InverseTrigonometryFollowsTheCyclesGuards(string target)
|
||||
|
|
|
|||
|
|
@ -516,6 +516,109 @@ def family_ramp():
|
|||
]
|
||||
|
||||
|
||||
# ---- infinite operands ----
|
||||
|
||||
INF = float("inf")
|
||||
|
||||
# Rows of (function, node, operands) whose operands may be an infinity. The film writes 0 for an infinite operand and
|
||||
# for an infinite or NaN result, so neither can travel through an image: the operand is made in-kernel (+-1 times
|
||||
# exp(100), which overflows to +-inf) and the result is read back through three comparisons with 0.
|
||||
INFINITE = [
|
||||
("cycles_power", "POWER", [
|
||||
(-1.0, INF), (-1.0, -INF), (-2.0, INF), (-2.0, -INF), (-0.5, INF), (-0.5, -INF), (-0.0, INF), (-0.0, -INF),
|
||||
(0.5, INF), (0.5, -INF), (2.0, INF), (2.0, -INF), (1.0, INF), (0.0, INF), (0.0, -INF), (INF, 3.0), (-INF, 3.0),
|
||||
(-INF, 2.0), (INF, -1.0), (-INF, -1.0), (-INF, INF), (INF, INF)]),
|
||||
("cycles_modulo", "MODULO", [
|
||||
(INF, 1.0), (-INF, 1.0), (INF, 3.0), (-INF, 3.0), (INF, 0.1), (-INF, 0.1), (INF, -2.0), (5.0, INF), (-5.0, INF),
|
||||
(5.0, -INF), (INF, INF), (-INF, INF), (0.0, INF), (INF, 0.0)]),
|
||||
]
|
||||
|
||||
# Map Range, Smooth Step: (value, from_min, from_max, to_min, to_max).
|
||||
INFINITE_SMOOTHSTEP = [
|
||||
(-INF, -INF, 1.0, 2.0, 3.0), (-INF, 1.0, -INF, 2.0, 3.0), (-INF, 1.0, 5.0, 2.0, 3.0), (-INF, -5.0, 1.0, 2.0, 3.0),
|
||||
(INF, -5.0, 1.0, 2.0, 3.0), (INF, 1.0, -5.0, 2.0, 3.0), (INF, INF, 1.0, 2.0, 3.0), (INF, 1.0, INF, 2.0, 3.0),
|
||||
(-INF, INF, 1.0, 2.0, 3.0), (-INF, 1.0, INF, 2.0, 3.0), (1.0, -INF, INF, 2.0, 3.0),
|
||||
]
|
||||
|
||||
|
||||
def result_token(value, greater, less, equal):
|
||||
"""A result as the fixture writes it: float32 bits, or inf / -inf / nan where the film showed 0 and the comparisons say why."""
|
||||
if bits(value) != "00000000" and bits(value) != "80000000":
|
||||
return bits(value)
|
||||
if equal:
|
||||
return bits(value)
|
||||
return "inf" if greater else ("-inf" if less else "nan")
|
||||
|
||||
|
||||
def infinite_entry(function, node_label, build_node, rows):
|
||||
count = len(rows[0])
|
||||
value_columns = []
|
||||
flag_columns = []
|
||||
for k in range(count):
|
||||
value_columns.append(f32([(0.0 if r >= len(rows) else (float(np.sign(rows[r][k])) if np.isinf(rows[r][k]) else rows[r][k])) for r in range(CASES)]))
|
||||
flag_columns.append(f32([(0.0 if r >= len(rows) else (1.0 if np.isinf(rows[r][k]) else 0.0)) for r in range(CASES)]))
|
||||
images = pack_operands(value_columns + flag_columns)
|
||||
|
||||
def build(tree, imgs):
|
||||
sockets = operand_sockets(tree, imgs, 2 * count)
|
||||
operands = []
|
||||
for k in range(count):
|
||||
hundred = tree.nodes.new("ShaderNodeMath")
|
||||
hundred.operation = "MULTIPLY"
|
||||
tree.links.new(sockets[count + k], hundred.inputs[0])
|
||||
hundred.inputs[1].default_value = 100.0
|
||||
overflow = tree.nodes.new("ShaderNodeMath")
|
||||
overflow.operation = "EXPONENT"
|
||||
tree.links.new(hundred.outputs[0], overflow.inputs[0])
|
||||
made = tree.nodes.new("ShaderNodeMath")
|
||||
made.operation = "MULTIPLY"
|
||||
tree.links.new(sockets[k], made.inputs[0])
|
||||
tree.links.new(overflow.outputs[0], made.inputs[1])
|
||||
operands.append(made.outputs[0])
|
||||
result = build_node(tree, operands)
|
||||
probes = []
|
||||
for operation in ("GREATER_THAN", "LESS_THAN", "COMPARE"):
|
||||
probe = tree.nodes.new("ShaderNodeMath")
|
||||
probe.operation = operation
|
||||
tree.links.new(result, probe.inputs[0])
|
||||
probe.inputs[1].default_value = 0.0
|
||||
if operation == "COMPARE":
|
||||
probe.inputs[2].default_value = 0.0
|
||||
probes.append(probe.outputs[0])
|
||||
return [result] + probes
|
||||
|
||||
value, greater, less, equal = bake(function + ".infinite", images, build, 4)
|
||||
cases = []
|
||||
for r, row in enumerate(rows):
|
||||
operands = " ".join(("inf" if x == INF else "-inf" if x == -INF else bits(x)) for x in row)
|
||||
cases.append("%s -> %s" % (operands, result_token(value[r], greater[r] > 0.5, less[r] > 0.5, equal[r] > 0.5)))
|
||||
return {"function": function, "node": node_label, "cases": cases}
|
||||
|
||||
|
||||
def family_infinite():
|
||||
def math_node(operation):
|
||||
def build(tree, operands):
|
||||
node = tree.nodes.new("ShaderNodeMath")
|
||||
node.operation = operation
|
||||
for k, socket in enumerate(operands):
|
||||
tree.links.new(socket, node.inputs[k])
|
||||
return node.outputs[0]
|
||||
return build
|
||||
|
||||
def smoothstep(tree, operands):
|
||||
node = tree.nodes.new("ShaderNodeMapRange")
|
||||
node.data_type = "FLOAT"
|
||||
node.interpolation_type = "SMOOTHSTEP"
|
||||
node.clamp = False
|
||||
for identifier, socket in zip(["Value", "From Min", "From Max", "To Min", "To Max"], operands):
|
||||
tree.links.new(socket, by_identifier(node.inputs, identifier))
|
||||
return node.outputs["Result"]
|
||||
|
||||
functions = [infinite_entry(function, "ShaderNodeMath " + operation, math_node(operation), rows) for function, operation, rows in INFINITE]
|
||||
functions.append(infinite_entry("cycles_map_range_smoothstep", "ShaderNodeMapRange SMOOTHSTEP", smoothstep, INFINITE_SMOOTHSTEP))
|
||||
return functions
|
||||
|
||||
|
||||
FAMILIES = {
|
||||
"math": family_math,
|
||||
"map-range": family_map_range,
|
||||
|
|
@ -523,6 +626,7 @@ FAMILIES = {
|
|||
"mix": family_mix,
|
||||
"rgb-to-bw": family_rgb_to_bw,
|
||||
"ramp": family_ramp,
|
||||
"infinite": family_infinite,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -42,7 +42,8 @@ float cultmath_cycles_power(float a, float b)
|
|||
if (a < 0.0)
|
||||
{
|
||||
float magnitude = pow(-a, b);
|
||||
return b - 2.0 * floor(0.5 * b) != 0.0 ? -magnitude : magnitude;
|
||||
// Odd exactly when the remainder is 1: an infinite exponent leaves NaN here and is even, as powf(a, +-inf) is never negative.
|
||||
return b - 2.0 * floor(0.5 * b) == 1.0 ? -magnitude : magnitude;
|
||||
}
|
||||
return pow(a, b);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue