From 559800f494945f9930f2b66f4118175c05a28523 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 12 Oct 2026 03:59:59 +0200 Subject: [PATCH] CultMath: Cycles node semantics (Math, Map Range, Clamp, Mix, RGB to BW, ramp index) as math.Cycles.cs and CultMath.Cycles.hlsl under Apache-2.0 Ports of Blender v5.2.2's intern/cycles kernel functions, one function per node operation, with the HLSL mirror, the Unity copy, the lowering table entry for a separate Apache-2.0 GLSL file, the golden-fixture families, the package licence expressions and the notices section. Builds. Does not pass yet: CultMath.Cycles.glsl and the regenerated golden fixture arrive in a later commit, so CommittedGlslEqualsLowering and GoldenFixtureMatchesCSharp fail at this commit. Co-Authored-By: Claude Sonnet 5.5 --- packages/cultmath/THIRD-PARTY-NOTICES.md | 29 ++ .../cultmath/scripts/build-unity-package.ps1 | 4 +- .../cultmath/shaders/CultMath.Cycles.hlsl | 343 ++++++++++++++++ .../shaders/CultMath.Cycles.hlsl.meta | 7 + packages/cultmath/shaders/CultMath.hlsl | 1 + .../cultmath/src/CultMath/CultMath.csproj | 5 +- packages/cultmath/src/CultMath/math.Cycles.cs | 368 ++++++++++++++++++ .../cultmath/src/CultMath/math.Cycles.cs.meta | 2 + .../tests/CultMath.Tests/GlslLowering.cs | 2 + .../tests/CultMath.Tests/GlslMirrorTests.cs | 42 +- .../tests/CultMath.Tests/HlslMirrorTests.cs | 1 + .../unity/org.gamecult.cultmath/LICENSE.md | 13 +- .../Shaders/CultMath.Cycles.hlsl | 343 ++++++++++++++++ .../Shaders/CultMath.Cycles.hlsl.meta | 7 + .../THIRD-PARTY-NOTICES.md | 29 ++ .../unity/org.gamecult.cultmath/package.json | 2 +- 16 files changed, 1189 insertions(+), 9 deletions(-) create mode 100644 packages/cultmath/shaders/CultMath.Cycles.hlsl create mode 100644 packages/cultmath/shaders/CultMath.Cycles.hlsl.meta create mode 100644 packages/cultmath/src/CultMath/math.Cycles.cs create mode 100644 packages/cultmath/src/CultMath/math.Cycles.cs.meta create mode 100644 packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl create mode 100644 packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl.meta diff --git a/packages/cultmath/THIRD-PARTY-NOTICES.md b/packages/cultmath/THIRD-PARTY-NOTICES.md index b0d2f4c1..041f048b 100644 --- a/packages/cultmath/THIRD-PARTY-NOTICES.md +++ b/packages/cultmath/THIRD-PARTY-NOTICES.md @@ -22,6 +22,35 @@ permutation and scale as `snoise(float3)`. - Upstream: - License: MIT. +## Cycles node semantics + +`math.cycles_*` and `cultmath_cycles_*` port what Blender's Cycles renderer +computes for the Math, Map Range, Clamp, Mix and RGB to BW nodes and for a Color +Ramp lookup. Each function's comment names the Cycles file it follows, all from +`intern/cycles` at the Blender `v5.2.2` tag: `kernel/svm/math_util.h`, +`kernel/svm/map_range.h`, `kernel/svm/clamp.h`, `kernel/svm/mix.h`, +`kernel/svm/color_util.h`, `kernel/svm/ramp.h`, `kernel/svm/convert.h`, +`util/math_base.h`, `util/math_float3.h` and `util/color.h`. The code was +changed: Cycles C++ became C# and HLSL, one function per node operation, with +the nodes' socket defaults and clamp options left to the caller, and the modulo +is the quotient form every CultMath target shares rather than `fmodf`. Nothing +was read from Blender's GPL sources (`source/blender/gpu`, `source/blender/blenlib`). + +- Copyright 2011-2022 Blender Foundation. +- Upstream: (`intern/cycles`). +- License: Apache-2.0 (), which is + per-file here: it covers `src/CultMath/math.Cycles.cs`, + `shaders/CultMath.Cycles.hlsl` and the Unity copy of that include, and + `shaders/CultMath.Cycles.glsl`, which is generated from that include. Each + carries the Apache-2.0 header and this attribution. The rest of CultMath, the + generated GLSL library included, is MIT or MPL-2.0 as above, so the + `GameCult.Math` NuGet package and the `org.gamecult.cultmath` Unity package + both declare `MIT AND MPL-2.0 AND Apache-2.0` and ship this file. +- The test fixtures under `tests/CultMath.Tests/fixtures/cycles-oracle` are + results of running Blender 5.2.2 through `tools/cycles-oracle/oracle.py` + (GPL-3.0-or-later, a Blender Python script, shipped neither in the NuGet + package nor in the Unity package). + ## Phacelle Noise `math.phacelle` and `cultmath_phacelle` follow Rune Skovbo Johansen's Phacelle diff --git a/packages/cultmath/scripts/build-unity-package.ps1 b/packages/cultmath/scripts/build-unity-package.ps1 index f232d6eb..a1e16282 100644 --- a/packages/cultmath/scripts/build-unity-package.ps1 +++ b/packages/cultmath/scripts/build-unity-package.ps1 @@ -86,7 +86,9 @@ $requiredTemplateFiles = @( "Shaders\CultMath.Interval.hlsl", "Shaders\CultMath.Interval.hlsl.meta", "Shaders\CultMath.Affine.hlsl", - "Shaders\CultMath.Affine.hlsl.meta" + "Shaders\CultMath.Affine.hlsl.meta", + "Shaders\CultMath.Cycles.hlsl", + "Shaders\CultMath.Cycles.hlsl.meta" ) foreach ($relativePath in $requiredTemplateFiles) { if (-not (Test-Path -LiteralPath (Join-Path $templateRoot $relativePath) -PathType Leaf)) { diff --git a/packages/cultmath/shaders/CultMath.Cycles.hlsl b/packages/cultmath/shaders/CultMath.Cycles.hlsl new file mode 100644 index 00000000..60aa0ce0 --- /dev/null +++ b/packages/cultmath/shaders/CultMath.Cycles.hlsl @@ -0,0 +1,343 @@ +#ifndef CULTMATH_CYCLES_HLSL +#define CULTMATH_CYCLES_HLSL + +// SPDX-License-Identifier: Apache-2.0 +// +// Cycles node semantics: what Blender's Cycles renderer computes for the Math, Map Range, Clamp, Mix and +// RGB to BW nodes and for a Color Ramp lookup, ported function by function from the Cycles kernel +// (intern/cycles of the Blender v5.2.2 tag; SPDX-FileCopyrightText: 2011-2022 Blender Foundation, Apache-2.0): +// kernel/svm/{math_util,map_range,clamp,mix,color_util,ramp,convert}.h, util/math_base.h, util/color.h and +// util/math_float3.h. Each function's comment names its Cycles source. Modified by GameCult: Cycles C++ +// becomes HLSL, a function per node operation, and the node's socket defaults and clamp options are left to +// the caller. This file is Apache-2.0 (section 4: it was changed); CultMath.hlsl, which includes it after its +// own definitions, is MIT (see THIRD-PARTY-NOTICES.md). math.Cycles.cs mirrors it bit for bit. +// +// Operands are finite floats. NaN and infinite operands are outside the oracle's reach and the mirror's. + +// FLT_EPSILON, the least epsilon a Compare node honours. +static const float CULTMATH_CYCLES_FLT_EPSILON = 1.1920929e-07; + +// The scene-linear luminance coefficients Cycles reads from Blender 5.2.2's default OCIO config +// (kernel/film linear_rgb_to_gray). The oracle fixture pins them with a case per primary. +static const float CULTMATH_CYCLES_RGB_TO_Y_R = 0.2126729; +static const float CULTMATH_CYCLES_RGB_TO_Y_G = 0.7151522; +static const float CULTMATH_CYCLES_RGB_TO_Y_B = 0.0721750; + +// ---- Math node (kernel/svm/math_util.h svm_math; util/math_base.h) ---- + +// safe_divide +float cultmath_cycles_divide(float a, float b) { return b != 0.0 ? a / b : 0.0; } + +// safe_powf with compatible_powf. A negative base takes an integral exponent's parity; a base below zero +// with a fractional exponent is 0, and so is one with an exponent past the int range, where Cycles' int +// round trip cannot hold it. +float cultmath_cycles_power(float a, float b) +{ + float whole = b >= 0.0 ? floor(b) : ceil(b); + if (a < 0.0 && (b != whole || b >= 2147483648.0 || b < -2147483648.0)) + return 0.0; + if (b == 0.0) + return 1.0; + if (a == 0.0) + return 0.0; + if (a < 0.0) + { + float magnitude = pow(-a, b); + return b - 2.0 * floor(0.5 * b) != 0.0 ? -magnitude : magnitude; + } + return pow(a, b); +} + +// safe_logf +float cultmath_cycles_logarithm(float a, float b) +{ + if (a <= 0.0 || b <= 0.0) + return 0.0; + float denominator = log(b); + return denominator != 0.0 ? log(a) / denominator : 0.0; +} + +// safe_sqrtf +float cultmath_cycles_sqrt(float a) { return sqrt(max(a, 0.0)); } + +// inversesqrtf +float cultmath_cycles_inverse_sqrt(float a) { return a > 0.0 ? 1.0 / sqrt(a) : 0.0; } + +// safe_modulo is fmodf, the remainder with the dividend's sign. GLSL has no fmod and the HLSL one is this +// quotient form on a GPU, so this is the form every CultMath target shares; it is not fmodf bit for bit, +// and CyclesOracleTests states how far it is. +float cultmath_cycles_modulo(float a, float b) +{ + float quotient = a / b; + return b != 0.0 ? a - b * (quotient >= 0.0 ? floor(quotient) : ceil(quotient)) : 0.0; +} + +// safe_floored_modulo +float cultmath_cycles_floored_modulo(float a, float b) { return b != 0.0 ? a - floor(a / b) * b : 0.0; } + +// wrapf (note the argument order: value, max, min) +float cultmath_cycles_wrap(float value, float max_value, float min_value) +{ + float range = max_value - min_value; + return range != 0.0 ? value - (range * floor((value - min_value) / range)) : min_value; +} + +// floorf(safe_divide(a, b)) * b +float cultmath_cycles_snap(float a, float b) { return floor(cultmath_cycles_divide(a, b)) * b; } + +// pingpongf +float cultmath_cycles_pingpong(float a, float b) +{ + float scaled = (a - b) / (b * 2.0); + return b != 0.0 ? abs((scaled - floor(scaled)) * b * 2.0 - b) : 0.0; +} + +// smoothminf +float cultmath_cycles_smooth_min(float a, float b, float k) +{ + float h = max(k - abs(a - b), 0.0) / k; + return k != 0.0 ? min(a, b) - h * h * h * k * 0.16666667 : min(a, b); +} + +// -smoothminf(-a, -b, k) +float cultmath_cycles_smooth_max(float a, float b, float k) { return -cultmath_cycles_smooth_min(-a, -b, k); } + +// NODE_MATH_COMPARE +float cultmath_cycles_compare(float a, float b, float epsilon) +{ + return (a == b || abs(a - b) <= max(epsilon, CULTMATH_CYCLES_FLT_EPSILON)) ? 1.0 : 0.0; +} + +// NODE_MATH_ROUND +float cultmath_cycles_round(float a) { return floor(a + 0.5); } + +// NODE_MATH_TRUNC +float cultmath_cycles_trunc(float a) { return a >= 0.0 ? floor(a) : ceil(a); } + +// NODE_MATH_FRACTION +float cultmath_cycles_fract(float a) { return a - floor(a); } + +// compatible_signf +float cultmath_cycles_sign(float a) { return a == 0.0 ? 0.0 : (a < 0.0 ? -1.0 : 1.0); } + +// ---- Map Range node (kernel/svm/map_range.h svm_node_map_range) ---- +// A Map Range node's Clamp option is a Clamp node of type Range on its result (MapRangeNode::expand): +// compose cultmath_cycles_clamp_range over these. + +float cultmath_cycles_map_range_linear(float value, float from_min, float from_max, float to_min, float to_max) +{ + float factor = (value - from_min) / (from_max - from_min); + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0; +} + +float cultmath_cycles_map_range_stepped(float value, float from_min, float from_max, float to_min, float to_max, float steps) +{ + float factor = (value - from_min) / (from_max - from_min); + factor = steps > 0.0 ? floor(factor * (steps + 1.0)) / steps : 0.0; + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0; +} + +// The value itself, not a normalized one, goes through smoothstep(from_min, from_max, .), flipped when +// from_min > from_max (util/math_base.h smoothstep). +float cultmath_cycles_map_range_smoothstep(float value, float from_min, float from_max, float to_min, float to_max) +{ + float low = min(from_min, from_max); + float high = max(from_min, from_max); + float t = (value - low) / (high - low); + float eased = value < low ? 0.0 : (value >= high ? 1.0 : (3.0 - 2.0 * t) * (t * t)); + float factor = from_min > from_max ? 1.0 - eased : eased; + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0; +} + +// smootherstep of map_range.h, flipped when from_min > from_max. +float cultmath_cycles_map_range_smootherstep(float value, float from_min, float from_max, float to_min, float to_max) +{ + float low = min(from_min, from_max); + float high = max(from_min, from_max); + float x = clamp(cultmath_cycles_divide(value - low, high - low), 0.0, 1.0); + float eased = x * x * x * (x * (x * 6.0 - 15.0) + 10.0); + float factor = from_min > from_max ? 1.0 - eased : eased; + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0; +} + +// ---- Clamp node (kernel/svm/clamp.h svm_node_clamp) ---- + +// Type Min Max +float cultmath_cycles_clamp_minmax(float value, float min_value, float max_value) { return clamp(value, min_value, max_value); } + +// Type Range: the bounds swap when min_value > max_value. +float cultmath_cycles_clamp_range(float value, float min_value, float max_value) +{ + return min_value > max_value ? clamp(value, max_value, min_value) : clamp(value, min_value, max_value); +} + +// ---- Mix node (kernel/svm/mix.h, color_util.h) ---- +// Factor clamping (svm_node_mix_color's saturatef) and result clamping (saturate) are the caller's. + +// svm_node_mix_float +float cultmath_cycles_mix_float(float t, float a, float b) { return a * (1.0 - t) + b * t; } + +// svm_node_mix_vector +float3 cultmath_cycles_mix_vector(float t, float3 a, float3 b) { return a * (float3(1.0, 1.0, 1.0) - t) + b * t; } + +float3 cultmath_cycles_mix_blend(float t, float3 col1, float3 col2) { return col1 + t * (col2 - col1); } + +float3 cultmath_cycles_mix_add(float t, float3 col1, float3 col2) { return col1 + t * ((col1 + col2) - col1); } + +float3 cultmath_cycles_mix_mul(float t, float3 col1, float3 col2) { return col1 + t * (col1 * col2 - col1); } + +float3 cultmath_cycles_mix_screen(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + float3 one = float3(1.0, 1.0, 1.0); + return one - (float3(tm, tm, tm) + t * (one - col2)) * (one - col1); +} + +float3 cultmath_cycles_mix_overlay(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + return float3( + col1.x < 0.5 ? col1.x * (tm + 2.0 * t * col2.x) : 1.0 - (tm + 2.0 * t * (1.0 - col2.x)) * (1.0 - col1.x), + col1.y < 0.5 ? col1.y * (tm + 2.0 * t * col2.y) : 1.0 - (tm + 2.0 * t * (1.0 - col2.y)) * (1.0 - col1.y), + col1.z < 0.5 ? col1.z * (tm + 2.0 * t * col2.z) : 1.0 - (tm + 2.0 * t * (1.0 - col2.z)) * (1.0 - col1.z)); +} + +float3 cultmath_cycles_mix_sub(float t, float3 col1, float3 col2) { return col1 + t * ((col1 - col2) - col1); } + +float3 cultmath_cycles_mix_div(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + return float3( + col2.x != 0.0 ? tm * col1.x + t * col1.x / col2.x : col1.x, + col2.y != 0.0 ? tm * col1.y + t * col1.y / col2.y : col1.y, + col2.z != 0.0 ? tm * col1.z + t * col1.z / col2.z : col1.z); +} + +float3 cultmath_cycles_mix_diff(float t, float3 col1, float3 col2) { return col1 + t * (abs(col1 - col2) - col1); } + +float3 cultmath_cycles_mix_exclusion(float t, float3 col1, float3 col2) +{ + return max(col1 + t * ((col1 + col2 - 2.0 * col1 * col2) - col1), float3(0.0, 0.0, 0.0)); +} + +float3 cultmath_cycles_mix_dark(float t, float3 col1, float3 col2) { return col1 + t * (min(col1, col2) - col1); } + +float3 cultmath_cycles_mix_light(float t, float3 col1, float3 col2) { return col1 + t * (max(col1, col2) - col1); } + +float3 cultmath_cycles_mix_dodge(float t, float3 col1, float3 col2) +{ + float3 divisor = float3(1.0, 1.0, 1.0) - t * col2; + return float3( + col1.x != 0.0 ? (divisor.x <= 0.0 ? 1.0 : min(col1.x / divisor.x, 1.0)) : col1.x, + col1.y != 0.0 ? (divisor.y <= 0.0 ? 1.0 : min(col1.y / divisor.y, 1.0)) : col1.y, + col1.z != 0.0 ? (divisor.z <= 0.0 ? 1.0 : min(col1.z / divisor.z, 1.0)) : col1.z); +} + +float3 cultmath_cycles_mix_burn(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + float3 divisor = tm + t * col2; + float3 burnt = float3(1.0, 1.0, 1.0) - (float3(1.0, 1.0, 1.0) - col1) / divisor; + return float3( + divisor.x <= 0.0 ? 0.0 : clamp(burnt.x, 0.0, 1.0), + divisor.y <= 0.0 ? 0.0 : clamp(burnt.y, 0.0, 1.0), + divisor.z <= 0.0 ? 0.0 : clamp(burnt.z, 0.0, 1.0)); +} + +// rgb_to_hsv of util/color.h +float3 cultmath_cycles_rgb_to_hsv(float3 rgb) +{ + float cmax = max(rgb.x, max(rgb.y, rgb.z)); + float cmin = min(rgb.x, min(rgb.y, rgb.z)); + float cdelta = cmax - cmin; + float s = cmax != 0.0 ? cdelta / cmax : 0.0; + float3 c = (float3(cmax, cmax, cmax) - rgb) / cdelta; + float h = rgb.x == cmax ? c.z - c.y : (rgb.y == cmax ? 2.0 + c.x - c.z : 4.0 + c.y - c.x); + h = h / 6.0; + h = h < 0.0 ? h + 1.0 : h; + return float3(s != 0.0 ? h : 0.0, s, cmax); +} + +// hsv_to_rgb of util/color.h +float3 cultmath_cycles_hsv_to_rgb(float3 hsv) +{ + float h = hsv.x == 1.0 ? 0.0 : hsv.x; + h = h * 6.0; + float i = floor(h); + float f = h - i; + float p = hsv.z * (1.0 - hsv.y); + float q = hsv.z * (1.0 - (hsv.y * f)); + float t = hsv.z * (1.0 - (hsv.y * (1.0 - f))); + float3 rgb = i == 0.0 ? float3(hsv.z, t, p) : + (i == 1.0 ? float3(q, hsv.z, p) : + (i == 2.0 ? float3(p, hsv.z, t) : + (i == 3.0 ? float3(p, q, hsv.z) : + (i == 4.0 ? float3(t, p, hsv.z) : float3(hsv.z, p, q))))); + return hsv.y != 0.0 ? rgb : float3(hsv.z, hsv.z, hsv.z); +} + +float3 cultmath_cycles_mix_hue(float t, float3 col1, float3 col2) +{ + float3 hsv2 = cultmath_cycles_rgb_to_hsv(col2); + float3 hsv = cultmath_cycles_rgb_to_hsv(col1); + float3 shifted = cultmath_cycles_hsv_to_rgb(float3(hsv2.x, hsv.y, hsv.z)); + return hsv2.y != 0.0 ? col1 + t * (shifted - col1) : col1; +} + +float3 cultmath_cycles_mix_sat(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + float3 hsv = cultmath_cycles_rgb_to_hsv(col1); + float3 hsv2 = cultmath_cycles_rgb_to_hsv(col2); + return hsv.y != 0.0 ? cultmath_cycles_hsv_to_rgb(float3(hsv.x, tm * hsv.y + t * hsv2.y, hsv.z)) : col1; +} + +float3 cultmath_cycles_mix_val(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + float3 hsv = cultmath_cycles_rgb_to_hsv(col1); + float3 hsv2 = cultmath_cycles_rgb_to_hsv(col2); + return cultmath_cycles_hsv_to_rgb(float3(hsv.x, hsv.y, tm * hsv.z + t * hsv2.z)); +} + +float3 cultmath_cycles_mix_color(float t, float3 col1, float3 col2) +{ + float3 hsv2 = cultmath_cycles_rgb_to_hsv(col2); + float3 hsv = cultmath_cycles_rgb_to_hsv(col1); + float3 shifted = cultmath_cycles_hsv_to_rgb(float3(hsv2.x, hsv2.y, hsv.z)); + return hsv2.y != 0.0 ? col1 + t * (shifted - col1) : col1; +} + +float3 cultmath_cycles_mix_soft(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + float3 one = float3(1.0, 1.0, 1.0); + float3 scr = one - (one - col2) * (one - col1); + return tm * col1 + t * ((one - col1) * col2 * col1 + col1 * scr); +} + +float3 cultmath_cycles_mix_linear(float t, float3 col1, float3 col2) +{ + return col1 + t * (2.0 * col2 + float3(-1.0, -1.0, -1.0)); +} + +// ---- RGB to BW node (kernel/svm/convert.h NODE_CONVERT_CF) ---- + +float cultmath_cycles_rgb_to_bw(float3 color) +{ + return color.x * CULTMATH_CYCLES_RGB_TO_Y_R + color.y * CULTMATH_CYCLES_RGB_TO_Y_G + color.z * CULTMATH_CYCLES_RGB_TO_Y_B; +} + +// ---- Color Ramp lookup (kernel/svm/ramp.h rgb_ramp_lookup, without extrapolation) ---- + +// 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. +float2 cultmath_cycles_ramp_index(float t, int size) +{ + float f = (t > 0.0 ? (t < 1.0 ? t : 1.0) : 0.0) * (size - 1); + int index = clamp((int)f, 0, size - 1); + return float2(index, f - index); +} + +#endif diff --git a/packages/cultmath/shaders/CultMath.Cycles.hlsl.meta b/packages/cultmath/shaders/CultMath.Cycles.hlsl.meta new file mode 100644 index 00000000..c455a809 --- /dev/null +++ b/packages/cultmath/shaders/CultMath.Cycles.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: e7f15f6042b54d728ada5e82dee741b4 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/packages/cultmath/shaders/CultMath.hlsl b/packages/cultmath/shaders/CultMath.hlsl index 6856a000..c29fd603 100644 --- a/packages/cultmath/shaders/CultMath.hlsl +++ b/packages/cultmath/shaders/CultMath.hlsl @@ -509,6 +509,7 @@ CultCellular cultmath_cellular(float3 p) #include "CultMath.Phacelle.hlsl" #include "CultMath.Interval.hlsl" +#include "CultMath.Cycles.hlsl" #include "CultMath.Affine.hlsl" float cultmath_value_noise(float2 position) diff --git a/packages/cultmath/src/CultMath/CultMath.csproj b/packages/cultmath/src/CultMath/CultMath.csproj index 6d2f9983..333fb20a 100644 --- a/packages/cultmath/src/CultMath/CultMath.csproj +++ b/packages/cultmath/src/CultMath/CultMath.csproj @@ -8,8 +8,8 @@ CultMath CultMath GameCult.Math - - MIT AND MPL-2.0 + + MIT AND MPL-2.0 AND Apache-2.0 0.4.1 Clean-room C# math shaped for HLSL-style renderer code. GameCult @@ -26,6 +26,7 @@ + diff --git a/packages/cultmath/src/CultMath/math.Cycles.cs b/packages/cultmath/src/CultMath/math.Cycles.cs new file mode 100644 index 00000000..2bb9185a --- /dev/null +++ b/packages/cultmath/src/CultMath/math.Cycles.cs @@ -0,0 +1,368 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Cycles node semantics: what Blender's Cycles renderer computes for the Math, Map Range, Clamp, Mix and +// RGB to BW nodes and for a Color Ramp lookup, ported function by function from the Cycles kernel +// (intern/cycles of the Blender v5.2.2 tag; SPDX-FileCopyrightText: 2011-2022 Blender Foundation, Apache-2.0): +// kernel/svm/{math_util,map_range,clamp,mix,color_util,ramp,convert}.h, util/math_base.h, util/color.h and +// util/math_float3.h. Each function's comment names its Cycles source. Modified by GameCult: Cycles C++ +// becomes C#, a function per node operation, and the node's socket defaults and clamp options are left to +// the caller. This file is Apache-2.0 (section 4: it was changed); the rest of CultMath is MIT, except the +// MPL-2.0 Phacelle files (see THIRD-PARTY-NOTICES.md). shaders/CultMath.Cycles.hlsl mirrors it bit for bit. +// +// Operands are finite floats. NaN and infinite operands are outside the oracle's reach and the mirror's. +// CyclesOracleTests holds every function to the results of Blender 5.2.2's own CPU Cycles. + +namespace CultMath; + +public static partial class math +{ + // FLT_EPSILON, the least epsilon a Compare node honours. + private const float CYCLES_FLT_EPSILON = 1.1920929e-07f; + + /// The scene-linear red luminance coefficient Cycles' RGB to BW node reads from Blender 5.2.2's default OCIO config. + public const float CYCLES_RGB_TO_Y_R = 0.2126729f; + + /// The scene-linear green luminance coefficient Cycles' RGB to BW node reads from Blender 5.2.2's default OCIO config. + public const float CYCLES_RGB_TO_Y_G = 0.7151522f; + + /// The scene-linear blue luminance coefficient Cycles' RGB to BW node reads from Blender 5.2.2's default OCIO config. + public const float CYCLES_RGB_TO_Y_B = 0.0721750f; + + // ---- Math node (kernel/svm/math_util.h svm_math; util/math_base.h) ---- + + /// Math node Divide (safe_divide): a / b, or 0 when b is 0. + public static float cycles_divide(float a, float b) => b != 0.0f ? a / b : 0.0f; + + /// + /// Math node Power (safe_powf with compatible_powf): 0 for a negative base with a fractional exponent, + /// 1 for exponent 0, 0 for base 0. A negative base takes an integral exponent's parity. An exponent past + /// the int range counts as fractional for a negative base, where Cycles' int round trip cannot hold it. + /// + public static float cycles_power(float a, float b) + { + var whole = b >= 0.0f ? floor(b) : ceil(b); + if (a < 0.0f && (b != whole || b >= 2147483648.0f || b < -2147483648.0f)) + return 0.0f; + if (b == 0.0f) + return 1.0f; + if (a == 0.0f) + return 0.0f; + if (a < 0.0f) + { + var magnitude = pow(-a, b); + return b - 2.0f * floor(0.5f * b) != 0.0f ? -magnitude : magnitude; + } + + return pow(a, b); + } + + /// Math node Logarithm (safe_logf): log(a) / log(b), or 0 when a or b is not positive or log(b) is 0. + public static float cycles_logarithm(float a, float b) + { + if (a <= 0.0f || b <= 0.0f) + return 0.0f; + var denominator = log(b); + return denominator != 0.0f ? log(a) / denominator : 0.0f; + } + + /// Math node Square Root (safe_sqrtf): the square root of max(a, 0). + public static float cycles_sqrt(float a) => sqrt(max(a, 0.0f)); + + /// Math node Inverse Square Root (inversesqrtf): 1 / sqrt(a), or 0 when a is not positive. + public static float cycles_inverse_sqrt(float a) => a > 0.0f ? 1.0f / sqrt(a) : 0.0f; + + /// + /// Math node Truncated Modulo (safe_modulo), the remainder with the dividend's sign, or 0 when b is 0. + /// Cycles computes fmodf. GLSL has no fmod and HLSL's is this quotient form on a GPU, so this is the form + /// every CultMath target shares: it is not fmodf bit for bit, and CyclesOracleTests states how far it is. + /// + public static float cycles_modulo(float a, float b) + { + var quotient = a / b; + return b != 0.0f ? a - b * (quotient >= 0.0f ? floor(quotient) : ceil(quotient)) : 0.0f; + } + + /// Math node Floored Modulo (safe_floored_modulo): a - floor(a / b) b, or 0 when b is 0. + public static float cycles_floored_modulo(float a, float b) => b != 0.0f ? a - floor(a / b) * b : 0.0f; + + /// Math node Wrap (wrapf; note the argument order): value wrapped into [min_value, max_value), or min_value for an empty range. + public static float cycles_wrap(float value, float max_value, float min_value) + { + var range = max_value - min_value; + return range != 0.0f ? value - (range * floor((value - min_value) / range)) : min_value; + } + + /// Math node Snap: floor(safe_divide(a, b)) b. + public static float cycles_snap(float a, float b) => floor(cycles_divide(a, b)) * b; + + /// Math node Ping-Pong (pingpongf): a triangle wave of a with period 2 b, or 0 when b is 0. + public static float cycles_pingpong(float a, float b) + { + var scaled = (a - b) / (b * 2.0f); + return b != 0.0f ? abs((scaled - floor(scaled)) * b * 2.0f - b) : 0.0f; + } + + /// Math node Smooth Minimum (smoothminf): the polynomial smooth minimum of width k, or min(a, b) when k is 0. + public static float cycles_smooth_min(float a, float b, float k) + { + var h = max(k - abs(a - b), 0.0f) / k; + return k != 0.0f ? min(a, b) - h * h * h * k * 0.16666667f : min(a, b); + } + + /// Math node Smooth Maximum: -smoothminf(-a, -b, k). + public static float cycles_smooth_max(float a, float b, float k) => -cycles_smooth_min(-a, -b, k); + + /// Math node Compare: 1 when a equals b or they differ by at most max(epsilon, FLT_EPSILON), else 0. + public static float cycles_compare(float a, float b, float epsilon) => + (a == b || abs(a - b) <= max(epsilon, CYCLES_FLT_EPSILON)) ? 1.0f : 0.0f; + + /// Math node Round: floor(a + 0.5). + public static float cycles_round(float a) => floor(a + 0.5f); + + /// Math node Truncate: floor for a >= 0, ceil otherwise. + public static float cycles_trunc(float a) => a >= 0.0f ? floor(a) : ceil(a); + + /// Math node Fraction: a - floor(a). + public static float cycles_fract(float a) => a - floor(a); + + /// Math node Sign (compatible_signf): 0 for 0, -1 below, 1 above. + public static float cycles_sign(float a) => a == 0.0f ? 0.0f : (a < 0.0f ? -1.0f : 1.0f); + + // ---- Map Range node (kernel/svm/map_range.h svm_node_map_range) ---- + // A Map Range node's Clamp option is a Clamp node of type Range on its result (MapRangeNode::expand): + // compose cycles_clamp_range over these. + + /// Map Range, Linear: to_min + (value - from_min) / (from_max - from_min) (to_max - to_min), or 0 when from_max equals from_min. + public static float cycles_map_range_linear(float value, float from_min, float from_max, float to_min, float to_max) + { + var factor = (value - from_min) / (from_max - from_min); + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0f; + } + + /// Map Range, Stepped: the linear factor quantized to `steps` steps (0 when steps is not positive), or 0 when from_max equals from_min. + public static float cycles_map_range_stepped(float value, float from_min, float from_max, float to_min, float to_max, float steps) + { + var factor = (value - from_min) / (from_max - from_min); + factor = steps > 0.0f ? floor(factor * (steps + 1.0f)) / steps : 0.0f; + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0f; + } + + /// + /// Map Range, Smooth Step: the value goes through smoothstep(from_min, from_max, .), flipped when from_min + /// is above from_max (util/math_base.h smoothstep), or 0 when from_max equals from_min. + /// + public static float cycles_map_range_smoothstep(float value, float from_min, float from_max, float to_min, float to_max) + { + var low = min(from_min, from_max); + var high = max(from_min, from_max); + var t = (value - low) / (high - low); + var eased = value < low ? 0.0f : (value >= high ? 1.0f : (3.0f - 2.0f * t) * (t * t)); + var factor = from_min > from_max ? 1.0f - eased : eased; + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0f; + } + + /// Map Range, Smoother Step (map_range.h smootherstep), flipped when from_min is above from_max, or 0 when from_max equals from_min. + public static float cycles_map_range_smootherstep(float value, float from_min, float from_max, float to_min, float to_max) + { + var low = min(from_min, from_max); + var high = max(from_min, from_max); + var x = clamp(cycles_divide(value - low, high - low), 0.0f, 1.0f); + var eased = x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f); + var factor = from_min > from_max ? 1.0f - eased : eased; + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0f; + } + + // ---- Clamp node (kernel/svm/clamp.h svm_node_clamp) ---- + + /// Clamp node, Min Max: clamp(value, min_value, max_value). + public static float cycles_clamp_minmax(float value, float min_value, float max_value) => clamp(value, min_value, max_value); + + /// Clamp node, Range: as Min Max, with the bounds swapped when min_value is above max_value. + public static float cycles_clamp_range(float value, float min_value, float max_value) => + min_value > max_value ? clamp(value, max_value, min_value) : clamp(value, min_value, max_value); + + // ---- Mix node (kernel/svm/mix.h, color_util.h) ---- + // Factor clamping (svm_node_mix_color's saturatef) and result clamping (saturate) are the caller's. + + /// Mix node, Float (svm_node_mix_float): a (1 - t) + b t. + public static float cycles_mix_float(float t, float a, float b) => a * (1.0f - t) + b * t; + + /// Mix node, Vector, uniform factor (svm_node_mix_vector): a (1 - t) + b t per component. + public static float3 cycles_mix_vector(float t, float3 a, float3 b) => a * (new float3(1.0f, 1.0f, 1.0f) - t) + b * t; + + /// Mix node, Mix blend mode (svm_mix_blend). + public static float3 cycles_mix_blend(float t, float3 col1, float3 col2) => col1 + t * (col2 - col1); + + /// Mix node, Add blend mode (svm_mix_add). + public static float3 cycles_mix_add(float t, float3 col1, float3 col2) => col1 + t * ((col1 + col2) - col1); + + /// Mix node, Multiply blend mode (svm_mix_mul). + public static float3 cycles_mix_mul(float t, float3 col1, float3 col2) => col1 + t * (col1 * col2 - col1); + + /// Mix node, Screen blend mode (svm_mix_screen). + public static float3 cycles_mix_screen(float t, float3 col1, float3 col2) + { + var tm = 1.0f - t; + var one = new float3(1.0f, 1.0f, 1.0f); + return one - (new float3(tm, tm, tm) + t * (one - col2)) * (one - col1); + } + + /// Mix node, Overlay blend mode (svm_mix_overlay). + public static float3 cycles_mix_overlay(float t, float3 col1, float3 col2) + { + var tm = 1.0f - t; + return new float3( + col1.x < 0.5f ? col1.x * (tm + 2.0f * t * col2.x) : 1.0f - (tm + 2.0f * t * (1.0f - col2.x)) * (1.0f - col1.x), + col1.y < 0.5f ? col1.y * (tm + 2.0f * t * col2.y) : 1.0f - (tm + 2.0f * t * (1.0f - col2.y)) * (1.0f - col1.y), + col1.z < 0.5f ? col1.z * (tm + 2.0f * t * col2.z) : 1.0f - (tm + 2.0f * t * (1.0f - col2.z)) * (1.0f - col1.z)); + } + + /// Mix node, Subtract blend mode (svm_mix_sub). + public static float3 cycles_mix_sub(float t, float3 col1, float3 col2) => col1 + t * ((col1 - col2) - col1); + + /// Mix node, Divide blend mode (svm_mix_div): a channel whose col2 is 0 keeps col1. + public static float3 cycles_mix_div(float t, float3 col1, float3 col2) + { + var tm = 1.0f - t; + return new float3( + col2.x != 0.0f ? tm * col1.x + t * col1.x / col2.x : col1.x, + col2.y != 0.0f ? tm * col1.y + t * col1.y / col2.y : col1.y, + col2.z != 0.0f ? tm * col1.z + t * col1.z / col2.z : col1.z); + } + + /// Mix node, Difference blend mode (svm_mix_diff). + public static float3 cycles_mix_diff(float t, float3 col1, float3 col2) => col1 + t * (abs(col1 - col2) - col1); + + /// Mix node, Exclusion blend mode (svm_mix_exclusion). + public static float3 cycles_mix_exclusion(float t, float3 col1, float3 col2) => + max(col1 + t * ((col1 + col2 - 2.0f * col1 * col2) - col1), new float3(0.0f, 0.0f, 0.0f)); + + /// Mix node, Darken blend mode (svm_mix_dark). + public static float3 cycles_mix_dark(float t, float3 col1, float3 col2) => col1 + t * (min(col1, col2) - col1); + + /// Mix node, Lighten blend mode (svm_mix_light). + public static float3 cycles_mix_light(float t, float3 col1, float3 col2) => col1 + t * (max(col1, col2) - col1); + + /// Mix node, Dodge blend mode (svm_mix_dodge). + public static float3 cycles_mix_dodge(float t, float3 col1, float3 col2) + { + var divisor = new float3(1.0f, 1.0f, 1.0f) - t * col2; + return new float3( + col1.x != 0.0f ? (divisor.x <= 0.0f ? 1.0f : min(col1.x / divisor.x, 1.0f)) : col1.x, + col1.y != 0.0f ? (divisor.y <= 0.0f ? 1.0f : min(col1.y / divisor.y, 1.0f)) : col1.y, + col1.z != 0.0f ? (divisor.z <= 0.0f ? 1.0f : min(col1.z / divisor.z, 1.0f)) : col1.z); + } + + /// Mix node, Burn blend mode (svm_mix_burn). + public static float3 cycles_mix_burn(float t, float3 col1, float3 col2) + { + var tm = 1.0f - t; + var divisor = tm + t * col2; + var burnt = new float3(1.0f, 1.0f, 1.0f) - (new float3(1.0f, 1.0f, 1.0f) - col1) / divisor; + return new float3( + divisor.x <= 0.0f ? 0.0f : clamp(burnt.x, 0.0f, 1.0f), + divisor.y <= 0.0f ? 0.0f : clamp(burnt.y, 0.0f, 1.0f), + divisor.z <= 0.0f ? 0.0f : clamp(burnt.z, 0.0f, 1.0f)); + } + + /// RGB to HSV as Cycles computes it (util/color.h rgb_to_hsv): hue in [0, 1), saturation, value. + public static float3 cycles_rgb_to_hsv(float3 rgb) + { + var cmax = max(rgb.x, max(rgb.y, rgb.z)); + var cmin = min(rgb.x, min(rgb.y, rgb.z)); + var cdelta = cmax - cmin; + var s = cmax != 0.0f ? cdelta / cmax : 0.0f; + var c = (new float3(cmax, cmax, cmax) - rgb) / cdelta; + var h = rgb.x == cmax ? c.z - c.y : (rgb.y == cmax ? 2.0f + c.x - c.z : 4.0f + c.y - c.x); + h = h / 6.0f; + h = h < 0.0f ? h + 1.0f : h; + return new float3(s != 0.0f ? h : 0.0f, s, cmax); + } + + /// HSV to RGB as Cycles computes it (util/color.h hsv_to_rgb). + public static float3 cycles_hsv_to_rgb(float3 hsv) + { + var h = hsv.x == 1.0f ? 0.0f : hsv.x; + h = h * 6.0f; + var i = floor(h); + var f = h - i; + var p = hsv.z * (1.0f - hsv.y); + var q = hsv.z * (1.0f - (hsv.y * f)); + var t = hsv.z * (1.0f - (hsv.y * (1.0f - f))); + var rgb = i == 0.0f ? new float3(hsv.z, t, p) : + (i == 1.0f ? new float3(q, hsv.z, p) : + (i == 2.0f ? new float3(p, hsv.z, t) : + (i == 3.0f ? new float3(p, q, hsv.z) : + (i == 4.0f ? new float3(t, p, hsv.z) : new float3(hsv.z, p, q))))); + return hsv.y != 0.0f ? rgb : new float3(hsv.z, hsv.z, hsv.z); + } + + /// Mix node, Hue blend mode (svm_mix_hue). + public static float3 cycles_mix_hue(float t, float3 col1, float3 col2) + { + var hsv2 = cycles_rgb_to_hsv(col2); + var hsv = cycles_rgb_to_hsv(col1); + var shifted = cycles_hsv_to_rgb(new float3(hsv2.x, hsv.y, hsv.z)); + return hsv2.y != 0.0f ? col1 + t * (shifted - col1) : col1; + } + + /// Mix node, Saturation blend mode (svm_mix_sat). + public static float3 cycles_mix_sat(float t, float3 col1, float3 col2) + { + var tm = 1.0f - t; + var hsv = cycles_rgb_to_hsv(col1); + var hsv2 = cycles_rgb_to_hsv(col2); + return hsv.y != 0.0f ? cycles_hsv_to_rgb(new float3(hsv.x, tm * hsv.y + t * hsv2.y, hsv.z)) : col1; + } + + /// Mix node, Value blend mode (svm_mix_val). + public static float3 cycles_mix_val(float t, float3 col1, float3 col2) + { + var tm = 1.0f - t; + var hsv = cycles_rgb_to_hsv(col1); + var hsv2 = cycles_rgb_to_hsv(col2); + return cycles_hsv_to_rgb(new float3(hsv.x, hsv.y, tm * hsv.z + t * hsv2.z)); + } + + /// Mix node, Color blend mode (svm_mix_color). + public static float3 cycles_mix_color(float t, float3 col1, float3 col2) + { + var hsv2 = cycles_rgb_to_hsv(col2); + var hsv = cycles_rgb_to_hsv(col1); + var shifted = cycles_hsv_to_rgb(new float3(hsv2.x, hsv2.y, hsv.z)); + return hsv2.y != 0.0f ? col1 + t * (shifted - col1) : col1; + } + + /// Mix node, Soft Light blend mode (svm_mix_soft). + public static float3 cycles_mix_soft(float t, float3 col1, float3 col2) + { + var tm = 1.0f - t; + var one = new float3(1.0f, 1.0f, 1.0f); + var scr = one - (one - col2) * (one - col1); + return tm * col1 + t * ((one - col1) * col2 * col1 + col1 * scr); + } + + /// Mix node, Linear Light blend mode (svm_mix_linear). + public static float3 cycles_mix_linear(float t, float3 col1, float3 col2) => + col1 + t * (2.0f * col2 + new float3(-1.0f, -1.0f, -1.0f)); + + // ---- RGB to BW node (kernel/svm/convert.h NODE_CONVERT_CF) ---- + + /// RGB to BW node: the scene-linear luminance of a colour. + public static float cycles_rgb_to_bw(float3 color) => + color.x * CYCLES_RGB_TO_Y_R + color.y * CYCLES_RGB_TO_Y_G + color.z * CYCLES_RGB_TO_Y_B; + + // ---- Color Ramp lookup (kernel/svm/ramp.h rgb_ramp_lookup, without extrapolation) ---- + + /// + /// 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. + /// + public static float2 cycles_ramp_index(float t, int size) + { + var f = (t > 0.0f ? (t < 1.0f ? t : 1.0f) : 0.0f) * (size - 1); + var index = clamp((int)f, 0, size - 1); + return new float2(index, f - index); + } +} diff --git a/packages/cultmath/src/CultMath/math.Cycles.cs.meta b/packages/cultmath/src/CultMath/math.Cycles.cs.meta new file mode 100644 index 00000000..fc3e97cb --- /dev/null +++ b/packages/cultmath/src/CultMath/math.Cycles.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 0484788fead24b7cb0b8b373bf7e1f9d diff --git a/packages/cultmath/tests/CultMath.Tests/GlslLowering.cs b/packages/cultmath/tests/CultMath.Tests/GlslLowering.cs index eff40196..13e21808 100644 --- a/packages/cultmath/tests/CultMath.Tests/GlslLowering.cs +++ b/packages/cultmath/tests/CultMath.Tests/GlslLowering.cs @@ -21,6 +21,8 @@ internal static class GlslLowering { // Phacelle is a port of MPL-2.0 code, and MPL-2.0 covers the files that carry it. ["CultMath.Phacelle.hlsl"] = ("CultMath.Phacelle.glsl", "MPL-2.0"), + // The Cycles node semantics are a port of Apache-2.0 code, and the licence travels with the file. + ["CultMath.Cycles.hlsl"] = ("CultMath.Cycles.glsl", "Apache-2.0"), }; private const string Regenerate = diff --git a/packages/cultmath/tests/CultMath.Tests/GlslMirrorTests.cs b/packages/cultmath/tests/CultMath.Tests/GlslMirrorTests.cs index f0f61622..4c6243cf 100644 --- a/packages/cultmath/tests/CultMath.Tests/GlslMirrorTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/GlslMirrorTests.cs @@ -503,6 +503,12 @@ public sealed class GlslMirrorTests "Tightness: R0 + d <= GPU radius <= R0 + d + 2^-19 (R0 + |c0|_1 + spread). " + "Report the least slack of each side; only the bounds fail the check."; + // The Cycles node families' operands: values past [0, 1] on both sides, so the clamps, the extrapolating + // factors and the divisions by a small number all show. + private static float CyclesValue(System.Random r) => Uniform(r, -20.0f, 20.0f); + private static float CyclesFactor(System.Random r) => Uniform(r, -0.5f, 1.5f); + private static float3 CyclesColor(System.Random r) => new(Uniform(r, -0.25f, 1.25f), Uniform(r, -0.25f, 1.25f), Uniform(r, -0.25f, 1.25f)); + // A Phacelle stripe wave vector: each component in [-6, 6], about one stripe per cell. private static float3 Side(System.Random r) => new(Uniform(r, -6.0f, 6.0f), Uniform(r, -6.0f, 6.0f), Uniform(r, -6.0f, 6.0f)); @@ -511,7 +517,7 @@ public sealed class GlslMirrorTests // float families are ulp-bounded, with the bound measured on the device by the consumer, and // iv_frustum_ball also carries an enclosure check. phacelle is in CultMath.Phacelle.glsl, so its // evaluator concatenates that file after CultMath.glsl; the site, which does not vendor it, skips it. - private static readonly Family[] Families = + private static readonly Family[] Families = new Family[] { new("float cultmath_snoise(vec3)", "ulp-bounded", r => new object[] { Point3(r) }), new("float cultmath_snoise(vec2)", "ulp-bounded", r => new object[] { new float2(Uniform(r, -50.0f, 50.0f), Uniform(r, -50.0f, 50.0f)) }), @@ -575,7 +581,39 @@ public sealed class GlslMirrorTests new("vec2 cultmath_iv_snoise_ball(vec2, float)", "ulp-bounded", r => new object[] { new float2(Uniform(r, -50.0f, 50.0f), Uniform(r, -50.0f, 50.0f)), Uniform(r, 0.001f, 2.0f) }), new("vec3 cultmath_af_snoise(vec3, vec3, float)", "ulp-bounded, reach 1e-6 to 1e-3", SmallReachSlice, SnoiseSmallForm, Verify: cases => AssertSnoiseForm(cases, allowanceDecides: true)), new("vec3 cultmath_af_fbm(vec3, vec3, float, int, float, float)", "ulp-bounded, reach 1e-6 to 1e-3", r => SmallReachSlice(r).Concat(new object[] { 4, 2.0f, 0.5f }).ToArray(), FbmSmallForm, Verify: cases => AssertFbmForm(cases, allowanceDecides: true)), - }; + new("float cultmath_cycles_divide(float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r) }), + new("float cultmath_cycles_power(float, float)", "ulp-bounded, platform pow", r => new object[] { Uniform(r, -4.0f, 4.0f), r.Next(2) == 0 ? r.Next(-4, 5) : Uniform(r, -3.0f, 3.0f) }, Platform: Platform.Ulp), + new("float cultmath_cycles_logarithm(float, float)", "ulp-bounded, platform log", r => new object[] { Uniform(r, 0.01f, 20.0f), Uniform(r, 0.01f, 20.0f) }, Platform: Platform.Ulp), + new("float cultmath_cycles_sqrt(float)", "ulp-bounded", r => new object[] { CyclesValue(r) }), + new("float cultmath_cycles_inverse_sqrt(float)", "ulp-bounded", r => new object[] { CyclesValue(r) }), + new("float cultmath_cycles_modulo(float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r) }), + new("float cultmath_cycles_floored_modulo(float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r) }), + new("float cultmath_cycles_wrap(float, float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), Uniform(r, -10.0f, 10.0f), Uniform(r, -10.0f, 10.0f) }), + new("float cultmath_cycles_snap(float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r) }), + new("float cultmath_cycles_pingpong(float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r) }), + new("float cultmath_cycles_smooth_min(float, float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r), Uniform(r, -5.0f, 5.0f) }), + new("float cultmath_cycles_smooth_max(float, float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r), Uniform(r, -5.0f, 5.0f) }), + new("float cultmath_cycles_compare(float, float, float)", "ulp-bounded", r => { var a = CyclesValue(r); return new object[] { a, a + Uniform(r, -1.0f, 1.0f) * 1.0e-3f, Uniform(r, 0.0f, 2.0e-3f) }; }), + new("float cultmath_cycles_round(float)", "ulp-bounded", r => new object[] { CyclesValue(r) }), + new("float cultmath_cycles_trunc(float)", "ulp-bounded", r => new object[] { CyclesValue(r) }), + new("float cultmath_cycles_fract(float)", "ulp-bounded", r => new object[] { CyclesValue(r) }), + new("float cultmath_cycles_sign(float)", "ulp-bounded", r => new object[] { r.Next(4) == 0 ? 0.0f : CyclesValue(r) }), + new("float cultmath_cycles_map_range_linear(float, float, float, float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r), CyclesValue(r), CyclesValue(r), CyclesValue(r) }), + new("float cultmath_cycles_map_range_stepped(float, float, float, float, float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r), CyclesValue(r), CyclesValue(r), CyclesValue(r), Uniform(r, -1.0f, 8.0f) }), + new("float cultmath_cycles_map_range_smoothstep(float, float, float, float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r), CyclesValue(r), CyclesValue(r), CyclesValue(r) }), + new("float cultmath_cycles_map_range_smootherstep(float, float, float, float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r), CyclesValue(r), CyclesValue(r), CyclesValue(r) }), + new("float cultmath_cycles_clamp_minmax(float, float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r), CyclesValue(r) }), + new("float cultmath_cycles_clamp_range(float, float, float)", "ulp-bounded", r => new object[] { CyclesValue(r), CyclesValue(r), CyclesValue(r) }), + new("float cultmath_cycles_mix_float(float, float, float)", "ulp-bounded", r => new object[] { CyclesFactor(r), CyclesValue(r), CyclesValue(r) }), + new("vec3 cultmath_cycles_mix_vector(float, vec3, vec3)", "ulp-bounded", r => new object[] { CyclesFactor(r), CyclesColor(r), CyclesColor(r) }), + 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 }), + }.Concat(new[] + { + "blend", "add", "mul", "screen", "overlay", "sub", "div", "diff", "exclusion", "dark", "light", "dodge", "burn", "hue", "sat", "val", "color", "soft", "linear", + }.Select(mode => new Family($"vec3 cultmath_cycles_mix_{mode}(float, vec3, vec3)", "ulp-bounded", r => new object[] { CyclesFactor(r), CyclesColor(r), CyclesColor(r) }))).ToArray(); // The fixture text, each case's arguments given by family and point index. private static string GoldenFixture(Func arguments) diff --git a/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs b/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs index 00a2e90a..477cb613 100644 --- a/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs @@ -67,6 +67,7 @@ public sealed class HlslMirrorTests [InlineData("CultMath.Phacelle.hlsl")] [InlineData("CultMath.Interval.hlsl")] [InlineData("CultMath.Affine.hlsl")] + [InlineData("CultMath.Cycles.hlsl")] public void UnityPackageShaderIsIdenticalToTheCanonicalShader(string name) { var root = GetCultMathRoot(); diff --git a/packages/cultmath/unity/org.gamecult.cultmath/LICENSE.md b/packages/cultmath/unity/org.gamecult.cultmath/LICENSE.md index e31bf344..364306fa 100644 --- a/packages/cultmath/unity/org.gamecult.cultmath/LICENSE.md +++ b/packages/cultmath/unity/org.gamecult.cultmath/LICENSE.md @@ -7,9 +7,16 @@ files, which are licensed under the Mozilla Public License 2.0 (`MPL-2.0`): - the Phacelle sources compiled into `Runtime/Plugins/CultMath.dll` (`math.Phacelle.cs` and `CultPhasor.cs` in the GameCult/CultLib repository) -The package license expression is therefore `MIT AND MPL-2.0`, matching the -`GameCult.Math` NuGet package. MPL-2.0 is file-level: it applies to those files -and not to the rest of CultMath. +and for the Cycles node semantics files, which are licensed under the Apache +License 2.0 (`Apache-2.0`): + +- `Shaders/CultMath.Cycles.hlsl` +- the Cycles sources compiled into `Runtime/Plugins/CultMath.dll` + (`math.Cycles.cs` in the GameCult/CultLib repository) + +The package license expression is therefore `MIT AND MPL-2.0 AND Apache-2.0`, +matching the `GameCult.Math` NuGet package. MPL-2.0 and Apache-2.0 are file-level: +each applies to its files and not to the rest of CultMath. The MPL-2.0 text is available from the Mozilla Foundation at . Source and modification history for the diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl new file mode 100644 index 00000000..60aa0ce0 --- /dev/null +++ b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl @@ -0,0 +1,343 @@ +#ifndef CULTMATH_CYCLES_HLSL +#define CULTMATH_CYCLES_HLSL + +// SPDX-License-Identifier: Apache-2.0 +// +// Cycles node semantics: what Blender's Cycles renderer computes for the Math, Map Range, Clamp, Mix and +// RGB to BW nodes and for a Color Ramp lookup, ported function by function from the Cycles kernel +// (intern/cycles of the Blender v5.2.2 tag; SPDX-FileCopyrightText: 2011-2022 Blender Foundation, Apache-2.0): +// kernel/svm/{math_util,map_range,clamp,mix,color_util,ramp,convert}.h, util/math_base.h, util/color.h and +// util/math_float3.h. Each function's comment names its Cycles source. Modified by GameCult: Cycles C++ +// becomes HLSL, a function per node operation, and the node's socket defaults and clamp options are left to +// the caller. This file is Apache-2.0 (section 4: it was changed); CultMath.hlsl, which includes it after its +// own definitions, is MIT (see THIRD-PARTY-NOTICES.md). math.Cycles.cs mirrors it bit for bit. +// +// Operands are finite floats. NaN and infinite operands are outside the oracle's reach and the mirror's. + +// FLT_EPSILON, the least epsilon a Compare node honours. +static const float CULTMATH_CYCLES_FLT_EPSILON = 1.1920929e-07; + +// The scene-linear luminance coefficients Cycles reads from Blender 5.2.2's default OCIO config +// (kernel/film linear_rgb_to_gray). The oracle fixture pins them with a case per primary. +static const float CULTMATH_CYCLES_RGB_TO_Y_R = 0.2126729; +static const float CULTMATH_CYCLES_RGB_TO_Y_G = 0.7151522; +static const float CULTMATH_CYCLES_RGB_TO_Y_B = 0.0721750; + +// ---- Math node (kernel/svm/math_util.h svm_math; util/math_base.h) ---- + +// safe_divide +float cultmath_cycles_divide(float a, float b) { return b != 0.0 ? a / b : 0.0; } + +// safe_powf with compatible_powf. A negative base takes an integral exponent's parity; a base below zero +// with a fractional exponent is 0, and so is one with an exponent past the int range, where Cycles' int +// round trip cannot hold it. +float cultmath_cycles_power(float a, float b) +{ + float whole = b >= 0.0 ? floor(b) : ceil(b); + if (a < 0.0 && (b != whole || b >= 2147483648.0 || b < -2147483648.0)) + return 0.0; + if (b == 0.0) + return 1.0; + if (a == 0.0) + return 0.0; + if (a < 0.0) + { + float magnitude = pow(-a, b); + return b - 2.0 * floor(0.5 * b) != 0.0 ? -magnitude : magnitude; + } + return pow(a, b); +} + +// safe_logf +float cultmath_cycles_logarithm(float a, float b) +{ + if (a <= 0.0 || b <= 0.0) + return 0.0; + float denominator = log(b); + return denominator != 0.0 ? log(a) / denominator : 0.0; +} + +// safe_sqrtf +float cultmath_cycles_sqrt(float a) { return sqrt(max(a, 0.0)); } + +// inversesqrtf +float cultmath_cycles_inverse_sqrt(float a) { return a > 0.0 ? 1.0 / sqrt(a) : 0.0; } + +// safe_modulo is fmodf, the remainder with the dividend's sign. GLSL has no fmod and the HLSL one is this +// quotient form on a GPU, so this is the form every CultMath target shares; it is not fmodf bit for bit, +// and CyclesOracleTests states how far it is. +float cultmath_cycles_modulo(float a, float b) +{ + float quotient = a / b; + return b != 0.0 ? a - b * (quotient >= 0.0 ? floor(quotient) : ceil(quotient)) : 0.0; +} + +// safe_floored_modulo +float cultmath_cycles_floored_modulo(float a, float b) { return b != 0.0 ? a - floor(a / b) * b : 0.0; } + +// wrapf (note the argument order: value, max, min) +float cultmath_cycles_wrap(float value, float max_value, float min_value) +{ + float range = max_value - min_value; + return range != 0.0 ? value - (range * floor((value - min_value) / range)) : min_value; +} + +// floorf(safe_divide(a, b)) * b +float cultmath_cycles_snap(float a, float b) { return floor(cultmath_cycles_divide(a, b)) * b; } + +// pingpongf +float cultmath_cycles_pingpong(float a, float b) +{ + float scaled = (a - b) / (b * 2.0); + return b != 0.0 ? abs((scaled - floor(scaled)) * b * 2.0 - b) : 0.0; +} + +// smoothminf +float cultmath_cycles_smooth_min(float a, float b, float k) +{ + float h = max(k - abs(a - b), 0.0) / k; + return k != 0.0 ? min(a, b) - h * h * h * k * 0.16666667 : min(a, b); +} + +// -smoothminf(-a, -b, k) +float cultmath_cycles_smooth_max(float a, float b, float k) { return -cultmath_cycles_smooth_min(-a, -b, k); } + +// NODE_MATH_COMPARE +float cultmath_cycles_compare(float a, float b, float epsilon) +{ + return (a == b || abs(a - b) <= max(epsilon, CULTMATH_CYCLES_FLT_EPSILON)) ? 1.0 : 0.0; +} + +// NODE_MATH_ROUND +float cultmath_cycles_round(float a) { return floor(a + 0.5); } + +// NODE_MATH_TRUNC +float cultmath_cycles_trunc(float a) { return a >= 0.0 ? floor(a) : ceil(a); } + +// NODE_MATH_FRACTION +float cultmath_cycles_fract(float a) { return a - floor(a); } + +// compatible_signf +float cultmath_cycles_sign(float a) { return a == 0.0 ? 0.0 : (a < 0.0 ? -1.0 : 1.0); } + +// ---- Map Range node (kernel/svm/map_range.h svm_node_map_range) ---- +// A Map Range node's Clamp option is a Clamp node of type Range on its result (MapRangeNode::expand): +// compose cultmath_cycles_clamp_range over these. + +float cultmath_cycles_map_range_linear(float value, float from_min, float from_max, float to_min, float to_max) +{ + float factor = (value - from_min) / (from_max - from_min); + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0; +} + +float cultmath_cycles_map_range_stepped(float value, float from_min, float from_max, float to_min, float to_max, float steps) +{ + float factor = (value - from_min) / (from_max - from_min); + factor = steps > 0.0 ? floor(factor * (steps + 1.0)) / steps : 0.0; + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0; +} + +// The value itself, not a normalized one, goes through smoothstep(from_min, from_max, .), flipped when +// from_min > from_max (util/math_base.h smoothstep). +float cultmath_cycles_map_range_smoothstep(float value, float from_min, float from_max, float to_min, float to_max) +{ + float low = min(from_min, from_max); + float high = max(from_min, from_max); + float t = (value - low) / (high - low); + float eased = value < low ? 0.0 : (value >= high ? 1.0 : (3.0 - 2.0 * t) * (t * t)); + float factor = from_min > from_max ? 1.0 - eased : eased; + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0; +} + +// smootherstep of map_range.h, flipped when from_min > from_max. +float cultmath_cycles_map_range_smootherstep(float value, float from_min, float from_max, float to_min, float to_max) +{ + float low = min(from_min, from_max); + float high = max(from_min, from_max); + float x = clamp(cultmath_cycles_divide(value - low, high - low), 0.0, 1.0); + float eased = x * x * x * (x * (x * 6.0 - 15.0) + 10.0); + float factor = from_min > from_max ? 1.0 - eased : eased; + return from_max != from_min ? to_min + factor * (to_max - to_min) : 0.0; +} + +// ---- Clamp node (kernel/svm/clamp.h svm_node_clamp) ---- + +// Type Min Max +float cultmath_cycles_clamp_minmax(float value, float min_value, float max_value) { return clamp(value, min_value, max_value); } + +// Type Range: the bounds swap when min_value > max_value. +float cultmath_cycles_clamp_range(float value, float min_value, float max_value) +{ + return min_value > max_value ? clamp(value, max_value, min_value) : clamp(value, min_value, max_value); +} + +// ---- Mix node (kernel/svm/mix.h, color_util.h) ---- +// Factor clamping (svm_node_mix_color's saturatef) and result clamping (saturate) are the caller's. + +// svm_node_mix_float +float cultmath_cycles_mix_float(float t, float a, float b) { return a * (1.0 - t) + b * t; } + +// svm_node_mix_vector +float3 cultmath_cycles_mix_vector(float t, float3 a, float3 b) { return a * (float3(1.0, 1.0, 1.0) - t) + b * t; } + +float3 cultmath_cycles_mix_blend(float t, float3 col1, float3 col2) { return col1 + t * (col2 - col1); } + +float3 cultmath_cycles_mix_add(float t, float3 col1, float3 col2) { return col1 + t * ((col1 + col2) - col1); } + +float3 cultmath_cycles_mix_mul(float t, float3 col1, float3 col2) { return col1 + t * (col1 * col2 - col1); } + +float3 cultmath_cycles_mix_screen(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + float3 one = float3(1.0, 1.0, 1.0); + return one - (float3(tm, tm, tm) + t * (one - col2)) * (one - col1); +} + +float3 cultmath_cycles_mix_overlay(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + return float3( + col1.x < 0.5 ? col1.x * (tm + 2.0 * t * col2.x) : 1.0 - (tm + 2.0 * t * (1.0 - col2.x)) * (1.0 - col1.x), + col1.y < 0.5 ? col1.y * (tm + 2.0 * t * col2.y) : 1.0 - (tm + 2.0 * t * (1.0 - col2.y)) * (1.0 - col1.y), + col1.z < 0.5 ? col1.z * (tm + 2.0 * t * col2.z) : 1.0 - (tm + 2.0 * t * (1.0 - col2.z)) * (1.0 - col1.z)); +} + +float3 cultmath_cycles_mix_sub(float t, float3 col1, float3 col2) { return col1 + t * ((col1 - col2) - col1); } + +float3 cultmath_cycles_mix_div(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + return float3( + col2.x != 0.0 ? tm * col1.x + t * col1.x / col2.x : col1.x, + col2.y != 0.0 ? tm * col1.y + t * col1.y / col2.y : col1.y, + col2.z != 0.0 ? tm * col1.z + t * col1.z / col2.z : col1.z); +} + +float3 cultmath_cycles_mix_diff(float t, float3 col1, float3 col2) { return col1 + t * (abs(col1 - col2) - col1); } + +float3 cultmath_cycles_mix_exclusion(float t, float3 col1, float3 col2) +{ + return max(col1 + t * ((col1 + col2 - 2.0 * col1 * col2) - col1), float3(0.0, 0.0, 0.0)); +} + +float3 cultmath_cycles_mix_dark(float t, float3 col1, float3 col2) { return col1 + t * (min(col1, col2) - col1); } + +float3 cultmath_cycles_mix_light(float t, float3 col1, float3 col2) { return col1 + t * (max(col1, col2) - col1); } + +float3 cultmath_cycles_mix_dodge(float t, float3 col1, float3 col2) +{ + float3 divisor = float3(1.0, 1.0, 1.0) - t * col2; + return float3( + col1.x != 0.0 ? (divisor.x <= 0.0 ? 1.0 : min(col1.x / divisor.x, 1.0)) : col1.x, + col1.y != 0.0 ? (divisor.y <= 0.0 ? 1.0 : min(col1.y / divisor.y, 1.0)) : col1.y, + col1.z != 0.0 ? (divisor.z <= 0.0 ? 1.0 : min(col1.z / divisor.z, 1.0)) : col1.z); +} + +float3 cultmath_cycles_mix_burn(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + float3 divisor = tm + t * col2; + float3 burnt = float3(1.0, 1.0, 1.0) - (float3(1.0, 1.0, 1.0) - col1) / divisor; + return float3( + divisor.x <= 0.0 ? 0.0 : clamp(burnt.x, 0.0, 1.0), + divisor.y <= 0.0 ? 0.0 : clamp(burnt.y, 0.0, 1.0), + divisor.z <= 0.0 ? 0.0 : clamp(burnt.z, 0.0, 1.0)); +} + +// rgb_to_hsv of util/color.h +float3 cultmath_cycles_rgb_to_hsv(float3 rgb) +{ + float cmax = max(rgb.x, max(rgb.y, rgb.z)); + float cmin = min(rgb.x, min(rgb.y, rgb.z)); + float cdelta = cmax - cmin; + float s = cmax != 0.0 ? cdelta / cmax : 0.0; + float3 c = (float3(cmax, cmax, cmax) - rgb) / cdelta; + float h = rgb.x == cmax ? c.z - c.y : (rgb.y == cmax ? 2.0 + c.x - c.z : 4.0 + c.y - c.x); + h = h / 6.0; + h = h < 0.0 ? h + 1.0 : h; + return float3(s != 0.0 ? h : 0.0, s, cmax); +} + +// hsv_to_rgb of util/color.h +float3 cultmath_cycles_hsv_to_rgb(float3 hsv) +{ + float h = hsv.x == 1.0 ? 0.0 : hsv.x; + h = h * 6.0; + float i = floor(h); + float f = h - i; + float p = hsv.z * (1.0 - hsv.y); + float q = hsv.z * (1.0 - (hsv.y * f)); + float t = hsv.z * (1.0 - (hsv.y * (1.0 - f))); + float3 rgb = i == 0.0 ? float3(hsv.z, t, p) : + (i == 1.0 ? float3(q, hsv.z, p) : + (i == 2.0 ? float3(p, hsv.z, t) : + (i == 3.0 ? float3(p, q, hsv.z) : + (i == 4.0 ? float3(t, p, hsv.z) : float3(hsv.z, p, q))))); + return hsv.y != 0.0 ? rgb : float3(hsv.z, hsv.z, hsv.z); +} + +float3 cultmath_cycles_mix_hue(float t, float3 col1, float3 col2) +{ + float3 hsv2 = cultmath_cycles_rgb_to_hsv(col2); + float3 hsv = cultmath_cycles_rgb_to_hsv(col1); + float3 shifted = cultmath_cycles_hsv_to_rgb(float3(hsv2.x, hsv.y, hsv.z)); + return hsv2.y != 0.0 ? col1 + t * (shifted - col1) : col1; +} + +float3 cultmath_cycles_mix_sat(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + float3 hsv = cultmath_cycles_rgb_to_hsv(col1); + float3 hsv2 = cultmath_cycles_rgb_to_hsv(col2); + return hsv.y != 0.0 ? cultmath_cycles_hsv_to_rgb(float3(hsv.x, tm * hsv.y + t * hsv2.y, hsv.z)) : col1; +} + +float3 cultmath_cycles_mix_val(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + float3 hsv = cultmath_cycles_rgb_to_hsv(col1); + float3 hsv2 = cultmath_cycles_rgb_to_hsv(col2); + return cultmath_cycles_hsv_to_rgb(float3(hsv.x, hsv.y, tm * hsv.z + t * hsv2.z)); +} + +float3 cultmath_cycles_mix_color(float t, float3 col1, float3 col2) +{ + float3 hsv2 = cultmath_cycles_rgb_to_hsv(col2); + float3 hsv = cultmath_cycles_rgb_to_hsv(col1); + float3 shifted = cultmath_cycles_hsv_to_rgb(float3(hsv2.x, hsv2.y, hsv.z)); + return hsv2.y != 0.0 ? col1 + t * (shifted - col1) : col1; +} + +float3 cultmath_cycles_mix_soft(float t, float3 col1, float3 col2) +{ + float tm = 1.0 - t; + float3 one = float3(1.0, 1.0, 1.0); + float3 scr = one - (one - col2) * (one - col1); + return tm * col1 + t * ((one - col1) * col2 * col1 + col1 * scr); +} + +float3 cultmath_cycles_mix_linear(float t, float3 col1, float3 col2) +{ + return col1 + t * (2.0 * col2 + float3(-1.0, -1.0, -1.0)); +} + +// ---- RGB to BW node (kernel/svm/convert.h NODE_CONVERT_CF) ---- + +float cultmath_cycles_rgb_to_bw(float3 color) +{ + return color.x * CULTMATH_CYCLES_RGB_TO_Y_R + color.y * CULTMATH_CYCLES_RGB_TO_Y_G + color.z * CULTMATH_CYCLES_RGB_TO_Y_B; +} + +// ---- Color Ramp lookup (kernel/svm/ramp.h rgb_ramp_lookup, without extrapolation) ---- + +// 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. +float2 cultmath_cycles_ramp_index(float t, int size) +{ + float f = (t > 0.0 ? (t < 1.0 ? t : 1.0) : 0.0) * (size - 1); + int index = clamp((int)f, 0, size - 1); + return float2(index, f - index); +} + +#endif diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl.meta b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl.meta new file mode 100644 index 00000000..4085b3eb --- /dev/null +++ b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Cycles.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 477bb38d537244b78e3eaec093530bcc +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/packages/cultmath/unity/org.gamecult.cultmath/THIRD-PARTY-NOTICES.md b/packages/cultmath/unity/org.gamecult.cultmath/THIRD-PARTY-NOTICES.md index f7e22fd0..8361acfc 100644 --- a/packages/cultmath/unity/org.gamecult.cultmath/THIRD-PARTY-NOTICES.md +++ b/packages/cultmath/unity/org.gamecult.cultmath/THIRD-PARTY-NOTICES.md @@ -22,6 +22,35 @@ permutation and scale as `snoise(float3)`. - Upstream: - License: MIT. +## Cycles node semantics + +`math.cycles_*` and `cultmath_cycles_*` port what Blender's Cycles renderer +computes for the Math, Map Range, Clamp, Mix and RGB to BW nodes and for a Color +Ramp lookup. Each function's comment names the Cycles file it follows, all from +`intern/cycles` at the Blender `v5.2.2` tag: `kernel/svm/math_util.h`, +`kernel/svm/map_range.h`, `kernel/svm/clamp.h`, `kernel/svm/mix.h`, +`kernel/svm/color_util.h`, `kernel/svm/ramp.h`, `kernel/svm/convert.h`, +`util/math_base.h`, `util/math_float3.h` and `util/color.h`. The code was +changed: Cycles C++ became C# and HLSL, one function per node operation, with +the nodes' socket defaults and clamp options left to the caller, and the modulo +is the quotient form every CultMath target shares rather than `fmodf`. Nothing +was read from Blender's GPL sources (`source/blender/gpu`, `source/blender/blenlib`). + +- Copyright 2011-2022 Blender Foundation. +- Upstream: (`intern/cycles`). +- License: Apache-2.0 (), which is + per-file here: it covers `src/CultMath/math.Cycles.cs`, + `shaders/CultMath.Cycles.hlsl` and the Unity copy of that include, and + `shaders/CultMath.Cycles.glsl`, which is generated from that include. Each + carries the Apache-2.0 header and this attribution. The rest of CultMath, the + generated GLSL library included, is MIT or MPL-2.0 as above, so the + `GameCult.Math` NuGet package and the `org.gamecult.cultmath` Unity package + both declare `MIT AND MPL-2.0 AND Apache-2.0` and ship this file. +- The test fixtures under `tests/CultMath.Tests/fixtures/cycles-oracle` are + results of running Blender 5.2.2 through `tools/cycles-oracle/oracle.py` + (GPL-3.0-or-later, a Blender Python script, shipped neither in the NuGet + package nor in the Unity package). + ## Phacelle Noise `math.phacelle` and `cultmath_phacelle` follow Rune Skovbo Johansen's Phacelle diff --git a/packages/cultmath/unity/org.gamecult.cultmath/package.json b/packages/cultmath/unity/org.gamecult.cultmath/package.json index 6e0a0b75..04f90e59 100644 --- a/packages/cultmath/unity/org.gamecult.cultmath/package.json +++ b/packages/cultmath/unity/org.gamecult.cultmath/package.json @@ -4,7 +4,7 @@ "version": "0.4.1", "unity": "2021.3", "description": "Precompiled CultMath numeric primitives and matching HLSL semantics for GameCult Unity runtimes.", - "license": "MIT AND MPL-2.0", + "license": "MIT AND MPL-2.0 AND Apache-2.0", "author": { "name": "GameCult" },