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 frame.userdata = &userdata[frame_idx];
59 userdata[frame_idx].pbo = pbo;
60 userdata[frame_idx].pixel_format = pixel_format;
63 // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
64 // into separate textures. For 10-bit, the input format (v210)
65 // is complicated enough that we need to interpolate up to 4:4:4,
66 // which we do in a compute shader ourselves. For BGRA, the data
67 // is already 4:4:4:4.
68 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
70 // Create textures. We don't allocate any data for the second field at this point
71 // (just create the texture state with the samplers), since our default assumed
72 // resolution is progressive.
73 switch (pixel_format) {
74 case bmusb::PixelFormat_8BitYCbCr:
75 glGenTextures(2, userdata[frame_idx].tex_y);
77 glGenTextures(2, userdata[frame_idx].tex_cbcr);
80 case bmusb::PixelFormat_10BitYCbCr:
81 glGenTextures(2, userdata[frame_idx].tex_v210);
83 glGenTextures(2, userdata[frame_idx].tex_444);
86 case bmusb::PixelFormat_8BitBGRA:
87 glGenTextures(2, userdata[frame_idx].tex_rgba);
90 case bmusb::PixelFormat_8BitYCbCrPlanar:
91 glGenTextures(2, userdata[frame_idx].tex_y);
93 glGenTextures(2, userdata[frame_idx].tex_cb);
95 glGenTextures(2, userdata[frame_idx].tex_cr);
102 userdata[frame_idx].last_width[0] = width;
103 userdata[frame_idx].last_height[0] = height;
104 userdata[frame_idx].last_cbcr_width[0] = width / 2;
105 userdata[frame_idx].last_cbcr_height[0] = height;
106 userdata[frame_idx].last_v210_width[0] = 0;
108 userdata[frame_idx].last_width[1] = 0;
109 userdata[frame_idx].last_height[1] = 0;
110 userdata[frame_idx].last_cbcr_width[1] = 0;
111 userdata[frame_idx].last_cbcr_height[1] = 0;
112 userdata[frame_idx].last_v210_width[1] = 0;
114 userdata[frame_idx].last_interlaced = false;
115 userdata[frame_idx].last_has_signal = false;
116 userdata[frame_idx].last_is_connected = false;
117 for (unsigned field = 0; field < 2; ++field) {
118 switch (pixel_format) {
119 case bmusb::PixelFormat_10BitYCbCr: {
120 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
122 // Seemingly we need to set the minification filter even though
123 // shader image loads don't use them, or NVIDIA will just give us
125 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_v210[field]);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
130 userdata[frame_idx].last_v210_width[0] = v210_width;
131 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
135 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_444[field]);
139 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
144 case bmusb::PixelFormat_8BitYCbCr:
145 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_y[field]);
149 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
153 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cbcr[field]);
157 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
161 case bmusb::PixelFormat_8BitBGRA:
162 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_rgba[field]);
166 if (global_flags.can_disable_srgb_decoder) { // See the comments in tweaked_inputs.h.
167 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
169 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
174 case bmusb::PixelFormat_8BitYCbCrPlanar:
175 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_y[field]);
179 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
183 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cb[field]);
187 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
191 glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cr[field]);
195 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
204 freelist.push(frame);
207 PBOFrameAllocator::~PBOFrameAllocator()
209 while (!freelist.empty()) {
210 Frame frame = freelist.front();
212 destroy_frame(&frame);
216 void PBOFrameAllocator::destroy_frame(Frame *frame)
218 delete[] frame->data_copy;
220 GLuint pbo = ((Userdata *)frame->userdata)->pbo;
221 glBindBuffer(buffer, pbo);
223 glUnmapBuffer(buffer);
225 glBindBuffer(buffer, 0);
227 glDeleteBuffers(1, &pbo);
229 switch (pixel_format) {
230 case bmusb::PixelFormat_10BitYCbCr:
231 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_v210);
233 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_444);
236 case bmusb::PixelFormat_8BitYCbCr:
237 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_y);
239 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cbcr);
242 case bmusb::PixelFormat_8BitBGRA:
243 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_rgba);
246 case bmusb::PixelFormat_8BitYCbCrPlanar:
247 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_y);
249 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cb);
251 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cr);
258 //static int sumsum = 0;
260 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
264 unique_lock<mutex> lock(freelist_mutex); // Meh.
265 if (freelist.empty()) {
266 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
268 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
269 vf = freelist.front();
270 freelist.pop(); // Meh.
277 void PBOFrameAllocator::release_frame(Frame frame)
279 if (frame.overflow > 0) {
280 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
284 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
285 memset(frame.data, 0, frame.size);
286 Userdata *userdata = (Userdata *)frame.userdata;
287 for (unsigned field = 0; field < 2; ++field) {
288 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
290 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
296 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
299 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
301 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
303 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
305 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
307 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
312 unique_lock<mutex> lock(freelist_mutex);
313 freelist.push(frame);