]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/host_buffer.cpp
2.0. - Added GPU fences for read-back which will avoid blocking the rendering thread...
[casparcg] / core / mixer / gpu / host_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 "../gpu/host_buffer.h"\r
23 \r
24 #include <common/gl/gl_check.h>\r
25 \r
26 namespace caspar { namespace core {\r
27                                                                                                                                                                                                                                                                                                                                 \r
28 struct host_buffer::implementation : boost::noncopyable\r
29 {       \r
30         GLuint pbo_;\r
31         GLuint fence_;\r
32 \r
33         const size_t size_;\r
34 \r
35         void* data_;\r
36         GLenum usage_;\r
37         GLenum target_;\r
38 \r
39 public:\r
40         implementation(size_t size, usage_t usage) \r
41                 : size_(size)\r
42                 , data_(nullptr)\r
43                 , pbo_(0)\r
44                 , target_(usage == write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)\r
45                 , usage_(usage == write_only ? GL_STREAM_DRAW : GL_STREAM_READ)\r
46                 , fence_(0)\r
47         {\r
48                 GL(glGenBuffers(1, &pbo_));\r
49                 GL(glBindBuffer(target_, pbo_));\r
50                 if(usage_ != write_only)        \r
51                         GL(glBufferData(target_, size_, NULL, usage_)); \r
52                 GL(glBindBuffer(target_, 0));\r
53 \r
54                 if(!pbo_)\r
55                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));\r
56 \r
57                 CASPAR_LOG(debug) << "[host_buffer] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");\r
58         }       \r
59 \r
60         ~implementation()\r
61         {\r
62                 try\r
63                 {\r
64                         if(fence_)\r
65                                 glDeleteFencesNV(1, &fence_);\r
66 \r
67                         GL(glDeleteBuffers(1, &pbo_));\r
68                 }\r
69                 catch(...)\r
70                 {\r
71                         CASPAR_LOG_CURRENT_EXCEPTION();\r
72                 }\r
73         }\r
74 \r
75         void map()\r
76         {\r
77                 if(data_)\r
78                         return;\r
79 \r
80                 if(usage_ == write_only)                        \r
81                         GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.\r
82                 \r
83                 GL(glBindBuffer(target_, pbo_));\r
84                 data_ = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));  \r
85                 GL(glBindBuffer(target_, 0)); \r
86                 if(!data_)\r
87                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));\r
88         }\r
89 \r
90         void unmap()\r
91         {\r
92                 if(!data_)\r
93                         return;\r
94                 \r
95                 GL(glBindBuffer(target_, pbo_));\r
96                 GL(glUnmapBuffer(target_));     \r
97                 data_ = nullptr;                \r
98                 GL(glBindBuffer(target_, 0));\r
99         }\r
100 \r
101         void bind()\r
102         {\r
103                 GL(glBindBuffer(target_, pbo_));\r
104         }\r
105 \r
106         void unbind()\r
107         {\r
108                 GL(glBindBuffer(target_, 0));\r
109         }\r
110         \r
111         void fence_set()\r
112         {\r
113                 if(fence_)\r
114                         glDeleteFencesNV(1, &fence_);\r
115                         \r
116                 GL(glGenFencesNV(1, &fence_));\r
117                 GL(glSetFenceNV(fence_, GL_ALL_COMPLETED_NV));\r
118         }\r
119 \r
120         bool fence_rdy() const\r
121         {\r
122                 return GL2(glTestFenceNV(fence_)) != GL_FALSE;\r
123         }\r
124 };\r
125 \r
126 host_buffer::host_buffer(size_t size, usage_t usage) : impl_(new implementation(size, usage)){}\r
127 const void* host_buffer::data() const {return impl_->data_;}\r
128 void* host_buffer::data() {return impl_->data_;}\r
129 void host_buffer::map(){impl_->map();}\r
130 void host_buffer::unmap(){impl_->unmap();}\r
131 void host_buffer::bind(){impl_->bind();}\r
132 void host_buffer::unbind(){impl_->unbind();}\r
133 void host_buffer::fence_set(){impl_->fence_set();}\r
134 bool host_buffer::fence_rdy() const{return impl_->fence_rdy();}\r
135 \r
136 size_t host_buffer::size() const { return impl_->size_; }\r
137 \r
138 }}