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