/xbmc/guilib/FrameBufferObject.cpp

http://github.com/xbmc/xbmc · C++ · 163 lines · 114 code · 24 blank · 25 comment · 17 complexity · 6dc5426da1674f87e896dea25d3428c6 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2013 Team XBMC
  3. * http://xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with XBMC; see the file COPYING. If not, see
  17. * <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "system.h"
  21. #if defined(HAS_GL) || HAS_GLES == 2
  22. #include "FrameBufferObject.h"
  23. #include "windowing/WindowingFactory.h"
  24. #include "utils/GLUtils.h"
  25. #include "utils/log.h"
  26. #if HAS_GLES == 2
  27. // For OpenGL ES2.0, FBO are not extensions but part of the API.
  28. #define GL_FRAMEBUFFER_EXT GL_FRAMEBUFFER
  29. #define glBindFramebufferEXT glBindFramebuffer
  30. #define glGenFramebuffersEXT glGenFramebuffers
  31. #define glDeleteFramebuffersEXT glDeleteFramebuffers
  32. #define glFramebufferTexture2DEXT glFramebufferTexture2D
  33. #define glCheckFramebufferStatusEXT glCheckFramebufferStatus
  34. #define GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT0
  35. #define GL_FRAMEBUFFER_COMPLETE_EXT GL_FRAMEBUFFER_COMPLETE
  36. #endif
  37. //////////////////////////////////////////////////////////////////////
  38. // CFrameBufferObject
  39. //////////////////////////////////////////////////////////////////////
  40. CFrameBufferObject::CFrameBufferObject()
  41. {
  42. m_fbo = 0;
  43. m_valid = false;
  44. m_supported = false;
  45. m_bound = false;
  46. m_texid = 0;
  47. }
  48. bool CFrameBufferObject::IsSupported()
  49. {
  50. if(g_Windowing.IsExtSupported("GL_EXT_framebuffer_object"))
  51. m_supported = true;
  52. else
  53. m_supported = false;
  54. return m_supported;
  55. }
  56. bool CFrameBufferObject::Initialize()
  57. {
  58. if (!IsSupported())
  59. return false;
  60. Cleanup();
  61. glGenFramebuffersEXT(1, &m_fbo);
  62. VerifyGLState();
  63. if (!m_fbo)
  64. return false;
  65. m_valid = true;
  66. return true;
  67. }
  68. void CFrameBufferObject::Cleanup()
  69. {
  70. if (!IsValid())
  71. return;
  72. if (m_fbo)
  73. glDeleteFramebuffersEXT(1, &m_fbo);
  74. if (m_texid)
  75. glDeleteTextures(1, &m_texid);
  76. m_texid = 0;
  77. m_fbo = 0;
  78. m_valid = false;
  79. m_bound = false;
  80. }
  81. bool CFrameBufferObject::CreateAndBindToTexture(GLenum target, int width, int height, GLenum format,
  82. GLenum filter, GLenum clampmode)
  83. {
  84. if (!IsValid())
  85. return false;
  86. if (m_texid)
  87. glDeleteTextures(1, &m_texid);
  88. glGenTextures(1, &m_texid);
  89. glBindTexture(target, m_texid);
  90. glTexImage2D(target, 0, format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  91. glTexParameteri(target, GL_TEXTURE_WRAP_S, clampmode);
  92. glTexParameteri(target, GL_TEXTURE_WRAP_T, clampmode);
  93. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
  94. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
  95. VerifyGLState();
  96. return BindToTexture(target, m_texid);
  97. }
  98. void CFrameBufferObject::SetFiltering(GLenum target, GLenum mode)
  99. {
  100. glBindTexture(target, m_texid);
  101. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mode);
  102. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, mode);
  103. }
  104. bool CFrameBufferObject::BindToTexture(GLenum target, GLuint texid)
  105. {
  106. if (!IsValid())
  107. return false;
  108. m_bound = false;
  109. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
  110. glBindTexture(target, texid);
  111. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, target, texid, 0);
  112. VerifyGLState();
  113. GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  114. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  115. if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
  116. {
  117. VerifyGLState();
  118. return false;
  119. }
  120. m_bound = true;
  121. return true;
  122. }
  123. // Begin rendering to FBO
  124. bool CFrameBufferObject::BeginRender()
  125. {
  126. if (IsValid() && IsBound())
  127. {
  128. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
  129. return true;
  130. }
  131. return false;
  132. }
  133. // Finish rendering to FBO
  134. void CFrameBufferObject::EndRender() const
  135. {
  136. if (IsValid())
  137. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  138. }
  139. #endif