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 software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
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   // TODO fix this for other types
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; // total instructions for hashlcg will be LCG_REPS * 2 + 2
00055 // TODO: may not work as intended on 24-bit floats
00056 // since there may not be enough precision 
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   // TODO fix this for long tuples
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; // total instructions for hashmrg will be MRG_REPS * N * 2 + 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 

Generated on Thu Apr 21 17:32:47 2005 for Sh by  doxygen 1.4.2