FrameBuffer.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "Base.h"
  2. #include "FrameBuffer.h"
  3. #include "Game.h"
  4. #define FRAMEBUFFER_ID_DEFAULT "org.gameplay3d.framebuffer.default"
  5. namespace gameplay
  6. {
  7. unsigned int FrameBuffer::_maxRenderTargets = 0;
  8. std::vector<FrameBuffer*> FrameBuffer::_frameBuffers;
  9. FrameBuffer* FrameBuffer::_defaultFrameBuffer = NULL;
  10. FrameBuffer* FrameBuffer::_currentFrameBuffer = NULL;
  11. FrameBuffer::FrameBuffer(const char* id, unsigned int width, unsigned int height, FrameBufferHandle handle) :
  12. _id(id ? id : ""), _width(width), _height(height), _handle(handle),
  13. _renderTargets(NULL), _depthStencilTarget(NULL)
  14. {
  15. }
  16. FrameBuffer::~FrameBuffer()
  17. {
  18. if (_renderTargets)
  19. {
  20. for (unsigned int i = 0; i < _maxRenderTargets; ++i)
  21. {
  22. if (_renderTargets[i])
  23. {
  24. SAFE_RELEASE(_renderTargets[i]);
  25. }
  26. }
  27. SAFE_DELETE_ARRAY(_renderTargets);
  28. }
  29. if (_depthStencilTarget)
  30. {
  31. SAFE_RELEASE(_depthStencilTarget);
  32. }
  33. // Release GL resource.
  34. if (_handle)
  35. {
  36. GL_ASSERT( glDeleteFramebuffers(1, &_handle) );
  37. }
  38. // Remove self from vector.
  39. std::vector<FrameBuffer*>::iterator it = std::find(_frameBuffers.begin(), _frameBuffers.end(), this);
  40. if (it != _frameBuffers.end())
  41. {
  42. _frameBuffers.erase(it);
  43. }
  44. }
  45. void FrameBuffer::initialize()
  46. {
  47. // Query the current/initial FBO handle and store is as out 'default' frame buffer.
  48. // On many platforms this will simply be the zero (0) handle, but this is not always the case.
  49. GLint fbo;
  50. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &fbo);
  51. _defaultFrameBuffer = new FrameBuffer(FRAMEBUFFER_ID_DEFAULT, 0, 0, (FrameBufferHandle)fbo);
  52. _currentFrameBuffer = _defaultFrameBuffer;
  53. // Query the max supported color attachments. This glGet operation is not supported
  54. // on GL ES 2.x, so if the define does not exist, assume a value of 1.
  55. #ifdef GL_MAX_COLOR_ATTACHMENTS
  56. GLint val;
  57. GL_ASSERT( glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &val) );
  58. _maxRenderTargets = (unsigned int)std::max(1, val);
  59. #else
  60. _maxRenderTargets = 1;
  61. #endif
  62. }
  63. void FrameBuffer::finalize()
  64. {
  65. SAFE_RELEASE(_defaultFrameBuffer);
  66. }
  67. FrameBuffer* FrameBuffer::create(const char* id)
  68. {
  69. return create(id, 0, 0);
  70. }
  71. FrameBuffer* FrameBuffer::create(const char* id, unsigned int width, unsigned int height)
  72. {
  73. RenderTarget* renderTarget = NULL;
  74. if (width > 0 && height > 0)
  75. {
  76. // Create a default RenderTarget with same ID.
  77. renderTarget = RenderTarget::create(id, width, height);
  78. if (renderTarget == NULL)
  79. {
  80. GP_ERROR("Failed to create render target for frame buffer.");
  81. return NULL;
  82. }
  83. }
  84. // Create the frame buffer
  85. GLuint handle = 0;
  86. GL_ASSERT( glGenFramebuffers(1, &handle) );
  87. FrameBuffer* frameBuffer = new FrameBuffer(id, width, height, handle);
  88. // Create the render target array for the new frame buffer
  89. frameBuffer->_renderTargets = new RenderTarget*[_maxRenderTargets];
  90. memset(frameBuffer->_renderTargets, 0, sizeof(RenderTarget*) * _maxRenderTargets);
  91. if (renderTarget)
  92. {
  93. frameBuffer->setRenderTarget(renderTarget, 0);
  94. SAFE_RELEASE(renderTarget);
  95. }
  96. _frameBuffers.push_back(frameBuffer);
  97. return frameBuffer;
  98. }
  99. FrameBuffer* FrameBuffer::getFrameBuffer(const char* id)
  100. {
  101. GP_ASSERT(id);
  102. // Search the vector for a matching ID.
  103. std::vector<FrameBuffer*>::const_iterator it;
  104. for (it = _frameBuffers.begin(); it < _frameBuffers.end(); ++it)
  105. {
  106. FrameBuffer* fb = *it;
  107. GP_ASSERT(fb);
  108. if (strcmp(id, fb->getId()) == 0)
  109. {
  110. return fb;
  111. }
  112. }
  113. return NULL;
  114. }
  115. const char* FrameBuffer::getId() const
  116. {
  117. return _id.c_str();
  118. }
  119. unsigned int FrameBuffer::getWidth() const
  120. {
  121. return _width;
  122. }
  123. unsigned int FrameBuffer::getHeight() const
  124. {
  125. return _height;
  126. }
  127. unsigned int FrameBuffer::getMaxRenderTargets()
  128. {
  129. return _maxRenderTargets;
  130. }
  131. void FrameBuffer::setRenderTarget(RenderTarget* target, unsigned int index)
  132. {
  133. GP_ASSERT(index < _maxRenderTargets);
  134. GP_ASSERT(_renderTargets);
  135. // No change
  136. if (_renderTargets[index] == target)
  137. return;
  138. // Release our reference to the current RenderTarget at this index.
  139. SAFE_RELEASE(_renderTargets[index]);
  140. _renderTargets[index] = target;
  141. if (target)
  142. {
  143. GP_ASSERT( _renderTargets[index]->getTexture() );
  144. // This FrameBuffer now references the RenderTarget.
  145. target->addRef();
  146. // Now set this target as the color attachment corresponding to index.
  147. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  148. GLenum attachment = GL_COLOR_ATTACHMENT0 + index;
  149. GL_ASSERT( glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, _renderTargets[index]->getTexture()->getHandle(), 0) );
  150. GLenum fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  151. if (fboStatus != GL_FRAMEBUFFER_COMPLETE)
  152. {
  153. GP_ERROR("Framebuffer status incomplete: 0x%x", fboStatus);
  154. }
  155. // Restore the FBO binding
  156. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _currentFrameBuffer->_handle) );
  157. }
  158. }
  159. RenderTarget* FrameBuffer::getRenderTarget(unsigned int index) const
  160. {
  161. GP_ASSERT(_renderTargets);
  162. if (index < _maxRenderTargets)
  163. {
  164. return _renderTargets[index];
  165. }
  166. return NULL;
  167. }
  168. void FrameBuffer::setDepthStencilTarget(DepthStencilTarget* target)
  169. {
  170. if (_depthStencilTarget == target)
  171. return;
  172. // Release our existing depth stencil target.
  173. SAFE_RELEASE(_depthStencilTarget);
  174. _depthStencilTarget = target;
  175. if (target)
  176. {
  177. // The FrameBuffer now owns this DepthStencilTarget.
  178. target->addRef();
  179. // Now set this target as the color attachment corresponding to index.
  180. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  181. // Attach the render buffer to the framebuffer
  182. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthStencilTarget->_renderBuffer) );
  183. if (target->getFormat() == DepthStencilTarget::DEPTH_STENCIL)
  184. {
  185. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _depthStencilTarget->_renderBuffer) );
  186. }
  187. // Check the framebuffer is good to go.
  188. GLenum fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  189. if (fboStatus != GL_FRAMEBUFFER_COMPLETE)
  190. {
  191. GP_ERROR("Framebuffer status incomplete: 0x%x", fboStatus);
  192. }
  193. // Restore the FBO binding
  194. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _currentFrameBuffer->_handle) );
  195. }
  196. }
  197. DepthStencilTarget* FrameBuffer::getDepthStencilTarget() const
  198. {
  199. return _depthStencilTarget;
  200. }
  201. FrameBuffer* FrameBuffer::bind()
  202. {
  203. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  204. FrameBuffer* previousFrameBuffer = _currentFrameBuffer;
  205. _currentFrameBuffer = this;
  206. return previousFrameBuffer;
  207. }
  208. FrameBuffer* FrameBuffer::bindDefault()
  209. {
  210. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _defaultFrameBuffer->_handle) );
  211. _currentFrameBuffer = _defaultFrameBuffer;
  212. return _defaultFrameBuffer;
  213. }
  214. }