]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/ogl_device.h
2.0. Fixed rendering stalls caused by ogl frame allocation delays.
[casparcg] / core / mixer / gpu / ogl_device.h
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\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
10 *\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
15 \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
18 *\r
19 */\r
20 #pragma once\r
21 \r
22 #include "host_buffer.h"\r
23 #include "device_buffer.h"\r
24 \r
25 #include <common/concurrency/executor.h>\r
26 #include <common/memory/safe_ptr.h>\r
27 \r
28 #include <tbb/concurrent_unordered_map.h>\r
29 #include <tbb/concurrent_queue.h>\r
30 \r
31 #include <boost/noncopyable.hpp>\r
32 #include <boost/thread/future.hpp>\r
33 \r
34 #include <array>\r
35 #include <unordered_map>\r
36 \r
37 #include <gl/glew.h>\r
38 \r
39 #include "../../dependencies\SFML-1.6\include\SFML/Window/Context.hpp"\r
40 \r
41 namespace caspar { namespace core {\r
42 \r
43 class shader;\r
44 \r
45 template<typename T>\r
46 struct buffer_pool\r
47 {\r
48         tbb::atomic<int> usage_count;\r
49         tbb::atomic<int> flush_count;\r
50         tbb::concurrent_bounded_queue<std::shared_ptr<T>> items;\r
51 \r
52         buffer_pool()\r
53         {\r
54                 usage_count = 0;\r
55                 flush_count = 0;\r
56         }\r
57 };\r
58 \r
59 class ogl_device : boost::noncopyable\r
60 {       \r
61         std::unordered_map<GLenum, bool> caps_;\r
62         std::array<size_t, 4>                    viewport_;\r
63         std::array<size_t, 4>                    scissor_;\r
64         const GLubyte*                                   pattern_;\r
65         GLint                                                    attached_texture_;\r
66         GLint                                                    active_shader_;\r
67         std::array<GLint, 16>                    binded_textures_;\r
68         std::array<GLint, 4>                     blend_func_;\r
69 \r
70         std::unique_ptr<sf::Context> context_;\r
71         \r
72         std::array<tbb::concurrent_unordered_map<size_t, safe_ptr<buffer_pool<device_buffer>>>, 4> device_pools_;\r
73         std::array<tbb::concurrent_unordered_map<size_t, safe_ptr<buffer_pool<host_buffer>>>, 2> host_pools_;\r
74         \r
75         unsigned int fbo_;\r
76 \r
77         executor executor_;\r
78                                 \r
79 public:         \r
80         ogl_device();\r
81         ~ogl_device();\r
82 \r
83         // Not thread-safe, must be called inside of context\r
84         void enable(GLenum cap);\r
85         void disable(GLenum cap);\r
86         void viewport(size_t x, size_t y, size_t width, size_t height);\r
87         void scissor(size_t x, size_t y, size_t width, size_t height);\r
88         void stipple_pattern(const GLubyte* pattern);\r
89 \r
90         void attach(device_buffer& texture);\r
91         void clear(device_buffer& texture);\r
92 \r
93         void begin_read(host_buffer& dest, device_buffer& source);\r
94         void begin_read(device_buffer& dest, host_buffer& source);\r
95 \r
96         void blend_func(int c1, int c2, int a1, int a2);\r
97         void blend_func(int c1, int c2);\r
98         \r
99         void use(shader& shader);\r
100 \r
101         void flush();\r
102 \r
103         // thread-afe\r
104         template<typename Func>\r
105         auto begin_invoke(Func&& func, task_priority priority = normal_priority) -> boost::unique_future<decltype(func())> // noexcept\r
106         {                       \r
107                 return executor_.begin_invoke(std::forward<Func>(func), priority);\r
108         }\r
109         \r
110         template<typename Func>\r
111         auto invoke(Func&& func, task_priority priority = normal_priority) -> decltype(func())\r
112         {\r
113                 return executor_.invoke(std::forward<Func>(func), priority);\r
114         }\r
115                 \r
116         safe_ptr<device_buffer> create_device_buffer(size_t width, size_t height, size_t stride);\r
117         safe_ptr<host_buffer> create_host_buffer(size_t size, host_buffer::usage_t usage);\r
118         \r
119         void yield();\r
120         boost::unique_future<void> gc();\r
121 \r
122         static std::wstring get_version();\r
123 \r
124 private:\r
125         safe_ptr<device_buffer> allocate_device_buffer(size_t width, size_t height, size_t stride);\r
126         safe_ptr<host_buffer> allocate_host_buffer(size_t size, host_buffer::usage_t usage);\r
127 };\r
128 \r
129 }}