]> git.sesse.net Git - casparcg/blobdiff - core/mixer/gpu/ogl_device.cpp
2.0. Fixed rendering stalls caused by ogl frame allocation delays.
[casparcg] / core / mixer / gpu / ogl_device.cpp
index 2431d1dabea826e0137998e6c42b4dd94d4f645d..9f83317d91f2645e33e13530e6c354d93c549ee5 100644 (file)
 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
 *\r
 */\r
+// TODO: Smart GC\r
+\r
 #include "../../stdafx.h"\r
 \r
 #include "ogl_device.h"\r
 \r
+#include "shader.h"\r
+\r
+#include <common/exception/exceptions.h>\r
 #include <common/utility/assert.h>\r
 #include <common/gl/gl_check.h>\r
 \r
 #include <boost/foreach.hpp>\r
 \r
+#include <gl/glew.h>\r
+\r
 namespace caspar { namespace core {\r
 \r
-ogl_device::ogl_device() : executor_(L"ogl_device")\r
+ogl_device::ogl_device() \r
+       : executor_(L"ogl_device")\r
+       , pattern_(nullptr)\r
+       , attached_texture_(0)\r
+       , active_shader_(0)\r
 {\r
+       std::fill(binded_textures_.begin(), binded_textures_.end(), 0);\r
+       std::fill(viewport_.begin(), viewport_.end(), 0);\r
+       std::fill(scissor_.begin(), scissor_.end(), 0);\r
+       std::fill(blend_func_.begin(), blend_func_.end(), 0);\r
+       \r
        invoke([=]\r
        {\r
                context_.reset(new sf::Context());\r
                context_->SetActive(true);\r
-                                               \r
-               if(!GLEE_VERSION_3_0)\r
-                       BOOST_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 3.0 support."));\r
-\r
+               \r
+               if (glewInit() != GLEW_OK)\r
+                       BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));\r
+                               \r
+               if(!GLEW_VERSION_3_2)\r
+                       CASPAR_LOG(warning) << "Missing OpenGL 3.2 support.";\r
+       \r
                GL(glGenFramebuffers(1, &fbo_));                \r
                GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));\r
                GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));\r
+        GL(glDisable(GL_MULTISAMPLE_ARB));\r
        });\r
 }\r
 \r
@@ -55,100 +75,288 @@ ogl_device::~ogl_device()
                glDeleteFramebuffersEXT(1, &fbo_);\r
        });\r
 }\r
+\r
+safe_ptr<device_buffer> ogl_device::allocate_device_buffer(size_t width, size_t height, size_t stride)\r
+{\r
+       std::shared_ptr<device_buffer> buffer;\r
+       try\r
+       {\r
+               buffer.reset(new device_buffer(width, height, stride));\r
+       }\r
+       catch(...)\r
+       {\r
+               try\r
+               {\r
+                       yield();\r
+                       gc().wait();\r
+                                       \r
+                       // Try again\r
+                       buffer.reset(new device_buffer(width, height, stride));\r
+               }\r
+               catch(...)\r
+               {\r
+                       CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";\r
+                       throw;\r
+               }\r
+       }\r
+       return make_safe(buffer);\r
+}\r
                                \r
 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)\r
 {\r
        CASPAR_VERIFY(stride > 0 && stride < 5);\r
        CASPAR_VERIFY(width > 0 && height > 0);\r
-       auto pool = &device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
+       auto& pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
        std::shared_ptr<device_buffer> buffer;\r
-       if(!pool->try_pop(buffer))              \r
+       if(!pool->items.try_pop(buffer))                \r
+               buffer = executor_.invoke([&]{return allocate_device_buffer(width, height, stride);}, high_priority);                   \r
+       \r
+       //++pool->usage_count;\r
+\r
+       return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*) mutable\r
+       {               \r
+               pool->items.push(buffer);       \r
+       });\r
+}\r
+\r
+safe_ptr<host_buffer> ogl_device::allocate_host_buffer(size_t size, host_buffer::usage_t usage)\r
+{\r
+       std::shared_ptr<host_buffer> buffer;\r
+\r
+       try\r
        {\r
-               executor_.invoke([&]\r
+               buffer.reset(new host_buffer(size, usage));\r
+               if(usage == host_buffer::write_only)\r
+                       buffer->map();\r
+               else\r
+                       buffer->unmap();                        \r
+       }\r
+       catch(...)\r
+       {\r
+               try\r
                {\r
-                       try\r
-                       {\r
-                               buffer = std::make_shared<device_buffer>(width, height, stride);\r
-                       }\r
-                       catch(...)\r
-                       {\r
-                               BOOST_THROW_EXCEPTION(bad_alloc()\r
-                                       << errinfo_nested_exception(std::current_exception()));\r
-                       }\r
-               }, high_priority);      \r
+                       yield();\r
+                       gc().wait();\r
+\r
+                       // Try again\r
+                       buffer.reset(new host_buffer(size, usage));\r
+                       if(usage == host_buffer::write_only)\r
+                               buffer->map();\r
+                       else\r
+                               buffer->unmap();        \r
+               }\r
+               catch(...)\r
+               {\r
+                       CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";\r
+                       throw;          \r
+               }\r
        }\r
