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 "../gpu/host_buffer.h"
\r
24 #include <common/gl/gl_check.h>
\r
26 namespace caspar { namespace core {
\r
28 struct host_buffer::implementation : boost::noncopyable
\r
39 implementation(size_t size, usage_t usage)
\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
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
53 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));
\r
55 CASPAR_LOG(trace) << "[host_buffer] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");
\r
60 GL(glDeleteBuffers(1, &pbo_));
\r
68 if(usage_ == write_only)
\r
69 GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
\r
71 GL(glBindBuffer(target_, pbo_));
\r
72 data_ = glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY);
\r
73 GL(glBindBuffer(target_, 0));
\r
75 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));
\r
83 GL(glBindBuffer(target_, pbo_));
\r
84 GL(glUnmapBuffer(target_));
\r
86 GL(glBindBuffer(target_, 0));
\r
91 GL(glBindBuffer(target_, pbo_));
\r
96 GL(glBindBuffer(target_, 0));
\r
100 host_buffer::host_buffer(size_t size, usage_t usage) : impl_(new implementation(size, usage)){}
\r
101 const void* host_buffer::data() const {return impl_->data_;}
\r
102 void* host_buffer::data() {return impl_->data_;}
\r
103 void host_buffer::map(){impl_->map();}
\r
104 void host_buffer::unmap(){impl_->unmap();}
\r
105 void host_buffer::bind(){impl_->bind();}
\r
106 void host_buffer::unbind(){impl_->unbind();}
\r
108 size_t host_buffer::size() const { return impl_->size_; }
\r