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

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