|
| 1 | +// Copyright (C) 2022 - DevSH Graphics Programming Sp. z O.O. |
| 2 | +// This file is part of the "Nabla Engine". |
| 3 | +// For conditions of distribution and use, see copyright notice in nabla.h |
| 4 | +#ifndef _NBL_BUILTIN_HLSL_TGMATH_INCLUDED_ |
| 5 | +#define _NBL_BUILTIN_HLSL_TGMATH_INCLUDED_ |
| 6 | + |
| 7 | +#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl> |
| 8 | +#include <nbl/builtin/hlsl/cpp_compat/impl/intrinsics_impl.hlsl> |
| 9 | +// C++ headers |
| 10 | +#ifndef __HLSL_VERSION |
| 11 | +#include <algorithm> |
| 12 | +#include <cmath> |
| 13 | +#endif |
| 14 | + |
| 15 | +namespace nbl |
| 16 | +{ |
| 17 | +namespace hlsl |
| 18 | +{ |
| 19 | + |
| 20 | +template<typename FloatingPoint> |
| 21 | +inline FloatingPoint erf(FloatingPoint _x) |
| 22 | +{ |
| 23 | +#ifdef __HLSL_VERSION |
| 24 | + const FloatingPoint a1 = 0.254829592; |
| 25 | + const FloatingPoint a2 = -0.284496736; |
| 26 | + const FloatingPoint a3 = 1.421413741; |
| 27 | + const FloatingPoint a4 = -1.453152027; |
| 28 | + const FloatingPoint a5 = 1.061405429; |
| 29 | + const FloatingPoint p = 0.3275911; |
| 30 | + |
| 31 | + FloatingPoint sign = sign(_x); |
| 32 | + FloatingPoint x = abs(_x); |
| 33 | + |
| 34 | + FloatingPoint t = 1.0 / (1.0 + p*x); |
| 35 | + FloatingPoint y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * exp(-x * x); |
| 36 | + |
| 37 | + return sign * y; |
| 38 | +#else |
| 39 | + return std::erf(_x); |
| 40 | +#endif |
| 41 | +} |
| 42 | + |
| 43 | +template<typename FloatingPoint> |
| 44 | +inline FloatingPoint erfInv(FloatingPoint _x) |
| 45 | +{ |
| 46 | + FloatingPoint x = clamp<FloatingPoint>(_x, -0.99999, 0.99999); |
| 47 | +#ifdef __HLSL_VERSION |
| 48 | + FloatingPoint w = -log((1.0-x) * (1.0+x)); |
| 49 | +#else |
| 50 | + FloatingPoint w = -std::log((1.0-x) * (1.0+x)); |
| 51 | +#endif |
| 52 | + FloatingPoint p; |
| 53 | + if (w<5.0) |
| 54 | + { |
| 55 | + w -= 2.5; |
| 56 | + p = 2.81022636e-08; |
| 57 | + p = 3.43273939e-07 + p*w; |
| 58 | + p = -3.5233877e-06 + p*w; |
| 59 | + p = -4.39150654e-06 + p*w; |
| 60 | + p = 0.00021858087 + p*w; |
| 61 | + p = -0.00125372503 + p*w; |
| 62 | + p = -0.00417768164 + p*w; |
| 63 | + p = 0.246640727 + p*w; |
| 64 | + p = 1.50140941 + p*w; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | +#ifdef __HLSL_VERSION |
| 69 | + w = sqrt(w) - 3.0; |
| 70 | +#else |
| 71 | + w = std::sqrt(w) - 3.0; |
| 72 | +#endif |
| 73 | + p = -0.000200214257; |
| 74 | + p = 0.000100950558 + p*w; |
| 75 | + p = 0.00134934322 + p*w; |
| 76 | + p = -0.00367342844 + p*w; |
| 77 | + p = 0.00573950773 + p*w; |
| 78 | + p = -0.0076224613 + p*w; |
| 79 | + p = 0.00943887047 + p*w; |
| 80 | + p = 1.00167406 + p*w; |
| 81 | + p = 2.83297682 + p*w; |
| 82 | + } |
| 83 | + return p*x; |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +template<typename T> |
| 88 | +inline T floor(NBL_CONST_REF_ARG(T) val) |
| 89 | +{ |
| 90 | +#ifdef __HLSL_VERSION |
| 91 | + return spirv::floor<T>(val); |
| 92 | +#else |
| 93 | + return glm::floor(val); |
| 94 | +#endif |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +template<typename T, typename U> |
| 99 | +inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(U) a) |
| 100 | +{ |
| 101 | + return cpp_compat_intrinsics_impl::lerp_helper<T, U>::lerp(x, y, a); |
| 102 | +} |
| 103 | + |
| 104 | +template<typename FloatingPoint> |
| 105 | +inline FloatingPoint isnan(NBL_CONST_REF_ARG(FloatingPoint) val) |
| 106 | +{ |
| 107 | +#ifdef __HLSL_VERSION |
| 108 | + return spirv::isNan<T>(val); |
| 109 | +#else |
| 110 | + return std::isnan(val); |
| 111 | +#endif |
| 112 | +} |
| 113 | + |
| 114 | +template<typename FloatingPoint> |
| 115 | +inline FloatingPoint isinf(NBL_CONST_REF_ARG(FloatingPoint) val) |
| 116 | +{ |
| 117 | +#ifdef __HLSL_VERSION |
| 118 | + return spirv::isInf<T>(val); |
| 119 | +#else |
| 120 | + return std::isinf(val); |
| 121 | +#endif |
| 122 | +} |
| 123 | + |
| 124 | +template<typename T> |
| 125 | +inline T pow(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y) |
| 126 | +{ |
| 127 | +#ifdef __HLSL_VERSION |
| 128 | + return spirv::pow<T>(x, y); |
| 129 | +#else |
| 130 | + return std::pow(x, y); |
| 131 | +#endif |
| 132 | +} |
| 133 | + |
| 134 | +template<typename T> |
| 135 | +inline T exp(NBL_CONST_REF_ARG(T) val) |
| 136 | +{ |
| 137 | +#ifdef __HLSL_VERSION |
| 138 | + return spirv::exp<T>(val); |
| 139 | +#else |
| 140 | + return std::exp(val); |
| 141 | +#endif |
| 142 | +} |
| 143 | + |
| 144 | +template<typename T> |
| 145 | +inline T exp2(NBL_CONST_REF_ARG(T) val) |
| 146 | +{ |
| 147 | +#ifdef __HLSL_VERSION |
| 148 | + return spirv::exp2<T>(val); |
| 149 | +#else |
| 150 | + return std::exp2(val); |
| 151 | +#endif |
| 152 | +} |
| 153 | + |
| 154 | +#define DEFINE_EXP2_SPECIALIZATION(TYPE)\ |
| 155 | +template<>\ |
| 156 | +inline TYPE exp2(NBL_CONST_REF_ARG(TYPE) val)\ |
| 157 | +{\ |
| 158 | + return _static_cast<TYPE>(1ull << val);\ |
| 159 | +}\ |
| 160 | + |
| 161 | +DEFINE_EXP2_SPECIALIZATION(int16_t) |
| 162 | +DEFINE_EXP2_SPECIALIZATION(int32_t) |
| 163 | +DEFINE_EXP2_SPECIALIZATION(int64_t) |
| 164 | +DEFINE_EXP2_SPECIALIZATION(uint16_t) |
| 165 | +DEFINE_EXP2_SPECIALIZATION(uint32_t) |
| 166 | +DEFINE_EXP2_SPECIALIZATION(uint64_t) |
| 167 | + |
| 168 | +template<typename T> |
| 169 | +inline T log(NBL_CONST_REF_ARG(T) val) |
| 170 | +{ |
| 171 | +#ifdef __HLSL_VERSION |
| 172 | + return spirv::log<T>(val); |
| 173 | +#else |
| 174 | + return std::log(val); |
| 175 | +#endif |
| 176 | +} |
| 177 | + |
| 178 | +template<typename T> |
| 179 | +inline T abs(NBL_CONST_REF_ARG(T) val) |
| 180 | +{ |
| 181 | +#ifdef __HLSL_VERSION |
| 182 | + return abs(val); |
| 183 | +#else |
| 184 | + return glm::abs(val); |
| 185 | +#endif |
| 186 | +} |
| 187 | + |
| 188 | +template<typename T> |
| 189 | +inline T sqrt(NBL_CONST_REF_ARG(T) val) |
| 190 | +{ |
| 191 | +#ifdef __HLSL_VERSION |
| 192 | + return sqrt(val); |
| 193 | +#else |
| 194 | + return std::sqrt(val); |
| 195 | +#endif |
| 196 | +} |
| 197 | + |
| 198 | +template<typename T> |
| 199 | +inline T sin(NBL_CONST_REF_ARG(T) val) |
| 200 | +{ |
| 201 | +#ifdef __HLSL_VERSION |
| 202 | + return sin(val); |
| 203 | +#else |
| 204 | + return std::sin(val); |
| 205 | +#endif |
| 206 | +} |
| 207 | + |
| 208 | +template<typename T> |
| 209 | +inline T cos(NBL_CONST_REF_ARG(T) val) |
| 210 | +{ |
| 211 | +#ifdef __HLSL_VERSION |
| 212 | + return cos(val); |
| 213 | +#else |
| 214 | + return std::cos(val); |
| 215 | +#endif |
| 216 | +} |
| 217 | + |
| 218 | +template<typename T> |
| 219 | +inline T acos(NBL_CONST_REF_ARG(T) val) |
| 220 | +{ |
| 221 | +#ifdef __HLSL_VERSION |
| 222 | + return acos(val); |
| 223 | +#else |
| 224 | + return std::acos(val); |
| 225 | +#endif |
| 226 | +} |
| 227 | + |
| 228 | +} |
| 229 | +} |
| 230 | + |
| 231 | +#endif |
0 commit comments