I made a shader to display a YUYV surface (data arranged as: Y0 U0 Y1 V0). It's working, but I don't know anything about shaders, or openGL, or anything graphic related as a matter of fact. I feel more like a mad scientist patching random bits of code together than a programmer. Thus, I would appreciate if anyone could look it up and tell me what I could improve, performance-wise, logic-wise or syntax-wise. I also have some questions:
- What desktop hardware will that work on? Is there any restriction, or will it work on any 10 years old desktop?
- Would that work on an android tablet?
- Will any interpolation of pixel be performed, if my rendering surface is the same size as the texture? Is it possible to have interpolation (with some flag, not by implementing it)?
I load my YUYV texture as RGBA texture of width/2
by height
. The rest should be self-explanatory:
#version 330 core
uniform sampler2D tex;
uniform mat4 convMat; /* Color conversion matrix */
uniform vec2 dim; /* Width and height of image */
in vec2 UV;
out vec3 color;
void main(){
vec4 yuv;
ivec2 iUV = ivec2(UV*dim*vec2(0.5, 1)); /* Absolute coordinate of desired pixel in texture */
yuv = texelFetch(texy, iUV, 0);
if (int(UV.x*dim.x) % 2 == 1){
/* There are 2 Y in a texture RGBA pixel, choose the right one based on absolute pixel coordinate */
yuv.x = yuv.z;
};
yuv.z = yuv.w;
yuv.w = 1;
color = (convMat*yuv).rgb;
};