-                       \r
-       return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*){pool->push(buffer);});\r
+\r
+       return make_safe(buffer);\r
 }\r
        \r
 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)\r
 {\r
        CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);\r
        CASPAR_VERIFY(size > 0);\r
-       auto pool = &host_pools_[usage][size];\r
+       auto& pool = host_pools_[usage][size];\r
        std::shared_ptr<host_buffer> buffer;\r
-       if(!pool->try_pop(buffer))\r
-       {\r
-               executor_.invoke([&]\r
-               {\r
-                       try\r
-                       {\r
-                               buffer = std::make_shared<host_buffer>(size, usage);\r
-                               if(usage == host_buffer::write_only)\r
-                                       buffer->map();\r
-                               else\r
-                                       buffer->unmap();                        \r
-                       }\r
-                       catch(...)\r
-                       {\r
-                               BOOST_THROW_EXCEPTION(bad_alloc()\r
-                                       << errinfo_nested_exception(std::current_exception()));\r
-                       }\r
-\r
-               }, high_priority);      \r
-       }\r
+       if(!pool->items.try_pop(buffer))        \r
+               buffer = executor_.invoke([=]{return allocate_host_buffer(size, usage);}, high_priority);       \r
        \r
-       return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)\r
+       //++pool->usage_count;\r
+\r
+       return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable\r
        {\r
-               executor_.begin_invoke([=]\r
-               {\r
+               executor_.begin_invoke([=]() mutable\r
+               {               \r
                        if(usage == host_buffer::write_only)\r
                                buffer->map();\r
                        else\r
                                buffer->unmap();\r
-                       \r
-                       pool->push(buffer);\r
 \r
-               }, high_priority);\r
+                       pool->items.push(buffer);\r
+               }, high_priority);      \r
        });\r
 }\r
 \r
+//template<typename T>\r
+//void flush_pool(buffer_pool<T>& pool)\r
+//{    \r
+//     if(pool.flush_count.fetch_and_increment() < 16)\r
+//             return;\r
+//\r
+//     if(pool.usage_count.fetch_and_store(0) < pool.items.size())\r
+//     {\r
+//             std::shared_ptr<T> buffer;\r
+//             pool.items.try_pop(buffer);\r
+//     }\r
+//\r
+//     pool.flush_count = 0;\r
+//     pool.usage_count = 0;\r
+//}\r
+\r
+void ogl_device::flush()\r
+{\r
+       GL(glFlush());  \r
+               \r
+       //try\r
+       //{\r
+       //      BOOST_FOREACH(auto& pools, device_pools_)\r
+       //      {\r
+       //              BOOST_FOREACH(auto& pool, pools)\r
+       //                      flush_pool(*pool.second);\r
+       //      }\r
+       //      BOOST_FOREACH(auto& pools, host_pools_)\r
+       //      {\r
+       //              BOOST_FOREACH(auto& pool, pools)\r
+       //                      flush_pool(*pool.second);\r
+       //      }\r
+       //}\r
+       //catch(...)\r
+       //{\r
+       //      CASPAR_LOG_CURRENT_EXCEPTION();\r
+       //}\r
+}\r
+\r
 void ogl_device::yield()\r
 {\r
        executor_.yield();\r
 }\r
 \r
