CmGLFrameBufferObject.cpp 11 KB

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