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

ShFuncImpl.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 SHUTIL_FUNCIMPL_HPP 
00021 #define SHUTIL_FUNCIMPL_HPP 
00022 
00023 #include <cmath>
00024 #include <numeric>
00025 #include "ShAttrib.hpp"
00026 #include "ShSwizzle.hpp" 
00027 #include "ShVariable.hpp"
00028 #include "ShFunc.hpp"
00029 #include "ShLib.hpp"
00030 
00031 namespace ShUtil {
00032 
00033 using namespace SH;
00034 
00035 template<int N, typename T>
00036 ShGeneric<N, T> deprecated_smoothstep(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b,
00037     const ShGeneric<N, T> x) {
00038   ShGeneric<N, T> t = (x - a) / (b - a);
00039   // TODO fix this for other types
00040   t = clamp(t, 0.0f, 1.0f); 
00041   return t * t * mad(-2.0f, t, ShConstAttrib1f(3.0f));
00042 }
00043 
00044 static const int LCG_REPS = 5; // total instructions for hashlcg will be LCG_REPS * 2 + 2
00051 // TODO: may not work as intended on 24-bit floats
00052 // since there may not be enough precision 
00053 template<int N, typename T>
00054 ShGeneric<N, T> hashlcg(const ShGeneric<N, T>& p) {
00055   ShAttrib<N, SH_TEMP, T> result = frac(p * 0.01);
00056 
00057   // TODO fix this for long tuples
00058   ShGeneric<N, T> a = fillcast<N>(
00059       ShConstAttrib4f(M_PI * M_PI * M_PI * M_PI, std::exp(4.0), 
00060           std::pow(13.0, M_PI / 2.0), std::sqrt(1997.0)));
00061   ShGeneric<N, T> m = fillcast<N>(
00062       ShConstAttrib4f(std::sqrt(2.0), 1.0 / M_PI, std::sqrt(3.0), 
00063           std::exp(-1.0)));
00064 
00065   for(int i = 0; i < LCG_REPS; ++i) result = frac(mad(result, a, m)); 
00066   return result;
00067 }
00068 
00072 template<typename T>
00073 ShGeneric<3, T> changeBasis(const ShGeneric<3, T> &b0, 
00074     const ShGeneric<3, T> &b1, const ShGeneric<3, T> &b2, const ShGeneric<3, T> &v) {
00075   ShAttrib<3, SH_TEMP, T> result;
00076   result(0) = b0 | v;
00077   result(1) = b1 | v;
00078   result(2) = b2 | v;
00079   return result;
00080 }
00081 
00084 template <int N, typename T>
00085 ShAttrib4f bernstein_basis_4(const ShGeneric<N, T>& t)
00086 {
00087    ShAttrib4f r;
00088    ShAttrib<N,SH_TEMP, T> it = fillcast<N>(ShAttrib<1,SH_TEMP, T>(1.0f)) - t;
00089    r(0) = it*it*it;
00090    r(1) = 3.0*it*it*t;
00091    r(2) = 3.0*it*t*t;
00092    r(3) = t*t*t;
00093    return r;
00094 }
00095 
00098 template <int N, typename T>
00099 ShGeneric<N, T> bezier(const ShGeneric<N, T>& t, const ShAttrib4f& p)
00100 {
00101    ShAttrib4f B = bernstein_basis_4(t);
00102    ShGeneric<N, T> r = B[0] * p[0];
00103    for (int i=1; i<4; i++) {
00104       r += B[i] * p[i];  
00105    }
00106    return r;
00107 }
00108 
00109 }
00110 
00111 #endif // SHUTIL_FUNCIMPL_HPP 

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