BsGLFrameBufferObject.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGLFrameBufferObject.h"
  4. #include "BsGLPixelFormat.h"
  5. #include "BsGLPixelBuffer.h"
  6. #include "BsGLRenderTexture.h"
  7. #include "BsRenderStats.h"
  8. namespace BansheeEngine
  9. {
  10. GLFrameBufferObject::GLFrameBufferObject()
  11. {
  12. glGenFramebuffers(1, &mFB);
  13. for(UINT32 x = 0; x < BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  14. mColor[x].buffer = nullptr;
  15. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_FrameBufferObject);
  16. }
  17. GLFrameBufferObject::~GLFrameBufferObject()
  18. {
  19. glDeleteFramebuffers(1, &mFB);
  20. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_FrameBufferObject);
  21. }
  22. void GLFrameBufferObject::bindSurface(UINT32 attachment, const GLSurfaceDesc &target)
  23. {
  24. assert(attachment < BS_MAX_MULTIPLE_RENDER_TARGETS);
  25. mColor[attachment] = target;
  26. if(mColor[0].buffer)
  27. rebuild();
  28. }
  29. void GLFrameBufferObject::unbindSurface(UINT32 attachment)
  30. {
  31. assert(attachment < BS_MAX_MULTIPLE_RENDER_TARGETS);
  32. mColor[attachment].buffer = nullptr;
  33. if(mColor[0].buffer)
  34. rebuild();
  35. }
  36. void GLFrameBufferObject::bindDepthStencil(GLPixelBufferPtr depthStencilBuffer)
  37. {
  38. mDepthStencilBuffer = depthStencilBuffer;
  39. if (mColor[0].buffer)
  40. rebuild();
  41. }
  42. void GLFrameBufferObject::unbindDepthStencil()
  43. {
  44. mDepthStencilBuffer = nullptr;
  45. }
  46. void GLFrameBufferObject::rebuild()
  47. {
  48. // First buffer must be bound
  49. if(!mColor[0].buffer)
  50. BS_EXCEPT(InvalidParametersException, "Attachment 0 must have surface attached");
  51. // Store basic stats
  52. UINT32 width = mColor[0].buffer->getWidth();
  53. UINT32 height = mColor[0].buffer->getHeight();
  54. GLuint glformat = mColor[0].buffer->getGLFormat();
  55. PixelFormat format = mColor[0].buffer->getFormat();
  56. UINT16 maxSupportedMRTs = BansheeEngine::RenderAPICore::instancePtr()->getCapabilities()->getNumMultiRenderTargets();
  57. // Bind simple buffer to add color attachments
  58. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  59. // Bind all attachment points to frame buffer
  60. for(UINT16 x = 0; x < maxSupportedMRTs; ++x)
  61. {
  62. if(mColor[x].buffer)
  63. {
  64. if(mColor[x].buffer->getWidth() != width || mColor[x].buffer->getHeight() != height)
  65. {
  66. StringStream ss;
  67. ss << "Attachment " << x << " has incompatible size ";
  68. ss << mColor[x].buffer->getWidth() << "x" << mColor[x].buffer->getHeight();
  69. ss << ". It must be of the same as the size of surface 0, ";
  70. ss << width << "x" << height;
  71. ss << ".";
  72. BS_EXCEPT(InvalidParametersException, ss.str());
  73. }
  74. if(mColor[x].buffer->getGLFormat() != glformat)
  75. {
  76. StringStream ss;
  77. ss << "Attachment " << x << " has incompatible format.";
  78. BS_EXCEPT(InvalidParametersException, ss.str());
  79. }
  80. // Note: I'm attaching textures to FBO while renderbuffers might yield better performance if I
  81. // don't need to read from them
  82. mColor[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0 + x, mColor[x].zoffset);
  83. }
  84. else
  85. {
  86. // Detach
  87. glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + x, 0, 0);
  88. }
  89. }
  90. if(mDepthStencilBuffer != nullptr)
  91. mDepthStencilBuffer->bindToFramebuffer(GL_DEPTH_STENCIL_ATTACHMENT, 0);
  92. // Do glDrawBuffer calls
  93. GLenum bufs[BS_MAX_MULTIPLE_RENDER_TARGETS];
  94. GLsizei n = 0;
  95. for(UINT32 x = 0; x < BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  96. {
  97. // Fill attached colour buffers
  98. if(mColor[x].buffer)
  99. {
  100. bufs[x] = GL_COLOR_ATTACHMENT0 + x;
  101. // Keep highest used buffer + 1
  102. n = x+1;
  103. }
  104. else
  105. {
  106. bufs[x] = GL_NONE;
  107. }
  108. }
  109. glDrawBuffers(n, bufs);
  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 = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  115. // Bind main buffer
  116. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  117. switch(status)
  118. {
  119. case GL_FRAMEBUFFER_COMPLETE:
  120. break;
  121. case GL_FRAMEBUFFER_UNSUPPORTED:
  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. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  130. }
  131. }