Using a fragment shader to convert YUV to RGB
Posted: Wed Nov 08, 2006 3:17 pm
Hi,
We have some video textures that were H264 encoded, and thus in YUV420 pixel format. As Ogre/graphics hardware can't natively use this pixel format, we have to convert it to RGB(A). I want to do (part of) this in a fragment shader, but as I have no experience in this area, I have some questions.
I've found some cg source of a possible shader in this source file. However, I'm not sure how to convert this to the Ogre world.
I've put the cg source itself (without the quotes and newlines off course) in YUVtoRGB.cg, and created this Ogre material:
I have no idea how to continue from here. I'm searching for some good reference/tutorial on using shaders in Ogre, but no success so far.
I don't think the shader is very complex, so the profile I've chosen is probably way too heavy. Which parameters should I bind? Where do the gl_TexCoord and gl_FragColor variables come from? Are they automatically accessible?
What I want to do is feed the YUV data to a dynamic texture and let the fragment shader to the conversion to RGB(A).
Any help appreciated!
Greetz,
JeDi
We have some video textures that were H264 encoded, and thus in YUV420 pixel format. As Ogre/graphics hardware can't natively use this pixel format, we have to convert it to RGB(A). I want to do (part of) this in a fragment shader, but as I have no experience in this area, I have some questions.
I've found some cg source of a possible shader in this source file. However, I'm not sure how to convert this to the Ogre world.
I've put the cg source itself (without the quotes and newlines off course) in YUVtoRGB.cg, and created this Ogre material:
Code: Select all
uniform sampler2DRect Ytex;
uniform sampler2DRect Utex,Vtex;
void main(void) {
float nx,ny,r,g,b,y,u,v;
vec4 txl,ux,vx;
nx=gl_TexCoord[0].x;
ny=576.0-gl_TexCoord[0].y;
y=texture2DRect(Ytex,vec2(nx,ny)).r;
u=texture2DRect(Utex,vec2(nx/2.0,ny/2.0)).r;
v=texture2DRect(Vtex,vec2(nx/2.0,ny/2.0)).r;
y=1.1643*(y-0.0625);
u=u-0.5;
v=v-0.5;
r=y+1.5958*v;
g=y-0.39173*u-0.81290*v;
b=y+2.017*u;
gl_FragColor=vec4(r,g,b,1.0);
}
Code: Select all
fragment_program YUVtoRGB cg
{
source YUVtoRGB.cg
entry_point main
profiles ps_2_0 arbfp1
//default_params
//{
//}
}
material test/video
{
technique
{
pass
{
texture_unit
{
texture test_video
tex_address_mode clamp
}
fragment_program_ref YUVtoRGB
{
}
}
}
}
I don't think the shader is very complex, so the profile I've chosen is probably way too heavy. Which parameters should I bind? Where do the gl_TexCoord and gl_FragColor variables come from? Are they automatically accessible?
What I want to do is feed the YUV data to a dynamic texture and let the fragment shader to the conversion to RGB(A).
Any help appreciated!
Greetz,
JeDi