]> git.sesse.net Git - casparcg/blob - accelerator/ogl/context.cpp
2.1.0: image_mixer: Optimized transfers.
[casparcg] / accelerator / ogl / context.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 "context.h"\r
27 \r
28 #include "shader.h"\r
29 \r
30 #include <common/except.h>\r
31 #include <common/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 accelerator { namespace ogl {\r
39 \r
40 context::context() \r
41         : executor_(L"context")\r
42 {\r
43         CASPAR_LOG(info) << L"Initializing OpenGL Device.";\r
44                 \r
45         invoke([=]\r
46         {\r
47                 context_.reset(new sf::Context());\r
48                 context_->SetActive(true);\r
49                 \r
50                 if (glewInit() != GLEW_OK)\r
51                         BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));\r
52                                                 \r
53                 CASPAR_LOG(info) << L"OpenGL " << version();\r
54 \r
55                 if(!GLEW_VERSION_3_0)\r
56                         BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Your graphics card does not meet the minimum hardware requirements since it does not support OpenGL 3.0 or higher. CasparCG Server will not be able to continue."));\r
57         \r
58                 glGenFramebuffers(1, &fbo_);    \r
59                 \r
60                 CASPAR_LOG(info) << L"Successfully initialized OpenGL Device.";\r
61         });\r
62 }\r
63 \r
64 context::~context()\r
65 {\r
66         invoke([=]\r
67         {\r
68                 BOOST_FOREACH(auto& pool, device_pools_)\r
69                         pool.clear();\r
70                 BOOST_FOREACH(auto& pool, host_pools_)\r
71                         pool.clear();\r
72                 glDeleteFramebuffers(1, &fbo_);\r
73         });\r
74 }\r
75 \r
76 spl::shared_ptr<device_buffer> context::allocate_device_buffer(int width, int height, int stride)\r
77 {\r
78         std::shared_ptr<device_buffer> buffer;\r
79         try\r
80         {\r
81                 buffer.reset(new device_buffer(shared_from_this(), width, height, stride));\r
82         }\r
83         catch(...)\r
84         {\r
85                 try\r
86                 {\r
87                         executor_.yield();\r
88                         gc().wait();\r
89                                         \r
90                         // Try again\r
91                         buffer.reset(new device_buffer(shared_from_this(), width, height, stride));\r
92                 }\r
93                 catch(...)\r
94                 {\r
95                         CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";\r
96                         throw;\r
97                 }\r
98         }\r
99         return spl::make_shared_ptr(buffer);\r
100 }\r
101                                 \r
102 spl::shared_ptr<device_buffer> context::create_device_buffer(int width, int height, int stride)\r
103 {\r
104         CASPAR_VERIFY(stride > 0 && stride < 5);\r
105         CASPAR_VERIFY(width > 0 && height > 0);\r
106         auto& pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
107         std::shared_ptr<device_buffer> buffer;\r
108         if(!pool->items.try_pop(buffer))                \r
109                 buffer = executor_.invoke([&]{return allocate_device_buffer(width, height, stride);}, task_priority::high_priority);                    \r
110         \r
111         //++pool->usage_count;\r
112 \r
113         return spl::shared_ptr<device_buffer>(buffer.get(), [=](device_buffer*) mutable\r
114         {               \r
115                 pool->items.push(buffer);       \r
116         });\r
117 }\r
118 \r
119 spl::shared_ptr<host_buffer> context::allocate_host_buffer(int size, host_buffer::usage usage)\r
120 {\r
121         std::shared_ptr<host_buffer> buffer;\r
122 \r
123         try\r
124         {\r
125                 buffer.reset(new host_buffer(shared_from_this(), size, usage));\r
126                 if(usage == host_buffer::usage::write_only)\r
127                         buffer->map();\r
128                 else\r
129                         buffer->unmap();                        \r
130         }\r
131         catch(...)\r
132         {\r
133                 try\r
134                 {\r
135                         executor_.yield();\r
136                         gc().wait();\r
137 \r
138                         // Try again\r
139                         buffer.reset(new host_buffer(shared_from_this(), size, usage));\r
140                         if(usage == host_buffer::usage::write_only)\r
141                                 buffer->map();\r
142                         else\r
143                                 buffer->unmap();        \r
144                 }\r
145                 catch(...)\r
146                 {\r
147                         CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";\r
148                         throw;          \r
149                 }\r
150         }\r
151 \r
152         return spl::make_shared_ptr(buffer);\r
153 }\r
154         \r
155 spl::shared_ptr<host_buffer> context::create_host_buffer(int size, host_buffer::usage usage)\r
156 {\r
157         CASPAR_VERIFY(usage == host_buffer::usage::write_only || usage == host_buffer::usage::read_only);\r
158         CASPAR_VERIFY(size > 0);\r
159         auto& pool = host_pools_[usage.value()][size];\r
160         std::shared_ptr<host_buffer> buffer;\r
161         if(!pool->items.try_pop(buffer))        \r
162                 buffer = executor_.invoke([=]{return allocate_host_buffer(size, usage);}, task_priority::high_priority);        \r
163         \r
164         auto self = shared_from_this();\r
165         bool is_write_only      = (usage == host_buffer::usage::write_only);\r
166         return spl::shared_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable\r
167         {\r
168                 self->executor_.begin_invoke([=]() mutable\r
169                 {               \r
170                         if(is_write_only)\r
171                                 buffer->map();\r
172                         else\r
173                                 buffer->unmap();\r
174 \r
175                         pool->items.push(buffer);\r
176                 }, task_priority::high_priority);       \r
177         });\r
178 }\r
179 \r
180 spl::shared_ptr<context> context::create()\r
181 {\r
182         return spl::shared_ptr<context>(new context());\r
183 }\r
184 \r
185 //template<typename T>\r
186 //void flush_pool(buffer_pool<T>& pool)\r
187 //{     \r
188 //      if(pool.flush_count.fetch_and_increment() < 16)\r
189 //              return;\r
190 //\r
191 //      if(pool.usage_count.fetch_and_store(0) < pool.items.size())\r
192 //      {\r
193 //              std::shared_ptr<T> buffer;\r
194 //              pool.items.try_pop(buffer);\r
195 //      }\r
196 //\r
197 //      pool.flush_count = 0;\r
198 //      pool.usage_count = 0;\r
199 //}\r
200 \r
201 boost::unique_future<void> context::gc()\r
202 {       \r
203         return begin_invoke([=]\r
204         {\r
205                 CASPAR_LOG(info) << " ogl: Running GC.";                \r
206         \r
207                 try\r
208                 {\r
209                         BOOST_FOREACH(auto& pools, device_pools_)\r
210                         {\r
211                                 BOOST_FOREACH(auto& pool, pools)\r
212                                         pool.second->items.clear();\r
213                         }\r
214                         BOOST_FOREACH(auto& pools, host_pools_)\r
215                         {\r
216                                 BOOST_FOREACH(auto& pool, pools)\r
217                                         pool.second->items.clear();\r
218                         }\r
219                 }\r
220                 catch(...)\r
221                 {\r
222                         CASPAR_LOG_CURRENT_EXCEPTION();\r
223                 }\r
224         }, task_priority::high_priority);\r
225 }\r
226 \r
227 std::wstring context::version()\r
228 {       \r
229         static std::wstring ver = L"Not found";\r
230         try\r
231         {\r
232                 ver = u16(invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})\r
233                 + " "   + invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));                      \r
234         }\r
235         catch(...){}\r
236 \r
237         return ver;\r
238 }\r
239 \r
240 void context::attach(device_buffer& texture)\r
241 {       \r
242         glBindFramebuffer(GL_FRAMEBUFFER, fbo_);\r
243         GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));\r
244 }\r
245 \r
246 void context::clear(device_buffer& texture)\r
247 {       \r
248         attach(texture);\r
249         GL(glClear(GL_COLOR_BUFFER_BIT));\r
250 }\r
251 \r
252 void context::use(shader& shader)\r
253 {       \r
254         GL(glUseProgramObjectARB(shader.id())); \r
255 }\r
256 \r
257 boost::unique_future<spl::shared_ptr<device_buffer>> context::copy_async(spl::shared_ptr<host_buffer>& source, int width, int height, int stride)\r
258 {\r
259         return executor_.begin_invoke([=]() -> spl::shared_ptr<device_buffer>\r
260         {\r
261                 auto result = create_device_buffer(width, height, stride);\r
262                 result->copy_from(source);\r
263                 return result;\r
264         }, task_priority::high_priority);\r
265 }
266
267 void context::yield()\r
268 {\r
269         executor_.yield(task_priority::high_priority);\r
270 }\r
271 \r
272 }}}\r
273 \r
274 \r