3D Texture Gradient computation on the fly

Hi,

I’ve been wondering if given a 3D texture and a shader pointing to one of its voxel, I could compute the gradient on the fly(most likely I’ll be using a central difference).

After looking at the GLSL spec, I found out the noise an backward/forward differences functions but I cannot apply them here.
Someone told me it could be done but couldn’t say how…

Does anyone knows about this technique?

Thanks, Jeff.

as far as i understood your problem, if you want to caculate the gradient, you’ve got to access the neighboring voxels. you can do this by accessing the texture with an offset on the texture coordinate.

That is what I do in fact.

I’d like to have optimized shaders, so here is my real problem :
The code can handle any format of 3D textures, I mean any width height and depth as far as the hardware supports it. So the offsets are 1/width, 1/height and 1/depth. Actually I send those as uniforms in my shaders so I provide the correct offsets.
Isn’t there a built in function for fetching the correct voxels in hardware?

Or do you have an idea for doing this without much performance impact?

Thanks,
Jeff.

Originally posted by funkeejeffou:
[b]That is what I do in fact.

I’d like to have optimized shaders, so here is my real problem :
The code can handle any format of 3D textures, I mean any width height and depth as far as the hardware supports it. So the offsets are 1/width, 1/height and 1/depth. Actually I send those as uniforms in my shaders so I provide the correct offsets.
Isn’t there a built in function for fetching the correct voxels in hardware?

Or do you have an idea for doing this without much performance impact?

Thanks,
Jeff.[/b]
sorry, i didn’ really understand this. texture3D function cannot do that?
i did something similar but for 2D. to caculate the gradient is slow. there must be many texture lookups performed. i don’t know why you need thegradient values. i think you should coose a algorithm which needs the least gradient values. and also you need to reduce the number of uniforms you pass to the shader. for instance you can use a 2D vector to pass the height and with insdead of seperately. i also obsered, that iterations and conditions in shaders are slow. if want to write a iteration, i would rather write line by line instead of a loop.
i ain’t sure if i can help you. wish you good luck.

Thanks for the reply.

The gradient I do is called the central difference, so if (x,y,z) is my voxel’s coordinate, than the 3D gradient of it will be :
gx = (x+1) - (x-1);
gy = (y+1) - (y-1);
gz = (z+1) - (z-1);

In opengl the texture coordinates are normalised to [0,1], so if I wanna fetch the correct neighboors, I must use the ratios 1/width, 1/height and 1/depth.

Example :
If my current voxel’s coordinates are (s,t,u) in normalised texture space, than my left neighboor voxel(x+1) will have the coordinate (s+1/width, t, u).

It would have been cool to have a built in function to fetch neighboors but I guess there aren’t any. There are the dFdx and dFdy in the GLSL spec but I am not sure what they are meant to be. Anybody knows?

Thanks for the tip about sending less uniforms, I didn’t know that.

Cheers, Jeff.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.