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_(L"ogl_device")
\r
43 , attached_texture_(0)
\r
48 CASPAR_LOG(info) << L"Initializing OpenGL Device.";
\r
50 std::fill(binded_textures_.begin(), binded_textures_.end(), 0);
\r
51 std::fill(viewport_.begin(), viewport_.end(), 0);
\r
52 std::fill(scissor_.begin(), scissor_.end(), 0);
\r
53 std::fill(blend_func_.begin(), blend_func_.end(), 0);
\r
57 context_.reset(new sf::Context());
\r
58 context_->SetActive(true);
\r
60 if (glewInit() != GLEW_OK)
\r
61 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
63 CASPAR_LOG(info) << L"OpenGL " << version();
\r
65 if(!GLEW_VERSION_3_0)
\r
66 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Your graphics card does not meet the minimum hardware requirements since it does not support OpenGL 3.0 or higher. CasparCG Server will not be able to continue."));
\r
68 glGenFramebuffers(1, &fbo_);
\r
70 CASPAR_LOG(info) << L"Successfully initialized OpenGL Device.";
\r
74 ogl_device::~ogl_device()
\r
78 BOOST_FOREACH(auto& pool, device_pools_)
\r
80 BOOST_FOREACH(auto& pool, host_pools_)
\r
82 glDeleteFramebuffers(1, &fbo_);
\r
86 safe_ptr<device_buffer> ogl_device::allocate_device_buffer(size_t width, size_t height, size_t stride)
\r
88 std::shared_ptr<device_buffer> buffer;
\r
91 buffer.reset(new device_buffer(width, height, stride));
\r
101 buffer.reset(new device_buffer(width, height, stride));
\r
105 CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";
\r
109 return make_safe_ptr(buffer);
\r
112 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)
\r
114 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
115 CASPAR_VERIFY(width > 0 && height > 0);
\r
116 auto& pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
117 std::shared_ptr<device_buffer> buffer;
\r
118 if(!pool->items.try_pop(buffer))
\r
119 buffer = executor_.invoke([&]{return allocate_device_buffer(width, height, stride);}, high_priority);
\r
121 //++pool->usage_count;
\r
123 return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*) mutable
\r
125 pool->items.push(buffer);
\r
129 safe_ptr<host_buffer> ogl_device::allocate_host_buffer(size_t size, host_buffer::usage_t usage)
\r
131 std::shared_ptr<host_buffer> buffer;
\r
135 buffer.reset(new host_buffer(size, usage));
\r
136 if(usage == host_buffer::write_only)
\r
149 buffer.reset(new host_buffer(size, usage));
\r
150 if(usage == host_buffer::write_only)
\r
157 CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";
\r
162 return make_safe_ptr(buffer);
\r
165 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)
\r
167 CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);
\r
168 CASPAR_VERIFY(size > 0);
\r
169 auto& pool = host_pools_[usage][size];
\r
170 std::shared_ptr<host_buffer> buffer;
\r
171 if(!pool->items.try_pop(buffer))
\r
172 buffer = executor_.invoke([=]{return allocate_host_buffer(size, usage);}, high_priority);
\r
174 //++pool->usage_count;
\r
176 auto self = shared_from_this();
\r
177 return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable
\r
179 self->executor_.begin_invoke([=]() mutable
\r
181 if(usage == host_buffer::write_only)
\r
186 pool->items.push(buffer);
\r
187 }, high_priority);
\r
191 safe_ptr<ogl_device> ogl_device::create()
\r
193 return safe_ptr<ogl_device>(new ogl_device());
\r
196 //template<typename T>
\r
197 //void flush_pool(buffer_pool<T>& pool)
\r
199 // if(pool.flush_count.fetch_and_increment() < 16)
\r
202 // if(pool.usage_count.fetch_and_store(0) < pool.items.size())
\r
204 // std::shared_ptr<T> buffer;
\r
205 // pool.items.try_pop(buffer);
\r
208 // pool.flush_count = 0;
\r
209 // pool.usage_count = 0;
\r
212 void ogl_device::flush()
\r
218 // BOOST_FOREACH(auto& pools, device_pools_)
\r
220 // BOOST_FOREACH(auto& pool, pools)
\r
221 // flush_pool(*pool.second);
\r
223 // BOOST_FOREACH(auto& pools, host_pools_)
\r
225 // BOOST_FOREACH(auto& pool, pools)
\r
226 // flush_pool(*pool.second);
\r
231 // CASPAR_LOG_CURRENT_EXCEPTION();
\r
235 void ogl_device::yield()
\r
240 boost::unique_future<void> ogl_device::gc()
\r
242 return begin_invoke([=]
\r
244 CASPAR_LOG(info) << " ogl: Running GC.";
\r
248 BOOST_FOREACH(auto& pools, device_pools_)
\r
250 BOOST_FOREACH(auto& pool, pools)
\r
251 pool.second->items.clear();
\r
253 BOOST_FOREACH(auto& pools, host_pools_)
\r
255 BOOST_FOREACH(auto& pool, pools)
\r
256 pool.second->items.clear();
\r
261 CASPAR_LOG_CURRENT_EXCEPTION();
\r
266 std::wstring ogl_device::version()
\r
268 static std::wstring ver = L"Not found";
\r
271 ver = widen(invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})
\r
272 + " " + invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));
\r
280 void ogl_device::enable(GLenum cap)
\r
282 auto& val = caps_[cap];
\r
290 void ogl_device::disable(GLenum cap)
\r
292 auto& val = caps_[cap];
\r
300 void ogl_device::viewport(size_t x, size_t y, size_t width, size_t height)
\r
302 if(x != viewport_[0] || y != viewport_[1] || width != viewport_[2] || height != viewport_[3])
\r
304 glViewport(x, y, width, height);
\r
307 viewport_[2] = width;
\r
308 viewport_[3] = height;
\r
312 void ogl_device::scissor(size_t x, size_t y, size_t width, size_t height)
\r
314 if(x != scissor_[0] || y != scissor_[1] || width != scissor_[2] || height != scissor_[3])
\r
316 glScissor(x, y, width, height);
\r
319 scissor_[2] = width;
\r
320 scissor_[3] = height;
\r
324 void ogl_device::stipple_pattern(const GLubyte* pattern)
\r
326 if(pattern_ != pattern)
\r
328 glPolygonStipple(pattern);
\r
329 pattern_ = pattern;
\r
333 void ogl_device::attach(device_buffer& texture)
\r
335 if(attached_texture_ != texture.id())
\r
337 if(attached_fbo_ != fbo_)
\r
339 glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
\r
340 attached_fbo_ = fbo_;
\r
343 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));
\r
344 attached_texture_ = texture.id();
\r
348 void ogl_device::clear(device_buffer& texture)
\r
351 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
354 void ogl_device::read_buffer(device_buffer&)
\r
356 if(read_buffer_ != GL_COLOR_ATTACHMENT0)
\r
358 GL(glReadBuffer(GL_COLOR_ATTACHMENT0));
\r
359 read_buffer_ = GL_COLOR_ATTACHMENT0;
\r
363 void ogl_device::use(shader& shader)
\r
365 if(active_shader_ != shader.id())
\r
367 GL(glUseProgramObjectARB(shader.id()));
\r
368 active_shader_ = shader.id();
\r
372 void ogl_device::blend_func(int c1, int c2, int a1, int a2)
\r
374 std::array<int, 4> func = {c1, c2, a1, a2};
\r
376 if(blend_func_ != func)
\r
378 blend_func_ = func;
\r
379 GL(glBlendFuncSeparate(c1, c2, a1, a2));
\r
383 void ogl_device::blend_func(int c1, int c2)
\r
385 blend_func(c1, c2, c1, c2);
\r