BsGLFrameBufferObject.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // Note: I'm attaching textures to FBO while renderbuffers might yield better performance if I
  77. // don't need to read from them
  78. mColor[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT + x, mColor[x].zoffset);
  79. }
  80. else
  81. {
  82. // Detach
  83. glFramebufferTextureEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + x, 0, 0);
  84. }
  85. }
  86. if(mDepthStencilBuffer != nullptr)
  87. mDepthStencilBuffer->bindToFramebuffer(GL_DEPTH_STENCIL_ATTACHMENT, 0);
  88. // Do glDrawBuffer calls
  89. GLenum bufs[BS_MAX_MULTIPLE_RENDER_TARGETS];
  90. GLsizei n=0;
  91. for(UINT32 x=0; x<BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  92. {
  93. // Fill attached colour buffers
  94. if(mColor[x].buffer)
  95. {
  96. bufs[x] = GL_COLOR_ATTACHMENT0_EXT + x;
  97. // Keep highest used buffer + 1
  98. n = x+1;
  99. }
  100. else
  101. {
  102. bufs[x] = GL_NONE;
  103. }
  104. }
  105. if(glDrawBuffers)
  106. {
  107. // Drawbuffer extension supported, use it
  108. glDrawBuffers(n, bufs);
  109. }
  110. else
  111. {
  112. // In this case, the capabilities will not show more than 1 simultaneaous render target.
  113. glDrawBuffer(bufs[0]);
  114. }
  115. // No read buffer, by default, if we want to read anyway we must not forget to set this.
  116. glReadBuffer(GL_NONE);
  117. // Check status
  118. GLuint status;
  119. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  120. // Bind main buffer
  121. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  122. switch(status)
  123. {
  124. case GL_FRAMEBUFFER_COMPLETE_EXT:
  125. break;
  126. case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
  127. BS_EXCEPT(InvalidParametersException, "All framebuffer formats with this texture internal format unsupported");
  128. default:
  129. BS_EXCEPT(InvalidParametersException, "Framebuffer incomplete or other FBO status error");
  130. }
  131. }
  132. void GLFrameBufferObject::bind()
  133. {
  134. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  135. }
  136. }