From 9d39e7574d0f08c649fb82a69e313eece57970bb Mon Sep 17 00:00:00 2001 From: kevyuu Date: Wed, 12 Aug 2026 16:49:32 +0700 Subject: [PATCH 1/5] Add fast_acos_csc implementation --- include/nbl/builtin/hlsl/math/fast_acos.hlsl | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/include/nbl/builtin/hlsl/math/fast_acos.hlsl b/include/nbl/builtin/hlsl/math/fast_acos.hlsl index fea2fd6ca7..f48f923f0c 100644 --- a/include/nbl/builtin/hlsl/math/fast_acos.hlsl +++ b/include/nbl/builtin/hlsl/math/fast_acos.hlsl @@ -80,6 +80,53 @@ struct fast_acos_stegun_poly5 } }; +template +T fast_acos_csc(const T arg) +{ + const T u = hlsl::log2(_static_cast(1)+arg); + T poly; + // See https://www.desmos.com/calculator/sdptomhbju + if (order==1) + poly = (_static_cast(1)-u)*_static_cast(0.627); + else if (order==2) + { + const T a = -0.6509; + const T b = -0.6369; + const T c = -0.0134; + poly = hlsl::fma(u, hlsl::fma(u, a, b), c); + + } + else if (order==3) + { + const T a = 0.6494; + const T b = -0.6310; + const T c = -0.0123; + const T d = -0.0004; + poly = hlsl::fma(u, hlsl::fma(u, hlsl::fma(u, d, c), b), a); + } + return hlsl::exp2(poly); +} + +template +T fast_acos_csc(const T arg, bool overestimate) +{ + T u = log2(1.0 + arg); + + T a_under = 0.6505590683f; + T b_under = -0.6369475329f; + T c_under = -0.0136113553f; + + T a_over = 0.6546594925f; + T b_over = -0.6269055372f; + T c_over = -0.0067733255f; + + T a = hlsl::spirv::select(overestimate, a_over, a_under); + T b = hlsl::spirv::select(overestimate, b_over, b_under); + T c = hlsl::spirv::select(overestimate, c_over, c_under); + T poly = hlsl::fma(u, hlsl::fma(u, c, b), a); + return hlsl::exp2(poly); +} + } } } From a9e3f5ea6de23d0f02b1a431c217f01a59bca70b Mon Sep 17 00:00:00 2001 From: kevyuu Date: Wed, 12 Aug 2026 18:24:25 +0700 Subject: [PATCH 2/5] Remove spirv namespace from select --- include/nbl/builtin/hlsl/math/fast_acos.hlsl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/nbl/builtin/hlsl/math/fast_acos.hlsl b/include/nbl/builtin/hlsl/math/fast_acos.hlsl index f48f923f0c..426fe88e33 100644 --- a/include/nbl/builtin/hlsl/math/fast_acos.hlsl +++ b/include/nbl/builtin/hlsl/math/fast_acos.hlsl @@ -120,9 +120,9 @@ T fast_acos_csc(const T arg, bool overestimate) T b_over = -0.6269055372f; T c_over = -0.0067733255f; - T a = hlsl::spirv::select(overestimate, a_over, a_under); - T b = hlsl::spirv::select(overestimate, b_over, b_under); - T c = hlsl::spirv::select(overestimate, c_over, c_under); + T a = hlsl::select(overestimate, a_over, a_under); + T b = hlsl::select(overestimate, b_over, b_under); + T c = hlsl::select(overestimate, c_over, c_under); T poly = hlsl::fma(u, hlsl::fma(u, c, b), a); return hlsl::exp2(poly); } From e4e29c5fb220999d040a50fd1dad34f8588470cd Mon Sep 17 00:00:00 2001 From: kevyuu Date: Thu, 13 Aug 2026 01:08:26 +0700 Subject: [PATCH 3/5] Refactor to fast_acos_csc to functor --- include/nbl/builtin/hlsl/math/fast_acos.hlsl | 26 ++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/include/nbl/builtin/hlsl/math/fast_acos.hlsl b/include/nbl/builtin/hlsl/math/fast_acos.hlsl index 426fe88e33..d7896a9f25 100644 --- a/include/nbl/builtin/hlsl/math/fast_acos.hlsl +++ b/include/nbl/builtin/hlsl/math/fast_acos.hlsl @@ -80,20 +80,22 @@ struct fast_acos_stegun_poly5 } }; -template -T fast_acos_csc(const T arg) +template +struct fast_acos_csc { - const T u = hlsl::log2(_static_cast(1)+arg); + static T __call(const T val) + { + const T u = hlsl::log2(_static_cast(1)+val); T poly; // See https://www.desmos.com/calculator/sdptomhbju if (order==1) poly = (_static_cast(1)-u)*_static_cast(0.627); else if (order==2) { - const T a = -0.6509; + const T a = 0.6509; const T b = -0.6369; const T c = -0.0134; - poly = hlsl::fma(u, hlsl::fma(u, a, b), c); + poly = hlsl::fma(u, hlsl::fma(u, c, b), a); } else if (order==3) @@ -105,12 +107,15 @@ T fast_acos_csc(const T arg) poly = hlsl::fma(u, hlsl::fma(u, hlsl::fma(u, d, c), b), a); } return hlsl::exp2(poly); -} + } +}; -template -T fast_acos_csc(const T arg, bool overestimate) +template +struct fast_acos_csc_directed { - T u = log2(1.0 + arg); + static T __call(const T val, bool overestimate) + { + T u = log2(1.0 + val); T a_under = 0.6505590683f; T b_under = -0.6369475329f; @@ -125,7 +130,8 @@ T fast_acos_csc(const T arg, bool overestimate) T c = hlsl::select(overestimate, c_over, c_under); T poly = hlsl::fma(u, hlsl::fma(u, c, b), a); return hlsl::exp2(poly); -} + } +}; } } From abd8b3be8cf4334ffebfd3d8c1f353d370bd7874 Mon Sep 17 00:00:00 2001 From: kevyuu Date: Thu, 13 Aug 2026 01:56:40 +0700 Subject: [PATCH 4/5] Fix NBL_STRUCT_CONSTRAINABLE placement --- include/nbl/builtin/hlsl/math/fast_acos.hlsl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/nbl/builtin/hlsl/math/fast_acos.hlsl b/include/nbl/builtin/hlsl/math/fast_acos.hlsl index d7896a9f25..69c00e26f9 100644 --- a/include/nbl/builtin/hlsl/math/fast_acos.hlsl +++ b/include/nbl/builtin/hlsl/math/fast_acos.hlsl @@ -110,7 +110,7 @@ struct fast_acos_csc } }; -template +template struct fast_acos_csc_directed { static T __call(const T val, bool overestimate) @@ -133,6 +133,7 @@ struct fast_acos_csc_directed } }; + } } } From b0a242414939b3b5e6438e68d8dd1f1048db9539 Mon Sep 17 00:00:00 2001 From: kevyuu Date: Thu, 13 Aug 2026 08:58:35 +0700 Subject: [PATCH 5/5] Fix NBL_STRUCT_CONSTRAINABLE placement in fast_acos_csc --- include/nbl/builtin/hlsl/math/fast_acos.hlsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nbl/builtin/hlsl/math/fast_acos.hlsl b/include/nbl/builtin/hlsl/math/fast_acos.hlsl index 69c00e26f9..e19c983b03 100644 --- a/include/nbl/builtin/hlsl/math/fast_acos.hlsl +++ b/include/nbl/builtin/hlsl/math/fast_acos.hlsl @@ -80,7 +80,7 @@ struct fast_acos_stegun_poly5 } }; -template +template struct fast_acos_csc { static T __call(const T val)