]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Release Nageru 1.7.2.
[nageru] / pbo_frame_allocator.cpp
1 #include "pbo_frame_allocator.h"
2
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <cstddef>
9
10 #include "flags.h"
11 #include "v210_converter.h"
12
13 using namespace std;
14
15 namespace {
16
17 void set_clamp_to_edge()
18 {
19         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
20         check_error();
21         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
22         check_error();
23         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
24         check_error();
25 }
26
27 }  // namespace
28
29 PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
30         : pixel_format(pixel_format), buffer(buffer)
31 {
32         userdata.reset(new Userdata[num_queued_frames]);
33         for (size_t i = 0; i < num_queued_frames; ++i) {
34                 init_frame(i, frame_size, width, height, permissions, map_bits);
35         }
36         glBindBuffer(buffer, 0);
37         check_error();
38         glBindTexture(GL_TEXTURE_2D, 0);
39         check_error();
40 }
41
42 void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits)
43 {
44         GLuint pbo;
45         glGenBuffers(1, &pbo);
46         check_error();
47         glBindBuffer(buffer, pbo);
48         check_error();
49         glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT);
50         check_error();
51
52         Frame frame;
53         frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
54         frame.data2 = frame.data + frame_size / 2;
55         check_error();
56         frame.size = frame_size;
57         frame.userdata = &userdata[frame_idx];
58         userdata[frame_idx].pbo = pbo;
59         userdata[frame_idx].pixel_format = pixel_format;
60         frame.owner = this;
61
62         // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
63         // into separate textures. For 10-bit, the input format (v210)
64         // is complicated enough that we need to interpolate up to 4:4:4,
65         // which we do in a compute shader ourselves. For BGRA, the data
66         // is already 4:4:4:4.
67         frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
68
69         // Create textures. We don't allocate any data for the second field at this point
70         // (just create the texture state with the samplers), since our default assumed
71         // resolution is progressive.
72         switch (pixel_format) {
73         case bmusb::PixelFormat_8BitYCbCr:
74                 glGenTextures(2, userdata[frame_idx].tex_y);
75                 check_error();
76                 glGenTextures(2, userdata[frame_idx].tex_cbcr);
77                 check_error();
78                 break;
79         case bmusb::PixelFormat_10BitYCbCr:
80                 glGenTextures(2, userdata[frame_idx].tex_v210);
81                 check_error();
82                 glGenTextures(2, userdata[frame_idx].tex_444);
83                 check_error();
84                 break;
85         case bmusb::PixelFormat_8BitBGRA:
86                 glGenTextures(2, userdata[frame_idx].tex_rgba);
87                 check_error();
88                 break;
89         case bmusb::PixelFormat_8BitYCbCrPlanar:
90                 glGenTextures(2, userdata[frame_idx].tex_y);
91                 check_error();
92                 glGenTextures(2, userdata[frame_idx].tex_cb);
93                 check_error();
94                 glGenTextures(2, userdata[frame_idx].tex_cr);
95                 check_error();
96                 break;
97         default:
98                 assert(false);
99         }
100
101         userdata[frame_idx].last_width[0] = width;
102         userdata[frame_idx].last_height[0] = height;
103         userdata[frame_idx].last_cbcr_width[0] = width / 2;
104         userdata[frame_idx].last_cbcr_height[0] = height;
105         userdata[frame_idx].last_v210_width[0] = 0;
106
107         userdata[frame_idx].last_width[1] = 0;
108         userdata[frame_idx].last_height[1] = 0;
109         userdata[frame_idx].last_cbcr_width[1] = 0;
110         userdata[frame_idx].last_cbcr_height[1] = 0;
111         userdata[frame_idx].last_v210_width[1] = 0;
112
113         userdata[frame_idx].last_interlaced = false;
114         userdata[frame_idx].last_has_signal = false;
115         userdata[frame_idx].last_is_connected = false;
116         for (unsigned field = 0; field < 2; ++field) {
117                 switch (pixel_format) {
118                 case bmusb::PixelFormat_10BitYCbCr: {
119                         const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
120
121                         // Seemingly we need to set the minification filter even though
122                         // shader image loads don't use them, or NVIDIA will just give us
123                         // zero back.
124                         glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_v210[field]);
125                         check_error();
126                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
127                         check_error();
128                         if (field == 0) {
129                                 userdata[frame_idx].last_v210_width[0] = v210_width;
130                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
131                                 check_error();
132                         }
133
134                         glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_444[field]);
135                         check_error();
136                         set_clamp_to_edge();
137                         if (field == 0) {
138                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
139                                 check_error();
140                         }
141                         break;
142                 }
143                 case bmusb::PixelFormat_8BitYCbCr:
144                         glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_y[field]);
145                         check_error();
146                         set_clamp_to_edge();
147                         if (field == 0) {
148                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
149                                 check_error();
150                         }
151
152                         glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cbcr[field]);
153                         check_error();
154                         set_clamp_to_edge();
155                         if (field == 0) {
156                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
157                                 check_error();
158                         }
159                         break;
160                 case bmusb::PixelFormat_8BitBGRA:
161                         glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_rgba[field]);
162                         check_error();
163                         set_clamp_to_edge();
164                         if (field == 0) {
165                                 if (global_flags.can_disable_srgb_decoder) {  // See the comments in tweaked_inputs.h.
166                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
167                                 } else {
168                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
169                                 }
170                                 check_error();
171                         }
172                         break;
173                 case bmusb::PixelFormat_8BitYCbCrPlanar:
174                         glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_y[field]);
175                         check_error();
176                         set_clamp_to_edge();
177                         if (field == 0) {
178                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
179                                 check_error();
180                         }
181
182                         glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cb[field]);
183                         check_error();
184                         set_clamp_to_edge();
185                         if (field == 0) {
186                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
187                                 check_error();
188                         }
189
190                         glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cr[field]);
191                         check_error();
192                         set_clamp_to_edge();
193                         if (field == 0) {
194                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
195                                 check_error();
196                         }
197                         break;
198                 default:
199                         assert(false);
200                 }
201         }
202
203         freelist.push(frame);
204 }
205
206 PBOFrameAllocator::~PBOFrameAllocator()
207 {
208         while (!freelist.empty()) {
209                 Frame frame = freelist.front();
210                 freelist.pop();
211                 destroy_frame(&frame);
212         }
213 }
214
215 void PBOFrameAllocator::destroy_frame(Frame *frame)
216 {
217         GLuint pbo = ((Userdata *)frame->userdata)->pbo;
218         glBindBuffer(buffer, pbo);
219         check_error();
220         glUnmapBuffer(buffer);
221         check_error();
222         glBindBuffer(buffer, 0);
223         check_error();
224         glDeleteBuffers(1, &pbo);
225         check_error();
226         switch (pixel_format) {
227         case bmusb::PixelFormat_10BitYCbCr:
228                 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_v210);
229                 check_error();
230                 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_444);
231                 check_error();
232                 break;
233         case bmusb::PixelFormat_8BitYCbCr:
234                 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_y);
235                 check_error();
236                 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cbcr);
237                 check_error();
238                 break;
239         case bmusb::PixelFormat_8BitBGRA:
240                 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_rgba);
241                 check_error();
242                 break;
243         case bmusb::PixelFormat_8BitYCbCrPlanar:
244                 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_y);
245                 check_error();
246                 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cb);
247                 check_error();
248                 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cr);
249                 check_error();
250                 break;
251         default:
252                 assert(false);
253         }
254 }
255 //static int sumsum = 0;
256
257 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
258 {
259         Frame vf;
260
261         unique_lock<mutex> lock(freelist_mutex);  // Meh.
262         if (freelist.empty()) {
263                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
264         } else {
265                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
266                 vf = freelist.front();
267                 freelist.pop();  // Meh.
268         }
269         vf.len = 0;
270         vf.overflow = 0;
271         return vf;
272 }
273
274 void PBOFrameAllocator::release_frame(Frame frame)
275 {
276         if (frame.overflow > 0) {
277                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
278         }
279
280 #if 0
281         // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
282         memset(frame.data, 0, frame.size);
283         Userdata *userdata = (Userdata *)frame.userdata;
284         for (unsigned field = 0; field < 2; ++field) {
285                 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
286                 check_error();
287                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
288                 check_error();
289                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
290                 check_error();
291                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
292                 check_error();
293                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
294                 check_error();
295
296                 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
297                 check_error();
298                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
299                 check_error();
300                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
301                 check_error();
302                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
303                 check_error();
304                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
305                 check_error();
306         }
307 #endif
308
309         unique_lock<mutex> lock(freelist_mutex);
310         freelist.push(frame);
311         //--sumsum;
312 }