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 std::fill(binded_textures_.begin(), binded_textures_.end(), 0);
\r
45 std::fill(viewport_.begin(), viewport_.end(), 0);
\r
46 std::fill(scissor_.begin(), scissor_.end(), 0);
\r
47 std::fill(blend_func_.begin(), blend_func_.end(), 0);
\r
51 context_.reset(new sf::Context());
\r
52 context_->SetActive(true);
\r
54 if (glewInit() != GLEW_OK)
\r
55 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
57 if(!GLEW_VERSION_3_2)
\r
58 CASPAR_LOG(warning) << "Missing OpenGL 3.2 support.";
\r
60 GL(glGenFramebuffers(1, &fbo_));
\r
61 GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));
\r
62 GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));
\r
63 GL(glDisable(GL_MULTISAMPLE_ARB));
\r
67 ogl_device::~ogl_device()
\r
71 BOOST_FOREACH(auto& pool, device_pools_)
\r
73 BOOST_FOREACH(auto& pool, host_pools_)
\r
75 glDeleteFramebuffersEXT(1, &fbo_);
\r
79 safe_ptr<device_buffer> ogl_device::allocate_device_buffer(size_t width, size_t height, size_t stride)
\r
81 std::shared_ptr<device_buffer> buffer;
\r
84 buffer.reset(new device_buffer(width, height, stride));
\r
94 buffer.reset(new device_buffer(width, height, stride));
\r
98 CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";
\r
102 return make_safe_ptr(buffer);
\r
105 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)
\r
107 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
108 CASPAR_VERIFY(width > 0 && height > 0);
\r
109 auto& pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
110 std::shared_ptr<device_buffer> buffer;
\r
111 if(!pool->items.try_pop(buffer))
\r
112 buffer = executor_.invoke([&]{return allocate_device_buffer(width, height, stride);}, high_priority);
\r
114 //++pool->usage_count;
\r
116 return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*) mutable
\r
118 pool->items.push(buffer);
\r
122 safe_ptr<host_buffer> ogl_device::allocate_host_buffer(size_t size, host_buffer::usage_t usage)
\r
124 std::shared_ptr<host_buffer> buffer;
\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 return make_safe_ptr(buffer);
\r
158 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)
\r
160 CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);
\r
161 CASPAR_VERIFY(size > 0);
\r
162 auto& pool = host_pools_[usage][size];
\r
163 std::shared_ptr<host_buffer> buffer;
\r
164 if(!pool->items.try_pop(buffer))
\r
165 buffer = executor_.invoke([=]{return allocate_host_buffer(size, usage);}, high_priority);
\r
167 //++pool->usage_count;
\r
169 return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable
\r
171 executor_.begin_invoke([=]() mutable
\r
173 if(usage == host_buffer::write_only)
\r
178 pool->items.push(buffer);
\r
179 }, high_priority);
\r
183 //template<typename T>
\r
184 //void flush_pool(buffer_pool<T>& pool)
\r
186 // if(pool.flush_count.fetch_and_increment() < 16)
\r
189 // if(pool.usage_count.fetch_and_store(0) < pool.items.size())
\r
191 // std::shared_ptr<T> buffer;
\r
192 // pool.items.try_pop(buffer);
\r
195 // pool.flush_count = 0;
\r
196 // pool.usage_count = 0;
\r
199 void ogl_device::flush()
\r
205 // BOOST_FOREACH(auto& pools, device_pools_)
\r
207 // BOOST_FOREACH(auto& pool, pools)
\r
208 // flush_pool(*pool.second);
\r
210 // BOOST_FOREACH(auto& pools, host_pools_)
\r
212 // BOOST_FOREACH(auto& pool, pools)
\r
213 // flush_pool(*pool.second);
\r
218 // CASPAR_LOG_CURRENT_EXCEPTION();
\r
222 void ogl_device::yield()
\r
227 boost::unique_future<void> ogl_device::gc()
\r
229 return begin_invoke([=]
\r
231 CASPAR_LOG(info) << " ogl: Running GC.";
\r
235 BOOST_FOREACH(auto& pools, device_pools_)
\r
237 BOOST_FOREACH(auto& pool, pools)
\r
238 pool.second->items.clear();
\r
240 BOOST_FOREACH(auto& pools, host_pools_)
\r
242 BOOST_FOREACH(auto& pool, pools)
\r
243 pool.second->items.clear();
\r
248 CASPAR_LOG_CURRENT_EXCEPTION();
\r
253 std::wstring ogl_device::get_version()
\r
255 static std::wstring ver = L"Not found";
\r
259 ver = widen(tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})
\r
260 + " " + tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));
\r
268 void ogl_device::enable(GLenum cap)
\r
270 auto& val = caps_[cap];
\r
278 void ogl_device::disable(GLenum cap)
\r
280 auto& val = caps_[cap];
\r
288 void ogl_device::viewport(size_t x, size_t y, size_t width, size_t height)
\r
290 if(x != viewport_[0] || y != viewport_[1] || width != viewport_[2] || height != viewport_[3])
\r
292 glViewport(x, y, width, height);
\r
295 viewport_[2] = width;
\r
296 viewport_[3] = height;
\r
300 void ogl_device::scissor(size_t x, size_t y, size_t width, size_t height)
\r
302 if(x != scissor_[0] || y != scissor_[1] || width != scissor_[2] || height != scissor_[3])
\r
304 glScissor(x, y, width, height);
\r
307 scissor_[2] = width;
\r
308 scissor_[3] = height;
\r
312 void ogl_device::stipple_pattern(const GLubyte* pattern)
\r
314 if(pattern_ != pattern)
\r
316 glPolygonStipple(pattern);
\r
317 pattern_ = pattern;
\r
321 void ogl_device::attach(device_buffer& texture)
\r
323 if(attached_texture_ != texture.id())
\r
325 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));
\r
326 attached_texture_ = texture.id();
\r
330 void ogl_device::clear(device_buffer& texture)
\r
333 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
336 void ogl_device::use(shader& shader)
\r
338 if(active_shader_ != shader.id())
\r
340 GL(glUseProgramObjectARB(shader.id()));
\r
341 active_shader_ = shader.id();
\r
345 void ogl_device::blend_func(int c1, int c2, int a1, int a2)
\r
347 std::array<int, 4> func = {c1, c2, a1, a2};
\r
349 if(blend_func_ != func)
\r
351 blend_func_ = func;
\r
352 GL(glBlendFuncSeparate(c1, c2, a1, a2));
\r
356 void ogl_device::blend_func(int c1, int c2)
\r
358 blend_func(c1, c2, c1, c2);
\r