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
24 #include "host_buffer.h"
\r
27 #include <common/gl/gl_check.h>
\r
29 namespace caspar { namespace core {
\r
31 static GLenum FORMAT[] = {0, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_BGR, GL_BGRA};
\r
32 static GLenum INTERNAL_FORMAT[] = {0, GL_LUMINANCE8, GL_LUMINANCE8_ALPHA8, GL_RGB8, GL_RGBA8};
\r
34 GLenum format(size_t stride)
\r
36 return FORMAT[stride];
\r
39 struct device_buffer::implementation : boost::noncopyable
\r
43 const size_t width_;
\r
44 const size_t height_;
\r
45 const size_t stride_;
\r
50 implementation(size_t width, size_t height, size_t stride)
\r
55 GL(glGenTextures(1, &id_));
\r
56 GL(glBindTexture(GL_TEXTURE_2D, id_));
\r
57 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
\r
58 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
\r
59 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
\r
60 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
\r
61 GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));
\r
62 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
63 CASPAR_LOG(debug) << "[device_buffer] allocated size:" << width*height*stride;
\r
71 GL(glDeleteTextures(1, &id_));
\r
75 CASPAR_LOG_CURRENT_EXCEPTION();
\r
81 GL(glBindTexture(GL_TEXTURE_2D, id_));
\r
84 void bind(int index)
\r
86 GL(glActiveTexture(GL_TEXTURE0+index));
\r
92 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
95 void read(host_buffer& source)
\r
97 GL(glBindTexture(GL_TEXTURE_2D, id_));
\r
100 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));
\r
102 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
107 void attach(int index)
\r
109 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_2D, id_, 0));
\r
115 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
120 return fence_.ready();
\r
124 device_buffer::device_buffer(size_t width, size_t height, size_t stride) : impl_(new implementation(width, height, stride)){}
\r
125 size_t device_buffer::stride() const { return impl_->stride_; }
\r
126 size_t device_buffer::width() const { return impl_->width_; }
\r
127 size_t device_buffer::height() const { return impl_->height_; }
\r
128 void device_buffer::attach(int index){impl_->attach(index);}
\r
129 void device_buffer::bind(){impl_->bind();}
\r
130 void device_buffer::bind(int index){impl_->bind(index);}
\r
131 void device_buffer::unbind(){impl_->unbind();}
\r
132 void device_buffer::read(host_buffer& source){impl_->read(source);}
\r
133 void device_buffer::clear(){impl_->clear();}
\r
134 bool device_buffer::ready() const{return impl_->ready();}
\r