   "texture-depth" dont work (DirectX 9 & VC2005)
  I'm trying to create a volumetric fog but encountered difficulties. It does not work fog pixel shader. I suspect that the depth information recorded in the texture is not correct. basis for the fog to accept my mesh cube. Сurrently draws a black cube on the background of my scene. Prompt please what is the problem?p_d3d_Device->BeginScene ();D3DXMATRIX m_matRes;m_matRes = m_matWorld * m_matView * m_matProj;D3DXMatrixTranspose(&m_matRes,&m_matRes);LPDIRECT3DSURFACE9 pSurface;p_d3d_Device->SetRenderTarget( 0, m_pOldRenderTarget );// Draw normal scene without Foghp_d3d_Device->SetPixelShader(0);p_d3d_Device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);p_d3d_Device->Clear(0, NULL, D3DCLEAR_TARGET  ,    D3DCOLOR_ARGB( 0x00, 0x00, 0x00, 0x00), 1.0,0);p_d3d_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);p_d3d_Device->SetStreamSource( 0, m_pLandVB, 0, sizeof(D3DVERTEX) );p_d3d_Device->SetIndices( m_pLandIB );p_d3d_Device->SetMaterial(g_pMeshMaterialsLand);p_d3d_Device->SetTexture(0,g_pMeshTexturesLand);p_d3d_Device->SetVertexShaderConstantF(4, (float*)&m_matRes,4);p_d3d_Device->SetVertexDeclaration( m_pLandVertexDeclaration );p_d3d_Device->SetVertexShader( m_pLandVertexShader );p_d3d_Device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0,    0, m_dwNumLandVertices,    0, m_dwNumLandFaces );p_d3d_Device->SetRenderTarget( 0, m_pOldRenderTarget );D3DXMATRIX m_ResMat = m_matWorld*m_matView*m_matProj;Depth_VS_ConstTable->SetMatrix(p_d3d_Device, WorldViewProjMatrixHandle, &m_ResMat);// We take the distance scenep_d3d_Device->SetTexture(0,NULL);p_d3d_Device->SetTexture(1,NULL);p_d3d_Device->SetTexture(2,NULL);p_d3d_Device->SetTexture(3,NULL);p_d3d_Device->SetPixelShader(0);p_d3d_Device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);p_d3d_Device->SetRenderState(D3DRS_ZENABLE, TRUE);p_d3d_Device->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE); m_pSceneDist->GetSurfaceLevel(0,&pSurface);p_d3d_Device->SetRenderTarget( 0, pSurface ); p_d3d_Device->Clear(0, NULL, D3DCLEAR_TARGET  ,   D3DCOLOR_ARGB( 0x00, 0x00, 0x00, 0x00), 1.0,0);p_d3d_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);p_d3d_Device->SetVertexShaderConstantF(4, (float*)&m_matRes,4);p_d3d_Device->SetStreamSource( 0, m_pLandVB, 0, sizeof(D3DVERTEX) );p_d3d_Device->SetIndices( m_pLandIB );p_d3d_Device->SetVertexShader( Depth_VS_Shader );p_d3d_Device->SetPixelShader( Depth_PS_Shader );p_d3d_Device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0,    0, m_dwNumLandVertices,    0, m_dwNumLandFaces );// Take the back wall Foghp_d3d_Device->SetRenderTarget( 0, m_pOldRenderTarget );m_pBackFogDist->GetSurfaceLevel(0,&pSurface);p_d3d_Device->SetRenderTarget( 0, pSurface );p_d3d_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB( 0x00, 0x00, 0x00, 0x00), 0.0,0);p_d3d_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);p_d3d_Device->SetRenderState(D3DRS_ZFUNC, D3DCMP_GREATER);p_d3d_Device->SetStreamSource( 0, m_pFogVB, 0, sizeof(D3DVERTEX) );p_d3d_Device->SetIndices( m_pFogIB );p_d3d_Device->SetVertexShader( Depth_VS_Shader );p_d3d_Device->SetPixelShader( Depth_PS_Shader );p_d3d_Device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0,    0, m_dwNumMeshFogVertices,    0, m_dwNumMeshFogFaces );// Take the front wall Foghm_pFrontFogDist->GetSurfaceLevel(0, &pSurface);p_d3d_Device->SetRenderTarget( 0, pSurface );p_d3d_Device->Clear(0, NULL, D3DCLEAR_TARGET  ,   D3DCOLOR_ARGB( 0x00, 0x00, 0x00, 0x00), 0.0,0);p_d3d_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);p_d3d_Device->SetStreamSource( 0, m_pFogVB, 0, sizeof(D3DVERTEX) );p_d3d_Device->SetIndices( m_pFogIB );p_d3d_Device->SetVertexShader( Depth_VS_Shader );p_d3d_Device->SetPixelShader( Depth_PS_Shader );p_d3d_Device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0,    0, m_dwNumMeshFogVertices,    0, m_dwNumMeshFogFaces );//Draw the volumetric fogp_d3d_Device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);p_d3d_Device->SetRenderTarget( 0, m_pOldRenderTarget );p_d3d_Device->Clear(0, NULL, NULL  ,   D3DCOLOR_ARGB( 0x00, 0x00, 0x00, 0x00), 1.0,0);p_d3d_Device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);p_d3d_Device->SetRenderState(D3DRS_ZENABLE, TRUE);p_d3d_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);   //p_d3d_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); //p_d3d_Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);//p_d3d_Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);p_d3d_Device->SetTexture(FogBackDesc.RegisterIndex,m_pBackFogDist);p_d3d_Device->SetTexture(FogFrontDesc.RegisterIndex,m_pFrontFogDist);p_d3d_Device->SetTexture(SDepthDesc.RegisterIndex,m_pSceneDist);p_d3d_Device->SetSamplerState(FogBackDesc.RegisterIndex,    D3DSAMP_MAGFILTER, D3DTEXF_POINT);p_d3d_Device->SetSamplerState(FogBackDesc.RegisterIndex,    D3DSAMP_MINFILTER, D3DTEXF_POINT);p_d3d_Device->SetSamplerState(FogBackDesc.RegisterIndex,    D3DSAMP_MIPFILTER, D3DTEXF_NONE);p_d3d_Device->SetSamplerState(FogFrontDesc.RegisterIndex,    D3DSAMP_MAGFILTER, D3DTEXF_POINT);p_d3d_Device->SetSamplerState(FogFrontDesc.RegisterIndex,    D3DSAMP_MINFILTER, D3DTEXF_POINT);p_d3d_Device->SetSamplerState(FogFrontDesc.RegisterIndex,    D3DSAMP_MIPFILTER, D3DTEXF_NONE);p_d3d_Device->SetSamplerState(SDepthDesc.RegisterIndex,    D3DSAMP_MAGFILTER, D3DTEXF_POINT);p_d3d_Device->SetSamplerState(SDepthDesc.RegisterIndex,    D3DSAMP_MINFILTER, D3DTEXF_POINT);p_d3d_Device->SetSamplerState(SDepthDesc.RegisterIndex,    D3DSAMP_MIPFILTER, D3DTEXF_NONE);p_d3d_Device->SetStreamSource( 0, m_pFogVB, 0, sizeof(D3DVERTEX) );p_d3d_Device->SetIndices( m_pFogIB );p_d3d_Device->SetPixelShader( Fog_PS_Shader );p_d3d_Device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0,    0, m_dwNumMeshFogVertices,    0, m_dwNumMeshFogFaces );p_d3d_Device->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);  p_d3d_Device->EndScene ();p_d3d_Device->Present (NULL, NULL, NULL, NULL);Depth vertex shaderfloat4x4 g_wvp;void vs_depth( inout float4 pos   : POSITION  ,  out float  depth : TEXCOORD0 ){    pos = mul( pos, g_wvp );    depth = pos.z;}Depth pixel shaderfloat4 ps_depth( in float depth : TEXCOORD0 ) : COLOR{    return float4( depth, 0.f, 0.f, 0.f );}Fog pixel shadersampler2D s_fog_back;sampler2D s_fog_front;sampler2D s_scene_depth;float3 fog_color={0.8,0.8,0.8};struct PS_INPUT{float2 s_fog_back : TEXCOORD0;float2 s_fog_front: TEXCOORD1;float2 s_scene_depth : TEXCOORD2;};float4 ps_draw_fog( PS_INPUT input ) : COLOR{    float fog_back    = tex2D( s_fog_back    , input.s_fog_back ).r;    float fog_front   = tex2D( s_fog_front   , input.s_fog_front ).r;    float scene_depth = tex2D( s_scene_depth , input.s_scene_depth ).r;   float k = fog_back - fog_front;    k -= fog_back - clamp( scene_depth, 0, fog_back );   return float4( fog_color*k, 1.0  );}
  Post-perspective z is not a useful value to store. You'll either want to store w (which will be your linear, view-space Z value) or z/w (this will be a non-linear value of the range [0,1] where 0 is the near-clip plane and 1 is the far-clip plane).   ;  I do not quite understand MJP - I need to change the vertex shader:depth = pos.z;changed to:depth = pos.w;or changed to:depth = pos.z/pos.w;I have changed but nothing happens - all the same black cube, not transparent.   