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

ShNibblesImpl.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
00027 #ifndef SHNIBBLESIMPL_HPP
00028 #define SHNIBBLESIMPL_HPP
00029 
00030 #include "ShNibbles.hpp"
00031 #include "ShTexCoord.hpp"
00032 
00033 namespace SH {
00034 
00035 template<typename T>
00036 ShProgram keep(const std::string & name) {
00037   ShProgram nibble = SH_BEGIN_PROGRAM() {
00038     typename T::InOutType SH_NAMEDECL(attr, name); 
00039   } SH_END_PROGRAM;
00040   nibble.name("keep");
00041   return nibble;
00042 }
00043 
00044 template<typename T>
00045 ShProgram dup(const std::string & name) {
00046   ShProgram nibble = SH_BEGIN_PROGRAM() {
00047     typename T::InputType SH_NAMEDECL(attr, name); 
00048     typename T::OutputType SH_NAMEDECL(attr1, name + "_1") = attr; 
00049     typename T::OutputType SH_NAMEDECL(attr2, name + "_2") = attr; 
00050   } SH_END_PROGRAM;
00051   nibble.name("dup");
00052   return nibble;
00053 }
00054 
00055 template<typename T>
00056 ShProgram lose(const std::string & name) {
00057   ShProgram nibble = SH_BEGIN_PROGRAM() {
00058     typename T::InputType SH_NAMEDECL(attr, name);
00059   } SH_END_PROGRAM;
00060   nibble.name("lose");
00061   return nibble;
00062 };
00063 
00064 template<typename T>
00065 ShProgram access(const ShBaseTexture1D<T> &tex, const std::string &tcname, const std::string & name) {
00066   ShProgram nibble = SH_BEGIN_PROGRAM() {
00067     ShInputTexCoord1f SH_NAMEDECL(tc, tcname);
00068     typename T::OutputType SH_NAMEDECL(result, name) = tex(tc);
00069   } SH_END;
00070   nibble.name("access");
00071   return nibble;
00072 }
00073 
00074 template<typename T>
00075 ShProgram access(const ShBaseTexture2D<T> &tex, const std::string & tcname, const std::string & name) {
00076   ShProgram nibble = SH_BEGIN_PROGRAM() {
00077     ShInputTexCoord2f SH_NAMEDECL(tc, tcname);
00078     typename T::OutputType SH_NAMEDECL(result, name) = tex(tc);
00079   } SH_END;
00080   nibble.name("access");
00081   return nibble;
00082 }
00083 
00084 template<typename T>
00085 ShProgram access(const ShBaseTextureRect<T> &tex, const std::string & tcname, const std::string & name) {
00086   ShProgram nibble = SH_BEGIN_PROGRAM() {
00087     ShInputTexCoord2f SH_NAMEDECL(tc, tcname);
00088     typename T::OutputType SH_NAMEDECL(result, name) = tex(tc);
00089   } SH_END;
00090   nibble.name("access");
00091   return nibble;
00092 }
00093 
00094 template<typename T>
00095 ShProgram access(const ShBaseTexture3D<T> &tex, const std::string & tcname, const std::string & name) {
00096   ShProgram nibble = SH_BEGIN_PROGRAM() {
00097     ShInputTexCoord3f SH_NAMEDECL(tc, tcname);
00098     typename T::OutputType SH_NAMEDECL(result, name) = tex(tc);
00099   } SH_END;
00100   nibble.name("access");
00101   return nibble;
00102 }
00103 
00104 template<typename T>
00105 ShProgram access(const ShBaseTextureCube<T> &tex, const std::string & tcname, const std::string & name) {
00106   ShProgram nibble = SH_BEGIN_PROGRAM() {
00107     ShInputTexCoord3f SH_NAMEDECL(tc, tcname);
00108     typename T::OutputType SH_NAMEDECL(result, name) = tex(tc);
00109   } SH_END;
00110   nibble.name("access");
00111   return nibble;
00112 }
00113 
00114 template<typename T, int Rows, int Cols, ShBindingType Binding, typename T2>
00115 ShProgram transform(const ShMatrix<Rows, Cols, Binding, T2> &m, const std::string & name) {
00116   ShProgram nibble = SH_BEGIN_PROGRAM() {
00117     typename T::InOutType SH_NAMEDECL(attrib, name) = m | attrib;
00118   } SH_END;
00119   nibble.name("transform");
00120   return nibble;
00121 }
00122 
00123 template<typename T, typename T2>
00124 ShProgram cast(const std::string & name) {
00125   ShProgram nibble = SH_BEGIN_PROGRAM() {
00126     typename T::InputType SH_NAMEDECL(in, name);
00127     typename T2::OutputType SH_NAMEDECL(out, name) = cast<T2::typesize>( in );
00128   } SH_END;
00129   nibble.name("cast");
00130   return nibble;
00131 }
00132 
00133 template<typename T, typename T2>
00134 ShProgram fillcast(const std::string & name) {
00135   ShProgram nibble = SH_BEGIN_PROGRAM() {
00136     typename T::InputType SH_NAMEDECL(in, name);
00137     typename T2::OutputType SH_NAMEDECL(out, name) = fillcast<T2::typesize>( in );
00138   } SH_END;
00139   nibble.name("fillcast");
00140   return nibble;
00141 }
00142 
00143 #define SHNIBBLE_UNARY_OP(opfunc, opcode) \
00144 template<typename T>\
00145 ShProgram opfunc(const std::string & name) {\
00146   ShProgram nibble = SH_BEGIN_PROGRAM() {\
00147     typename T::InOutType SH_NAMEDECL(x, name) = opcode;\
00148   } SH_END;\
00149   nibble.name(# opfunc); \
00150   return nibble; \
00151 }
00152 
00153 SHNIBBLE_UNARY_OP(abs, abs(x));
00154 SHNIBBLE_UNARY_OP(acos, acos(x));
00155 SHNIBBLE_UNARY_OP(asin, asin(x));
00156 SHNIBBLE_UNARY_OP(cos, cos(x));
00157 SHNIBBLE_UNARY_OP(frac, frac(x));
00158 SHNIBBLE_UNARY_OP(sin, sin(x));
00159 SHNIBBLE_UNARY_OP(sqrt, sqrt(x));
00160 SHNIBBLE_UNARY_OP(normalize, normalize(x));
00161 SHNIBBLE_UNARY_OP(pos, pos(x));
00162 
00163 #define SHNIBBLE_BINARY_OP(opfunc, opcode) \
00164 template<typename T1, typename T2> \
00165 ShProgram opfunc(const std::string & output_name, \
00166     const std::string & input_name0, const std::string & input_name1) { \
00167   ShProgram nibble = SH_BEGIN_PROGRAM() { \
00168     typename T1::InputType SH_NAMEDECL(a, input_name0); \
00169     typename T2::InputType SH_NAMEDECL(b, input_name1); \
00170     typename SelectType<(T1::typesize > T2::typesize), typename T1::OutputType, typename T2::OutputType>::type\
00171       SH_NAMEDECL(result, output_name) = opcode; \
00172   } SH_END; \
00173   nibble.name(# opfunc); \
00174   return nibble; \
00175 } \
00176 \
00177 template<typename T1> \
00178 ShProgram opfunc(const std::string & output_name,\
00179     const std::string & input_name0, const std::string & input_name1 ) { \
00180   return opfunc<T1, T1>(output_name, input_name0, input_name1); \
00181 }
00182 
00183 SHNIBBLE_BINARY_OP(add, a + b)
00184 SHNIBBLE_BINARY_OP(sub, a - b)
00185 SHNIBBLE_BINARY_OP(mul, a * b)
00186 SHNIBBLE_BINARY_OP(div, a / b) 
00187 SHNIBBLE_BINARY_OP(pow, pow(a, b))
00188 SHNIBBLE_BINARY_OP(slt, a < b) 
00189 SHNIBBLE_BINARY_OP(sle, a <= b) 
00190 SHNIBBLE_BINARY_OP(sgt, a > b) 
00191 SHNIBBLE_BINARY_OP(sge, a >= b) 
00192 SHNIBBLE_BINARY_OP(seq, a == b) 
00193 SHNIBBLE_BINARY_OP(sne, a != b) 
00194 SHNIBBLE_BINARY_OP(mod, mod(a, b))
00195 SHNIBBLE_BINARY_OP(min, min(a, b))
00196 SHNIBBLE_BINARY_OP(max, max(a, b))
00197 
00198 template<typename T> 
00199 ShProgram dot(const std::string & name) { 
00200   ShProgram nibble = SH_BEGIN_PROGRAM() {
00201     typename T::InputType SH_DECL(a); 
00202     typename T::InputType SH_DECL(b); 
00203     ShOutputAttrib1f SH_NAMEDECL(result, name) = dot(a, b); 
00204   } SH_END; 
00205   nibble.name("dot");
00206   return nibble;
00207 }
00208 
00209 template<typename T1, typename T2>
00210 ShProgram lerp(const std::string & name) { 
00211   ShProgram nibble = SH_BEGIN_PROGRAM() {
00212     typename T1::InputType SH_DECL(a); 
00213     typename T1::InputType SH_DECL(b); 
00214     typename T2::InputType SH_DECL(alpha); 
00215     typename T1::OutputType SH_NAMEDECL(result, name) = lerp(alpha, a, b); 
00216   } SH_END; 
00217   nibble.name("lerp");
00218   return nibble;
00219 }
00220 
00221 template<typename T1>
00222 ShProgram lerp(const std::string & name) { 
00223   return lerp<T1, T1>(name);
00224 }
00225 
00226 
00227 }
00228 
00229 #endif

Generated on Mon Jan 24 18:36:33 2005 for Sh by  doxygen 1.4.1