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
30 #include <common/assert.h>
\r
31 #include <common/except.h>
\r
32 #include <common/gl/gl_check.h>
\r
34 #include <boost/foreach.hpp>
\r
36 #include <gl/glew.h>
\r
38 #include <windows.h>
\r
40 #include <SFML/Window/Context.hpp>
\r
43 #include <unordered_map>
\r
45 #include <tbb/concurrent_unordered_map.h>
\r
46 #include <tbb/concurrent_queue.h>
\r
47 #include <tbb/atomic.h>
\r
49 tbb::atomic<int> g_count = tbb::atomic<int>();
\r
51 namespace caspar { namespace accelerator { namespace ogl {
\r
53 struct device::impl : public std::enable_shared_from_this<impl>
\r
55 std::map<host_buffer*, spl::shared_ptr<device_buffer>> write_buffer_transfer_cache_;
\r
57 std::unique_ptr<sf::Context> device_;
\r
58 std::unique_ptr<sf::Context> host_alloc_device_;
\r
60 std::array<tbb::concurrent_unordered_map<int, tbb::concurrent_bounded_queue<std::shared_ptr<device_buffer>>>, 4> device_pools_;
\r
61 std::array<tbb::concurrent_unordered_map<int, tbb::concurrent_bounded_queue<std::shared_ptr<host_buffer>>>, 2> host_pools_;
\r
65 executor& executor_;
\r
66 executor host_alloc_executor_;
\r
68 impl(executor& executor)
\r
69 : executor_(executor)
\r
70 , host_alloc_executor_(L"OpenGL allocation device")
\r
73 CASPAR_LOG(warning) << L"Multiple OGL devices.";
\r
75 CASPAR_LOG(info) << L"Initializing OpenGL Device.";
\r
77 auto ctx1 = executor_.invoke([=]() -> HGLRC
\r
79 device_.reset(new sf::Context());
\r
80 device_->SetActive(true);
\r
82 if (glewInit() != GLEW_OK)
\r
83 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
85 if(!GLEW_VERSION_3_0)
\r
86 BOOST_THROW_EXCEPTION(not_supported() << msg_info("Your graphics card does not meet the minimum hardware requirements since it does not support OpenGL 3.0 or higher."));
\r
88 glGenFramebuffers(1, &fbo_);
\r
89 glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
\r
91 auto ctx1 = wglGetCurrentContext();
\r
93 device_->SetActive(false);
\r
98 host_alloc_executor_.invoke([=]
\r
100 host_alloc_device_.reset(new sf::Context());
\r
101 host_alloc_device_->SetActive(true);
\r
102 auto ctx2 = wglGetCurrentContext();
\r
104 if(!wglShareLists(ctx1, ctx2))
\r
105 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to share OpenGL devices."));
\r
108 executor_.invoke([=]
\r
110 device_->SetActive(true);
\r
113 CASPAR_LOG(info) << L"Successfully initialized OpenGL " << version();
\r
118 host_alloc_executor_.invoke([=]
\r
120 host_alloc_device_.reset();
\r
121 BOOST_FOREACH(auto& pool, host_pools_)
\r
125 executor_.invoke([=]
\r
127 BOOST_FOREACH(auto& pool, device_pools_)
\r
129 glDeleteFramebuffers(1, &fbo_);
\r
135 spl::shared_ptr<device_buffer> allocate_device_buffer(int width, int height, int stride)
\r
137 return executor_.invoke([&]() -> spl::shared_ptr<device_buffer>
\r
141 return spl::make_shared<device_buffer>(width, height, stride);
\r
145 CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";
\r
151 spl::shared_ptr<device_buffer> create_device_buffer(int width, int height, int stride)
\r
153 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
154 CASPAR_VERIFY(width > 0 && height > 0);
\r
156 auto pool = &device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
158 std::shared_ptr<device_buffer> buffer;
\r
159 if(!pool->try_pop(buffer))
\r
160 buffer = allocate_device_buffer(width, height, stride);
\r
162 auto self = shared_from_this();
\r
163 return spl::shared_ptr<device_buffer>(buffer.get(), [self, buffer, pool](device_buffer*) mutable
\r
165 pool->push(buffer);
\r
169 spl::shared_ptr<host_buffer> allocate_host_buffer(int size, host_buffer::usage usage)
\r
171 return host_alloc_executor_.invoke([=]() -> spl::shared_ptr<host_buffer>
\r
175 auto buffer = spl::make_shared<host_buffer>(size, usage);
\r
176 if(usage == host_buffer::usage::write_only)
\r
185 CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";
\r
191 spl::shared_ptr<host_buffer> create_host_buffer(int size, host_buffer::usage usage)
\r
193 CASPAR_VERIFY(usage == host_buffer::usage::write_only || usage == host_buffer::usage::read_only);
\r
194 CASPAR_VERIFY(size > 0);
\r
196 auto pool = &host_pools_[usage.value()][size];
\r
198 std::shared_ptr<host_buffer> buffer;
\r
199 if(!pool->try_pop(buffer))
\r
200 buffer = allocate_host_buffer(size, usage);
\r
202 bool is_write = (usage == host_buffer::usage::write_only);
\r
204 auto self = shared_from_this();
\r
205 return spl::shared_ptr<host_buffer>(buffer.get(), [self, is_write, buffer, pool](host_buffer*) mutable
\r
207 self->host_alloc_executor_.begin_invoke([=]() mutable
\r
214 pool->push(buffer);
\r
215 }, task_priority::high_priority);
\r
217 self->executor_.begin_invoke([=]
\r
219 write_buffer_transfer_cache_.erase(buffer.get());
\r
220 }, task_priority::high_priority);
\r
224 std::wstring version()
\r
226 static std::wstring ver = L"Not found";
\r
229 ver = u16(executor_.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})
\r
230 + " " + executor_.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));
\r
237 boost::unique_future<spl::shared_ptr<device_buffer>> copy_async(spl::shared_ptr<host_buffer>& source, int width, int height, int stride)
\r
239 return executor_.begin_invoke([=]() -> spl::shared_ptr<device_buffer>
\r
241 auto buffer_it = write_buffer_transfer_cache_.find(source.get());
\r
242 if(buffer_it == write_buffer_transfer_cache_.end())
\r
244 auto result = create_device_buffer(width, height, stride);
\r
245 result->copy_from(*source);
\r
246 buffer_it = write_buffer_transfer_cache_.insert(std::make_pair(source.get(), result)).first;
\r
248 return buffer_it->second;
\r
249 }, task_priority::high_priority);
\r
254 : executor_(L"device")
\r
255 , impl_(new impl(executor_))
\r
259 spl::shared_ptr<device_buffer> device::create_device_buffer(int width, int height, int stride){return impl_->create_device_buffer(width, height, stride);}
\r
260 spl::shared_ptr<host_buffer> device::create_host_buffer(int size, host_buffer::usage usage){return impl_->create_host_buffer(size, usage);}
\r
261 boost::unique_future<spl::shared_ptr<device_buffer>> device::copy_async(spl::shared_ptr<host_buffer>& source, int width, int height, int stride){return impl_->copy_async(source, width, height, stride);}
\r
262 std::wstring device::version(){return impl_->version();}
\r