]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/host_buffer.cpp
2.0.0.2: Merged mixer into core/mixer.
[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 "../gpu/host_buffer.h"\r
23 \r
24 #include <common/gl/gl_check.h>\r
25 \r
26 namespace caspar { namespace mixer {\r
27                                                                                                                                                                                                                                                                                                                                 \r
28 struct host_buffer::implementation : boost::noncopyable\r
29 {       \r
30         GLuint pbo_;\r
31 \r
32         const size_t size_;\r
33 \r
34         void* data_;\r
35         GLenum usage_;\r
36         GLenum target_;\r
37 \r
38 public:\r
39         implementation(size_t size, usage_t usage) \r
40                 : size_(size)\r
41                 , data_(nullptr)\r
42                 , pbo_(0)\r
43                 , target_(usage == write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)\r
44                 , usage_(usage == write_only ? GL_STREAM_DRAW : GL_STREAM_READ)\r
45         {\r
46                 GL(glGenBuffers(1, &pbo_));\r
47                 GL(glBindBuffer(target_, pbo_));\r
48                 GL(glBufferData(target_, size_, NULL, usage_)); \r
49                 GL(glBindBuffer(target_, 0));\r
50 \r
51                 if(!pbo_)\r
52                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));\r
53 \r
54                 //CASPAR_LOG(trace) << "[host_buffer] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");\r
55         }       \r
56 \r
57         ~implementation()\r
58         {\r
59                 GL(glDeleteBuffers(1, &pbo_));\r
60         }\r
61 \r
62         void map()\r
63         {\r
64                 if(data_)\r
65                         return;\r
66                 \r
67                 GL(glBindBuffer(target_, pbo_));\r
68                 data_ = glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY);  \r
69                 GL(glBindBuffer(target_, 0)); \r
70                 if(!data_)\r
71                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));\r
72         }\r
73 \r
74         void unmap()\r
75         {\r
76                 if(!data_)\r
77                         return;\r
78                 \r
79                 GL(glBindBuffer(target_, pbo_));\r
80                 GL(glUnmapBuffer(target_));     \r
81                 data_ = nullptr;                \r
82                 GL(glBindBuffer(target_, 0));\r
83         }\r
84 \r
85         void bind()\r
86         {\r
87                 GL(glBindBuffer(target_, pbo_));\r
88         }\r
89 \r
90         void unbind()\r
91         {\r
92                 GL(glBindBuffer(target_, 0));\r
93         }\r
94 };\r
95 \r
96 host_buffer::host_buffer(size_t size, usage_t usage) : impl_(new implementation(size, usage)){}\r
97 const void* host_buffer::data() const {return impl_->data_;}\r
98 void* host_buffer::data() {return impl_->data_;}\r
99 void host_buffer::map(){impl_->map();}\r
100 void host_buffer::unmap(){impl_->unmap();}\r
101 void host_buffer::bind(){impl_->bind();}\r
102 void host_buffer::unbind(){impl_->unbind();}\r
103 \r
104 size_t host_buffer::size() const { return impl_->size_; }\r
105 \r
106 }}