2
0

CmGLFrameBufferObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGLFrameBufferObject.h"
  25. #include "CmGLPixelFormat.h"
  26. #include "CmGLHardwarePixelBuffer.h"
  27. #include "CmGLRenderTexture.h"
  28. namespace CamelotEngine {
  29. //-----------------------------------------------------------------------------
  30. GLFrameBufferObject::GLFrameBufferObject(GLRTTManager *manager, UINT32 fsaa):
  31. mManager(manager), mNumSamples(fsaa)
  32. {
  33. /// Generate framebuffer object
  34. glGenFramebuffersEXT(1, &mFB);
  35. // check multisampling
  36. if (GLEW_EXT_framebuffer_blit && GLEW_EXT_framebuffer_multisample)
  37. {
  38. // check samples supported
  39. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  40. GLint maxSamples;
  41. glGetIntegerv(GL_MAX_SAMPLES_EXT, &maxSamples);
  42. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  43. mNumSamples = std::min(mNumSamples, (GLsizei)maxSamples);
  44. }
  45. else
  46. {
  47. mNumSamples = 0;
  48. }
  49. // will we need a second FBO to do multisampling?
  50. if (mNumSamples)
  51. {
  52. glGenFramebuffersEXT(1, &mMultisampleFB);
  53. }
  54. else
  55. {
  56. mMultisampleFB = 0;
  57. }
  58. /// Initialise state
  59. mDepth.buffer=0;
  60. mStencil.buffer=0;
  61. for(UINT32 x=0; x<CM_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  62. {
  63. mColour[x].buffer=0;
  64. }
  65. }
  66. GLFrameBufferObject::~GLFrameBufferObject()
  67. {
  68. mManager->releaseRenderBuffer(mDepth);
  69. mManager->releaseRenderBuffer(mStencil);
  70. mManager->releaseRenderBuffer(mMultisampleColourBuffer);
  71. /// Delete framebuffer object
  72. glDeleteFramebuffersEXT(1, &mFB);
  73. if (mMultisampleFB)
  74. glDeleteFramebuffersEXT(1, &mMultisampleFB);
  75. }
  76. void GLFrameBufferObject::bindSurface(UINT32 attachment, const GLSurfaceDesc &target)
  77. {
  78. assert(attachment < CM_MAX_MULTIPLE_RENDER_TARGETS);
  79. mColour[attachment] = target;
  80. // Re-initialise
  81. if(mColour[0].buffer)
  82. initialise();
  83. }
  84. void GLFrameBufferObject::unbindSurface(UINT32 attachment)
  85. {
  86. assert(attachment < CM_MAX_MULTIPLE_RENDER_TARGETS);
  87. mColour[attachment].buffer = 0;
  88. // Re-initialise if buffer 0 still bound
  89. if(mColour[0].buffer)
  90. {
  91. initialise();
  92. }
  93. }
  94. void GLFrameBufferObject::initialise()
  95. {
  96. // Release depth and stencil, if they were bound
  97. mManager->releaseRenderBuffer(mDepth);
  98. mManager->releaseRenderBuffer(mStencil);
  99. mManager->releaseRenderBuffer(mMultisampleColourBuffer);
  100. /// First buffer must be bound
  101. if(!mColour[0].buffer)
  102. {
  103. CM_EXCEPT(InvalidParametersException,
  104. "Attachment 0 must have surface attached");
  105. }
  106. // If we're doing multisampling, then we need another FBO which contains a
  107. // renderbuffer which is set up to multisample, and we'll blit it to the final
  108. // FBO afterwards to perform the multisample resolve. In that case, the
  109. // mMultisampleFB is bound during rendering and is the one with a depth/stencil
  110. /// Store basic stats
  111. UINT32 width = mColour[0].buffer->getWidth();
  112. UINT32 height = mColour[0].buffer->getHeight();
  113. GLuint glformat = mColour[0].buffer->getGLFormat();
  114. PixelFormat format = mColour[0].buffer->getFormat();
  115. UINT16 maxSupportedMRTs = CamelotEngine::RenderSystem::instancePtr()->getCapabilities()->getNumMultiRenderTargets();
  116. // Bind simple buffer to add colour attachments
  117. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  118. /// Bind all attachment points to frame buffer
  119. for(UINT16 x=0; x<maxSupportedMRTs; ++x)
  120. {
  121. if(mColour[x].buffer)
  122. {
  123. if(mColour[x].buffer->getWidth() != width || mColour[x].buffer->getHeight() != height)
  124. {
  125. StringStream ss;
  126. ss << "Attachment " << x << " has incompatible size ";
  127. ss << mColour[x].buffer->getWidth() << "x" << mColour[x].buffer->getHeight();
  128. ss << ". It must be of the same as the size of surface 0, ";
  129. ss << width << "x" << height;
  130. ss << ".";
  131. CM_EXCEPT(InvalidParametersException, ss.str());
  132. }
  133. if(mColour[x].buffer->getGLFormat() != glformat)
  134. {
  135. StringStream ss;
  136. ss << "Attachment " << x << " has incompatible format.";
  137. CM_EXCEPT(InvalidParametersException, ss.str());
  138. }
  139. mColour[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT+x, mColour[x].zoffset);
  140. }
  141. else
  142. {
  143. // Detach
  144. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+x,
  145. GL_RENDERBUFFER_EXT, 0);
  146. }
  147. }
  148. // Now deal with depth / stencil
  149. if (mMultisampleFB)
  150. {
  151. // Bind multisample buffer
  152. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mMultisampleFB);
  153. // Create AA render buffer (colour)
  154. // note, this can be shared too because we blit it to the final FBO
  155. // right after the render is finished
  156. mMultisampleColourBuffer = mManager->requestRenderBuffer(glformat, width, height, mNumSamples);
  157. // Attach it, because we won't be attaching below and non-multisample has
  158. // actually been attached to other FBO
  159. mMultisampleColourBuffer.buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT,
  160. mMultisampleColourBuffer.zoffset);
  161. // depth & stencil will be dealt with below
  162. }
  163. /// Find suitable depth and stencil format that is compatible with colour format
  164. GLenum depthFormat, stencilFormat;
  165. mManager->getBestDepthStencil(format, &depthFormat, &stencilFormat);
  166. /// Request surfaces
  167. mDepth = mManager->requestRenderBuffer(depthFormat, width, height, mNumSamples);
  168. if (depthFormat == GL_DEPTH24_STENCIL8_EXT)
  169. {
  170. // bind same buffer to depth and stencil attachments
  171. mManager->requestRenderBuffer(mDepth);
  172. mStencil = mDepth;
  173. }
  174. else
  175. {
  176. // separate stencil
  177. mStencil = mManager->requestRenderBuffer(stencilFormat, width, height, mNumSamples);
  178. }
  179. /// Attach/detach surfaces
  180. if(mDepth.buffer)
  181. {
  182. mDepth.buffer->bindToFramebuffer(GL_DEPTH_ATTACHMENT_EXT, mDepth.zoffset);
  183. }
  184. else
  185. {
  186. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  187. GL_RENDERBUFFER_EXT, 0);
  188. }
  189. if(mStencil.buffer)
  190. {
  191. mStencil.buffer->bindToFramebuffer(GL_STENCIL_ATTACHMENT_EXT, mStencil.zoffset);
  192. }
  193. else
  194. {
  195. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  196. GL_RENDERBUFFER_EXT, 0);
  197. }
  198. /// Do glDrawBuffer calls
  199. GLenum bufs[CM_MAX_MULTIPLE_RENDER_TARGETS];
  200. GLsizei n=0;
  201. for(UINT32 x=0; x<CM_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  202. {
  203. // Fill attached colour buffers
  204. if(mColour[x].buffer)
  205. {
  206. bufs[x] = GL_COLOR_ATTACHMENT0_EXT + x;
  207. // Keep highest used buffer + 1
  208. n = x+1;
  209. }
  210. else
  211. {
  212. bufs[x] = GL_NONE;
  213. }
  214. }
  215. if(glDrawBuffers)
  216. {
  217. /// Drawbuffer extension supported, use it
  218. glDrawBuffers(n, bufs);
  219. }
  220. else
  221. {
  222. /// In this case, the capabilities will not show more than 1 simultaneaous render target.
  223. glDrawBuffer(bufs[0]);
  224. }
  225. if (mMultisampleFB)
  226. {
  227. // we need a read buffer because we'll be blitting to mFB
  228. glReadBuffer(bufs[0]);
  229. }
  230. else
  231. {
  232. /// No read buffer, by default, if we want to read anyway we must not forget to set this.
  233. glReadBuffer(GL_NONE);
  234. }
  235. /// Check status
  236. GLuint status;
  237. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  238. /// Bind main buffer
  239. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  240. switch(status)
  241. {
  242. case GL_FRAMEBUFFER_COMPLETE_EXT:
  243. // All is good
  244. break;
  245. case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
  246. CM_EXCEPT(InvalidParametersException,
  247. "All framebuffer formats with this texture internal format unsupported");
  248. default:
  249. CM_EXCEPT(InvalidParametersException,
  250. "Framebuffer incomplete or other FBO status error");
  251. }
  252. }
  253. void GLFrameBufferObject::bind()
  254. {
  255. /// Bind it to FBO
  256. if (mMultisampleFB)
  257. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mMultisampleFB);
  258. else
  259. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  260. }
  261. void GLFrameBufferObject::swapBuffers()
  262. {
  263. if (mMultisampleFB)
  264. {
  265. // blit from multisample buffer to final buffer, triggers resolve
  266. UINT32 width = mColour[0].buffer->getWidth();
  267. UINT32 height = mColour[0].buffer->getHeight();
  268. glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, mMultisampleFB);
  269. glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, mFB);
  270. glBlitFramebufferEXT(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  271. }
  272. }
  273. UINT32 GLFrameBufferObject::getWidth()
  274. {
  275. assert(mColour[0].buffer);
  276. return mColour[0].buffer->getWidth();
  277. }
  278. UINT32 GLFrameBufferObject::getHeight()
  279. {
  280. assert(mColour[0].buffer);
  281. return mColour[0].buffer->getHeight();
  282. }
  283. PixelFormat GLFrameBufferObject::getFormat()
  284. {
  285. assert(mColour[0].buffer);
  286. return mColour[0].buffer->getFormat();
  287. }
  288. }