2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../../stdafx.h"
\r
26 #include "texture.h"
\r
29 #include <common/except.h>
\r
30 #include <common/gl/gl_check.h>
\r
32 #include <gl/glew.h>
\r
34 #include <tbb/atomic.h>
\r
36 namespace caspar { namespace accelerator { namespace ogl {
\r
38 static tbb::atomic<int> g_w_total_count;
\r
39 static tbb::atomic<int> g_r_total_count;
\r
41 struct buffer::impl : boost::noncopyable
\r
44 const std::size_t size_;
\r
45 tbb::atomic<uint8_t*> data_;
\r
50 impl(std::size_t size, buffer::usage usage)
\r
52 , target_(usage == buffer::usage::write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)
\r
53 , usage_(usage == buffer::usage::write_only ? GL_STREAM_DRAW : GL_STREAM_READ)
\r
56 GL(glGenBuffers(1, &pbo_));
\r
58 GL(glBufferData(target_, size_, NULL, usage_));
\r
59 if(usage_ == GL_STREAM_DRAW)
\r
60 data_ = (uint8_t*)GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
\r
64 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));
\r
66 //CASPAR_LOG(trace) << "[buffer] [" << ++(usage_ == buffer::usage::write_only ? g_w_total_count : g_r_total_count) << L"] allocated size:" << size_ << " usage: " << (usage == buffer::usage::write_only ? "write_only" : "read_only");
\r
71 glDeleteBuffers(1, &pbo_);
\r
76 if(data_ != nullptr)
\r
79 GL(glBindBuffer(target_, pbo_));
\r
80 if(usage_ == GL_STREAM_DRAW)
\r
81 GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
\r
83 data_ = (uint8_t*)GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
\r
84 GL(glBindBuffer(target_, 0));
\r
86 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target OpenGL Pixel Buffer Object."));
\r
93 if(data_ == nullptr)
\r
96 GL(glBindBuffer(target_, pbo_));
\r
97 GL(glUnmapBuffer(target_));
\r
98 if(usage_ == GL_STREAM_READ)
\r
99 GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
\r
101 GL(glBindBuffer(target_, 0));
\r
106 GL(glBindBuffer(target_, pbo_));
\r
111 GL(glBindBuffer(target_, 0));
\r
115 buffer::buffer(std::size_t size, usage usage) : impl_(new impl(size, usage)){}
\r
116 buffer::~buffer(){}
\r
117 uint8_t* buffer::data(){return impl_->data_;}
\r
118 void buffer::map(){impl_->map();}
\r
119 void buffer::unmap(){impl_->unmap();}
\r
120 void buffer::bind() const{impl_->bind();}
\r
121 void buffer::unbind() const{impl_->unbind();}
\r
122 std::size_t buffer::size() const { return impl_->size_; }
\r