CmGLFrameBufferObject.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "CmGLPixelBuffer.h"
  27. #include "CmGLRenderTexture.h"
  28. namespace CamelotFramework
  29. {
  30. GLFrameBufferObject::GLFrameBufferObject(UINT32 fsaa)
  31. :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. /// Initialise state
  50. for(UINT32 x = 0; x < CM_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  51. mColor[x].buffer = nullptr;
  52. }
  53. GLFrameBufferObject::~GLFrameBufferObject()
  54. {
  55. /// Delete framebuffer object
  56. glDeleteFramebuffersEXT(1, &mFB);
  57. }
  58. void GLFrameBufferObject::bindSurface(UINT32 attachment, const GLSurfaceDesc &target)
  59. {
  60. assert(attachment < CM_MAX_MULTIPLE_RENDER_TARGETS);
  61. mColor[attachment] = target;
  62. // Re-initialise
  63. if(mColor[0].buffer)
  64. initialise();
  65. }
  66. void GLFrameBufferObject::unbindSurface(UINT32 attachment)
  67. {
  68. assert(attachment < CM_MAX_MULTIPLE_RENDER_TARGETS);
  69. mColor[attachment].buffer = nullptr;
  70. // Re-initialise if buffer 0 still bound
  71. if(mColor[0].buffer)
  72. {
  73. initialise();
  74. }
  75. }
  76. void GLFrameBufferObject::bindDepthStencil(GLPixelBufferPtr depthStencilBuffer)
  77. {
  78. mDepthStencilBuffer = depthStencilBuffer;
  79. }
  80. void GLFrameBufferObject::unbindDepthStencil()
  81. {
  82. mDepthStencilBuffer = nullptr;
  83. }
  84. void GLFrameBufferObject::initialise()
  85. {
  86. /// First buffer must be bound
  87. if(!mColor[0].buffer)
  88. CM_EXCEPT(InvalidParametersException, "Attachment 0 must have surface attached");
  89. /// Store basic stats
  90. UINT32 width = mColor[0].buffer->getWidth();
  91. UINT32 height = mColor[0].buffer->getHeight();
  92. GLuint glformat = mColor[0].buffer->getGLFormat();
  93. PixelFormat format = mColor[0].buffer->getFormat();
  94. UINT16 maxSupportedMRTs = CamelotFramework::RenderSystem::instancePtr()->getCapabilities()->getNumMultiRenderTargets();
  95. // Bind simple buffer to add colour attachments
  96. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  97. /// Bind all attachment points to frame buffer
  98. for(UINT16 x = 0; x < maxSupportedMRTs; ++x)
  99. {
  100. if(mColor[x].buffer)
  101. {
  102. if(mColor[x].buffer->getWidth() != width || mColor[x].buffer->getHeight() != height)
  103. {
  104. StringStream ss;
  105. ss << "Attachment " << x << " has incompatible size ";
  106. ss << mColor[x].buffer->getWidth() << "x" << mColor[x].buffer->getHeight();
  107. ss << ". It must be of the same as the size of surface 0, ";
  108. ss << width << "x" << height;
  109. ss << ".";
  110. CM_EXCEPT(InvalidParametersException, ss.str());
  111. }
  112. if(mColor[x].buffer->getGLFormat() != glformat)
  113. {
  114. StringStream ss;
  115. ss << "Attachment " << x << " has incompatible format.";
  116. CM_EXCEPT(InvalidParametersException, ss.str());
  117. }
  118. mColor[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT+x, mColor[x].zoffset);
  119. }
  120. else
  121. {
  122. // Detach
  123. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+x,
  124. GL_RENDERBUFFER_EXT, 0);
  125. }
  126. }
  127. if(mDepthStencilBuffer != nullptr)
  128. mDepthStencilBuffer->bindToFramebuffer(GL_DEPTH_STENCIL_ATTACHMENT, 0);
  129. /// Do glDrawBuffer calls
  130. GLenum bufs[CM_MAX_MULTIPLE_RENDER_TARGETS];
  131. GLsizei n=0;
  132. for(UINT32 x=0; x<CM_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  133. {
  134. // Fill attached colour buffers
  135. if(mColor[x].buffer)
  136. {
  137. bufs[x] = GL_COLOR_ATTACHMENT0_EXT + x;
  138. // Keep highest used buffer + 1
  139. n = x+1;
  140. }
  141. else
  142. {
  143. bufs[x] = GL_NONE;
  144. }
  145. }
  146. if(glDrawBuffers)
  147. {
  148. /// Drawbuffer extension supported, use it
  149. glDrawBuffers(n, bufs);
  150. }
  151. else
  152. {
  153. /// In this case, the capabilities will not show more than 1 simultaneaous render target.
  154. glDrawBuffer(bufs[0]);
  155. }
  156. /// No read buffer, by default, if we want to read anyway we must not forget to set this.
  157. glReadBuffer(GL_NONE);
  158. /// Check status
  159. GLuint status;
  160. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  161. /// Bind main buffer
  162. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  163. switch(status)
  164. {
  165. case GL_FRAMEBUFFER_COMPLETE_EXT:
  166. // All is good
  167. break;
  168. case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
  169. CM_EXCEPT(InvalidParametersException,
  170. "All framebuffer formats with this texture internal format unsupported");
  171. default:
  172. CM_EXCEPT(InvalidParametersException,
  173. "Framebuffer incomplete or other FBO status error");
  174. }
  175. }
  176. void GLFrameBufferObject::bind()
  177. {
  178. /// Bind it to FBO
  179. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  180. }
  181. UINT32 GLFrameBufferObject::getWidth()
  182. {
  183. assert(mColor[0].buffer);
  184. return mColor[0].buffer->getWidth();
  185. }
  186. UINT32 GLFrameBufferObject::getHeight()
  187. {
  188. assert(mColor[0].buffer);
  189. return mColor[0].buffer->getHeight();
  190. }
  191. PixelFormat GLFrameBufferObject::getFormat()
  192. {
  193. assert(mColor[0].buffer);
  194. return mColor[0].buffer->getFormat();
  195. }
  196. }