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