csgfx/shadervar.h
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2003 by Mat Sutcliffe <oktal@gmx.co.uk> 00003 2003-2008 by 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 00027 #include "csextern.h" 00028 00029 #include "csgeom/math.h" 00030 #include "csgeom/quaternion.h" 00031 #include "csgeom/transfrm.h" 00032 #include "csgeom/vector2.h" 00033 #include "csgeom/vector3.h" 00034 #include "csgeom/vector4.h" 00035 #include "csgfx/rgbpixel.h" 00036 #include "csutil/cscolor.h" 00037 #include "csutil/leakguard.h" 00038 #include "csutil/refarr.h" 00039 #include "csutil/refcount.h" 00040 #include "csutil/strset.h" 00041 00042 #include "iengine/texture.h" 00043 #include "ivideo/texture.h" 00044 #include "ivideo/rndbuf.h" 00045 00046 struct iTextureHandle; 00047 struct iTextureWrapper; 00048 struct csShaderVariableWrapper; 00049 00050 class csShaderVariable; 00051 00060 struct iShaderVariableAccessor : public virtual iBase 00061 { 00062 SCF_INTERFACE (iShaderVariableAccessor, 2, 0, 0); 00063 00065 virtual void PreGetValue (csShaderVariable *variable) = 0; 00066 }; 00067 00073 class CS_CRYSTALSPACE_EXPORT csShaderVariable : public csRefCount 00074 { 00075 public: 00081 enum VariableType 00082 { 00084 UNKNOWN = 0, 00086 INT = 1, 00088 FLOAT, 00090 TEXTURE, 00092 RENDERBUFFER, 00094 VECTOR2, 00096 VECTOR3, 00098 VECTOR4, 00100 MATRIX, 00102 TRANSFORM, 00104 ARRAY, 00105 00110 COLOR = VECTOR4 00111 }; 00112 00113 00118 csShaderVariable (); 00120 csShaderVariable (csStringID name); 00121 00122 csShaderVariable (const csShaderVariable& other); 00123 00124 virtual ~csShaderVariable (); 00125 00126 csShaderVariable& operator= (const csShaderVariable& copyFrom); 00127 00129 VariableType GetType() 00130 { 00131 /* The accessor should be called at least once so the var has a proper 00132 * type set */ 00133 if ((Type == UNKNOWN) && accessor) 00134 accessor->PreGetValue (this); 00135 return Type; 00136 } 00138 void SetType (VariableType t) 00139 { 00140 NewType (t); 00141 } 00142 00144 void SetAccessor (iShaderVariableAccessor* a, intptr_t extraData = 0) 00145 { 00146 accessor = a; 00147 accessorData = extraData; 00148 } 00149 00155 void SetName (csStringID newName) 00156 { 00157 Name = newName; 00158 } 00159 00161 csStringID GetName () const 00162 { 00163 return Name; 00164 } 00165 00167 intptr_t GetAccessorData () const 00168 { 00169 return accessorData; 00170 } 00171 00173 bool GetValue (int& value) 00174 { 00175 if (accessor) 00176 accessor->PreGetValue (this); 00177 00178 value = Int; 00179 return true; 00180 } 00181 00183 bool GetValue (float& value) 00184 { 00185 if (accessor) 00186 accessor->PreGetValue (this); 00187 00188 value = VectorValue.x; 00189 return true; 00190 } 00191 00193 bool GetValue (csRGBpixel& value) 00194 { 00195 if (accessor) 00196 accessor->PreGetValue (this); 00197 00198 value.red = 00199 (unsigned char) csClamp (int (VectorValue.x * 255.0f), 255, 0); 00200 value.green = 00201 (unsigned char) csClamp (int (VectorValue.y * 255.0f), 255, 0); 00202 value.blue = 00203 (unsigned char) csClamp (int (VectorValue.z * 255.0f), 255, 0); 00204 value.alpha = 00205 (unsigned char) csClamp (int (VectorValue.w * 255.0f), 255, 0);; 00206 return true; 00207 } 00208 00210 bool GetValue (iTextureHandle*& value) 00211 { 00212 if (accessor) 00213 accessor->PreGetValue (this); 00214 00215 if (Type != TEXTURE) 00216 { 00217 value = 0; 00218 return false; 00219 } 00220 00221 value = texture.HandValue; 00222 if (!value && texture.WrapValue) 00223 { 00224 value = texture.HandValue = texture.WrapValue->GetTextureHandle (); 00225 if(value) 00226 value->IncRef(); 00227 } 00228 return true; 00229 } 00230 00232 bool GetValue (iTextureWrapper*& value) 00233 { 00234 if (accessor) 00235 accessor->PreGetValue (this); 00236 00237 if (Type != TEXTURE) 00238 { 00239 value = 0; 00240 return false; 00241 } 00242 00243 value = texture.WrapValue; 00244 return true; 00245 } 00246 00248 bool GetValue (iRenderBuffer*& value) 00249 { 00250 if (accessor) 00251 accessor->PreGetValue (this); 00252 00253 value = RenderBuffer; 00254 return true; 00255 } 00256 00258 bool GetValue (csVector2& value) 00259 { 00260 if (accessor) 00261 accessor->PreGetValue (this); 00262 00263 value.Set (VectorValue.x, VectorValue.y); 00264 return true; 00265 } 00266 00268 bool GetValue (csVector3& value) 00269 { 00270 if (accessor) 00271 accessor->PreGetValue (this); 00272 00273 value.Set (VectorValue.x, VectorValue.y, VectorValue.z); 00274 return true; 00275 } 00276 00278 bool GetValue (csColor& value) 00279 { 00280 if (accessor) 00281 accessor->PreGetValue (this); 00282 00283 value.Set (VectorValue.x, VectorValue.y, VectorValue.z); 00284 return true; 00285 } 00286 00288 bool GetValue (csVector4& value) 00289 { 00290 if (accessor) 00291 accessor->PreGetValue (this); 00292 00293 value = VectorValue; 00294 return true; 00295 } 00296 00298 bool GetValue (csQuaternion& value) 00299 { 00300 if (accessor) 00301 accessor->PreGetValue (this); 00302 00303 value.Set (VectorValue.x, VectorValue.y, VectorValue.z, VectorValue.w); 00304 return true; 00305 } 00306 00308 bool GetValue (csMatrix3& value) 00309 { 00310 if (accessor) 00311 accessor->PreGetValue (this); 00312 00313 if (Type == MATRIX) 00314 { 00315 value = *MatrixValuePtr; 00316 return true; 00317 } 00318 00319 value = csMatrix3(); 00320 return false; 00321 } 00322 00324 bool GetValue (csReversibleTransform& value) 00325 { 00326 if (accessor) 00327 accessor->PreGetValue (this); 00328 00329 if (Type == TRANSFORM) 00330 { 00331 value = *TransformPtr; 00332 return true; 00333 } 00334 00335 value = csReversibleTransform(); 00336 return false; 00337 } 00338 00339 00341 bool SetValue (int value) 00342 { 00343 if (Type != INT) 00344 NewType (INT); 00345 00346 Int = value; 00347 float f = (float)value; 00348 VectorValue.Set (f, f, f, f); 00349 return true; 00350 } 00351 00353 bool SetValue (float value) 00354 { 00355 if (Type != FLOAT) 00356 NewType (FLOAT); 00357 00358 Int = (int)value; 00359 VectorValue.Set (value, value, value, value); 00360 return true; 00361 } 00362 00364 bool SetValue (const csRGBpixel &value) 00365 { 00366 if (Type != COLOR) 00367 NewType (COLOR); 00368 00369 VectorValue.x = (float)value.red / 255.0f; 00370 VectorValue.y = (float)value.green / 255.0f; 00371 VectorValue.z = (float)value.blue / 255.0f; 00372 VectorValue.w = (float)value.alpha / 255.0f; 00373 return true; 00374 } 00375 00377 bool SetValue (iTextureHandle* value) 00378 { 00379 if (Type != TEXTURE) 00380 NewType (TEXTURE); 00381 00382 texture.HandValue = value; 00383 00384 if (value) 00385 value->IncRef (); 00386 return true; 00387 } 00388 00390 bool SetValue (iTextureWrapper* value) 00391 { 00392 if (Type != TEXTURE) 00393 NewType (TEXTURE); 00394 00395 texture.WrapValue = value; 00396 00397 if (value) 00398 value->IncRef (); 00399 return true; 00400 } 00401 00403 bool SetValue (iRenderBuffer* value) 00404 { 00405 if (Type != RENDERBUFFER) 00406 NewType (RENDERBUFFER); 00407 00408 RenderBuffer = value; 00409 00410 if (value) 00411 value->IncRef (); 00412 return true; 00413 } 00414 00416 bool SetValue (const csVector2 &value) 00417 { 00418 if (Type != VECTOR2) 00419 NewType (VECTOR2); 00420 00421 VectorValue.Set (value.x, value.y, 0.0f, 1.0f); 00422 Int = (int)value.x; 00423 return true; 00424 } 00425 00427 bool SetValue (const csVector3 &value) 00428 { 00429 if (Type != VECTOR3) 00430 NewType (VECTOR3); 00431 00432 VectorValue.Set (value.x, value.y, value.z, 1.0f); 00433 Int = (int)value.x; 00434 return true; 00435 } 00436 00438 bool SetValue (const csColor& value) 00439 { 00440 if (Type != VECTOR3) 00441 NewType (VECTOR3); 00442 00443 VectorValue.Set (value.red, value.green, value.blue, 1.0f); 00444 Int = (int)value.red; 00445 return true; 00446 } 00447 00449 bool SetValue (const csColor4& value) 00450 { 00451 if (Type != VECTOR4) 00452 NewType (VECTOR4); 00453 00454 VectorValue.Set (value.red, value.green, value.blue, value.alpha); 00455 Int = (int)value.red; 00456 return true; 00457 } 00458 00460 bool SetValue (const csVector4 &value) 00461 { 00462 if (Type != VECTOR4) 00463 NewType (VECTOR4); 00464 00465 VectorValue.Set (value.x, value.y, value.z, value.w); 00466 Int = (int)value.x; 00467 return true; 00468 } 00469 00470 bool SetValue (const csQuaternion& value) 00471 { 00472 if (Type != VECTOR4) 00473 NewType (VECTOR4); 00474 00475 VectorValue.Set (value.v.x, value.v.y, value.v.z, value.w); 00476 return true; 00477 } 00478 00480 bool SetValue (const csMatrix3 &value) 00481 { 00482 if (Type != MATRIX) 00483 NewType (MATRIX); 00484 00485 *MatrixValuePtr = value; 00486 00487 return true; 00488 } 00489 00491 bool SetValue (const csReversibleTransform &value) 00492 { 00493 if (Type != TRANSFORM) 00494 NewType (TRANSFORM); 00495 00496 *TransformPtr = value; 00497 00498 return true; 00499 } 00500 00501 void AddVariableToArray (csShaderVariable *variable) 00502 { 00503 if (Type == ARRAY) 00504 ShaderVarArray->Push (variable); 00505 } 00506 00507 void RemoveFromArray (size_t element) 00508 { 00509 if (Type == ARRAY) 00510 ShaderVarArray->DeleteIndex (element); 00511 } 00512 00514 void SetArraySize (size_t size) 00515 { 00516 if (Type != ARRAY) 00517 NewType (ARRAY); 00518 00519 ShaderVarArray->SetSize (size); 00520 } 00521 00523 size_t GetArraySize () 00524 { 00525 return (Type == ARRAY) ? ShaderVarArray->GetSize () : 0; 00526 } 00527 00533 csShaderVariable *GetArrayElement (size_t element) 00534 { 00535 if (Type == ARRAY && element < ShaderVarArray->GetSize ()) 00536 { 00537 return ShaderVarArray->Get (element); 00538 } 00539 return 0; 00540 } 00541 00545 void SetArrayElement (size_t element, csShaderVariable *variable) 00546 { 00547 ShaderVarArray->Put (element, variable); 00548 } 00549 00550 private: 00551 csStringID Name; 00552 VariableType Type; 00553 00554 // Storage for types that can be combined.. 00555 union 00556 { 00557 // Refcounted 00558 struct 00559 { 00560 iTextureHandle* HandValue; 00561 iTextureWrapper* WrapValue; 00562 } texture; 00563 iRenderBuffer* RenderBuffer; 00564 00565 int Int; 00566 csMatrix3* MatrixValuePtr; 00567 csReversibleTransform* TransformPtr; 00568 csRefArray<csShaderVariable> *ShaderVarArray; 00569 }; 00570 00571 csVector4 VectorValue; 00572 csRef<iShaderVariableAccessor> accessor; 00573 intptr_t accessorData; 00574 00575 virtual void NewType (VariableType nt); 00576 }; 00577 00578 namespace CS 00579 { 00581 struct ShaderVarName 00582 { 00583 csStringID name; 00584 00585 ShaderVarName() : name (csInvalidStringID) {} 00586 ShaderVarName (iStringSet* strings, const char* name) 00587 { this->name = strings->Request (name); } 00588 00589 operator csStringID () const { return name; } 00590 }; 00591 00592 } // namespace CS 00593 00596 #endif
Generated for Crystal Space 1.4.1 by doxygen 1.7.1