#version 450

layout (local_size_x = 16, local_size_y = 16) in;

layout (set = 0, binding = 0) uniform sampler2D inputImage;
layout (set = 1, binding = 1, rgba8) uniform image2D resultImage;

layout(push_constant) uniform constants
{
	uint width;
	uint height;
} image_size;

void main()
{   
    ivec2 pixelCoords = ivec2(gl_GlobalInvocationID.xy);
    if(pixelCoords.x >= image_size.width || pixelCoords.y >= image_size.height){
        return;
    }

    ivec2 textureCoords = ivec2(pixelCoords.x / 2, pixelCoords.y);

    vec4 yuv = texelFetch(inputImage, textureCoords, 0).grba;
    if(pixelCoords.x % 2 == 1){
        yuv.r = yuv.a;
    }

    float Y_SCALED = 1.1643835;
    float R_CR_709 = 1.7926522;
    float G_CB_709 = -0.21323606;
    float G_CR_709 = -0.5330038;
    float B_CB_709 = 2.11242;

    yuv.r = Y_SCALED * (yuv.r - 0.0625);
    yuv.g = yuv.g - 0.5;
    yuv.b = yuv.b - 0.5;
    float r = yuv.r + R_CR_709 * yuv.b;
    float g = yuv.r + G_CB_709 * yuv.g + G_CR_709 * yuv.b;
    float b = yuv.r + B_CB_709 * yuv.g;

    imageStore(resultImage, pixelCoords, vec4(r, g, b, 1));
}




