Funny Depth Value Behaviour

Started by
12 comments, last by GameDev.net 19 years, 6 months ago
Hi all, first off, im using Cg on an ATI Radeon 9500 Cat 4.7 WinXP SP2. the problem, it seems that the depth value for a fragment is not being interpolated properly or something (this is not an ATI driver bug, checked this with a 5900XT) basically here is my CgVP

struct appdata {
	float4 position : POSITION;
};

struct V2FI {
	float4  pos:  POSITION;
	float2 tex0:  TEXCOORD0;
	float  npos:  TEXCOORD1;
};

V2FI main( appdata IN, uniform float4x4 matMVP ) {
	V2FI OUT;

	float4 p = mul( matMVP, IN.position );
	OUT.pos = p;
	OUT.npos = p.z;
	OUT.tex0 = float2(0,0);

	return OUT;
}


and all i do is get the interpolated Z value in my FP by a simple
 float depth = IN.pos.z
I've tried: 1. dividing the depth by IN.pos.w, nothing 2. tried hard coded values to see if the rest of the FP is working (it is.) 3. used some color debugging, eg. if depth value < x then fragment = blue else fragment = red, behaves incorrectly when rendering my scene, but is correct for hardcoded values. Odd things: 1. if i use "depth = IN.npos" i get totaly different results (still incorrect though) 2. this should be a very simple task, i have no lighting or textures, just a sphere (which is called from a DL btw) any pointers? i'd be glad to provide more code if need be, but prefer not to. Cheers, Danu PS. are there any Pixel Shader debuggers around (appart from NV-FXComposer)?
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Advertisement
You don't mention what you want to do with the depth value. Anyway, you want to get the depth value, but in what space? Object space,eye,clip coordinates, window coordinates(the value that is written in the z-buffer) ?
im doing a DOF blur (Depth of Field) so i want window coords sorry (ie. Z buffer value of that fragment)

basically i need to test if a fragment's z is infront or behind the focal "plane" (its just a single float value)

is this safe to do?

Cheers,
Danu
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
I don't know much about CG, but in GLSL and fragment shaders you can access the window coordinates with gl_FragCoord, and you can obtain the depth value with gl_FragCoord.z . I think there is a WPOS input variable in CG, or am I wrong?

If that doesn't exist, you can compute the depth value yourself in the vertex shader and interpolate it. So far you've computed the vertex position (Xc,Yc,Zc,Wc) in clip coordinates.
You have to:

1)Perspective division:
Zd=Zc/Wc;

2)Window coordinates:
Zw=[(f − n)/2]Zd + (n + f)/2
n,f are the values you supply to glDepthRange(), so they will probably be n=0,f=1.

Zw is the depth value in window coordinates, so you can interpolate it and retrieve it in the fragment program.






[Edited by - mikeman on October 5, 2004 6:33:16 AM]
thanks, ill try that
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
i was looking at an ATI presentation on DOF (DX HLSL) they
sample the POS.z value and send it to the fragment sh just like
how im doing it...

odd, do you think D3D HLSL and Cg would behave diff in this respect?

Cheers
Danu
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Quote:Original post by silvermace
i was looking at an ATI presentation on DOF (DX HLSL) they
sample the POS.z value and send it to the fragment sh just like
how im doing it...

odd, do you think D3D HLSL and Cg would behave diff in this respect?

Cheers
Danu


It all depends on the coordinate system you decide to use. You say you need to compare the fragment's z value to a focal plane,which I assume you supply. If you express the focal plane in eye coordinates, you'll need the fragments pos in eye coordinates. If you express it in window coords, you'll need the fragment's window zvalue, and so on.
hi mike,

i tried the window coord calculation, still dosnt work
the fragments color just comes out plane white, when it should
be an "alpha" value ranging from 0-1

heres all the Cg Code:
// Vertex Programstruct appdata {  float4 position : POSITION;};struct V2FI {  float4 pos: POSITION;  float fDepth: TEXCOORD0;};V2FI main( appdata IN, uniform float4x4 matMVP ){	V2FI OUT;	float4 p = mul( matMVP, IN.position );	OUT.pos = p;	float Zd = p.z / p.w;	float f = 1;	float n = 0;	float z = ((f - n)/2) * Zd + ((n + f)/2);	OUT.fDepth = z;	return OUT;}// Fragment Programstruct V2FI {	float4 pos: POSITION;	float  fDepth: TEXCOORD0;};float4 main( V2FI vin, 		uniform float near, 		uniform float far, 		uniform float focalPlane,		uniform float clampBlurFactor ) : COLOR{	float depth = vin.fDepth;	float f;	if( depth < focalPlane ) {		f = (depth - focalPlane) / (focalPlane - near);	}	else {		f = (depth - focalPlane) / (far - focalPlane);		f = clamp(f, 0, clampBlurFactor);	}	// range compress to 'alpha'	f = f * 0.5f + 0.5f;	return f;}


funny thing is the fragment program is straight out of an ATI DOF presentation, they say that "depth" should be in "view" coordinates (i beleive this is World * View * Proj == Modelview * Projection)

Thanks for your help on this btw, Rate++.

-Danu
You get eye coordinates if you multiply with the ModelView matrix. The way you're doing it(MVP*vector), you get clip coordinates.

Also remember, the fragment z-value will be negative,due to the coordinate system GL uses. I'm not sure, but the calculations seem to require positive values, and you probably supply the focal plane as positive too, right? In that case, just do "float depth = -vin.fDepth;" in the fragment program to adjust things right.

I suppose the "far" and "near" values are the far and near planes as you define them in gluPerspective.
i changed the compare signs and now i interpolate
fDepth = mul( matMV, IN.pos ).z

still dosn't work, there is absolutly no change in depth
value when im using glTranslate or glRotate on the scene

i also check that i am binding the Cg shader and matricies
AFTER transformations.

Any more ideas?

Cheers,
Danu

PS. you've been really patient, i really appreciate it.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website

This topic is closed to new replies.

Advertisement