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