-void ogl_device::gc()\r
-{\r
-       //begin_invoke([=]\r
-       //{             \r
-       //      BOOST_FOREACH(auto& pool, device_pools_)\r
-       //              pool.clear();\r
-       //      BOOST_FOREACH(auto& pool, host_pools_)\r
-       //              pool.clear();\r
-       //});\r
+boost::unique_future<void> ogl_device::gc()\r
+{      \r
+       return begin_invoke([=]\r
+       {\r
+               CASPAR_LOG(info) << " ogl: Running GC.";                \r
+       \r
+               try\r
+               {\r
+                       BOOST_FOREACH(auto& pools, device_pools_)\r
+                       {\r
+                               BOOST_FOREACH(auto& pool, pools)\r
+                                       pool.second->items.clear();\r
+                       }\r
+                       BOOST_FOREACH(auto& pools, host_pools_)\r
+                       {\r
+                               BOOST_FOREACH(auto& pool, pools)\r
+                                       pool.second->items.clear();\r
+                       }\r
+               }\r
+               catch(...)\r
+               {\r
+                       CASPAR_LOG_CURRENT_EXCEPTION();\r
+               }\r
+       }, high_priority);\r
 }\r
 \r
 std::wstring ogl_device::get_version()\r
 {      \r
-       static std::wstring ver;\r
-       if(ver.empty())\r
+       static std::wstring ver = L"Not found";\r
+       try\r
        {\r
                ogl_device tmp;\r
                ver = widen(tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})\r
-               + " "   + tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));  \r
+               + " "   + tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));                  \r
        }\r
+       catch(...){}\r
+\r
        return ver;\r
 }\r
 \r
-}}
\ No newline at end of file
+\r
+void ogl_device::enable(GLenum cap)\r
+{\r
+       auto& val = caps_[cap];\r
+       if(!val)\r
+       {\r
+               glEnable(cap);\r
+               val = true;\r
+       }\r
+}\r
+\r
+void ogl_device::disable(GLenum cap)\r
+{\r
+       auto& val = caps_[cap];\r
+       if(val)\r
+       {\r
+               glDisable(cap);\r
+               val = false;\r
+       }\r
+}\r
+\r
+void ogl_device::viewport(size_t x, size_t y, size_t width, size_t height)\r
+{\r
+       if(x != viewport_[0] || y != viewport_[1] || width != viewport_[2] || height != viewport_[3])\r
+       {               \r
+               glViewport(x, y, width, height);\r
+               viewport_[0] = x;\r
+               viewport_[1] = y;\r
+               viewport_[2] = width;\r
+               viewport_[3] = height;\r
+       }\r
+}\r
+\r
+void ogl_device::scissor(size_t x, size_t y, size_t width, size_t height)\r
+{\r
+       if(x != scissor_[0] || y != scissor_[1] || width != scissor_[2] || height != scissor_[3])\r
+       {               \r
+               glScissor(x, y, width, height);\r
+               scissor_[0] = x;\r
+               scissor_[1] = y;\r
+               scissor_[2] = width;\r
+               scissor_[3] = height;\r
+       }\r
+}\r
+\r
+void ogl_device::stipple_pattern(const GLubyte* pattern)\r
+{\r
+       if(pattern_ != pattern)\r
+       {               \r
+               glPolygonStipple(pattern);\r
+               pattern_ = pattern;\r
+       }\r
+}\r
+\r
+void ogl_device::attach(device_buffer& texture)\r
+{      \r
+       if(attached_texture_ != texture.id())\r
+       {\r
+               GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, texture.id(), 0));\r
+               attached_texture_ = texture.id();\r
+       }\r
+}\r
+\r
+void ogl_device::clear(device_buffer& texture)\r
+{      \r
+       attach(texture);\r
+       GL(glClear(GL_COLOR_BUFFER_BIT));\r
+}\r
+\r
+void ogl_device::use(shader& shader)\r
+{\r
+       if(active_shader_ != shader.id())\r
+       {               \r
+               GL(glUseProgramObjectARB(shader.id())); \r
+               active_shader_ = shader.id();\r
+       }\r
+}\r
+\r
+void ogl_device::blend_func(int c1, int c2, int a1, int a2)\r
+{\r
+       std::array<int, 4> func = {c1, c2, a1, a2};\r
+\r
+       if(blend_func_ != func)\r
+       {\r
+               blend_func_ = func;\r
+               GL(glBlendFuncSeparate(c1, c2, a1, a2));\r
+       }\r
+}\r
+\r
+void ogl_device::blend_func(int c1, int c2)\r
+{\r
+       blend_func(c1, c2, c1, c2);\r
+}\r
+\r
+}}\r
+\r