RT depth values

Started by
8 comments, last by dreddlox 19 years, 7 months ago
hihi! i was wondering how do u pple get actual depth values frmo the render target? so far i know of one way that is to hardcode a depth value into a color component via the pixel shader. But is there another "more direct" way?? thx! Edwinz
Advertisement
Depending on your card, reading back the z buffer is either impossible or horribly, horribly slow.

The long and short of it is just don't do it. Recent video cards allow multiple render targets though, so if you have one, and you need to use the alpha channel of the primary target for blending then you could create a second render target just for the depth values.
hi AP!
I am using it for debugging, so do not worry abt actual performance.

I jus need to find out how to get the depth values directly, without using a pixel shader, jus like GL_DEPTH_COMPONENT.

EDIT: i wonder if locking it jus like performing texture readbacks via surfaces, could help...

thx!
Edwinz
I think you'll have to get the backbuffer, and lock the surface and read&write to another surface/texture.

See Coders post:

[Edited by - Pipo DeClown on September 4, 2004 10:52:35 AM]
IDirect3DDevice9::GetDepthStencilSurface gives you a surface, IDirect3DSurface9::LockRect locks a rect on it.

ok yupz!
I got those separate deth surfaces done.

I havent inspect the values yet, coz i need to know how to save a depth stencil surface to a file first, then load up in my own Image Debugger...

simply, can depth stencil surfaces be saved jus like normal texture's surface??

somehow i tried, but it gave an error.
Direct3D9: (ERROR) :This format is not supported for SystemMem, Scratch or Managed textures

I am using D24S8 for the surface;

thx!
Edwinz
You can't save a depth surface to a file like a normal texture surface. The reason is that each card can implement depth surfaces in anyway they see fit for optimal performance.

I take it that you're using an Nvidia card because no other vender supports locking the depth buffer directly. You can search through Nvidia's developer site for information on retreiving data from the depth buffer and write your own save routine. The D3DXSaveSurfaceToFile type functions won't help because those functions don't know how each vender decided to implement the depth buffer.

neneboricua
HIHIHhihi!

i am using an ATI card actually.

ok i managed to do it this way:
1)copy values from depth stencil surface to a dummy texture
2)save the surface of that dummy texture
3)load my own Image Debugger and inspect that texture/surface which contains depth values

thx!
Edwinz
Hi, i've been thinking about the problem for some time now(wanting to make projected texture maps without a messy pixel shader) and I was thinking, but have yet to actually try:

The projection matrix ensures the depth buffer is a value between 0 and 1.0f, if you can use the deth buffer as a normal texture and use a pixelshader to do a backwards matrix thing onto a FP texture, it should work fine.

unfortunately my projection buffer knowledge is thin... I was a NeHe reading child...
what nauseating MSDN documents...

I found out that the z buffer's value should look something like this:

Pf = far plane
Pn = near plane
Z = input Z
ZB = value in the buffer(from 0.0 to 1.0)

ZB = (Pf*Z/(Pf-Pn) - Pf*Pn/(Pf-Pn))/Z

this is assuming that ZB = projected point.z / projected point.w

so actually finding the original Z may be quite a hassle...
Lemme try some algebra

ZB = (Z-Pn)/(Z*(Pf-Pn))
ZB = 1/(Pf-Pn) - Pn/(Z*(Pf-Pn))
Pn/((ZB - 1/(Pf-Pn))*(Pf-Pn)) = Z
Pn/(ZB*(Pf-Pn) - 1) = Z

but check my math...lots...check it till it bleeds, even I dont trust it

But if it works, that should be your Z value

Edit: formula is really:
-Pf*Pn/(ZB(Pf-Pn)-Pf) = Z

still, check it...

[Edited by - dreddlox on September 6, 2004 12:41:25 AM]

This topic is closed to new replies.

Advertisement