FrameBuffer.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. FrameBuffer::FrameBuffer(const char* id, unsigned int width, unsigned int height) :
  12. _id(id ? id : ""), _width(width), _height(height), _handle(0),
  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. SAFE_RELEASE(_renderTargets[i]);
  23. }
  24. SAFE_DELETE_ARRAY(_renderTargets);
  25. }
  26. if (_depthStencilTarget)
  27. {
  28. SAFE_RELEASE(_depthStencilTarget);
  29. }
  30. // Release GL resource.
  31. if (_handle)
  32. {
  33. GL_ASSERT( glDeleteFramebuffers(1, &_handle) );
  34. }
  35. // Remove self from vector.
  36. std::vector<FrameBuffer*>::iterator it = std::find(__frameBuffers.begin(), __frameBuffers.end(), this);
  37. if (it != __frameBuffers.end())
  38. {
  39. __frameBuffers.erase(it);
  40. }
  41. }
  42. void FrameBuffer::initialize()
  43. {
  44. GLint fbo;
  45. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &fbo);
  46. __defaultHandle = (FrameBufferHandle)fbo;
  47. }
  48. FrameBuffer* FrameBuffer::create(const char* id, unsigned int width, unsigned int height)
  49. {
  50. if (!isPowerOfTwo(width) | !isPowerOfTwo(height))
  51. {
  52. GP_ERROR("Failed to create render target for frame buffer. Width and Height must be a power of 2.");
  53. return NULL;
  54. }
  55. // Call getMaxRenderTargets() to force __maxRenderTargets to be set
  56. getMaxRenderTargets();
  57. // Create RenderTarget with same ID.
  58. RenderTarget* renderTarget = NULL;
  59. renderTarget = RenderTarget::create(id, width, height);
  60. if (renderTarget == NULL)
  61. {
  62. GP_ERROR("Failed to create render target for frame buffer.");
  63. return NULL;
  64. }
  65. // Create the frame buffer
  66. GLuint handle = 0;
  67. GL_ASSERT( glGenFramebuffers(1, &handle) );
  68. FrameBuffer* frameBuffer = new FrameBuffer(id, width, height);
  69. frameBuffer->_handle = handle;
  70. // Create the render target array for the new frame buffer
  71. RenderTarget** renderTargets = new RenderTarget*[__maxRenderTargets];
  72. memset(renderTargets, 0, sizeof(RenderTarget*) * __maxRenderTargets);
  73. frameBuffer->_renderTargets = renderTargets;
  74. frameBuffer->setRenderTarget(renderTarget, 0);
  75. __frameBuffers.push_back(frameBuffer);
  76. return frameBuffer;
  77. }
  78. FrameBuffer* FrameBuffer::getFrameBuffer(const char* id)
  79. {
  80. GP_ASSERT(id);
  81. // Search the vector for a matching ID.
  82. std::vector<FrameBuffer*>::const_iterator it;
  83. for (it = __frameBuffers.begin(); it < __frameBuffers.end(); it++)
  84. {
  85. FrameBuffer* fb = *it;
  86. GP_ASSERT(fb);
  87. if (strcmp(id, fb->getID()) == 0)
  88. {
  89. return fb;
  90. }
  91. }
  92. return NULL;
  93. }
  94. const char* FrameBuffer::getID() const
  95. {
  96. return _id.c_str();
  97. }
  98. unsigned int FrameBuffer::getWidth() const
  99. {
  100. return _width;
  101. }
  102. unsigned int FrameBuffer::getHeight() const
  103. {
  104. return _height;
  105. }
  106. unsigned int FrameBuffer::getMaxRenderTargets()
  107. {
  108. if (__maxRenderTargets == 0)
  109. {
  110. #ifdef GL_MAX_COLOR_ATTACHMENTS
  111. GLint val;
  112. GL_ASSERT( glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &val) );
  113. __maxRenderTargets = (unsigned int) val;
  114. #else
  115. __maxRenderTargets = 1;
  116. #endif
  117. }
  118. return __maxRenderTargets;
  119. }
  120. void FrameBuffer::setRenderTarget(RenderTarget* target, unsigned int index)
  121. {
  122. GP_ASSERT(index < __maxRenderTargets);
  123. GP_ASSERT(_renderTargets);
  124. if (_renderTargets[index] == target)
  125. {
  126. // No change.
  127. return;
  128. }
  129. // Release our reference to the current RenderTarget at this index.
  130. SAFE_RELEASE(_renderTargets[index]);
  131. _renderTargets[index] = target;
  132. if (target)
  133. {
  134. // This FrameBuffer now references the RenderTarget.
  135. target->addRef();
  136. // Store the current FBO binding so we can restore it
  137. GLint currentFbo;
  138. GL_ASSERT( glGetIntegerv(GL_FRAMEBUFFER_BINDING, &currentFbo) );
  139. // Now set this target as the color attachment corresponding to index.
  140. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  141. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _renderTargets[index]->getTexture()->getHandle()) );
  142. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) );
  143. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) );
  144. GL_ASSERT( glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) );
  145. GL_ASSERT( glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) );
  146. GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _width, _height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL) );
  147. GLenum attachment = GL_COLOR_ATTACHMENT0 + index;
  148. GP_ASSERT( _renderTargets[index]->getTexture() );
  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 incompleted: 0x%x", fboStatus);
  154. }
  155. // Restore the FBO binding
  156. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, currentFbo) );
  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. // Store the current FBO binding so we can restore it.
  180. GLint currentFbo;
  181. GL_ASSERT( glGetIntegerv(GL_FRAMEBUFFER_BINDING, &currentFbo) );
  182. // Now set this target as the color attachment corresponding to index.
  183. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  184. // Create a render buffer
  185. RenderBufferHandle renderBuffer = 0;
  186. GL_ASSERT( glGenRenderbuffers(1, &renderBuffer) );
  187. GL_ASSERT( glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer) );
  188. GL_ASSERT( glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, _width, _height) );
  189. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderBuffer) );
  190. // Attach the
  191. if (target->getFormat() == DepthStencilTarget::DEPTH_STENCIL)
  192. {
  193. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBuffer) );
  194. }
  195. // Check the framebuffer is good to go.
  196. GLenum fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  197. if (fboStatus != GL_FRAMEBUFFER_COMPLETE)
  198. {
  199. GP_ERROR("Framebuffer status incompleted: 0x%x", fboStatus);
  200. }
  201. _depthStencilTarget->_renderBuffer = renderBuffer;
  202. // Restore the FBO binding
  203. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, currentFbo) );
  204. }
  205. }
  206. DepthStencilTarget* FrameBuffer::getDepthStencilTarget() const
  207. {
  208. return _depthStencilTarget;
  209. }
  210. void FrameBuffer::bind()
  211. {
  212. // Bind this FrameBuffer for rendering.
  213. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
  214. }
  215. void FrameBuffer::bindDefault()
  216. {
  217. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, __defaultHandle) );
  218. }
  219. bool FrameBuffer::isPowerOfTwo(unsigned int value)
  220. {
  221. return (value != 0) && ((value & (value - 1)) == 0);
  222. }
  223. }