1 #include "pbo_frame_allocator.h"
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
11 #include "v210_converter.h"
17 void set_clamp_to_edge()
19 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
21 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
23 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
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)
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);
36 glBindBuffer(buffer, 0);
38 glBindTexture(GL_TEXTURE_2D, 0);
42 void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits)
45 glGenBuffers(1, &pbo);
47 glBindBuffer(buffer, pbo);
49 glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT);
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];
57 frame.size = frame_size;
58 Userdata *ud = &userdata[frame_idx];
61 ud->pixel_format = pixel_format;
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);
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);
78 glGenTextures(2, ud->tex_cbcr);
81 case bmusb::PixelFormat_10BitYCbCr:
82 glGenTextures(2, ud->tex_v210);
84 glGenTextures(2, ud->tex_444);
87 case bmusb::PixelFormat_8BitBGRA:
88 glGenTextures(2, ud->tex_rgba);
91 case bmusb::PixelFormat_8BitYCbCrPlanar:
92 glGenTextures(2, ud->tex_y);
94 glGenTextures(2, ud->tex_cb);
96 glGenTextures(2, ud->tex_cr);
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;
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;
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);
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
126 glBindTexture(GL_TEXTURE_2D, ud->tex_v210[field]);
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
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);
136 glBindTexture(GL_TEXTURE_2D, ud->tex_444[field]);
140 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
145 case bmusb::PixelFormat_8BitYCbCr:
146 glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
150 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
154 glBindTexture(GL_TEXTURE_2D, ud->tex_cbcr[field]);
158 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
162 case bmusb::PixelFormat_8BitBGRA:
163 glBindTexture(GL_TEXTURE_2D, ud->tex_rgba[field]);
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);
170 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
175 case bmusb::PixelFormat_8BitYCbCrPlanar:
176 glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
180 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
184 glBindTexture(GL_TEXTURE_2D, ud->tex_cb[field]);
188 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
192 glBindTexture(GL_TEXTURE_2D, ud->tex_cr[field]);
196 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
205 freelist.push(frame);
208 PBOFrameAllocator::~PBOFrameAllocator()
210 while (!freelist.empty()) {
211 Frame frame = freelist.front();
213 destroy_frame(&frame);
217 void PBOFrameAllocator::destroy_frame(Frame *frame)
219 Userdata *ud = (Userdata *)frame->userdata;
220 delete[] frame->data_copy;
222 GLuint pbo = ud->pbo;
223 glBindBuffer(buffer, pbo);
225 glUnmapBuffer(buffer);
227 glBindBuffer(buffer, 0);
229 glDeleteBuffers(1, &pbo);
231 switch (pixel_format) {
232 case bmusb::PixelFormat_10BitYCbCr:
233 glDeleteTextures(2, ud->tex_v210);
235 glDeleteTextures(2, ud->tex_444);
238 case bmusb::PixelFormat_8BitYCbCr:
239 glDeleteTextures(2, ud->tex_y);
241 glDeleteTextures(2, ud->tex_cbcr);
244 case bmusb::PixelFormat_8BitBGRA:
245 glDeleteTextures(2, ud->tex_rgba);
248 case bmusb::PixelFormat_8BitYCbCrPlanar:
249 glDeleteTextures(2, ud->tex_y);
251 glDeleteTextures(2, ud->tex_cb);
253 glDeleteTextures(2, ud->tex_cr);
260 //static int sumsum = 0;
262 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
266 lock_guard<mutex> lock(freelist_mutex); // Meh.
267 if (freelist.empty()) {
268 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
270 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
271 vf = freelist.front();
272 freelist.pop(); // Meh.
279 void PBOFrameAllocator::release_frame(Frame frame)
281 if (frame.overflow > 0) {
282 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
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]);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
298 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
301 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
303 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
305 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
307 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
309 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
314 lock_guard<mutex> lock(freelist_mutex);
315 freelist.push(frame);