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 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 SHNIBBLESIMPL_HPP
00025 #define SHNIBBLESIMPL_HPP
00026 
00027 #include "ShNibbles.hpp"
00028 #include "ShTexCoord.hpp"
00029 
00030 namespace SH {
00031 
00032 template<typename T>
00033 ShProgram keep(const std::string & name) {
00034   ShProgram nibble = SH_BEGIN_PROGRAM() {
00035     typename T::InOutType SH_NAMEDECL(attr, name); 
00036   } SH_END_PROGRAM;
00037   nibble.name("keep");
00038   return nibble;
00039 }
00040 
00041 template<typename T>
00042 ShProgram dup(const std::string & name) {
00043   ShProgram nibble = SH_BEGIN_PROGRAM() {
00044     typename T::InputType SH_NAMEDECL(attr, name); 
00045     typename T::OutputType SH_NAMEDECL(attr1, name + "_1") = attr; 
00046     typename T::OutputType SH_NAMEDECL(attr2, name + "_2") = attr; 
00047   } SH_END_PROGRAM;
00048   nibble.name("dup");
00049   return nibble;
00050 }
00051 
00052 template<typename T>
00053 ShProgram lose(const std::string & name) {
00054   ShProgram nibble = SH_BEGIN_PROGRAM() {
00055     typename T::InputType SH_NAMEDECL(attr, name);
00056   } SH_END_PROGRAM;
00057   nibble.name("lose");
00058   return nibble;
00059 };
00060 
00061 template<typename T>
00062 ShProgram access(const ShBaseTexture1D<T> &tex, const std::string &tcname, const std::string & name) {
00063   ShProgram nibble = SH_BEGIN_PROGRAM() {
00064     ShInputTexCoord1f SH_NAMEDECL(tc, tcname);
00065     typename T::OutputType SH_NAMEDECL(result, name) = tex(tc);
00066   } SH_END;
00067   nibble.name("access");
00068   return nibble;
00069 }
00070 
00071 template<typename T>
00072 ShProgram access(const ShBaseTexture2D<T> &tex, const std::string & tcname, const std::string & name) {
00073   ShProgram nibble = SH_BEGIN_PROGRAM() {
00074     ShInputTexCoord2f SH_NAMEDECL(tc, tcname);
00075     typename T::OutputType SH_NAMEDECL(result, name) = tex(tc);
00076   } SH_END;
00077   nibble.name("access");
00078   return nibble;
00079 }
00080 
00081 template<typename T>
00082 ShProgram access(const ShBaseTextureRect<T> &tex, const std::string & tcname, const std::string & name) {
00083   ShProgram nibble = SH_BEGIN_PROGRAM() {
00084     ShInputTexCoord2f SH_NAMEDECL(tc, tcname);
00085     typename T::OutputType SH_NAMEDECL(result, name) = tex(tc);
00086   } SH_END;
00087   nibble.name("access");
00088   return nibble;
00089 }
00090 
00091 template<typename T>
00092 ShProgram access(const ShBaseTexture3D<T> &tex, const std::string & tcname, const std::string & name) {
00093   ShProgram nibble = SH_BEGIN_PROGRAM() {
00094     ShInputTexCoord3f SH_NAMEDECL(tc, tcname);
00095     typename T::OutputType SH_NAMEDECL(result, name) = tex(tc);
00096   } SH_END;
00097   nibble.name("access");
00098   return nibble;
00099 }
00100 
00101 template<typename T>
00102 ShProgram access(const ShBaseTextureCube<T> &tex, const std::string & tcname, const std::string & name) {
00103   ShProgram nibble = SH_BEGIN_PROGRAM() {
00104     ShInputTexCoord3f SH_NAMEDECL(tc, tcname);
00105     typename T::OutputType SH_NAMEDECL(result, name) = tex(tc);
00106   } SH_END;
00107   nibble.name("access");
00108   return nibble;
00109 }
00110 
00111 template<typename T, int Rows, int Cols, ShBindingType Binding, typename T2>
00112 ShProgram transform(const ShMatrix<Rows, Cols, Binding, T2> &m, const std::string & name) {
00113   ShProgram nibble = SH_BEGIN_PROGRAM() {
00114     typename T::InOutType SH_NAMEDECL(attrib, name) = m | attrib;
00115   } SH_END;
00116   nibble.name("transform");
00117   return nibble;
00118 }
00119 
00120 template<typename T, typename T2>
00121 ShProgram cast(const std::string & name) {
00122   ShProgram nibble = SH_BEGIN_PROGRAM() {
00123     typename T::InputType SH_NAMEDECL(in, name);
00124     typename T2::OutputType SH_NAMEDECL(out, name) = cast<T2::typesize>( in );
00125   } SH_END;
00126   nibble.name("cast");
00127   return nibble;
00128 }
00129 
00130 template<typename T, typename T2>
00131 ShProgram fillcast(const std::string & name) {
00132   ShProgram nibble = SH_BEGIN_PROGRAM() {
00133     typename T::InputType SH_NAMEDECL(in, name);
00134     typename T2::OutputType SH_NAMEDECL(out, name) = fillcast<T2::typesize>( in );
00135   } SH_END;
00136   nibble.name("fillcast");
00137   return nibble;
00138 }
00139 
00140 #define SHNIBBLE_UNARY_OP(opfunc, opcode) \
00141 template<typename T>\
00142 ShProgram opfunc(const std::string & name) {\
00143   ShProgram nibble = SH_BEGIN_PROGRAM() {\
00144     typename T::InOutType SH_NAMEDECL(x, name) = opcode;\
00145   } SH_END;\
00146   nibble.name(# opfunc); \
00147   return nibble; \
00148 }
00149 
00150 SHNIBBLE_UNARY_OP(abs, abs(x));
00151 SHNIBBLE_UNARY_OP(acos, acos(x));
00152 SHNIBBLE_UNARY_OP(asin, asin(x));
00153 SHNIBBLE_UNARY_OP(cos, cos(x));
00154 SHNIBBLE_UNARY_OP(frac, frac(x));
00155 SHNIBBLE_UNARY_OP(sin, sin(x));
00156 SHNIBBLE_UNARY_OP(sqrt, sqrt(x));
00157 SHNIBBLE_UNARY_OP(normalize, normalize(x));
00158 SHNIBBLE_UNARY_OP(pos, pos(x));
00159 
00160 #define SHNIBBLE_BINARY_OP(opfunc, opcode) \
00161 template<typename T1, typename T2> \
00162 ShProgram opfunc(const std::string & output_name, \
00163     const std::string & input_name0, const std::string & input_name1) { \
00164   ShProgram nibble = SH_BEGIN_PROGRAM() { \
00165     typename T1::InputType SH_NAMEDECL(a, input_name0); \
00166     typename T2::InputType SH_NAMEDECL(b, input_name1); \
00167     typename SelectType<(T1::typesize > T2::typesize), typename T1::OutputType, typename T2::OutputType>::type\
00168       SH_NAMEDECL(result, output_name) = opcode; \
00169   } SH_END; \
00170   nibble.name(# opfunc); \
00171   return nibble; \
00172 } \
00173 \
00174 template<typename T1> \
00175 ShProgram opfunc(const std::string & output_name,\
00176     const std::string & input_name0, const std::string & input_name1 ) { \
00177   return opfunc<T1, T1>(output_name, input_name0, input_name1); \
00178 }
00179 
00180 SHNIBBLE_BINARY_OP(add, a + b)
00181 SHNIBBLE_BINARY_OP(sub, a - b)
00182 SHNIBBLE_BINARY_OP(mul, a * b)
00183 SHNIBBLE_BINARY_OP(div, a / b) 
00184 SHNIBBLE_BINARY_OP(pow, pow(a, b))
00185 SHNIBBLE_BINARY_OP(slt, a < b) 
00186 SHNIBBLE_BINARY_OP(sle, a <= b) 
00187 SHNIBBLE_BINARY_OP(sgt, a > b) 
00188 SHNIBBLE_BINARY_OP(sge, a >= b) 
00189 SHNIBBLE_BINARY_OP(seq, a == b) 
00190 SHNIBBLE_BINARY_OP(sne, a != b) 
00191 SHNIBBLE_BINARY_OP(mod, mod(a, b))
00192 SHNIBBLE_BINARY_OP(min, min(a, b))
00193 SHNIBBLE_BINARY_OP(max, max(a, b))
00194 
00195 template<typename T> 
00196 ShProgram dot(const std::string & name) { 
00197   ShProgram nibble = SH_BEGIN_PROGRAM() {
00198     typename T::InputType SH_DECL(a); 
00199     typename T::InputType SH_DECL(b); 
00200     ShOutputAttrib1f SH_NAMEDECL(result, name) = dot(a, b); 
00201   } SH_END; 
00202   nibble.name("dot");
00203   return nibble;
00204 }
00205 
00206 template<typename T1, typename T2>
00207 ShProgram lerp(const std::string & name) { 
00208   ShProgram nibble = SH_BEGIN_PROGRAM() {
00209     typename T1::InputType SH_DECL(a); 
00210     typename T1::InputType SH_DECL(b); 
00211     typename T2::InputType SH_DECL(alpha); 
00212     typename T1::OutputType SH_NAMEDECL(result, name) = lerp(alpha, a, b); 
00213   } SH_END; 
00214   nibble.name("lerp");
00215   return nibble;
00216 }
00217 
00218 template<typename T1>
00219 ShProgram lerp(const std::string & name) { 
00220   return lerp<T1, T1>(name);
00221 }
00222 
00223 
00224 }
00225 
00226 #endif

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