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