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