BsGLFrameBufferObject.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(SPtr<GLPixelBuffer> 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. 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. // Note: I'm attaching textures to FBO while renderbuffers might yield better performance if I
  73. // don't need to read from them
  74. mColor[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0 + x, mColor[x].zoffset);
  75. }
  76. else
  77. {
  78. // Detach
  79. glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + x, 0, 0);
  80. }
  81. }
  82. if(mDepthStencilBuffer != nullptr)
  83. mDepthStencilBuffer->bindToFramebuffer(GL_DEPTH_STENCIL_ATTACHMENT, 0);
  84. // Do glDrawBuffer calls
  85. GLenum bufs[BS_MAX_MULTIPLE_RENDER_TARGETS];
  86. GLsizei n = 0;
  87. for(UINT32 x = 0; x < BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  88. {
  89. // Fill attached colour buffers
  90. if(mColor[x].buffer)
  91. {
  92. bufs[x] = GL_COLOR_ATTACHMENT0 + x;
  93. // Keep highest used buffer + 1
  94. n = x+1;
  95. }
  96. else
  97. {
  98. bufs[x] = GL_NONE;
  99. }
  100. }
  101. glDrawBuffers(n, bufs);
  102. // No read buffer, by default, if we want to read anyway we must not forget to set this.
  103. glReadBuffer(GL_NONE);
  104. // Check status
  105. GLuint status;
  106. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  107. // Bind main buffer
  108. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  109. switch(status)
  110. {
  111. case GL_FRAMEBUFFER_COMPLETE:
  112. break;
  113. case GL_FRAMEBUFFER_UNSUPPORTED:
  114. BS_EXCEPT(InvalidParametersException, "All framebuffer formats with this texture internal format unsupported");
  115. default:
  116. BS_EXCEPT(InvalidParametersException, "Framebuffer incomplete or other FBO status error");
  117. }
  118. }
  119. void GLFrameBufferObject::bind()
  120. {
  121. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  122. }
  123. }