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