gfxGLShader.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 FileStream;
  30. class GFXGLDevice;
  31. class GFXGLShader;
  32. struct BufferRange
  33. {
  34. U32 mBufMin = U32_MAX;
  35. U32 mBufMax = 0;
  36. inline void addSlot(U32 slot)
  37. {
  38. mBufMin = getMin(mBufMin, slot);
  39. mBufMax = getMax(mBufMax, slot);
  40. }
  41. inline bool isValid() const { return mBufMin <= mBufMax; }
  42. };
  43. struct ConstantBuffer
  44. {
  45. GLuint bufHandle;
  46. U8* data;
  47. U32 size;
  48. bool isDirty;
  49. };
  50. class GFXGLShaderConstHandle : public GFXShaderConstHandle
  51. {
  52. friend class GFXGLShader;
  53. public:
  54. // DX side needs the description map as the same uniform can exist across stages. for gl it is program wide.
  55. GFXGLShaderConstHandle(GFXGLShader* shader);
  56. GFXGLShaderConstHandle(GFXGLShader* shader,
  57. const GFXShaderConstDesc& desc);
  58. void reinit(const GFXShaderConstDesc& desc);
  59. virtual ~GFXGLShaderConstHandle();
  60. const GFXShaderConstDesc getDesc();
  61. const String& getName() const override { return mDesc.name; }
  62. GFXShaderConstType getType() const override { return mDesc.constType; }
  63. U32 getArraySize() const override { return mDesc.arraySize; }
  64. U32 getSize() const { return mDesc.size; }
  65. void setValid(bool valid) { mValid = valid; }
  66. /// @warning This will always return the value assigned when the shader was
  67. /// initialized. If the value is later changed this method won't reflect that.
  68. S32 getSamplerRegister() const override { return (!isSampler() || !mValid) ? -1 : mDesc.samplerReg; }
  69. // Returns true if this is a handle to a sampler register.
  70. bool isSampler() const
  71. {
  72. return (getType() >= GFXSCT_Sampler);
  73. }
  74. /// Restore to uninitialized state.
  75. void clear()
  76. {
  77. mShader = NULL;
  78. mInstancingConstant = false;
  79. mValid = false;
  80. }
  81. GFXShaderConstDesc mDesc;
  82. GFXGLShader* mShader;
  83. bool mUBOUniform;
  84. bool mInstancingConstant;
  85. };
  86. class GFXGLShaderConstBuffer : public GFXShaderConstBuffer
  87. {
  88. public:
  89. // -1 is the global buffer.
  90. typedef Map<S32, ConstantBuffer> BufferMap;
  91. GFXGLShaderConstBuffer(GFXGLShader* shader);
  92. ~GFXGLShaderConstBuffer();
  93. /// Called by GFXGLDevice to activate this buffer.
  94. void activate(GFXGLShaderConstBuffer* prevShaderBuffer);
  95. void addBuffer(const GFXShaderConstDesc desc);
  96. /// Called when the shader this buffer references is reloaded.
  97. void onShaderReload(GFXGLShader* shader);
  98. // GFXShaderConstBuffer
  99. GFXShader* getShader() override;
  100. void set(GFXShaderConstHandle* handle, const F32 fv) override;
  101. void set(GFXShaderConstHandle* handle, const Point2F& fv) override;
  102. void set(GFXShaderConstHandle* handle, const Point3F& fv) override;
  103. void set(GFXShaderConstHandle* handle, const Point4F& fv) override;
  104. void set(GFXShaderConstHandle* handle, const PlaneF& fv) override;
  105. void set(GFXShaderConstHandle* handle, const LinearColorF& fv) override;
  106. void set(GFXShaderConstHandle* handle, const S32 f) override;
  107. void set(GFXShaderConstHandle* handle, const Point2I& fv) override;
  108. void set(GFXShaderConstHandle* handle, const Point3I& fv) override;
  109. void set(GFXShaderConstHandle* handle, const Point4I& fv) override;
  110. void set(GFXShaderConstHandle* handle, const AlignedArray<F32>& fv) override;
  111. void set(GFXShaderConstHandle* handle, const AlignedArray<Point2F>& fv) override;
  112. void set(GFXShaderConstHandle* handle, const AlignedArray<Point3F>& fv) override;
  113. void set(GFXShaderConstHandle* handle, const AlignedArray<Point4F>& fv) override;
  114. void set(GFXShaderConstHandle* handle, const AlignedArray<S32>& fv) override;
  115. void set(GFXShaderConstHandle* handle, const AlignedArray<Point2I>& fv) override;
  116. void set(GFXShaderConstHandle* handle, const AlignedArray<Point3I>& fv) override;
  117. void set(GFXShaderConstHandle* handle, const AlignedArray<Point4I>& fv) override;
  118. void set(GFXShaderConstHandle* handle, const MatrixF& mat, const GFXShaderConstType matType = GFXSCT_Float4x4) override;
  119. void set(GFXShaderConstHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4) override;
  120. // GFXResource
  121. const String describeSelf() const override;
  122. void zombify() override {}
  123. void resurrect() override {}
  124. private:
  125. friend class GFXGLShader;
  126. WeakRefPtr<GFXGLShader> mShader;
  127. BufferMap mBufferMap;
  128. template<typename ConstType>
  129. void internalSet(GFXShaderConstHandle* handle, const ConstType& param);
  130. template<typename ConstType>
  131. void internalSet(GFXShaderConstHandle* handle, const AlignedArray<ConstType>& fv);
  132. };
  133. class GFXGLShader : public GFXShader
  134. {
  135. friend class GFXGLShaderConstBuffer;
  136. friend class GFXGLShaderConstHandle;
  137. public:
  138. typedef Map<String, GFXGLShaderConstHandle*> HandleMap;
  139. typedef Map<String, GFXShaderConstDesc> BufferMap;
  140. GFXGLShader(GFXGLDevice* device);
  141. virtual ~GFXGLShader();
  142. /// @name GFXShader interface
  143. /// @{
  144. GFXShaderConstHandle* getShaderConstHandle(const String& name) override;
  145. GFXShaderConstHandle* findShaderConstHandle(const String& name) override;
  146. /// Returns our list of shader constants, the material can get this and just set the constants it knows about
  147. const Vector<GFXShaderConstDesc>& getShaderConstDesc() const override;
  148. /// Returns the alignment value for constType
  149. U32 getAlignmentValue(const GFXShaderConstType constType) const override;
  150. GFXShaderConstBufferRef allocConstBuffer() override;
  151. /// @}
  152. /// @name GFXResource interface
  153. /// @{
  154. void zombify() override;
  155. void resurrect() override { reload(); }
  156. const String describeSelf() const override;
  157. /// @}
  158. /// Activates this shader in the GL context.
  159. void useProgram();
  160. protected:
  161. bool _init() override;
  162. bool initShader(const Torque::Path& file,
  163. GFXShaderStage stage,
  164. const Vector<GFXShaderMacro>& macros);
  165. void clearShaders();
  166. void initConstantDescs();
  167. void initHandles();
  168. void setConstantsFromBuffer(U8* buffer);
  169. static char* _handleIncludes(const Torque::Path& path, FileStream* s);
  170. static bool _loadShaderFromStream(GLuint shader,
  171. const Torque::Path& path,
  172. FileStream* s,
  173. const Vector<GFXShaderMacro>& macros);
  174. /// @name Internal GL handles
  175. /// @{
  176. GLuint mVertexShader;
  177. GLuint mPixelShader;
  178. GLuint mGeometryShader;
  179. GLuint mProgram;
  180. /// @}
  181. U8* mGlobalConstBuffer;
  182. Vector<GFXShaderConstDesc> mShaderConsts;
  183. HandleMap mHandles;
  184. BufferMap mBuffers;
  185. GFXGLDevice* mDevice;
  186. GFXShaderConstType convertConstType(GLenum constType);
  187. };
  188. #endif // _GFXGLSHADER_H_