• Advertisement
Sign in to follow this  

DX11 [C++, DX11] Drawing depth buffer

This topic is 2401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I'm trying to draw Depth Buffer (DB), but it seems shader doesn't read DB values correctly, to be more exact, it reads 1 all the time.
If I clear DB using other value, than 1, then it does it read that other value. I though maybe DB doesn't work correctly, however all geometry is drawn correctly.
Maybe anyone could help to resolve this issue?

Depth Buffer initialization:

// modified DXUT11 code

ID3D11Texture2D* pDepthStencil = NULL;
D3D11_TEXTURE2D_DESC descDepth;
descDepth.Width = backBufferSurfaceDesc.Width;
descDepth.Height = backBufferSurfaceDesc.Height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_R32_TYPELESS;
descDepth.SampleDesc.Count = pDeviceSettings->d3d11.sd.SampleDesc.Count;
descDepth.SampleDesc.Quality = pDeviceSettings->d3d11.sd.SampleDesc.Quality;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
hr = pd3dDevice->CreateTexture2D( &descDepth, NULL, &pDepthStencil );

D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
descDSV.Format = DXGI_FORMAT_D32_FLOAT;
descDSV.Flags = 0;
if( descDepth.SampleDesc.Count > 1 )
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
else
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
hr = pd3dDevice->CreateDepthStencilView( pDepthStencil, &descDSV, &pDSV );

// ShaderResourceView
ID3D11DepthStencilView *pDepthStencil = DXUTGetD3D11DepthStencilView();
pDepthStencil->GetResource((ID3D11Resource**)&pTexture);
srdesc.Format = DXGI_FORMAT_R32_FLOAT;
srdesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srdesc.TextureCube.MostDetailedMip = 0;
srdesc.TextureCube.MipLevels = 1;
pDevice->CreateShaderResourceView(pTexture, &srdesc, &m_pDepthBuffer);
SAFE_RELEASE(pTexture);



Rendering:

pDeviceContext->PSSetShader(m_pPixelShader1, 0, 0);
pDeviceContext->PSSetShaderResources(0, 1, &m_pDepthBuffer);
ID3D11RenderTargetView *view = DXUTGetD3D11RenderTargetView();
pDeviceContext->OMSetRenderTargets(1, &view, 0);
pDeviceContext->Draw(4, 0);


Shader:
SamplerState texSampler : register(s0);
Texture2D<float> depthTex : register(t0);

float4 PS(float4 position : SV_POSITION, float2 texcoord : TEXCOORD0) : SV_TARGET {
return float4(depthTex.Sample(texSampler, texcoord).xxx, 1);
}



If anything else needed let me know. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Have you tried to apply some bias to the value in the pixel shader, like:
return pow(depth,10)
// or

return pow(depth*0.01,10)
// play around with these two values



You could also check it in PIX, but it is important that you adjust the depth range sliders (the left one must go _almost_ all the way to the right).

Share this post


Link to post
Share on other sites
Are you sure it's *actually* 1.0, or is it just very close to 1.0? Because a depth buffer from a perspective projection will contain values very close to 1.0 for most of the depth range. I gave a more detailed explanation here if you're interested: http://www.gamedev.net/topic/605640-hlsl-saving-depth/page__p__4831558#entry4831558

Share this post


Link to post
Share on other sites

Have you tried to apply some bias to the value in the pixel shader, like:
return pow(depth,10)
// or

return pow(depth*0.01,10)
// play around with these two values



You could also check it in PIX, but it is important that you adjust the depth range sliders (the left one must go _almost_ all the way to the right).

It seems pow(depth, 100); does the trick, but when I change near/far planes it's wrong again, not to mention power of 100 probably takes a lot time.




Are you sure it's *actually* 1.0, or is it just very close to 1.0? Because a depth buffer from a perspective projection will contain values very close to 1.0 for most of the depth range. I gave a more detailed explanation here if you're interested: http://www.gamedev.n...58#entry4831558

Now that you mention, I noticed a problem... With n3Xus's suggestion to try pow(depth, 100); I can clearly see darker area's, PIX shows it darker as well, but when I debug dark pixel, it returns (1.0, 1.0, 1.0, 1.0), even though pixel is actually (0.3, 0.3, 0.3, 1.0).




