]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/texture.cpp
Radically improved performance in texture.cpp at least on Nvidia Quadro 2000. GL_UNSI...
[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_BYTE };
42 static GLenum READPIXELS_TYPE[] = { 0, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_INT_8_8_8_8_REV };
43
44 static tbb::atomic<int>                 g_total_count;
45 static tbb::atomic<std::size_t> g_total_size;
46
47 struct texture::impl : boost::noncopyable
48 {
49         GLuint  id_;
50
51         const int       width_;
52         const int       height_;
53         const int       stride_;
54         const bool      mipmapped_;
55 public:
56         impl(int width, int height, int stride, bool mipmapped) 
57                 : width_(width)
58                 , height_(height)
59                 , stride_(stride)
60                 , mipmapped_(mipmapped)
61         {       
62                 GL(glGenTextures(1, &id_));
63                 GL(glBindTexture(GL_TEXTURE_2D, id_));
64                 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (mipmapped ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR)));
65                 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
66                 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
67                 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
68                 GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], TYPE[stride_], NULL));
69
70                 if (mipmapped)
71                 {
72                         enable_anosotropic_filtering_if_available();
73                         GL(glGenerateMipmap(GL_TEXTURE_2D));
74                 }
75
76                 GL(glBindTexture(GL_TEXTURE_2D, 0));
77                 g_total_count++;
78                 g_total_size += static_cast<std::size_t>(width * height * stride * (mipmapped ? 1.33 : 1.0));
79                 //CASPAR_LOG(trace) << "[texture] [" << ++g_total_count << L"] allocated size:" << width*height*stride; 
80         }       
81
82         void enable_anosotropic_filtering_if_available()
83         {
84                 static auto AVAILABLE = glewIsSupported("GL_EXT_texture_filter_anisotropic");
85
86                 if (!AVAILABLE)
87                         return;
88
89                 static GLfloat MAX_ANISOTROPY = []() -> GLfloat
90                 {
91                         GLfloat anisotropy;
92                         glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropy);
93                         return anisotropy;
94                 }();
95
96                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MAX_ANISOTROPY);
97         }
98
99         ~impl()
100         {
101                 glDeleteTextures(1, &id_);
102                 g_total_size -= static_cast<std::size_t>(width_ * height_ * stride_ * (mipmapped_ ? 1.33 : 1.0));
103                 g_total_count--;
104         }
105         
106         void bind()
107         {
108                 GL(glBindTexture(GL_TEXTURE_2D, id_));
109         }
110
111         void bind(int index)
112         {
113                 GL(glActiveTexture(GL_TEXTURE0+index));
114                 bind();
115         }
116
117         void unbind()
118         {
119                 GL(glBindTexture(GL_TEXTURE_2D, 0));
120         }
121
122         void attach()
123         {               
124                 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, id_, 0));
125         }
126
127         void clear()
128         {
129                 attach();               
130                 GL(glClear(GL_COLOR_BUFFER_BIT));
131         }
132                 
133         void copy_from(buffer& source)
134         {
135                 source.unmap();
136                 source.bind();
137                 GL(glBindTexture(GL_TEXTURE_2D, id_));
138                 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], TYPE[stride_], NULL));
139
140                 if (mipmapped_)
141                         GL(glGenerateMipmap(GL_TEXTURE_2D));
142
143                 GL(glBindTexture(GL_TEXTURE_2D, 0));
144                 source.unbind();
145                 source.map(); // Just map it back since map will orphan buffer.
146         }
147
148         void copy_to(buffer& dest)
149         {
150                 dest.unmap();
151                 dest.bind();
152                 GL(glBindTexture(GL_TEXTURE_2D, id_));
153                 GL(glReadBuffer(GL_COLOR_ATTACHMENT0));
154                 GL(glReadPixels(0, 0, width_, height_, FORMAT[stride_], READPIXELS_TYPE[stride_], NULL));
155                 GL(glBindTexture(GL_TEXTURE_2D, 0));
156                 dest.unbind();
157                 GL(glFlush());
158         }
159 };
160
161 texture::texture(int width, int height, int stride, bool mipmapped) : impl_(new impl(width, height, stride, mipmapped)){}
162 texture::texture(texture&& other) : impl_(std::move(other.impl_)){}
163 texture::~texture(){}
164 texture& texture::operator=(texture&& other){impl_ = std::move(other.impl_); return *this;}
165 void texture::bind(int index){impl_->bind(index);}
166 void texture::unbind(){impl_->unbind();}
167 void texture::attach(){impl_->attach();}
168 void texture::clear(){impl_->clear();}
169 void texture::copy_from(buffer& source){impl_->copy_from(source);}
170 void texture::copy_to(buffer& dest){impl_->copy_to(dest);}
171 int texture::width() const { return impl_->width_; }
172 int texture::height() const { return impl_->height_; }
173 int texture::stride() const { return impl_->stride_; }
174 bool texture::mipmapped() const { return impl_->mipmapped_; }
175 std::size_t texture::size() const { return static_cast<std::size_t>(impl_->width_*impl_->height_*impl_->stride_); }
176 int texture::id() const{ return impl_->id_;}
177
178 boost::property_tree::wptree texture::info()
179 {
180         boost::property_tree::wptree info;
181
182         info.add(L"total_count", g_total_count);
183         info.add(L"total_size", g_total_size);
184
185         return info;
186 }
187
188 }}}