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 "imap/services.h" 00034 #include "iutil/document.h" 00035 #include "iutil/strset.h" 00036 #include "iutil/vfs.h" 00037 00038 #include "csplugincommon/shader/shaderplugin.h" 00039 00040 struct iDataBuffer; 00041 struct iObjectRegistry; 00042 00043 /* Hack to have the Jam dependency scanner pick up shaderprogram.tok. 00044 * Since the enums generated by this .tok are intended to be used by 00045 * descendants of csShaderProgram as well, this approach instead of the usual 00046 * "Includes" one was taken here since it also plays well with external 00047 * projects, and a dependency on the .tok file is picked up as well. */ 00048 #define CS_TOKEN_LIST_TOKEN(x) 00049 #include "csplugincommon/shader/shaderprogram.tok" 00050 #undef CS_TOKEN_LIST_TOKEN 00051 00059 class CS_CRYSTALSPACE_EXPORT csShaderProgram : 00060 public scfImplementation2<csShaderProgram, 00061 iShaderProgram, 00062 iShaderDestinationResolver> 00063 { 00064 protected: 00065 csStringHash commonTokens; 00066 #define CS_INIT_TOKEN_TABLE_NAME InitCommonTokens 00067 #define CS_TOKEN_ITEM_FILE \ 00068 "csplugincommon/shader/shaderprogram.tok" 00069 #include "cstool/tokenlist.h" 00070 #undef CS_TOKEN_ITEM_FILE 00071 #undef CS_INIT_TOKEN_TABLE_NAME 00072 00073 protected: 00074 iObjectRegistry* objectReg; 00075 csRef<iSyntaxService> synsrv; 00076 csRef<iStringSet> strings; 00077 00078 public: 00082 enum ProgramParamType 00083 { 00084 ParamInvalid = 0, 00085 ParamFloat = 0x0001, 00086 ParamVector2 = 0x0002, 00087 ParamVector3 = 0x0004, 00088 ParamVector4 = 0x0008, 00089 ParamMatrix = 0x0010, 00090 ParamTransform = 0x0020, 00091 ParamArray = 0x0040, 00092 ParamShaderExp = 0x0080, 00093 00094 ParamVector = ParamFloat | ParamVector2 | ParamVector3 | ParamVector4 00095 }; 00096 00100 struct ProgramParam 00101 { 00102 bool valid; 00103 00104 // Name of SV to use (if any) 00105 csStringID name; 00106 // Reference to const value shadervar 00107 csRef<csShaderVariable> var; 00108 00109 ProgramParam() : valid (false), name(csInvalidStringID) { } 00111 bool IsConstant() const { return valid && var.IsValid(); } 00112 }; 00113 00114 class CS_CRYSTALSPACE_EXPORT ProgramParamParser 00115 { 00116 iSyntaxService* synsrv; 00117 iStringSet* stringsSvName; 00118 public: 00119 ProgramParamParser (iSyntaxService* synsrv, iStringSet* stringsSvName) : 00120 synsrv (synsrv), stringsSvName (stringsSvName) {} 00121 00129 bool ParseProgramParam (iDocumentNode* node, 00130 ProgramParam& param, uint types = ~0); 00131 }; 00132 00133 protected: 00138 bool ParseProgramParam (iDocumentNode* node, 00139 ProgramParam& param, uint types = ~0) 00140 { 00141 ProgramParamParser parser (synsrv, strings); 00142 return parser.ParseProgramParam (node, param, types); 00143 } 00144 00148 struct VariableMapEntry : public csShaderVarMapping 00149 { 00150 ProgramParam mappingParam; 00151 intptr_t userVal; 00152 00153 VariableMapEntry (csStringID s, const char* d) : 00154 csShaderVarMapping (s, d) 00155 { 00156 userVal = 0; 00157 mappingParam.name = s; 00158 mappingParam.valid = true; 00159 } 00160 VariableMapEntry (const csShaderVarMapping& other) : 00161 csShaderVarMapping (other.name, other.destination) 00162 { 00163 userVal = 0; 00164 mappingParam.name = other.name; 00165 mappingParam.valid = true; 00166 } 00167 }; 00169 csArray<VariableMapEntry> variablemap; 00170 00172 csString description; 00173 00175 csRef<iDocumentNode> programNode; 00177 csRef<iFile> programFile; 00178 00180 csString programFileName; 00181 00186 bool doVerbose; 00187 00189 bool ParseCommon (iDocumentNode* child); 00191 iDocumentNode* GetProgramNode (); 00193 csPtr<iDataBuffer> GetProgramData (); 00194 00196 void DumpProgramInfo (csString& output); 00198 void DumpVariableMappings (csString& output); 00199 00201 00205 inline csVector4 GetParamVectorVal (const iShaderVarStack* stacks, 00206 const ProgramParam ¶m, const csVector4& defVal) 00207 { 00208 csRef<csShaderVariable> var; 00209 00210 var = csGetShaderVariableFromStack (stacks, param.name); 00211 if (!var.IsValid ()) 00212 var = param.var; 00213 00214 // If var is null now we have no const nor any passed value, ignore it 00215 if (!var.IsValid ()) 00216 return defVal; 00217 00218 csVector4 v; 00219 var->GetValue (v); 00220 return v; 00221 } 00222 inline csReversibleTransform GetParamTransformVal (const iShaderVarStack* stacks, 00223 const ProgramParam ¶m, const csReversibleTransform& defVal) 00224 { 00225 csRef<csShaderVariable> var; 00226 00227 var = csGetShaderVariableFromStack (stacks, param.name); 00228 if (!var.IsValid ()) 00229 var = param.var; 00230 00231 // If var is null now we have no const nor any passed value, ignore it 00232 if (!var.IsValid ()) 00233 return defVal; 00234 00235 csReversibleTransform t; 00236 var->GetValue (t); 00237 return t; 00238 } 00239 inline float GetParamFloatVal (const iShaderVarStack* stacks, 00240 const ProgramParam ¶m, float defVal) 00241 { 00242 csRef<csShaderVariable> var; 00243 00244 var = csGetShaderVariableFromStack (stacks, param.name); 00245 if (!var.IsValid ()) 00246 var = param.var; 00247 00248 // If var is null now we have no const nor any passed value, ignore it 00249 if (!var.IsValid ()) 00250 return defVal; 00251 00252 float f; 00253 var->GetValue (f); 00254 return f; 00255 } 00257 public: 00258 CS_LEAKGUARD_DECLARE (csShaderProgram); 00259 00260 csShaderProgram (iObjectRegistry* objectReg); 00261 virtual ~csShaderProgram (); 00262 00263 virtual int ResolveTU (const char* /*binding*/) 00264 { return -1; } 00265 00266 virtual csVertexAttrib ResolveBufferDestination (const char* /*binding*/) 00267 { return CS_VATTRIB_INVALID; } 00268 }; 00269 00272 #endif // __CS_SHADERPLUGINS_COMMON_SHADERPROGRAM_H__
Generated for Crystal Space 1.4.0 by doxygen 1.5.8