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..1d4d9fb72d 100644 --- a/tests/cpu/ops/fixedfunction/FixedFunctionOpCPU_tests.cpp +++ b/tests/cpu/ops/fixedfunction/FixedFunctionOpCPU_tests.cpp @@ -1453,3 +1453,53 @@ 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) +{ + // 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, + // Rec709 gamut + 0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.3290 + }; + + 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) + { + input_32f.insert(input_32f.end(), { 100.0f, 10.0f, h, 1.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)); + + // 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) + { + 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)"); + } +}