Merge pull request #26273 from 78andyp/blurayfixes2
[xbmc.git] / xbmc / cores / VideoPlayer / VideoRenderers / FrameBufferObject.cpp
blob58c3410e99de08e768f63c2b883b0f3faed64d50
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "FrameBufferObject.h"
11 #include "ServiceBroker.h"
12 #include "rendering/RenderSystem.h"
13 #include "utils/GLUtils.h"
14 #include "utils/log.h"
16 //////////////////////////////////////////////////////////////////////
17 // CFrameBufferObject
18 //////////////////////////////////////////////////////////////////////
20 CFrameBufferObject::CFrameBufferObject()
22 m_valid = false;
23 m_supported = false;
24 m_bound = false;
27 bool CFrameBufferObject::IsSupported()
29 if(CServiceBroker::GetRenderSystem()->IsExtSupported("GL_EXT_framebuffer_object"))
30 m_supported = true;
31 else
32 m_supported = false;
33 return m_supported;
36 bool CFrameBufferObject::Initialize()
38 if (!IsSupported())
39 return false;
41 Cleanup();
43 glGenFramebuffers(1, &m_fbo);
44 VerifyGLState();
46 if (!m_fbo)
47 return false;
49 m_valid = true;
50 return true;
53 void CFrameBufferObject::Cleanup()
55 if (!IsValid())
56 return;
58 if (m_fbo)
59 glDeleteFramebuffers(1, &m_fbo);
61 if (m_texid)
62 glDeleteTextures(1, &m_texid);
64 m_texid = 0;
65 m_fbo = 0;
66 m_valid = false;
67 m_bound = false;
70 bool CFrameBufferObject::CreateAndBindToTexture(GLenum target, int width, int height, GLenum format, GLenum type,
71 GLenum filter, GLenum clampmode)
73 if (!IsValid())
74 return false;
76 if (m_texid)
77 glDeleteTextures(1, &m_texid);
79 glGenTextures(1, &m_texid);
80 glBindTexture(target, m_texid);
81 glTexImage2D(target, 0, format, width, height, 0, GL_RGBA, type, NULL);
82 glTexParameteri(target, GL_TEXTURE_WRAP_S, clampmode);
83 glTexParameteri(target, GL_TEXTURE_WRAP_T, clampmode);
84 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
85 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
86 VerifyGLState();
88 m_bound = false;
89 glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
90 glBindTexture(target, m_texid);
91 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, m_texid, 0);
92 VerifyGLState();
93 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
94 glBindFramebuffer(GL_FRAMEBUFFER, 0);
95 if (status != GL_FRAMEBUFFER_COMPLETE)
97 VerifyGLState();
98 return false;
100 m_bound = true;
101 return true;
104 void CFrameBufferObject::SetFiltering(GLenum target, GLenum mode)
106 glBindTexture(target, m_texid);
107 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mode);
108 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, mode);
111 // Begin rendering to FBO
112 bool CFrameBufferObject::BeginRender()
114 if (IsValid() && IsBound())
116 glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
117 return true;
119 return false;
122 // Finish rendering to FBO
123 void CFrameBufferObject::EndRender() const
125 if (IsValid())
126 glBindFramebuffer(GL_FRAMEBUFFER, 0);