]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/texture.cpp
2.1.0: -cii: Merged changes from trunk.
[casparcg] / accelerator / ogl / util / texture.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "texture.h"\r
25 \r
26 #include "buffer.h"\r
27 \r
28 #include <common/except.h>\r
29 #include <common/gl/gl_check.h>\r
30 \r
31 #include <gl/glew.h>\r
32 \r
33 #include <tbb/atomic.h>\r
34 \r
35 #include <boost/thread/future.hpp>\r
36 \r
37 namespace caspar { namespace accelerator { namespace ogl {\r
38         \r
39 static GLenum FORMAT[] = {0, GL_RED, GL_RG, GL_BGR, GL_BGRA};\r
40 static GLenum INTERNAL_FORMAT[] = {0, GL_R8, GL_RG8, GL_RGB8, GL_RGBA8};        \r
41 static GLenum TYPE[] = {0, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_INT_8_8_8_8_REV};  \r
42 \r
43 unsigned int format(int stride)\r
44 {\r
45         return FORMAT[stride];\r
46 }\r
47 \r
48 static tbb::atomic<int> g_total_count;\r
49 \r
50 struct texture::impl : boost::noncopyable\r
51 {\r
52         GLuint                                          id_;\r
53 \r
54         const int width_;\r
55         const int height_;\r
56         const int stride_;\r
57 public:\r
58         impl(int width, int height, int stride) \r
59                 : width_(width)\r
60                 , height_(height)\r
61                 , stride_(stride)\r
62         {       \r
63                 GL(glGenTextures(1, &id_));\r
64                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
65                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));\r
66                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));\r
67                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));\r
68                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));\r
69                 GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], TYPE[stride_], NULL));\r
70                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
71                 //CASPAR_LOG(trace) << "[texture] [" << ++g_total_count << L"] allocated size:" << width*height*stride; \r
72         }       \r
73 \r
74         ~impl()\r
75         {\r
76                 glDeleteTextures(1, &id_);\r
77         }\r
78         \r
79         void bind()\r
80         {\r
81                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
82         }\r
83 \r
84         void bind(int index)\r
85         {\r
86                 GL(glActiveTexture(GL_TEXTURE0+index));\r
87                 bind();\r
88         }\r
89 \r
90         void unbind()\r
91         {\r
92                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
93         }\r
94 \r
95         void attach()\r
96         {               \r
97                 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, id_, 0));\r
98         }\r
99 \r
100         void clear()\r
101         {\r
102                 attach();               \r
103                 GL(glClear(GL_COLOR_BUFFER_BIT));\r
104         }\r
105                 \r
106         void copy_from(buffer& source)\r
107         {\r
108                 source.unmap();\r
109                 source.bind();\r
110                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
111                 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], TYPE[stride_], NULL));\r
112                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
113                 source.unbind();\r
114         }\r
115 \r
116         void copy_to(buffer& dest)\r
117         {\r
118                 dest.unmap();\r
119                 dest.bind();\r
120                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
121                 GL(glReadBuffer(GL_COLOR_ATTACHMENT0));\r
122                 GL(glReadPixels(0, 0, width_, height_, FORMAT[stride_], TYPE[stride_], NULL));\r
123                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
124                 dest.unbind();\r
125                 GL(glFlush());\r
126         }\r
127 };\r
128 \r
129 texture::texture(int width, int height, int stride) : impl_(new impl(width, height, stride)){}\r
130 texture::~texture(){}\r
131 void texture::bind(int index){impl_->bind(index);}\r
132 void texture::unbind(){impl_->unbind();}\r
133 void texture::attach(){impl_->attach();}\r
134 void texture::clear(){impl_->clear();}\r
135 void texture::copy_from(buffer& source){impl_->copy_from(source);}\r
136 void texture::copy_to(buffer& dest){impl_->copy_to(dest);}\r
137 int texture::width() const { return impl_->width_; }\r
138 int texture::height() const { return impl_->height_; }\r
139 int texture::size() const { return impl_->width_*impl_->height_*impl_->stride_; }\r
140 int texture::stride() const { return impl_->stride_; }\r
141 int texture::id() const{ return impl_->id_;}\r
142 \r
143 }}}