2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
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
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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../stdafx.h"
\r
24 #include "device_buffer.h"
\r
25 #include "context.h"
\r
27 #include <common/except.h>
\r
28 #include <common/gl/gl_check.h>
\r
30 #include <gl/glew.h>
\r
32 #include <tbb/atomic.h>
\r
34 #include <boost/thread/future.hpp>
\r
36 namespace caspar { namespace accelerator { namespace ogl {
\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
42 unsigned int format(int stride)
\r
44 return FORMAT[stride];
\r
47 static tbb::atomic<int> g_total_count;
\r
49 struct device_buffer::impl : boost::noncopyable
\r
57 impl(int width, int height, int stride)
\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
75 glDeleteTextures(1, &id_);
\r
80 GL(glBindTexture(GL_TEXTURE_2D, id_));
\r
83 void bind(int index)
\r
85 GL(glActiveTexture(GL_TEXTURE0+index));
\r
91 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
96 GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, id_, 0));
\r
102 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
105 void copy_from(host_buffer& source)
\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
115 void copy_to(host_buffer& dest)
\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
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