BsGLFrameBufferObject.cpp 3.8 KB

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