]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Fix an issue where the v210 input texture would have an inefficient width.
[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 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
16         : buffer(buffer)
17 {
18         userdata.reset(new Userdata[num_queued_frames]);
19         for (size_t i = 0; i < num_queued_frames; ++i) {
20                 GLuint pbo;
21                 glGenBuffers(1, &pbo);
22                 check_error();
23                 glBindBuffer(buffer, pbo);
24                 check_error();
25                 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
26                 check_error();
27
28                 Frame frame;
29                 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
30                 frame.data2 = frame.data + frame_size / 2;
31                 check_error();
32                 frame.size = frame_size;
33                 frame.userdata = &userdata[i];
34                 userdata[i].pbo = pbo;
35                 frame.owner = this;
36
37                 // For 8-bit Y'CbCr, we ask the driver to split Y' and Cb/Cr
38                 // into separate textures. For 10-bit, the input format (v210)
39                 // is complicated enough that we need to interpolate up to 4:4:4,
40                 // which we do in a compute shader ourselves.
41                 frame.interleaved = !global_flags.ten_bit_input;
42
43                 // Create textures. We don't allocate any data for the second field at this point
44                 // (just create the texture state with the samplers), since our default assumed
45                 // resolution is progressive.
46                 if (global_flags.ten_bit_input) {
47                         glGenTextures(2, userdata[i].tex_v210);
48                         check_error();
49                         glGenTextures(2, userdata[i].tex_444);
50                         check_error();
51                 } else {
52                         glGenTextures(2, userdata[i].tex_y);
53                         check_error();
54                         glGenTextures(2, userdata[i].tex_cbcr);
55                         check_error();
56                 }
57                 userdata[i].last_width[0] = width;
58                 userdata[i].last_height[0] = height;
59                 userdata[i].last_width[1] = 0;
60                 userdata[i].last_height[1] = 0;
61                 userdata[i].last_interlaced = false;
62                 userdata[i].last_has_signal = false;
63                 userdata[i].last_is_connected = false;
64                 for (unsigned field = 0; field < 2; ++field) {
65                         if (global_flags.ten_bit_input) {
66                                 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
67
68                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_v210[field]);
69                                 check_error();
70                                 // Don't care about texture parameters, we're only going to read it
71                                 // from the compute shader anyway.
72                                 if (field == 0) {
73                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
74                                         check_error();
75                                 }
76
77                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_444[field]);
78                                 check_error();
79                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
80                                 check_error();
81                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
82                                 check_error();
83                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
84                                 check_error();
85                                 if (field == 0) {
86                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
87                                         check_error();
88                                 }
89                         } else {
90                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
91                                 check_error();
92                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
93                                 check_error();
94                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
95                                 check_error();
96                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
97                                 check_error();
98                                 if (field == 0) {
99                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
100                                         check_error();
101                                 }
102
103                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
104                                 check_error();
105                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
106                                 check_error();
107                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
108                                 check_error();
109                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
110                                 check_error();
111                                 if (field == 0) {
112                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
113                                         check_error();
114                                 }
115                         }
116                 }
117
118                 freelist.push(frame);
119         }
120         glBindBuffer(buffer, 0);
121         check_error();
122         glBindTexture(GL_TEXTURE_2D, 0);
123         check_error();
124 }
125
126 PBOFrameAllocator::~PBOFrameAllocator()
127 {
128         while (!freelist.empty()) {
129                 Frame frame = freelist.front();
130                 freelist.pop();
131                 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
132                 glBindBuffer(buffer, pbo);
133                 check_error();
134                 glUnmapBuffer(buffer);
135                 check_error();
136                 glBindBuffer(buffer, 0);
137                 check_error();
138                 glDeleteBuffers(1, &pbo);
139                 check_error();
140                 if (global_flags.ten_bit_input) {
141                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_v210);
142                         check_error();
143                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_444);
144                         check_error();
145                 } else {
146                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
147                         check_error();
148                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
149                         check_error();
150                 }
151         }
152 }
153 //static int sumsum = 0;
154
155 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
156 {
157         Frame vf;
158
159         unique_lock<mutex> lock(freelist_mutex);  // Meh.
160         if (freelist.empty()) {
161                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
162         } else {
163                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
164                 vf = freelist.front();
165                 freelist.pop();  // Meh.
166         }
167         vf.len = 0;
168         vf.overflow = 0;
169         return vf;
170 }
171
172 void PBOFrameAllocator::release_frame(Frame frame)
173 {
174         if (frame.overflow > 0) {
175                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
176         }
177
178 #if 0
179         // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
180         memset(frame.data, 0, frame.size);
181         Userdata *userdata = (Userdata *)frame.userdata;
182         for (unsigned field = 0; field < 2; ++field) {
183                 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
184                 check_error();
185                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
186                 check_error();
187                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
188                 check_error();
189                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
190                 check_error();
191                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
192                 check_error();
193
194                 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
195                 check_error();
196                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
197                 check_error();
198                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
199                 check_error();
200                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
201                 check_error();
202                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
203                 check_error();
204         }
205 #endif
206
207         unique_lock<mutex> lock(freelist_mutex);
208         freelist.push(frame);
209         //--sumsum;
210 }