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
29 #include "texture.h"
\r
32 #include <common/assert.h>
\r
33 #include <common/except.h>
\r
34 #include <common/concurrency/async.h>
\r
35 #include <common/gl/gl_check.h>
\r
36 #include <common/os/windows/windows.h>
\r
38 #include <boost/foreach.hpp>
\r
40 #include <gl/glew.h>
\r
42 #include <SFML/Window/Context.hpp>
\r
44 #include <tbb/concurrent_unordered_map.h>
\r
45 #include <tbb/concurrent_hash_map.h>
\r
46 #include <tbb/concurrent_queue.h>
\r
48 #include <boost/utility/declval.hpp>
\r
51 #include <unordered_map>
\r
53 tbb::atomic<int> g_count = tbb::atomic<int>();
\r
55 namespace caspar { namespace accelerator { namespace ogl {
\r
57 struct device::impl : public std::enable_shared_from_this<impl>
\r
59 static_assert(std::is_same<decltype(boost::declval<device>().impl_), spl::shared_ptr<impl>>::value, "impl_ must be shared_ptr");
\r
61 tbb::concurrent_hash_map<buffer*, std::shared_ptr<texture>> texture_mapping_;
\r
63 std::unique_ptr<sf::Context> device_;
\r
64 std::unique_ptr<sf::Context> host_alloc_device_;
\r
66 std::array<tbb::concurrent_unordered_map<int, tbb::concurrent_bounded_queue<std::shared_ptr<texture>>>, 4> device_pools_;
\r
67 std::array<tbb::concurrent_unordered_map<int, tbb::concurrent_bounded_queue<std::shared_ptr<buffer>>>, 2> host_pools_;
\r
71 executor& render_executor_;
\r
72 executor alloc_executor_;
\r
74 impl(executor& executor)
\r
75 : render_executor_(executor)
\r
76 , alloc_executor_(L"OpenGL allocation context.")
\r
79 CASPAR_LOG(warning) << L"Multiple OGL devices.";
\r
81 CASPAR_LOG(info) << L"Initializing OpenGL Device.";
\r
83 auto ctx1 = render_executor_.invoke([=]() -> HGLRC
\r
85 device_.reset(new sf::Context());
\r
86 device_->SetActive(true);
\r
88 if (glewInit() != GLEW_OK)
\r
89 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
91 if(!GLEW_VERSION_3_0)
\r
92 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
94 glGenFramebuffers(1, &fbo_);
\r
95 glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
\r
97 auto ctx1 = wglGetCurrentContext();
\r
99 device_->SetActive(false);
\r
104 alloc_executor_.invoke([=]
\r
106 host_alloc_device_.reset(new sf::Context());
\r
107 host_alloc_device_->SetActive(true);
\r
108 auto ctx2 = wglGetCurrentContext();
\r
110 if(!wglShareLists(ctx1, ctx2))
\r
111 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to share OpenGL devices."));
\r
114 render_executor_.invoke([=]
\r
116 device_->SetActive(true);
\r
119 CASPAR_LOG(info) << L"Successfully initialized OpenGL " << version();
\r
124 alloc_executor_.invoke([=]
\r
126 host_alloc_device_.reset();
\r
127 BOOST_FOREACH(auto& pool, host_pools_)
\r
131 render_executor_.invoke([=]
\r
133 BOOST_FOREACH(auto& pool, device_pools_)
\r
135 glDeleteFramebuffers(1, &fbo_);
\r
141 std::wstring version()
\r
145 return alloc_executor_.invoke([]
\r
147 return u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VERSION)))) + L" " + u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VENDOR))));
\r
152 return L"Not found";;
\r
156 spl::shared_ptr<texture> create_texture(int width, int height, int stride)
\r
158 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
159 CASPAR_VERIFY(width > 0 && height > 0);
\r
161 auto pool = &device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
163 std::shared_ptr<texture> buffer;
\r
164 if(!pool->try_pop(buffer))
\r
165 buffer = spl::make_shared<texture>(width, height, stride);
\r
167 return spl::shared_ptr<texture>(buffer.get(), [buffer, pool](texture*) mutable
\r
169 pool->push(buffer);
\r
173 spl::shared_ptr<buffer> create_buffer(int size, buffer::usage usage)
\r
175 CASPAR_VERIFY(size > 0);
\r
177 auto pool = &host_pools_[usage.value()][size];
\r
179 std::shared_ptr<buffer> buf;
\r
180 if(!pool->try_pop(buf))
\r
182 buf = alloc_executor_.invoke([&]
\r
184 return spl::make_shared<buffer>(size, usage);
\r
188 auto ptr = buf->data();
\r
189 auto self = shared_from_this(); // buffers can leave the device context, take a hold on life-time.
\r
191 auto on_release = [self, buf, ptr, usage, pool]() mutable
\r
193 if(usage == buffer::usage::write_only)
\r
198 self->texture_mapping_.erase(buf.get());
\r
203 return spl::shared_ptr<buffer>(buf.get(), [=](buffer*) mutable
\r
205 self->alloc_executor_.begin_invoke(on_release);
\r
209 core::mutable_array create_array(int size)
\r
211 auto buf = create_buffer(size, buffer::usage::write_only);
\r
212 return core::mutable_array(buf->data(), buf->size(), buf);
\r
215 boost::unique_future<spl::shared_ptr<texture>> copy_async(const core::mutable_array& source, int width, int height, int stride)
\r
217 auto buf = boost::any_cast<spl::shared_ptr<buffer>>(source.storage());
\r
219 return render_executor_.begin_invoke([=]() -> spl::shared_ptr<texture>
\r
221 tbb::concurrent_hash_map<buffer*, std::shared_ptr<texture>>::const_accessor a;
\r
222 if(texture_mapping_.find(a, buf.get()))
\r
223 return spl::make_shared_ptr(a->second);
\r
225 auto texture = create_texture(width, height, stride);
\r
226 texture->copy_from(*buf);
\r
228 texture_mapping_.insert(std::make_pair(buf.get(), texture));
\r
232 }, task_priority::high_priority);
\r
235 boost::unique_future<core::const_array> copy_async(const spl::shared_ptr<texture>& source)
\r
237 return fold(render_executor_.begin_invoke([=]() -> boost::shared_future<core::const_array>
\r
239 auto buffer = create_buffer(source->size(), buffer::usage::read_only);
\r
240 source->copy_to(*buffer);
\r
242 return make_shared(async(launch_policy::deferred, [=]() mutable -> core::const_array
\r
244 const auto& buf = buffer.get();
\r
246 alloc_executor_.invoke(std::bind(&buffer::map, std::ref(buf))); // Defer blocking "map" call until data is needed.
\r
248 return core::const_array(buf->data(), buf->size(), buffer);
\r
250 }, task_priority::high_priority));
\r
255 : executor_(L"OpenGL Rendering Context.")
\r
256 , impl_(new impl(executor_))
\r
259 device::~device(){}
\r
260 spl::shared_ptr<texture> device::create_texture(int width, int height, int stride){return impl_->create_texture(width, height, stride);}
\r
261 core::mutable_array device::create_array(int size){return impl_->create_array(size);}
\r
262 boost::unique_future<spl::shared_ptr<texture>> device::copy_async(const core::mutable_array& source, int width, int height, int stride){return impl_->copy_async(source, width, height, stride);}
\r
263 boost::unique_future<core::const_array> device::copy_async(const spl::shared_ptr<texture>& source){return impl_->copy_async(source);}
\r
264 std::wstring device::version(){return impl_->version();}
\r