FastMathFunctions.hlsl 791 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Shaders/Common.hlsl>
  7. /// Sin approximation: https://www.desmos.com/calculator/svgcjfskne
  8. template<typename T>
  9. T fastSin(T x)
  10. {
  11. x = (x + kHalfPi) / (k2Pi) + T(0.75);
  12. x = frac(x);
  13. x = x * T(2) - T(1);
  14. x = x * abs(x) - x;
  15. x *= T(4);
  16. return x;
  17. }
  18. /// Cos approximation
  19. template<typename T>
  20. T fastCos(T x)
  21. {
  22. return fastSin<T>(x + T(kHalfPi));
  23. }
  24. F32 fastSqrt(F32 x)
  25. {
  26. return asfloat(0x1FBD1DF5 + (asint(x) >> 1));
  27. }
  28. template<typename T>
  29. T fastAcos(T x)
  30. {
  31. T res = T(-0.156583) * abs(x) + T(kHalfPi);
  32. res *= fastSqrt(T(1) - abs(x));
  33. return (x > T(0)) ? res : T(kPi) - res;
  34. }