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