]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/device.cpp
2.1.0: -ogl/device: Supports non-gpu arrays.
[casparcg] / accelerator / ogl / util / device.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\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
10 *\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
15 *\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
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 // TODO: Smart GC\r
23 \r
24 #include "../../stdafx.h"\r
25 \r
26 #include "device.h"\r
27 \r
28 #include "buffer.h"\r
29 #include "texture.h"\r
30 #include "shader.h"\r
31 \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
38 \r
39 #include <boost/foreach.hpp>\r
40 \r
41 #include <gl/glew.h>\r
42 \r
43 #include <SFML/Window/Context.hpp>\r
44 \r
45 #include <tbb/concurrent_unordered_map.h>\r
46 #include <tbb/concurrent_hash_map.h>\r
47 #include <tbb/concurrent_queue.h>\r
48 \r
49 #include <boost/utility/declval.hpp>\r
50 \r
51 #include <array>\r
52 #include <unordered_map>\r
53 \r
54 #include <asmlib.h>\r
55 #include <tbb/parallel_for.h>\r
56 \r
57 tbb::atomic<int> g_count = tbb::atomic<int>();\r
58 \r
59 namespace caspar { namespace accelerator { namespace ogl {\r
60                 \r
61 struct device::impl : public std::enable_shared_from_this<impl>\r
62 {       \r
63         static_assert(std::is_same<decltype(boost::declval<device>().impl_), spl::shared_ptr<impl>>::value, "impl_ must be shared_ptr");\r
64 \r
65         tbb::concurrent_hash_map<buffer*, std::shared_ptr<texture>> texture_cache_;\r
66 \r
67         std::unique_ptr<sf::Context> device_;\r
68         std::unique_ptr<sf::Context> host_alloc_device_;\r
69         \r
70         std::array<tbb::concurrent_unordered_map<std::size_t, tbb::concurrent_bounded_queue<std::shared_ptr<texture>>>, 4>      device_pools_;\r
71         std::array<tbb::concurrent_unordered_map<std::size_t, tbb::concurrent_bounded_queue<std::shared_ptr<buffer>>>, 2>       host_pools_;\r
72         \r
73         GLuint fbo_;\r
74 \r
75         executor& render_executor_;\r
76         executor  alloc_executor_;\r
77                                 \r
78         impl(executor& executor) \r
79                 : render_executor_(executor)\r
80                 , alloc_executor_(L"OpenGL allocation context.")\r
81         {\r
82                 if(g_count++ > 1)\r
83                         CASPAR_LOG(warning) << L"Multiple OGL devices.";\r
84 \r
85                 CASPAR_LOG(info) << L"Initializing OpenGL Device.";\r
86                 \r
87                 auto ctx1 = render_executor_.invoke([=]() -> HGLRC \r
88                 {\r
89                         device_.reset(new sf::Context());\r
90                         device_->SetActive(true);               \r
91                                                 \r
92                         if (glewInit() != GLEW_OK)\r
93                                 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));\r
94                 \r
95                         if(!GLEW_VERSION_3_0)\r
96                                 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
97         \r
98                         glGenFramebuffers(1, &fbo_);                            \r
99                         glBindFramebuffer(GL_FRAMEBUFFER, fbo_);\r
100                         \r
101                         auto ctx1 = wglGetCurrentContext();\r
102                         \r
103                         device_->SetActive(false);\r
104 \r
105                         return ctx1;\r
106                 });\r
107 \r
108                 alloc_executor_.invoke([=]\r
109                 {\r
110                         host_alloc_device_.reset(new sf::Context());\r
111                         host_alloc_device_->SetActive(true);    \r
112                         auto ctx2 = wglGetCurrentContext();\r
113 \r
114                         if(!wglShareLists(ctx1, ctx2))\r
115                                 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to share OpenGL devices."));\r
116                 });\r
117 \r
118                 render_executor_.invoke([=]\r
119                 {               \r
120                         device_->SetActive(true);\r
121                 });\r
122                 \r
123                 CASPAR_LOG(info) << L"Successfully initialized OpenGL " << version();\r
124         }\r
125 \r
126         ~impl()\r
127         {\r
128                 alloc_executor_.invoke([=]\r
129                 {\r
130                         host_alloc_device_.reset();\r
131                         BOOST_FOREACH(auto& pool, host_pools_)\r
132                                 pool.clear();\r
133                 });\r
134 \r
135                 render_executor_.invoke([=]\r
136                 {\r
137                         BOOST_FOREACH(auto& pool, device_pools_)\r
138                                 pool.clear();\r
139                         glDeleteFramebuffers(1, &fbo_);\r
140 \r
141                         device_.reset();\r
142                 });\r
143         }\r
144                 \r
145         std::wstring version()\r
146         {       \r
147                 try\r
148                 {\r
149                         return alloc_executor_.invoke([]\r
150                         {\r
151                                 return u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VERSION)))) + L" " + u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VENDOR))));\r
152                         });     \r
153                 }\r
154                 catch(...)\r
155                 {\r
156                         return L"Not found";;\r
157                 }\r
158         }\r
159                                                         \r
160         spl::shared_ptr<texture> create_texture(int width, int height, int stride, bool clear = false)\r
161         {\r
162                 CASPAR_VERIFY(stride > 0 && stride < 5);\r
163                 CASPAR_VERIFY(width > 0 && height > 0);\r
164 \r
165                 if(!render_executor_.is_current())\r
166                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Operation only valid in an OpenGL Context."));\r
167                                         \r
168                 auto pool = &device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
169                 \r
170                 std::shared_ptr<texture> buffer;\r
171                 if(!pool->try_pop(buffer))              \r
172                         buffer = spl::make_shared<texture>(width, height, stride);\r
173         \r
174                 if(clear)\r
175                         buffer->clear();\r
176 \r
177                 return spl::shared_ptr<texture>(buffer.get(), [buffer, pool](texture*) mutable\r
178                 {               \r
179                         pool->push(buffer);     \r
180                 });\r
181         }\r
182                 \r
183         spl::shared_ptr<buffer> create_buffer(std::size_t size, buffer::usage usage)\r
184         {\r
185                 CASPAR_VERIFY(size > 0);\r
186                 \r
187                 auto pool = &host_pools_[usage.value()][size];\r
188                 \r
189                 std::shared_ptr<buffer> buf;\r
190                 if(!pool->try_pop(buf)) \r
191                 {\r
192                         buf = alloc_executor_.invoke([&]\r
193                         {\r
194                                 return spl::make_shared<buffer>(size, usage);\r
195                         });\r
196                 }\r
197                 \r
198                 auto self = shared_from_this(); // buffers can leave the device context, take a hold on life-time.\r
199                 return spl::shared_ptr<buffer>(buf.get(), [=](buffer*) mutable\r
200                 {       \r
201                         texture_cache_.erase(buf.get());\r
202                         pool->push(buf);\r
203                 });\r
204         }\r
205 \r
206         array<std::uint8_t> create_array(std::size_t size)\r
207         {               \r
208                 auto buf = create_buffer(size, buffer::usage::write_only);\r
209                 return array<std::uint8_t>(buf->data(), buf->size(), false, buf);\r
210         }\r
211 \r
212         template<typename T>\r
213         std::shared_ptr<buffer> copy_to_buf(const T& source)\r
214         {\r
215                 std::shared_ptr<buffer> buf;\r
216 \r
217                 auto tmp = source.storage<spl::shared_ptr<buffer>>();\r
218                 if(tmp)\r
219                         buf = *tmp;\r
220                 else\r
221                 {                       \r
222                         buf = create_buffer(source.size(), buffer::usage::write_only);\r
223                         tbb::parallel_for(tbb::blocked_range<std::size_t>(0, source.size()), [&](const tbb::blocked_range<std::size_t>& r)\r
224                         {\r
225                                 A_memcpy(buf->data() + r.begin(), source.data() + r.begin(), r.size());\r
226                         });\r
227                 }\r
228 \r
229                 return buf;\r
230         }\r
231 \r
232         boost::unique_future<spl::shared_ptr<texture>> copy_async(const array<const std::uint8_t>& source, int width, int height, int stride)\r
233         {\r
234                 std::shared_ptr<buffer> buf = copy_to_buf(source);\r
235                                 \r
236                 return render_executor_.begin_invoke([=]() -> spl::shared_ptr<texture>\r
237                 {\r
238                         tbb::concurrent_hash_map<buffer*, std::shared_ptr<texture>>::const_accessor a;\r
239                         if(texture_cache_.find(a, buf.get()))\r
240                                 return spl::make_shared_ptr(a->second);\r
241 \r
242                         auto texture = create_texture(width, height, stride);\r
243                         texture->copy_from(*buf);       \r
244 \r
245                         texture_cache_.insert(std::make_pair(buf.get(), texture));\r
246 \r
247                         return texture;\r
248                 }, task_priority::high_priority);\r
249         }\r
250         \r
251         boost::unique_future<spl::shared_ptr<texture>> copy_async(const array<std::uint8_t>& source, int width, int height, int stride)\r
252         {\r
253                 std::shared_ptr<buffer> buf = copy_to_buf(source);\r
254 \r
255                 return render_executor_.begin_invoke([=]() -> spl::shared_ptr<texture>\r
256                 {\r
257                         auto texture = create_texture(width, height, stride, false);\r
258                         texture->copy_from(*buf);                               \r
259                         return texture;\r
260                 }, task_priority::high_priority);\r
261         }\r
262 \r
263         boost::unique_future<array<const std::uint8_t>> copy_async(const spl::shared_ptr<texture>& source)\r
264         {\r
265                 if(!render_executor_.is_current())\r
266                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Operation only valid in an OpenGL Context."));\r
267 \r
268                 auto buffer = create_buffer(source->size(), buffer::usage::read_only); \r
269                 source->copy_to(*buffer);       \r
270 \r
271                 auto self = shared_from_this();\r
272                 return async(launch::deferred, [self, buffer]() mutable -> array<const std::uint8_t>\r
273                 {\r
274                         self->alloc_executor_.invoke(std::bind(&buffer::map, std::ref(buffer))); // Defer blocking "map" call until data is needed.\r
275                         return array<const std::uint8_t>(buffer->data(), buffer->size(), true, buffer);\r
276                 });\r
277         }\r
278 };\r
279 \r
280 device::device() \r
281         : executor_(L"OpenGL Rendering Context.")\r
282         , impl_(new impl(executor_)){}\r
283 device::~device(){}\r
284 spl::shared_ptr<texture>                                                        device::create_texture(int width, int height, int stride){return impl_->create_texture(width, height, stride, true);}\r
285 array<std::uint8_t>                                                                     device::create_array(int size){return impl_->create_array(size);}\r
286 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
287 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
288 boost::unique_future<array<const std::uint8_t>>         device::copy_async(const spl::shared_ptr<texture>& source){return impl_->copy_async(source);}\r
289 std::wstring                                                                            device::version() const{return impl_->version();}\r
290 \r
291 \r
292 }}}\r
293 \r
294 \r