]> git.sesse.net Git - casparcg/blobdiff - core/mixer/gpu/device_buffer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / mixer / gpu / device_buffer.cpp
index 7755274dd6d4301800c8dc61e29e2275d3f27f6a..4eaf6e3e4a1d500f4eed8f1b64aa5a5533b41f8f 100644 (file)
@@ -1,13 +1,42 @@
-#include "../../StdAfx.h"\r
+/*\r
+* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+*\r
+*  This file is part of CasparCG.\r
+*\r
+*    CasparCG is free software: you can redistribute it and/or modify\r
+*    it under the terms of the GNU General Public License as published by\r
+*    the Free Software Foundation, either version 3 of the License, or\r
+*    (at your option) any later version.\r
+*\r
+*    CasparCG is distributed in the hope that it will be useful,\r
+*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+*    GNU General Public License for more details.\r
+\r
+*    You should have received a copy of the GNU General Public License\r
+*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
+*\r
+*/\r
+#include "../../stdafx.h"\r
 \r
 #include "device_buffer.h"\r
 \r
+#include "fence.h"\r
+\r
+#include <common/exception/exceptions.h>\r
 #include <common/gl/gl_check.h>\r
 \r
+#include <gl/glew.h>\r
+\r
 namespace caspar { namespace core {\r
        \r
-GLenum FORMAT[] = {0, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_BGR, GL_BGRA};\r
-GLenum INTERNAL_FORMAT[] = {0, GL_LUMINANCE8, GL_LUMINANCE8_ALPHA8, GL_RGB8, GL_RGBA8};        \r
+static GLenum FORMAT[] = {0, GL_RED, GL_RG, GL_BGR, GL_BGRA};\r
+static GLenum INTERNAL_FORMAT[] = {0, GL_R8, GL_RG8, GL_RGB8, GL_RGBA8};       \r
+\r
+unsigned int format(size_t stride)\r
+{\r
+       return FORMAT[stride];\r
+}\r
 \r
 struct device_buffer::implementation : boost::noncopyable\r
 {\r
@@ -17,6 +46,8 @@ struct device_buffer::implementation : boost::noncopyable
        const size_t height_;\r
        const size_t stride_;\r
 \r
+       fence            fence_;\r
+\r
 public:\r
        implementation(size_t width, size_t height, size_t stride) \r
                : width_(width)\r
@@ -30,12 +61,20 @@ public:
                GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));\r
                GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));\r
                GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
-               GL(glBindTexture(GL_TEXTURE_2D, 0));    \r
+               GL(glBindTexture(GL_TEXTURE_2D, 0));\r
+               CASPAR_LOG(debug) << "[device_buffer] allocated size:" << width*height*stride;  \r
        }       \r
 \r
        ~implementation()\r
        {\r
-               GL(glDeleteTextures(1, &id_));\r
+               try\r
+               {\r
+                       GL(glDeleteTextures(1, &id_));\r
+               }\r
+               catch(...)\r
+               {\r
+                       CASPAR_LOG_CURRENT_EXCEPTION();\r
+               }\r
        }\r
        \r
        void bind()\r
@@ -43,34 +82,28 @@ public:
                GL(glBindTexture(GL_TEXTURE_2D, id_));\r
        }\r
 \r
+       void bind(int index)\r
+       {\r
+               GL(glActiveTexture(GL_TEXTURE0+index));\r
+               bind();\r
+       }\r
+\r
        void unbind()\r
        {\r
                GL(glBindTexture(GL_TEXTURE_2D, 0));\r
        }\r
 \r
-       void read(host_buffer& source)\r
+       void begin_read()\r
        {\r
-               GL(glBindTexture(GL_TEXTURE_2D, id_));\r
-               source.unmap();\r
-               source.bind();\r
+               bind();\r
                GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
-               source.unbind();\r
-               GL(glBindTexture(GL_TEXTURE_2D, 0));\r
+               unbind();\r
+               fence_.set();\r
        }\r
        \r
-       void write(host_buffer& target)\r
+       bool ready() const\r
        {\r
-               GL(glBindTexture(GL_TEXTURE_2D, id_));\r
-               target.unmap();\r
-               target.bind();\r
-               GL(glReadPixels(0, 0, width_, height_, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
-               target.unbind();\r
-               GL(glBindTexture(GL_TEXTURE_2D, 0));\r
-       }\r
-\r
-       void attach(int index)\r
-       {\r
-               GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_2D, id_, 0));\r
+               return fence_.ready();\r
        }\r
 };\r
 \r
@@ -78,10 +111,11 @@ device_buffer::device_buffer(size_t width, size_t height, size_t stride) : impl_
 size_t device_buffer::stride() const { return impl_->stride_; }\r
 size_t device_buffer::width() const { return impl_->width_; }\r
 size_t device_buffer::height() const { return impl_->height_; }\r
-void device_buffer::attach(int index){impl_->attach(index);}\r
-void device_buffer::bind(){impl_->bind();}\r
+void device_buffer::bind(int index){impl_->bind(index);}\r
 void device_buffer::unbind(){impl_->unbind();}\r
-void device_buffer::read(host_buffer& source){impl_->read(source);}\r
-void device_buffer::write(host_buffer& target){impl_->write(target);}\r
+void device_buffer::begin_read(){impl_->begin_read();}\r
+bool device_buffer::ready() const{return impl_->ready();}\r
+int device_buffer::id() const{ return impl_->id_;}\r
+\r
 \r
 }}
\ No newline at end of file