]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/host_buffer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[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 "ogl_device.h"\r
26 \r
27 #include <common/gl/gl_check.h>\r
28 \r
29 namespace caspar { namespace core {\r
30                                                                                                                                                                                                                                                                                                                                 \r
31 struct host_buffer::implementation : boost::noncopyable\r
32 {       \r
33         GLuint                  pbo_;\r
34         const size_t    size_;\r
35         void*                   data_;\r
36         GLenum                  usage_;\r
37         GLenum                  target_;\r
38         core::fence             fence_;\r
39 \r
40 public:\r
41         implementation(size_t size, usage_t usage) \r
42                 : size_(size)\r
43                 , data_(nullptr)\r
44                 , pbo_(0)\r
45                 , target_(usage == write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)\r
46                 , usage_(usage == write_only ? GL_STREAM_DRAW : GL_STREAM_READ)\r
47         {\r
48                 GL(glGenBuffers(1, &pbo_));\r
49                 GL(glBindBuffer(target_, pbo_));\r
50                 if(usage_ != write_only)        \r
51                         GL(glBufferData(target_, size_, NULL, usage_)); \r
52                 GL(glBindBuffer(target_, 0));\r
53 \r
54                 if(!pbo_)\r
55                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));\r
56 \r
57                 CASPAR_LOG(debug) << "[host_buffer] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");\r
58         }       \r
59 \r
60         ~implementation()\r
61         {\r
62                 try\r
63                 {\r
64                         GL(glDeleteBuffers(1, &pbo_));\r
65                 }\r
66                 catch(...)\r
67                 {\r
68                         CASPAR_LOG_CURRENT_EXCEPTION();\r
69                 }\r
70         }\r
71 \r
72         void map()\r
73         {\r
74                 if(data_)\r
75                         return;\r
76 \r
77                 if(usage_ == write_only)                        \r
78                         GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.\r
79                 \r
80                 GL(glBindBuffer(target_, pbo_));\r
81                 data_ = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));  \r
82                 GL(glBindBuffer(target_, 0)); \r
83                 if(!data_)\r
84                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));\r
85         }\r
86 \r
87         void map2(ogl_device& ogl)\r
88         {\r
89                 fence_.wait(ogl);\r
90                 ogl.invoke(std::bind(&implementation::map, this), high_priority);\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 fence()\r
115         {\r
116                 fence_.set();\r
117                 GL(glFlush());\r
118         }\r
119 };\r
120 \r
121 host_buffer::host_buffer(size_t size, usage_t usage) : impl_(new implementation(size, usage)){}\r
122 const void* host_buffer::data() const {return impl_->data_;}\r
123 void* host_buffer::data() {return impl_->data_;}\r
124 void host_buffer::map(){impl_->map();}\r
125 void host_buffer::map(ogl_device& ogl){impl_->map2(ogl);}\r
126 void host_buffer::unmap(){impl_->unmap();}\r
127 void host_buffer::bind(){impl_->bind();}\r
128 void host_buffer::unbind(){impl_->unbind();}\r
129 void host_buffer::fence(){impl_->fence();}\r
130 size_t host_buffer::size() const { return impl_->size_; }\r
131 \r
132 }}