/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright 2013 Sveriges Television AB http://casparcg.com/\r
*\r
-* This file is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\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
+* 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
+* 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
+* Author: Robert Nagy, ronag89@gmail.com\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
+#include <boost/property_tree/ptree.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
+ , attached_fbo_(0)\r
+ , active_shader_(0)\r
+ , read_buffer_(0)\r
{\r
+ CASPAR_LOG(info) << L"Initializing OpenGL Device.";\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 (glewInit() != GLEW_OK)\r
+ BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));\r
\r
- if(!GLEE_VERSION_3_0)\r
- BOOST_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 3.0 support."));\r
+ CASPAR_LOG(info) << L"OpenGL " << version();\r
\r
- GL(glGenFramebuffers(1, &fbo_)); \r
- GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));\r
- GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));\r
+ if(!GLEW_VERSION_3_0)\r
+ BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Your graphics card does not meet the minimum hardware requirements since it does not support OpenGL 3.0 or higher. CasparCG Server will not be able to continue."));\r
+ \r
+ glGenFramebuffers(1, &fbo_); \r
+ \r
+ CASPAR_LOG(info) << L"Successfully initialized OpenGL Device.";\r
});\r
}\r
\r
pool.clear();\r
BOOST_FOREACH(auto& pool, host_pools_)\r
pool.clear();\r
- glDeleteFramebuffersEXT(1, &fbo_);\r
+ glDeleteFramebuffers(1, &fbo_);\r
});\r
}\r
+\r
+safe_ptr<device_buffer> ogl_device::allocate_device_buffer(size_t width, size_t height, size_t stride, bool mipmapped)\r
+{\r
+ std::shared_ptr<device_buffer> buffer;\r
+ try\r
+ {\r
+ buffer.reset(new device_buffer(width, height, stride, mipmapped));\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, mipmapped));\r
+ }\r
+ catch(...)\r
+ {\r
+ CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";\r
+ throw;\r
+ }\r
+ }\r
+ return make_safe_ptr(buffer);\r
+}\r
\r
-safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)\r
+safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride, bool mipmapped)\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 + (mipmapped ? 4 : 0)][((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, mipmapped);}, 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
+ 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
- executor_.invoke([&]\r
+ try\r
{\r
- try\r
- {\r
- buffer = std::make_shared<device_buffer>(width, height, stride);\r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG(info) << "ogl: bad_alloc, retrying.";\r
- gc().wait();\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
+ yield();\r
+ gc().wait();\r
\r
- }\r
- }, high_priority); \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_ptr(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
- CASPAR_LOG(info) << "ogl: bad_alloc, retrying.";\r
- gc().wait();\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
-\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
+ auto self = shared_from_this();\r
+ return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*) mutable\r
{\r
- executor_.begin_invoke([=]\r
- {\r
+ self->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
+safe_ptr<ogl_device> ogl_device::create()\r
+{\r
+ return safe_ptr<ogl_device>(new ogl_device());\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
-boost::unique_future<void> ogl_device::gc()\r
+boost::property_tree::wptree ogl_device::info() const\r
{\r
- CASPAR_LOG(info) << " ogl: Running GC.";\r
+ boost::property_tree::wptree info;\r
+\r
+ boost::property_tree::wptree pooled_device_buffers;\r
+ size_t total_pooled_device_buffer_size = 0;\r
+ size_t total_pooled_device_buffer_count = 0;\r
+\r
+ for (size_t i = 0; i < device_pools_.size(); ++i)\r
+ {\r
+ auto& pools = device_pools_.at(i);\r
+ bool mipmapping = i > 3;\r
+ int stride = mipmapping ? i - 3 : i + 1;\r
+\r
+ BOOST_FOREACH(auto& pool, pools)\r
+ {\r
+ auto width = pool.first >> 16;\r
+ auto height = pool.first & 0x0000FFFF;\r
+ auto size = width * height * stride;\r
+ auto count = pool.second->items.size();\r
+\r
+ if (count == 0)\r
+ continue;\r
+\r
+ boost::property_tree::wptree pool_info;\r
+\r
+ pool_info.add(L"stride", stride);\r
+ pool_info.add(L"mipmapping", mipmapping);\r
+ pool_info.add(L"width", width);\r
+ pool_info.add(L"height", height);\r
+ pool_info.add(L"size", size);\r
+ pool_info.add(L"count", count);\r
+\r
+ total_pooled_device_buffer_size += size * count;\r
+ total_pooled_device_buffer_count += count;\r
+\r
+ pooled_device_buffers.add_child(L"device_buffer_pool", pool_info);\r
+ }\r
+ }\r
+\r
+ info.add_child(L"gl.details.pooled_device_buffers", pooled_device_buffers);\r
+\r
+ boost::property_tree::wptree pooled_host_buffers;\r
+ size_t total_read_size = 0;\r
+ size_t total_write_size = 0;\r
+ size_t total_read_count = 0;\r
+ size_t total_write_count = 0;\r
+\r
+ for (size_t i = 0; i < host_pools_.size(); ++i)\r
+ {\r
+ auto& pools = host_pools_.at(i);\r
+ host_buffer::usage_t usage = static_cast<host_buffer::usage_t>(i);\r
+\r
+ BOOST_FOREACH(auto& pool, pools)\r
+ {\r
+ auto size = pool.first;\r
+ auto count = pool.second->items.size();\r
+\r
+ if (count == 0)\r
+ continue;\r
+\r
+ boost::property_tree::wptree pool_info;\r
+\r
+ pool_info.add(L"usage", usage == host_buffer::read_only\r
+ ? L"read_only" : L"write_only");\r
+ pool_info.add(L"size", size);\r
+ pool_info.add(L"count", count);\r
\r
+ pooled_host_buffers.add_child(L"host_buffer_pool", pool_info);\r
+\r
+ (usage == host_buffer::read_only\r
+ ? total_read_count : total_write_count) += count;\r
+ (usage == host_buffer::read_only\r
+ ? total_read_size : total_write_size) += size * count;\r
+ }\r
+ }\r
+\r
+ info.add_child(L"gl.details.pooled_host_buffers", pooled_host_buffers);\r
+\r
+ info.add(L"gl.summary.pooled_device_buffers.total_count", total_pooled_device_buffer_count);\r
+ info.add(L"gl.summary.pooled_device_buffers.total_size", total_pooled_device_buffer_size);\r
+ info.add_child(L"gl.summary.all_device_buffers", device_buffer::info());\r
+ info.add(L"gl.summary.pooled_host_buffers.total_read_count", total_read_count);\r
+ info.add(L"gl.summary.pooled_host_buffers.total_write_count", total_write_count);\r
+ info.add(L"gl.summary.pooled_host_buffers.total_read_size", total_read_size);\r
+ info.add(L"gl.summary.pooled_host_buffers.total_write_size", total_write_size);\r
+ info.add_child(L"gl.summary.all_host_buffers", host_buffer::info());\r
+\r
+ return info;\r
+}\r
+\r
+boost::unique_future<void> ogl_device::gc()\r
+{ \r
return 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
+ {\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
+std::wstring ogl_device::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
+ ver = widen(invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})\r
+ + " " + 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
+ if(attached_fbo_ != fbo_)\r
+ {\r
+ glBindFramebuffer(GL_FRAMEBUFFER, fbo_);\r
+ attached_fbo_ = fbo_;\r
+ }\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::read_buffer(device_buffer&)\r
+{\r
+ if(read_buffer_ != GL_COLOR_ATTACHMENT0)\r
+ {\r
+ GL(glReadBuffer(GL_COLOR_ATTACHMENT0));\r
+ read_buffer_ = GL_COLOR_ATTACHMENT0;\r
+ }\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