BsGLFrameBufferObject.cpp 4.6 KB

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