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 "device_buffer.h"
\r
26 #include "ogl_device.h"
\r
28 #include <common/gl/gl_check.h>
\r
30 namespace caspar { namespace core {
\r
32 struct host_buffer::implementation : boost::noncopyable
\r
42 implementation(size_t size, usage_t usage)
\r
46 , target_(usage == write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)
\r
47 , usage_(usage == write_only ? GL_STREAM_DRAW : GL_STREAM_READ)
\r
49 GL(glGenBuffers(1, &pbo_));
\r
50 GL(glBindBuffer(target_, pbo_));
\r
51 if(usage_ != write_only)
\r
52 GL(glBufferData(target_, size_, NULL, usage_));
\r
53 GL(glBindBuffer(target_, 0));
\r
56 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));
\r
58 CASPAR_LOG(debug) << "[host_buffer] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");
\r
65 GL(glDeleteBuffers(1, &pbo_));
\r
69 CASPAR_LOG_CURRENT_EXCEPTION();
\r
78 if(usage_ == write_only)
\r
79 GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
\r
81 GL(glBindBuffer(target_, pbo_));
\r
82 data_ = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
\r
83 GL(glBindBuffer(target_, 0));
\r
85 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));
\r
88 void wait(ogl_device& ogl)
\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
114 void begin_read(device_buffer& source)
\r
120 GL(glReadPixels(0, 0, source.width(), source.height(), format(source.stride()), GL_UNSIGNED_BYTE, NULL));
\r
129 return fence_.ready();
\r
133 host_buffer::host_buffer(size_t size, usage_t usage) : impl_(new implementation(size, usage)){}
\r
134 const void* host_buffer::data() const {return impl_->data_;}
\r
135 void* host_buffer::data() {return impl_->data_;}
\r
136 void host_buffer::map(){impl_->map();}
\r
137 void host_buffer::unmap(){impl_->unmap();}
\r
138 void host_buffer::bind(){impl_->bind();}
\r
139 void host_buffer::unbind(){impl_->unbind();}
\r
140 void host_buffer::begin_read(device_buffer& source){impl_->begin_read(source);}
\r
141 size_t host_buffer::size() const { return impl_->size_; }
\r
142 bool host_buffer::ready() const{return impl_->ready();}
\r
143 void host_buffer::wait(ogl_device& ogl){impl_->wait(ogl);}
\r