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 mDXGeometryShaderName;
  50. StringTableEntry mOGLVertexShaderName;
  51. StringTableEntry mOGLPixelShaderName;
  52. StringTableEntry mOGLGeometryShaderName;
  53. /// A semicolon, tab, or newline delimited string of case
  54. /// sensitive defines that are passed to the shader compiler.
  55. ///
  56. /// For example:
  57. ///
  58. /// SAMPLE_TAPS=10;USE_TEXKILL;USE_TORQUE_FOG=1
  59. ///
  60. String mDefines;
  61. /// The shader macros built from mDefines.
  62. /// @see _getMacros()
  63. Vector<GFXShaderMacro> mShaderMacros;
  64. /// Returns the shader macros taking care to rebuild
  65. /// them if the content has changed.
  66. const Vector<GFXShaderMacro>& _getMacros();
  67. /// Helper for converting an array of macros
  68. /// into a formatted string.
  69. void _stringizeMacros(const Vector<GFXShaderMacro>& macros,
  70. String* outString);
  71. /// Creates a new shader returning NULL on error.
  72. GFXShader* _createShader(const Vector<GFXShaderMacro>& macros);
  73. /// @see LightManager::smActivateSignal
  74. static void _onLMActivate(const char* lm, bool activate);
  75. enum
  76. {
  77. NumTextures = 16
  78. };
  79. String mSamplerNames[NumTextures];
  80. bool mRTParams[NumTextures];
  81. bool _checkDefinition(GFXShader* shader);
  82. public:
  83. void setSamplerName(const String& name, int idx) { mSamplerNames[idx] = name; }
  84. String getSamplerName(int idx) const { return mSamplerNames[idx]; }
  85. bool hasSamplerDef(const String& samplerName, int& pos) const;
  86. bool hasRTParamsDef(const int pos) const { return mRTParams[pos]; }
  87. ShaderData();
  88. /// Returns an initialized shader instance or NULL
  89. /// if the shader failed to be created.
  90. GFXShader* getShader(const Vector<GFXShaderMacro>& macros = Vector<GFXShaderMacro>());
  91. /// Forces a reinitialization of all the instanced shaders.
  92. void reloadShaders();
  93. /// Forces a reinitialization of the instanced shaders for
  94. /// all loaded ShaderData objects in the system.
  95. static void reloadAllShaders();
  96. /// Returns the required pixel shader version for this shader.
  97. F32 getPixVersion() const { return mPixVersion; }
  98. // SimObject
  99. bool onAdd() override;
  100. void onRemove() override;
  101. // ConsoleObject
  102. static void initPersistFields();
  103. DECLARE_CONOBJECT(ShaderData);
  104. };
  105. #endif // _SHADERTDATA_H_