BsGLFrameBufferObject.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 = RenderAPICore::instancePtr()->getCapabilities(0).getNumMultiRenderTargets();
  55. // Bind simple buffer to add color attachments
  56. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  57. bool bindAllLayers = false;
  58. // Bind all attachment points to frame buffer
  59. for(UINT16 x = 0; x < maxSupportedMRTs; ++x)
  60. {
  61. if(mColor[x].buffer)
  62. {
  63. if(mColor[x].buffer->getWidth() != width || mColor[x].buffer->getHeight() != height)
  64. {
  65. StringStream ss;
  66. ss << "Attachment " << x << " has incompatible size ";
  67. ss << mColor[x].buffer->getWidth() << "x" << mColor[x].buffer->getHeight();
  68. ss << ". It must be of the same as the size of surface 0, ";
  69. ss << width << "x" << height;
  70. ss << ".";
  71. BS_EXCEPT(InvalidParametersException, ss.str());
  72. }
  73. // Note: I'm attaching textures to FBO while renderbuffers might yield better performance if I
  74. // don't need to read from them
  75. mColor[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0 + x, mColor[x].zoffset, mColor[x].allLayers);
  76. bindAllLayers |= mColor[x].allLayers;
  77. }
  78. else
  79. {
  80. // Detach
  81. glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + x, 0, 0);
  82. }
  83. }
  84. if(mDepthStencilBuffer != nullptr)
  85. mDepthStencilBuffer->bindToFramebuffer(GL_DEPTH_STENCIL_ATTACHMENT, 0, bindAllLayers);
  86. // Do glDrawBuffer calls
  87. GLenum bufs[BS_MAX_MULTIPLE_RENDER_TARGETS];
  88. GLsizei n = 0;
  89. for(UINT32 x = 0; x < BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  90. {
  91. // Fill attached colour buffers
  92. if(mColor[x].buffer)
  93. {
  94. bufs[x] = GL_COLOR_ATTACHMENT0 + x;
  95. // Keep highest used buffer + 1
  96. n = x+1;
  97. }
  98. else
  99. {
  100. bufs[x] = GL_NONE;
  101. }
  102. }
  103. glDrawBuffers(n, bufs);
  104. // No read buffer, by default, if we want to read anyway we must not forget to set this.
  105. glReadBuffer(GL_NONE);
  106. // Check status
  107. GLuint status;
  108. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  109. // Bind main buffer
  110. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  111. switch(status)
  112. {
  113. case GL_FRAMEBUFFER_COMPLETE:
  114. break;
  115. case GL_FRAMEBUFFER_UNSUPPORTED:
  116. BS_EXCEPT(InvalidParametersException, "All framebuffer formats with this texture internal format unsupported");
  117. default:
  118. BS_EXCEPT(InvalidParametersException, "Framebuffer incomplete or other FBO status error");
  119. }
  120. }
  121. void GLFrameBufferObject::bind()
  122. {
  123. glBindFramebuffer(GL_FRAMEBUFFER, mFB);
  124. }
  125. }