FrameBuffer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. FrameBuffer::FrameBuffer(const char* id) :
  11. _id(id ? id : ""), _handle(0), _renderTargets(NULL), _depthStencilTarget(NULL)
  12. {
  13. }
  14. FrameBuffer::~FrameBuffer()
  15. {
  16. if (_renderTargets)
  17. {
  18. for (unsigned int i = 0; i < __maxRenderTargets; ++i)
  19. {
  20. SAFE_RELEASE(_renderTargets[i]);
  21. }
  22. SAFE_DELETE_ARRAY(_renderTargets);
  23. }
  24. // Release GL resource.
  25. if (_handle)
  26. {
  27. GL_ASSERT( glDeleteFramebuffers(1, &_handle) );
  28. }
  29. // Remove self from vector.
  30. std::vector<FrameBuffer*>::iterator it = std::find(__frameBuffers.begin(), __frameBuffers.end(), this);
  31. if (it != __frameBuffers.end())
  32. {
  33. __frameBuffers.erase(it);
  34. }
  35. }
  36. FrameBuffer* FrameBuffer::create(const char* id)
  37. {
  38. // Create GL FBO resource.
  39. GLuint handle = 0;
  40. GL_ASSERT( glGenFramebuffers(1, &handle) );
  41. // Call getMaxRenderTargets() to force __maxRenderTargets to be set
  42. getMaxRenderTargets();
  43. // Create the render target array for the new frame buffer
  44. RenderTarget** renderTargets = new RenderTarget*[__maxRenderTargets];
  45. memset(renderTargets, 0, sizeof(RenderTarget*) * __maxRenderTargets);
  46. // Create the new frame buffer
  47. FrameBuffer* frameBuffer = new FrameBuffer(id ? id : "");
  48. frameBuffer->_handle = handle;
  49. frameBuffer->_renderTargets = renderTargets;
  50. // Add to the global list of managed frame buffers
  51. __frameBuffers.push_back(frameBuffer);
  52. return frameBuffer;
  53. }
  54. FrameBuffer* FrameBuffer::create(const char* id, unsigned int width, unsigned int height)
  55. {
  56. // Create RenderTarget with same ID
  57. RenderTarget* renderTarget = RenderTarget::create(id, width, height);
  58. if (renderTarget == NULL)
  59. {
  60. return NULL;
  61. }
  62. // Create the frame buffer
  63. FrameBuffer* frameBuffer = create(id);
  64. if (frameBuffer == NULL)
  65. {
  66. return NULL;
  67. }
  68. // Add the render target as the first color attachment
  69. frameBuffer->setRenderTarget(renderTarget);
  70. SAFE_RELEASE(renderTarget);
  71. return frameBuffer;
  72. }
  73. FrameBuffer* FrameBuffer::getFrameBuffer(const char* id)
  74. {
  75. // Search the vector for a matching ID.
  76. std::vector<FrameBuffer*>::const_iterator it;
  77. for (it = __frameBuffers.begin(); it < __frameBuffers.end(); it++)
  78. {
  79. FrameBuffer* fb = *it;
  80. if (strcmp(id, fb->getID()) == 0)
  81. {
  82. return fb;
  83. }
  84. }
  85. return NULL;
  86. }
  87. const char* FrameBuffer::getID() const
  88. {
  89. return _id.c_str();
  90. }
  91. unsigned int FrameBuffer::getMaxRenderTargets()
  92. {
  93. if (__maxRenderTargets == 0)
  94. {
  95. #ifdef GL_MAX_COLOR_ATTACHMENTS
  96. GLint val;
  97. GL_ASSERT( glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &val) );
  98. __maxRenderTargets = (unsigned int) val;
  99. #else
  100. __maxRenderTargets = 1;
  101. #endif
  102. }
  103. return __maxRenderTargets;
  104. }
  105. void FrameBuffer::setRenderTarget(RenderTarget* target, unsigned int index)
  106. {
  107. GP_ASSERT(index < __maxRenderTargets);
  108. if (_renderTargets[index] == target)
  109. {
  110. // No change
  111. return;
  112. }
  113. // Release our reference to the current RenderTarget at this index.
  114. SAFE_RELEASE(_renderTargets[index]);
  115. _renderTargets[index] = target;
  116. if (target)
  117. {
  118. // This FrameBuffer now references the RenderTarget.
  119. target->addRef();
  120. // Store the current FBO binding so we can restore it
  121. GLint currentFbo;
  122. GL_ASSERT( glGetIntegerv(GL_FRAMEBUFFER_BINDING, &currentFbo) );
  123. // Now set this target as the color attachment corresponding to index.
  124. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  125. GLenum attachment = GL_COLOR_ATTACHMENT0 + index;
  126. GL_ASSERT( glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, _renderTargets[index]->getTexture()->getHandle(), 0) );
  127. // Restore the FBO binding
  128. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, currentFbo) );
  129. }
  130. }
  131. RenderTarget* FrameBuffer::getRenderTarget(unsigned int index) const
  132. {
  133. if (index < __maxRenderTargets)
  134. {
  135. return _renderTargets[index];
  136. }
  137. return NULL;
  138. }
  139. void FrameBuffer::setDepthStencilTarget(DepthStencilTarget* target)
  140. {
  141. if (_depthStencilTarget == target)
  142. {
  143. return; // No change
  144. }
  145. // Release our existing depth stencil target
  146. SAFE_RELEASE(_depthStencilTarget);
  147. _depthStencilTarget = target;
  148. if (target)
  149. {
  150. // The FrameBuffer now owns this DepthStencilTarget
  151. target->addRef();
  152. // Store the current FBO binding so we can restore it
  153. GLint currentFbo;
  154. GL_ASSERT( glGetIntegerv(GL_FRAMEBUFFER_BINDING, &currentFbo) );
  155. // Now set this target as the color attachment corresponding to index.
  156. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  157. // Bind the depth texture
  158. GL_ASSERT( glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _depthStencilTarget->getTexture()->getHandle(), 0) );
  159. // If the taget has a stencil buffer, bind that as well
  160. if (target->getFormat() == DepthStencilTarget::DEPTH24_STENCIL8)
  161. {
  162. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _depthStencilTarget->_stencilBuffer) );
  163. }
  164. // Restore the FBO binding
  165. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, currentFbo) );
  166. }
  167. }
  168. DepthStencilTarget* FrameBuffer::getDepthStencilTarget() const
  169. {
  170. return _depthStencilTarget;
  171. }
  172. void FrameBuffer::bind()
  173. {
  174. // Bind this FrameBuffer for rendering.
  175. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  176. }
  177. void FrameBuffer::bindDefault()
  178. {
  179. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, 0) );
  180. }
  181. }