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
47 CASPAR_LOG(info) << L"Initializing OpenGL Device.";
\r
49 std::fill(binded_textures_.begin(), binded_textures_.end(), 0);
\r
50 std::fill(viewport_.begin(), viewport_.end(), 0);
\r
51 std::fill(scissor_.begin(), scissor_.end(), 0);
\r
52 std::fill(blend_func_.begin(), blend_func_.end(), 0);
\r
56 context_.reset(new sf::Context());
\r
57 context_->SetActive(true);
\r
59 if (glewInit() != GLEW_OK)
\r
60 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
62 CASPAR_LOG(info) << L"OpenGL " << version();
\r
64 if(!GLEW_VERSION_3_0)
\r
65 CASPAR_LOG(warning) << "Missing OpenGL 3.0 support.";
\r
67 CASPAR_LOG(info) << L"Successfully initialized GLEW.";
\r
69 glGenFramebuffers(1, &fbo_);
\r
70 glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
\r
72 CASPAR_LOG(info) << L"Successfully initialized OpenGL Device.";
\r
76 ogl_device::~ogl_device()
\r
80 BOOST_FOREACH(auto& pool, device_pools_)
\r
82 BOOST_FOREACH(auto& pool, host_pools_)
\r
84 glDeleteFramebuffersEXT(1, &fbo_);
\r
88 safe_ptr<device_buffer> ogl_device::allocate_device_buffer(size_t width, size_t height, size_t stride)
\r
90 std::shared_ptr<device_buffer> buffer;
\r
93 buffer.reset(new device_buffer(width, height, stride));
\r
103 buffer.reset(new device_buffer(width, height, stride));
\r
107 CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";
\r
111 return make_safe_ptr(buffer);
\r
114 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)
\r
116 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
117 CASPAR_VERIFY(width > 0 && height > 0);
\r
118 auto& pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
119 std::shared_ptr<device_buffer> buffer;
\r
120 if(!pool->items.try_pop(buffer))
\r
121 buffer = executor_.invoke([&]{return allocate_device_buffer(width, height, stride);}, high_priority);
\r
123 //++pool->usage_count;
\r
125 return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*) mutable
\r
127 pool->items.push(buffer);
\r
131 safe_ptr<host_buffer> ogl_device::allocate_host_buffer(size_t size, host_buffer::usage_t usage)
\r
133 std::shared_ptr<host_buffer> buffer;
\r
137 buffer.reset(new host_buffer(size, usage));
\r
138 if(usage == host_buffer::write_only)
\r
151 buffer.reset(new host_buffer(size, usage));
\r
152 if(usage == host_buffer::write_only)
\r
159 CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";
\r
164 return make_safe_ptr(buffer);
\r
167 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)
\r
169 CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);
\r
170 CASPAR_VERIFY(size > 0);
\r
171 auto& pool = host_pools_[usage][size];
\r
172 std::shared_ptr<host_buffer> buffer;
\r
173 if(!pool->items.try_pop(buffer))
\r
174 buffer = executor_.invoke([=]{return allocate_host_buffer(size, usage);}, high_priority);
\r
176 //++pool->usage_count;
\r
178 auto self = shared_from_this();
\r
179 return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable
\r
181 self->executor_.begin_invoke([=]() mutable
\r
183 if(usage == host_buffer::write_only)
\r
188 pool->items.push(buffer);
\r
189 }, high_priority);
\r
193 safe_ptr<ogl_device> ogl_device::create()
\r
195 return safe_ptr<ogl_device>(new ogl_device());
\r
198 //template<typename T>
\r
199 //void flush_pool(buffer_pool<T>& pool)
\r
201 // if(pool.flush_count.fetch_and_increment() < 16)
\r
204 // if(pool.usage_count.fetch_and_store(0) < pool.items.size())
\r
206 // std::shared_ptr<T> buffer;
\r
207 // pool.items.try_pop(buffer);
\r
210 // pool.flush_count = 0;
\r
211 // pool.usage_count = 0;
\r
214 void ogl_device::flush()
\r
220 // BOOST_FOREACH(auto& pools, device_pools_)
\r
222 // BOOST_FOREACH(auto& pool, pools)
\r
223 // flush_pool(*pool.second);
\r
225 // BOOST_FOREACH(auto& pools, host_pools_)
\r
227 // BOOST_FOREACH(auto& pool, pools)
\r
228 // flush_pool(*pool.second);
\r
233 // CASPAR_LOG_CURRENT_EXCEPTION();
\r
237 void ogl_device::yield()
\r
242 boost::unique_future<void> ogl_device::gc()
\r
244 return begin_invoke([=]
\r
246 CASPAR_LOG(info) << " ogl: Running GC.";
\r
250 BOOST_FOREACH(auto& pools, device_pools_)
\r
252 BOOST_FOREACH(auto& pool, pools)
\r
253 pool.second->items.clear();
\r
255 BOOST_FOREACH(auto& pools, host_pools_)
\r
257 BOOST_FOREACH(auto& pool, pools)
\r
258 pool.second->items.clear();
\r
263 CASPAR_LOG_CURRENT_EXCEPTION();
\r
268 std::wstring ogl_device::version()
\r
270 static std::wstring ver = L"Not found";
\r
273 ver = widen(invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})
\r
274 + " " + invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));
\r
282 void ogl_device::enable(GLenum cap)
\r
284 auto& val = caps_[cap];
\r
292 void ogl_device::disable(GLenum cap)
\r
294 auto& val = caps_[cap];
\r
302 void ogl_device::viewport(size_t x, size_t y, size_t width, size_t height)
\r
304 if(x != viewport_[0] || y != viewport_[1] || width != viewport_[2] || height != viewport_[3])
\r
306 glViewport(x, y, width, height);
\r
309 viewport_[2] = width;
\r
310 viewport_[3] = height;
\r
314 void ogl_device::scissor(size_t x, size_t y, size_t width, size_t height)
\r
316 if(x != scissor_[0] || y != scissor_[1] || width != scissor_[2] || height != scissor_[3])
\r
318 glScissor(x, y, width, height);
\r
321 scissor_[2] = width;
\r
322 scissor_[3] = height;
\r
326 void ogl_device::stipple_pattern(const GLubyte* pattern)
\r
328 if(pattern_ != pattern)
\r
330 glPolygonStipple(pattern);
\r
331 pattern_ = pattern;
\r
335 void ogl_device::attach(device_buffer& texture)
\r
337 if(attached_texture_ != texture.id())
\r
339 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));
\r
340 attached_texture_ = texture.id();
\r
344 void ogl_device::clear(device_buffer& texture)
\r
347 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
350 void ogl_device::read_buffer(device_buffer&)
\r
352 if(read_buffer_ != GL_COLOR_ATTACHMENT0)
\r
354 GL(glReadBuffer(GL_COLOR_ATTACHMENT0));
\r
355 read_buffer_ = GL_COLOR_ATTACHMENT0;
\r
359 void ogl_device::use(shader& shader)
\r
361 if(active_shader_ != shader.id())
\r
363 GL(glUseProgramObjectARB(shader.id()));
\r
364 active_shader_ = shader.id();
\r
368 void ogl_device::blend_func(int c1, int c2, int a1, int a2)
\r
370 std::array<int, 4> func = {c1, c2, a1, a2};
\r
372 if(blend_func_ != func)
\r
374 blend_func_ = func;
\r
375 GL(glBlendFuncSeparate(c1, c2, a1, a2));
\r
379 void ogl_device::blend_func(int c1, int c2)
\r
381 blend_func(c1, c2, c1, c2);
\r