BsGLFrameBufferObject.cpp 4.8 KB

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