]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/ogl_device.cpp
0ea77fbb4641b625975a0eeca14650045352f734
[casparcg] / core / mixer / gpu / ogl_device.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://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 "ogl_device.h"\r
27 \r
28 #include "shader.h"\r
29 \r
30 #include <common/exception/exceptions.h>\r
31 #include <common/utility/assert.h>\r
32 #include <common/gl/gl_check.h>\r
33 \r
34 #include <boost/foreach.hpp>\r
35 #include <boost/property_tree/ptree.hpp>\r
36 \r
37 #include <gl/glew.h>\r
38 \r
39 namespace caspar { namespace core {\r
40 \r
41 ogl_device::ogl_device() \r
42         : executor_(L"ogl_device")\r
43         , pattern_(nullptr)\r
44         , attached_texture_(0)\r
45         , attached_fbo_(0)\r
46         , active_shader_(0)\r
47         , read_buffer_(0)\r
48 {\r
49         CASPAR_LOG(info) << L"Initializing OpenGL Device.";\r
50 \r
51         std::fill(binded_textures_.begin(), binded_textures_.end(), 0);\r
52         std::fill(viewport_.begin(), viewport_.end(), 0);\r
53         std::fill(scissor_.begin(), scissor_.end(), 0);\r
54         std::fill(blend_func_.begin(), blend_func_.end(), 0);\r
55         \r
56         invoke([=]\r
57         {\r
58                 context_.reset(new sf::Context());\r
59                 context_->SetActive(true);\r
60                 \r
61                 if (glewInit() != GLEW_OK)\r
62                         BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));\r
63                                                 \r
64                 CASPAR_LOG(info) << L"OpenGL " << version();\r
65 \r
66                 if(!GLEW_VERSION_3_0)\r
67                         BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Your graphics card does not meet the minimum hardware requirements since it does not support OpenGL 3.0 or higher. CasparCG Server will not be able to continue."));\r
68         \r
69                 glGenFramebuffers(1, &fbo_);    \r
70                 \r
71                 CASPAR_LOG(info) << L"Successfully initialized OpenGL Device.";\r
72         });\r
73 }\r
74 \r
75 ogl_device::~ogl_device()\r
76 {\r
77         invoke([=]\r
78         {\r
79                 BOOST_FOREACH(auto& pool, device_pools_)\r
80                         pool.clear();\r
81                 BOOST_FOREACH(auto& pool, host_pools_)\r
82                         pool.clear();\r
83                 glDeleteFramebuffers(1, &fbo_);\r
84         });\r
85 }\r
86 \r
87 safe_ptr<device_buffer> ogl_device::allocate_device_buffer(size_t width, size_t height, size_t stride, bool mipmapped)\r
88 {\r
89         std::shared_ptr<device_buffer> buffer;\r
90         try\r
91         {\r
92                 buffer.reset(new device_buffer(width, height, stride, mipmapped));\r
93         }\r
94         catch(...)\r
95         {\r
96                 try\r
97                 {\r
98                         yield();\r
99                         gc().wait();\r
100                                         \r
101                         // Try again\r
102                         buffer.reset(new device_buffer(width, height, stride, mipmapped));\r
103                 }\r
104                 catch(...)\r
105                 {\r
106                         CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";\r
107                         throw;\r
108                 }\r
109         }\r
110         return make_safe_ptr(buffer);\r
111 }\r
112                                 \r
113 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride, bool mipmapped)\r
114 {\r
115         CASPAR_VERIFY(stride > 0 && stride < 5);\r
116         CASPAR_VERIFY(width > 0 && height > 0);\r
117         auto& pool = device_pools_[stride-1 + (mipmapped ? 4 : 0)][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
118         std::shared_ptr<device_buffer> buffer;\r
119         if(!pool->items.try_pop(buffer))                \r
120                 buffer = executor_.invoke([&]{return allocate_device_buffer(width, height, stride, mipmapped);}, high_priority);                        \r
121         \r
122         //++pool->usage_count;\r
123 \r
124         return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*) mutable\r
125         {               \r
126                 pool->items.push(buffer);       \r
127         });\r
128 }\r
129 \r
130 safe_ptr<host_buffer> ogl_device::allocate_host_buffer(size_t size, host_buffer::usage_t usage)\r
131 {\r
132         std::shared_ptr<host_buffer> buffer;\r
133 \r
134         try\r
135         {\r
136                 buffer.reset(new host_buffer(size, usage));\r
137                 if(usage == host_buffer::write_only)\r
138                         buffer->map();\r
139                 else\r
140                         buffer->unmap();                        \r
141         }\r
142         catch(...)\r
143         {\r
144                 try\r
145                 {\r
146                         yield();\r
147                         gc().wait();\r
148 \r
149                         // Try again\r
150                         buffer.reset(new host_buffer(size, usage));\r
151                         if(usage == host_buffer::write_only)\r
152                                 buffer->map();\r
153                         else\r
154                                 buffer->unmap();        \r
155                 }\r
156                 catch(...)\r
157                 {\r
158                         CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";\r
159                         throw;          \r
160                 }\r
161         }\r
162 \r
163         return make_safe_ptr(buffer);\r
164 }\r
165         \r
166 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)\r
167 {\r
168         CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);\r
169         CASPAR_VERIFY(size > 0);\r
170         auto& pool = host_pools_[usage][size];\r
171         std::shared_ptr<host_buffer> buffer;\r
172         if(!pool->items.try_pop(buffer))        \r
173                 buffer = executor_.invoke([=]{return allocate_host_buffer(size, usage);}, high_priority);       \r
174         \r
175         //++pool->usage_count;\r
176 \r
177         auto self = shared_from_this();\r
178         return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable\r
179         {\r
180                 self->executor_.begin_invoke([=]() mutable\r
181                 {               \r
182                         if(usage == host_buffer::write_only)\r
183                                 buffer->map();\r
184                         else\r
185                                 buffer->unmap();\r
186 \r
187                         pool->items.push(buffer);\r
188                 }, high_priority);      \r
189         });\r
190 }\r
191 \r
192 safe_ptr<ogl_device> ogl_device::create()\r
193 {\r
194         return safe_ptr<ogl_device>(new ogl_device());\r
195 }\r
196 \r
197 //template<typename T>\r
198 //void flush_pool(buffer_pool<T>& pool)\r
199 //{     \r
200 //      if(pool.flush_count.fetch_and_increment() < 16)\r
201 //              return;\r
202 //\r
203 //      if(pool.usage_count.fetch_and_store(0) < pool.items.size())\r
204 //      {\r
205 //              std::shared_ptr<T> buffer;\r
206 //              pool.items.try_pop(buffer);\r
207 //      }\r
208 //\r
209 //      pool.flush_count = 0;\r
210 //      pool.usage_count = 0;\r
211 //}\r
212 \r
213 void ogl_device::flush()\r
214 {\r
215         GL(glFlush());  \r
216                 \r
217         //try\r
218         //{\r
219         //      BOOST_FOREACH(auto& pools, device_pools_)\r
220         //      {\r
221         //              BOOST_FOREACH(auto& pool, pools)\r
222         //                      flush_pool(*pool.second);\r
223         //      }\r
224         //      BOOST_FOREACH(auto& pools, host_pools_)\r
225         //      {\r
226         //              BOOST_FOREACH(auto& pool, pools)\r
227         //                      flush_pool(*pool.second);\r
228         //      }\r
229         //}\r
230         //catch(...)\r
231         //{\r
232         //      CASPAR_LOG_CURRENT_EXCEPTION();\r
233         //}\r
234 }\r
235 \r
236 void ogl_device::yield()\r
237 {\r
238         executor_.yield();\r
239 }\r
240 \r
241 boost::property_tree::wptree ogl_device::info() const\r
242 {\r
243         boost::property_tree::wptree info;\r
244 \r
245         boost::property_tree::wptree pooled_device_buffers;\r
246         size_t total_pooled_device_buffer_size = 0;\r
247         size_t total_pooled_device_buffer_count = 0;\r
248 \r
249         for (size_t i = 0; i < device_pools_.size(); ++i)\r
250         {\r
251                 auto& pools = device_pools_.at(i);\r
252                 bool mipmapping = i > 3;\r
253                 int stride = mipmapping ? i - 3 : i + 1;\r
254 \r
255                 BOOST_FOREACH(auto& pool, pools)\r
256                 {\r
257                         auto width = pool.first >> 16;\r
258                         auto height = pool.first & 0x0000FFFF;\r
259                         auto size = width * height * stride;\r
260                         auto count = pool.second->items.size();\r
261 \r
262                         if (count == 0)\r
263                                 continue;\r
264 \r
265                         boost::property_tree::wptree pool_info;\r
266 \r
267                         pool_info.add(L"stride", stride);\r
268                         pool_info.add(L"mipmapping", mipmapping);\r
269                         pool_info.add(L"width", width);\r
270                         pool_info.add(L"height", height);\r
271                         pool_info.add(L"size", size);\r
272                         pool_info.add(L"count", count);\r
273 \r
274                         total_pooled_device_buffer_size += size * count;\r
275                         total_pooled_device_buffer_count += count;\r
276 \r
277                         pooled_device_buffers.add_child(L"device_buffer_pool", pool_info);\r
278                 }\r
279         }\r
280 \r
281         info.add_child(L"gl.details.pooled_device_buffers", pooled_device_buffers);\r
282 \r
283         boost::property_tree::wptree pooled_host_buffers;\r
284         size_t total_read_size = 0;\r
285         size_t total_write_size = 0;\r
286         size_t total_read_count = 0;\r
287         size_t total_write_count = 0;\r
288 \r
289         for (size_t i = 0; i < host_pools_.size(); ++i)\r
290         {\r
291                 auto& pools = host_pools_.at(i);\r
292                 host_buffer::usage_t usage = static_cast<host_buffer::usage_t>(i);\r
293 \r
294                 BOOST_FOREACH(auto& pool, pools)\r
295                 {\r
296                         auto size = pool.first;\r
297                         auto count = pool.second->items.size();\r
298 \r
299                         if (count == 0)\r
300                                 continue;\r
301 \r
302                         boost::property_tree::wptree pool_info;\r
303 \r
304                         pool_info.add(L"usage", usage == host_buffer::read_only\r
305                                 ? L"read_only" : L"write_only");\r
306                         pool_info.add(L"size", size);\r
307                         pool_info.add(L"count", count);\r
308 \r
309                         pooled_host_buffers.add_child(L"host_buffer_pool", pool_info);\r
310 \r
311                         (usage == host_buffer::read_only\r
312                                         ? total_read_count : total_write_count) += count;\r
313                         (usage == host_buffer::read_only\r
314                                         ? total_read_size : total_write_size) += size * count;\r
315                 }\r
316         }\r
317 \r
318         info.add_child(L"gl.details.pooled_host_buffers", pooled_host_buffers);\r
319 \r
320         info.add(L"gl.summary.pooled_device_buffers.total_count", total_pooled_device_buffer_count);\r
321         info.add(L"gl.summary.pooled_device_buffers.total_size", total_pooled_device_buffer_size);\r
322         info.add_child(L"gl.summary.all_device_buffers", device_buffer::info());\r
323         info.add(L"gl.summary.pooled_host_buffers.total_read_count", total_read_count);\r
324         info.add(L"gl.summary.pooled_host_buffers.total_write_count", total_write_count);\r
325         info.add(L"gl.summary.pooled_host_buffers.total_read_size", total_read_size);\r
326         info.add(L"gl.summary.pooled_host_buffers.total_write_size", total_write_size);\r
327         info.add_child(L"gl.summary.all_host_buffers", host_buffer::info());\r
328 \r
329         return info;\r
330 }\r
331 \r
332 boost::unique_future<void> ogl_device::gc()\r
333 {       \r
334         return begin_invoke([=]\r
335         {\r
336                 CASPAR_LOG(info) << " ogl: Running GC.";                \r
337         \r
338                 try\r
339                 {\r
340                         BOOST_FOREACH(auto& pools, device_pools_)\r
341                         {\r
342                                 BOOST_FOREACH(auto& pool, pools)\r
343                                         pool.second->items.clear();\r
344                         }\r
345                         BOOST_FOREACH(auto& pools, host_pools_)\r
346                         {\r
347                                 BOOST_FOREACH(auto& pool, pools)\r
348                                         pool.second->items.clear();\r
349                         }\r
350                 }\r
351                 catch(...)\r
352                 {\r
353                         CASPAR_LOG_CURRENT_EXCEPTION();\r
354                 }\r
355         }, high_priority);\r
356 }\r
357 \r
358 std::wstring ogl_device::version()\r
359 {       \r
360         static std::wstring ver = L"Not found";\r
361         try\r
362         {\r
363                 ver = widen(invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})\r
364                 + " "   + invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));                      \r
365         }\r
366         catch(...){}\r
367 \r
368         return ver;\r
369 }\r
370 \r
371 \r
372 void ogl_device::enable(GLenum cap)\r
373 {\r
374         auto& val = caps_[cap];\r
375         if(!val)\r
376         {\r
377                 glEnable(cap);\r
378                 val = true;\r
379         }\r
380 }\r
381 \r
382 void ogl_device::disable(GLenum cap)\r
383 {\r
384         auto& val = caps_[cap];\r
385         if(val)\r
386         {\r
387                 glDisable(cap);\r
388                 val = false;\r
389         }\r
390 }\r
391 \r
392 void ogl_device::viewport(size_t x, size_t y, size_t width, size_t height)\r
393 {\r
394         if(x != viewport_[0] || y != viewport_[1] || width != viewport_[2] || height != viewport_[3])\r
395         {               \r
396                 glViewport(x, y, width, height);\r
397                 viewport_[0] = x;\r
398                 viewport_[1] = y;\r
399                 viewport_[2] = width;\r
400                 viewport_[3] = height;\r
401         }\r
402 }\r
403 \r
404 void ogl_device::scissor(size_t x, size_t y, size_t width, size_t height)\r
405 {\r
406         if(x != scissor_[0] || y != scissor_[1] || width != scissor_[2] || height != scissor_[3])\r
407         {               \r
408                 glScissor(x, y, width, height);\r
409                 scissor_[0] = x;\r
410                 scissor_[1] = y;\r
411                 scissor_[2] = width;\r
412                 scissor_[3] = height;\r
413         }\r
414 }\r
415 \r
416 void ogl_device::stipple_pattern(const GLubyte* pattern)\r
417 {\r
418         if(pattern_ != pattern)\r
419         {               \r
420                 glPolygonStipple(pattern);\r
421                 pattern_ = pattern;\r
422         }\r
423 }\r
424 \r
425 void ogl_device::attach(device_buffer& texture)\r
426 {       \r
427         if(attached_texture_ != texture.id())\r
428         {\r
429                 if(attached_fbo_ != fbo_)\r
430                 {\r
431                         glBindFramebuffer(GL_FRAMEBUFFER, fbo_);\r
432                         attached_fbo_ = fbo_;\r
433                 }\r
434 \r
435                 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));\r
436                 attached_texture_ = texture.id();\r
437         }\r
438 }\r
439 \r
440 void ogl_device::clear(device_buffer& texture)\r
441 {       \r
442         attach(texture);\r
443         GL(glClear(GL_COLOR_BUFFER_BIT));\r
444 }\r
445 \r
446 void ogl_device::read_buffer(device_buffer&)\r
447 {\r
448         if(read_buffer_ != GL_COLOR_ATTACHMENT0)\r
449         {\r
450                 GL(glReadBuffer(GL_COLOR_ATTACHMENT0));\r
451                 read_buffer_ = GL_COLOR_ATTACHMENT0;\r
452         }\r
453 }\r
454 \r
455 void ogl_device::use(shader& shader)\r
456 {\r
457         if(active_shader_ != shader.id())\r
458         {               \r
459                 GL(glUseProgramObjectARB(shader.id())); \r
460                 active_shader_ = shader.id();\r
461         }\r
462 }\r
463 \r
464 void ogl_device::blend_func(int c1, int c2, int a1, int a2)\r
465 {\r
466         std::array<int, 4> func = {c1, c2, a1, a2};\r
467 \r
468         if(blend_func_ != func)\r
469         {\r
470                 blend_func_ = func;\r
471                 GL(glBlendFuncSeparate(c1, c2, a1, a2));\r
472         }\r
473 }\r
474 \r
475 void ogl_device::blend_func(int c1, int c2)\r
476 {\r
477         blend_func(c1, c2, c1, c2);\r
478 }\r
479 \r
480 }}\r
481 \r