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