0
\$\begingroup\$

I am doing volumetric fog. In the first pass I write down the depth of the front polygons of the cube, in the second pass I write down the depth of the back polygons of the cube, in the third pass I set two textures with depth and calculate the difference - this is the intensity of the fog. For checking, I set the first depth texture in the shader and see that the background on the screen is white (that is right, the texture is cleared 1.0), but in the place where the cube is zero depth, the cube is a solid color.

That is, the depth is not written into the texture. What to do?

    D3D11_DEPTH_STENCIL_DESC dsDesc;
    ZeroMemory(&dsDesc, sizeof(dsDesc));
    
    dsDesc.DepthEnable = true;
    dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
    dsDesc.DepthFunc = D3D11_COMPARISON_LESS;

    dsDesc.StencilEnable = false;
    dsDesc.StencilReadMask = 0xFF;
    dsDesc.StencilWriteMask = 0xFF;

    hr = m_pd3dDevice->CreateDepthStencilState(&dsDesc, &m_DepthStencilStateFog);

    D3D11_TEXTURE2D_DESC descDepth;
    ZeroMemory(&descDepth, sizeof(descDepth));
    descDepth.Width = width;
    descDepth.Height = height;
    descDepth.MipLevels = 1;
    descDepth.ArraySize = 1;
    descDepth.Format = DXGI_FORMAT_R32_TYPELESS;
    descDepth.SampleDesc.Count = 1;
    descDepth.SampleDesc.Quality = 0;
    descDepth.Usage = D3D11_USAGE_DEFAULT;
    descDepth.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_DEPTH_STENCIL;
    descDepth.CPUAccessFlags = 0;
    descDepth.MiscFlags = 0;
    hr = m_pd3dDevice->CreateTexture2D(&descDepth, NULL, &m_pTextureDepthFront);
    if (FAILED(hr))
    {
        return;
    }

    hr = m_pd3dDevice->CreateTexture2D(&descDepth, NULL, &m_pTextureDepthBack);
    if (FAILED(hr))
        return;


    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    ZeroMemory(&descDSV, sizeof(descDSV));
    descDSV.Format = DXGI_FORMAT_D32_FLOAT;
    
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
    descDSV.Texture2D.MipSlice = 0;
    hr = m_pd3dDevice->CreateDepthStencilView(m_pTextureDepthFront, &descDSV, 

&pDepthStencilView_Front);
    if (FAILED(hr))
    {
        return;
    }

    hr = m_pd3dDevice->CreateDepthStencilView(m_pTextureDepthBack, &descDSV, 

&pDepthStencilView_Back);
    if (FAILED(hr))
        return;


    D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
    ZeroMemory(&shaderResourceViewDesc, sizeof(shaderResourceViewDesc));

    shaderResourceViewDesc.Format = DXGI_FORMAT_R32_FLOAT;
    shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
    shaderResourceViewDesc.Texture2D.MipLevels = 1;
    
    hr = m_pd3dDevice->CreateShaderResourceView(m_pTextureDepthFront, 

&shaderResourceViewDesc, &m_shaderResourceViewDepthFront);
    if (FAILED(hr))
    {
        return;
    }

    hr = m_pd3dDevice->CreateShaderResourceView(m_pTextureDepthBack, 

&shaderResourceViewDesc, &m_shaderResourceViewDepthBack);
    if (FAILED(hr))
        return;

    struct VS_OUTPUT_DEPTH
    {
           float4 Position : SV_POSITION;
           float TexDepth: TEXCOORD0;
    };
  
    VS_OUTPUT_DEPTH vs_depth( float4 Position : POSITION )
    {
          VS_OUTPUT_DEPTH Output;

          Output.Position = mul( Position,mxRes  );
          Output.TexDepth=Output.Position.z/55;

          return( Output );
    }
//------------------------------------------------------------------------------------
// Pixel Shader
//------------------------------------------------------------------------------------
struct PS_OUT
{
    float4 color : SV_Target;
    float depth : SV_Depth;
};

PS_OUT ps_depth(VS_OUTPUT_DEPTH input)
{
        PS_OUT output = (PS_OUT)0;
        
        output.depth = input.TexDepth;

        return output;
}


```
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Forgot setup min max depth in viewport, now it works:

D3D11_VIEWPORT vp;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
...
m_pImmediateContext->RSSetViewports( 1, &vp );
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .