CmGLFrameBufferObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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(size_t 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(size_t 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(size_t 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. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  105. "Attachment 0 must have surface attached",
  106. "GLFrameBufferObject::initialise");
  107. }
  108. // If we're doing multisampling, then we need another FBO which contains a
  109. // renderbuffer which is set up to multisample, and we'll blit it to the final
  110. // FBO afterwards to perform the multisample resolve. In that case, the
  111. // mMultisampleFB is bound during rendering and is the one with a depth/stencil
  112. /// Store basic stats
  113. size_t width = mColour[0].buffer->getWidth();
  114. size_t height = mColour[0].buffer->getHeight();
  115. GLuint format = mColour[0].buffer->getGLFormat();
  116. PixelFormat ogreFormat = mColour[0].buffer->getFormat();
  117. UINT16 maxSupportedMRTs = CamelotEngine::RenderSystemManager::getActive()->getCapabilities()->getNumMultiRenderTargets();
  118. // Bind simple buffer to add colour attachments
  119. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  120. /// Bind all attachment points to frame buffer
  121. for(size_t x=0; x<maxSupportedMRTs; ++x)
  122. {
  123. if(mColour[x].buffer)
  124. {
  125. if(mColour[x].buffer->getWidth() != width || mColour[x].buffer->getHeight() != height)
  126. {
  127. StringStream ss;
  128. ss << "Attachment " << x << " has incompatible size ";
  129. ss << mColour[x].buffer->getWidth() << "x" << mColour[x].buffer->getHeight();
  130. ss << ". It must be of the same as the size of surface 0, ";
  131. ss << width << "x" << height;
  132. ss << ".";
  133. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, ss.str(), "GLFrameBufferObject::initialise");
  134. }
  135. if(mColour[x].buffer->getGLFormat() != format)
  136. {
  137. StringStream ss;
  138. ss << "Attachment " << x << " has incompatible format.";
  139. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, ss.str(), "GLFrameBufferObject::initialise");
  140. }
  141. mColour[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT+x, mColour[x].zoffset);
  142. }
  143. else
  144. {
  145. // Detach
  146. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+x,
  147. GL_RENDERBUFFER_EXT, 0);
  148. }
  149. }
  150. // Now deal with depth / stencil
  151. if (mMultisampleFB)
  152. {
  153. // Bind multisample buffer
  154. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mMultisampleFB);
  155. // Create AA render buffer (colour)
  156. // note, this can be shared too because we blit it to the final FBO
  157. // right after the render is finished
  158. mMultisampleColourBuffer = mManager->requestRenderBuffer(format, width, height, mNumSamples);
  159. // Attach it, because we won't be attaching below and non-multisample has
  160. // actually been attached to other FBO
  161. mMultisampleColourBuffer.buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT,
  162. mMultisampleColourBuffer.zoffset);
  163. // depth & stencil will be dealt with below
  164. }
  165. /// Find suitable depth and stencil format that is compatible with colour format
  166. GLenum depthFormat, stencilFormat;
  167. mManager->getBestDepthStencil(ogreFormat, &depthFormat, &stencilFormat);
  168. /// Request surfaces
  169. mDepth = mManager->requestRenderBuffer(depthFormat, width, height, mNumSamples);
  170. if (depthFormat == GL_DEPTH24_STENCIL8_EXT)
  171. {
  172. // bind same buffer to depth and stencil attachments
  173. mManager->requestRenderBuffer(mDepth);
  174. mStencil = mDepth;
  175. }
  176. else
  177. {
  178. // separate stencil
  179. mStencil = mManager->requestRenderBuffer(stencilFormat, width, height, mNumSamples);
  180. }
  181. /// Attach/detach surfaces
  182. if(mDepth.buffer)
  183. {
  184. mDepth.buffer->bindToFramebuffer(GL_DEPTH_ATTACHMENT_EXT, mDepth.zoffset);
  185. }
  186. else
  187. {
  188. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  189. GL_RENDERBUFFER_EXT, 0);
  190. }
  191. if(mStencil.buffer)
  192. {
  193. mStencil.buffer->bindToFramebuffer(GL_STENCIL_ATTACHMENT_EXT, mStencil.zoffset);
  194. }
  195. else
  196. {
  197. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  198. GL_RENDERBUFFER_EXT, 0);
  199. }
  200. /// Do glDrawBuffer calls
  201. GLenum bufs[CM_MAX_MULTIPLE_RENDER_TARGETS];
  202. GLsizei n=0;
  203. for(size_t x=0; x<CM_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  204. {
  205. // Fill attached colour buffers
  206. if(mColour[x].buffer)
  207. {
  208. bufs[x] = GL_COLOR_ATTACHMENT0_EXT + x;
  209. // Keep highest used buffer + 1
  210. n = x+1;
  211. }
  212. else
  213. {
  214. bufs[x] = GL_NONE;
  215. }
  216. }
  217. if(glDrawBuffers)
  218. {
  219. /// Drawbuffer extension supported, use it
  220. glDrawBuffers(n, bufs);
  221. }
  222. else
  223. {
  224. /// In this case, the capabilities will not show more than 1 simultaneaous render target.
  225. glDrawBuffer(bufs[0]);
  226. }
  227. if (mMultisampleFB)
  228. {
  229. // we need a read buffer because we'll be blitting to mFB
  230. glReadBuffer(bufs[0]);
  231. }
  232. else
  233. {
  234. /// No read buffer, by default, if we want to read anyway we must not forget to set this.
  235. glReadBuffer(GL_NONE);
  236. }
  237. /// Check status
  238. GLuint status;
  239. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  240. /// Bind main buffer
  241. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  242. switch(status)
  243. {
  244. case GL_FRAMEBUFFER_COMPLETE_EXT:
  245. // All is good
  246. break;
  247. case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
  248. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  249. "All framebuffer formats with this texture internal format unsupported",
  250. "GLFrameBufferObject::initialise");
  251. default:
  252. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  253. "Framebuffer incomplete or other FBO status error",
  254. "GLFrameBufferObject::initialise");
  255. }
  256. }
  257. void GLFrameBufferObject::bind()
  258. {
  259. /// Bind it to FBO
  260. if (mMultisampleFB)
  261. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mMultisampleFB);
  262. else
  263. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  264. }
  265. void GLFrameBufferObject::swapBuffers()
  266. {
  267. if (mMultisampleFB)
  268. {
  269. // blit from multisample buffer to final buffer, triggers resolve
  270. size_t width = mColour[0].buffer->getWidth();
  271. size_t height = mColour[0].buffer->getHeight();
  272. glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, mMultisampleFB);
  273. glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, mFB);
  274. glBlitFramebufferEXT(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  275. }
  276. }
  277. size_t GLFrameBufferObject::getWidth()
  278. {
  279. assert(mColour[0].buffer);
  280. return mColour[0].buffer->getWidth();
  281. }
  282. size_t GLFrameBufferObject::getHeight()
  283. {
  284. assert(mColour[0].buffer);
  285. return mColour[0].buffer->getHeight();
  286. }
  287. PixelFormat GLFrameBufferObject::getFormat()
  288. {
  289. assert(mColour[0].buffer);
  290. return mColour[0].buffer->getFormat();
  291. }
  292. //-----------------------------------------------------------------------------
  293. }