gfxGLShader.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 _GFXGLSHADER_H_
  23. #define _GFXGLSHADER_H_
  24. #include "core/util/refBase.h"
  25. #include "gfx/gfxShader.h"
  26. #include "gfx/gl/tGL/tGL.h"
  27. #include "core/util/tSignal.h"
  28. #include "core/util/tDictionary.h"
  29. class GFXGLShaderConstHandle;
  30. class FileStream;
  31. class GFXGLShaderConstBuffer;
  32. class GFXGLShader : public GFXShader
  33. {
  34. typedef Map<String, GFXGLShaderConstHandle*> HandleMap;
  35. public:
  36. GFXGLShader();
  37. virtual ~GFXGLShader();
  38. /// @name GFXShader interface
  39. /// @{
  40. virtual GFXShaderConstHandle* getShaderConstHandle(const String& name);
  41. virtual GFXShaderConstHandle* findShaderConstHandle(const String& name);
  42. /// Returns our list of shader constants, the material can get this and just set the constants it knows about
  43. virtual const Vector<GFXShaderConstDesc>& getShaderConstDesc() const;
  44. /// Returns the alignment value for constType
  45. virtual U32 getAlignmentValue(const GFXShaderConstType constType) const;
  46. virtual GFXShaderConstBufferRef allocConstBuffer();
  47. /// @}
  48. /// @name GFXResource interface
  49. /// @{
  50. virtual void zombify();
  51. virtual void resurrect() { reload(); }
  52. virtual const String describeSelf() const;
  53. /// @}
  54. /// Activates this shader in the GL context.
  55. void useProgram();
  56. protected:
  57. friend class GFXGLShaderConstBuffer;
  58. friend class GFXGLShaderConstHandle;
  59. virtual bool _init();
  60. bool initShader( const Torque::Path &file,
  61. bool isVertex,
  62. const Vector<GFXShaderMacro> &macros );
  63. void clearShaders();
  64. void initConstantDescs();
  65. void initHandles();
  66. void setConstantsFromBuffer(GFXGLShaderConstBuffer* buffer);
  67. static char* _handleIncludes( const Torque::Path &path, FileStream *s );
  68. static bool _loadShaderFromStream( GLuint shader,
  69. const Torque::Path& path,
  70. FileStream* s,
  71. const Vector<GFXShaderMacro>& macros );
  72. /// @name Internal GL handles
  73. /// @{
  74. GLuint mVertexShader;
  75. GLuint mPixelShader;
  76. GLuint mProgram;
  77. /// @}
  78. Vector<GFXShaderConstDesc> mConstants;
  79. U32 mConstBufferSize;
  80. U8* mConstBuffer;
  81. HandleMap mHandles;
  82. Vector<GFXGLShaderConstHandle*> mValidHandles;
  83. };
  84. class GFXGLShaderConstBuffer : public GFXShaderConstBuffer
  85. {
  86. public:
  87. GFXGLShaderConstBuffer(GFXGLShader* shader, U32 bufSize, U8* existingConstants);
  88. ~GFXGLShaderConstBuffer();
  89. /// Called by GFXGLDevice to activate this buffer.
  90. void activate();
  91. /// Called when the shader this buffer references is reloaded.
  92. void onShaderReload( GFXGLShader *shader );
  93. // GFXShaderConstBuffer
  94. virtual GFXShader* getShader() { return mShader; }
  95. virtual void set(GFXShaderConstHandle* handle, const F32 fv);
  96. virtual void set(GFXShaderConstHandle* handle, const Point2F& fv);
  97. virtual void set(GFXShaderConstHandle* handle, const Point3F& fv);
  98. virtual void set(GFXShaderConstHandle* handle, const Point4F& fv);
  99. virtual void set(GFXShaderConstHandle* handle, const PlaneF& fv);
  100. virtual void set(GFXShaderConstHandle* handle, const ColorF& fv);
  101. virtual void set(GFXShaderConstHandle* handle, const S32 f);
  102. virtual void set(GFXShaderConstHandle* handle, const Point2I& fv);
  103. virtual void set(GFXShaderConstHandle* handle, const Point3I& fv);
  104. virtual void set(GFXShaderConstHandle* handle, const Point4I& fv);
  105. virtual void set(GFXShaderConstHandle* handle, const AlignedArray<F32>& fv);
  106. virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point2F>& fv);
  107. virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point3F>& fv);
  108. virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point4F>& fv);
  109. virtual void set(GFXShaderConstHandle* handle, const AlignedArray<S32>& fv);
  110. virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point2I>& fv);
  111. virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point3I>& fv);
  112. virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point4I>& fv);
  113. virtual void set(GFXShaderConstHandle* handle, const MatrixF& mat, const GFXShaderConstType matType = GFXSCT_Float4x4);
  114. virtual void set(GFXShaderConstHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4);
  115. // GFXResource
  116. virtual const String describeSelf() const;
  117. virtual void zombify() {}
  118. virtual void resurrect() {}
  119. private:
  120. friend class GFXGLShader;
  121. U8* mBuffer;
  122. WeakRefPtr<GFXGLShader> mShader;
  123. template<typename ConstType>
  124. void internalSet(GFXShaderConstHandle* handle, const ConstType& param);
  125. template<typename ConstType>
  126. void internalSet(GFXShaderConstHandle* handle, const AlignedArray<ConstType>& fv);
  127. };
  128. #endif // _GFXGLSHADER_H_