From 59a7fe0ba67164c26147ee78b402225c16b0334a Mon Sep 17 00:00:00 2001 From: Kazuaki Matsumura Date: Wed, 28 Oct 2026 05:45:43 -0700 Subject: [PATCH 1/2] Fix NaN in ACES 2.0 gamut compression at J = limit_J_max When J is at the limit (exactly 100.0 at 100 nits, which passes the J > limit_J_max guard), solve_J_intersect can return J_intersect a float step above limit_J_max. The upper-hull base J_max - J_intersect_source in find_gamut_boundary_intersection is then slightly negative, powf returns NaN, and the gamut_boundary_M <= 0 guard does not catch it. Through the studio config the range clamp turns the NaN into 0, so some very bright pixels render black. Clamp the base at 0 on the CPU and in the generated shader text. The boundary M is then 0 and the existing guard returns {J, 0, h}, as the J > limit_J_max branch does. Tests: forward and inverse gamut compression for J at the limit and the float steps below it over all hues, and an output-transform input that reached the limit. Signed-off-by: Kazuaki Matsumura --- .../ops/fixedfunction/ACES2/Transform.cpp | 4 +- .../ops/fixedfunction/FixedFunctionOpGPU.cpp | 2 +- .../FixedFunctionOpCPU_tests.cpp | 87 +++++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/src/OpenColorIO/ops/fixedfunction/ACES2/Transform.cpp b/src/OpenColorIO/ops/fixedfunction/ACES2/Transform.cpp index 889c2a90be..b776280635 100644 --- a/src/OpenColorIO/ops/fixedfunction/ACES2/Transform.cpp +++ b/src/OpenColorIO/ops/fixedfunction/ACES2/Transform.cpp @@ -993,7 +993,9 @@ float find_gamut_boundary_intersection(const f2 &JM_cusp, float J_max, float gam // The upper hull is flipped and thus 'zeroed' at J_max // Also note we negate the slope const float f_J_intersect_cusp = J_max - J_intersect_cusp; - const float f_J_intersect_source = J_max - J_intersect_source; + // J_intersect_source can land a float step above J_max when J is at the limit: + // clamp so that powf never gets a negative base. + const float f_J_intersect_source = std::max(0.f, J_max - J_intersect_source); const float f_JM_cusp_J = J_max - JM_cusp[0]; const float M_boundary_upper = estimate_line_and_boundary_intersection_M(f_J_intersect_source, -slope, gamma_top_inv, f_JM_cusp_J, JM_cusp[1], f_J_intersect_cusp); diff --git a/src/OpenColorIO/ops/fixedfunction/FixedFunctionOpGPU.cpp b/src/OpenColorIO/ops/fixedfunction/FixedFunctionOpGPU.cpp index 01a3ab4983..081ad9ad42 100644 --- a/src/OpenColorIO/ops/fixedfunction/FixedFunctionOpGPU.cpp +++ b/src/OpenColorIO/ops/fixedfunction/FixedFunctionOpGPU.cpp @@ -1048,7 +1048,7 @@ std::string _Add_Find_Gamut_Boundary_Intersection_func( ss.indent(); ss.newLine() << ss.floatDecl("M_boundary_lower") << " = J_intersect_cusp * pow(J_intersect_source / J_intersect_cusp, gamma_bottom_inv) / (JM_cusp.r / JM_cusp.g - slope);"; - ss.newLine() << ss.floatDecl("M_boundary_upper") << " = JM_cusp.g * (" << s.limit_J_max << " - J_intersect_cusp) * pow((" << s.limit_J_max << " - J_intersect_source) / (" << s.limit_J_max << " - J_intersect_cusp), gamma_top_inv) / (slope * JM_cusp.g + " << s.limit_J_max << " - JM_cusp.r);"; + ss.newLine() << ss.floatDecl("M_boundary_upper") << " = JM_cusp.g * (" << s.limit_J_max << " - J_intersect_cusp) * pow(max(0.0, " << s.limit_J_max << " - J_intersect_source) / (" << s.limit_J_max << " - J_intersect_cusp), gamma_top_inv) / (slope * JM_cusp.g + " << s.limit_J_max << " - JM_cusp.r);"; ss.newLine() << ss.floatDecl("smin") << " = 0.0;"; ss.newLine() << "{"; diff --git a/tests/cpu/ops/fixedfunction/FixedFunctionOpCPU_tests.cpp b/tests/cpu/ops/fixedfunction/FixedFunctionOpCPU_tests.cpp index 03c9dfa739..c4b789add5 100644 --- a/tests/cpu/ops/fixedfunction/FixedFunctionOpCPU_tests.cpp +++ b/tests/cpu/ops/fixedfunction/FixedFunctionOpCPU_tests.cpp @@ -1453,3 +1453,90 @@ OCIO_ADD_TEST(FixedFunctionOpCPU, LIN_TO_DOUBLE_LOG) ApplyFixedFunction(img.data(), linearFrame.data(), NumPixels, dataFInv, 1e-6f, __LINE__, false); } } + + +OCIO_ADD_TEST(FixedFunctionOpCPU, aces_gamut_comp_20_limit_J) +{ + // Inputs at or just below the J limit used to produce NaN because the + // J intersection may land slightly above limit_J_max. + OCIO::FixedFunctionOpData::Params params = { + // Peak luminance + 100.f, + // Rec709 gamut + 0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.3290 + }; + + std::vector input_32f = { + 100.0f, 57.28898620605469f, 45.65402603149414f, 1.0f, + 100.0f, 10.0f, 46.0f, 1.0f, + }; + const size_t num_limit_samples = input_32f.size() / 4; + + // J at the limit (100 at 100 nits) and the last float steps below it, over all hues + // in 0.5 degree steps. About 20 % of these hues gave NaN before. + const float M_values[] = { 1.0f, 10.0f, 30.0f, 57.28898620605469f }; + for (float M : M_values) + { + for (int ih = 0; ih < 720; ++ih) + { + float J = 100.0f; + for (int i = 0; i < 4; ++i) + { + input_32f.insert(input_32f.end(), { J, M, 0.5f * ih, 1.0f }); + J = std::nextafter(J, 0.0f); + } + } + } + const unsigned num_samples = static_cast(input_32f.size() / 4); + + for (auto style : { OCIO::FixedFunctionOpData::ACES_GAMUT_COMPRESS_20_FWD, + OCIO::FixedFunctionOpData::ACES_GAMUT_COMPRESS_20_INV }) + { + OCIO::ConstFixedFunctionOpDataRcPtr funcData + = std::make_shared(style, params); + + OCIO::ConstOpCPURcPtr op; + OCIO_CHECK_NO_THROW(op = OCIO::GetFixedFunctionCPURenderer(funcData, false)); + + std::vector output_32f(input_32f); + OCIO_CHECK_NO_THROW(op->apply(output_32f.data(), output_32f.data(), num_samples)); + + for (unsigned idx = 0; idx < num_samples * 4; ++idx) + { + OCIO_CHECK_ASSERT_MESSAGE(std::isfinite(output_32f[idx]), + "Index: " + std::to_string(idx)); + } + for (size_t i = 0; i < num_limit_samples; ++i) + { + OCIO_CHECK_CLOSE(output_32f[i * 4 + 0], 100.0f, 1e-5f); + OCIO_CHECK_CLOSE(output_32f[i * 4 + 1], 0.0f, 1e-5f); + } + } +} + +OCIO_ADD_TEST(FixedFunctionOpCPU, aces_output_transform_20_limit_J) +{ + // This input reaches the J limit inside the gamut compression and used to + // produce NaN. + OCIO::FixedFunctionOpData::Params params = { + // Peak luminance + 100.f, + // Rec709 gamut + 0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.3290 + }; + OCIO::ConstFixedFunctionOpDataRcPtr funcData + = std::make_shared(OCIO::FixedFunctionOpData::ACES_OUTPUT_TRANSFORM_20_FWD, + params); + OCIO::ConstOpCPURcPtr op; + OCIO_CHECK_NO_THROW(op = OCIO::GetFixedFunctionCPURenderer(funcData, false)); + + float rgba[4] = { 220.57275390625f, 74.11682891845703f, 3.2731568813323975f, 1.0f }; + OCIO_CHECK_NO_THROW(op->apply(rgba, rgba, 1)); + + for (int c = 0; c < 3; ++c) + { + OCIO_CHECK_ASSERT(std::isfinite(rgba[c])); + OCIO_CHECK_CLOSE(rgba[c], 1.0f, 1e-3f); + } + OCIO_CHECK_EQUAL(rgba[3], 1.0f); +} From 0af52ae36ee5d679a69bf3a8629fd40139f86b88 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsumura Date: Tue, 10 Nov 2026 16:38:18 +0900 Subject: [PATCH 2/2] Reduce the J limit test Use 8 hues and one failure message per direction. Drop the output transform test: it depends on FMA. Signed-off-by: Kazuaki Matsumura --- .../FixedFunctionOpCPU_tests.cpp | 75 +++++-------------- 1 file changed, 19 insertions(+), 56 deletions(-) diff --git a/tests/cpu/ops/fixedfunction/FixedFunctionOpCPU_tests.cpp b/tests/cpu/ops/fixedfunction/FixedFunctionOpCPU_tests.cpp index c4b789add5..1d4d9fb72d 100644 --- a/tests/cpu/ops/fixedfunction/FixedFunctionOpCPU_tests.cpp +++ b/tests/cpu/ops/fixedfunction/FixedFunctionOpCPU_tests.cpp @@ -1457,8 +1457,9 @@ OCIO_ADD_TEST(FixedFunctionOpCPU, LIN_TO_DOUBLE_LOG) OCIO_ADD_TEST(FixedFunctionOpCPU, aces_gamut_comp_20_limit_J) { - // Inputs at or just below the J limit used to produce NaN because the - // J intersection may land slightly above limit_J_max. + // At J = limit_J_max the J intersection may land a float step above the limit and the + // upper hull term used to be NaN. These hues gave NaN without the fix, both with and + // without fused multiply-add. OCIO::FixedFunctionOpData::Params params = { // Peak luminance 100.f, @@ -1466,26 +1467,11 @@ OCIO_ADD_TEST(FixedFunctionOpCPU, aces_gamut_comp_20_limit_J) 0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.3290 }; - std::vector input_32f = { - 100.0f, 57.28898620605469f, 45.65402603149414f, 1.0f, - 100.0f, 10.0f, 46.0f, 1.0f, - }; - const size_t num_limit_samples = input_32f.size() / 4; - - // J at the limit (100 at 100 nits) and the last float steps below it, over all hues - // in 0.5 degree steps. About 20 % of these hues gave NaN before. - const float M_values[] = { 1.0f, 10.0f, 30.0f, 57.28898620605469f }; - for (float M : M_values) + const float hues[] = { 4.f, 46.f, 70.f, 101.f, 160.f, 210.f, 266.f, 325.f }; + std::vector input_32f; + for (float h : hues) { - for (int ih = 0; ih < 720; ++ih) - { - float J = 100.0f; - for (int i = 0; i < 4; ++i) - { - input_32f.insert(input_32f.end(), { J, M, 0.5f * ih, 1.0f }); - J = std::nextafter(J, 0.0f); - } - } + input_32f.insert(input_32f.end(), { 100.0f, 10.0f, h, 1.0f }); } const unsigned num_samples = static_cast(input_32f.size() / 4); @@ -1501,42 +1487,19 @@ OCIO_ADD_TEST(FixedFunctionOpCPU, aces_gamut_comp_20_limit_J) std::vector output_32f(input_32f); OCIO_CHECK_NO_THROW(op->apply(output_32f.data(), output_32f.data(), num_samples)); - for (unsigned idx = 0; idx < num_samples * 4; ++idx) - { - OCIO_CHECK_ASSERT_MESSAGE(std::isfinite(output_32f[idx]), - "Index: " + std::to_string(idx)); - } - for (size_t i = 0; i < num_limit_samples; ++i) + // At the limit the colourfulness is mapped to 0, as for J above the limit. + unsigned num_failed = 0; + for (unsigned i = 0; i < num_samples; ++i) { - OCIO_CHECK_CLOSE(output_32f[i * 4 + 0], 100.0f, 1e-5f); - OCIO_CHECK_CLOSE(output_32f[i * 4 + 1], 0.0f, 1e-5f); + const float J = output_32f[i * 4 + 0]; + const float M = output_32f[i * 4 + 1]; + if (!std::isfinite(J) || !std::isfinite(M) || std::abs(J - 100.0f) > 1e-5f || std::abs(M) > 1e-5f) + { + ++num_failed; + } } + OCIO_CHECK_ASSERT_MESSAGE(num_failed == 0, + std::to_string(num_failed) + " of " + std::to_string(num_samples) + + " samples at J = limit_J_max are not (J, 0, h)"); } } - -OCIO_ADD_TEST(FixedFunctionOpCPU, aces_output_transform_20_limit_J) -{ - // This input reaches the J limit inside the gamut compression and used to - // produce NaN. - OCIO::FixedFunctionOpData::Params params = { - // Peak luminance - 100.f, - // Rec709 gamut - 0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.3290 - }; - OCIO::ConstFixedFunctionOpDataRcPtr funcData - = std::make_shared(OCIO::FixedFunctionOpData::ACES_OUTPUT_TRANSFORM_20_FWD, - params); - OCIO::ConstOpCPURcPtr op; - OCIO_CHECK_NO_THROW(op = OCIO::GetFixedFunctionCPURenderer(funcData, false)); - - float rgba[4] = { 220.57275390625f, 74.11682891845703f, 3.2731568813323975f, 1.0f }; - OCIO_CHECK_NO_THROW(op->apply(rgba, rgba, 1)); - - for (int c = 0; c < 3; ++c) - { - OCIO_CHECK_ASSERT(std::isfinite(rgba[c])); - OCIO_CHECK_CLOSE(rgba[c], 1.0f, 1e-3f); - } - OCIO_CHECK_EQUAL(rgba[3], 1.0f); -}