]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/ogl_device.cpp
2.0. - blend_modes are re-enabled.
[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 #include "../../stdafx.h"\r
21 \r
22 #include "ogl_device.h"\r
23 \r
24 #include "shader.h"\r
25 \r
26 #include <common/exception/exceptions.h>\r
27 #include <common/utility/assert.h>\r
28 #include <common/gl/gl_check.h>\r
29 \r
30 #include <boost/foreach.hpp>\r
31 \r
32 #include <gl/glew.h>\r
33 \r
34 namespace caspar { namespace core {\r
35 \r
36 ogl_device::ogl_device() \r
37         : executor_(L"ogl_device")\r
38         , pattern_(nullptr)\r
39         , attached_texture_(0)\r
40         , active_shader_(0)\r
41 {\r
42         std::fill(binded_textures_.begin(), binded_textures_.end(), 0);\r
43         std::fill(viewport_.begin(), viewport_.end(), 0);\r
44         std::fill(scissor_.begin(), scissor_.end(), 0);\r
45         \r
46         invoke([=]\r
47         {\r
48                 context_.reset(new sf::Context());\r
49                 context_->SetActive(true);\r
50                 \r
51                 if (glewInit() != GLEW_OK)\r
52                         BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));\r
53                                 \r
54                 if(!GLEW_VERSION_3_2)\r
55                         CASPAR_LOG(warning) << "Missing OpenGL 3.2 support.";\r
56         \r
57                 GL(glGenFramebuffers(1, &fbo_));                \r
58                 GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));\r
59                 GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));\r
60         });\r
61 }\r
62 \r
63 ogl_device::~ogl_device()\r
64 {\r
65         invoke([=]\r
66         {\r
67                 BOOST_FOREACH(auto& pool, device_pools_)\r
68                         pool.clear();\r
69                 BOOST_FOREACH(auto& pool, host_pools_)\r
70                         pool.clear();\r
71                 glDeleteFramebuffersEXT(1, &fbo_);\r
72         });\r
73 }\r
74                                 \r
75 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)\r
76 {\r
77         CASPAR_VERIFY(stride > 0 && stride < 5);\r
78         CASPAR_VERIFY(width > 0 && height > 0);\r
79         auto pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
80         std::shared_ptr<device_buffer> buffer;\r
81         if(!pool->try_pop(buffer))              \r
82         {\r
83                 executor_.invoke([&]\r
84                 {       \r
85                         try\r
86                         {\r
87                                 buffer.reset(new device_buffer(width, height, stride));\r
88                         }\r
89                         catch(...)\r
90                         {\r
91                                 try\r
92                                 {\r
93                                         yield();\r
94                                         gc().wait();\r
95                                         \r
96                                         // Try again\r
97                                         buffer.reset(new device_buffer(width, height, stride));\r
98                                 }\r
99                                 catch(...)\r
100                                 {\r
101                                         CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";\r
102                                         throw;\r
103                                 }\r
104                         }\r
105 \r
106                 }, high_priority);      \r
107         }\r
108                         \r
109         return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*)\r
110         {\r
111                 pool->push(buffer);\r
112         });\r
113 }\r
114         \r
115 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)\r
116 {\r
117         CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);\r
118         CASPAR_VERIFY(size > 0);\r
119         auto pool = host_pools_[usage][size];\r
120         std::shared_ptr<host_buffer> buffer;\r
121         if(!pool->try_pop(buffer))\r
122         {\r
123                 executor_.invoke([&]\r
124                 {\r
125                         try\r
126                         {\r
127                                 buffer.reset(new host_buffer(size, usage));\r
128                                 if(usage == host_buffer::write_only)\r
129                                         buffer->map();\r
130                                 else\r
131                                         buffer->unmap();                        \r
132                         }\r
133                         catch(...)\r
134                         {\r
135                                 try\r
136                                 {\r
137                                         yield();\r
138                                         gc().wait();\r
139 \r
140                                         // Try again\r
141                                         buffer.reset(new host_buffer(size, usage));\r
142                                         if(usage == host_buffer::write_only)\r
143                                                 buffer->map();\r
144                                         else\r
145                                                 buffer->unmap();        \r
146                                 }\r
147                                 catch(...)\r
148                                 {\r
149                                         CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";\r
150                                         throw;          \r
151                                 }\r
152                         }\r
153 \r
154                 }, high_priority);      \r
155         }\r
156         \r
157         return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)\r
158         {\r
159                 executor_.begin_invoke([=]\r
160                 {\r
161                         if(usage == host_buffer::write_only)\r
162                                 buffer->map();\r
163                         else\r
164                                 buffer->unmap();\r
165                         \r
166                         pool->push(buffer);\r
167 \r
168                 }, high_priority);\r
169         });\r
170 }\r
171 \r
172 void ogl_device::yield()\r
173 {\r
174         executor_.yield();\r
175 }\r
176 \r
177 boost::unique_future<void> ogl_device::gc()\r
178 {       \r
179         return begin_invoke([=]\r
180         {\r
181                 CASPAR_LOG(info) << " ogl: Running GC.";                \r
182         \r
183                 try\r
184                 {\r
185                         BOOST_FOREACH(auto& pools, device_pools_)\r
186                         {\r
187                                 BOOST_FOREACH(auto& pool, pools)\r
188                                         pool.second->clear();\r
189                         }\r
190                         BOOST_FOREACH(auto& pools, host_pools_)\r
191                         {\r
192                                 BOOST_FOREACH(auto& pool, pools)\r
193                                         pool.second->clear();\r
194                         }\r
195                 }\r
196                 catch(...)\r
197                 {\r
198                         CASPAR_LOG_CURRENT_EXCEPTION();\r
199                 }\r
200         }, high_priority);\r
201 }\r
202 \r
203 std::wstring ogl_device::get_version()\r
204 {       \r
205         static std::wstring ver = L"Not found";\r
206         try\r
207         {\r
208                 ogl_device tmp;\r
209                 ver = widen(tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})\r
210                 + " "   + tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));                  \r
211         }\r
212         catch(...){}\r
213 \r
214         return ver;\r
215 }\r
216 \r
217 \r
218 void ogl_device::enable(GLenum cap)\r
219 {\r
220         auto& val = caps_[cap];\r
221         if(!val)\r
222         {\r
223                 glEnable(cap);\r
224                 val = true;\r
225         }\r
226 }\r
227 \r
228 void ogl_device::disable(GLenum cap)\r
229 {\r
230         auto& val = caps_[cap];\r
231         if(val)\r
232         {\r
233                 glDisable(cap);\r
234                 val = false;\r
235         }\r
236 }\r
237 \r
238 void ogl_device::viewport(size_t x, size_t y, size_t width, size_t height)\r
239 {\r
240         if(x != viewport_[0] || y != viewport_[1] || width != viewport_[2] || height != viewport_[3])\r
241         {               \r
242                 glViewport(x, y, width, height);\r
243                 viewport_[0] = x;\r
244                 viewport_[1] = y;\r
245                 viewport_[2] = width;\r
246                 viewport_[3] = height;\r
247         }\r
248 }\r
249 \r
250 void ogl_device::scissor(size_t x, size_t y, size_t width, size_t height)\r
251 {\r
252         if(x != scissor_[0] || y != scissor_[1] || width != scissor_[2] || height != scissor_[3])\r
253         {               \r
254                 glScissor(x, y, width, height);\r
255                 scissor_[0] = x;\r
256                 scissor_[1] = y;\r
257                 scissor_[2] = width;\r
258                 scissor_[3] = height;\r
259         }\r
260 }\r
261 \r
262 void ogl_device::stipple_pattern(const GLubyte* pattern)\r
263 {\r
264         if(pattern_ != pattern)\r
265         {               \r
266                 glPolygonStipple(pattern);\r
267                 pattern_ = pattern;\r
268         }\r
269 }\r
270 \r
271 void ogl_device::attach(device_buffer& texture)\r
272 {       \r
273         if(attached_texture_ != texture.id())\r
274         {\r
275                 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0);\r
276                 attached_texture_ = texture.id();\r
277         }\r
278 }\r
279 \r
280 void ogl_device::clear(device_buffer& texture)\r
281 {       \r
282         attach(texture);\r
283         glClear(GL_COLOR_BUFFER_BIT);\r
284 }\r
285 \r
286 void ogl_device::use(shader& shader)\r
287 {\r
288         if(active_shader_ != shader.id())\r
289         {               \r
290                 glUseProgramObjectARB(shader.id());     \r
291                 active_shader_ = shader.id();\r
292         }\r
293 }\r
294 \r
295 }}\r
296 \r