1 #include "v210_converter.h"
4 #include <movit/util.h>
8 v210Converter::~v210Converter()
10 for (const auto &shader : shaders) {
11 glDeleteProgram(shader.second.glsl_program_num);
16 bool v210Converter::has_hardware_support()
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()) {
25 if (epoxy_gl_version() >= 43) {
26 // Supports compute shaders natively.
29 return epoxy_has_gl_extension("GL_ARB_compute_shader") &&
30 epoxy_has_gl_extension("GL_ARB_shader_image_load_store");
33 void v210Converter::precompile_shader(unsigned width)
35 unsigned num_local_work_groups = (width + 5) / 6;
36 if (shaders.count(num_local_work_groups)) {
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];
54 int xb = int(gl_LocalInvocationID.x); // X block.
55 int y = int(gl_GlobalInvocationID.y); // Y (actual line).
57 // Load our pixel group, containing data for six pixels.
59 for (int i = 0; i < 4; ++i) {
60 indata[i] = imageLoad(inbuf, ivec2(xb * 4 + i, y)).xyz;
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();
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;
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.
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]);
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);
100 GLuint shader_num = movit::compile_shader(shader_src, GL_COMPUTE_SHADER);
102 shader.glsl_program_num = glCreateProgram();
104 glAttachShader(shader.glsl_program_num, shader_num);
106 glLinkProgram(shader.glsl_program_num);
110 glGetProgramiv(shader.glsl_program_num, GL_LINK_STATUS, &success);
112 if (success == GL_FALSE) {
113 GLchar error_log[1024] = {0};
114 glGetProgramInfoLog(shader.glsl_program_num, 1024, nullptr, error_log);
115 fprintf(stderr, "Error linking program: %s\n", error_log);
119 shader.max_cbcr_x_pos = glGetUniformLocation(shader.glsl_program_num, "max_cbcr_x");
121 shader.inbuf_pos = glGetUniformLocation(shader.glsl_program_num, "inbuf");
123 shader.outbuf_pos = glGetUniformLocation(shader.glsl_program_num, "outbuf");
126 shaders.emplace(num_local_work_groups, shader);
129 void v210Converter::convert(GLuint tex_src, GLuint tex_dst, unsigned width, unsigned height)
131 precompile_shader(width);
132 unsigned num_local_work_groups = (width + 5) / 6;
133 const Shader &shader = shaders[num_local_work_groups];
135 glUseProgram(shader.glsl_program_num);
137 glUniform1i(shader.max_cbcr_x_pos, width / 2 - 1);
140 // Bind the textures.
141 glUniform1i(shader.inbuf_pos, 0);
143 glUniform1i(shader.outbuf_pos, 1);
145 glBindImageTexture(0, tex_src, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGB10_A2);
147 glBindImageTexture(1, tex_dst, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGB10_A2);
150 // Actually run the shader.
151 glDispatchCompute(1, height, 1);