Created
May 18, 2016 05:58
-
-
Save varphone/e21618adcdb687ce5c6cf54021593949 to your computer and use it in GitHub Desktop.
YUV to RGBA GL Shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uniform sampler2D texY; // Y | |
uniform sampler2D texU; // U | |
uniform sampler2D texV; // V | |
varying vec2 vary_tex_cord; | |
vec3 yuv2rgb(in vec3 yuv) | |
{ | |
// YUV offset | |
// const vec3 offset = vec3(-0.0625, -0.5, -0.5); | |
const vec3 offset = vec3(-0.0625, -0.5, -0.5); | |
// RGB coefficients | |
const vec3 Rcoeff = vec3( 1.164, 0.000, 1.596); | |
const vec3 Gcoeff = vec3( 1.164, -0.391, -0.813); | |
const vec3 Bcoeff = vec3( 1.164, 2.018, 0.000); | |
vec3 rgb; | |
yuv = clamp(yuv, 0.0, 1.0); | |
yuv += offset; | |
rgb.r = dot(yuv, Rcoeff); | |
rgb.g = dot(yuv, Gcoeff); | |
rgb.b = dot(yuv, Bcoeff); | |
return rgb; | |
} | |
vec3 get_yuv_from_texture(in vec2 tcoord) | |
{ | |
vec3 yuv; | |
yuv.x = texture(texY, tcoord).r; | |
// Get the U and V values | |
yuv.y = texture(texU, tcoord).r; | |
yuv.z = texture(texV, tcoord).r; | |
return yuv; | |
} | |
vec4 mytexture2D(in vec2 tcoord) | |
{ | |
vec3 rgb, yuv; | |
yuv = get_yuv_from_texture(tcoord); | |
// Do the color transform | |
rgb = yuv2rgb(yuv); | |
return vec4(rgb, 1.0); | |
} | |
out vec4 out_color; | |
void main() | |
{ | |
// That was easy. :) | |
out_color = mytexture2D(vary_tex_cord); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used this one