Is there any more efficient way to get proper value, than power of <high number>? With my usual depth range of 0.1 - 5000.0f I had to use pow(depth, 500); to get decent color difference.
Basically I'm trying to make depth edge detection, but with such high power value even flat surface near camera gets detected as edge, or I'll have to make linear depth buffer?

Share this post


Link to post
Share on other sites
Usually you just rescale so that 0.9 in your depth buffer is mapped to 0.0 in your output color, so output = (depth - 0.9f) * 10.0f or something like that. A more robust way would be to convert the depth value back to linear Z and then divide by your far clip plane.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Karol Plewa
      Hi, 
       
      I am working on a project where I'm trying to use Forward Plus Rendering on point lights. I have a simple reflective scene with many point lights moving around it. I am using effects file (.fx) to keep my shaders in one place. I am having a problem with Compute Shader code. I cannot get it to work properly and calculate the tiles and lighting properly. 
       
      Is there anyone that is wishing to help me set up my compute shader?
      Thank you in advance for any replies and interest!
    • By turanszkij
      Hi, right now building my engine in visual studio involves a shader compiling step to build hlsl 5.0 shaders. I have a separate project which only includes shader sources and the compiler is the visual studio integrated fxc compiler. I like this method because on any PC that has visual studio installed, I can just download the solution from GitHub and everything just builds without additional dependencies and using the latest version of the compiler. I also like it because the shaders are included in the solution explorer and easy to browse, and double-click to open (opening files can be really a pain in the ass in visual studio run in admin mode). Also it's nice that VS displays the build output/errors in the output window.
      But now I have the HLSL 6 compiler and want to build hlsl 6 shaders as well (and as I understand I can also compile vulkan compatible shaders with it later). Any idea how to do this nicely? I want only a single project containing shader sources, like it is now, but build them for different targets. I guess adding different building projects would be the way to go that reference the shader source project? But how would they differentiate from shader type of the sources (eg. pixel shader, compute shader,etc.)? Now the shader building project contains for each shader the shader type, how can other building projects reference that?
      Anyone with some experience in this?
    • By osiris_dev
      Hello!
      Have a problem with reflection shader for D3D11:
      1>engine_render_d3d11_system.obj : error LNK2001: unresolved external symbol IID_ID3D11ShaderReflection
      I tried to add this:
      #include <D3D11Shader.h>
      #include <D3Dcompiler.h>
      #include <D3DCompiler.inl>
      #pragma comment(lib, "D3DCompiler.lib")
      //#pragma comment(lib, "D3DCompiler_47.lib")
      As MSDN tells me but still no fortune. I think lot of people did that already, what I missing?
      I also find this article http://mattfife.com/?p=470
      where recommend to use SDK headers and libs before Wind SDK, but I am not using DirectX SDK for this project at all, should I?
    • By trojanfoe
      Hi there, this is my first post in what looks to be a very interesting forum.
      I am using DirectXTK to put together my 2D game engine but would like to use the GPU depth buffer in order to avoid sorting back-to-front on the CPU and I think I also want to use GPU instancing, so can I do that with SpriteBatch or am I looking at implementing my own sprite rendering?
      Thanks in advance!
    • By Matt_Aufderheide
      I am trying to draw a screen-aligned quad with arbitrary sizes.
       
      currently I just send 4 vertices to the vertex shader like so:
      pDevCon->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
      pDevCon->Draw(4, 0);
       
      then in the vertex shader I am doing this:
      float4 main(uint vI : SV_VERTEXID) : SV_POSITION
      {
       float2 texcoord = float2(vI & 1, vI >> 1);
      return float4((texcoord.x - 0.5f) * 2, -(texcoord.y - 0.5f) * 2, 0, 1);
      }
      that gets me a screen-sized quad...ok .. what's the correct way to get arbitrary sizes?...I have messed around with various numbers, but I think I don't quite get something in these relationships.
      one thing I tried is: 
       
      float4 quad = float4((texcoord.x - (xpos/screensizex)) * (width/screensizex), -(texcoord.y - (ypos/screensizey)) * (height/screensizey), 0, 1);
       
      .. where xpos and ypos is number of pixels from upper right corner..width and height is the desired size of the quad in pixels
      this gets me somewhat close, but not right.. a bit too small..so I'm missing something ..any ideas?
       
      .
  • Advertisement