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