• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
neroziros

DX11
HLSL Shader + Depth Map Problem

2 posts in this topic

Hi there! I write here because I'm having a problem rendering the scene depth as a texture.

 

I have been following this tutorial  but the depth is rendered as a black texture. Any help will be greatly appreciated. Here is what I have been doing so far:

 

 

DEPTH MAP CREATION

 

// CREATE THE AUXILIARY DEPTH MAP
D3D11_TEXTURE2D_DESC texDesc;
ZeroMemory(&texDesc, sizeof(texDesc));
texDesc.Width = screenWidth;
texDesc.Height = screenHeight;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.Format = DXGI_FORMAT_R32_TYPELESS;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_DEPTH_STENCIL ;
texDesc.CPUAccessFlags = 0;
texDesc.MiscFlags = 0;
result = m_device->CreateTexture2D(&texDesc, 0, &m_depthMap);
if (FAILED(result))return false;


// Create the depth stencil view.
D3D11_DEPTH_STENCIL_VIEW_DESC dsvd;
ZeroMemory(&dsvd, sizeof(dsvd));
dsvd.Format = DXGI_FORMAT_D32_FLOAT;
dsvd.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
dsvd.Texture2D.MipSlice = 0;
result = m_device->CreateDepthStencilView(m_depthMap, &dsvd, &m_depthMapView);
if (FAILED(result))return false;


// Create the shader resource view (this is what will be sent to the shader)
D3D11_SHADER_RESOURCE_VIEW_DESC srvd;
srvd.Format = DXGI_FORMAT_R32_FLOAT;
srvd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvd.Texture2D.MipLevels = texDesc.MipLevels;
srvd.Texture2D.MostDetailedMip = 0;
result = m_device->CreateShaderResourceView(m_depthMap, &srvd, &m_depthShaderResource);
if (FAILED(result))return false;

_

 

CREATE THE DEPTH STENCIL STATE TO BE USED DURING NORMAL RENDERING

 

