]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/device_buffer.cpp
2.0: amcp: Flash invoke and update should leave case for data string unchanged.
[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 "host_buffer.h"\r
25 \r
26 #include <common/gl/gl_check.h>\r
27 \r
28 namespace caspar { namespace core {\r
29         \r
30 GLenum FORMAT[] = {0, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_BGR, GL_BGRA};\r
31 GLenum INTERNAL_FORMAT[] = {0, GL_LUMINANCE8, GL_LUMINANCE8_ALPHA8, GL_RGB8, GL_RGBA8}; \r
32 \r
33 struct device_buffer::implementation : boost::noncopyable\r
34 {\r
35         GLuint id_;\r
36 \r
37         const size_t width_;\r
38         const size_t height_;\r
39         const size_t stride_;\r
40         bool  empty_;\r
41 \r
42 public:\r
43         implementation(size_t width, size_t height, size_t stride) \r
44                 : width_(width)\r
45                 , height_(height)\r
46                 , stride_(stride)\r
47                 , empty_(true)\r
48         {       \r
49                 GL(glGenTextures(1, &id_));\r
50                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
51                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));\r
52                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));\r
53                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));\r
54                 GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));\r
55                 GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
56                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
57                 //CASPAR_LOG(trace) << "[device_buffer] allocated size:" << width*height*stride;        \r
58         }       \r
59 \r
60         ~implementation()\r
61         {\r
62                 try\r
63                 {\r
64                         GL(glDeleteTextures(1, &id_));\r
65                 }\r
66                 catch(...)\r
67                 {\r
68                         CASPAR_LOG_CURRENT_EXCEPTION();\r
69                 }\r
70         }\r
71         \r
72         void bind()\r
73         {\r
74                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
75         }\r
76 \r
77         void bind(int index)\r
78         {\r
79                 GL(glActiveTexture(GL_TEXTURE0+index));\r
80                 bind();\r
81         }\r
82 \r
83         void unbind()\r
84         {\r
85                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
86         }\r
87 \r
88         void read(host_buffer& source)\r
89         {\r
90                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
91                 source.unmap();\r
92                 source.bind();\r
93                 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
94                 source.unbind();\r
95                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
96         }\r
97         \r
98         void write(host_buffer& target)\r
99         {\r
100                 attach(0);\r
101                 GL(glBindTexture(GL_TEXTURE_2D, id_));\r
102                 target.unmap();\r
103                 target.bind();\r
104                 GL(glReadPixels(0, 0, width_, height_, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
105                 target.unbind();\r
106                 GL(glBindTexture(GL_TEXTURE_2D, 0));\r
107         }\r
108 \r
109         void attach(int index)\r
110         {\r
111                 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_2D, id_, 0));\r
112                 empty_ = false;\r
113         }\r
114 \r
115         void clear()\r
116         {\r
117                 attach(0);\r
118                 GL(glClear(GL_COLOR_BUFFER_BIT));\r
119                 empty_ = true;\r
120         }\r
121 };\r
122 \r
123 device_buffer::device_buffer(size_t width, size_t height, size_t stride) : impl_(new implementation(width, height, stride)){}\r
124 size_t device_buffer::stride() const { return impl_->stride_; }\r
125 size_t device_buffer::width() const { return impl_->width_; }\r
126 size_t device_buffer::height() const { return impl_->height_; }\r
127 void device_buffer::attach(int index){impl_->attach(index);}\r
128 void device_buffer::bind(){impl_->bind();}\r
129 void device_buffer::bind(int index){impl_->bind(index);}\r
130 void device_buffer::unbind(){impl_->unbind();}\r
131 void device_buffer::read(host_buffer& source){impl_->read(source);}\r
132 void device_buffer::write(host_buffer& target){impl_->write(target);}\r
133 void device_buffer::clear(){impl_->clear();}\r
134 bool device_buffer::empty() const { return impl_->empty_; }\r
135 \r
136 }}