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/future.h>
\r
35 #include <common/array.h>
\r
36 #include <common/gl/gl_check.h>
\r
37 #include <common/os/windows/windows.h>
\r
39 #include <boost/foreach.hpp>
\r
41 #include <gl/glew.h>
\r
43 #include <SFML/Window/Context.hpp>
\r
45 #include <tbb/concurrent_unordered_map.h>
\r
46 #include <tbb/concurrent_hash_map.h>
\r
47 #include <tbb/concurrent_queue.h>
\r
49 #include <boost/utility/declval.hpp>
\r
52 #include <unordered_map>
\r
55 #include <tbb/parallel_for.h>
\r
57 namespace caspar { namespace accelerator { namespace ogl {
\r
59 struct device::impl : public std::enable_shared_from_this<impl>
\r
61 static_assert(std::is_same<decltype(boost::declval<device>().impl_), spl::shared_ptr<impl>>::value, "impl_ must be shared_ptr");
\r
63 tbb::concurrent_hash_map<buffer*, std::shared_ptr<texture>> texture_cache_;
\r
65 std::unique_ptr<sf::Context> device_;
\r
67 std::array<tbb::concurrent_unordered_map<std::size_t, tbb::concurrent_bounded_queue<std::shared_ptr<texture>>>, 4> device_pools_;
\r
68 std::array<tbb::concurrent_unordered_map<std::size_t, tbb::concurrent_bounded_queue<std::shared_ptr<buffer>>>, 2> host_pools_;
\r
72 executor& executor_;
\r
74 impl(executor& executor)
\r
75 : executor_(executor)
\r
77 executor_.set_capacity(256);
\r
79 CASPAR_LOG(info) << L"Initializing OpenGL Device.";
\r
81 executor_.invoke([=]
\r
83 device_.reset(new sf::Context());
\r
84 device_->SetActive(true);
\r
86 if (glewInit() != GLEW_OK)
\r
87 CASPAR_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
89 if(!GLEW_VERSION_3_0)
\r
90 CASPAR_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
92 glGenFramebuffers(1, &fbo_);
\r
93 glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
\r
96 CASPAR_LOG(info) << L"Successfully initialized OpenGL " << version();
\r
101 executor_.invoke([=]
\r
103 BOOST_FOREACH(auto& pool, device_pools_)
\r
105 glDeleteFramebuffers(1, &fbo_);
\r
111 std::wstring version()
\r
115 return executor_.invoke([]
\r
117 return u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VERSION)))) + L" " + u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VENDOR))));
\r
122 return L"Not found";;
\r
126 spl::shared_ptr<texture> create_texture(int width, int height, int stride, bool clear = false)
\r
128 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
129 CASPAR_VERIFY(width > 0 && height > 0);
\r
131 if(!executor_.is_current())
\r
132 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Operation only valid in an OpenGL Context."));
\r
134 auto pool = &device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
136 std::shared_ptr<texture> tex;
\r
137 if(!pool->try_pop(tex))
\r
138 tex = spl::make_shared<texture>(width, height, stride);
\r
143 return spl::shared_ptr<texture>(tex.get(), [tex, pool](texture*) mutable
\r
149 spl::shared_ptr<buffer> create_buffer(std::size_t size, buffer::usage usage)
\r
151 CASPAR_VERIFY(size > 0);
\r
153 auto pool = &host_pools_[usage.value()][size];
\r
155 std::shared_ptr<buffer> buf;
\r
156 if(!pool->try_pop(buf))
\r
158 buf = executor_.invoke([&]
\r
160 return spl::make_shared<buffer>(size, usage);
\r
161 }, task_priority::high_priority);
\r
164 auto self = shared_from_this(); // buffers can leave the device context, take a hold on life-time.
\r
165 return spl::shared_ptr<buffer>(buf.get(), [=](buffer*) mutable
\r
167 texture_cache_.erase(buf.get());
\r
172 array<std::uint8_t> create_array(std::size_t size)
\r
174 auto buf = create_buffer(size, buffer::usage::write_only);
\r
175 return array<std::uint8_t>(buf->data(), buf->size(), false, buf);
\r
178 template<typename T>
\r
179 std::shared_ptr<buffer> copy_to_buf(const T& source)
\r
181 std::shared_ptr<buffer> buf;
\r
183 auto tmp = source.storage<spl::shared_ptr<buffer>>();
\r
188 buf = create_buffer(source.size(), buffer::usage::write_only);
\r
189 tbb::parallel_for(tbb::blocked_range<std::size_t>(0, source.size()), [&](const tbb::blocked_range<std::size_t>& r)
\r
191 A_memcpy(buf->data() + r.begin(), source.data() + r.begin(), r.size());
\r
198 // TODO: Since the returned texture is cached it SHOULD NOT be modified.
\r
199 boost::unique_future<spl::shared_ptr<texture>> copy_async(const array<const std::uint8_t>& source, int width, int height, int stride)
\r
201 std::shared_ptr<buffer> buf = copy_to_buf(source);
\r
203 return executor_.begin_invoke([=]() -> spl::shared_ptr<texture>
\r
205 tbb::concurrent_hash_map<buffer*, std::shared_ptr<texture>>::const_accessor a;
\r
206 if(texture_cache_.find(a, buf.get()))
\r
207 return spl::make_shared_ptr(a->second);
\r
209 auto texture = create_texture(width, height, stride);
\r
210 texture->copy_from(*buf);
\r
212 texture_cache_.insert(std::make_pair(buf.get(), texture));
\r
215 }, task_priority::high_priority);
\r
218 boost::unique_future<spl::shared_ptr<texture>> copy_async(const array<std::uint8_t>& source, int width, int height, int stride)
\r
220 std::shared_ptr<buffer> buf = copy_to_buf(source);
\r
222 return executor_.begin_invoke([=]() -> spl::shared_ptr<texture>
\r
224 auto texture = create_texture(width, height, stride, false);
\r
225 texture->copy_from(*buf);
\r
227 }, task_priority::high_priority);
\r
230 boost::unique_future<array<const std::uint8_t>> copy_async(const spl::shared_ptr<texture>& source)
\r
232 if(!executor_.is_current())
\r
233 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Operation only valid in an OpenGL Context."));
\r
235 auto buffer = create_buffer(source->size(), buffer::usage::read_only);
\r
236 source->copy_to(*buffer);
\r
238 auto self = shared_from_this();
\r
239 return async(launch::deferred, [self, buffer]() mutable -> array<const std::uint8_t>
\r
241 self->executor_.invoke(std::bind(&buffer::map, std::ref(buffer))); // Defer blocking "map" call until data is needed.
\r
242 return array<const std::uint8_t>(buffer->data(), buffer->size(), true, buffer);
\r
248 : executor_(L"OpenGL Rendering Context")
\r
249 , impl_(new impl(executor_)){}
\r
250 device::~device(){}
\r
251 spl::shared_ptr<texture> device::create_texture(int width, int height, int stride){return impl_->create_texture(width, height, stride, true);}
\r
252 array<std::uint8_t> device::create_array(int size){return impl_->create_array(size);}
\r
253 boost::unique_future<spl::shared_ptr<texture>> device::copy_async(const array<const std::uint8_t>& source, int width, int height, int stride){return impl_->copy_async(source, width, height, stride);}
\r
254 boost::unique_future<spl::shared_ptr<texture>> device::copy_async(const array<std::uint8_t>& source, int width, int height, int stride){return impl_->copy_async(source, width, height, stride);}
\r
255 boost::unique_future<array<const std::uint8_t>> device::copy_async(const spl::shared_ptr<texture>& source){return impl_->copy_async(source);}
\r
256 std::wstring device::version() const{return impl_->version();}
\r