BsGLFrameBufferObject.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsGLFrameBufferObject.h"
  5. #include "BsGLPixelFormat.h"
  6. #include "BsGLPixelBuffer.h"
  7. #include "BsGLRenderTexture.h"
  8. #include "BsRenderStats.h"
  9. namespace BansheeEngine
  10. {
  11. GLFrameBufferObject::GLFrameBufferObject()
  12. {
  13. glGenFramebuffersEXT(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. glDeleteFramebuffersEXT(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. if(mColor[0].buffer)
  28. rebuild();
  29. }
  30. void GLFrameBufferObject::unbindSurface(UINT32 attachment)
  31. {
  32. assert(attachment < BS_MAX_MULTIPLE_RENDER_TARGETS);
  33. mColor[attachment].buffer = nullptr;
  34. if(mColor[0].buffer)
  35. rebuild();
  36. }
  37. void GLFrameBufferObject::bindDepthStencil(GLPixelBufferPtr depthStencilBuffer)
  38. {
  39. mDepthStencilBuffer = depthStencilBuffer;
  40. }
  41. void GLFrameBufferObject::unbindDepthStencil()
  42. {
  43. mDepthStencilBuffer = nullptr;
  44. }
  45. void GLFrameBufferObject::rebuild()
  46. {
  47. // First buffer must be bound
  48. if(!mColor[0].buffer)
  49. BS_EXCEPT(InvalidParametersException, "Attachment 0 must have surface attached");
  50. // Store basic stats
  51. UINT32 width = mColor[0].buffer->getWidth();
  52. UINT32 height = mColor[0].buffer->getHeight();
  53. GLuint glformat = mColor[0].buffer->getGLFormat();
  54. PixelFormat format = mColor[0].buffer->getFormat();
  55. UINT16 maxSupportedMRTs = BansheeEngine::RenderSystem::instancePtr()->getCapabilities()->getNumMultiRenderTargets();
  56. // Bind simple buffer to add color attachments
  57. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  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. if(mColor[x].buffer->getGLFormat() != glformat)
  74. {
  75. StringStream ss;
  76. ss << "Attachment " << x << " has incompatible format.";
  77. BS_EXCEPT(InvalidParametersException, ss.str());
  78. }
  79. mColor[x].buffer->bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT + x, mColor[x].zoffset);
  80. }
  81. else
  82. {
  83. // Detach
  84. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + x, GL_RENDERBUFFER_EXT, 0);
  85. }
  86. }
  87. if(mDepthStencilBuffer != nullptr)
  88. mDepthStencilBuffer->bindToFramebuffer(GL_DEPTH_STENCIL_ATTACHMENT, 0);
  89. // Do glDrawBuffer calls
  90. GLenum bufs[BS_MAX_MULTIPLE_RENDER_TARGETS];
  91. GLsizei n=0;
  92. for(UINT32 x=0; x<BS_MAX_MULTIPLE_RENDER_TARGETS; ++x)
  93. {
  94. // Fill attached colour buffers
  95. if(mColor[x].buffer)
  96. {
  97. bufs[x] = GL_COLOR_ATTACHMENT0_EXT + x;
  98. // Keep highest used buffer + 1
  99. n = x+1;
  100. }
  101. else
  102. {
  103. bufs[x] = GL_NONE;
  104. }
  105. }
  106. if(glDrawBuffers)
  107. {
  108. // Drawbuffer extension supported, use it
  109. glDrawBuffers(n, bufs);
  110. }
  111. else
  112. {
  113. // In this case, the capabilities will not show more than 1 simultaneaous render target.
  114. glDrawBuffer(bufs[0]);
  115. }
  116. // No read buffer, by default, if we want to read anyway we must not forget to set this.
  117. glReadBuffer(GL_NONE);
  118. // Check status
  119. GLuint status;
  120. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  121. // Bind main buffer
  122. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  123. switch(status)
  124. {
  125. case GL_FRAMEBUFFER_COMPLETE_EXT:
  126. break;
  127. case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
  128. BS_EXCEPT(InvalidParametersException, "All framebuffer formats with this texture internal format unsupported");
  129. default:
  130. BS_EXCEPT(InvalidParametersException, "Framebuffer incomplete or other FBO status error");
  131. }
  132. }
  133. void GLFrameBufferObject::bind()
  134. {
  135. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFB);
  136. }
  137. }