BsGLFrameBufferObject.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 bs { namespace ct
  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. }
  27. void GLFrameBufferObject::unbindSurface(UINT32 attachment)
  28. {
  29. assert(attachment < BS_MAX_MULTIPLE_RENDER_TARGETS);
  30. mColor[attachment].buffer = nullptr;
  31. }
  32. void GLFrameBufferObject::bindDepthStencil(SPtr<GLPixelBuffer> depthStencilBuffer)
  33. {
  34. mDepthStencilBuffer = depthStencilBuffer;
  35. }
  36. void GLFrameBufferObject::unbindDepthStencil()
  37. {
  38. mDepthStencilBuffer = nullptr;
  39. }
  40. void GLFrameBufferObject::rebuild()
  41. {
  42. // Store basic stats
  43. UINT16 maxSupportedMRTs = RenderAPI::instancePtr()->getCapabilities(0).getNumMultiRenderTargets();
  44. // Bind simple buffer to add color attachments
  45. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  46. bool bindAllLayers = false;
  47. // Bind all attachment points to frame buffer
  48. for(UINT16 x = 0; x < maxSupportedMRTs; ++x)
  49. {
  50. if(mColor[x].buffer)
  51. {
  52. // Note: I'm attaching textures to FBO while renderbuffers might yield better performance if I
  53. // don't need to read from them
  54. mColor[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0 + x, mColor[x].zoffset, mColor[x].allLayers);
  55. bindAllLayers |= mColor[x].allLayers;
  56. }
  57. else
  58. {
  59. // Detach
  60. glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + x, 0, 0);
  61. }
  62. }
  63. if (mDepthStencilBuffer != nullptr)
  64. {
  65. GLenum depthStencilFormat = GLPixelUtil::getDepthStencilFormatFromPF(mDepthStencilBuffer->getFormat());
  66. GLenum attachmentPoint;
  67. if (depthStencilFormat == GL_DEPTH_STENCIL)
  68. attachmentPoint = GL_DEPTH_STENCIL_ATTACHMENT;
  69. else // Depth only
  70. attachmentPoint = GL_DEPTH_ATTACHMENT;
  71. mDepthStencilBuffer->bindToFramebuffer(attachmentPoint, 0, true);
  72. }
  73. // Do glDrawBuffer calls
  74. GLenum bufs[BS_MAX_MULTIPLE_RENDER_TARGETS];
  75. GLsizei n = 0;
  76. for(UINT32 x = 0; x < BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  77. {
  78. // Fill attached colour buffers
  79. if(mColor[x].buffer)
  80. {
  81. bufs[x] = GL_COLOR_ATTACHMENT0 + x;
  82. // Keep highest used buffer + 1
  83. n = x+1;
  84. }
  85. else
  86. {
  87. bufs[x] = GL_NONE;
  88. }
  89. }
  90. glDrawBuffers(n, bufs);
  91. // No read buffer, by default, if we want to read anyway we must not forget to set this.
  92. glReadBuffer(GL_NONE);
  93. // Check status
  94. GLuint status;
  95. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  96. // Bind main buffer
  97. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  98. switch(status)
  99. {
  100. case GL_FRAMEBUFFER_COMPLETE:
  101. break;
  102. case GL_FRAMEBUFFER_UNSUPPORTED:
  103. LOGERR("All framebuffer formats with this texture internal format unsupported");
  104. default:
  105. LOGERR("Framebuffer incomplete or other FBO status error");
  106. }
  107. }
  108. void GLFrameBufferObject::bind()
  109. {
  110. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  111. }
  112. }}