shaderData.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SHADERTDATA_H_
  23. #define _SHADERTDATA_H_
  24. #ifndef _SIMOBJECT_H_
  25. #include "console/simObject.h"
  26. #endif
  27. #ifndef _GFXSHADER_H_
  28. #include "gfx/gfxShader.h"
  29. #endif
  30. #ifndef _TDICTIONARY_H_
  31. #include "core/util/tDictionary.h"
  32. #endif
  33. class GFXShader;
  34. class ShaderData;
  35. struct GFXShaderMacro;
  36. ///
  37. class ShaderData : public SimObject
  38. {
  39. typedef SimObject Parent;
  40. protected:
  41. ///
  42. static Vector<ShaderData*> smAllShaderData;
  43. typedef HashTable<String,GFXShaderRef> ShaderCache;
  44. ShaderCache mShaders;
  45. bool mUseDevicePixVersion;
  46. F32 mPixVersion;
  47. StringTableEntry mDXVertexShaderName;
  48. StringTableEntry mDXPixelShaderName;
  49. StringTableEntry mOGLVertexShaderName;
  50. StringTableEntry mOGLPixelShaderName;
  51. /// A semicolon, tab, or newline delimited string of case
  52. /// sensitive defines that are passed to the shader compiler.
  53. ///
  54. /// For example:
  55. ///
  56. /// SAMPLE_TAPS=10;USE_TEXKILL;USE_TORQUE_FOG=1
  57. ///
  58. String mDefines;
  59. /// The shader macros built from mDefines.
  60. /// @see _getMacros()
  61. Vector<GFXShaderMacro> mShaderMacros;
  62. /// Returns the shader macros taking care to rebuild
  63. /// them if the content has changed.
  64. const Vector<GFXShaderMacro>& _getMacros();
  65. /// Helper for converting an array of macros
  66. /// into a formatted string.
  67. void _stringizeMacros( const Vector<GFXShaderMacro> &macros,
  68. String *outString );
  69. /// Creates a new shader returning NULL on error.
  70. GFXShader* _createShader( const Vector<GFXShaderMacro> &macros );
  71. /// @see LightManager::smActivateSignal
  72. static void _onLMActivate( const char *lm, bool activate );
  73. enum
  74. {
  75. NumTextures = 16
  76. };
  77. String mSamplerNames[NumTextures];
  78. bool mRTParams[NumTextures];
  79. bool _checkDefinition(GFXShader *shader);
  80. public:
  81. void setSamplerName(const String &name, int idx) { mSamplerNames[idx] = name; }
  82. String getSamplerName(int idx) const { return mSamplerNames[idx]; }
  83. bool hasSamplerDef(const String &samplerName, int &pos) const;
  84. bool hasRTParamsDef(const int pos) const { return mRTParams[pos]; }
  85. ShaderData();
  86. /// Returns an initialized shader instance or NULL
  87. /// if the shader failed to be created.
  88. GFXShader* getShader( const Vector<GFXShaderMacro> &macros = Vector<GFXShaderMacro>() );
  89. /// Forces a reinitialization of all the instanced shaders.
  90. void reloadShaders();
  91. /// Forces a reinitialization of the instanced shaders for
  92. /// all loaded ShaderData objects in the system.
  93. static void reloadAllShaders();
  94. /// Returns the required pixel shader version for this shader.
  95. F32 getPixVersion() const { return mPixVersion; }
  96. // SimObject
  97. virtual bool onAdd();
  98. virtual void onRemove();
  99. // ConsoleObject
  100. static void initPersistFields();
  101. DECLARE_CONOBJECT(ShaderData);
  102. };
  103. #endif // _SHADERTDATA_H_