Skip to content

Instantly share code, notes, and snippets.

@wrightwriter
Created April 14, 2024 10:09
Show Gist options
  • Save wrightwriter/455df8352afda341b7d34df094b3debf to your computer and use it in GitHub Desktop.
Save wrightwriter/455df8352afda341b7d34df094b3debf to your computer and use it in GitHub Desktop.
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
uniform int input_tex;
uniform int jpg_result_tex;
shared vec3 shared_inputtex[8][8];
shared vec3 shared_dct_horiz[8][8];
shared vec3 shared_dct_vert[8][8];
shared vec3 shared_dct_vert_inv[8][8];
mat3 rgb_to_ycbcr = mat3(vec3(.299, -.168736, .5), vec3(.587, -.331264, -.418688), vec3(.114, .5, -.081312));
vec3 rgb_to_ycbcr_offset = vec3(0., .5, .5);
float dct[64] = float[64](
0.353553,0.353553,0.353553,0.353553,0.353553,0.353553,0.353553,0.353553,
0.490393,0.415735,0.277785,0.0975452,-0.0975452,-0.277785,-0.415735,-0.490393,
0.46194,0.191342,-0.191342,-0.46194,-0.46194,-0.191342,0.191342,0.46194,
0.415735,-0.0975452,-0.490393,-0.277785,0.277785,0.490393,0.0975452,-0.415735,
0.353553,-0.353553,-0.353553,0.353553,0.353553,-0.353553,-0.353553,0.353553,
0.277785,-0.490393,0.0975452,0.415735,-0.415735,-0.0975452,0.490393,-0.277785,
0.191342,-0.46194,0.46194,-0.191342,-0.191342,0.46194,-0.46194,0.191342,
0.0975452,-0.277785,0.415735,-0.490393,0.490393,-0.415735,0.277785,-0.0975452
);
mat3 ycbcr_to_rgb = mat3(vec3(1.), vec3(0., -.344136, 1.772), vec3(1.402, -.714136, 0.));
vec3 ycbcr_offset_to_rgb = vec3(0., -.5, -.5);
void main() {
ivec2 gid = ivec2(gl_GlobalInvocationID.xy);
ivec2 lid = ivec2(gl_LocalInvocationID.xy);
ivec2 id_start = gid - lid;
layout(rgba32f) coherent restrict image2D output_image = f_images[jpg_result_tex];
{
vec3 samp = texelFetch(textures[input_tex], gid.xy, 0).xyz;
samp = clamp(samp,0.0001,1.);
shared_inputtex[lid.y][lid.x] = rgb_to_ycbcr_offset + rgb_to_ycbcr * samp;
}
barrier();
{
vec3 dct_out = vec3(0);
for(int i = 0; i < 8; i++){
vec3 tex_samp = shared_inputtex[lid.y][i];
float dct_samp = dct[8*lid.x + i];
dct_out += dct_samp * tex_samp;
}
shared_dct_horiz[lid.y][lid.x] = dct_out;
}
barrier();
{
vec3 dct_out = vec3(0);
for(int i = 0; i < 8; i++){
vec3 tex_samp = shared_dct_horiz[i][lid.x];
float dct_samp = dct[8*lid.y + i];
dct_out += dct_samp * tex_samp;
}
// Quantize
{
float f = 1 * 20.;
float quantization = f * float(lid.x + lid.y + 1) + 1.f;
dct_out = round(128. * dct_out / quantization) * (quantization / 128.);
}
shared_dct_vert[lid.y][lid.x] = dct_out;
}
barrier();
{
vec3 dct_out = vec3(0);
for(int i = 0; i < 8; i++){
vec3 tex_samp = shared_dct_vert[i][lid.x];
float dct_samp = dct[8*i + lid.y];
dct_out += dct_samp * tex_samp;
}
shared_dct_vert_inv[lid.y][lid.x] = dct_out;
}
barrier();
vec3 dct_out = vec3(0);
{
for(int i = 0; i < 8; i++){
vec3 tex_samp = shared_dct_vert_inv[lid.y][i];
float dct_samp = dct[8*i + lid.x];
dct_out += dct_samp * tex_samp;
}
}
barrier();
vec4 s = texelFetch(textures[input_tex], gid.xy, 0);
vec3 C = ycbcr_to_rgb * (dct_out+ ycbcr_offset_to_rgb);
imageStore(output_image, gid.xy, vec4(C,1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment