Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShLibClampImpl.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
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 smoothstep (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 inline
00246 ShGeneric<N, T>
00247 smoothpulse (const ShGeneric<N, T>& t, const ShGeneric<N, T>& r0, 
00248              const ShGeneric<N, T>& r1, const ShGeneric<N, T>& w)
00249 {
00250    return sstep(t,r0,w) - sstep(t,r1,w);
00251 }
00252 
00253 
00254 }
00255 
00256 #endif

Generated on Thu Jul 28 17:33:03 2005 for Sh by  doxygen 1.4.3-20050530