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 "context.h"
\r
30 #include <common/except.h>
\r
31 #include <common/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 accelerator { namespace ogl {
\r
41 : executor_(L"context")
\r
43 CASPAR_LOG(info) << L"Initializing OpenGL Device.";
\r
47 context_.reset(new sf::Context());
\r
48 context_->SetActive(true);
\r
50 if (glewInit() != GLEW_OK)
\r
51 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
53 CASPAR_LOG(info) << L"OpenGL " << version();
\r
55 if(!GLEW_VERSION_3_0)
\r
56 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
58 glGenFramebuffers(1, &fbo_);
\r
60 CASPAR_LOG(info) << L"Successfully initialized OpenGL Device.";
\r
68 BOOST_FOREACH(auto& pool, device_pools_)
\r
70 BOOST_FOREACH(auto& pool, host_pools_)
\r
72 glDeleteFramebuffers(1, &fbo_);
\r
76 spl::shared_ptr<device_buffer> context::allocate_device_buffer(int width, int height, int stride)
\r
78 std::shared_ptr<device_buffer> buffer;
\r
81 buffer.reset(new device_buffer(shared_from_this(), width, height, stride));
\r
91 buffer.reset(new device_buffer(shared_from_this(), width, height, stride));
\r
95 CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";
\r
99 return spl::make_shared_ptr(buffer);
\r
102 spl::shared_ptr<device_buffer> context::create_device_buffer(int width, int height, int stride)
\r
104 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
105 CASPAR_VERIFY(width > 0 && height > 0);
\r
106 auto& pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
107 std::shared_ptr<device_buffer> buffer;
\r
108 if(!pool->items.try_pop(buffer))
\r
109 buffer = executor_.invoke([&]{return allocate_device_buffer(width, height, stride);}, task_priority::high_priority);
\r
111 //++pool->usage_count;
\r
113 return spl::shared_ptr<device_buffer>(buffer.get(), [=](device_buffer*) mutable
\r
115 pool->items.push(buffer);
\r
119 spl::shared_ptr<host_buffer> context::allocate_host_buffer(int size, host_buffer::usage usage)
\r
121 std::shared_ptr<host_buffer> buffer;
\r
125 buffer.reset(new host_buffer(shared_from_this(), size, usage));
\r
126 if(usage == host_buffer::usage::write_only)
\r
139 buffer.reset(new host_buffer(shared_from_this(), size, usage));
\r
140 if(usage == host_buffer::usage::write_only)
\r
147 CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";
\r
152 return spl::make_shared_ptr(buffer);
\r
155 spl::shared_ptr<host_buffer> context::create_host_buffer(int size, host_buffer::usage usage)
\r
157 CASPAR_VERIFY(usage == host_buffer::usage::write_only || usage == host_buffer::usage::read_only);
\r
158 CASPAR_VERIFY(size > 0);
\r
159 auto& pool = host_pools_[usage.value()][size];
\r
160 std::shared_ptr<host_buffer> buffer;
\r
161 if(!pool->items.try_pop(buffer))
\r
162 buffer = executor_.invoke([=]{return allocate_host_buffer(size, usage);}, task_priority::high_priority);
\r
164 auto self = shared_from_this();
\r
165 bool is_write_only = (usage == host_buffer::usage::write_only);
\r
166 return spl::shared_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable
\r
168 self->executor_.begin_invoke([=]() mutable
\r
175 pool->items.push(buffer);
\r
176 }, task_priority::high_priority);
\r
180 spl::shared_ptr<context> context::create()
\r
182 return spl::shared_ptr<context>(new context());
\r
185 //template<typename T>
\r
186 //void flush_pool(buffer_pool<T>& pool)
\r
188 // if(pool.flush_count.fetch_and_increment() < 16)
\r
191 // if(pool.usage_count.fetch_and_store(0) < pool.items.size())
\r
193 // std::shared_ptr<T> buffer;
\r
194 // pool.items.try_pop(buffer);
\r
197 // pool.flush_count = 0;
\r
198 // pool.usage_count = 0;
\r
201 boost::unique_future<void> context::gc()
\r
203 return begin_invoke([=]
\r
205 CASPAR_LOG(info) << " ogl: Running GC.";
\r
209 BOOST_FOREACH(auto& pools, device_pools_)
\r
211 BOOST_FOREACH(auto& pool, pools)
\r
212 pool.second->items.clear();
\r
214 BOOST_FOREACH(auto& pools, host_pools_)
\r
216 BOOST_FOREACH(auto& pool, pools)
\r
217 pool.second->items.clear();
\r
222 CASPAR_LOG_CURRENT_EXCEPTION();
\r
224 }, task_priority::high_priority);
\r
227 std::wstring context::version()
\r
229 static std::wstring ver = L"Not found";
\r
232 ver = u16(invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})
\r
233 + " " + invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));
\r
240 void context::attach(device_buffer& texture)
\r
242 glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
\r
243 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));
\r
246 void context::clear(device_buffer& texture)
\r
249 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
252 void context::use(shader& shader)
\r
254 GL(glUseProgramObjectARB(shader.id()));
\r
257 boost::unique_future<spl::shared_ptr<device_buffer>> context::copy_async(spl::shared_ptr<host_buffer>& source, int width, int height, int stride)
\r
259 return executor_.begin_invoke([=]() -> spl::shared_ptr<device_buffer>
\r
261 auto result = create_device_buffer(width, height, stride);
\r
262 result->copy_from(source);
\r
264 }, task_priority::high_priority);
\r
267 void context::yield()
\r
269 executor_.yield(task_priority::high_priority);
\r