csgfx/shaderexp.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2003 by Andrew Topp <designa@users.sourceforge.net> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_GFX_SHADEREXP_H__ 00020 #define __CS_GFX_SHADEREXP_H__ 00021 00026 #include "csextern.h" 00027 00028 #include "csutil/strhash.h" 00029 #include "csutil/array.h" 00030 #include "csutil/leakguard.h" 00031 #include "csgeom/vector4.h" 00032 #include "csgfx/shadervarnameparser.h" 00033 #include "ivideo/shader/shader.h" 00034 00035 struct iObjectRegistry; 00036 class csShaderVariable; 00037 struct iShaderVariableContext; 00038 struct iStringSet; 00039 struct iDocumentNode; 00040 struct cons; 00041 00045 class CS_CRYSTALSPACE_EXPORT csShaderExpression 00046 { 00047 public: 00048 CS_LEAKGUARD_DECLARE (csShaderExpression); 00049 00050 struct oper_arg 00051 { 00052 uint8 type; 00053 00054 struct SvVarValue 00055 { 00056 CS::StringIDValue id; 00057 size_t* indices; 00058 }; 00059 union 00060 { 00061 float num; 00062 SvVarValue var; 00063 00064 // Illegal outside of a cons cell 00065 int oper; 00066 cons* cell; 00067 00068 // Special internal values 00069 int acc; 00070 }; 00071 00072 csVector4 vec4; 00073 CS::Math::Matrix4 matrix; 00074 00075 uint GetHash() const; 00076 }; 00077 00078 struct oper 00079 { 00080 uint8 opcode, acc; 00081 oper_arg arg1, arg2, arg3; 00082 00083 uint GetHash() const; 00084 }; 00085 00086 typedef csArray<oper> oper_array; 00087 typedef csArray<oper_arg> arg_array; 00088 00089 private: 00090 iObjectRegistry* obj_reg; 00092 csShaderVariableStack* stack; 00094 csRef<iShaderVarStringSet> strset; 00096 csMemoryPool svIndicesScratch; 00098 oper_array opcodes; 00103 int accstack_max; 00109 arg_array accstack; 00110 00112 bool parse_xml (cons*, iDocumentNode*); 00114 bool parse_xml_atom (oper_arg&, csStringID, const char*, const char*); 00116 bool parse_sexp (cons*, iDocumentNode*); 00119 bool parse_sexp_form (const char*& text, cons*); 00121 bool parse_sexp_atom (const char*& text, cons*); 00123 bool parse_num_atom (const char*& text, oper_arg&); 00124 00126 bool compile_cons (const cons*, int& acc_top); 00128 bool compile_make_vector (const cons*, int& acc_top, int acc); 00130 bool compile_if (const cons*, int& acc_top, int acc); 00131 00134 00135 bool eval_const (cons*&); 00140 bool optimize_cse (oper_array&); 00143 00144 bool resolve_arg (oper_arg& arg); 00146 bool eval_oper (int oper, oper_arg arg1, oper_arg arg2, oper_arg arg3, oper_arg& output); 00148 bool eval_oper (int oper, oper_arg arg1, oper_arg arg2, oper_arg& output); 00150 bool eval_oper (int oper, oper_arg arg1, oper_arg& output); 00152 bool eval_oper (int oper, oper_arg& output); 00153 00155 bool eval_add (const oper_arg& arg1, const oper_arg& arg2, 00156 oper_arg& output) const; 00158 bool eval_sub (const oper_arg& arg1, const oper_arg& arg2, 00159 oper_arg& output) const; 00161 bool eval_mul (const oper_arg& arg1, const oper_arg& arg2, 00162 oper_arg& output) const; 00164 bool eval_div (const oper_arg& arg1, const oper_arg& arg2, 00165 oper_arg& output) const; 00166 00168 bool eval_elt1 (const oper_arg& arg1, oper_arg& output) const; 00170 bool eval_elt2 (const oper_arg& arg1, oper_arg& output) const; 00172 bool eval_elt3 (const oper_arg& arg1, oper_arg& output) const; 00174 bool eval_elt4 (const oper_arg& arg1, oper_arg& output) const; 00175 00177 bool eval_sin (const oper_arg& arg1, oper_arg& output) const; 00179 bool eval_cos (const oper_arg& arg1, oper_arg& output) const; 00181 bool eval_tan (const oper_arg& arg1, oper_arg& output) const; 00182 00183 bool eval_arcsin (const oper_arg& arg1, oper_arg& output) const; 00184 bool eval_arccos (const oper_arg& arg1, oper_arg& output) const; 00185 bool eval_arctan (const oper_arg& arg1, oper_arg& output) const; 00186 00187 bool eval_dot (const oper_arg& arg1, const oper_arg& arg2, 00188 oper_arg& output) const; 00189 bool eval_cross (const oper_arg& arg1, const oper_arg& arg2, 00190 oper_arg& output) const; 00191 bool eval_vec_len (const oper_arg& arg1, oper_arg& output) const; 00192 bool eval_normal (const oper_arg& arg1, oper_arg& output) const; 00194 bool eval_floor (const oper_arg& arg1, oper_arg& output) const; 00195 00196 bool eval_pow (const oper_arg& arg1, const oper_arg& arg2, 00197 oper_arg& output) const; 00198 bool eval_min (const oper_arg& arg1, const oper_arg& arg2, 00199 oper_arg& output) const; 00200 bool eval_max (const oper_arg& arg1, const oper_arg& arg2, 00201 oper_arg& output) const; 00202 00204 bool eval_time (oper_arg& output) const; 00206 bool eval_frame (oper_arg& output) const; 00207 00208 template<typename Comparator> 00209 bool eval_compare (const Comparator& cmp, const oper_arg& arg1, 00210 const oper_arg& arg2, oper_arg& output) const; 00211 00213 bool eval_and (const oper_arg& arg1, const oper_arg& arg2, 00214 oper_arg& output) const; 00216 bool eval_or (const oper_arg& arg1, const oper_arg& arg2, 00217 oper_arg& output) const; 00219 bool eval_not (const oper_arg& arg, oper_arg& output) const; 00220 00221 bool eval_matrix_column (const oper_arg& arg1, const oper_arg& arg2, 00222 oper_arg& output) const; 00223 bool eval_matrix_row (const oper_arg& arg1, const oper_arg& arg2, 00224 oper_arg& output) const; 00225 bool eval_matrix2gl (const oper_arg& arg1, oper_arg& output) const; 00226 bool eval_matrix_transp (const oper_arg& arg1, oper_arg& output) const; 00227 bool eval_matrix_inv (const oper_arg& arg1, oper_arg& output) const; 00228 00230 bool eval_selt12 (const oper_arg& arg1, const oper_arg& arg2, 00231 oper_arg & output) const; 00233 bool eval_selt3 (const oper_arg & arg1, const oper_arg & arg2, 00234 oper_arg& output) const; 00236 bool eval_selt34 (const oper_arg & arg1, const oper_arg & arg2, 00237 const oper_arg & arg3, oper_arg& output) const; 00239 bool eval_load (const oper_arg& arg1, oper_arg& output) const; 00241 bool eval_select (const oper_arg& arg1, const oper_arg& arg2, 00242 const oper_arg& arg3, oper_arg& output) const; 00243 00245 bool eval_variable (csShaderVariable*, oper_arg& out); 00247 bool eval_argument (const oper_arg&, csShaderVariable*); 00248 00250 void destruct_cons (cons*) const; 00252 void print_cons (const cons*) const; 00254 void print_ops (const oper_array&) const; 00256 const char* oper_arg_str (const oper_arg&) const; 00258 void print_result (const oper_arg&) const; 00259 00260 /*inline*/static const char* GetTypeName (unsigned int id)/* const*/; 00261 /*{ 00262 return xmltypes.Request(id); 00263 }*/ 00264 static const char* GetOperName (unsigned int id); 00265 static csStringID GetCommonTokenOp (const char* token); 00266 static csStringID GetXmlTokenOp (const char* token); 00267 static csStringID GetSexpTokenOp (const char* token); 00268 static csStringID GetXmlType (const char* token); 00269 00271 size_t* AllocSVIndices (const CS::Graphics::ShaderVarNameParser& parser); 00272 csShaderVariable* ResolveVar (const oper_arg::SvVarValue& var); 00273 00274 mutable csString errorMsg; 00275 void ParseError (const char* message, ...) const; 00276 void EvalError (const char* message, ...) const; 00277 00278 mutable csString* tmpOperArgStr; 00279 public: 00280 csShaderExpression (iObjectRegistry*); 00281 ~csShaderExpression (); 00282 00284 bool Parse (iDocumentNode*); 00286 00290 bool Evaluate (csShaderVariable*, csShaderVariableStack& stacks); 00292 00294 const char* GetError () const { return errorMsg; } 00295 }; 00296 00297 #endif
Generated for Crystal Space 2.1 by doxygen 1.6.1
