Skip to content

Instantly share code, notes, and snippets.

@ithmz
Created February 19, 2020 02:25
Show Gist options
  • Save ithmz/18fa42ed46a3cad367241c239b7b3adb to your computer and use it in GitHub Desktop.
Save ithmz/18fa42ed46a3cad367241c239b7b3adb to your computer and use it in GitHub Desktop.
Fragment shader to convert YUV420SP (a.k.a NV12 or NV21) to RGB
#version 330 core
out vec4 FragColor;
in vec2 texCoord;
uniform sampler2D textureY;
uniform sampler2D textureVU;
void main()
{
vec3 yuv, rgb;
vec3 yuv2r = vec3(1.164, 0.0, 1.596);
vec3 yuv2g = vec3(1.164, -0.391, -0.813);
vec3 yuv2b = vec3(1.164, 2.018, 0.0);
yuv.x = texture(textureY, texCoord).r - 0.0625;
yuv.y = texture(textureVU, texCoord).g - 0.5;
yuv.z = texture(textureVU, texCoord).r - 0.5;
rgb.x = dot(yuv, yuv2r);
rgb.y = dot(yuv, yuv2g);
rgb.z = dot(yuv, yuv2b);
FragColor = vec4(rgb, 1.0);
}
@AhmedX6
Copy link

AhmedX6 commented Dec 7, 2020

Hello,
When I use your shader, I'm getting green image.
Please could you share also the code that is using this shader... I'm not able to get right colors on my texture..
I got two buffers one Y (345600 size) and one UV (345600 / 2).

A little help would be appreciated thank you

@ithmz
Copy link
Author

ithmz commented Dec 7, 2020

@refvalue
Copy link

Appreciate it!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment