CmGLFrameBufferObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "CmGLFBORenderTexture.h"
  28. #include "CmRenderSystemManager.h"
  29. namespace CamelotEngine {
  30. //-----------------------------------------------------------------------------
  31. GLFrameBufferObject::GLFrameBufferObject(GLFBOManager *manager, UINT32 fsaa):
  32. mManager(manager), mNumSamples(fsaa)
  33. {
  34. /// Generate framebuffer object
  35. glGenFramebuffersEXT(1, &mFB);
  36. // check multisampling
  37. if (GLEW_EXT_framebuffer_blit && GLEW_EXT_framebuffer_multisample)
  38. {
  39. // check samples supported
  40. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  41. GLint maxSamples;
  42. glGetIntegerv(GL_MAX_SAMPLES_EXT, &maxSamples);
  43. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  44. mNumSamples = std::min(mNumSamples, (GLsizei)maxSamples);
  45. }
  46. else
  47. {
  48. mNumSamples = 0;
  49. }
  50. // will we need a second FBO to do multisampling?
  51. if (mNumSamples)
  52. {
  53. glGenFramebuffersEXT(1, &mMultisampleFB);
  54. }
  55. else
  56. {
  57. mMultisampleFB = 0;
  58. }
  59. /// Initialise state
  60. mDepth.buffer=0;
  61. mStencil.buffer=0;
  62. for(UINT32 x=0; x<CM_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  63. {
  64. mColour[x].buffer=0;
  65. }
  66. }
  67. GLFrameBufferObject::~GLFrameBufferObject()
  68. {
  69. mManager->releaseRenderBuffer(mDepth);
  70. mManager->releaseRenderBuffer(mStencil);
  71. mManager->releaseRenderBuffer(mMultisampleColourBuffer);
  72. /// Delete framebuffer object
  73. glDeleteFramebuffersEXT(1, &mFB);
  74. if (mMultisampleFB)
  75. glDeleteFramebuffersEXT(1, &mMultisampleFB);
  76. }
  77. void GLFrameBufferObject::bindSurface(UINT32 attachment, const GLSurfaceDesc &target)
  78. {
  79. assert(attachment < CM_MAX_MULTIPLE_RENDER_TARGETS);
  80. mColour[attachment] = target;
  81. // Re-initialise
  82. if(mColour[0].buffer)
  83. initialise();
  84. }
  85. void GLFrameBufferObject::unbindSurface(UINT32 attachment)
  86. {
  87. assert(attachment < CM_MAX_MULTIPLE_RENDER_TARGETS);
  88. mColour[attachment].buffer = 0;
  89. // Re-initialise if buffer 0 still bound
  90. if(mColour[0].buffer)
  91. {
  92. initialise();
  93. }
  94. }
  95. void GLFrameBufferObject::initialise()
  96. {
  97. // Release depth and stencil, if they were bound
  98. mManager->releaseRenderBuffer(mDepth);
  99. mManager->releaseRenderBuffer(mStencil);
  100. mManager->releaseRenderBuffer(mMultisampleColourBuffer);
  101. /// First buffer must be bound
  102. if(!mColour[0].buffer)
  103. {
  104. CM_EXCEPT(InvalidParametersException,
  105. "Attachment 0 must have surface attached");
  106. }
  107. // If we're doing multisampling, then we need another FBO which contains a
  108. // renderbuffer which is set up to multisample, and we'll blit it to the final
  109. // FBO afterwards to perform the multisample resolve. In that case, the
  110. // mMultisampleFB is bound during rendering and is the one with a depth/stencil
  111. /// Store basic stats
  112. UINT32 width = mColour[0].buffer->getWidth();
  113. UINT32 height = mColour[0].buffer->getHeight();
  114. GLuint format = mColour[0].buffer->getGLFormat();
  115. PixelFormat ogreFormat = mColour[0].buffer->getFormat();
  116. UINT16 maxSupportedMRTs = CamelotEngine::RenderSystemManager::getActive()->getCapabilities_internal()->getNumMultiRenderTargets();
  117. // Bind simple buffer to add colour attachments
  118. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  119. /// Bind all attachment points to frame buffer
  120. for(UINT16 x=0; x<maxSupportedMRTs; ++x)
  121. {
  122. if(mColour[x].buffer)
  123. {
  124. if(mColour[x].buffer->getWidth() != width || mColour[x].buffer->getHeight() != height)
  125. {
  126. StringStream ss;
  127. ss << "Attachment " << x << " has incompatible size ";
  128. ss << mColour[x].buffer->getWidth() << "x" << mColour[x].buffer->getHeight();
  129. ss << ". It must be of the same as the size of surface 0, ";
  130. ss << width << "x" << height;
  131. ss << ".";
  132. CM_EXCEPT(InvalidParametersException, ss.str());
  133. }
  134. if(mColour[x].buffer->getGLFormat() != format)
  135. {
  136. StringStream ss;
  137. ss << "Attachment " << x << " has incompatible format.";
  138. CM_EXCEPT(InvalidParametersException, ss.str());
  139. }
  140. mColour[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT+x, mColour[x].zoffset);
  141. }
  142. else
  143. {
  144. // Detach
  145. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+x,
  146. GL_RENDERBUFFER_EXT, 0);
  147. }
  148. }
  149. // Now deal with depth / stencil
  150. if (mMultisampleFB)
  151. {
  152. // Bind multisample buffer
  153. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mMultisampleFB);
  154. // Create AA render buffer (colour)
  155. // note, this can be shared too because we blit it to the final FBO
  156. // right after the render is finished
  157. mMultisampleColourBuffer = mManager->requestRenderBuffer(format, width, height, mNumSamples);
  158. // Attach it, because we won't be attaching below and non-multisample has
  159. // actually been attached to other FBO
  160. mMultisampleColourBuffer.buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT,
  161. mMultisampleColourBuffer.zoffset);
  162. // depth & stencil will be dealt with below
  163. }
  164. /// Find suitable depth and stencil format that is compatible with colour format
  165. GLenum depthFormat, stencilFormat;
  166. mManager->getBestDepthStencil(ogreFormat, &depthFormat, &stencilFormat);
  167. /// Request surfaces
  168. mDepth = mManager->requestRenderBuffer(depthFormat, width, height, mNumSamples);
  169. if (depthFormat == GL_DEPTH24_STENCIL8_EXT)
  170. {
  171. // bind same buffer to depth and stencil attachments
  172. mManager->requestRenderBuffer(mDepth);
  173. mStencil = mDepth;
  174. }
  175. else
  176. {
  177. // separate stencil
  178. mStencil = mManager->requestRenderBuffer(stencilFormat, width, height, mNumSamples);
  179. }
  180. /// Attach/detach surfaces
  181. if(mDepth.buffer)
  182. {
  183. mDepth.buffer->bindToFramebuffer(GL_DEPTH_ATTACHMENT_EXT, mDepth.zoffset);
  184. }
  185. else
  186. {
  187. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  188. GL_RENDERBUFFER_EXT, 0);
  189. }
  190. if(mStencil.buffer)
  191. {
  192. mStencil.buffer->bindToFramebuffer(GL_STENCIL_ATTACHMENT_EXT, mStencil.zoffset);
  193. }
  194. else
  195. {
  196. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  197. GL_RENDERBUFFER_EXT, 0);
  198. }
  199. /// Do glDrawBuffer calls
  200. GLenum bufs[CM_MAX_MULTIPLE_RENDER_TARGETS];
  201. GLsizei n=0;
  202. for(UINT32 x=0; x<CM_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  203. {
  204. // Fill attached colour buffers
  205. if(mColour[x].buffer)
  206. {
  207. bufs[x] = GL_COLOR_ATTACHMENT0_EXT + x;
  208. // Keep highest used buffer + 1
  209. n = x+1;
  210. }
  211. else
  212. {
  213. bufs[x] = GL_NONE;
  214. }
  215. }
  216. if(glDrawBuffers)
  217. {
  218. /// Drawbuffer extension supported, use it
  219. glDrawBuffers(n, bufs);
  220. }
  221. else
  222. {
  223. /// In this case, the capabilities will not show more than 1 simultaneaous render target.
  224. glDrawBuffer(bufs[0]);
  225. }
  226. if (mMultisampleFB)
  227. {
  228. // we need a read buffer because we'll be blitting to mFB
  229. glReadBuffer(bufs[0]);
  230. }
  231. else
  232. {
  233. /// No read buffer, by default, if we want to read anyway we must not forget to set this.
  234. glReadBuffer(GL_NONE);
  235. }
  236. /// Check status
  237. GLuint status;
  238. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  239. /// Bind main buffer
  240. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  241. switch(status)
  242. {
  243. case GL_FRAMEBUFFER_COMPLETE_EXT:
  244. // All is good
  245. break;
  246. case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
  247. CM_EXCEPT(InvalidParametersException,
  248. "All framebuffer formats with this texture internal format unsupported");
  249. default:
  250. CM_EXCEPT(InvalidParametersException,
  251. "Framebuffer incomplete or other FBO status error");
  252. }
  253. }
  254. void GLFrameBufferObject::bind()
  255. {
  256. /// Bind it to FBO
  257. if (mMultisampleFB)
  258. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mMultisampleFB);
  259. else
  260. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  261. }
  262. void GLFrameBufferObject::swapBuffers()
  263. {
  264. if (mMultisampleFB)
  265. {
  266. // blit from multisample buffer to final buffer, triggers resolve
  267. UINT32 width = mColour[0].buffer->getWidth();
  268. UINT32 height = mColour[0].buffer->getHeight();
  269. glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, mMultisampleFB);
  270. glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, mFB);
  271. glBlitFramebufferEXT(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  272. }
  273. }
  274. UINT32 GLFrameBufferObject::getWidth()
  275. {
  276. assert(mColour[0].buffer);
  277. return mColour[0].buffer->getWidth();
  278. }
  279. UINT32 GLFrameBufferObject::getHeight()
  280. {
  281. assert(mColour[0].buffer);
  282. return mColour[0].buffer->getHeight();
  283. }
  284. PixelFormat GLFrameBufferObject::getFormat()
  285. {
  286. assert(mColour[0].buffer);
  287. return mColour[0].buffer->getFormat();
  288. }
  289. //-----------------------------------------------------------------------------
  290. }