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 "csgfx/shadervararrayhelper.h" 00029 #include "csutil/array.h" 00030 #include "csutil/dirtyaccessarray.h" 00031 #include "csutil/leakguard.h" 00032 #include "csutil/ref.h" 00033 #include "csutil/scf_implementation.h" 00034 #include "csutil/strhash.h" 00035 #include "imap/services.h" 00036 #include "iutil/document.h" 00037 #include "iutil/strset.h" 00038 #include "iutil/vfs.h" 00039 00040 #include "csplugincommon/shader/shaderplugin.h" 00041 00042 struct iDataBuffer; 00043 struct iObjectRegistry; 00044 00045 /* Hack to have the Jam dependency scanner pick up shaderprogram.tok. 00046 * Since the enums generated by this .tok are intended to be used by 00047 * descendants of csShaderProgram as well, this approach instead of the usual 00048 * "Includes" one was taken here since it also plays well with external 00049 * projects, and a dependency on the .tok file is picked up as well. */ 00050 #define CS_TOKEN_LIST_TOKEN(x) 00051 #include "csplugincommon/shader/shaderprogram.tok" 00052 #undef CS_TOKEN_LIST_TOKEN 00053 00061 class CS_CRYSTALSPACE_EXPORT csShaderProgram : 00062 public scfImplementation2<csShaderProgram, 00063 iShaderProgram, 00064 iShaderDestinationResolver> 00065 { 00066 protected: 00067 csStringHash commonTokens; 00068 #define CS_INIT_TOKEN_TABLE_NAME InitCommonTokens 00069 #define CS_TOKEN_ITEM_FILE \ 00070 "csplugincommon/shader/shaderprogram.tok" 00071 #include "cstool/tokenlist.h" 00072 #undef CS_TOKEN_ITEM_FILE 00073 #undef CS_INIT_TOKEN_TABLE_NAME 00074 00075 protected: 00076 iObjectRegistry* objectReg; 00077 csRef<iSyntaxService> synsrv; 00078 csRef<iShaderVarStringSet> stringsSvName; 00079 00080 public: 00084 enum ProgramParamType 00085 { 00086 ParamInvalid = 0, 00087 ParamInt = 0x0001, 00088 ParamFloat = 0x0002, 00089 ParamVector2 = 0x0004, 00090 ParamVector3 = 0x0008, 00091 ParamVector4 = 0x0010, 00092 ParamMatrix = 0x0020, 00093 ParamTransform = 0x0040, 00094 ParamArray = 0x0080, 00095 ParamShaderExp = 0x0100, 00096 00097 ParamVector = ParamInt | ParamFloat | ParamVector2 | ParamVector3 | ParamVector4 00098 }; 00099 00103 struct CS_CRYSTALSPACE_EXPORT ProgramParam 00104 { 00105 bool valid; 00106 00107 // Name of SV to use (if any) 00108 CS::ShaderVarStringID name; 00109 csDirtyAccessArray<size_t, csArrayElementHandler<size_t>, 00110 CS::Memory::LocalBufferAllocator<size_t, 2, 00111 CS::Memory::AllocatorMalloc, true> > indices; 00112 // Reference to const value shadervar 00113 csRef<csShaderVariable> var; 00114 00115 ProgramParam() : valid (false), name (CS::InvalidShaderVarStringID) { } 00117 bool IsConstant() const 00118 { return valid && var.IsValid() && (var->GetAccessor() == 0); } 00119 00121 00122 void SetValue (float val); 00123 void SetValue (const csVector4& val); 00125 }; 00126 00127 class CS_CRYSTALSPACE_EXPORT ProgramParamParser 00128 { 00129 iSyntaxService* synsrv; 00130 iShaderVarStringSet* stringsSvName; 00131 public: 00132 ProgramParamParser (iSyntaxService* synsrv, 00133 iShaderVarStringSet* stringsSvName) : synsrv (synsrv), 00134 stringsSvName (stringsSvName) {} 00135 00143 bool ParseProgramParam (iDocumentNode* node, 00144 ProgramParam& param, uint types = ~0); 00145 }; 00146 00147 protected: 00152 bool ParseProgramParam (iDocumentNode* node, 00153 ProgramParam& param, uint types = ~0) 00154 { 00155 ProgramParamParser parser (synsrv, stringsSvName); 00156 return parser.ParseProgramParam (node, param, types); 00157 } 00158 00162 struct VariableMapEntry : public csShaderVarMapping 00163 { 00164 ProgramParam mappingParam; 00165 intptr_t userVal; 00166 00167 VariableMapEntry (CS::ShaderVarStringID s, const char* d) : 00168 csShaderVarMapping (s, d) 00169 { 00170 userVal = 0; 00171 mappingParam.name = s; 00172 mappingParam.valid = true; 00173 } 00174 VariableMapEntry (const csShaderVarMapping& other) : 00175 csShaderVarMapping (other.name, other.destination) 00176 { 00177 userVal = 0; 00178 mappingParam.name = other.name; 00179 mappingParam.valid = true; 00180 } 00181 }; 00183 csSafeCopyArray<VariableMapEntry> variablemap; 00184 00185 void TryAddUsedShaderVarName (CS::ShaderVarStringID name, csBitArray& bits) const 00186 { 00187 if (name != CS::InvalidShaderVarStringID) 00188 { 00189 if (bits.GetSize() > name) bits.SetBit (name); 00190 } 00191 } 00192 void TryAddUsedShaderVarProgramParam (const ProgramParam& param, 00193 csBitArray& bits) const 00194 { 00195 if (param.valid) 00196 { 00197 TryAddUsedShaderVarName (param.name, bits); 00198 } 00199 } 00200 void GetUsedShaderVarsFromVariableMappings (csBitArray& bits) const; 00201 00203 csString description; 00204 00208 struct ProgramSource 00209 { 00211 csRef<iDocumentNode> programNode; 00213 csRef<iDataBuffer> programData; 00214 00216 csString programFileName; 00217 }; 00219 ProgramSource programSource; 00220 00225 bool doVerbose; 00226 00228 bool ParseCommon (iDocumentNode* child); 00230 bool ParseProgramNode (iDocumentNode* child, ProgramSource& parsedSource); 00232 00236 iDocumentNode* GetProgramNode (ProgramSource& programSource); 00237 iDocumentNode* GetProgramNode () 00238 { return GetProgramNode (programSource); } 00240 00241 00245 csPtr<iDataBuffer> GetProgramData (ProgramSource& programSource); 00246 csPtr<iDataBuffer> GetProgramData () 00247 { return GetProgramData (programSource); } 00249 00251 void DumpProgramInfo (csString& output); 00253 void DumpVariableMappings (csString& output); 00254 00258 inline csShaderVariable* GetParamSV (const csShaderVariableStack& stack, 00259 const ProgramParam ¶m) 00260 { 00261 csShaderVariable* var = 0; 00262 00263 var = csGetShaderVariableFromStack (stack, param.name); 00264 if (var) 00265 var = CS::Graphics::ShaderVarArrayHelper::GetArrayItem (var, 00266 param.indices.GetArray(), param.indices.GetSize(), 00267 CS::Graphics::ShaderVarArrayHelper::maFail); 00268 if (!var) 00269 var = param.var; 00270 00271 return var; 00272 } 00274 00278 inline bool GetParamVectorVal (const csShaderVariableStack& stack, 00279 const ProgramParam ¶m, csVector4* result) 00280 { 00281 csShaderVariable* var (GetParamSV (stack, param)); 00282 00283 // If var is null now we have no const nor any passed value, ignore it 00284 if (!var) 00285 return false; 00286 00287 var->GetValue (*result); 00288 return true; 00289 } 00290 inline csVector4 GetParamVectorVal (const csShaderVariableStack& stack, 00291 const ProgramParam ¶m, const csVector4& defVal) 00292 { 00293 csVector4 v; 00294 if (!GetParamVectorVal (stack, param, &v)) return defVal; 00295 return v; 00296 } 00297 00298 inline bool GetParamTransformVal (const csShaderVariableStack& stack, 00299 const ProgramParam ¶m, csReversibleTransform* result) 00300 { 00301 csShaderVariable* var (GetParamSV (stack, param)); 00302 00303 // If var is null now we have no const nor any passed value, ignore it 00304 if (!var) 00305 return false; 00306 00307 var->GetValue (*result); 00308 return true; 00309 } 00310 inline csReversibleTransform GetParamTransformVal (const csShaderVariableStack& stack, 00311 const ProgramParam ¶m, const csReversibleTransform& defVal) 00312 { 00313 csReversibleTransform t; 00314 if (!GetParamTransformVal (stack, param, &t)) return defVal; 00315 return t; 00316 } 00317 00318 inline bool GetParamFloatVal (const csShaderVariableStack& stack, 00319 const ProgramParam ¶m, float* result) 00320 { 00321 csShaderVariable* var (GetParamSV (stack, param)); 00322 00323 // If var is null now we have no const nor any passed value, ignore it 00324 if (!var) 00325 return false; 00326 00327 var->GetValue (*result); 00328 return true; 00329 } 00330 inline float GetParamFloatVal (const csShaderVariableStack& stack, 00331 const ProgramParam ¶m, float defVal) 00332 { 00333 float f; 00334 if (!GetParamFloatVal (stack, param, &f)) return defVal; 00335 return f; 00336 } 00338 public: 00339 CS_LEAKGUARD_DECLARE (csShaderProgram); 00340 00341 csShaderProgram (iObjectRegistry* objectReg); 00342 virtual ~csShaderProgram (); 00343 00344 virtual int ResolveTU (const char* /*binding*/) 00345 { return -1; } 00346 00347 virtual csVertexAttrib ResolveBufferDestination (const char* /*binding*/) 00348 { return CS_VATTRIB_INVALID; } 00349 00350 virtual void GetUsedShaderVars (csBitArray& bits) const; 00351 00352 /* Does not really load from cache, just pretends to - in reality, calls 00353 Load() */ 00354 virtual iShaderProgram::CacheLoadResult LoadFromCache ( 00355 iHierarchicalCache* cache, iBase* previous, iDocumentNode* programNode, 00356 csRef<iString>* failReason = 0, csRef<iString>* = 0); 00357 }; 00358 00361 #endif // __CS_SHADERPLUGINS_COMMON_SHADERPROGRAM_H__
Generated for Crystal Space 2.1 by doxygen 1.6.1
