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