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