]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/device.cpp
* OpenGL debug logging
[casparcg] / accelerator / ogl / util / device.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 // TODO: Smart GC
23
24 #include "../../StdAfx.h"
25
26 #include "device.h"
27
28 #include "buffer.h"
29 #include "texture.h"
30 #include "shader.h"
31
32 #include <common/assert.h>
33 #include <common/except.h>
34 #include <common/future.h>
35 #include <common/array.h>
36 #include <common/memory.h>
37 #include <common/gl/gl_check.h>
38 #include <common/timer.h>
39
40 #include <GL/glew.h>
41
42 #include <SFML/Window/Context.hpp>
43
44 #include <tbb/concurrent_unordered_map.h>
45 #include <tbb/concurrent_hash_map.h>
46 #include <tbb/concurrent_queue.h>
47
48 #include <boost/utility/declval.hpp>
49 #include <boost/property_tree/ptree.hpp>
50
51 #include <array>
52 #include <unordered_map>
53
54 #include <asmlib.h>
55 #include <tbb/parallel_for.h>
56
57 namespace caspar { namespace accelerator { namespace ogl {
58
59         void __stdcall gl_debug_callback(GLenum, GLenum, GLuint, GLenum, GLsizei, const GLchar* message, const void*) {
60                 CASPAR_LOG(debug) << message;
61         }
62
63
64 struct device::impl : public std::enable_shared_from_this<impl>
65 {       
66         static_assert(std::is_same<decltype(boost::declval<device>().impl_), spl::shared_ptr<impl>>::value, "impl_ must be shared_ptr");
67
68         tbb::concurrent_hash_map<buffer*, std::shared_ptr<texture>> texture_cache_;
69
70         std::unique_ptr<sf::Context> device_;
71         
72         std::array<tbb::concurrent_unordered_map<std::size_t, tbb::concurrent_bounded_queue<std::shared_ptr<texture>>>, 8>      device_pools_;
73         std::array<tbb::concurrent_unordered_map<std::size_t, tbb::concurrent_bounded_queue<std::shared_ptr<buffer>>>, 2>       host_pools_;
74         
75         GLuint fbo_;
76
77         executor& executor_;
78                                 
79         impl(executor& executor) 
80                 : executor_(executor)
81         {
82                 executor_.set_capacity(256);
83
84                 CASPAR_LOG(info) << L"Initializing OpenGL Device.";
85                 
86                 executor_.invoke([=]
87                 {
88                         device_.reset(new sf::Context());
89                         device_->setActive(true);               
90                                                 
91                         if (glewInit() != GLEW_OK)
92                                 CASPAR_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
93
94                         if(!GLEW_VERSION_3_0)
95                                 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."));
96
97                         glDebugMessageCallbackARB(gl_debug_callback, nullptr);
98                         //glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, nullptr, GL_FALSE);
99                         glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE, GL_DEBUG_SEVERITY_LOW, 0, nullptr, GL_FALSE);
100                         glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE, GL_DEBUG_SEVERITY_MEDIUM, 0, nullptr, GL_FALSE);
101
102                         glGenFramebuffers(1, &fbo_);                            
103                         glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
104                 });
105                                 
106                 CASPAR_LOG(info) << L"Successfully initialized OpenGL " << version();
107         }
108
109         ~impl()
110         {
111                 executor_.invoke([=]
112                 {
113                         texture_cache_.clear();
114
115                         for (auto& pool : host_pools_)
116                                 pool.clear();
117
118                         for (auto& pool : device_pools_)
119                                 pool.clear();
120
121                         glDeleteFramebuffers(1, &fbo_);
122
123                         device_.reset();
124                 });
125         }
126
127         boost::property_tree::wptree info() const
128         {
129                 boost::property_tree::wptree info;
130
131                 boost::property_tree::wptree pooled_device_buffers;
132                 size_t total_pooled_device_buffer_size  = 0;
133                 size_t total_pooled_device_buffer_count = 0;
134
135                 for (size_t i = 0; i < device_pools_.size(); ++i)
136                 {
137                         auto& pools             = device_pools_.at(i);
138                         bool mipmapping = i > 3;
139                         auto stride             = mipmapping ? i - 3 : i + 1;
140
141                         for (auto& pool : pools)
142                         {
143                                 auto width      = pool.first >> 16;
144                                 auto height     = pool.first & 0x0000FFFF;
145                                 auto size       = width * height * stride;
146                                 auto count      = pool.second.size();
147
148                                 if (count == 0)
149                                         continue;
150
151                                 boost::property_tree::wptree pool_info;
152
153                                 pool_info.add(L"stride",                stride);
154                                 pool_info.add(L"mipmapping",    mipmapping);
155                                 pool_info.add(L"width",                 width);
156                                 pool_info.add(L"height",                height);
157                                 pool_info.add(L"size",                  size);
158                                 pool_info.add(L"count",                 count);
159
160                                 total_pooled_device_buffer_size         += size * count;
161                                 total_pooled_device_buffer_count        += count;
162
163                                 pooled_device_buffers.add_child(L"device_buffer_pool", pool_info);
164                         }
165                 }
166
167                 info.add_child(L"gl.details.pooled_device_buffers", pooled_device_buffers);
168
169                 boost::property_tree::wptree pooled_host_buffers;
170                 size_t total_read_size          = 0;
171                 size_t total_write_size         = 0;
172                 size_t total_read_count         = 0;
173                 size_t total_write_count        = 0;
174
175                 for (size_t i = 0; i < host_pools_.size(); ++i)
176                 {
177                         auto& pools     = host_pools_.at(i);
178                         auto usage      = static_cast<buffer::usage>(i);
179
180                         for (auto& pool : pools)
181                         {
182                                 auto size       = pool.first;
183                                 auto count      = pool.second.size();
184
185                                 if (count == 0)
186                                         continue;
187
188                                 boost::property_tree::wptree pool_info;
189
190                                 pool_info.add(L"usage", usage == buffer::usage::read_only ? L"read_only" : L"write_only");
191                                 pool_info.add(L"size",  size);
192                                 pool_info.add(L"count", count);
193
194                                 pooled_host_buffers.add_child(L"host_buffer_pool", pool_info);
195
196                                 (usage == buffer::usage::read_only ? total_read_count : total_write_count) += count;
197                                 (usage == buffer::usage::read_only ? total_read_size : total_write_size) += size * count;
198                         }
199                 }
200
201                 info.add_child(L"gl.details.pooled_host_buffers",                               pooled_host_buffers);
202                 info.add(L"gl.summary.pooled_device_buffers.total_count",               total_pooled_device_buffer_count);
203                 info.add(L"gl.summary.pooled_device_buffers.total_size",                total_pooled_device_buffer_size);
204                 info.add_child(L"gl.summary.all_device_buffers",                                texture::info());
205                 info.add(L"gl.summary.pooled_host_buffers.total_read_count",    total_read_count);
206                 info.add(L"gl.summary.pooled_host_buffers.total_write_count",   total_write_count);
207                 info.add(L"gl.summary.pooled_host_buffers.total_read_size",             total_read_size);
208                 info.add(L"gl.summary.pooled_host_buffers.total_write_size",    total_write_size);
209                 info.add_child(L"gl.summary.all_host_buffers",                                  buffer::info());
210
211                 return info;
212         }
213                 
214         std::wstring version()
215         {       
216                 try
217                 {
218                         return executor_.invoke([]
219                         {
220                                 return u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VERSION)))) + L" " + u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VENDOR))));
221                         });     
222                 }
223                 catch(...)
224                 {
225                         return L"Not found";;
226                 }
227         }
228                                                         
229         spl::shared_ptr<texture> create_texture(int width, int height, int stride, bool mipmapped, bool clear)
230         {
231                 CASPAR_VERIFY(stride > 0 && stride < 5);
232                 CASPAR_VERIFY(width > 0 && height > 0);
233
234                 if(!executor_.is_current())
235                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Operation only valid in an OpenGL Context."));
236                                         
237                 auto pool = &device_pools_[stride - 1 + (mipmapped ? 4 : 0)][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
238                 
239                 std::shared_ptr<texture> tex;
240                 if(!pool->try_pop(tex))         
241                         tex = spl::make_shared<texture>(width, height, stride, mipmapped);
242         
243                 if(clear)
244                         tex->clear();
245
246                 return spl::shared_ptr<texture>(tex.get(), [tex, pool](texture*) mutable
247                 {               
248                         pool->push(tex);        
249                 });
250         }
251                 
252         spl::shared_ptr<buffer> create_buffer(std::size_t size, buffer::usage usage)
253         {
254                 CASPAR_VERIFY(size > 0);
255                 
256                 auto pool = &host_pools_[static_cast<int>(usage)][size];
257                 
258                 std::shared_ptr<buffer> buf;
259                 if(!pool->try_pop(buf)) 
260                 {
261                         caspar::timer timer;
262
263                         buf = executor_.invoke([&]
264                         {
265                                 return std::make_shared<buffer>(size, usage);
266                         }, task_priority::high_priority);
267                         
268                         if(timer.elapsed() > 0.02)
269                                 CASPAR_LOG(warning) << L"[ogl-device] Performance warning. Buffer allocation blocked: " << timer.elapsed();
270                 }
271                 
272                 std::weak_ptr<impl> self = shared_from_this(); // buffers can leave the device context, take a hold on life-time.
273                 return spl::shared_ptr<buffer>(buf.get(), [=](buffer*) mutable
274                 {
275                         auto strong = self.lock();
276
277                         if (strong)
278                         {
279                                 strong->executor_.invoke([&]
280                                 {
281                                         strong->texture_cache_.erase(buf.get());
282                                 }, task_priority::high_priority);
283                                 
284                                 pool->push(buf);
285                         }
286                         else
287                         {
288                                 CASPAR_LOG(info) << L"Buffer outlived ogl device";
289                         }
290                 });
291         }
292
293         array<std::uint8_t> create_array(std::size_t size)
294         {               
295                 auto buf = create_buffer(size, buffer::usage::write_only);
296                 return array<std::uint8_t>(buf->data(), buf->size(), false, buf);
297         }
298
299         template<typename T>
300         std::shared_ptr<buffer> copy_to_buf(const T& source)
301         {
302                 std::shared_ptr<buffer> buf;
303
304                 auto tmp = source.template storage<spl::shared_ptr<buffer>>();
305                 if(tmp)
306                         buf = *tmp;
307                 else
308                 {                       
309                         buf = create_buffer(source.size(), buffer::usage::write_only);
310                         tbb::parallel_for(tbb::blocked_range<std::size_t>(0, source.size()), [&](const tbb::blocked_range<std::size_t>& r)
311                         {
312                                 A_memcpy(buf->data() + r.begin(), source.data() + r.begin(), r.size());
313                         });
314                 }
315
316                 return buf;
317         }
318
319         // TODO: Since the returned texture is cached it SHOULD NOT be modified.
320         std::future<std::shared_ptr<texture>> copy_async(const array<const std::uint8_t>& source, int width, int height, int stride, bool mipmapped)
321         {
322                 std::shared_ptr<buffer> buf = copy_to_buf(source);
323                                 
324                 return executor_.begin_invoke([=]() -> std::shared_ptr<texture>
325                 {
326                         tbb::concurrent_hash_map<buffer*, std::shared_ptr<texture>>::const_accessor a;
327                         if(texture_cache_.find(a, buf.get()))
328                                 return spl::make_shared_ptr(a->second);
329
330                         auto texture = create_texture(width, height, stride, mipmapped, false);
331                         texture->copy_from(*buf);
332
333                         texture_cache_.insert(std::make_pair(buf.get(), texture));
334                         
335                         return texture;
336                 }, task_priority::high_priority);
337         }
338         
339         std::future<std::shared_ptr<texture>> copy_async(const array<std::uint8_t>& source, int width, int height, int stride, bool mipmapped)
340         {
341                 std::shared_ptr<buffer> buf = copy_to_buf(source);
342
343                 return executor_.begin_invoke([=]() -> std::shared_ptr<texture>
344                 {
345                         auto texture = create_texture(width, height, stride, mipmapped, false);
346                         texture->copy_from(*buf);       
347                         
348                         return texture;
349                 }, task_priority::high_priority);
350         }
351
352         std::future<array<const std::uint8_t>> copy_async(const spl::shared_ptr<texture>& source)
353         {
354                 if(!executor_.is_current())
355                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Operation only valid in an OpenGL Context."));
356
357                 auto buffer = create_buffer(source->size(), buffer::usage::read_only); 
358                 source->copy_to(*buffer);       
359
360                 auto self = shared_from_this();
361                 auto cmd = [self, buffer]() mutable -> array<const std::uint8_t>
362                 {
363                         self->executor_.invoke(std::bind(&buffer::map, std::ref(buffer))); // Defer blocking "map" call until data is needed.
364                         return array<const std::uint8_t>(buffer->data(), buffer->size(), true, buffer);
365                 };
366                 return std::async(std::launch::deferred, std::move(cmd));
367         }
368
369         std::future<void> gc()
370         {
371                 return executor_.begin_invoke([=]
372                 {
373                         CASPAR_LOG(info) << " ogl: Running GC.";
374
375                         try
376                         {
377                                 for (auto& pools : device_pools_)
378                                 {
379                                         for (auto& pool : pools)
380                                                 pool.second.clear();
381                                 }
382                                 for (auto& pools : host_pools_)
383                                 {
384                                         for (auto& pool : pools)
385                                                 pool.second.clear();
386                                 }
387                         }
388                         catch (...)
389                         {
390                                 CASPAR_LOG_CURRENT_EXCEPTION();
391                         }
392                 }, task_priority::high_priority);
393         }
394 };
395
396 device::device() 
397         : executor_(L"OpenGL Rendering Context")
398         , impl_(new impl(executor_)){}
399 device::~device(){}
400 spl::shared_ptr<texture>                                        device::create_texture(int width, int height, int stride, bool mipmapped){ return impl_->create_texture(width, height, stride, mipmapped, true); }
401 array<std::uint8_t>                                                     device::create_array(int size){return impl_->create_array(size);}
402 std::future<std::shared_ptr<texture>>           device::copy_async(const array<const std::uint8_t>& source, int width, int height, int stride, bool mipmapped){return impl_->copy_async(source, width, height, stride, mipmapped);}
403 std::future<std::shared_ptr<texture>>           device::copy_async(const array<std::uint8_t>& source, int width, int height, int stride, bool mipmapped){ return impl_->copy_async(source, width, height, stride, mipmapped); }
404 std::future<array<const std::uint8_t>>          device::copy_async(const spl::shared_ptr<texture>& source){return impl_->copy_async(source);}
405 std::future<void>                                                       device::gc() { return impl_->gc(); }
406 boost::property_tree::wptree                            device::info() const { return impl_->info(); }
407 std::wstring                                                            device::version() const{return impl_->version();}
408
409
410 }}}
411
412