00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020 #ifndef SHLIBCLAMPIMPL_HPP
00021 #define SHLIBCLAMPIMPL_HPP
00022
00023 #include "ShLibClamp.hpp"
00024 #include "ShInstructions.hpp"
00025 #include "ShAttrib.hpp"
00026 #include "ShLibMiscImpl.hpp"
00027
00028 namespace SH {
00029
00030 template<int N, typename T>
00031 ShGeneric<N, T> abs(const ShGeneric<N, T>& var)
00032 {
00033 ShAttrib<N, SH_TEMP, T> t;
00034 shABS(t, var);
00035 return t;
00036 }
00037
00038 template<int N, typename T>
00039 ShGeneric<N, T> ceil(const ShGeneric<N, T>& var)
00040 {
00041 ShAttrib<N, SH_TEMP, T> t;
00042 shCEIL(t, var);
00043 return t;
00044 }
00045
00046 template<int N, typename T>
00047 ShGeneric<N, T> floor(const ShGeneric<N, T>& var)
00048 {
00049 ShAttrib<N, SH_TEMP, T> t;
00050 shFLR(t, var);
00051 return t;
00052 }
00053
00054 template<int N, typename T>
00055 ShGeneric<N, T> round(const ShGeneric<N, T>& var)
00056 {
00057 ShAttrib<N, SH_TEMP, T> t;
00058 shRND(t, var);
00059 return t;
00060 }
00061
00062 template<int N, typename T1, typename T2>
00063 ShGeneric<N, CT1T2> mod(const ShGeneric<N, T1>& left, const ShGeneric<N, T2>& right)
00064 {
00065 ShAttrib<N, SH_TEMP, CT1T2> t;
00066 shMOD(t, left, right);
00067 return t;
00068 }
00069 template<int N, typename T1, typename T2>
00070 ShGeneric<N, CT1T2> mod(const ShGeneric<N, T1>& left, const ShGeneric<1, T2>& right)
00071 {
00072 ShAttrib<N, SH_TEMP, CT1T2> t;
00073 shMOD(t, left, right);
00074 return t;
00075 }
00076 template<int N, typename T1, typename T2>
00077 ShGeneric<N, CT1T2> mod(const ShGeneric<1, T1>& left, const ShGeneric<N, T2>& right)
00078 {
00079 ShAttrib<N, SH_TEMP, CT1T2> t;
00080 shMOD(t, left, right);
00081 return t;
00082 }
00083 template<typename T1, typename T2>
00084 ShGeneric<1, CT1T2> mod(const ShGeneric<1, T1>& left, const ShGeneric<1, T2>& right)
00085 {
00086 ShAttrib<1, SH_TEMP, CT1T2> t;
00087 shMOD(t, left, right);
00088 return t;
00089 }
00090
00091 template<int N, typename T1, typename T2>
00092 inline
00093 ShGeneric<N, CT1T2> operator%(const ShGeneric<N, T1>& left, const ShGeneric<N, T2>& right)
00094 {
00095 return mod(left, right);
00096 }
00097 template<int N, typename T1, typename T2>
00098 inline
00099 ShGeneric<N, CT1T2> operator%(const ShGeneric<N, T1>& left, const ShGeneric<1, T2>& right)
00100 {
00101 return mod(left, right);
00102 }
00103 template<int N, typename T1, typename T2>
00104 inline
00105 ShGeneric<N, CT1T2> operator%(const ShGeneric<1, T1>& left, const ShGeneric<N, T2>& right)
00106 {
00107 return mod(left, right);
00108 }
00109 template<typename T1, typename T2>
00110 inline
00111 ShGeneric<1, CT1T2> operator%(const ShGeneric<1, T1>& left, const ShGeneric<1, T2>& right)
00112 {
00113 return mod(left, right);
00114 }
00115 SH_SHLIB_CONST_SCALAR_OP(mod);
00116 SH_SHLIB_CONST_N_OP_LEFT(mod);
00117 SH_SHLIB_CONST_SCALAR_OP(operator%);
00118 SH_SHLIB_CONST_N_OP_LEFT(operator%);
00119
00120 template<int N, typename T>
00121 ShGeneric<N, T> frac(const ShGeneric<N, T>& var)
00122 {
00123 ShAttrib<N, SH_TEMP, T> t;
00124 shFRAC(t, var);
00125 return t;
00126 }
00127
00128 template<int N, typename T>
00129 inline
00130 ShGeneric<N, T> pos(const ShGeneric<N, T>& var)
00131 {
00132 return max(var, fillcast<N>(0.0f));
00133 }
00134
00135 template<int N, typename T1, typename T2>
00136 ShGeneric<N, CT1T2> max(const ShGeneric<N, T1>& left, const ShGeneric<N, T2>& right)
00137 {
00138 ShAttrib<N, SH_TEMP, CT1T2> t;
00139 shMAX(t, left, right);
00140 return t;
00141 }
00142 SH_SHLIB_CONST_SCALAR_OP(max);
00143
00144 template<int N, typename T1, typename T2>
00145 ShGeneric<N, CT1T2> min(const ShGeneric<N, T1>& left, const ShGeneric<N, T2>& right)
00146 {
00147 ShAttrib<N, SH_TEMP, CT1T2> t;
00148 shMIN(t, left, right);
00149 return t;
00150 }
00151 SH_SHLIB_CONST_SCALAR_OP(min);
00152
00153 template<int N, typename T>
00154 ShGeneric<1, T> max(const ShGeneric<N, T>& a)
00155 {
00156 int lhswz[N/2 + N%2];
00157 for (int i = 0; i < N/2 + N%2; i++) {
00158 lhswz[i] = i;
00159 }
00160 int rhswz[N/2];
00161 for (int i = 0; i < N/2; i++) {
00162 rhswz[i] = i + N/2 + N%2;
00163 }
00164
00165 return max(max(a.template swiz<N/2 + N%2>(lhswz)), max(a.template swiz<N/2>(rhswz)));
00166 }
00167
00168 template<typename T>
00169 inline
00170 ShGeneric<1, T> max(const ShGeneric<1, T>& a)
00171 {
00172 return a;
00173 }
00174
00175 template<int N, typename T>
00176 ShGeneric<1, T> min(const ShGeneric<N, T>& a)
00177 {
00178 int lhswz[N/2 + N%2];
00179 for (int i = 0; i < N/2 + N%2; i++) {
00180 lhswz[i] = i;
00181 }
00182 int rhswz[N/2];
00183 for (int i = 0; i < N/2; i++) {
00184 rhswz[i] = i + N/2 + N%2;
00185 }
00186
00187 return min(min(a.template swiz<N/2 + N%2>(lhswz)), min(a.template swiz<N/2>(rhswz)));
00188 }
00189
00190 template<typename T>
00191 inline
00192 ShGeneric<1, T> min(const ShGeneric<1, T>& a)
00193 {
00194 return a;
00195 }
00196
00197 template<int N, typename T1, typename T2, typename T3>
00198 inline
00199 ShGeneric<N, CT1T2T3> clamp(const ShGeneric<N, T1>& a,
00200 const ShGeneric<N, T2>& b, const ShGeneric<N, T3>& c)
00201 {
00202 return min(max(a, b), c);
00203 }
00204 template<int N, typename T1, typename T2, typename T3>
00205 inline
00206 ShGeneric<N, CT1T2T3> clamp(const ShGeneric<N, T1>& a,
00207 const ShGeneric<1, T2>& b, const ShGeneric<1, T3>& c)
00208 {
00209 return min(max(a, fillcast<N>(b)), fillcast<N>(c));
00210 }
00211
00212 template<typename T1, typename T2, typename T3>
00213 inline
00214 ShGeneric<1, CT1T2T3> clamp(const ShGeneric<1, T1>& a,
00215 const ShGeneric<1, T2>& b, const ShGeneric<1, T3>& c)
00216 {
00217 return min(max(a, b), c);
00218 }
00219 SH_SHLIB_CONST_TRINARY_OP_011(clamp);
00220
00221 template<int N, typename T>
00222 inline
00223 ShGeneric<N, T> sat(const ShGeneric<N, T>& var)
00224 {
00225 return min(var, fillcast<N>(ShConstAttrib1f(1.0)));
00226 }
00227
00228 template<int N, typename T>
00229 ShGeneric<N, T> sign(const ShGeneric<N, T>& var)
00230 {
00231 ShAttrib<N, SH_TEMP, T> t;
00232 shSGN(t, var);
00233 return t;
00234 }
00235
00236 template <int N, typename T>
00237 inline
00238 ShGeneric<N, T>
00239 linstep(const ShGeneric<N, T>& t, const ShGeneric<N, T>& c, const ShGeneric<N, T>& w)
00240 {
00241 return clamp((t - c)/w + 0.5f, 0.0f, 1.0f);
00242 }
00243
00244 template<int N, typename T>
00245 ShGeneric<N, T>
00246 smoothstep(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b, const ShGeneric<N, T>& x)
00247 {
00248 ShGeneric<N, T> t = (x - a) / (b - a);
00249
00250 t = clamp(t, 0.0f, 1.0f);
00251 return t * t * mad(-2.0f, t, ShConstAttrib1f(3.0f));
00252 }
00253
00254 template <int N, typename T>
00255 inline
00256 ShGeneric<N, T>
00257 smoothpulse(const ShGeneric<N, T>& t, const ShGeneric<N, T>& r0,
00258 const ShGeneric<N, T>& r1, const ShGeneric<N, T>& w)
00259 {
00260 return smoothstep(t,r0,w) - smoothstep(t,r1,w);
00261 }
00262
00263 }
00264
00265 #endif