]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Make sure our uploaded RGBA textures can deal with being asked for linear gamma.
[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(bmusb::PixelFormat pixel_format, size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
16         : pixel_format(pixel_format), 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                 userdata[i].pixel_format = pixel_format;
36                 frame.owner = this;
37
38                 // For 8-bit Y'CbCr, we ask the driver to split Y' and Cb/Cr
39                 // into separate textures. For 10-bit, the input format (v210)
40                 // is complicated enough that we need to interpolate up to 4:4:4,
41                 // which we do in a compute shader ourselves. For RGBA, the data
42                 // is already 4:4:4:4.
43                 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
44
45                 // Create textures. We don't allocate any data for the second field at this point
46                 // (just create the texture state with the samplers), since our default assumed
47                 // resolution is progressive.
48                 switch (pixel_format) {
49                 case bmusb::PixelFormat_8BitYCbCr:
50                         glGenTextures(2, userdata[i].tex_y);
51                         check_error();
52                         glGenTextures(2, userdata[i].tex_cbcr);
53                         check_error();
54                         break;
55                 case bmusb::PixelFormat_10BitYCbCr:
56                         glGenTextures(2, userdata[i].tex_v210);
57                         check_error();
58                         glGenTextures(2, userdata[i].tex_444);
59                         check_error();
60                         break;
61                 case bmusb::PixelFormat_8BitRGBA:
62                         glGenTextures(2, userdata[i].tex_rgba);
63                         check_error();
64                         break;
65                 }
66
67                 userdata[i].last_width[0] = width;
68                 userdata[i].last_height[0] = height;
69                 userdata[i].last_v210_width[0] = 0;
70
71                 userdata[i].last_width[1] = 0;
72                 userdata[i].last_height[1] = 0;
73                 userdata[i].last_v210_width[1] = 0;
74
75                 userdata[i].last_interlaced = false;
76                 userdata[i].last_has_signal = false;
77                 userdata[i].last_is_connected = false;
78                 for (unsigned field = 0; field < 2; ++field) {
79                         switch (pixel_format) {
80                         case bmusb::PixelFormat_10BitYCbCr: {
81                                 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
82
83                                 // Seemingly we need to set the minification filter even though
84                                 // shader image loads don't use them, or NVIDIA will just give us
85                                 // zero back.
86                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_v210[field]);
87                                 check_error();
88                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
89                                 check_error();
90                                 if (field == 0) {
91                                         userdata[i].last_v210_width[0] = v210_width;
92                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
93                                         check_error();
94                                 }
95
96                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_444[field]);
97                                 check_error();
98                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
99                                 check_error();
100                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
101                                 check_error();
102                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
103                                 check_error();
104                                 if (field == 0) {
105                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
106                                         check_error();
107                                 }
108                                 break;
109                         }
110                         case bmusb::PixelFormat_8BitYCbCr:
111                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
112                                 check_error();
113                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
114                                 check_error();
115                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
116                                 check_error();
117                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
118                                 check_error();
119                                 if (field == 0) {
120                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
121                                         check_error();
122                                 }
123
124                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
125                                 check_error();
126                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
127                                 check_error();
128                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
129                                 check_error();
130                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
131                                 check_error();
132                                 if (field == 0) {
133                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
134                                         check_error();
135                                 }
136                                 break;
137                         case bmusb::PixelFormat_8BitRGBA:
138                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_rgba[field]);
139                                 check_error();
140                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
141                                 check_error();
142                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
143                                 check_error();
144                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
145                                 check_error();
146                                 if (field == 0) {
147                                         if (global_flags.can_disable_srgb_decoder) {  // See the comments in tweaked_inputs.h.
148                                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
149                                         } else {
150                                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
151                                         }
152                                         check_error();
153                                 }
154                                 break;
155                         default:
156                                 assert(false);
157                         }
158                 }
159
160                 freelist.push(frame);
161         }
162         glBindBuffer(buffer, 0);
163         check_error();
164         glBindTexture(GL_TEXTURE_2D, 0);
165         check_error();
166 }
167
168 PBOFrameAllocator::~PBOFrameAllocator()
169 {
170         while (!freelist.empty()) {
171                 Frame frame = freelist.front();
172                 freelist.pop();
173                 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
174                 glBindBuffer(buffer, pbo);
175                 check_error();
176                 glUnmapBuffer(buffer);
177                 check_error();
178                 glBindBuffer(buffer, 0);
179                 check_error();
180                 glDeleteBuffers(1, &pbo);
181                 check_error();
182                 switch (pixel_format) {
183                 case bmusb::PixelFormat_10BitYCbCr:
184                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_v210);
185                         check_error();
186                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_444);
187                         check_error();
188                         break;
189                 case bmusb::PixelFormat_8BitYCbCr:
190                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
191                         check_error();
192                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
193                         check_error();
194                         break;
195                 case bmusb::PixelFormat_8BitRGBA:
196                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_rgba);
197                         check_error();
198                         break;
199                 default:
200                         assert(false);
201                 }
202         }
203 }
204 //static int sumsum = 0;
205
206 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
207 {
208         Frame vf;
209
210         unique_lock<mutex> lock(freelist_mutex);  // Meh.
211         if (freelist.empty()) {
212                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
213         } else {
214                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
215                 vf = freelist.front();
216                 freelist.pop();  // Meh.
217         }
218         vf.len = 0;
219         vf.overflow = 0;
220         return vf;
221 }
222
223 void PBOFrameAllocator::release_frame(Frame frame)
224 {
225         if (frame.overflow > 0) {
226                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
227         }
228
229 #if 0
230         // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
231         memset(frame.data, 0, frame.size);
232         Userdata *userdata = (Userdata *)frame.userdata;
233         for (unsigned field = 0; field < 2; ++field) {
234                 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
235                 check_error();
236                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
237                 check_error();
238                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
239                 check_error();
240                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
241                 check_error();
242                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
243                 check_error();
244
245                 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
246                 check_error();
247                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
248                 check_error();
249                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
250                 check_error();
251                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
252                 check_error();
253                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
254                 check_error();
255         }
256 #endif
257
258         unique_lock<mutex> lock(freelist_mutex);
259         freelist.push(frame);
260         //--sumsum;
261 }