CrystalSpace

Public API Reference

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 
00076   struct oper 
00077   {
00078     uint8 opcode, acc;
00079     oper_arg arg1, arg2;
00080   };
00081 
00082   typedef csArray<oper> oper_array;
00083   typedef csArray<oper_arg> arg_array;
00084 
00085 private:
00086   iObjectRegistry* obj_reg;
00088   csShaderVariableStack* stack;
00090   csRef<iShaderVarStringSet> strset;
00092   csMemoryPool svIndicesScratch;
00094   oper_array opcodes;
00099   int accstack_max;
00105   arg_array accstack;
00106 
00108   bool parse_xml (cons*, iDocumentNode*);
00110   bool parse_xml_atom (oper_arg&, csStringID, const char*, const char*);
00112   bool parse_sexp (cons*, iDocumentNode*);
00115   bool parse_sexp_form (const char*& text, cons*);
00117   bool parse_sexp_atom (const char*& text, cons*);
00119   bool parse_num_atom (const char*& text, oper_arg&);
00120 
00122   bool compile_cons (const cons*, int& acc_top);
00124   bool compile_make_vector (const cons*, int& acc_top, int acc);
00126   bool compile_if (const cons*, int& acc_top, int acc);
00127 
00129   bool eval_const (cons*&);
00130 
00132   bool eval_oper (int oper, oper_arg arg1, oper_arg arg2, oper_arg& output);
00134   bool eval_oper (int oper, oper_arg arg1, oper_arg& output);
00136   bool eval_oper (int oper, oper_arg& output);
00137 
00139   bool eval_add (const oper_arg& arg1, const oper_arg& arg2,
00140         oper_arg& output) const;
00142   bool eval_sub (const oper_arg& arg1, const oper_arg& arg2,
00143         oper_arg& output) const;
00145   bool eval_mul (const oper_arg& arg1, const oper_arg& arg2,
00146         oper_arg& output) const;
00148   bool eval_div (const oper_arg& arg1, const oper_arg& arg2,
00149         oper_arg& output) const;
00150   
00152   bool eval_elt1 (const oper_arg& arg1, oper_arg& output) const;
00154   bool eval_elt2 (const oper_arg& arg1, oper_arg& output) const;
00156   bool eval_elt3 (const oper_arg& arg1, oper_arg& output) const;
00158   bool eval_elt4 (const oper_arg& arg1, oper_arg& output) const;
00159 
00161   bool eval_sin (const oper_arg& arg1, oper_arg& output) const;
00163   bool eval_cos (const oper_arg& arg1, oper_arg& output) const;
00165   bool eval_tan (const oper_arg& arg1, oper_arg& output) const;
00166 
00167   bool eval_arcsin (const oper_arg& arg1, oper_arg& output) const;
00168   bool eval_arccos (const oper_arg& arg1, oper_arg& output) const;
00169   bool eval_arctan (const oper_arg& arg1, oper_arg& output) const;
00170 
00171   bool eval_dot (const oper_arg& arg1, const oper_arg& arg2,
00172         oper_arg& output) const;
00173   bool eval_cross (const oper_arg& arg1, const oper_arg& arg2,
00174         oper_arg& output) const;
00175   bool eval_vec_len (const oper_arg& arg1, oper_arg& output) const;
00176   bool eval_normal (const oper_arg& arg1, oper_arg& output) const;
00178   bool eval_floor (const oper_arg& arg1, oper_arg& output) const;
00179 
00180   bool eval_pow (const oper_arg& arg1, const oper_arg& arg2,
00181         oper_arg& output) const;
00182   bool eval_min (const oper_arg& arg1, const oper_arg& arg2,
00183         oper_arg& output) const;
00184   bool eval_max (const oper_arg& arg1, const oper_arg& arg2,
00185         oper_arg& output) const;
00186 
00188   bool eval_time (oper_arg& output) const;
00190   bool eval_frame (oper_arg& output) const;
00191 
00192   template<typename Comparator>
00193   bool eval_compare (const Comparator& cmp, const oper_arg& arg1,
00194     const oper_arg& arg2, oper_arg& output) const;
00195         
00196   bool eval_matrix_column (const oper_arg& arg1, const oper_arg& arg2,
00197         oper_arg& output) const;
00198   bool eval_matrix_row (const oper_arg& arg1, const oper_arg& arg2,
00199         oper_arg& output) const;
00200   bool eval_matrix2gl (const oper_arg& arg1, oper_arg& output) const;
00201   bool eval_matrix_transp (const oper_arg& arg1, oper_arg& output) const;
00202   bool eval_matrix_inv (const oper_arg& arg1, oper_arg& output) const;
00203         
00205   bool eval_selt12 (const oper_arg& arg1, const oper_arg& arg2,
00206         oper_arg & output) const;
00208   bool eval_selt34 (const oper_arg & arg1, const oper_arg & arg2,
00209         oper_arg& output) const;
00211   bool eval_load (const oper_arg& arg1, oper_arg& output) const;
00213   bool eval_select (const oper_arg& arg1, const oper_arg& arg2,
00214     oper_arg& output) const;
00215 
00217   bool eval_variable (csShaderVariable*, oper_arg& out);
00219   bool eval_argument (const oper_arg&, csShaderVariable*);
00220 
00222   void destruct_cons (cons*) const;
00224   void print_cons (const cons*) const;
00226   void print_ops (const oper_array&) const;
00228   void print_result (const oper_arg&) const;
00229 
00230   /*inline*/static const char* GetTypeName (unsigned int id)/* const*/;
00231   /*{
00232     return xmltypes.Request(id);
00233   }*/
00234   static const char* GetOperName (unsigned int id);
00235   static csStringID GetCommonTokenOp (const char* token);
00236   static csStringID GetXmlTokenOp (const char* token);
00237   static csStringID GetSexpTokenOp (const char* token);
00238   static csStringID GetXmlType (const char* token);
00239 
00241   size_t* AllocSVIndices (const CS::Graphics::ShaderVarNameParser& parser);
00242   csShaderVariable* ResolveVar (const oper_arg::SvVarValue& var);
00243 
00244   mutable csString errorMsg;
00245   void ParseError (const char* message, ...) const;
00246   void EvalError (const char* message, ...) const;
00247 public:
00248   csShaderExpression (iObjectRegistry*);
00249   ~csShaderExpression ();
00250 
00252   bool Parse (iDocumentNode*);
00254 
00258   bool Evaluate (csShaderVariable*, csShaderVariableStack& stacks);
00260 
00262   const char* GetError () const { return errorMsg; }
00263 };
00264 
00265 #endif

Generated for Crystal Space 2.0 by doxygen 1.7.6.1