BsGLFrameBufferObject.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "BsGLFrameBufferObject.h"
  2. #include "BsGLPixelFormat.h"
  3. #include "BsGLPixelBuffer.h"
  4. #include "BsGLRenderTexture.h"
  5. namespace BansheeEngine
  6. {
  7. GLFrameBufferObject::GLFrameBufferObject(UINT32 multisampleCount)
  8. :mNumSamples(multisampleCount)
  9. {
  10. /// Generate framebuffer object
  11. glGenFramebuffersEXT(1, &mFB);
  12. // check multisampling
  13. if (GLEW_EXT_framebuffer_blit && GLEW_EXT_framebuffer_multisample)
  14. {
  15. // check samples supported
  16. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  17. GLint maxSamples;
  18. glGetIntegerv(GL_MAX_SAMPLES_EXT, &maxSamples);
  19. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  20. mNumSamples = std::min(mNumSamples, (GLsizei)maxSamples);
  21. }
  22. else
  23. {
  24. mNumSamples = 0;
  25. }
  26. /// Initialise state
  27. for(UINT32 x = 0; x < BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  28. mColor[x].buffer = nullptr;
  29. }
  30. GLFrameBufferObject::~GLFrameBufferObject()
  31. {
  32. /// Delete framebuffer object
  33. glDeleteFramebuffersEXT(1, &mFB);
  34. }
  35. void GLFrameBufferObject::bindSurface(UINT32 attachment, const GLSurfaceDesc &target)
  36. {
  37. assert(attachment < BS_MAX_MULTIPLE_RENDER_TARGETS);
  38. mColor[attachment] = target;
  39. // Re-initialise
  40. if(mColor[0].buffer)
  41. initialize();
  42. }
  43. void GLFrameBufferObject::unbindSurface(UINT32 attachment)
  44. {
  45. assert(attachment < BS_MAX_MULTIPLE_RENDER_TARGETS);
  46. mColor[attachment].buffer = nullptr;
  47. // Re-initialise if buffer 0 still bound
  48. if(mColor[0].buffer)
  49. {
  50. initialize();
  51. }
  52. }
  53. void GLFrameBufferObject::bindDepthStencil(GLPixelBufferPtr depthStencilBuffer)
  54. {
  55. mDepthStencilBuffer = depthStencilBuffer;
  56. }
  57. void GLFrameBufferObject::unbindDepthStencil()
  58. {
  59. mDepthStencilBuffer = nullptr;
  60. }
  61. void GLFrameBufferObject::initialize()
  62. {
  63. /// First buffer must be bound
  64. if(!mColor[0].buffer)
  65. BS_EXCEPT(InvalidParametersException, "Attachment 0 must have surface attached");
  66. /// Store basic stats
  67. UINT32 width = mColor[0].buffer->getWidth();
  68. UINT32 height = mColor[0].buffer->getHeight();
  69. GLuint glformat = mColor[0].buffer->getGLFormat();
  70. PixelFormat format = mColor[0].buffer->getFormat();
  71. UINT16 maxSupportedMRTs = BansheeEngine::RenderSystem::instancePtr()->getCapabilities()->getNumMultiRenderTargets();
  72. // Bind simple buffer to add colour attachments
  73. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  74. /// Bind all attachment points to frame buffer
  75. for(UINT16 x = 0; x < maxSupportedMRTs; ++x)
  76. {
  77. if(mColor[x].buffer)
  78. {
  79. if(mColor[x].buffer->getWidth() != width || mColor[x].buffer->getHeight() != height)
  80. {
  81. StringStream ss;
  82. ss << "Attachment " << x << " has incompatible size ";
  83. ss << mColor[x].buffer->getWidth() << "x" << mColor[x].buffer->getHeight();
  84. ss << ". It must be of the same as the size of surface 0, ";
  85. ss << width << "x" << height;
  86. ss << ".";
  87. BS_EXCEPT(InvalidParametersException, ss.str());
  88. }
  89. if(mColor[x].buffer->getGLFormat() != glformat)
  90. {
  91. StringStream ss;
  92. ss << "Attachment " << x << " has incompatible format.";
  93. BS_EXCEPT(InvalidParametersException, ss.str());
  94. }
  95. mColor[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT+x, mColor[x].zoffset);
  96. }
  97. else
  98. {
  99. // Detach
  100. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+x,
  101. GL_RENDERBUFFER_EXT, 0);
  102. }
  103. }
  104. if(mDepthStencilBuffer != nullptr)
  105. mDepthStencilBuffer->bindToFramebuffer(GL_DEPTH_STENCIL_ATTACHMENT, 0);
  106. /// Do glDrawBuffer calls
  107. GLenum bufs[BS_MAX_MULTIPLE_RENDER_TARGETS];
  108. GLsizei n=0;
  109. for(UINT32 x=0; x<BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  110. {
  111. // Fill attached colour buffers
  112. if(mColor[x].buffer)
  113. {
  114. bufs[x] = GL_COLOR_ATTACHMENT0_EXT + x;
  115. // Keep highest used buffer + 1
  116. n = x+1;
  117. }
  118. else
  119. {
  120. bufs[x] = GL_NONE;
  121. }
  122. }
  123. if(glDrawBuffers)
  124. {
  125. /// Drawbuffer extension supported, use it
  126. glDrawBuffers(n, bufs);
  127. }
  128. else
  129. {
  130. /// In this case, the capabilities will not show more than 1 simultaneaous render target.
  131. glDrawBuffer(bufs[0]);
  132. }
  133. /// No read buffer, by default, if we want to read anyway we must not forget to set this.
  134. glReadBuffer(GL_NONE);
  135. /// Check status
  136. GLuint status;
  137. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  138. /// Bind main buffer
  139. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  140. switch(status)
  141. {
  142. case GL_FRAMEBUFFER_COMPLETE_EXT:
  143. // All is good
  144. break;
  145. case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
  146. BS_EXCEPT(InvalidParametersException,
  147. "All framebuffer formats with this texture internal format unsupported");
  148. default:
  149. BS_EXCEPT(InvalidParametersException,
  150. "Framebuffer incomplete or other FBO status error");
  151. }
  152. }
  153. void GLFrameBufferObject::bind()
  154. {
  155. /// Bind it to FBO
  156. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  157. }
  158. UINT32 GLFrameBufferObject::getWidth()
  159. {
  160. assert(mColor[0].buffer);
  161. return mColor[0].buffer->getWidth();
  162. }
  163. UINT32 GLFrameBufferObject::getHeight()
  164. {
  165. assert(mColor[0].buffer);
  166. return mColor[0].buffer->getHeight();
  167. }
  168. PixelFormat GLFrameBufferObject::getFormat()
  169. {
  170. assert(mColor[0].buffer);
  171. return mColor[0].buffer->getFormat();
  172. }
  173. }