1 #include "pbo_frame_allocator.h"
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
11 #include "v210_converter.h"
15 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
18 userdata.reset(new Userdata[num_queued_frames]);
19 for (size_t i = 0; i < num_queued_frames; ++i) {
21 glGenBuffers(1, &pbo);
23 glBindBuffer(buffer, pbo);
25 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
29 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
30 frame.data2 = frame.data + frame_size / 2;
32 frame.size = frame_size;
33 frame.userdata = &userdata[i];
34 userdata[i].pbo = pbo;
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 = !global_flags.ten_bit_input;
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 (global_flags.ten_bit_input) {
47 glGenTextures(2, userdata[i].tex_v210);
49 glGenTextures(2, userdata[i].tex_444);
52 glGenTextures(2, userdata[i].tex_y);
54 glGenTextures(2, userdata[i].tex_cbcr);
58 userdata[i].last_width[0] = width;
59 userdata[i].last_height[0] = height;
60 userdata[i].last_v210_width[0] = 0;
62 userdata[i].last_width[1] = 0;
63 userdata[i].last_height[1] = 0;
64 userdata[i].last_v210_width[1] = 0;
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 (global_flags.ten_bit_input) {
71 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
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
76 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_v210[field]);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
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);
86 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_444[field]);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
95 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
99 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
105 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
108 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
112 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
121 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
127 freelist.push(frame);
129 glBindBuffer(buffer, 0);
131 glBindTexture(GL_TEXTURE_2D, 0);
135 PBOFrameAllocator::~PBOFrameAllocator()
137 while (!freelist.empty()) {
138 Frame frame = freelist.front();
140 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
141 glBindBuffer(buffer, pbo);
143 glUnmapBuffer(buffer);
145 glBindBuffer(buffer, 0);
147 glDeleteBuffers(1, &pbo);
149 if (global_flags.ten_bit_input) {
150 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_v210);
152 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_444);
155 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
157 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
162 //static int sumsum = 0;
164 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
168 unique_lock<mutex> lock(freelist_mutex); // Meh.
169 if (freelist.empty()) {
170 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
172 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
173 vf = freelist.front();
174 freelist.pop(); // Meh.
181 void PBOFrameAllocator::release_frame(Frame frame)
183 if (frame.overflow > 0) {
184 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
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]);
194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
200 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
203 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
205 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
207 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
209 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
211 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
216 unique_lock<mutex> lock(freelist_mutex);
217 freelist.push(frame);