Q:

Direct3d 9 z-buffer fails drastically

I have just completed my custom mesh class and my engine is very basic right now, but now I am facing this strange issue. I have posted the pictures bellow, it seems like z-buffer is not working properly. I am using the following for projection matrix
D3DXMatrixPerspectiveFovLH((D3DXMATRIX*)&m_proj, toRad( 45.f ), aspect_ratio, 0.f, 100.0f );

and this is my clear call
void Context::clear( Vec4& color )
{
    if( this->m_is_lost ) return;
    this->m_device->Clear(0, NULL , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB(0,0,50), 1.0, 0);
}

If I give anything other than 0 for near plane the model barely shows up and if camera is zoomed the geometry seems to be appearing partly and disappearing. I know that my view matrix is ok because if I use 0 near plane the geometry is rendered correctly but then the flickering happens. I used simple HLSL posted bellow. If someone faced the same situation please assist me..
EDIT : I used blender to export mesh to custom format, although unlikely but is there any winding order issue causing this problem??
void GameApp::renderScene()
{
LPDIRECT3DDEVICE9 dev = ((LPDIRECT3DDEVICE9)Engine::Context()->ptr());
HRESULT hr = 0;
Matrix u;
bool res;

w = u;
v = m_cam->view_copy();
p = *Engine::DefaultView()->fov();

Matrix wvp = w * v * p;

hr = dev->SetRenderState(D3DRS_DITHERENABLE, TRUE);
hr = dev->SetRenderState( D3DRS_LIGHTING, FALSE );
//dev->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
hr = dev->SetRenderState( D3DRS_ZENABLE, TRUE );
hr = dev->SetRenderState( D3DRS_STENCILENABLE, FALSE );

dev->SetTransform( D3DTS_PROJECTION, (D3DXMATRIX*)&p);

res = this->sh->vs_wvp( wvp );
res = this->sh->tex0( m_tex );
res = this->sh->use();

this->m_mesh->test_draw();

dev->SetPixelShader(NULL);
dev->SetVertexShader(NULL);
 }

#include "constants.hlsl"

struct VS_INPUT
{
    float3 position  : POSITION0;
    float3 normal    : NORMAL0;
    float2 tex0      : TEXCOORD0;
    float4 color0    : COLOR0;
};

/* vertex shader output */
struct VS_OUTPUT
{
    float4 hposition : POSITION0;
    float4 color0    : COLOR0;
    float2 tex0      : TEXCOORD0;
};

VS_OUTPUT main( VS_INPUT IN )
{
    VS_OUTPUT OUT = (VS_OUTPUT)0;
    OUT.hposition   = mul( float4( IN.position, 1.0 ), gWVP );
    OUT.tex0        = IN.tex0;
    return OUT;
}

/* The pixel shader */
#include "constants.hlsl"

sampler gTEX0;  /* primary texture */

struct VS_OUTPUT
{
    float4 hposition : POSITION0;
    float4 color0    : COLOR0;
    float2 tex0      : TEXCOORD0;
};

struct PS_OUTPUT
{
    float4 color : COLOR;
};

PS_OUTPUT main( VS_OUTPUT IN )
{
    PS_OUTPUT OUT;
    OUT.color = tex2D( gTEX0, IN.tex0 );
    return OUT;
}

A:

The near plane parameter shall be strictly larger than zero.
The smaller it is the more precision you burn close to the camera, and with zero, the projection matrix degenerates into unusability.
If your geometry isn't where you want it to be when you use a conformant projection matrix, address that problem instead.