// Create the new depthstate description and depth state
D3D11_DEPTH_STENCIL_DESC dsDesc;
ZeroMemory(&dsDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
dsDesc.DepthEnable = true;
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D11_COMPARISON_LESS;
dsDesc.StencilEnable = false;
dsDesc.StencilReadMask = 0xFF;
dsDesc.StencilWriteMask = 0x00;


// Stencil operations if pixel is front-facing
dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;


// Stencil operations if pixel is back-facing
dsDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
dsDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
// Create depth stencil state
device->CreateDepthStencilState(&dsDesc, &DepthState);

_

 

DEPTH MAP RENDER

 

// Setup the color to clear the buffer to.
color[0] = red;
color[1] = green;
color[2] = blue;
color[3] = alpha;

// Clear the back buffer.
m_deviceContext->ClearRenderTargetView(m_renderTargetView, color);
    
// Clear the depth buffer.
m_deviceContext->ClearDepthStencilView(m_depthMapView, D3D11_CLEAR_DEPTH, 1.0f, 0);

// Set the render targets
m_deviceContext->OMSetRenderTargets(1, &m_renderTargetView, m_depthMapView);
// Render the scene normally (Here I don't do anything depth buffer related exept binding the depthstencil state previously created)
// ...
deviceContext->OMSetDepthStencilState(DepthState, 1);
// ....

_

 

 

I "TRY" TO RENDER THE DEPTH MAP

// I set the current bound depth map as null because I read that you can't sample a depth map srv while it is still being used (Even if you don't write to it)
m_deviceContext->OMSetRenderTargets(1, &m_renderTargetView, NULL);

// Setup the render parameters (nothing depth related)
//  ...
// Set depth stencil state
m_D3D->GetDeviceContext()->OMSetDepthStencilState(pDepthState, 0);

// Send the depth map to the shader
m_D3D->GetDeviceContext()->PSSetShaderResources(1, 1,&m_depthShaderResource);
m_D3D->GetDeviceContext()->PSSetShaderResources(2, 1, &g_pColorTextureRV);

// Render the quad 
// ...

// Clean SRVs
m_D3D->GetDeviceContext()->VSSetShaderResources(0, 1, &nullSRV);
m_D3D->GetDeviceContext()->PSSetShaderResources(0, 1, &nullSRV);
m_D3D->GetDeviceContext()->PSSetShaderResources(1, 1, &nullSRV);

// Show the frame
m_swapChain->Present(0, 0);

_

 

AND HERE IS HOW I SAMPLE IT INSIDE THE SHADER

StructuredBuffer<Particle> SimulationState : register(t0);
Texture2D DepthMap : register(t1);
Texture2D ColorMap : register(t2);


SamplerState samplerPoint
{
Filter = MIN_MAG_MIP_POINT;
AddressU = Clamp;
AddressV = Clamp;
};


float4 PSMAIN(in PS_INPUT input) : SV_Target
{
float DepthMapSample = DepthMap.Sample(samplerPoint, input.texcoords).r;
float4 color = input.color * DepthMapSample;


return(color);
}

I added a temporal ColorMap texture to check if the texcoords and sampling was working and , as expected, they do are working fine. The problem is with the depth map which is rendered fully black.

 

Anyone has an idea about what could be the problem here?  If I can't solve this I guess that I will have to go for the MRTs route (writing the depth maps manually to an extra render target). But I really want to use the DX11 depth maps to aovid the extra calculations.

 

Cheers and thanks for your time!

Edited by neroziros
0

Share this post


Link to post
Share on other sites

Have you enabled debugging when creating the D3D objects?

 

Playing with the depth buffers and stuff may not be always obvious - the debugging information may give you some insight where the problem is. Also, you could use some 3rd party software to inspect the depth buffer content after rendering. That might help you to locate the problem part of the code.

 

Note : Actually it is possible to perform depth testing and read the depth buffer at the same time IF the depth stencil view is read_only. 

 

Cheers!

0

Share this post


Link to post
Share on other sites

Thanks! I will try using Nsight this week and post results.

 

In the meatime I have implemented a MRT version and it worked perfectly. Cheers!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By lonewolff
      Hi Guys,
      I am just wondering if it is possible to acquire the address of the backbuffer if an API (based on DX11) only exposes the 'device' and 'context' pointers?
      Any advice would be greatly appreciated
    • By MarcusAseth
      bool InitDirect3D::Init() { if (!D3DApp::Init()) { return false; } //Additional Initialization //Disable Alt+Enter Fullscreen Toggle shortkey IDXGIFactory* factory; CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&factory)); factory->MakeWindowAssociation(mhWindow, DXGI_MWA_NO_WINDOW_CHANGES); factory->Release(); return true; }  
      As stated on the title and displayed on the code above, regardless of it Alt+Enter still takes effect...
      I recall something from the book during the swapChain creation, where in order to create it one has to use the same factory used to create the ID3D11Device, therefore I tested and indeed using that same factory indeed it work.
      How is that one particular factory related to my window and how come the MakeWindowAssociation won't take effect with a newly created factory?
      Also what's even the point of being able to create this Factories if they won't work,?(except from that one associated with the ID3D11Device) 
    • By ProfL
      Can anyone recommend a wrapper for Direct3D 11 that is similarly simple to use as SFML? I don't need all the image formats etc. BUT I want a simple way to open a window, allocate a texture, buffer, shader.
    • By lucky6969b
      Q1:
      Since there is no more fixed pipeline rendering in DX11, for every part of rendering in DX11, do I need to create a brand-new vertex shader and pixel shader... or at least I have to find one relevant online. If you work on skinned meshes and other effects originally worked in DX9 fixed pipeline, do I have to rework everything by now?
       
      Q2:
      For assimp, if it originally was designed for DX9, like it is coupled to a DX9 device for creating meshes and materials etc. Do I have to add in the DX11 device in the assimp, or can I just leave the assimp to remain in DX9 and after the meshes are loaded, I just convert the vertex buffers and index buffers into DX11 buffers?
      Thanks
      Jack
    • By MarcusAseth
      This header is mentioned in the book I'm reading but there is no documentation on msdn... Is it like an... outdated and abandoned header?
      If so, what's the current default/recomended library for handling errors with directX?
  • Popular Now