]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/ogl_device.cpp
Subtree merge of old SVN "dependencies" folder into the "master" git branch. You...
[casparcg] / core / mixer / gpu / ogl_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 "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 \r
36 #include <gl/glew.h>\r
37 \r
38 namespace caspar { namespace core {\r
39 \r
40 ogl_device::ogl_device() \r
41         : executor_(L"ogl_device")\r
42         , pattern_(nullptr)\r
43         , attached_texture_(0)\r
44         , attached_fbo_(0)\r
45         , active_shader_(0)\r
46         , read_buffer_(0)\r
47 {\r
48         CASPAR_LOG(info) << L"Initializing OpenGL Device.";\r
49 \r
50         std::fill(binded_textures_.begin(), binded_textures_.end(), 0);\r
51         std::fill(viewport_.begin(), viewport_.end(), 0);\r
52         std::fill(scissor_.begin(), scissor_.end(), 0);\r
53         std::fill(blend_func_.begin(), blend_func_.end(), 0);\r
54         \r
55         invoke([=]\r
56         {\r
57                 context_.reset(new sf::Context());\r
58                 context_->SetActive(true);\r
59                 \r
60                 if (glewInit() != GLEW_OK)\r
61                         BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));\r
62                                                 \r
63                 CASPAR_LOG(info) << L"OpenGL " << version();\r
64 \r
65                 if(!GLEW_VERSION_3_0)\r
66                         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
67         \r
68                 glGenFramebuffers(1, &fbo_);    \r
69                 \r
70                 CASPAR_LOG(info) << L"Successfully initialized OpenGL Device.";\r
71         });\r
72 }\r
73 \r
74 ogl_device::~ogl_device()\r
75 {\r
76         invoke([=]\r
77         {\r
78                 BOOST_FOREACH(auto& pool, device_pools_)\r
79                         pool.clear();\r
80                 BOOST_FOREACH(auto& pool, host_pools_)\r
81                         pool.clear();\r
82                 glDeleteFramebuffers(1, &fbo_);\r
83         });\r
84 }\r
85 \r
86 safe_ptr<device_buffer> ogl_device::allocate_device_buffer(size_t width, size_t height, size_t stride)\r
87 {\r
88         std::shared_ptr<device_buffer> buffer;\r
89         try\r
90         {\r
91                 buffer.reset(new device_buffer(width, height, stride));\r
92         }\r
93         catch(...)\r
94         {\r
95                 try\r
96                 {\r
97                         yield();\r
98                         gc().wait();\r
99                                         \r
100                         // Try again\r
101                         buffer.reset(new device_buffer(width, height, stride));\r
102                 }\r
103                 catch(...)\r
104                 {\r
105                         CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";\r
106                         throw;\r
107                 }\r
108         }\r
109         return make_safe_ptr(buffer);\r
110 }\r
111                                 \r
112 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)\r
113 {\r
114         CASPAR_VERIFY(stride > 0 && stride < 5);\r
115         CASPAR_VERIFY(width > 0 && height > 0);\r
116         auto& pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
117         std::shared_ptr<device_buffer> buffer;\r
118         if(!pool->items.try_pop(buffer))                \r
119                 buffer = executor_.invoke([&]{return allocate_device_buffer(width, height, stride);}, high_priority);                   \r
120         \r
121         //++pool->usage_count;\r
122 \r
123         return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*) mutable\r
124         {               \r
125                 pool->items.push(buffer);       \r
126         });\r
127 }\r
128 \r
129 safe_ptr<host_buffer> ogl_device::allocate_host_buffer(size_t size, host_buffer::usage_t usage)\r
130 {\r
131         std::shared_ptr<host_buffer> buffer;\r
132 \r
133         try\r
134         {\r
135                 buffer.reset(new host_buffer(size, usage));\r
136                 if(usage == host_buffer::write_only)\r
137                         buffer->map();\r
138                 else\r
139                         buffer->unmap();                        \r
140         }\r
141         catch(...)\r
142         {\r
143                 try\r
144                 {\r
145                         yield();\r
146                         gc().wait();\r
147 \r
148                         // Try again\r
149                         buffer.reset(new host_buffer(size, usage));\r
150                         if(usage == host_buffer::write_only)\r
151                                 buffer->map();\r
152                         else\r
153                                 buffer->unmap();        \r
154                 }\r
155                 catch(...)\r
156                 {\r
157                         CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";\r
158                         throw;          \r
159                 }\r
160         }\r
161 \r
162         return make_safe_ptr(buffer);\r
163 }\r
164         \r
165 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)\r
166 {\r
167         CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);\r
168         CASPAR_VERIFY(size > 0);\r
169         auto& pool = host_pools_[usage][size];\r
170         std::shared_ptr<host_buffer> buffer;\r
171         if(!pool->items.try_pop(buffer))        \r
172                 buffer = executor_.invoke([=]{return allocate_host_buffer(size, usage);}, high_priority);       \r
173         \r
174         //++pool->usage_count;\r
175 \r
176         auto self = shared_from_this();\r
177         return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable\r
178         {\r
179                 self->executor_.begin_invoke([=]() mutable\r
180                 {               \r
181                         if(usage == host_buffer::write_only)\r
182                                 buffer->map();\r
183                         else\r
184                                 buffer->unmap();\r
185 \r
186                         pool->items.push(buffer);\r
187                 }, high_priority);      \r
188         });\r
189 }\r
190 \r
191 safe_ptr<ogl_device> ogl_device::create()\r
192 {\r
193         return safe_ptr<ogl_device>(new ogl_device());\r
194 }\r
195 \r
196 //template<typename T>\r
197 //void flush_pool(buffer_pool<T>& pool)\r
198 //{     \r
199 //      if(pool.flush_count.fetch_and_increment() < 16)\r
200 //              return;\r
201 //\r
202 //      if(pool.usage_count.fetch_and_store(0) < pool.items.size())\r
203 //      {\r
204 //              std::shared_ptr<T> buffer;\r
205 //              pool.items.try_pop(buffer);\r
206 //      }\r
207 //\r
208 //      pool.flush_count = 0;\r
209 //      pool.usage_count = 0;\r
210 //}\r
211 \r
212 void ogl_device::flush()\r
213 {\r
214         GL(glFlush());  \r
215                 \r
216         //try\r
217         //{\r
218         //      BOOST_FOREACH(auto& pools, device_pools_)\r
219         //      {\r
220         //              BOOST_FOREACH(auto& pool, pools)\r
221         //                      flush_pool(*pool.second);\r
222         //      }\r
223         //      BOOST_FOREACH(auto& pools, host_pools_)\r
224         //      {\r
225         //              BOOST_FOREACH(auto& pool, pools)\r
226         //                      flush_pool(*pool.second);\r
227         //      }\r
228         //}\r
229         //catch(...)\r
230         //{\r
231         //      CASPAR_LOG_CURRENT_EXCEPTION();\r
232         //}\r
233 }\r
234 \r
235 void ogl_device::yield()\r
236 {\r
237         executor_.yield();\r
238 }\r
239 \r
240 boost::unique_future<void> ogl_device::gc()\r
241 {       \r
242         return begin_invoke([=]\r
243         {\r
244                 CASPAR_LOG(info) << " ogl: Running GC.";                \r
245         \r
246                 try\r
247                 {\r
248                         BOOST_FOREACH(auto& pools, device_pools_)\r
249                         {\r
250                                 BOOST_FOREACH(auto& pool, pools)\r
251                                         pool.second->items.clear();\r
252                         }\r
253                         BOOST_FOREACH(auto& pools, host_pools_)\r
254                         {\r
255                                 BOOST_FOREACH(auto& pool, pools)\r
256                                         pool.second->items.clear();\r
257                         }\r
258                 }\r
259                 catch(...)\r
260                 {\r
261                         CASPAR_LOG_CURRENT_EXCEPTION();\r
262                 }\r
263         }, high_priority);\r
264 }\r
265 \r
266 std::wstring ogl_device::version()\r
267 {       \r
268         static std::wstring ver = L"Not found";\r
269         try\r
270         {\r
271                 ver = widen(invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})\r
272                 + " "   + invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));                      \r
273         }\r
274         catch(...){}\r
275 \r
276         return ver;\r
277 }\r
278 \r
279 \r
280 void ogl_device::enable(GLenum cap)\r
281 {\r
282         auto& val = caps_[cap];\r
283         if(!val)\r
284         {\r
285                 glEnable(cap);\r
286                 val = true;\r
287         }\r
288 }\r
289 \r
290 void ogl_device::disable(GLenum cap)\r
291 {\r
292         auto& val = caps_[cap];\r
293         if(val)\r
294         {\r
295                 glDisable(cap);\r
296                 val = false;\r
297         }\r
298 }\r
299 \r
300 void ogl_device::viewport(size_t x, size_t y, size_t width, size_t height)\r
301 {\r
302         if(x != viewport_[0] || y != viewport_[1] || width != viewport_[2] || height != viewport_[3])\r
303         {               \r
304                 glViewport(x, y, width, height);\r
305                 viewport_[0] = x;\r
306                 viewport_[1] = y;\r
307                 viewport_[2] = width;\r
308                 viewport_[3] = height;\r
309         }\r
310 }\r
311 \r
312 void ogl_device::scissor(size_t x, size_t y, size_t width, size_t height)\r
313 {\r
314         if(x != scissor_[0] || y != scissor_[1] || width != scissor_[2] || height != scissor_[3])\r
315         {               \r
316                 glScissor(x, y, width, height);\r
317                 scissor_[0] = x;\r
318                 scissor_[1] = y;\r
319                 scissor_[2] = width;\r
320                 scissor_[3] = height;\r
321         }\r
322 }\r
323 \r
324 void ogl_device::stipple_pattern(const GLubyte* pattern)\r
325 {\r
326         if(pattern_ != pattern)\r
327         {               \r
328                 glPolygonStipple(pattern);\r
329                 pattern_ = pattern;\r
330         }\r
331 }\r
332 \r
333 void ogl_device::attach(device_buffer& texture)\r
334 {       \r
335         if(attached_texture_ != texture.id())\r
336         {\r
337                 if(attached_fbo_ != fbo_)\r
338                 {\r
339                         glBindFramebuffer(GL_FRAMEBUFFER, fbo_);\r
340                         attached_fbo_ = fbo_;\r
341                 }\r
342 \r
343                 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));\r
344                 attached_texture_ = texture.id();\r
345         }\r
346 }\r
347 \r
348 void ogl_device::clear(device_buffer& texture)\r
349 {       \r
350         attach(texture);\r
351         GL(glClear(GL_COLOR_BUFFER_BIT));\r
352 }\r
353 \r
354 void ogl_device::read_buffer(device_buffer&)\r
355 {\r
356         if(read_buffer_ != GL_COLOR_ATTACHMENT0)\r
357         {\r
358                 GL(glReadBuffer(GL_COLOR_ATTACHMENT0));\r
359                 read_buffer_ = GL_COLOR_ATTACHMENT0;\r
360         }\r
361 }\r
362 \r
363 void ogl_device::use(shader& shader)\r
364 {\r
365         if(active_shader_ != shader.id())\r
366         {               \r
367                 GL(glUseProgramObjectARB(shader.id())); \r
368                 active_shader_ = shader.id();\r
369         }\r
370 }\r
371 \r
372 void ogl_device::blend_func(int c1, int c2, int a1, int a2)\r
373 {\r
374         std::array<int, 4> func = {c1, c2, a1, a2};\r
375 \r
376         if(blend_func_ != func)\r
377         {\r
378                 blend_func_ = func;\r
379                 GL(glBlendFuncSeparate(c1, c2, a1, a2));\r
380         }\r
381 }\r
382 \r
383 void ogl_device::blend_func(int c1, int c2)\r
384 {\r
385         blend_func(c1, c2, c1, c2);\r
386 }\r
387 \r
388 }}\r
389 \r