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 "device_buffer.h"
\r
26 #include <common/exception/exceptions.h>
\r
27 #include <common/gl/gl_check.h>
\r
29 #include <gl/glew.h>
\r
31 #include <tbb/atomic.h>
\r
33 namespace caspar { namespace core {
\r
35 static GLenum FORMAT[] = {0, GL_RED, GL_RG, GL_BGR, GL_BGRA};
\r
36 static GLenum INTERNAL_FORMAT[] = {0, GL_R8, GL_RG8, GL_RGB8, GL_RGBA8};
\r
38 unsigned int format(size_t stride)
\r
40 return FORMAT[stride];
\r
43 static tbb::atomic<int> g_total_count;
\r
45 struct device_buffer::implementation : boost::noncopyable
\r
49 const size_t width_;
\r
50 const size_t height_;
\r
51 const size_t stride_;
\r
56 implementation(size_t width, size_t height, size_t stride)
\r
61 GL(glGenTextures(1, &id_));
\r
62 GL(glBindTexture(GL_TEXTURE_2D, id_));
\r
63 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
\r
64 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
\r
65 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
\r
66 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
\r
67 GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));
\r
68 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
69 CASPAR_LOG(trace) << "[device_buffer] [" << ++g_total_count << L"] allocated size:" << width*height*stride;
\r
76 GL(glDeleteTextures(1, &id_));
\r
77 CASPAR_LOG(trace) << "[device_buffer] [" << --g_total_count << L"] deallocated size:" << width_*height_*stride_;
\r
81 CASPAR_LOG_CURRENT_EXCEPTION();
\r
87 GL(glBindTexture(GL_TEXTURE_2D, id_));
\r
90 void bind(int index)
\r
92 GL(glActiveTexture(GL_TEXTURE0+index));
\r
98 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
104 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));
\r
111 return fence_.ready();
\r
115 device_buffer::device_buffer(size_t width, size_t height, size_t stride) : impl_(new implementation(width, height, stride)){}
\r
116 size_t device_buffer::stride() const { return impl_->stride_; }
\r
117 size_t device_buffer::width() const { return impl_->width_; }
\r
118 size_t device_buffer::height() const { return impl_->height_; }
\r
119 void device_buffer::bind(int index){impl_->bind(index);}
\r
120 void device_buffer::unbind(){impl_->unbind();}
\r
121 void device_buffer::begin_read(){impl_->begin_read();}
\r
122 bool device_buffer::ready() const{return impl_->ready();}
\r
123 int device_buffer::id() const{ return impl_->id_;}
\r