]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/device.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[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 "shader.h"\r
29 \r
30 #include <common/assert.h>\r
31 #include <common/except.h>\r
32 #include <common/gl/gl_check.h>\r
33 \r
34 #include <boost/foreach.hpp>\r
35 \r
36 #include <gl/glew.h>\r
37 \r
38 #include <windows.h>\r
39 \r
40 #include <SFML/Window/Context.hpp>\r
41 \r
42 #include <array>\r
43 #include <unordered_map>\r
44 \r
45 #include <tbb/concurrent_unordered_map.h>\r
46 #include <tbb/concurrent_queue.h>\r
47 #include <tbb/atomic.h>\r
48 \r
49 tbb::atomic<int> g_count = tbb::atomic<int>();\r
50 \r
51 namespace caspar { namespace accelerator { namespace ogl {\r
52                 \r
53 struct device::impl : public std::enable_shared_from_this<impl>\r
54 {\r
55         std::map<host_buffer*, spl::shared_ptr<device_buffer>> write_buffer_transfer_cache_;\r
56 \r
57         std::unique_ptr<sf::Context> device_;\r
58         std::unique_ptr<sf::Context> host_alloc_device_;\r
59         \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
62         \r
63         GLuint fbo_;\r
64 \r
65         executor& executor_;\r
66         executor  host_alloc_executor_;\r
67                                 \r
68         impl(executor& executor) \r
69                 : executor_(executor)\r
70                 , host_alloc_executor_(L"OpenGL allocation device")\r
71         {\r
72                 if(g_count++ > 1)\r
73                         CASPAR_LOG(warning) << L"Multiple OGL devices.";\r
74 \r
75                 CASPAR_LOG(info) << L"Initializing OpenGL Device.";\r
76                 \r
77                 auto ctx1 = executor_.invoke([=]() -> HGLRC \r
78                 {\r
79                         device_.reset(new sf::Context());\r
80                         device_->SetActive(true);               \r
81                                                 \r
82                         if (glewInit() != GLEW_OK)\r
83                                 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));\r
84                 \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
87         \r
88                         glGenFramebuffers(1, &fbo_);                            \r
89                         glBindFramebuffer(GL_FRAMEBUFFER, fbo_);\r
90                         \r
91                         auto ctx1 = wglGetCurrentContext();\r
92                         \r
93                         device_->SetActive(false);\r
94 \r
95                         return ctx1;\r
96                 });\r
97 \r
98                 host_alloc_executor_.invoke([=]\r
99                 {\r
100                         host_alloc_device_.reset(new sf::Context());\r
101                         host_alloc_device_->SetActive(true);    \r
102                         auto ctx2 = wglGetCurrentContext();\r
103 \r
104                         if(!wglShareLists(ctx1, ctx2))\r
105                                 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to share OpenGL devices."));\r
106                 });\r
107 \r
108                 executor_.invoke([=]\r
109                 {               \r
110                         device_->SetActive(true);\r
111                 });\r
112                 \r
113                 CASPAR_LOG(info) << L"Successfully initialized OpenGL " << version();\r
114         }\r
115 \r
116         ~impl()\r
117         {\r
118                 host_alloc_executor_.invoke([=]\r
119                 {\r
120                         host_alloc_device_.reset();\r
121                         BOOST_FOREACH(auto& pool, host_pools_)\r
122                                 pool.clear();\r
123                 });\r
124 \r
125                 executor_.invoke([=]\r
126                 {\r
127                         BOOST_FOREACH(auto& pool, device_pools_)\r
128                                 pool.clear();\r
129                         glDeleteFramebuffers(1, &fbo_);\r
130 \r
131                         device_.reset();\r
132                 });\r
133         }\r
134 \r
135         spl::shared_ptr<device_buffer> allocate_device_buffer(int width, int height, int stride)\r
136         {\r
137                 return executor_.invoke([&]() -> spl::shared_ptr<device_buffer>\r
138                 {\r
139                         try\r
140                         {\r
141                                 return spl::make_shared<device_buffer>(width, height, stride);\r
142                         }\r
143                         catch(...)\r
144                         {\r
145                                 CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";\r
146                                 throw;\r
147                         }\r
148                 });\r
149         }\r
150                                 \r
151         spl::shared_ptr<device_buffer> create_device_buffer(int width, int height, int stride)\r
152         {\r
153                 CASPAR_VERIFY(stride > 0 && stride < 5);\r
154                 CASPAR_VERIFY(width > 0 && height > 0);\r
155                 \r
156                 auto pool = &device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
157                 \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
161         \r
162                 auto self = shared_from_this();\r
163                 return spl::shared_ptr<device_buffer>(buffer.get(), [self, buffer, pool](device_buffer*) mutable\r
164                 {               \r
165                         pool->push(buffer);     \r
166                 });\r
167         }\r
168 \r
169         spl::shared_ptr<host_buffer> allocate_host_buffer(int size, host_buffer::usage usage)\r
170         {\r
171                 return host_alloc_executor_.invoke([=]() -> spl::shared_ptr<host_buffer>\r
172                 {\r
173                         try\r
174                         {\r
175                                 auto buffer = spl::make_shared<host_buffer>(size, usage);\r
176                                 if(usage == host_buffer::usage::write_only)\r
177                                         buffer->map();\r
178                                 else\r
179                                         buffer->unmap();        \r
180 \r
181                                 return buffer;\r
182                         }\r
183                         catch(...)\r
184                         {\r
185                                 CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";\r
186                                 throw;  \r
187                         }\r
188                 });\r
189         }\r
190         \r
191         spl::shared_ptr<host_buffer> create_host_buffer(int size, host_buffer::usage usage)\r
192         {\r
193                 CASPAR_VERIFY(usage == host_buffer::usage::write_only || usage == host_buffer::usage::read_only);\r
194                 CASPAR_VERIFY(size > 0);\r
195                 \r
196                 auto pool = &host_pools_[usage.value()][size];\r
197                 \r
198                 std::shared_ptr<host_buffer> buffer;\r
199                 if(!pool->try_pop(buffer))      \r
200                         buffer = allocate_host_buffer(size, usage);     \r
201         \r
202                 bool is_write = (usage == host_buffer::usage::write_only);\r
203 \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
206                 {\r
207                         self->host_alloc_executor_.begin_invoke([=]() mutable\r
208                         {               \r
209                                 if(is_write)                            \r
210                                         buffer->map();                          \r
211                                 else\r
212                                         buffer->unmap();\r
213 \r
214                                 pool->push(buffer);\r
215                         }, task_priority::high_priority);       \r
216 \r
217                         self->executor_.begin_invoke([=]\r
218                         {\r
219                                 write_buffer_transfer_cache_.erase(buffer.get());                               \r
220                         }, task_priority::high_priority);       \r
221                 });\r
222         }\r
223                 \r
224         std::wstring version()\r
225         {       \r
226                 static std::wstring ver = L"Not found";\r
227                 try\r
228                 {\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
231                 }\r
232                 catch(...){}\r
233 \r
234                 return ver;\r
235         }\r
236                         \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
238         {\r
239                 return executor_.begin_invoke([=]() -> spl::shared_ptr<device_buffer>\r
240                 {\r
241                         auto buffer_it = write_buffer_transfer_cache_.find(source.get());\r
242                         if(buffer_it == write_buffer_transfer_cache_.end())\r
243                         {\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
247                         }\r
248                         return buffer_it->second;\r
249                 }, task_priority::high_priority);\r
250         }\r
251 };\r
252 \r
253 device::device() \r
254         : executor_(L"device")\r
255         , impl_(new impl(executor_))\r
256 {\r
257 }\r
258         \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
263 \r
264 \r
265 }}}\r
266 \r
267 \r