2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
24 #include "../../stdafx.h"
\r
26 #include "ogl_device.h"
\r
30 #include <common/exception/exceptions.h>
\r
31 #include <common/utility/assert.h>
\r
32 #include <common/gl/gl_check.h>
\r
34 #include <boost/foreach.hpp>
\r
36 #include <gl/glew.h>
\r
38 namespace caspar { namespace core {
\r
40 ogl_device::ogl_device()
\r
41 : executor_("ogl_device")
\r
43 , attached_texture_(0)
\r
46 CASPAR_LOG(info) << "Initializing OpenGL Device.";
\r
48 std::fill(binded_textures_.begin(), binded_textures_.end(), 0);
\r
49 std::fill(viewport_.begin(), viewport_.end(), 0);
\r
50 std::fill(scissor_.begin(), scissor_.end(), 0);
\r
51 std::fill(blend_func_.begin(), blend_func_.end(), 0);
\r
55 context_.reset(new sf::Context());
\r
56 context_->SetActive(true);
\r
58 if (glewInit() != GLEW_OK)
\r
59 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
61 CASPAR_LOG(info) << "OpenGL " << version();
\r
63 if(!GLEW_VERSION_3_0)
\r
64 CASPAR_LOG(warning) << "Missing OpenGL 3.0 support.";
\r
66 CASPAR_LOG(info) << "Successfully initialized GLEW.";
\r
68 GL(glGenFramebuffers(1, &fbo_));
\r
69 GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));
\r
70 GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));
\r
71 //GL(glDisable(GL_MULTISAMPLE_ARB));
\r
73 CASPAR_LOG(info) << "Successfully initialized OpenGL Device.";
\r
77 ogl_device::~ogl_device()
\r
81 BOOST_FOREACH(auto& pool, device_pools_)
\r
83 BOOST_FOREACH(auto& pool, host_pools_)
\r
85 glDeleteFramebuffersEXT(1, &fbo_);
\r
89 safe_ptr<device_buffer> ogl_device::allocate_device_buffer(size_t width, size_t height, size_t stride)
\r
91 std::shared_ptr<device_buffer> buffer;
\r
94 buffer.reset(new device_buffer(width, height, stride));
\r
104 buffer.reset(new device_buffer(width, height, stride));
\r
108 CASPAR_LOG(error) << "ogl: create_device_buffer failed!";
\r
112 return make_safe_ptr(buffer);
\r
115 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)
\r
117 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
118 CASPAR_VERIFY(width > 0 && height > 0);
\r
119 auto& pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
120 std::shared_ptr<device_buffer> buffer;
\r
121 if(!pool->items.try_pop(buffer))
\r
122 buffer = executor_.invoke([&]{return allocate_device_buffer(width, height, stride);}, high_priority);
\r
124 //++pool->usage_count;
\r
126 return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*) mutable
\r
128 pool->items.push(buffer);
\r
132 safe_ptr<host_buffer> ogl_device::allocate_host_buffer(size_t size, host_buffer::usage_t usage)
\r
134 std::shared_ptr<host_buffer> buffer;
\r
138 buffer.reset(new host_buffer(size, usage));
\r
139 if(usage == host_buffer::write_only)
\r
152 buffer.reset(new host_buffer(size, usage));
\r
153 if(usage == host_buffer::write_only)
\r
160 CASPAR_LOG(error) << "ogl: create_host_buffer failed!";
\r
165 return make_safe_ptr(buffer);
\r
168 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)
\r
170 CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);
\r
171 CASPAR_VERIFY(size > 0);
\r
172 auto& pool = host_pools_[usage][size];
\r
173 std::shared_ptr<host_buffer> buffer;
\r
174 if(!pool->items.try_pop(buffer))
\r
175 buffer = executor_.invoke([=]{return allocate_host_buffer(size, usage);}, high_priority);
\r
177 //++pool->usage_count;
\r
179 auto self = shared_from_this();
\r
180 return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable
\r
182 self->executor_.begin_invoke([=]() mutable
\r
184 if(usage == host_buffer::write_only)
\r
189 pool->items.push(buffer);
\r
190 }, high_priority);
\r
194 safe_ptr<ogl_device> ogl_device::create()
\r
196 return safe_ptr<ogl_device>(new ogl_device());
\r
199 //template<typename T>
\r
200 //void flush_pool(buffer_pool<T>& pool)
\r
202 // if(pool.flush_count.fetch_and_increment() < 16)
\r
205 // if(pool.usage_count.fetch_and_store(0) < pool.items.size())
\r
207 // std::shared_ptr<T> buffer;
\r
208 // pool.items.try_pop(buffer);
\r
211 // pool.flush_count = 0;
\r
212 // pool.usage_count = 0;
\r
215 void ogl_device::flush()
\r
221 // BOOST_FOREACH(auto& pools, device_pools_)
\r
223 // BOOST_FOREACH(auto& pool, pools)
\r
224 // flush_pool(*pool.second);
\r
226 // BOOST_FOREACH(auto& pools, host_pools_)
\r
228 // BOOST_FOREACH(auto& pool, pools)
\r
229 // flush_pool(*pool.second);
\r
234 // CASPAR_LOG_CURRENT_EXCEPTION();
\r
238 void ogl_device::yield()
\r
243 boost::unique_future<void> ogl_device::gc()
\r
245 return begin_invoke([=]
\r
247 CASPAR_LOG(info) << " ogl: Running GC.";
\r
251 BOOST_FOREACH(auto& pools, device_pools_)
\r
253 BOOST_FOREACH(auto& pool, pools)
\r
254 pool.second->items.clear();
\r
256 BOOST_FOREACH(auto& pools, host_pools_)
\r
258 BOOST_FOREACH(auto& pool, pools)
\r
259 pool.second->items.clear();
\r
264 CASPAR_LOG_CURRENT_EXCEPTION();
\r
269 std::string ogl_device::version()
\r
271 static std::string ver = "Not found";
\r
274 ver = invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})
\r
275 + " " + invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));});
\r
283 void ogl_device::enable(GLenum cap)
\r
285 auto& val = caps_[cap];
\r
293 void ogl_device::disable(GLenum cap)
\r
295 auto& val = caps_[cap];
\r
303 void ogl_device::viewport(size_t x, size_t y, size_t width, size_t height)
\r
305 if(x != viewport_[0] || y != viewport_[1] || width != viewport_[2] || height != viewport_[3])
\r
307 glViewport(x, y, width, height);
\r
310 viewport_[2] = width;
\r
311 viewport_[3] = height;
\r
315 void ogl_device::scissor(size_t x, size_t y, size_t width, size_t height)
\r
317 if(x != scissor_[0] || y != scissor_[1] || width != scissor_[2] || height != scissor_[3])
\r
319 glScissor(x, y, width, height);
\r
322 scissor_[2] = width;
\r
323 scissor_[3] = height;
\r
327 void ogl_device::stipple_pattern(const GLubyte* pattern)
\r
329 if(pattern_ != pattern)
\r
331 glPolygonStipple(pattern);
\r
332 pattern_ = pattern;
\r
336 void ogl_device::attach(device_buffer& texture)
\r
338 if(attached_texture_ != texture.id())
\r
340 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));
\r
341 attached_texture_ = texture.id();
\r
345 void ogl_device::clear(device_buffer& texture)
\r
348 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
351 void ogl_device::use(shader& shader)
\r
353 if(active_shader_ != shader.id())
\r
355 GL(glUseProgramObjectARB(shader.id()));
\r
356 active_shader_ = shader.id();
\r
360 void ogl_device::blend_func(int c1, int c2, int a1, int a2)
\r
362 std::array<int, 4> func = {c1, c2, a1, a2};
\r
364 if(blend_func_ != func)
\r
366 blend_func_ = func;
\r
367 GL(glBlendFuncSeparate(c1, c2, a1, a2));
\r
371 void ogl_device::blend_func(int c1, int c2)
\r
373 blend_func(c1, c2, c1, c2);
\r