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