## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csplugincommon/shader/shaderprogram.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2004 by Jorrit Tyberghein
    00003               (C) 2004 by Frank Richter
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 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     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CS_SHADERPLUGINS_COMMON_SHADERPROGRAM_H__
    00021 #define __CS_SHADERPLUGINS_COMMON_SHADERPROGRAM_H__
    00022
    00027 #include "csextern.h"
    00028 #include "csutil/array.h"
    00029 #include "csutil/leakguard.h"
    00030 #include "csutil/ref.h"
    00031 #include "csutil/scf_implementation.h"
    00032 #include "csutil/strhash.h"
    00033 #include "iutil/strset.h"
    00034
    00035 #include "csplugincommon/shader/shaderplugin.h"
    00036
    00037 struct iDataBuffer;
    00038 struct iFile;
    00039 struct iSyntaxService;
    00040 struct iObjectRegistry;
    00041
    00042 /* Hack to have the Jam dependency scanner pick up shaderprogram.tok.
    00043  * Since the enums generated by this .tok are intended to be used by
    00044  * descendants of csShaderProgram as well, this approach instead of the usual
    00045  * "Includes" one was taken here since it also plays well with external
    00046  * projects, and a dependency on the .tok file is picked up as well. */
    00047 #define CS_TOKEN_LIST_TOKEN(x)
    00048 #include "csplugincommon/shader/shaderprogram.tok"
    00049 #undef CS_TOKEN_LIST_TOKEN
    00050
    00058 class CS_CRYSTALSPACE_EXPORT csShaderProgram :
    00059   public scfImplementation2<csShaderProgram,
    00060                             iShaderProgram,
    00061                             iShaderDestinationResolver>
    00062 {
    00063 protected:
    00064   csStringHash commonTokens;
    00065 #define CS_INIT_TOKEN_TABLE_NAME InitCommonTokens
    00066 #define CS_TOKEN_ITEM_FILE \
    00067   "csplugincommon/shader/shaderprogram.tok"
    00068 #include "cstool/tokenlist.h"
    00069 #undef CS_TOKEN_ITEM_FILE
    00070 #undef CS_INIT_TOKEN_TABLE_NAME
    00071
    00072 protected:
    00073   iObjectRegistry* objectReg;
    00074   csRef<iSyntaxService> synsrv;
    00075   csRef<iStringSet> strings;
    00076
    00080   enum ProgramParamType
    00081   {
    00082     ParamInvalid    = 0,
    00083     ParamFloat      = 0x0001,
    00084     ParamVector2    = 0x0002,
    00085     ParamVector3    = 0x0004,
    00086     ParamVector4    = 0x0008,
    00087     ParamMatrix     = 0x0010,
    00088     ParamTransform  = 0x0020,
    00089     ParamArray      = 0x0040,
    00090     ParamShaderExp  = 0x0080,
    00091
    00092     ParamVector     = ParamFloat | ParamVector2 | ParamVector3 | ParamVector4
    00093   };
    00094
    00098   struct ProgramParam
    00099   {
    00100     bool valid;
    00101
    00102     // Name of SV to use (if any)
    00103     csStringID name;
    00104     // Reference to const value shadervar
    00105     csRef<csShaderVariable> var;
    00106
    00107     ProgramParam() : valid (false), name(csInvalidStringID) { }
    00108   };
    00109
    00111   bool ParseProgramParam (iDocumentNode* node,
    00112     ProgramParam& param, uint types = ~0);
    00113
    00117   struct VariableMapEntry : public csShaderVarMapping
    00118   {
    00119     ProgramParam mappingParam;
    00120     intptr_t userVal;
    00121
    00122     VariableMapEntry (csStringID s, const char* d) :
    00123       csShaderVarMapping (s, d)
    00124     {
    00125       userVal = 0;
    00126       mappingParam.name = s;
    00127       mappingParam.valid = true;
    00128     }
    00129     VariableMapEntry (const csShaderVarMapping& other) :
    00130       csShaderVarMapping (other.name, other.destination)
    00131     {
    00132       userVal = 0;
    00133       mappingParam.name = other.name;
    00134       mappingParam.valid = true;
    00135     }
    00136   };
    00138   csArray<VariableMapEntry> variablemap;
    00139
    00141   csString description;
    00142
    00144   csRef<iDocumentNode> programNode;
    00146   csRef<iFile> programFile;
    00147
    00149   csString programFileName;
    00150
    00155   bool doVerbose;
    00156
    00158   bool ParseCommon (iDocumentNode* child);
    00160   iDocumentNode* GetProgramNode ();
    00162   csPtr<iDataBuffer> GetProgramData ();
    00163
    00165   void DumpProgramInfo (csString& output);
    00167   void DumpVariableMappings (csString& output);
    00168
    00170
    00174   inline csVector4 GetParamVectorVal (const csShaderVarStack &stacks,
    00175     const ProgramParam &param, const csVector4& defVal)
    00176   {
    00177     csRef<csShaderVariable> var;
    00178
    00179     var = csGetShaderVariableFromStack (stacks, param.name);
    00180     if (!var.IsValid ())
    00181       var = param.var;
    00182
    00183     // If var is null now we have no const nor any passed value, ignore it
    00184     if (!var.IsValid ())
    00185       return defVal;
    00186
    00187     csVector4 v;
    00188     var->GetValue (v);
    00189     return v;
    00190   }
    00191   inline csReversibleTransform GetParamTransformVal (const csShaderVarStack &stacks,
    00192     const ProgramParam &param, const csReversibleTransform& defVal)
    00193   {
    00194     csRef<csShaderVariable> var;
    00195
    00196     var = csGetShaderVariableFromStack (stacks, param.name);
    00197     if (!var.IsValid ())
    00198       var = param.var;
    00199
    00200     // If var is null now we have no const nor any passed value, ignore it
    00201     if (!var.IsValid ())
    00202       return defVal;
    00203
    00204     csReversibleTransform t;
    00205     var->GetValue (t);
    00206     return t;
    00207   }
    00208   inline float GetParamFloatVal (const csShaderVarStack &stacks,
    00209     const ProgramParam &param, float defVal)
    00210   {
    00211     csRef<csShaderVariable> var;
    00212
    00213     var = csGetShaderVariableFromStack (stacks, param.name);
    00214     if (!var.IsValid ())
    00215       var = param.var;
    00216
    00217     // If var is null now we have no const nor any passed value, ignore it
    00218     if (!var.IsValid ())
    00219       return defVal;
    00220
    00221     float f;
    00222     var->GetValue (f);
    00223     return f;
    00224   }
    00226 public:
    00227   CS_LEAKGUARD_DECLARE (csShaderProgram);
    00228
    00229   csShaderProgram (iObjectRegistry* objectReg);
    00230   virtual ~csShaderProgram ();
    00231
    00232   virtual int ResolveTU (const char* /*binding*/)
    00233   { return -1; }
    00234
    00235   virtual csVertexAttrib ResolveBufferDestination (const char* /*binding*/)
    00236   { return CS_VATTRIB_INVALID; }
    00237 };
    00238
    00241 #endif // __CS_SHADERPLUGINS_COMMON_SHADERPROGRAM_H__