2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../stdafx.h"
28 #include <common/except.h>
29 #include <common/gl/gl_check.h>
33 #include <tbb/atomic.h>
35 #include <boost/thread/future.hpp>
37 namespace caspar { namespace accelerator { namespace ogl {
39 static GLenum FORMAT[] = {0, GL_RED, GL_RG, GL_BGR, GL_BGRA};
40 static GLenum INTERNAL_FORMAT[] = {0, GL_R8, GL_RG8, GL_RGB8, GL_RGBA8};
41 static GLenum TYPE[] = {0, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_INT_8_8_8_8_REV};
43 static tbb::atomic<int> g_total_count;
45 struct texture::impl : boost::noncopyable
53 impl(int width, int height, int stride)
58 GL(glGenTextures(1, &id_));
59 GL(glBindTexture(GL_TEXTURE_2D, id_));
60 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
61 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
62 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
63 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
64 GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], TYPE[stride_], NULL));
65 GL(glBindTexture(GL_TEXTURE_2D, 0));
66 //CASPAR_LOG(trace) << "[texture] [" << ++g_total_count << L"] allocated size:" << width*height*stride;
71 glDeleteTextures(1, &id_);
76 GL(glBindTexture(GL_TEXTURE_2D, id_));
81 GL(glActiveTexture(GL_TEXTURE0+index));
87 GL(glBindTexture(GL_TEXTURE_2D, 0));
92 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, id_, 0));
98 GL(glClear(GL_COLOR_BUFFER_BIT));
101 void copy_from(buffer& source)
105 GL(glBindTexture(GL_TEXTURE_2D, id_));
106 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], TYPE[stride_], NULL));
107 GL(glBindTexture(GL_TEXTURE_2D, 0));
109 source.map(); // Just map it back since map will orphan buffer.
112 void copy_to(buffer& dest)
116 GL(glBindTexture(GL_TEXTURE_2D, id_));
117 GL(glReadBuffer(GL_COLOR_ATTACHMENT0));
118 GL(glReadPixels(0, 0, width_, height_, FORMAT[stride_], TYPE[stride_], NULL));
119 GL(glBindTexture(GL_TEXTURE_2D, 0));
125 texture::texture(int width, int height, int stride) : impl_(new impl(width, height, stride)){}
126 texture::texture(texture&& other) : impl_(std::move(other.impl_)){}
127 texture::~texture(){}
128 texture& texture::operator=(texture&& other){impl_ = std::move(other.impl_); return *this;}
129 void texture::bind(int index){impl_->bind(index);}
130 void texture::unbind(){impl_->unbind();}
131 void texture::attach(){impl_->attach();}
132 void texture::clear(){impl_->clear();}
133 void texture::copy_from(buffer& source){impl_->copy_from(source);}
134 void texture::copy_to(buffer& dest){impl_->copy_to(dest);}
135 int texture::width() const { return impl_->width_; }
136 int texture::height() const { return impl_->height_; }
137 int texture::stride() const { return impl_->stride_; }
138 std::size_t texture::size() const { return static_cast<std::size_t>(impl_->width_*impl_->height_*impl_->stride_); }
139 int texture::id() const{ return impl_->id_;}