Sign in to follow this  
davsimp

Expanding 4 pixels components into new pixels

Recommended Posts

Hi Everyone,

I've got a problem I hoped would be simple..

I've got a GL texture (that is storage for an FBO and exists only in VRAM) that contains 4 alpha values, packed in RGBA components. I need to expand that out to another fbo target so that each component in turn gets converted into a new pixel with 0,0,0,x. This means the final fbo will be 4 times wider horizontally than the input. Simply scaling it obviously won't work because i need the exact alpha value from the original texture.

I'd like to do this without a pixel shader (if possible) -- does anyone have any suggestions of how to do it efficiently, with or without a shader.

Thanks alot!

Share this post


Link to post
Share on other sites
Is this purely a technique to save vram? I'm certain you couldn't achieve the unpack you're asking for without a pixel shader, and even with one I'm having a hard time thinking of an exact solution that doesn't have floating point rounding issues that require a lot of extra instructions to address.

If you are trying to save vram, couldn't you just convert your image from a 4 channel to a 1 channel image of the correct resolution? It should require the same amount of memory.

Or can you explain more what is going on? Does your packed texture dynamically change? Is it static? Why is it 1/4 the width? If I understood better your situation I could maybe have a better idea.

Share this post


Link to post
Share on other sites
Of course - sorry for being so brief.

I'm implementing drop shadow in a 2D application, and I'm only blurring the alpha component of four pixels at once to save bandwidth; it's a multiple pass shader that is very expensive, so the 4 at once really saves time.

Share this post


Link to post
Share on other sites
A shader can only output one pixel. (MRT makes this an exception but that's sooo not a solution for your current problem)

So you must read the values from your source as you step across something that's the final size. Basically, you use your packed format for a "read" operation later on, as opposed to using it for a "write" operation.

Share this post


Link to post
Share on other sites
When you render a fullscreen quadm tehn this could work (pseudo-glsl):


// tex_coord, texture width = 4*source texture width
varying vec2 tex_coord;

// remap texture coord to source texture
vec2 new_tex_coord = tex_coord * vec2(4.0,1.0);
int index = int(floor(new_tex_coord.x));
new_tex_coord.x = fract(tmp_tex_coord);

// get alpha values
vec4 alpha_tex_values = tex2d(texture, new_tex_coord);

const vec4 filters = {vec4(0,0,0,1),vec4(0,0,1,0),vec4(0,1,0,0),vec4(1,0,0,0)};

// calculate final alpha
float final_alpha = dot(filters[index],alpha_tex_values);



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this