]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/texture.cpp
18b899984d78e2c6ce433370a8feae0fdee3a996
[casparcg] / accelerator / ogl / util / texture.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "texture.h"
25
26 #include "buffer.h"
27
28 #include <common/except.h>
29 #include <common/gl/gl_check.h>
30
31 #include <GL/glew.h>
32
33 #include <tbb/atomic.h>
34
35 #include <boost/thread/future.hpp>
36
37 namespace caspar { namespace accelerator { namespace ogl {
38         
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};  
42
43 static tbb::atomic<int>                 g_total_count;
44 static tbb::atomic<std::size_t> g_total_size;
45
46 struct texture::impl : boost::noncopyable
47 {
48         GLuint  id_;
49
50         const int       width_;
51         const int       height_;
52         const int       stride_;
53         const bool      mipmapped_;
54 public:
55         impl(int width, int height, int stride, bool mipmapped) 
56                 : width_(width)
57                 , height_(height)
58                 , stride_(stride)
59                 , mipmapped_(mipmapped)
60         {       
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));
68
69                 if (mipmapped)
70                 {
71                         enable_anosotropic_filtering_if_available();
72                         GL(glGenerateMipmap(GL_TEXTURE_2D));
73                 }
74
75                 GL(glBindTexture(GL_TEXTURE_2D, 0));
76                 g_total_count++;
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; 
79         }       
80
81         void enable_anosotropic_filtering_if_available()
82         {
83                 static auto AVAILABLE = glewIsSupported("GL_EXT_texture_filter_anisotropic");
84
85                 if (!AVAILABLE)
86                         return;
87
88                 static GLfloat MAX_ANISOTROPY = []() -> GLfloat
89                 {
90                         GLfloat anisotropy;
91                         glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropy);
92                         return anisotropy;
93                 }();
94
95                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MAX_ANISOTROPY);
96         }
97
98         ~impl()
99         {
100                 glDeleteTextures(1, &id_);
101                 g_total_size -= static_cast<std::size_t>(width_ * height_ * stride_ * (mipmapped_ ? 1.33 : 1.0));
102                 g_total_count--;
103         }
104         
105         void bind()
106         {
107                 GL(glBindTexture(GL_TEXTURE_2D, id_));
108         }
109
110         void bind(int index)
111         {
112                 GL(glActiveTexture(GL_TEXTURE0+index));
113                 bind();
114         }
115
116         void unbind()
117         {
118                 GL(glBindTexture(GL_TEXTURE_2D, 0));
119         }
120
121         void attach()
122         {               
123                 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, id_, 0));
124         }
125
126         void clear()
127         {
128                 attach();               
129                 GL(glClear(GL_COLOR_BUFFER_BIT));
130         }
131                 
132         void copy_from(buffer& source)
133         {
134                 source.unmap();
135                 source.bind();
136                 GL(glBindTexture(GL_TEXTURE_2D, id_));
137                 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], TYPE[stride_], NULL));
138
139                 if (mipmapped_)
140                         GL(glGenerateMipmap(GL_TEXTURE_2D));
141
142                 GL(glBindTexture(GL_TEXTURE_2D, 0));
143                 source.unbind();
144                 source.map(); // Just map it back since map will orphan buffer.
145         }
146
147         void copy_to(buffer& dest)
148         {
149                 dest.unmap();
150                 dest.bind();
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));
155                 dest.unbind();
156                 GL(glFlush());
157         }
158 };
159
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_;}
176
177 boost::property_tree::wptree texture::info()
178 {
179         boost::property_tree::wptree info;
180
181         info.add(L"total_count", g_total_count);
182         info.add(L"total_size", g_total_size);
183
184         return info;
185 }
186
187 }}}