Sign in to follow this  
ZealousEngine

Dynamic loops and ps3.0 (CG)

Recommended Posts

ZealousEngine    100
Im using cg2.0 ps3.0, and im having trouble doing a simple dynamic loop...
Quote:
for ( int step = 0; step < stepNum; ++step ) { ... tex3D( noiseTex, sampUV ); }
The trouble actually doesnt seem to be the loop, but rather the 'scrolling sample' INSIDE the loop. Im trying to march a ray through a 3d volume, but ive heard you CANT scroll uvs inside a dynamic loop? Sure enough using a STATIC sample inside the loop does work (of course that does me no good though). Any idea what im doing wrong?

Share this post


Link to post
Share on other sites
jamesw    400
Here's two things to try:

Put an upper limit on the loop count. As in 'step < stepNum && step < 4'.

Use a tex3D function where you supply the gradients (not sure if there is one for Cg) and do the ddx and ddy outside of the loop.

Let me know if that works.

Share this post


Link to post
Share on other sites
ZealousEngine    100
Adding the upper limit to my loop didnt do anything.

Not sure what you mean by 'supplying my own gradient'.. ive never used tex3d/2d in any other way than (sample, uvcoord). And what are ddx/ddy?

Share this post


Link to post
Share on other sites
jamesw    400
There are tex functions where you supply the gradient. In HLSL it would be like this:

Quote:

float DeltaX = ddx(sampUV).x;
float DeltaY = ddy(sampUV).y;
for ( int step = 0; step < stepNum; ++step ) {
...
tex3Dgrad(noiseTex, sampUV, DeltaX, DeltaY);
}

Share this post


Link to post
Share on other sites
MxADD    499
If you are sampling texture inside dynamic loop/branch
AND you want corect mip-maps you need to pass own gradients to the sampling function.
The deal witch gradients is that they are computed not only from values
from pixel you are actually shading but from neighbour pixels too,
so if one pixel takes one part of a branch and its neighbour another,
then there is a trouble computing gradients inside conditional expression.

If you don't care about mipmaps or want to calculate mipmap filter on your own
use tex3Dlod instruction (the forth parameter is a mipmap level you wanna sample from).

If you need mipmaps use tex3Dgrad instruction and support ddx/ddy calculated
outside any dynamic conditional expression.


Share this post


Link to post
Share on other sites
ZealousEngine    100
Hmm well im doing volume rendering (GPU ray marching). It sounds like I need to pass some information so the sampler knows to use 'x' mipmap when sampling distant points along the ray (deep into the volume texture), and then use 'y' mipmap when sampling close up points along the ray. Right?

So imagine youre marching a ray through a 3d volume texture. How can you use ddx/y() to calculate these 'gradients'? If the purpose is to determine mipmap levels, wouldnt you need to do this calculation EVERY step along the ray (since the depth is constantly changing?)?

*edit @james - wouldnt I have to calculate those deltas every loop though? since the sampUV.x/y are constantly changing inside the loop?

[Edited by - ZealousEngine on February 21, 2008 11:58:27 AM]

Share this post


Link to post
Share on other sites
ZealousEngine    100
Well, the following 'works' (cg)...

Quote:

int stepMax = 'the max steps to take along this ray (based on volume depth)'
float3 uvSart = 'the point where the ray intersects the front face (texture space)'
float3 uvStep = 'the vector/step to take when sampling the volume texture'
float2 uvDelta; uvDelta.x = ddx( uvStart ).x; uvDelta.y = ddy( uvStart ).y;

for ( int step = 0; step < stepMax; ++step ) {

//sample the volume at this point
float temp = tex3D( noiseTex, uvStart + (uvStep*step), uvDelta.x, uvDelta.y );

//if the sample point is > than some value, we have hit a 'solid' point
if ( temp > 0.8 ) { outColor = temp; break; }

}


I just cant wrap my brain around whats going on. So what exactly is the ddx/y doing? And whatever value its generating, how can it be relevant for anything other than the FIRST sample along the ray? Once the ray starts marching, shouldnt we have to recalculate ddx/y for the NEW uvcoords?

I mean if the point is to determine what mipmap to use, doesnt this mean were using the SAME mipmap for EVERY sample along the ray? That seems ineficient (seems like we want to sample a LOW mipmap for distant points, and a HIGH mipmap for closer points).

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