PDA

View Full Version : Array output from Fragment Shader ?



prash_prash
01-13-2011, 03:39 AM
Hello,

I need to perform RGB to YUV conversion in the fragment shader.

I cannot figure it out, but is it possible to output the YUV values into an array from the fragment shader?

thanks!

Dark Photon
01-13-2011, 04:06 PM
Yes, pretty easily, but where are you going with this? Seems like we're missing something.

Is your goal just to render those YUVs into a 3-channel RGB texture or renderbuffer?

prash_prash
01-14-2011, 09:26 AM
I want to render the yuv to a framebuffer, but my final task in the main program is to store the yuv image from the rgb data.

I read about gl_FragData today and plan to use it.

I am not sure if my approach is efficient, but I am doing the following:

1. I get the RGB texture in the fragment shader.
2. Calculated the y, u and v from it in the shader.
3. Now i plan to write the separate y,u and v, into gl_FragData[0], glFragData[1], and gl_FragData[2] so that I can work on it in the main program.

Please comment on the approach and provide suggestions.

thanks.

Alfonse Reinheart
01-14-2011, 09:53 AM
Now i plan to write the separate y,u and v, into gl_FragData[0], glFragData[1], and gl_FragData[2] so that I can work on it in the main program.

Why? Is there some reason that putting the YUV values in the RGB values of a single output image wouldn't work for you? You're just taking what should be a single glReadPixels operations and turning it into 3.

prash_prash
01-16-2011, 03:05 PM
Now i plan to write the separate y,u and v, into gl_FragData[0], glFragData[1], and gl_FragData[2] so that I can work on it in the main program.

Why? Is there some reason that putting the YUV values in the RGB values of a single output image wouldn't work for you? You're just taking what should be a single glReadPixels operations and turning it into 3.

Hello, I think my little knowledge in opengl is coming to the fore now :(

Do you imply that,

gl_FragColor = vec4(y,u,v,0.0) would be enough to send the yuv values out of the fragment shader?

But I believe, if I display this value in the main program, this would be interpreted as RGB value, even though I put YUV into it.

Specifically, my question at this stage is, what do I do next after putting in yuv in gl_FragColor ?

Alfonse Reinheart
01-16-2011, 03:26 PM
But I believe, if I display this value in the main program, this would be interpreted as RGB value, even though I put YUV into it.

Of course it will.

If you are generating YUV color values, and you want to display them to the user, then you must convert them from the YUV colorspace to the RGB one. The Wikipedia article on YUV has the math for this. If you are generating YUV color values and want to do something with them on the CPU, or use them as a texture for later lookups, then simply outputting the YUV values is fine.

prash_prash
01-16-2011, 03:43 PM
Of course it will.

If you are generating YUV color values, and you want to display them to the user, then you must convert them from the YUV colorspace to the RGB one. The Wikipedia article on YUV has the math for this. If you are generating YUV color values and want to do something with them on the CPU, or use them as a texture for later lookups, then simply outputting the YUV values is fine.

I believe I am using the RGB to YUV equations correctly.

My shader looks like this:
----------------------------------------
vec4 pixel = texture2D(texfromRGB, texture_coordinate);
float r = float(pixel.r); //breaking down into r, g, b values

float g = float(pixel.g);
float b = float(pixel.b);

float y = clamp((r * 0.299) + (g * 0.587) + (b * 0.114), 0.6274, 0.92156);

float u = 0.565 * (b - y);
float v = 0.713 * (r - y);

gl_FragColor = vec4(y, u, v, 0.0);
---------------------------------------------

So, my understanding is that when I lay my hands on this value in the main program, I do have the YUV data of the image. Is it?

Can I simply write the texture data to a file and it will be my YUV image ?