]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/ogl_device.cpp
2.0.0.2: mixer: Disabled IS_KEY. Still not working 100%. Refactored. Use global frame...
[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 <common/utility/assert.h>\r
25 #include <common/gl/gl_check.h>\r
26 \r
27 #include <Glee.h>\r
28 #include <SFML/Window.hpp>\r
29 \r
30 #include <boost/foreach.hpp>\r
31 \r
32 namespace caspar { namespace mixer {\r
33 \r
34 ogl_device::ogl_device() : executor_(L"ogl_device", true)\r
35 {\r
36         invoke([=]\r
37         {\r
38                 context_.reset(new sf::Context());\r
39                 context_->SetActive(true);\r
40                                                 \r
41                 if(!GLEE_VERSION_3_0)\r
42                         BOOST_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 3.0 support."));\r
43 \r
44                 GL(glGenFramebuffers(1, &fbo_));                \r
45                 GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));\r
46                 GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));\r
47         });\r
48 }\r
49 \r
50 ogl_device::~ogl_device()\r
51 {\r
52         invoke([=]\r
53         {\r
54                 BOOST_FOREACH(auto& pool, device_pools_)\r
55                         pool.clear();\r
56                 BOOST_FOREACH(auto& pool, host_pools_)\r
57                         pool.clear();\r
58                 glDeleteFramebuffersEXT(1, &fbo_);\r
59         });\r
60 }\r
61                                 \r
62 safe_ptr<device_buffer> ogl_device::do_create_device_buffer(size_t width, size_t height, size_t stride)\r
63 {\r
64         CASPAR_VERIFY(stride > 0 && stride < 5);\r
65         CASPAR_VERIFY(width > 0 && height > 0);\r
66         auto pool = &device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
67         std::shared_ptr<device_buffer> buffer;\r
68         if(!pool->try_pop(buffer))              \r
69         {\r
70                 executor_.invoke([&]\r
71                 {\r
72                         buffer = std::make_shared<device_buffer>(width, height, stride);\r
73                 });     \r
74                 executor_.begin_invoke([=]\r
75                 {\r
76                         auto buffer = std::make_shared<device_buffer>(width, height, stride);\r
77                         pool->try_push(buffer);\r
78                 });     \r
79         }\r
80                         \r
81         return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*){pool->push(buffer);});\r
82 }\r
83         \r
84 safe_ptr<host_buffer> ogl_device::do_create_host_buffer(size_t size, host_buffer::usage_t usage)\r
85 {\r
86         CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);\r
87         CASPAR_VERIFY(size > 0);\r
88         auto pool = &host_pools_[usage][size];\r
89         std::shared_ptr<host_buffer> buffer;\r
90         if(!pool->try_pop(buffer))\r
91         {\r
92                 executor_.invoke([&]\r
93                 {\r
94                         buffer = std::make_shared<host_buffer>(size, usage);\r
95                         if(usage == host_buffer::write_only)\r
96                                 buffer->map();\r
97                         else\r
98                                 buffer->unmap();\r
99                 });     \r
100                 executor_.begin_invoke([=]\r
101                 {\r
102                         auto buffer = std::make_shared<host_buffer>(size, usage);\r
103                         if(usage == host_buffer::write_only)\r
104                                 buffer->map();\r
105                         else\r
106                                 buffer->unmap();\r
107                         pool->try_push(buffer);\r
108                 });     \r
109         }\r
110         \r
111         return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)\r
112         {\r
113                 executor_.begin_invoke([=]\r
114                 {\r
115                         if(usage == host_buffer::write_only)\r
116                                 buffer->map();\r
117                         else\r
118                                 buffer->unmap();\r
119                         pool->push(buffer);\r
120                 });\r
121         });\r
122 }\r
123 \r
124 std::wstring ogl_device::get_version()\r
125 {       \r
126         return widen(invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})\r
127         + " "   + invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));      \r
128 }\r
129 \r
130 }}