I am working on shadowmapping in D3D11 and my problem is that nothing is ever written to the depth texture into which I am rendering the scene. The texture is sampled correctly and I have also tried to draw the scene into a color texture and sample that. It worked fine too. I thought that perhaps the comparison function was somehow wrong, and tried changing it around but it did not help (I should have probably expected that since depth testing works fine when rendering the scene). I also make sure to unbind the texture after sampling it.
Maybe there is a problem in the initialization code (I am not sure whether the types I am using are 100% correct) or somewhere else. Any help would be greatly appreciated.
// SHADOWMAP RENDERTARGET INITIALIZATION
ID3D11Device *device = renderer->device;
D3D11_TEXTURE2D_DESC textureDesc = { 0 };
textureDesc.Width = (UINT)renderer->screenSize.x;
textureDesc.Height = (UINT)renderer->screenSize.y;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R24G8_TYPELESS;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
ID3D11Texture2D *tmpDepthStencil;
HRESULT hr = renderer->device->CreateTexture2D(&textureDesc, NULL, &tmpDepthStencil);
CHECK_WIN_ERROR(hr, "Error creating depth stencil\n");
CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D, DXGI_FORMAT_D24_UNORM_S8_UINT);
hr = renderer->device->CreateDepthStencilView(tmpDepthStencil, &depthStencilViewDesc, &renderTarget->depthStencilView;
CHECK_WIN_ERROR(hr, "Error creating depth stencil view\n");
if (SUCCEEDED(hr))
{
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc = {};
shaderResourceViewDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
hr = renderer->device->CreateShaderResourceView(tmpDepthStencil, &shaderResourceViewDesc,
&renderTarget->depthTexture);
CHECK_WIN_ERROR(hr, "Error creating render target texture\n");
}
else
{
OutputDebugStringA("Error creating render target\n");
}
RELEASE_DX_RESOURCE(tmpDepthStencil);
// CLEARING AND BINDING THE SHADOWMAP RENDERTARGET
renderer->context->ClearDepthStencilView(renderTarget->depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
ID3D11RenderTargetView *target[] = { NULL};
renderer->context->OMSetRenderTargets(1, target, renderTarget->depthStencilView);
// SHADOWMAPPING VERTEX SHADER (There is no pixel shader bound)
cbuffer PerSceneBuffer: register(b0)
{
matrix projection;
matrix view;
float3 lightDir;
};
cbuffer PerModelBuffer: register(b1)
{
matrix model;
};
struct VertexShaderInput
{
float3 pos : POSITION;
float3 col : COLOR;
float3 normal : NORMAL;
};
struct VertexShaderOutput
{
float4 pos : SV_POSITION;
};
VertexShaderOutput main(VertexShaderInput input)
{
VertexShaderOutput output;
float4 pos = float4(input.pos, 1.0f);
pos = mul(model, pos);
pos = mul(view, pos);
pos = mul(projection, pos);
output.pos = pos;
return output;
}
// TEXTURE SAMPLER USED
D3D11_SAMPLER_DESC samplerDesc = {};
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
HRESULT hr = renderer->device->CreateSamplerState(&samplerDesc, &renderer->texSampler);
CHECK_WIN_ERROR(hr, "Error creating Sampler \n");