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/exception/exceptions.h>
\r
29 #include <common/gl/gl_check.h>
\r
31 #include <gl/glew.h>
\r
33 #include <tbb/atomic.h>
\r
35 namespace caspar { namespace core {
\r
37 static tbb::atomic<int> g_w_total_count;
\r
38 static tbb::atomic<int> g_r_total_count;
\r
40 struct host_buffer::implementation : boost::noncopyable
\r
50 implementation(size_t size, usage_t usage)
\r
54 , target_(usage == write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)
\r
55 , usage_(usage == write_only ? GL_STREAM_DRAW : GL_STREAM_READ)
\r
57 GL(glGenBuffers(1, &pbo_));
\r
58 GL(glBindBuffer(target_, pbo_));
\r
59 if(usage_ != write_only)
\r
60 GL(glBufferData(target_, size_, NULL, usage_));
\r
61 GL(glBindBuffer(target_, 0));
\r
64 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));
\r
66 CASPAR_LOG(trace) << "[host_buffer] [" << ++(usage_ == write_only ? g_w_total_count : g_r_total_count) << L"] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");
\r
73 GL(glDeleteBuffers(1, &pbo_));
\r
74 //CASPAR_LOG(trace) << "[host_buffer] [" << --(usage_ == write_only ? g_w_total_count : g_r_total_count) << L"] deallocated size:" << size_ << " usage: " << (usage_ == write_only ? "write_only" : "read_only");
\r
78 CASPAR_LOG_CURRENT_EXCEPTION();
\r
87 if(usage_ == write_only)
\r
88 GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
\r
90 GL(glBindBuffer(target_, pbo_));
\r
91 data_ = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
\r
92 GL(glBindBuffer(target_, 0));
\r
94 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));
\r
97 void wait(ogl_device& ogl)
\r
107 GL(glBindBuffer(target_, pbo_));
\r
108 GL(glUnmapBuffer(target_));
\r
110 GL(glBindBuffer(target_, 0));
\r
115 GL(glBindBuffer(target_, pbo_));
\r
120 GL(glBindBuffer(target_, 0));
\r
123 void begin_read(size_t width, size_t height, GLuint format)
\r
127 GL(glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, NULL));
\r
134 return fence_.ready();
\r
138 host_buffer::host_buffer(size_t size, usage_t usage) : impl_(new implementation(size, usage)){}
\r
139 const void* host_buffer::data() const {return impl_->data_;}
\r
140 void* host_buffer::data() {return impl_->data_;}
\r
141 void host_buffer::map(){impl_->map();}
\r
142 void host_buffer::unmap(){impl_->unmap();}
\r
143 void host_buffer::bind(){impl_->bind();}
\r
144 void host_buffer::unbind(){impl_->unbind();}
\r
145 void host_buffer::begin_read(size_t width, size_t height, GLuint format){impl_->begin_read(width, height, format);}
\r
146 size_t host_buffer::size() const { return impl_->size_; }
\r
147 bool host_buffer::ready() const{return impl_->ready();}
\r
148 void host_buffer::wait(ogl_device& ogl){impl_->wait(ogl);}
\r