]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/buffer.cpp
6b20ad034fe062736377314de3fd1ac98a87fe5d
[casparcg] / accelerator / ogl / util / buffer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "buffer.h"
25
26 #include "texture.h"
27 #include "device.h"
28
29 #include <common/except.h>
30 #include <common/gl/gl_check.h>
31
32 #include <GL/glew.h>
33
34 #include <tbb/atomic.h>
35
36 namespace caspar { namespace accelerator { namespace ogl {
37
38 static tbb::atomic<int> g_w_total_count;
39 static tbb::atomic<int> g_r_total_count;
40                                                                                                                                                                                                                                                                                                                                 
41 struct buffer::impl : boost::noncopyable
42 {       
43         GLuint                                          pbo_;
44         const std::size_t                       size_;
45         tbb::atomic<uint8_t*>           data_;
46         GLenum                                          usage_;
47         GLenum                                          target_;
48
49 public:
50         impl(std::size_t size, buffer::usage usage) 
51                 : size_(size)
52                 , target_(usage == buffer::usage::write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)
53                 , usage_(usage == buffer::usage::write_only ? GL_STREAM_DRAW : GL_STREAM_READ)
54         {
55                 boost::timer timer;
56
57                 data_ = nullptr;
58                 GL(glGenBuffers(1, &pbo_));
59                 bind(); 
60                 GL(glBufferData(target_, size_, NULL, usage_));         
61                 if(usage_ == GL_STREAM_DRAW)
62                 {
63                         auto result = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
64                         data_ = reinterpret_cast<uint8_t*>(result);
65                 }
66                 unbind();
67
68                 if(!pbo_)
69                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));
70                 
71                 if(timer.elapsed() > 0.02)
72                         CASPAR_LOG(debug) << L"[buffer] Performance warning. Buffer allocation blocked: " << timer.elapsed();
73         
74                 //CASPAR_LOG(trace) << "[buffer] [" << ++(usage_ == buffer::usage::write_only ? g_w_total_count : g_r_total_count) << L"] allocated size:" << size_ << " usage: " << (usage == buffer::usage::write_only ? "write_only" : "read_only");
75         }       
76
77         ~impl()
78         {
79                 glDeleteBuffers(1, &pbo_);
80         }
81
82         void* map()
83         {
84                 if(data_ != nullptr)
85                         return data_;
86                 
87                 boost::timer timer;
88
89                 GL(glBindBuffer(target_, pbo_));
90                 if(usage_ == GL_STREAM_DRAW)                    
91                         GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
92                 
93                 auto result = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
94                 data_ = (uint8_t*) result;
95
96                 if(timer.elapsed() > 0.02)
97                         CASPAR_LOG(debug) << L"[buffer] Performance warning. Buffer mapping blocked: " << timer.elapsed();
98
99                 GL(glBindBuffer(target_, 0));
100                 if(!data_)
101                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target OpenGL Pixel Buffer Object."));
102
103                 return data_;
104         }
105         
106         void unmap()
107         {
108                 if(data_ == nullptr)
109                         return;
110                 
111                 GL(glBindBuffer(target_, pbo_));
112                 GL(glUnmapBuffer(target_));     
113                 if(usage_ == GL_STREAM_READ)                    
114                         GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
115                 data_ = nullptr;        
116                 GL(glBindBuffer(target_, 0));
117         }
118
119         void bind()
120         {
121                 GL(glBindBuffer(target_, pbo_));
122         }
123
124         void unbind()
125         {
126                 GL(glBindBuffer(target_, 0));
127         }
128 };
129
130 buffer::buffer(std::size_t size, usage usage) : impl_(new impl(size, usage)){}
131 buffer::buffer(buffer&& other) : impl_(std::move(other.impl_)){}
132 buffer::~buffer(){}
133 buffer& buffer::operator=(buffer&& other){impl_ = std::move(other.impl_); return *this;}
134 uint8_t* buffer::data(){return impl_->data_;}
135 void buffer::map(){impl_->map();}
136 void buffer::unmap(){impl_->unmap();}
137 void buffer::bind() const{impl_->bind();}
138 void buffer::unbind() const{impl_->unbind();}
139 std::size_t buffer::size() const { return impl_->size_; }
140 int buffer::id() const {return impl_->pbo_;}
141
142 }}}