]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/host_buffer.cpp
2.0.0.2: Fixed leaking gpu-buffers.
[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 core {\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                 if(usage_ != write_only)        \r
49                         GL(glBufferData(target_, size_, NULL, usage_)); \r
50                 GL(glBindBuffer(target_, 0));\r
51 \r
52                 if(!pbo_)\r
53                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));\r
54 \r
55                 //CASPAR_LOG(trace) << "[host_buffer] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");\r
56         }       \r
57 \r
58         ~implementation()\r
59         {\r
60                 try\r
61                 {\r
62                         GL(glDeleteBuffers(1, &pbo_));\r
63                 }\r
64                 catch(...)\r
65                 {\r
66                         CASPAR_LOG_CURRENT_EXCEPTION();\r
67                 }\r
68         }\r
69 \r
70         void map()\r
71         {\r
72                 if(data_)\r
73                         return;\r
74 \r
75                 if(usage_ == write_only)                        \r
76                         GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.\r
77                 \r
78                 GL(glBindBuffer(target_, pbo_));\r
79                 data_ = glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY);  \r
80                 GL(glBindBuffer(target_, 0)); \r
81                 if(!data_)\r
82                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));\r
83         }\r
84 \r
85         void unmap()\r
86         {\r
87                 if(!data_)\r
88                         return;\r
89                 \r
90                 GL(glBindBuffer(target_, pbo_));\r
91                 GL(glUnmapBuffer(target_));     \r
92                 data_ = nullptr;                \r
93                 GL(glBindBuffer(target_, 0));\r
94         }\r
95 \r
96         void bind()\r
97         {\r
98                 GL(glBindBuffer(target_, pbo_));\r
99         }\r
100 \r
101         void unbind()\r
102         {\r
103                 GL(glBindBuffer(target_, 0));\r
104         }\r
105 };\r
106 \r
107 host_buffer::host_buffer(size_t size, usage_t usage) : impl_(new implementation(size, usage)){}\r
108 const void* host_buffer::data() const {return impl_->data_;}\r
109 void* host_buffer::data() {return impl_->data_;}\r
110 void host_buffer::map(){impl_->map();}\r
111 void host_buffer::unmap(){impl_->unmap();}\r
112 void host_buffer::bind(){impl_->bind();}\r
113 void host_buffer::unbind(){impl_->unbind();}\r
114 \r
115 size_t host_buffer::size() const { return impl_->size_; }\r
116 \r
117 }}