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
24 #include "texture.h"
\r
28 #include <common/except.h>
\r
29 #include <common/gl/gl_check.h>
\r
31 #include <gl/glew.h>
\r
33 #include <tbb/atomic.h>
\r
35 #include <boost/thread/future.hpp>
\r
37 namespace caspar { namespace accelerator { namespace ogl {
\r
39 static GLenum FORMAT[] = {0, GL_RED, GL_RG, GL_BGR, GL_BGRA};
\r
40 static GLenum INTERNAL_FORMAT[] = {0, GL_R8, GL_RG8, GL_RGB8, GL_RGBA8};
\r
41 static GLenum TYPE[] = {0, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_INT_8_8_8_8_REV};
\r
43 unsigned int format(int stride)
\r
45 return FORMAT[stride];
\r
48 static tbb::atomic<int> g_total_count;
\r
50 struct texture::impl : boost::noncopyable
\r
58 impl(int width, int height, int stride)
\r
63 GL(glGenTextures(1, &id_));
\r
64 GL(glBindTexture(GL_TEXTURE_2D, id_));
\r
65 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
\r
66 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
\r
67 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
\r
68 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
\r
69 GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], TYPE[stride_], NULL));
\r
70 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
71 //CASPAR_LOG(trace) << "[texture] [" << ++g_total_count << L"] allocated size:" << width*height*stride;
\r
76 glDeleteTextures(1, &id_);
\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
97 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, id_, 0));
\r
103 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
106 void copy_from(buffer& source)
\r
110 GL(glBindTexture(GL_TEXTURE_2D, id_));
\r
111 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], TYPE[stride_], NULL));
\r
112 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
116 void copy_to(buffer& dest)
\r
120 GL(glBindTexture(GL_TEXTURE_2D, id_));
\r
121 GL(glReadBuffer(GL_COLOR_ATTACHMENT0));
\r
122 GL(glReadPixels(0, 0, width_, height_, FORMAT[stride_], TYPE[stride_], NULL));
\r
123 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
129 texture::texture(int width, int height, int stride) : impl_(new impl(width, height, stride)){}
\r
130 texture::~texture(){}
\r
131 void texture::bind(int index){impl_->bind(index);}
\r
132 void texture::unbind(){impl_->unbind();}
\r
133 void texture::attach(){impl_->attach();}
\r
134 void texture::clear(){impl_->clear();}
\r
135 void texture::copy_from(buffer& source){impl_->copy_from(source);}
\r
136 void texture::copy_to(buffer& dest){impl_->copy_to(dest);}
\r
137 int texture::width() const { return impl_->width_; }
\r
138 int texture::height() const { return impl_->height_; }
\r
139 int texture::size() const { return impl_->width_*impl_->height_*impl_->stride_; }
\r
140 int texture::stride() const { return impl_->stride_; }
\r
141 int texture::id() const{ return impl_->id_;}
\r