CCFrameBuffer.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CC_FRAME_BUFFER_H__
  22. #define __CC_FRAME_BUFFER_H__
  23. #include "base/CCRef.h"
  24. #include "platform/CCGL.h"
  25. #include "renderer/CCTexture2D.h"
  26. #include <set>
  27. NS_CC_BEGIN
  28. class GLView;
  29. class EventListenerCustom;
  30. namespace experimental {
  31. /**
  32. Viewport is a normalized to FrameBufferObject
  33. But for default FBO, the size is absolute.
  34. */
  35. struct CC_DLL Viewport
  36. {
  37. Viewport(float left, float bottom, float width, float height);
  38. Viewport();
  39. float _left;
  40. float _bottom;
  41. float _width;
  42. float _height;
  43. };
  44. class CC_DLL RenderTargetBase : public Ref
  45. {
  46. public:
  47. enum class Type
  48. {
  49. RenderBuffer,
  50. Texture2D,
  51. };
  52. protected:
  53. RenderTargetBase();
  54. virtual ~RenderTargetBase();
  55. bool init(unsigned int width, unsigned int height);
  56. public:
  57. virtual Texture2D* getTexture() const { return nullptr; }
  58. virtual GLuint getBuffer() const { return 0; }
  59. unsigned int getWidth() const { return _width; }
  60. unsigned int getHeight() const { return _height; }
  61. Type getType() const { return _type; }
  62. protected:
  63. Type _type;
  64. unsigned int _width;
  65. unsigned int _height;
  66. };
  67. class CC_DLL RenderTarget : public RenderTargetBase
  68. {
  69. public:
  70. static RenderTarget* create(unsigned int width, unsigned int height, Texture2D::PixelFormat format = Texture2D::PixelFormat::RGBA8888);
  71. bool init(unsigned int width, unsigned int height, Texture2D::PixelFormat format);
  72. virtual Texture2D* getTexture() const { return _texture; }
  73. CC_CONSTRUCTOR_ACCESS:
  74. RenderTarget();
  75. virtual ~RenderTarget();
  76. protected:
  77. Texture2D* _texture;
  78. #if CC_ENABLE_CACHE_TEXTURE_DATA
  79. EventListenerCustom* _rebuildTextureListener;
  80. #endif
  81. };
  82. class CC_DLL RenderTargetRenderBuffer : public RenderTargetBase
  83. {
  84. public:
  85. static RenderTargetRenderBuffer* create(unsigned int width, unsigned int height);
  86. bool init(unsigned int width, unsigned int height);
  87. virtual GLuint getBuffer() const { return _colorBuffer; }
  88. CC_CONSTRUCTOR_ACCESS:
  89. RenderTargetRenderBuffer();
  90. virtual ~RenderTargetRenderBuffer();
  91. protected:
  92. GLenum _format;
  93. GLuint _colorBuffer;
  94. #if CC_ENABLE_CACHE_TEXTURE_DATA
  95. EventListenerCustom* _reBuildRenderBufferListener;
  96. #endif
  97. };
  98. class CC_DLL RenderTargetDepthStencil : public RenderTargetBase
  99. {
  100. public:
  101. static RenderTargetDepthStencil* create(unsigned int width, unsigned int height);
  102. bool init(unsigned int width, unsigned int height);
  103. virtual GLuint getBuffer() const { return _depthStencilBuffer; }
  104. CC_DEPRECATED(3.7) GLuint getDepthStencilBuffer() const { return _depthStencilBuffer; }
  105. CC_CONSTRUCTOR_ACCESS:
  106. RenderTargetDepthStencil();
  107. virtual ~RenderTargetDepthStencil();
  108. protected:
  109. GLuint _depthStencilBuffer;
  110. #if CC_ENABLE_CACHE_TEXTURE_DATA
  111. EventListenerCustom* _reBuildDepthStencilListener;
  112. #endif
  113. };
  114. class CC_DLL FrameBuffer : public Ref
  115. {
  116. public:
  117. static FrameBuffer* create(uint8_t fid, unsigned int width, unsigned int height);
  118. bool init(uint8_t fid, unsigned int width, unsigned int height);
  119. public:
  120. GLuint getFBO() const { return _fbo; }
  121. GLuint getFID() const { return _fid; }
  122. //call glclear to clear frame buffer object
  123. void clearFBO();
  124. void applyFBO();
  125. void restoreFBO();
  126. void setClearColor(const Color4F& color) { _clearColor = color;}
  127. void setClearDepth(float depth) { _clearDepth = depth; }
  128. void setClearStencil(int8_t stencil) { _clearStencil = stencil; }
  129. const Color4F& getClearColor() const { return _clearColor; }
  130. float getClearDepth() const { return _clearDepth; }
  131. int8_t getClearStencil() const { return _clearStencil; }
  132. RenderTargetBase* getRenderTarget() const { return _rt; }
  133. RenderTargetDepthStencil* getDepthStencilTarget() const { return _rtDepthStencil; }
  134. void attachRenderTarget(RenderTargetBase* rt);
  135. void attachDepthStencilTarget(RenderTargetDepthStencil* rt);
  136. bool isDefaultFBO() const { return _isDefault; }
  137. unsigned int getWidth() const { return _width; }
  138. unsigned int getHeight() const { return _height; }
  139. CC_CONSTRUCTOR_ACCESS:
  140. FrameBuffer();
  141. virtual ~FrameBuffer();
  142. bool initWithGLView(GLView* view);
  143. private:
  144. //openGL content for FrameBuffer
  145. GLuint _fbo;
  146. GLuint _previousFBO;
  147. //dirty flag for fbo binding
  148. bool _fboBindingDirty;
  149. //
  150. uint8_t _fid;
  151. //
  152. Color4F _clearColor;
  153. float _clearDepth;
  154. int8_t _clearStencil;
  155. int _width;
  156. int _height;
  157. RenderTargetBase* _rt;
  158. RenderTargetDepthStencil* _rtDepthStencil;
  159. bool _isDefault;
  160. public:
  161. static FrameBuffer* getOrCreateDefaultFBO(GLView* glView);
  162. static void applyDefaultFBO();
  163. static void clearAllFBOs();
  164. private:
  165. //static GLuint _defaultFBO;
  166. static FrameBuffer* _defaultFBO;
  167. static std::set<FrameBuffer*> _frameBuffers;
  168. private:
  169. #if CC_ENABLE_CACHE_TEXTURE_DATA
  170. EventListenerCustom* _dirtyFBOListener;
  171. #endif
  172. };
  173. } // end of namespace experimental
  174. NS_CC_END
  175. #endif /* defined(__CC_FRAME_BUFFER_H__) */