FrameBuffer.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * FrameBuffer.cpp
  3. */
  4. #include "Base.h"
  5. #include "FrameBuffer.h"
  6. namespace gameplay
  7. {
  8. static unsigned int __maxRenderTargets = 0;
  9. static std::vector<FrameBuffer*> __frameBuffers;
  10. static FrameBufferHandle __defaultHandle = 0;
  11. static FrameBufferHandle __currentHandle = 0;
  12. FrameBuffer::FrameBuffer(const char* id, unsigned int width, unsigned int height) :
  13. _id(id ? id : ""), _width(width), _height(height), _handle(0),
  14. _renderTargets(NULL), _depthStencilTarget(NULL)
  15. {
  16. }
  17. FrameBuffer::~FrameBuffer()
  18. {
  19. if (_renderTargets)
  20. {
  21. for (unsigned int i = 0; i < __maxRenderTargets; ++i)
  22. {
  23. if (_renderTargets[i])
  24. {
  25. SAFE_RELEASE(_renderTargets[i]);
  26. }
  27. }
  28. SAFE_DELETE_ARRAY(_renderTargets);
  29. }
  30. if (_depthStencilTarget)
  31. {
  32. SAFE_RELEASE(_depthStencilTarget);
  33. }
  34. // Release GL resource.
  35. if (_handle)
  36. {
  37. GL_ASSERT( glDeleteFramebuffers(1, &_handle) );
  38. }
  39. // Remove self from vector.
  40. std::vector<FrameBuffer*>::iterator it = std::find(__frameBuffers.begin(), __frameBuffers.end(), this);
  41. if (it != __frameBuffers.end())
  42. {
  43. __frameBuffers.erase(it);
  44. }
  45. }
  46. void FrameBuffer::initialize()
  47. {
  48. // Query the current/initial FBO handle and store is as out 'default' frame buffer.
  49. // On many platforms this will simply be the zero (0) handle, but this is not always the case.
  50. GLint fbo;
  51. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &fbo);
  52. __defaultHandle = (FrameBufferHandle)fbo;
  53. __currentHandle = __defaultHandle;
  54. // Query the max supported color attachments. This glGet operation is not supported
  55. // on GL ES 2.x, so if the define does not exist, assume a value of 1.
  56. #ifdef GL_MAX_COLOR_ATTACHMENTS
  57. GLint val;
  58. GL_ASSERT( glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &val) );
  59. __maxRenderTargets = (unsigned int)std::max(1, val);
  60. #else
  61. __maxRenderTargets = 1;
  62. #endif
  63. }
  64. FrameBuffer* FrameBuffer::create(const char* id)
  65. {
  66. return create(id, 0, 0);
  67. }
  68. FrameBuffer* FrameBuffer::create(const char* id, unsigned int width, unsigned int height)
  69. {
  70. // Call getMaxRenderTargets() to force __maxRenderTargets to be set
  71. getMaxRenderTargets();
  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);
  87. frameBuffer->_handle = 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. if (_renderTargets[index] == target)
  136. {
  137. // No change.
  138. return;
  139. }
  140. // Release our reference to the current RenderTarget at this index.
  141. SAFE_RELEASE(_renderTargets[index]);
  142. _renderTargets[index] = target;
  143. if (target)
  144. {
  145. GP_ASSERT( _renderTargets[index]->getTexture() );
  146. // This FrameBuffer now references the RenderTarget.
  147. target->addRef();
  148. // Now set this target as the color attachment corresponding to index.
  149. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  150. GLenum attachment = GL_COLOR_ATTACHMENT0 + index;
  151. GL_ASSERT( glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, _renderTargets[index]->getTexture()->getHandle(), 0) );
  152. GLenum fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  153. if (fboStatus != GL_FRAMEBUFFER_COMPLETE)
  154. {
  155. GP_ERROR("Framebuffer status incomplete: 0x%x", fboStatus);
  156. }
  157. // Restore the FBO binding
  158. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, __currentHandle) );
  159. }
  160. }
  161. RenderTarget* FrameBuffer::getRenderTarget(unsigned int index) const
  162. {
  163. GP_ASSERT(_renderTargets);
  164. if (index < __maxRenderTargets)
  165. {
  166. return _renderTargets[index];
  167. }
  168. return NULL;
  169. }
  170. void FrameBuffer::setDepthStencilTarget(DepthStencilTarget* target)
  171. {
  172. if (_depthStencilTarget == target)
  173. return;
  174. // Release our existing depth stencil target.
  175. SAFE_RELEASE(_depthStencilTarget);
  176. _depthStencilTarget = target;
  177. if (target)
  178. {
  179. // The FrameBuffer now owns this DepthStencilTarget.
  180. target->addRef();
  181. // Now set this target as the color attachment corresponding to index.
  182. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  183. // Attach the render buffer to the framebuffer
  184. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthStencilTarget->_renderBuffer) );
  185. if (target->getFormat() == DepthStencilTarget::DEPTH_STENCIL)
  186. {
  187. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _depthStencilTarget->_renderBuffer) );
  188. }
  189. // Check the framebuffer is good to go.
  190. GLenum fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  191. if (fboStatus != GL_FRAMEBUFFER_COMPLETE)
  192. {
  193. GP_ERROR("Framebuffer status incomplete: 0x%x", fboStatus);
  194. }
  195. // Restore the FBO binding
  196. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, __currentHandle) );
  197. }
  198. }
  199. DepthStencilTarget* FrameBuffer::getDepthStencilTarget() const
  200. {
  201. return _depthStencilTarget;
  202. }
  203. void FrameBuffer::bind()
  204. {
  205. // Bind this FrameBuffer for rendering.
  206. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  207. // Update the current FBO handle
  208. __currentHandle = _handle;
  209. }
  210. void FrameBuffer::bindDefault()
  211. {
  212. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, __defaultHandle) );
  213. // Update the current FBO handle
  214. __currentHandle = __defaultHandle;
  215. }
  216. }