2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
20 #include "../../stdafx.h"
\r
22 #include "ogl_device.h"
\r
26 #include <common/exception/exceptions.h>
\r
27 #include <common/utility/assert.h>
\r
28 #include <common/gl/gl_check.h>
\r
30 #include <boost/foreach.hpp>
\r
32 #include <gl/glew.h>
\r
34 namespace caspar { namespace core {
\r
36 ogl_device::ogl_device()
\r
37 : executor_(L"ogl_device")
\r
39 , attached_texture_(0)
\r
42 std::fill(binded_textures_.begin(), binded_textures_.end(), 0);
\r
43 std::fill(viewport_.begin(), viewport_.end(), 0);
\r
44 std::fill(scissor_.begin(), scissor_.end(), 0);
\r
48 context_.reset(new sf::Context());
\r
49 context_->SetActive(true);
\r
51 if (glewInit() != GLEW_OK)
\r
52 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
54 if(!GLEW_VERSION_3_2)
\r
55 CASPAR_LOG(warning) << "Missing OpenGL 3.2 support.";
\r
57 GL(glGenFramebuffers(1, &fbo_));
\r
58 GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));
\r
59 GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));
\r
60 GL(glDisable(GL_MULTISAMPLE_ARB));
\r
64 ogl_device::~ogl_device()
\r
68 BOOST_FOREACH(auto& pool, device_pools_)
\r
70 BOOST_FOREACH(auto& pool, host_pools_)
\r
72 glDeleteFramebuffersEXT(1, &fbo_);
\r
76 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)
\r
78 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
79 CASPAR_VERIFY(width > 0 && height > 0);
\r
80 auto pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
81 std::shared_ptr<device_buffer> buffer;
\r
82 if(!pool->try_pop(buffer))
\r
84 executor_.invoke([&]
\r
88 buffer.reset(new device_buffer(width, height, stride));
\r
98 buffer.reset(new device_buffer(width, height, stride));
\r
102 CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";
\r
107 }, high_priority);
\r
110 return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*)
\r
112 pool->push(buffer);
\r
116 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)
\r
118 CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);
\r
119 CASPAR_VERIFY(size > 0);
\r
120 auto pool = host_pools_[usage][size];
\r
121 std::shared_ptr<host_buffer> buffer;
\r
122 if(!pool->try_pop(buffer))
\r
124 executor_.invoke([&]
\r
128 buffer.reset(new host_buffer(size, usage));
\r
129 if(usage == host_buffer::write_only)
\r
142 buffer.reset(new host_buffer(size, usage));
\r
143 if(usage == host_buffer::write_only)
\r
150 CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";
\r
155 }, high_priority);
\r
158 return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)
\r
160 executor_.begin_invoke([=]
\r
162 if(usage == host_buffer::write_only)
\r
167 pool->push(buffer);
\r
173 void ogl_device::yield()
\r
178 boost::unique_future<void> ogl_device::gc()
\r
180 return begin_invoke([=]
\r
182 CASPAR_LOG(info) << " ogl: Running GC.";
\r
186 BOOST_FOREACH(auto& pools, device_pools_)
\r
188 BOOST_FOREACH(auto& pool, pools)
\r
189 pool.second->clear();
\r
191 BOOST_FOREACH(auto& pools, host_pools_)
\r
193 BOOST_FOREACH(auto& pool, pools)
\r
194 pool.second->clear();
\r
199 CASPAR_LOG_CURRENT_EXCEPTION();
\r
204 std::wstring ogl_device::get_version()
\r
206 static std::wstring ver = L"Not found";
\r
210 ver = widen(tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})
\r
211 + " " + tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));
\r
219 void ogl_device::enable(GLenum cap)
\r
221 auto& val = caps_[cap];
\r
229 void ogl_device::disable(GLenum cap)
\r
231 auto& val = caps_[cap];
\r
239 void ogl_device::viewport(size_t x, size_t y, size_t width, size_t height)
\r
241 if(x != viewport_[0] || y != viewport_[1] || width != viewport_[2] || height != viewport_[3])
\r
243 glViewport(x, y, width, height);
\r
246 viewport_[2] = width;
\r
247 viewport_[3] = height;
\r
251 void ogl_device::scissor(size_t x, size_t y, size_t width, size_t height)
\r
253 if(x != scissor_[0] || y != scissor_[1] || width != scissor_[2] || height != scissor_[3])
\r
255 glScissor(x, y, width, height);
\r
258 scissor_[2] = width;
\r
259 scissor_[3] = height;
\r
263 void ogl_device::stipple_pattern(const GLubyte* pattern)
\r
265 if(pattern_ != pattern)
\r
267 glPolygonStipple(pattern);
\r
268 pattern_ = pattern;
\r
272 void ogl_device::attach(device_buffer& texture)
\r
274 if(attached_texture_ != texture.id())
\r
276 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));
\r
277 attached_texture_ = texture.id();
\r
281 void ogl_device::clear(device_buffer& texture)
\r
284 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
287 void ogl_device::use(shader& shader)
\r
289 if(active_shader_ != shader.id())
\r
291 GL(glUseProgramObjectARB(shader.id()));
\r
292 active_shader_ = shader.id();
\r