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) {
35 glGenBuffers(1, &pbo);
37 glBindBuffer(buffer, pbo);
39 glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT);
43 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
44 frame.data2 = frame.data + frame_size / 2;
46 frame.size = frame_size;
47 frame.userdata = &userdata[i];
48 userdata[i].pbo = pbo;
49 userdata[i].pixel_format = pixel_format;
52 // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
53 // into separate textures. For 10-bit, the input format (v210)
54 // is complicated enough that we need to interpolate up to 4:4:4,
55 // which we do in a compute shader ourselves. For BGRA, the data
56 // is already 4:4:4:4.
57 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
59 // Create textures. We don't allocate any data for the second field at this point
60 // (just create the texture state with the samplers), since our default assumed
61 // resolution is progressive.
62 switch (pixel_format) {
63 case bmusb::PixelFormat_8BitYCbCr:
64 glGenTextures(2, userdata[i].tex_y);
66 glGenTextures(2, userdata[i].tex_cbcr);
69 case bmusb::PixelFormat_10BitYCbCr:
70 glGenTextures(2, userdata[i].tex_v210);
72 glGenTextures(2, userdata[i].tex_444);
75 case bmusb::PixelFormat_8BitBGRA:
76 glGenTextures(2, userdata[i].tex_rgba);
79 case bmusb::PixelFormat_8BitYCbCrPlanar:
80 glGenTextures(2, userdata[i].tex_y);
82 glGenTextures(2, userdata[i].tex_cb);
84 glGenTextures(2, userdata[i].tex_cr);
91 userdata[i].last_width[0] = width;
92 userdata[i].last_height[0] = height;
93 userdata[i].last_cbcr_width[0] = width / 2;
94 userdata[i].last_cbcr_height[0] = height;
95 userdata[i].last_v210_width[0] = 0;
97 userdata[i].last_width[1] = 0;
98 userdata[i].last_height[1] = 0;
99 userdata[i].last_cbcr_width[1] = 0;
100 userdata[i].last_cbcr_height[1] = 0;
101 userdata[i].last_v210_width[1] = 0;
103 userdata[i].last_interlaced = false;
104 userdata[i].last_has_signal = false;
105 userdata[i].last_is_connected = false;
106 for (unsigned field = 0; field < 2; ++field) {
107 switch (pixel_format) {
108 case bmusb::PixelFormat_10BitYCbCr: {
109 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
111 // Seemingly we need to set the minification filter even though
112 // shader image loads don't use them, or NVIDIA will just give us
114 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_v210[field]);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
119 userdata[i].last_v210_width[0] = v210_width;
120 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
124 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_444[field]);
128 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
133 case bmusb::PixelFormat_8BitYCbCr:
134 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
138 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
142 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
146 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
150 case bmusb::PixelFormat_8BitBGRA:
151 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_rgba[field]);
155 if (global_flags.can_disable_srgb_decoder) { // See the comments in tweaked_inputs.h.
156 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
158 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
163 case bmusb::PixelFormat_8BitYCbCrPlanar:
164 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
168 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
172 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cb[field]);
176 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
180 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cr[field]);
184 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
193 freelist.push(frame);
195 glBindBuffer(buffer, 0);
197 glBindTexture(GL_TEXTURE_2D, 0);
201 PBOFrameAllocator::~PBOFrameAllocator()
203 while (!freelist.empty()) {
204 Frame frame = freelist.front();
206 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
207 glBindBuffer(buffer, pbo);
209 glUnmapBuffer(buffer);
211 glBindBuffer(buffer, 0);
213 glDeleteBuffers(1, &pbo);
215 switch (pixel_format) {
216 case bmusb::PixelFormat_10BitYCbCr:
217 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_v210);
219 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_444);
222 case bmusb::PixelFormat_8BitYCbCr:
223 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
225 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
228 case bmusb::PixelFormat_8BitBGRA:
229 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_rgba);
232 case bmusb::PixelFormat_8BitYCbCrPlanar:
233 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
235 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cb);
237 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cr);
245 //static int sumsum = 0;
247 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
251 unique_lock<mutex> lock(freelist_mutex); // Meh.
252 if (freelist.empty()) {
253 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
255 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
256 vf = freelist.front();
257 freelist.pop(); // Meh.
264 void PBOFrameAllocator::release_frame(Frame frame)
266 if (frame.overflow > 0) {
267 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
271 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
272 memset(frame.data, 0, frame.size);
273 Userdata *userdata = (Userdata *)frame.userdata;
274 for (unsigned field = 0; field < 2; ++field) {
275 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
277 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
279 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
281 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
283 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
286 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
288 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
290 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
294 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
299 unique_lock<mutex> lock(freelist_mutex);
300 freelist.push(frame);