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
22 #include "../../stdafx.h"
\r
24 #include "ogl_device.h"
\r
28 #include <common/exception/exceptions.h>
\r
29 #include <common/utility/assert.h>
\r
30 #include <common/gl/gl_check.h>
\r
32 #include <boost/foreach.hpp>
\r
34 #include <gl/glew.h>
\r
36 namespace caspar { namespace core {
\r
38 ogl_device::ogl_device()
\r
39 : executor_(L"ogl_device")
\r
41 , attached_texture_(0)
\r
44 CASPAR_LOG(info) << L"Initializing OpenGL Device.";
\r
46 std::fill(binded_textures_.begin(), binded_textures_.end(), 0);
\r
47 std::fill(viewport_.begin(), viewport_.end(), 0);
\r
48 std::fill(scissor_.begin(), scissor_.end(), 0);
\r
49 std::fill(blend_func_.begin(), blend_func_.end(), 0);
\r
53 context_.reset(new sf::Context());
\r
54 context_->SetActive(true);
\r
56 if (glewInit() != GLEW_OK)
\r
57 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
59 CASPAR_LOG(info) << L"OpenGL " << version();
\r
61 if(!GLEW_VERSION_3_0)
\r
62 CASPAR_LOG(warning) << "Missing OpenGL 3.0 support.";
\r
64 CASPAR_LOG(info) << L"Successfully initialized GLEW.";
\r
66 GL(glGenFramebuffers(1, &fbo_));
\r
67 GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));
\r
68 GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));
\r
69 //GL(glDisable(GL_MULTISAMPLE_ARB));
\r
71 CASPAR_LOG(info) << L"Successfully initialized OpenGL Device.";
\r
75 ogl_device::~ogl_device()
\r
79 BOOST_FOREACH(auto& pool, device_pools_)
\r
81 BOOST_FOREACH(auto& pool, host_pools_)
\r
83 glDeleteFramebuffersEXT(1, &fbo_);
\r
87 safe_ptr<device_buffer> ogl_device::allocate_device_buffer(size_t width, size_t height, size_t stride)
\r
89 std::shared_ptr<device_buffer> buffer;
\r
92 buffer.reset(new device_buffer(width, height, stride));
\r
102 buffer.reset(new device_buffer(width, height, stride));
\r
106 CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";
\r
110 return make_safe_ptr(buffer);
\r
113 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)
\r
115 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
116 CASPAR_VERIFY(width > 0 && height > 0);
\r
117 auto& pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
118 std::shared_ptr<device_buffer> buffer;
\r
119 if(!pool->items.try_pop(buffer))
\r
120 buffer = executor_.invoke([&]{return allocate_device_buffer(width, height, stride);}, high_priority);
\r
122 //++pool->usage_count;
\r
124 return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*) mutable
\r
126 pool->items.push(buffer);
\r
130 safe_ptr<host_buffer> ogl_device::allocate_host_buffer(size_t size, host_buffer::usage_t usage)
\r
132 std::shared_ptr<host_buffer> buffer;
\r
136 buffer.reset(new host_buffer(size, usage));
\r
137 if(usage == host_buffer::write_only)
\r
150 buffer.reset(new host_buffer(size, usage));
\r
151 if(usage == host_buffer::write_only)
\r
158 CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";
\r
163 return make_safe_ptr(buffer);
\r
166 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)
\r
168 CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);
\r
169 CASPAR_VERIFY(size > 0);
\r
170 auto& pool = host_pools_[usage][size];
\r
171 std::shared_ptr<host_buffer> buffer;
\r
172 if(!pool->items.try_pop(buffer))
\r
173 buffer = executor_.invoke([=]{return allocate_host_buffer(size, usage);}, high_priority);
\r
175 //++pool->usage_count;
\r
177 auto self = shared_from_this();
\r
178 return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable
\r
180 self->executor_.begin_invoke([=]() mutable
\r
182 if(usage == host_buffer::write_only)
\r
187 pool->items.push(buffer);
\r
188 }, high_priority);
\r
192 safe_ptr<ogl_device> ogl_device::create()
\r
194 return safe_ptr<ogl_device>(new ogl_device());
\r
197 //template<typename T>
\r
198 //void flush_pool(buffer_pool<T>& pool)
\r
200 // if(pool.flush_count.fetch_and_increment() < 16)
\r
203 // if(pool.usage_count.fetch_and_store(0) < pool.items.size())
\r
205 // std::shared_ptr<T> buffer;
\r
206 // pool.items.try_pop(buffer);
\r
209 // pool.flush_count = 0;
\r
210 // pool.usage_count = 0;
\r
213 void ogl_device::flush()
\r
219 // BOOST_FOREACH(auto& pools, device_pools_)
\r
221 // BOOST_FOREACH(auto& pool, pools)
\r
222 // flush_pool(*pool.second);
\r
224 // BOOST_FOREACH(auto& pools, host_pools_)
\r
226 // BOOST_FOREACH(auto& pool, pools)
\r
227 // flush_pool(*pool.second);
\r
232 // CASPAR_LOG_CURRENT_EXCEPTION();
\r
236 void ogl_device::yield()
\r
241 boost::unique_future<void> ogl_device::gc()
\r
243 return begin_invoke([=]
\r
245 CASPAR_LOG(info) << " ogl: Running GC.";
\r
249 BOOST_FOREACH(auto& pools, device_pools_)
\r
251 BOOST_FOREACH(auto& pool, pools)
\r
252 pool.second->items.clear();
\r
254 BOOST_FOREACH(auto& pools, host_pools_)
\r
256 BOOST_FOREACH(auto& pool, pools)
\r
257 pool.second->items.clear();
\r
262 CASPAR_LOG_CURRENT_EXCEPTION();
\r
267 std::wstring ogl_device::version()
\r
269 static std::wstring ver = L"Not found";
\r
272 ver = widen(invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})
\r
273 + " " + invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));
\r
281 void ogl_device::enable(GLenum cap)
\r
283 auto& val = caps_[cap];
\r
291 void ogl_device::disable(GLenum cap)
\r
293 auto& val = caps_[cap];
\r
301 void ogl_device::viewport(size_t x, size_t y, size_t width, size_t height)
\r
303 if(x != viewport_[0] || y != viewport_[1] || width != viewport_[2] || height != viewport_[3])
\r
305 glViewport(x, y, width, height);
\r
308 viewport_[2] = width;
\r
309 viewport_[3] = height;
\r
313 void ogl_device::scissor(size_t x, size_t y, size_t width, size_t height)
\r
315 if(x != scissor_[0] || y != scissor_[1] || width != scissor_[2] || height != scissor_[3])
\r
317 glScissor(x, y, width, height);
\r
320 scissor_[2] = width;
\r
321 scissor_[3] = height;
\r
325 void ogl_device::stipple_pattern(const GLubyte* pattern)
\r
327 if(pattern_ != pattern)
\r
329 glPolygonStipple(pattern);
\r
330 pattern_ = pattern;
\r
334 void ogl_device::attach(device_buffer& texture)
\r
336 if(attached_texture_ != texture.id())
\r
338 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));
\r
339 attached_texture_ = texture.id();
\r
343 void ogl_device::clear(device_buffer& texture)
\r
346 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
349 void ogl_device::use(shader& shader)
\r
351 if(active_shader_ != shader.id())
\r
353 GL(glUseProgramObjectARB(shader.id()));
\r
354 active_shader_ = shader.id();
\r
358 void ogl_device::blend_func(int c1, int c2, int a1, int a2)
\r
360 std::array<int, 4> func = {c1, c2, a1, a2};
\r
362 if(blend_func_ != func)
\r
364 blend_func_ = func;
\r
365 GL(glBlendFuncSeparate(c1, c2, a1, a2));
\r
369 void ogl_device::blend_func(int c1, int c2)
\r
371 blend_func(c1, c2, c1, c2);
\r