gfxGLShader.h 6.1 KB

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