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;
44 static tbb::atomic<std::size_t> g_total_size;
46 struct texture::impl : boost::noncopyable
53 const bool mipmapped_;
55 impl(int width, int height, int stride, bool mipmapped)
59 , mipmapped_(mipmapped)
61 GL(glGenTextures(1, &id_));
62 GL(glBindTexture(GL_TEXTURE_2D, id_));
63 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (mipmapped ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR)));
64 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
65 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
66 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
67 GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], TYPE[stride_], NULL));
71 enable_anosotropic_filtering_if_available();
72 GL(glGenerateMipmap(GL_TEXTURE_2D));
75 GL(glBindTexture(GL_TEXTURE_2D, 0));
77 g_total_size += static_cast<std::size_t>(width * height * stride * (mipmapped ? 1.33 : 1.0));
78 //CASPAR_LOG(trace) << "[texture] [" << ++g_total_count << L"] allocated size:" << width*height*stride;
81 void enable_anosotropic_filtering_if_available()
83 static auto AVAILABLE = glewIsSupported("GL_EXT_texture_filter_anisotropic");
88 static GLfloat MAX_ANISOTROPY = []() -> GLfloat
91 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropy);
95 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MAX_ANISOTROPY);
100 glDeleteTextures(1, &id_);
101 g_total_size -= static_cast<std::size_t>(width_ * height_ * stride_ * (mipmapped_ ? 1.33 : 1.0));
107 GL(glBindTexture(GL_TEXTURE_2D, id_));
112 GL(glActiveTexture(GL_TEXTURE0+index));
118 GL(glBindTexture(GL_TEXTURE_2D, 0));
123 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, id_, 0));
129 GL(glClear(GL_COLOR_BUFFER_BIT));
132 void copy_from(buffer& source)
136 GL(glBindTexture(GL_TEXTURE_2D, id_));
137 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], TYPE[stride_], NULL));
140 GL(glGenerateMipmap(GL_TEXTURE_2D));
142 GL(glBindTexture(GL_TEXTURE_2D, 0));
144 source.map(); // Just map it back since map will orphan buffer.
147 void copy_to(buffer& dest)
151 GL(glBindTexture(GL_TEXTURE_2D, id_));
152 GL(glReadBuffer(GL_COLOR_ATTACHMENT0));
153 GL(glReadPixels(0, 0, width_, height_, FORMAT[stride_], TYPE[stride_], NULL));
154 GL(glBindTexture(GL_TEXTURE_2D, 0));
160 texture::texture(int width, int height, int stride, bool mipmapped) : impl_(new impl(width, height, stride, mipmapped)){}
161 texture::texture(texture&& other) : impl_(std::move(other.impl_)){}
162 texture::~texture(){}
163 texture& texture::operator=(texture&& other){impl_ = std::move(other.impl_); return *this;}
164 void texture::bind(int index){impl_->bind(index);}
165 void texture::unbind(){impl_->unbind();}
166 void texture::attach(){impl_->attach();}
167 void texture::clear(){impl_->clear();}
168 void texture::copy_from(buffer& source){impl_->copy_from(source);}
169 void texture::copy_to(buffer& dest){impl_->copy_to(dest);}
170 int texture::width() const { return impl_->width_; }
171 int texture::height() const { return impl_->height_; }
172 int texture::stride() const { return impl_->stride_; }
173 bool texture::mipmapped() const { return impl_->mipmapped_; }
174 std::size_t texture::size() const { return static_cast<std::size_t>(impl_->width_*impl_->height_*impl_->stride_); }
175 int texture::id() const{ return impl_->id_;}
177 boost::property_tree::wptree texture::info()
179 boost::property_tree::wptree info;
181 info.add(L"total_count", g_total_count);
182 info.add(L"total_size", g_total_size);