gfxGLShader.h 5.9 KB

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