]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Give frames a pixel format.
[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.
41                 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
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 (pixel_format == bmusb::PixelFormat_10BitYCbCr) {
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
58                 userdata[i].last_width[0] = width;
59                 userdata[i].last_height[0] = height;
60                 userdata[i].last_v210_width[0] = 0;
61
62                 userdata[i].last_width[1] = 0;
63                 userdata[i].last_height[1] = 0;
64                 userdata[i].last_v210_width[1] = 0;
65
66                 userdata[i].last_interlaced = false;
67                 userdata[i].last_has_signal = false;
68                 userdata[i].last_is_connected = false;
69                 for (unsigned field = 0; field < 2; ++field) {
70                         if (pixel_format == bmusb::PixelFormat_10BitYCbCr) {
71                                 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
72
73                                 // Seemingly we need to set the minification filter even though
74                                 // shader image loads don't use them, or NVIDIA will just give us
75                                 // zero back.
76                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_v210[field]);
77                                 check_error();
78                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
79                                 check_error();
80                                 if (field == 0) {
81                                         userdata[i].last_v210_width[0] = v210_width;
82                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
83                                         check_error();
84                                 }
85
86                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_444[field]);
87                                 check_error();
88                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
89                                 check_error();
90                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
91                                 check_error();
92                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
93                                 check_error();
94                                 if (field == 0) {
95                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
96                                         check_error();
97                                 }
98                         } else {
99                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
100                                 check_error();
101                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
102                                 check_error();
103                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
104                                 check_error();
105                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
106                                 check_error();
107                                 if (field == 0) {
108                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
109                                         check_error();
110                                 }
111
112                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
113                                 check_error();
114                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
115                                 check_error();
116                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
117                                 check_error();
118                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
119                                 check_error();
120                                 if (field == 0) {
121                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
122                                         check_error();
123                                 }
124                         }
125                 }
126
127                 freelist.push(frame);
128         }
129         glBindBuffer(buffer, 0);
130         check_error();
131         glBindTexture(GL_TEXTURE_2D, 0);
132         check_error();
133 }
134
135 PBOFrameAllocator::~PBOFrameAllocator()
136 {
137         while (!freelist.empty()) {
138                 Frame frame = freelist.front();
139                 freelist.pop();
140                 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
141                 glBindBuffer(buffer, pbo);
142                 check_error();
143                 glUnmapBuffer(buffer);
144                 check_error();
145                 glBindBuffer(buffer, 0);
146                 check_error();
147                 glDeleteBuffers(1, &pbo);
148                 check_error();
149                 if (pixel_format == bmusb::PixelFormat_10BitYCbCr) {
150                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_v210);
151                         check_error();
152                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_444);
153                         check_error();
154                 } else {
155                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
156                         check_error();
157                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
158                         check_error();
159                 }
160         }
161 }
162 //static int sumsum = 0;
163
164 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
165 {
166         Frame vf;
167
168         unique_lock<mutex> lock(freelist_mutex);  // Meh.
169         if (freelist.empty()) {
170                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
171         } else {
172                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
173                 vf = freelist.front();
174                 freelist.pop();  // Meh.
175         }
176         vf.len = 0;
177         vf.overflow = 0;
178         return vf;
179 }
180
181 void PBOFrameAllocator::release_frame(Frame frame)
182 {
183         if (frame.overflow > 0) {
184                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
185         }
186
187 #if 0
188         // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
189         memset(frame.data, 0, frame.size);
190         Userdata *userdata = (Userdata *)frame.userdata;
191         for (unsigned field = 0; field < 2; ++field) {
192                 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
193                 check_error();
194                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
195                 check_error();
196                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
197                 check_error();
198                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
199                 check_error();
200                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
201                 check_error();
202
203                 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
204                 check_error();
205                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
206                 check_error();
207                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
208                 check_error();
209                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
210                 check_error();
211                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
212                 check_error();
213         }
214 #endif
215
216         unique_lock<mutex> lock(freelist_mutex);
217         freelist.push(frame);
218         //--sumsum;
219 }