FrameBuffer.cpp 7.4 KB

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