FrameBuffer.cpp 8.5 KB

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