PageRenderTime 50ms CodeModel.GetById 15ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 21#include "system.h"
 22
 23#if defined(HAS_GL) || HAS_GLES == 2
 24#include "FrameBufferObject.h"
 25#include "windowing/WindowingFactory.h"
 26#include "utils/GLUtils.h"
 27#include "utils/log.h"
 28
 29#if HAS_GLES == 2
 30// For OpenGL ES2.0, FBO are not extensions but part of the API.
 31#define GL_FRAMEBUFFER_EXT GL_FRAMEBUFFER
 32#define glBindFramebufferEXT glBindFramebuffer
 33#define glGenFramebuffersEXT glGenFramebuffers
 34#define glDeleteFramebuffersEXT glDeleteFramebuffers
 35#define glFramebufferTexture2DEXT	glFramebufferTexture2D
 36#define glCheckFramebufferStatusEXT	glCheckFramebufferStatus
 37#define GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT0
 38#define GL_FRAMEBUFFER_COMPLETE_EXT	GL_FRAMEBUFFER_COMPLETE
 39#endif
 40
 41//////////////////////////////////////////////////////////////////////
 42// CFrameBufferObject
 43//////////////////////////////////////////////////////////////////////
 44
 45CFrameBufferObject::CFrameBufferObject()
 46{
 47  m_fbo = 0;
 48  m_valid = false;
 49  m_supported = false;
 50  m_bound = false;
 51  m_texid = 0;
 52}
 53
 54bool CFrameBufferObject::IsSupported()
 55{
 56  if(g_Windowing.IsExtSupported("GL_EXT_framebuffer_object"))
 57    m_supported = true;
 58  else
 59    m_supported = false;
 60  return m_supported;
 61}
 62
 63bool CFrameBufferObject::Initialize()
 64{
 65  if (!IsSupported())
 66    return false;
 67
 68  Cleanup();
 69
 70  glGenFramebuffersEXT(1, &m_fbo);
 71  VerifyGLState();
 72
 73  if (!m_fbo)
 74    return false;
 75
 76  m_valid = true;
 77  return true;
 78}
 79
 80void CFrameBufferObject::Cleanup()
 81{
 82  if (!IsValid())
 83    return;
 84
 85  if (m_fbo)
 86    glDeleteFramebuffersEXT(1, &m_fbo);
 87
 88  if (m_texid)
 89    glDeleteTextures(1, &m_texid);
 90
 91  m_texid = 0;
 92  m_fbo = 0;
 93  m_valid = false;
 94  m_bound = false;
 95}
 96
 97bool CFrameBufferObject::CreateAndBindToTexture(GLenum target, int width, int height, GLenum format,
 98                                                GLenum filter, GLenum clampmode)
 99{
100  if (!IsValid())
101    return false;
102
103  if (m_texid)
104    glDeleteTextures(1, &m_texid);
105
106  glGenTextures(1, &m_texid);
107  glBindTexture(target, m_texid);
108  glTexImage2D(target, 0, format,  width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
109  glTexParameteri(target, GL_TEXTURE_WRAP_S, clampmode);
110  glTexParameteri(target, GL_TEXTURE_WRAP_T, clampmode);
111  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
112  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
113  VerifyGLState();
114  return BindToTexture(target, m_texid);
115}
116
117void CFrameBufferObject::SetFiltering(GLenum target, GLenum mode)
118{
119  glBindTexture(target, m_texid);
120  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mode);
121  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, mode);
122}
123
124bool CFrameBufferObject::BindToTexture(GLenum target, GLuint texid)
125{
126  if (!IsValid())
127    return false;
128
129  m_bound = false;
130  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
131  glBindTexture(target, texid);
132  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, target, texid, 0);
133  VerifyGLState();
134  GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
135  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
136  if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
137  {
138    VerifyGLState();
139    return false;
140  }
141  m_bound = true;
142  return true;
143}
144
145// Begin rendering to FBO
146bool CFrameBufferObject::BeginRender()
147{
148  if (IsValid() && IsBound())
149  {
150    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
151    return true;
152  }
153  return false;
154}
155
156// Finish rendering to FBO
157void CFrameBufferObject::EndRender() const
158{
159  if (IsValid())
160    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
161}
162
163#endif