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