Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Examples of Depth Buffer - DirectX?

This thread is locked; no one can reply to it. rss feed Print
Examples of Depth Buffer - DirectX?
thebignic
Member #14,419
July 2012

I'm new to HLSL and DirectX and trying to setup a depth buffer but not really getting anywhere. Are there any Allegro examples around?

Attached is image of the issue. Sprites draw ok, the depth value is going to the pixel shader ok (as evidenced by it visualizing correctly) but the depth is not actually stored in the depth buffer (or DX is not testing against the buffer at all??) The image shows darker values for depth further away from the camera. The characters are drawn from top to bottom, but the bottom character has depth that should set it behind all of the others - clearly it doesn't.

Display setup:

#SelectExpand
1 IDirect3DDevice9 *d3dd; 2 3 ... 4 display = al_create_display(...) 5 ... 6 7al_set_new_display_flags(ALLEGRO_PROGRAMMABLE_PIPELINE | ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE | ALLEGRO_DIRECT3D); 8al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 16, ALLEGRO_SUGGEST); 9 10d3dd = al_get_d3d_device(display); 11d3dd->SetRenderState(D3DRS_AMBIENT, 0x11111111); 12d3dd->SetRenderState(D3DRS_LIGHTING,false); 13d3dd->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); 14d3dd->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE); 15d3dd->SetRenderState(D3DRS_ZFUNC, D3DCMP_GREATEREQUAL); 16///also tried D3DCMP_LESS, and default to no avail

To enable depth drawing:

  d3dd->SetRenderState(D3DRS_ZWRITEENABLE , D3DZB_TRUE);

Before I draw an entity, I set the draw depth like so:

  al_set_shader_float("depth", y);

Pixel Shader (modified from Allegro default)

#SelectExpand
1bool al_use_tex; 2texture al_tex; 3 4sampler2D s = sampler_state { 5 texture = <al_tex>; 6}; 7 8float depth=0; 9bool useDepth; 10 11struct PS_OUTPUT 12{ 13 float4 Color : COLOR0; 14 float Depth : DEPTH; 15}; 16 17 18 19PS_OUTPUT ps_main(VS_OUTPUT Input) 20{ 21 22 PS_OUTPUT psout; 23 psout.Depth = 1.0; 24 25 float2 screenPos = Input.ScreenPos.xy / Input.ScreenPos.w; 26 screenPos = (screenPos + 1.0)/2; 27 screenPos.y = 1.0 - screenPos.y; 28 29 if (al_use_tex) { 30 psout.Color = Input.Color * tex2D(s, Input.TexCoord); 31 psout.Depth = depth; 32 } 33 34 35 ////VISUALIZE DEPTH BUFFER 36 if (screenPos.x < 0.5 && psout.Color.a > 0.5 && useDepth) { 37 psout.Color.r = depth; 38 psout.Color.g = depth; 39 psout.Color.b = depth; 40 } 41 42 return psout; 43}

Vertex:

#SelectExpand
1struct VS_INPUT 2{ 3 float4 Position : POSITION0; 4 float2 TexCoord : TEXCOORD0; 5 float4 Color : TEXCOORD1; 6}; 7struct VS_OUTPUT 8{ 9 float4 Position : POSITION0; 10 float4 Color : COLOR0; 11 float2 TexCoord : TEXCOORD0; 12 float4 ScreenPos : TEXCOORD1; 13 14}; 15 16 17float4x4 al_projview_matrix; 18bool al_use_tex_matrix; 19float4x4 al_tex_matrix; 20 21VS_OUTPUT vs_main(VS_INPUT Input) 22{ 23 VS_OUTPUT Output; 24 Output.Color = Input.Color; 25 if (al_use_tex_matrix) { 26 Output.TexCoord = mul(float4(Input.TexCoord, 1.0f, 0.0f), al_tex_matrix).xy; 27 } else { 28 Output.TexCoord = Input.TexCoord; 29 } 30 Output.Position = mul(Input.Position, al_projview_matrix); 31 32 ///because we cant apparently used position0 in shader.. 33 Output.ScreenPos = Output.Position; 34 35 return Output; 36}

But it does not appear to be drawing to the depth buffer and I'm not sure how to visualize the contents of the depth buffer to even see what's being drawn.
I don't know if the buffer is setup correctly.
I don't know if my shaders are setup correctly.
I don't know if I'm missing some DirectX specific calls to setup or enable drawing.

I know that setting the depth in fragment shader means that I'll lose efficiency, but this is the only way I can conceive of to draw 2D sprites with a specific depth?

Go to: