]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Unbreak 10-bit input on NVIDIA.
[nageru] / pbo_frame_allocator.cpp
1 #include "pbo_frame_allocator.h"
2
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <cstddef>
9
10 #include "flags.h"
11 #include "v210_converter.h"
12
13 using namespace std;
14
15 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
16         : buffer(buffer)
17 {
18         userdata.reset(new Userdata[num_queued_frames]);
19         for (size_t i = 0; i < num_queued_frames; ++i) {
20                 GLuint pbo;
21                 glGenBuffers(1, &pbo);
22                 check_error();
23                 glBindBuffer(buffer, pbo);
24                 check_error();
25                 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
26                 check_error();
27
28                 Frame frame;
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;
31                 check_error();
32                 frame.size = frame_size;
33                 frame.userdata = &userdata[i];
34                 userdata[i].pbo = pbo;
35                 frame.owner = this;
36
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;
42
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);
48                         check_error();
49                         glGenTextures(2, userdata[i].tex_444);
50                         check_error();
51                 } else {
52                         glGenTextures(2, userdata[i].tex_y);
53                         check_error();
54                         glGenTextures(2, userdata[i].tex_cbcr);
55                         check_error();
56                 }
57                 userdata[i].last_width[0] = width;
58                 userdata[i].last_height[0] = height;
59                 userdata[i].last_width[1] = 0;
60                 userdata[i].last_height[1] = 0;
61                 userdata[i].last_interlaced = false;
62                 userdata[i].last_has_signal = false;
63                 userdata[i].last_is_connected = false;
64                 for (unsigned field = 0; field < 2; ++field) {
65                         if (global_flags.ten_bit_input) {
66                                 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
67
68                                 // Seemingly we need to set the minification filter even though
69                                 // shader image loads don't use them, or NVIDIA will just give us
70                                 // zero back.
71                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_v210[field]);
72                                 check_error();
73                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
74                                 check_error();
75                                 if (field == 0) {
76                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
77                                         check_error();
78                                 }
79
80                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_444[field]);
81                                 check_error();
82                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
83                                 check_error();
84                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
85                                 check_error();
86                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
87                                 check_error();
88                                 if (field == 0) {
89                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
90                                         check_error();
91                                 }
92                         } else {
93                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
94                                 check_error();
95                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
96                                 check_error();
97                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
98                                 check_error();
99                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
100                                 check_error();
101                                 if (field == 0) {
102                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
103                                         check_error();
104                                 }
105
106                                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
107                                 check_error();
108                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
109                                 check_error();
110                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
111                                 check_error();
112                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
113                                 check_error();
114                                 if (field == 0) {
115                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
116                                         check_error();
117                                 }
118                         }
119                 }
120
121                 freelist.push(frame);
122         }
123         glBindBuffer(buffer, 0);
124         check_error();
125         glBindTexture(GL_TEXTURE_2D, 0);
126         check_error();
127 }
128
129 PBOFrameAllocator::~PBOFrameAllocator()
130 {
131         while (!freelist.empty()) {
132                 Frame frame = freelist.front();
133                 freelist.pop();
134                 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
135                 glBindBuffer(buffer, pbo);
136                 check_error();
137                 glUnmapBuffer(buffer);
138                 check_error();
139                 glBindBuffer(buffer, 0);
140                 check_error();
141                 glDeleteBuffers(1, &pbo);
142                 check_error();
143                 if (global_flags.ten_bit_input) {
144                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_v210);
145                         check_error();
146                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_444);
147                         check_error();
148                 } else {
149                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
150                         check_error();
151                         glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
152                         check_error();
153                 }
154         }
155 }
156 //static int sumsum = 0;
157
158 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
159 {
160         Frame vf;
161
162         unique_lock<mutex> lock(freelist_mutex);  // Meh.
163         if (freelist.empty()) {
164                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
165         } else {
166                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
167                 vf = freelist.front();
168                 freelist.pop();  // Meh.
169         }
170         vf.len = 0;
171         vf.overflow = 0;
172         return vf;
173 }
174
175 void PBOFrameAllocator::release_frame(Frame frame)
176 {
177         if (frame.overflow > 0) {
178                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
179         }
180
181 #if 0
182         // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
183         memset(frame.data, 0, frame.size);
184         Userdata *userdata = (Userdata *)frame.userdata;
185         for (unsigned field = 0; field < 2; ++field) {
186                 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
187                 check_error();
188                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
189                 check_error();
190                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
191                 check_error();
192                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
193                 check_error();
194                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
195                 check_error();
196
197                 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
198                 check_error();
199                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
200                 check_error();
201                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
202                 check_error();
203                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
204                 check_error();
205                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
206                 check_error();
207         }
208 #endif
209
210         unique_lock<mutex> lock(freelist_mutex);
211         freelist.push(frame);
212         //--sumsum;
213 }