00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024 #ifndef SHUTIL_FUNCIMPL_HPP
00025 #define SHUTIL_FUNCIMPL_HPP
00026
00027 #include <cmath>
00028 #include <numeric>
00029 #include "ShAttrib.hpp"
00030 #include "ShSwizzle.hpp"
00031 #include "ShVariable.hpp"
00032 #include "ShFunc.hpp"
00033 #include "ShLib.hpp"
00034
00035 namespace ShUtil {
00036
00037 using namespace SH;
00038
00039 template<int N, typename T>
00040 ShGeneric<N, T> smoothstep(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b,
00041 const ShGeneric<N, T> x) {
00042 ShGeneric<N, T> t = (x - a) / (b - a);
00043
00044 t = clamp(t, 0.0f, 1.0f);
00045 return t * t * mad(-2.0f, t, ShConstAttrib1f(3.0f));
00046 }
00047
00048 static const int LCG_REPS = 5;
00055
00056
00057 template<int N, typename T>
00058 ShGeneric<N, T> hashlcg(const ShGeneric<N, T>& p) {
00059 ShAttrib<N, SH_TEMP, T> result = frac(p * 0.01);
00060
00061
00062 ShGeneric<N, T> a = fillcast<N>(
00063 ShConstAttrib4f(M_PI * M_PI * M_PI * M_PI, std::exp(4.0),
00064 std::pow(13.0, M_PI / 2.0), std::sqrt(1997.0)));
00065 ShGeneric<N, T> m = fillcast<N>(
00066 ShConstAttrib4f(std::sqrt(2.0), 1.0 / M_PI, std::sqrt(3.0),
00067 std::exp(-1.0)));
00068
00069 for(int i = 0; i < LCG_REPS; ++i) result = frac(mad(result, a, m));
00070 return result;
00071 }
00072
00073
00074 static const int MRG_REPS = 2;
00087 template<int N, typename T>
00088 ShGeneric<N, T> hashmrg(const ShGeneric<N, T>& p) {
00089 ShAttrib<N, SH_TEMP, T> result = frac(p * 0.01);
00090 ShGeneric<N, T> a = fillcast<N>(
00091 ShConstAttrib4f(M_PI * M_PI * M_PI * M_PI, std::exp(4.0),
00092 std::pow(13.0, M_PI / 2.0), std::sqrt(1997.0)));
00093
00094 for(int i = 0; i < MRG_REPS; ++i) {
00095 for(int j = 0; j < N; ++j) {
00096 result(j) = frac(dot(result, a));
00097 }
00098 }
00099 return result;
00100 }
00101
00105 template<typename T>
00106 ShGeneric<3, T> changeBasis(const ShGeneric<3, T> &b0,
00107 const ShGeneric<3, T> &b1, const ShGeneric<3, T> &b2, const ShGeneric<3, T> &v) {
00108 ShAttrib<3, SH_TEMP, T> result;
00109 result(0) = b0 | v;
00110 result(1) = b1 | v;
00111 result(2) = b2 | v;
00112 return result;
00113 }
00114
00115 }
00116
00117 #endif // SHUTIL_FUNCIMPL_HPP