csgfx/shadervar.h
00001 /* 00002 Copyright (C) 2003 by Mat Sutcliffe <oktal@gmx.co.uk> 00003 Marten Svanfeldt 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_GFX_SHADERVAR_H__ 00021 #define __CS_GFX_SHADERVAR_H__ 00022 00023 #include "csutil/refcount.h" 00024 #include "csutil/strhash.h" 00025 #include "iutil/string.h" 00026 #include "csgeom/vector2.h" 00027 #include "csgeom/vector3.h" 00028 #include "csgeom/vector4.h" 00029 #include "csgfx/rgbpixel.h" 00030 #include "iengine/texture.h" 00031 #include "ivideo/texture.h" 00032 #include "csutil/refarr.h" 00033 #include "ivideo/rndbuf.h" 00034 00035 struct iTextureHandle; 00036 struct iTextureWrapper; 00037 struct csShaderVariableWrapper; 00038 00039 class csShaderVariable; 00040 00041 SCF_VERSION (iShaderVariableAccessor, 0, 0, 1); 00047 struct iShaderVariableAccessor : public iBase 00048 { 00050 virtual void PreGetValue (csShaderVariable *variable) = 0; 00051 }; 00052 00053 00057 class csShaderVariable : public csRefCount 00058 { 00059 public: 00061 enum VariableType 00062 { 00063 INT = 1, 00064 FLOAT, 00065 COLOR, 00066 TEXTURE, 00067 RENDERBUFFER, 00068 VECTOR2, 00069 VECTOR3, 00070 VECTOR4 00071 }; 00072 00073 private: 00074 00075 VariableType Type; 00076 00077 int Int; 00078 csRef<iTextureHandle> TextureHandValue; 00079 csRef<iTextureWrapper> TextureWrapValue; 00080 csRef<iRenderBuffer> RenderBuffer; 00081 csVector4 VectorValue; 00082 00083 csRef<iShaderVariableAccessor> accessor; 00084 00085 public: 00086 csStringID Name; 00087 00089 csShaderVariable (csStringID name); 00090 00091 csShaderVariable& operator= (csShaderVariable& copyFrom); 00092 00094 VariableType GetType() const { return Type; } 00096 void SetType (VariableType t) { Type = t; } 00097 00099 void SetAccessor (iShaderVariableAccessor* a) { accessor = a;} 00100 00102 csStringID GetName() const { return Name; } 00103 00105 bool GetValue (int& value) 00106 { 00107 if (accessor) accessor->PreGetValue (this); 00108 value = Int; 00109 return true; 00110 } 00111 00113 bool GetValue (float& value) 00114 { 00115 if (accessor) accessor->PreGetValue (this); 00116 value = VectorValue.x; 00117 return true; 00118 } 00119 00121 bool GetValue (csRGBpixel& value) 00122 { 00123 if (accessor) accessor->PreGetValue (this); 00124 value.red = (char) VectorValue.x; 00125 value.green = (char) VectorValue.y; 00126 value.blue = (char) VectorValue.z; 00127 value.alpha = (char) VectorValue.w; 00128 return true; 00129 } 00130 00132 bool GetValue (iTextureHandle*& value) 00133 { 00134 if (accessor) accessor->PreGetValue (this); 00135 value = TextureHandValue; 00136 return true; 00137 } 00138 00140 bool GetValue (iTextureWrapper*& value) 00141 { 00142 if (accessor) accessor->PreGetValue (this); 00143 value = TextureWrapValue; 00144 return true; 00145 } 00146 00148 bool GetValue (iRenderBuffer*& value) 00149 { 00150 if (accessor) accessor->PreGetValue (this); 00151 value = RenderBuffer; 00152 return true; 00153 } 00154 00156 bool GetValue (csVector2& value) 00157 { 00158 if (accessor) accessor->PreGetValue (this); 00159 value.Set (VectorValue.x, VectorValue.y); 00160 return true; 00161 } 00162 00164 bool GetValue (csVector3& value) 00165 { 00166 if (accessor) accessor->PreGetValue (this); 00167 value.Set (VectorValue.x, VectorValue.y, VectorValue.z); 00168 return true; 00169 } 00170 00172 bool GetValue (csVector4& value) 00173 { 00174 if (accessor) accessor->PreGetValue (this); 00175 value = VectorValue; 00176 return true; 00177 } 00178 00179 00181 bool SetValue (int value) 00182 { 00183 Type = INT; 00184 Int = value; 00185 float f = (float)value; 00186 VectorValue.Set (f, f, f, f); 00187 return true; 00188 } 00189 00191 bool SetValue (float value) 00192 { 00193 Type = FLOAT; 00194 Int = (int)value; 00195 VectorValue.Set (value, value, value, value); 00196 return true; 00197 } 00198 00200 bool SetValue (const csRGBpixel &value) 00201 { 00202 Type = COLOR; 00203 VectorValue.x = (float) value.red; 00204 VectorValue.y = (float) value.green; 00205 VectorValue.z = (float) value.blue; 00206 VectorValue.w = (float) value.alpha; 00207 return true; 00208 } 00209 00211 bool SetValue (iTextureHandle* value) 00212 { 00213 Type = TEXTURE; 00214 TextureHandValue = value; 00215 return true; 00216 } 00217 00219 bool SetValue (iTextureWrapper* value) 00220 { 00221 Type = TEXTURE; 00222 TextureWrapValue = value; 00223 if (value) 00224 TextureHandValue = value->GetTextureHandle (); 00225 return true; 00226 } 00227 00229 bool SetValue (iRenderBuffer* value) 00230 { 00231 Type = RENDERBUFFER; 00232 RenderBuffer = value; 00233 return true; 00234 } 00235 00237 bool SetValue (const csVector2 &value) 00238 { 00239 Type = VECTOR2; 00240 VectorValue.Set (value.x, value.y, 1.0f, 1.0f); 00241 Int = (int)value.x; 00242 return true; 00243 } 00244 00246 bool SetValue (const csVector3 &value) 00247 { 00248 Type = VECTOR3; 00249 VectorValue.Set (value.x, value.y, value.z, 1.0f); 00250 Int = (int)value.x; 00251 return true; 00252 } 00253 00255 bool SetValue (const csVector4 &value) 00256 { 00257 Type = VECTOR4; 00258 VectorValue.Set (value.x, value.y, value.z, value.w); 00259 Int = (int)value.x; 00260 return true; 00261 } 00262 00263 }; 00264 00265 struct csShaderVariableProxy 00266 { 00267 public: 00268 csShaderVariableProxy () : 00269 Name (csInvalidStringID), userData (0), shaderVariable(0), realLocation(0) 00270 {} 00271 00272 csShaderVariableProxy (csStringID name, void* ud, 00273 csRef<csShaderVariable>* realplace = 0) : 00274 Name (name), userData(ud), shaderVariable(0), realLocation(realplace) 00275 {} 00276 00277 csStringID Name; 00278 void* userData; 00279 csShaderVariable *shaderVariable; 00280 csRef<csShaderVariable>* realLocation; 00281 }; 00282 00283 00284 static int ShaderVariableCompare (csShaderVariable* const &item1, 00285 csShaderVariable* const &item2) 00286 { 00287 if (item1->Name < item2->Name) return -1; 00288 else if (item1->Name > item2->Name) return 1; 00289 else return 0; 00290 } 00291 00292 static int ShaderVariableKeyCompare (csShaderVariable* const& item1, void* item2) 00293 { 00294 csStringID key = (csStringID)item2; 00295 if (item1->Name < key) return -1; 00296 else if (item1->Name > key) return 1; 00297 else return 0; 00298 } 00299 00300 00304 class csShaderVariableProxyList : 00305 public csArray<csShaderVariableProxy> 00306 { 00307 public: 00308 int InsertSorted (csShaderVariableProxy item); 00309 int Push (csShaderVariableProxy item); 00310 void PrepareFill (); 00311 }; 00312 00313 class csShaderVariableContextHelper 00314 { 00315 public: 00316 csShaderVariableContextHelper () 00317 {} 00318 00319 ~csShaderVariableContextHelper() 00320 { 00321 } 00322 00324 inline void AddVariable (csShaderVariable *variable) 00325 { 00326 csShaderVariable* var = GetVariable(variable->Name); 00327 if (var == 0) 00328 variables.InsertSorted (variable, ShaderVariableCompare); 00329 else 00330 *var = *variable; 00331 } 00332 00334 inline csShaderVariable* GetVariable (csStringID name) const 00335 { 00336 int idx = variables.FindSortedKey ((void*)name, ShaderVariableKeyCompare); 00337 if (idx >= 0) return variables.Get (idx); 00338 else return 0; 00339 } 00340 00345 inline unsigned int FillVariableList (csShaderVariableProxyList *list) const 00346 { 00347 unsigned int count = 0; 00348 if (list->Length ()== 0 || variables.Length() == 0) return 0; 00349 00350 csRefArray<csShaderVariable>::Iterator varIter (variables.GetIterator ()); 00351 csShaderVariableProxyList::Iterator inputIter (list->GetIterator ()); 00352 csShaderVariable* curVar=0; 00353 curVar=varIter.Next (); 00354 00355 while (inputIter.HasNext()) 00356 { 00357 csShaderVariableProxy *curInput = (csShaderVariableProxy*)&inputIter.Next(); 00358 while (varIter.HasNext () && curVar->Name < curInput->Name) 00359 { 00360 curVar=varIter.Next (); 00361 } 00362 if (curVar->Name == curInput->Name && curInput->shaderVariable == 0) 00363 { 00364 curInput->shaderVariable = curVar; 00365 if (curInput->realLocation!=0) (*curInput->realLocation) = curVar; 00366 count++; 00367 } 00368 else if (curVar->Name > curInput->Name) 00369 continue; 00370 else if (varIter.HasNext ()) 00371 continue; //still may have more 00372 else if (curVar->Name > curInput->Name) 00373 continue; 00374 else 00375 return count; 00376 } 00377 return count; 00378 } 00379 00380 private: 00382 csRefArray<csShaderVariable> variables; 00383 }; 00384 00385 00386 #endif
Generated for Crystal Space by doxygen 1.2.18