]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/texture.cpp
set svn:eol-style native on .h and .cpp files
[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
45 struct texture::impl : boost::noncopyable
46 {
47         GLuint  id_;
48
49         const int width_;
50         const int height_;
51         const int stride_;
52 public:
53         impl(int width, int height, int stride) 
54                 : width_(width)
55                 , height_(height)
56                 , stride_(stride)
57         {       
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; 
67         }       
68
69         ~impl()
70         {
71                 glDeleteTextures(1, &id_);
72         }
73         
74         void bind()
75         {
76                 GL(glBindTexture(GL_TEXTURE_2D, id_));
77         }
78
79         void bind(int index)
80         {
81                 GL(glActiveTexture(GL_TEXTURE0+index));
82                 bind();
83         }
84
85         void unbind()
86         {
87                 GL(glBindTexture(GL_TEXTURE_2D, 0));
88         }
89
90         void attach()
91         {               
92                 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, id_, 0));
93         }
94
95         void clear()
96         {
97                 attach();               
98                 GL(glClear(GL_COLOR_BUFFER_BIT));
99         }
100                 
101         void copy_from(buffer& source)
102         {
103                 source.unmap();
104                 source.bind();
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));
108                 source.unbind();
109                 source.map(); // Just map it back since map will orphan buffer.
110         }
111
112         void copy_to(buffer& dest)
113         {
114                 dest.unmap();
115                 dest.bind();
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));
120                 dest.unbind();
121                 GL(glFlush());
122         }
123 };
124
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_;}
140
141 }}}