]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/device_buffer.cpp
2.0.0.2: Merged mixer into core/mixer.
[casparcg] / core / mixer / gpu / device_buffer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "device_buffer.h"\r
23 \r
24 #include <common/gl/gl_check.h>\r
25 namespace caspar { namespace mixer {\r
26         \r
27 GLenum FORMAT[] = {0, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_BGR, GL_BGRA};\r
28 GLenum INTERNAL_FORMAT[] = {0, GL_LUMINANCE8, GL_LUMINANCE8_ALPHA8, GL_RGB8, GL_RGBA8}; \r
29 \r
30 struct device_buffer::implementation : boost::noncopyable\r
31 {\r
32         GLuint id_;\r
33 \r
34         const size_t width_;\r
35         const size_t height_;\r
36         const size_t stride_;\r
37 \r
38 public:\r
39         implementation(size_t width, size_t height, size_t stride) \r
40                 : width_(width)\r
41                 , height_(height)\r
42                 , stride_(stride)\r
43         {       \r
44                 GL(glGenTextures(1, &id_));\r
45                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
46                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));\r
47                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));\r
48                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));\r
49                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));\r
50                 GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
51                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
52                 //CASPAR_LOG(trace) << "[device_buffer] allocated size:" << width*height*stride;        \r
53         }       \r
54 \r
55         ~implementation()\r
56         {\r
57                 GL(glDeleteTextures(1, &id_));\r
58         }\r
59         \r
60         void bind()\r
61         {\r
62                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
63         }\r
64 \r
65         void unbind()\r
66         {\r
67                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
68         }\r
69 \r
70         void read(host_buffer& source)\r
71         {\r
72                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
73                 source.unmap();\r
74                 source.bind();\r
75                 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
76                 source.unbind();\r
77                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
78         }\r
79         \r
80         void write(host_buffer& target)\r
81         {\r
82                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
83                 target.unmap();\r
84                 target.bind();\r
85                 GL(glReadPixels(0, 0, width_, height_, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
86                 target.unbind();\r
87                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
88         }\r
89 \r
90         void attach(int index)\r
91         {\r
92                 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_2D, id_, 0));\r
93         }\r
94 };\r
95 \r
96 device_buffer::device_buffer(size_t width, size_t height, size_t stride) : impl_(new implementation(width, height, stride)){}\r
97 size_t device_buffer::stride() const { return impl_->stride_; }\r
98 size_t device_buffer::width() const { return impl_->width_; }\r
99 size_t device_buffer::height() const { return impl_->height_; }\r
100 void device_buffer::attach(int index){impl_->attach(index);}\r
101 void device_buffer::bind(){impl_->bind();}\r
102 void device_buffer::unbind(){impl_->unbind();}\r
103 void device_buffer::read(host_buffer& source){impl_->read(source);}\r
104 void device_buffer::write(host_buffer& target){impl_->write(target);}\r
105 \r
106 }}