]> git.sesse.net Git - casparcg/blobdiff - core/mixer/gpu/host_buffer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / mixer / gpu / host_buffer.cpp
index 943014bc6a675decb7d3868da0b4cbecf456043e..ec65338668de59c759c75fa973ce3ce19ee1a036 100644 (file)
 */\r
 #include "../../stdafx.h"\r
 \r
-#include "../gpu/host_buffer.h"\r
+#include "host_buffer.h"\r
 \r
+#include "fence.h"\r
+#include "device_buffer.h"\r
+#include "ogl_device.h"\r
+\r
+#include <common/exception/exceptions.h>\r
 #include <common/gl/gl_check.h>\r
 \r
+#include <gl/glew.h>\r
+\r
+#include <tbb/atomic.h>\r
+\r
 namespace caspar { namespace core {\r
+\r
+static tbb::atomic<int> g_w_total_count;\r
+static tbb::atomic<int> g_r_total_count;\r
                                                                                                                                                                                                                                                                                                                                \r
 struct host_buffer::implementation : boost::noncopyable\r
 {      \r
-       GLuint pbo_;\r
-       GLuint fence_;\r
-\r
-       const size_t size_;\r
-\r
-       void* data_;\r
-       GLenum usage_;\r
-       GLenum target_;\r
+       GLuint                  pbo_;\r
+       const size_t    size_;\r
+       void*                   data_;\r
+       GLenum                  usage_;\r
+       GLenum                  target_;\r
+       fence                   fence_;\r
 \r
 public:\r
        implementation(size_t size, usage_t usage) \r
@@ -43,7 +53,6 @@ public:
                , pbo_(0)\r
                , target_(usage == write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)\r
                , usage_(usage == write_only ? GL_STREAM_DRAW : GL_STREAM_READ)\r
-               , fence_(0)\r
        {\r
                GL(glGenBuffers(1, &pbo_));\r
                GL(glBindBuffer(target_, pbo_));\r
@@ -54,17 +63,15 @@ public:
                if(!pbo_)\r
                        BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));\r
 \r
-               CASPAR_LOG(debug) << "[host_buffer] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");\r
+               CASPAR_LOG(trace) << "[host_buffer] [" << ++(usage_ == write_only ? g_w_total_count : g_r_total_count) << L"] allocated size:" << size_ << " usage: " << (usage == write_only ? "write_only" : "read_only");\r
        }       \r
 \r
        ~implementation()\r
        {\r
                try\r
                {\r
-                       if(fence_)\r
-                               glDeleteFencesNV(1, &fence_);\r
-\r
                        GL(glDeleteBuffers(1, &pbo_));\r
+                       //CASPAR_LOG(trace) << "[host_buffer] [" << --(usage_ == write_only ? g_w_total_count : g_r_total_count) << L"] deallocated size:" << size_ << " usage: " << (usage_ == write_only ? "write_only" : "read_only");\r
                }\r
                catch(...)\r
                {\r
@@ -87,6 +94,11 @@ public:
                        BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));\r
        }\r
 \r
+       void wait(ogl_device& ogl)\r
+       {\r
+               fence_.wait(ogl);\r
+       }\r
+\r
        void unmap()\r
        {\r
                if(!data_)\r
@@ -107,19 +119,19 @@ public:
        {\r
                GL(glBindBuffer(target_, 0));\r
        }\r
-       \r
-       void fence_set()\r
+\r
+       void begin_read(size_t width, size_t height, GLuint format)\r
        {\r
-               if(fence_)\r
-                       glDeleteFencesNV(1, &fence_);\r
-                       \r
-               GL(glGenFencesNV(1, &fence_));\r
-               GL(glSetFenceNV(fence_, GL_ALL_COMPLETED_NV));\r
+               unmap();\r
+               bind();\r
+               GL(glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, NULL));\r
+               unbind();\r
+               fence_.set();\r
        }\r
 \r
-       bool fence_rdy() const\r
+       bool ready() const\r
        {\r
-               return GL2(glTestFenceNV(fence_)) != GL_FALSE;\r
+               return fence_.ready();\r
        }\r
 };\r
 \r
@@ -130,9 +142,9 @@ void host_buffer::map(){impl_->map();}
 void host_buffer::unmap(){impl_->unmap();}\r
 void host_buffer::bind(){impl_->bind();}\r
 void host_buffer::unbind(){impl_->unbind();}\r
-void host_buffer::fence_set(){impl_->fence_set();}\r
-bool host_buffer::fence_rdy() const{return impl_->fence_rdy();}\r
-\r
+void host_buffer::begin_read(size_t width, size_t height, GLuint format){impl_->begin_read(width, height, format);}\r
 size_t host_buffer::size() const { return impl_->size_; }\r
+bool host_buffer::ready() const{return impl_->ready();}\r
+void host_buffer::wait(ogl_device& ogl){impl_->wait(ogl);}\r
 \r
 }}
\ No newline at end of file