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