BsGLFrameBufferObject.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. glGenFramebuffers(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. glDeleteFramebuffers(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. if (mColor[0].buffer)
  38. rebuild();
  39. }
  40. void GLFrameBufferObject::unbindDepthStencil()
  41. {
  42. mDepthStencilBuffer = nullptr;
  43. }
  44. void GLFrameBufferObject::rebuild()
  45. {
  46. // First buffer must be bound
  47. if(!mColor[0].buffer)
  48. BS_EXCEPT(InvalidParametersException, "Attachment 0 must have surface attached");
  49. // Store basic stats
  50. UINT32 width = mColor[0].buffer->getWidth();
  51. UINT32 height = mColor[0].buffer->getHeight();
  52. GLuint glformat = mColor[0].buffer->getGLFormat();
  53. PixelFormat format = mColor[0].buffer->getFormat();
  54. UINT16 maxSupportedMRTs = BansheeEngine::RenderAPICore::instancePtr()->getCapabilities()->getNumMultiRenderTargets();
  55. // Bind simple buffer to add color attachments
  56. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  57. // Bind all attachment points to frame buffer
  58. for(UINT16 x = 0; x < maxSupportedMRTs; ++x)
  59. {
  60. if(mColor[x].buffer)
  61. {
  62. if(mColor[x].buffer->getWidth() != width || mColor[x].buffer->getHeight() != height)
  63. {
  64. StringStream ss;
  65. ss << "Attachment " << x << " has incompatible size ";
  66. ss << mColor[x].buffer->getWidth() << "x" << mColor[x].buffer->getHeight();
  67. ss << ". It must be of the same as the size of surface 0, ";
  68. ss << width << "x" << height;
  69. ss << ".";
  70. BS_EXCEPT(InvalidParametersException, ss.str());
  71. }
  72. if(mColor[x].buffer->getGLFormat() != glformat)
  73. {
  74. StringStream ss;
  75. ss << "Attachment " << x << " has incompatible format.";
  76. BS_EXCEPT(InvalidParametersException, ss.str());
  77. }
  78. // Note: I'm attaching textures to FBO while renderbuffers might yield better performance if I
  79. // don't need to read from them
  80. mColor[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0 + x, mColor[x].zoffset);
  81. }
  82. else
  83. {
  84. // Detach
  85. glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + x, 0, 0);
  86. }
  87. }
  88. if(mDepthStencilBuffer != nullptr)
  89. mDepthStencilBuffer->bindToFramebuffer(GL_DEPTH_STENCIL_ATTACHMENT, 0);
  90. // Do glDrawBuffer calls
  91. GLenum bufs[BS_MAX_MULTIPLE_RENDER_TARGETS];
  92. GLsizei n = 0;
  93. for(UINT32 x = 0; x < BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  94. {
  95. // Fill attached colour buffers
  96. if(mColor[x].buffer)
  97. {
  98. bufs[x] = GL_COLOR_ATTACHMENT0 + x;
  99. // Keep highest used buffer + 1
  100. n = x+1;
  101. }
  102. else
  103. {
  104. bufs[x] = GL_NONE;
  105. }
  106. }
  107. glDrawBuffers(n, bufs);
  108. // No read buffer, by default, if we want to read anyway we must not forget to set this.
  109. glReadBuffer(GL_NONE);
  110. // Check status
  111. GLuint status;
  112. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  113. // Bind main buffer
  114. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  115. switch(status)
  116. {
  117. case GL_FRAMEBUFFER_COMPLETE:
  118. break;
  119. case GL_FRAMEBUFFER_UNSUPPORTED:
  120. BS_EXCEPT(InvalidParametersException, "All framebuffer formats with this texture internal format unsupported");
  121. default:
  122. BS_EXCEPT(InvalidParametersException, "Framebuffer incomplete or other FBO status error");
  123. }
  124. }
  125. void GLFrameBufferObject::bind()
  126. {
  127. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  128. }
  129. }