BsGLFrameBufferObject.cpp 3.6 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 "Profiling/BsRenderStats.h"
  8. namespace bs { namespace ct
  9. {
  10. GLFrameBufferObject::GLFrameBufferObject()
  11. : mDepthStencilAllLayers(false)
  12. {
  13. glGenFramebuffers(1, &mFB);
  14. for (UINT32 x = 0; x < BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  15. mColor[x].buffer = nullptr;
  16. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_FrameBufferObject);
  17. }
  18. GLFrameBufferObject::~GLFrameBufferObject()
  19. {
  20. glDeleteFramebuffers(1, &mFB);
  21. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_FrameBufferObject);
  22. }
  23. void GLFrameBufferObject::bindSurface(UINT32 attachment, const GLSurfaceDesc &target)
  24. {
  25. assert(attachment < BS_MAX_MULTIPLE_RENDER_TARGETS);
  26. mColor[attachment] = target;
  27. }
  28. void GLFrameBufferObject::unbindSurface(UINT32 attachment)
  29. {
  30. assert(attachment < BS_MAX_MULTIPLE_RENDER_TARGETS);
  31. mColor[attachment].buffer = nullptr;
  32. }
  33. void GLFrameBufferObject::bindDepthStencil(SPtr<GLPixelBuffer> depthStencilBuffer, bool allLayers)
  34. {
  35. mDepthStencilBuffer = depthStencilBuffer;
  36. mDepthStencilAllLayers = allLayers;
  37. }
  38. void GLFrameBufferObject::unbindDepthStencil()
  39. {
  40. mDepthStencilBuffer = nullptr;
  41. }
  42. void GLFrameBufferObject::rebuild()
  43. {
  44. // Store basic stats
  45. UINT16 maxSupportedMRTs = RenderAPI::instancePtr()->getCapabilities(0).getNumMultiRenderTargets();
  46. // Bind simple buffer to add color attachments
  47. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  48. // Bind all attachment points to frame buffer
  49. for (UINT16 x = 0; x < maxSupportedMRTs; ++x)
  50. {
  51. if (mColor[x].buffer)
  52. {
  53. // Note: I'm attaching textures to FBO while renderbuffers might yield better performance if I
  54. // don't need to read from them
  55. mColor[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0 + x, mColor[x].zoffset, 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, mDepthStencilAllLayers);
  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. break;
  105. default:
  106. LOGERR("Framebuffer incomplete or other FBO status error");
  107. }
  108. }
  109. void GLFrameBufferObject::bind()
  110. {
  111. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  112. }
  113. }}