2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
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
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
20 #include "../../stdafx.h"
\r
22 #include "host_buffer.h"
\r
25 #include "ogl_device.h"
\r
27 #include <common/gl/gl_check.h>
\r
29 namespace caspar { namespace core {
\r
31 struct host_buffer::implementation : boost::noncopyable
\r
41 implementation(size_t size, usage_t usage)
\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
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
55 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));
\r
57 CASPAR_LOG(debug) << "[host_buffer] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");
\r
64 GL(glDeleteBuffers(1, &pbo_));
\r
68 CASPAR_LOG_CURRENT_EXCEPTION();
\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
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
84 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));
\r
87 void map2(ogl_device& ogl)
\r
90 ogl.invoke(std::bind(&implementation::map, this), high_priority);
\r
98 GL(glBindBuffer(target_, pbo_));
\r
99 GL(glUnmapBuffer(target_));
\r
101 GL(glBindBuffer(target_, 0));
\r
106 GL(glBindBuffer(target_, pbo_));
\r
111 GL(glBindBuffer(target_, 0));
\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