]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/host_buffer.cpp
2.0. Refactoring.
[casparcg] / core / mixer / gpu / host_buffer.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 "host_buffer.h"\r
23 \r
24 #include "fence.h"\r
25 #include "device_buffer.h"\r
26 #include "ogl_device.h"\r
27 \r
28 #include <common/gl/gl_check.h>\r
29 \r
30 namespace caspar { namespace core {\r
31                                                                                                                                                                                                                                                                                                                                 \r
32 struct host_buffer::implementation : boost::noncopyable\r
33 {       \r
34         GLuint                  pbo_;\r
35         const size_t    size_;\r
36         void*                   data_;\r
37         GLenum                  usage_;\r
38         GLenum                  target_;\r
39         fence                   fence_;\r
40 \r
41 public:\r
42         implementation(size_t size, usage_t usage) \r
43                 : size_(size)\r
44                 , data_(nullptr)\r
45                 , pbo_(0)\r
46                 , target_(usage == write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)\r
47                 , usage_(usage == write_only ? GL_STREAM_DRAW : GL_STREAM_READ)\r
48         {\r
49                 GL(glGenBuffers(1, &pbo_));\r
50                 GL(glBindBuffer(target_, pbo_));\r
51                 if(usage_ != write_only)        \r
52                         GL(glBufferData(target_, size_, NULL, usage_)); \r
53                 GL(glBindBuffer(target_, 0));\r
54 \r
55                 if(!pbo_)\r
56                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));\r
57 \r
58                 CASPAR_LOG(debug) << "[host_buffer] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");\r
59         }       \r
60 \r
61         ~implementation()\r
62         {\r
63                 try\r
64                 {\r
65                         GL(glDeleteBuffers(1, &pbo_));\r
66                 }\r
67                 catch(...)\r
68                 {\r
69                         CASPAR_LOG_CURRENT_EXCEPTION();\r
70                 }\r
71         }\r
72 \r
73         void map()\r
74         {\r
75                 if(data_)\r
76                         return;\r
77 \r
78                 if(usage_ == write_only)                        \r
79                         GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.\r
80                 \r
81                 GL(glBindBuffer(target_, pbo_));\r
82                 data_ = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));  \r
83                 GL(glBindBuffer(target_, 0)); \r
84                 if(!data_)\r
85                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));\r
86         }\r
87 \r
88         void wait(ogl_device& ogl)\r
89         {\r
90                 fence_.wait(ogl);\r
91         }\r
92 \r
93         void unmap()\r
94         {\r
95                 if(!data_)\r
96                         return;\r
97                 \r
98                 GL(glBindBuffer(target_, pbo_));\r
99                 GL(glUnmapBuffer(target_));     \r
100                 data_ = nullptr;                \r
101                 GL(glBindBuffer(target_, 0));\r
102         }\r
103 \r
104         void bind()\r
105         {\r
106                 GL(glBindBuffer(target_, pbo_));\r
107         }\r
108 \r
109         void unbind()\r
110         {\r
111                 GL(glBindBuffer(target_, 0));\r
112         }\r
113 \r
114         void read(device_buffer& source)\r
115         {\r
116                 source.attach(0);\r
117                 source.bind();\r
118                 unmap();\r
119                 bind();\r
120                 GL(glReadPixels(0, 0, source.width(), source.height(), format(source.stride()), GL_UNSIGNED_BYTE, NULL));\r
121                 unbind();\r
122                 source.unbind();\r
123                 fence_.set();\r
124                 GL(glFlush());\r
125         }\r
126 \r
127         bool ready() const\r
128         {\r
129                 return fence_.ready();\r
130         }\r
131 };\r
132 \r
133 host_buffer::host_buffer(size_t size, usage_t usage) : impl_(new implementation(size, usage)){}\r
134 const void* host_buffer::data() const {return impl_->data_;}\r
135 void* host_buffer::data() {return impl_->data_;}\r
136 void host_buffer::map(){impl_->map();}\r
137 void host_buffer::unmap(){impl_->unmap();}\r
138 void host_buffer::bind(){impl_->bind();}\r
139 void host_buffer::unbind(){impl_->unbind();}\r
140 void host_buffer::read(device_buffer& source){impl_->read(source);}\r
141 size_t host_buffer::size() const { return impl_->size_; }\r
142 bool host_buffer::ready() const{return impl_->ready();}\r
143 void host_buffer::wait(ogl_device& ogl){impl_->wait(ogl);}\r
144 \r
145 }}