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