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;
56 frame.size = frame_size;
57 frame.userdata = &userdata[frame_idx];
58 userdata[frame_idx].pbo = pbo;
59 userdata[frame_idx].pixel_format = pixel_format;
62 // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
63 // into separate textures. For 10-bit, the input format (v210)
64 // is complicated enough that we need to interpolate up to 4:4:4,
65 // which we do in a compute shader ourselves. For BGRA, the data
66 // is already 4:4:4:4.
67 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
69 // Create textures. We don't allocate any data for the second field at this point
70 // (just create the texture state with the samplers), since our default assumed
71 // resolution is progressive.
72 switch (pixel_format) {
73 case bmusb::PixelFormat_8BitYCbCr:
74 glGenTextures(2, userdata[frame_idx].tex_y);
76 glGenTextures(2, userdata[frame_idx].tex_cbcr);
79 case bmusb::PixelFormat_10BitYCbCr:
80 glGenTextures(2, userdata[frame_idx].tex_v210);
82 glGenTextures(2, userdata[frame_idx].tex_444);
85 case bmusb::PixelFormat_8BitBGRA:
86 glGenTextures(2, userdata[frame_idx].tex_rgba);
89 case bmusb::PixelFormat_8BitYCbCrPlanar:
90 glGenTextures(2, userdata[frame_idx].tex_y);
92 glGenTextures(2, userdata[frame_idx].tex_cb);
94 glGenTextures(2, userdata[frame_idx].tex_cr);
101 userdata[frame_idx].last_width[0] = width;
102 userdata[frame_idx].last_height[0] = height;
103 userdata[frame_idx].last_cbcr_width[0] = width / 2;
104 userdata[frame_idx].last_cbcr_height[0] = height;
105 userdata[frame_idx].last_v210_width[0] = 0;
107 userdata[frame_idx].last_width[1] = 0;
108 userdata[frame_idx].last_height[1] = 0;
109 userdata[frame_idx].last_cbcr_width[1] = 0;
110 userdata[frame_idx].last_cbcr_height[1] = 0;
111 userdata[frame_idx].last_v210_width[1] = 0;
113 userdata[frame_idx].last_interlaced = false;
114 userdata[frame_idx].last_has_signal = false;
115 userdata[frame_idx].last_is_connected = false;
116 for (unsigned field = 0; field < 2; ++field) {
117 switch (pixel_format) {
118 case bmusb::PixelFormat_10BitYCbCr: {
119 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
121 // Seemingly we need to set the minification filter even though
122 // shader image loads don't use them, or NVIDIA will just give us
124 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_v210[field]);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
129 userdata[frame_idx].last_v210_width[0] = v210_width;
130 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
134 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_444[field]);
138 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
143 case bmusb::PixelFormat_8BitYCbCr:
144 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_y[field]);
148 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
152 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cbcr[field]);
156 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
160 case bmusb::PixelFormat_8BitBGRA:
161 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_rgba[field]);
165 if (global_flags.can_disable_srgb_decoder) { // See the comments in tweaked_inputs.h.
166 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
168 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
173 case bmusb::PixelFormat_8BitYCbCrPlanar:
174 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_y[field]);
178 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
182 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cb[field]);
186 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
190 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cr[field]);
194 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
203 freelist.push(frame);
206 PBOFrameAllocator::~PBOFrameAllocator()
208 while (!freelist.empty()) {
209 Frame frame = freelist.front();
211 destroy_frame(&frame);
215 void PBOFrameAllocator::destroy_frame(Frame *frame)
217 GLuint pbo = ((Userdata *)frame->userdata)->pbo;
218 glBindBuffer(buffer, pbo);
220 glUnmapBuffer(buffer);
222 glBindBuffer(buffer, 0);
224 glDeleteBuffers(1, &pbo);
226 switch (pixel_format) {
227 case bmusb::PixelFormat_10BitYCbCr:
228 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_v210);
230 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_444);
233 case bmusb::PixelFormat_8BitYCbCr:
234 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_y);
236 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cbcr);
239 case bmusb::PixelFormat_8BitBGRA:
240 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_rgba);
243 case bmusb::PixelFormat_8BitYCbCrPlanar:
244 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_y);
246 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cb);
248 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cr);
255 //static int sumsum = 0;
257 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
261 unique_lock<mutex> lock(freelist_mutex); // Meh.
262 if (freelist.empty()) {
263 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
265 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
266 vf = freelist.front();
267 freelist.pop(); // Meh.
274 void PBOFrameAllocator::release_frame(Frame frame)
276 if (frame.overflow > 0) {
277 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
281 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
282 memset(frame.data, 0, frame.size);
283 Userdata *userdata = (Userdata *)frame.userdata;
284 for (unsigned field = 0; field < 2; ++field) {
285 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
287 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
289 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
293 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
296 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
298 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
300 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
302 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
304 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
309 unique_lock<mutex> lock(freelist_mutex);
310 freelist.push(frame);