]> git.sesse.net Git - nageru/blob - v210_converter.cpp
Set CEF autoplay policy to be more lenient.
[nageru] / v210_converter.cpp
1 #include "v210_converter.h"
2
3 #include <epoxy/gl.h>
4 #include <movit/util.h>
5
6 using namespace std;
7
8 v210Converter::~v210Converter()
9 {
10         for (const auto &shader : shaders) {
11                 glDeleteProgram(shader.second.glsl_program_num);
12                 check_error();
13         }
14 }
15
16 bool v210Converter::has_hardware_support()
17 {
18         // We don't have a GLES version of this, although GLSL ES 3.1 supports
19         // compute shaders. Note that GLSL ES has some extra restrictions,
20         // like requiring that the images are allocated with glTexStorage*(),
21         // or that binding= is effectively mandatory.
22         if (!epoxy_is_desktop_gl()) {
23                 return false;
24         }
25         if (epoxy_gl_version() >= 43) {
26                 // Supports compute shaders natively.
27                 return true;
28         }
29         return epoxy_has_gl_extension("GL_ARB_compute_shader") &&
30                epoxy_has_gl_extension("GL_ARB_shader_image_load_store");
31 }
32
33 void v210Converter::precompile_shader(unsigned width)
34 {
35         unsigned num_local_work_groups = (width + 5) / 6;
36         if (shaders.count(num_local_work_groups)) {
37                 // Already exists.
38                 return;
39         }
40
41         char buf[16];
42         snprintf(buf, sizeof(buf), "%u", num_local_work_groups);
43         string shader_src = R"(#version 150
44 #extension GL_ARB_compute_shader : enable
45 #extension GL_ARB_shader_image_load_store : enable
46 layout(local_size_x = )" + string(buf) + R"() in;
47 layout(rgb10_a2) uniform restrict readonly image2D inbuf;
48 layout(rgb10_a2) uniform restrict writeonly image2D outbuf;
49 uniform int max_cbcr_x;
50 shared vec2 cbcr[gl_WorkGroupSize.x * 3u];
51
52 void main()
53 {
54         int xb = int(gl_LocalInvocationID.x);  // X block.
55         int y = int(gl_GlobalInvocationID.y);  // Y (actual line).
56
57         // Load our pixel group, containing data for six pixels.
58         vec3 indata[4];
59         for (int i = 0; i < 4; ++i) {
60                 indata[i] = imageLoad(inbuf, ivec2(xb * 4 + i, y)).xyz;
61         }
62
63         // Decode Cb and Cr to shared memory, because neighboring blocks need it for interpolation.
64         cbcr[xb * 3 + 0] = indata[0].xz;
65         cbcr[xb * 3 + 1] = vec2(indata[1].y, indata[2].x);
66         cbcr[xb * 3 + 2] = vec2(indata[2].z, indata[3].y);
67         memoryBarrierShared();
68
69         float pix_y[6];
70         pix_y[0] = indata[0].y;
71         pix_y[1] = indata[1].x;
72         pix_y[2] = indata[1].z;
73         pix_y[3] = indata[2].y;
74         pix_y[4] = indata[3].x;
75         pix_y[5] = indata[3].z;
76
77         barrier();
78
79         // Interpolate the missing Cb/Cr pixels, taking care not to read past the end of the screen
80         // for pixels that we use for interpolation.
81         vec2 pix_cbcr[7];
82         pix_cbcr[0] = indata[0].xz;
83         pix_cbcr[2] = cbcr[min(xb * 3 + 1, max_cbcr_x)];
84         pix_cbcr[4] = cbcr[min(xb * 3 + 2, max_cbcr_x)];
85         pix_cbcr[6] = cbcr[min(xb * 3 + 3, max_cbcr_x)];
86         pix_cbcr[1] = 0.5 * (pix_cbcr[0] + pix_cbcr[2]);
87         pix_cbcr[3] = 0.5 * (pix_cbcr[2] + pix_cbcr[4]);
88         pix_cbcr[5] = 0.5 * (pix_cbcr[4] + pix_cbcr[6]);
89
90         // Write the decoded pixels to the destination texture.
91         for (int i = 0; i < 6; ++i) {
92                 vec4 outdata = vec4(pix_y[i], pix_cbcr[i].x, pix_cbcr[i].y, 1.0f);
93                 imageStore(outbuf, ivec2(xb * 6 + i, y), outdata);
94         }
95 }
96 )";
97
98         Shader shader;
99
100         GLuint shader_num = movit::compile_shader(shader_src, GL_COMPUTE_SHADER);
101         check_error();
102         shader.glsl_program_num = glCreateProgram();
103         check_error();
104         glAttachShader(shader.glsl_program_num, shader_num);
105         check_error();
106         glLinkProgram(shader.glsl_program_num);
107         check_error();
108
109         GLint success;
110         glGetProgramiv(shader.glsl_program_num, GL_LINK_STATUS, &success);
111         check_error();
112         if (success == GL_FALSE) {
113                 GLchar error_log[1024] = {0};
114                 glGetProgramInfoLog(shader.glsl_program_num, 1024, NULL, error_log);
115                 fprintf(stderr, "Error linking program: %s\n", error_log);
116                 exit(1);
117         }
118
119         shader.max_cbcr_x_pos = glGetUniformLocation(shader.glsl_program_num, "max_cbcr_x");
120         check_error();
121         shader.inbuf_pos = glGetUniformLocation(shader.glsl_program_num, "inbuf");
122         check_error();
123         shader.outbuf_pos = glGetUniformLocation(shader.glsl_program_num, "outbuf");
124         check_error();
125
126         shaders.emplace(num_local_work_groups, shader);
127 }
128
129 void v210Converter::convert(GLuint tex_src, GLuint tex_dst, unsigned width, unsigned height)
130 {
131         precompile_shader(width);
132         unsigned num_local_work_groups = (width + 5) / 6;
133         const Shader &shader = shaders[num_local_work_groups];
134
135         glUseProgram(shader.glsl_program_num);
136         check_error();
137         glUniform1i(shader.max_cbcr_x_pos, width / 2 - 1);
138         check_error();
139
140         // Bind the textures.
141         glUniform1i(shader.inbuf_pos, 0);
142         check_error();
143         glUniform1i(shader.outbuf_pos, 1);
144         check_error();
145         glBindImageTexture(0, tex_src, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGB10_A2);
146         check_error();
147         glBindImageTexture(1, tex_dst, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGB10_A2);
148         check_error();
149
150         // Actually run the shader.
151         glDispatchCompute(1, height, 1);
152         check_error();
153
154         glUseProgram(0);
155         check_error();
156 }