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