Jump to content

View more

Image of the Day

WIP title screen for #DeathOfAPartisan #screenshotsaturday #gamedev https://t.co/qJNhfZCvd4
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Cubemap texture as depth buffer (shadowmapping)

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 KaiserJohan   Members   


Posted 05 August 2014 - 04:25 PM

I'm converting my OpenGL 4 renderer to DirectX 11. Once I got over making a simple textured triangle, the rest went very easy, I now have a deferred shader setup with ambient/point/directional lights and am now reimplementing the shadow mapping parts, starting with point lights (omnidirectional shadowmaps).


Here's how I create my texture cubemap:

// create shadowmap texture/view/srv
D3D11_TEXTURE2D_DESC depthBufferDesc;
ZeroMemory(&depthBufferDesc, sizeof(D3D11_TEXTURE2D_DESC));
depthBufferDesc.ArraySize = 6;
depthBufferDesc.Format = DXGI_FORMAT_D32_FLOAT;     // potential issue? UNORM instead?
depthBufferDesc.Width = shadowmapSize;
depthBufferDesc.Height = shadowmapSize;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDesc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
DXCALL(device->CreateTexture2D(&depthBufferDesc, NULL, &mShadowmapTexture));
DXCALL(device->CreateDepthStencilView(mShadowmapTexture, NULL, &mShadowmapView));
DXCALL(device->CreateShaderResourceView(mShadowmapTexture, NULL, &mShadowmapSRV));

I'm currently at a loss of how to select which face of the cubemap to use as the depth buffer. In OpenGL I would iterate 6 times and select the cubemap face with:


But in DirectX11, cubemaps seems to be defined as Texture2D but with different array size. Looking at the OMSetRenderTargets(), it dosn't specify anywhere which cubemap face to use either.


Any pointers on how to select the proper face to use as depth texture?


I've been browsing around like mad on MSDN, and it is fine as a reference for structures and functions, but awful for anything else :(

Edited by KaiserJohan, 05 August 2014 - 04:27 PM.

#2 MJP   Moderators   


Posted 05 August 2014 - 08:11 PM

This is actually a little tricky to get right in D3D11, so I don't blame you for having trouble. Here's some code from my sample framework should help you:


void DepthStencilBuffer::Initialize(ID3D11Device* device,
                                    uint32 width,
                                    uint32 height,
                                    DXGI_FORMAT format,
                                    bool32 useAsShaderResource,
                                    uint32 multiSamples,
                                    uint32 msQuality,
                                    uint32 arraySize)
    uint32 bindFlags = D3D11_BIND_DEPTH_STENCIL;
    if (useAsShaderResource)
        bindFlags |= D3D11_BIND_SHADER_RESOURCE;
    DXGI_FORMAT dsTexFormat;
    if (!useAsShaderResource)
        dsTexFormat = format;
    else if (format == DXGI_FORMAT_D16_UNORM)
        dsTexFormat = DXGI_FORMAT_R16_TYPELESS;
    else if(format == DXGI_FORMAT_D24_UNORM_S8_UINT)
        dsTexFormat = DXGI_FORMAT_R24G8_TYPELESS;
        dsTexFormat = DXGI_FORMAT_R32_TYPELESS;
    D3D11_TEXTURE2D_DESC desc;
    desc.Width = width;
    desc.Height = height;
    desc.ArraySize = arraySize;
    desc.BindFlags = bindFlags;
    desc.CPUAccessFlags = 0;
    desc.Format = dsTexFormat;
    desc.MipLevels = 1;
    desc.MiscFlags = 0;
    desc.SampleDesc.Count = multiSamples;
    desc.SampleDesc.Quality = msQuality;
    desc.Usage = D3D11_USAGE_DEFAULT;
    DXCall(device->CreateTexture2D(&desc, nullptr, &Texture));
    for (uint32 i = 0; i < arraySize; ++i)
        D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
        ID3D11DepthStencilViewPtr dsView;
        dsvDesc.Format = format;
        if (arraySize == 1)
            dsvDesc.ViewDimension = multiSamples > 1 ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D;
            dsvDesc.Texture2D.MipSlice = 0;
            if(multiSamples > 1)
                dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY;
                dsvDesc.Texture2DMSArray.ArraySize = 1;
                dsvDesc.Texture2DMSArray.FirstArraySlice = i;
                dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
                dsvDesc.Texture2DArray.ArraySize = 1;
                dsvDesc.Texture2DArray.FirstArraySlice = i;
                dsvDesc.Texture2DArray.MipSlice = 0;
        dsvDesc.Flags = 0;
        DXCall(device->CreateDepthStencilView(Texture, &dsvDesc, &dsView));
        if (i == 0)
            // Also create a read-only DSV
            dsvDesc.Flags = D3D11_DSV_READ_ONLY_DEPTH;
            if (format == DXGI_FORMAT_D24_UNORM_S8_UINT || format == DXGI_FORMAT_D32_FLOAT_S8X24_UINT)
                dsvDesc.Flags |= D3D11_DSV_READ_ONLY_STENCIL;
            DXCall(device->CreateDepthStencilView(Texture, &dsvDesc, &ReadOnlyDSView));
            dsvDesc.Flags = 0;
    DSView = ArraySlices[0];
    if (useAsShaderResource)
        DXGI_FORMAT dsSRVFormat;
        if (format == DXGI_FORMAT_D16_UNORM)
            dsSRVFormat = DXGI_FORMAT_R16_UNORM;
        else if(format == DXGI_FORMAT_D24_UNORM_S8_UINT)
            dsSRVFormat = DXGI_FORMAT_R24_UNORM_X8_TYPELESS ;
            dsSRVFormat = DXGI_FORMAT_R32_FLOAT;
        srvDesc.Format = dsSRVFormat;
        if (arraySize == 1)
            srvDesc.ViewDimension = multiSamples > 1 ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D;
            srvDesc.Texture2D.MipLevels = 1;
            srvDesc.Texture2D.MostDetailedMip = 0;
            srvDesc.ViewDimension = multiSamples > 1 ? D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY : D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
            srvDesc.Texture2DArray.ArraySize = arraySize;
            srvDesc.Texture2DArray.FirstArraySlice = 0;
            srvDesc.Texture2DArray.MipLevels = 1;
            srvDesc.Texture2DArray.MostDetailedMip = 0;
        DXCall(device->CreateShaderResourceView(Texture, &srvDesc, &SRView));
        SRView = nullptr;
    Width = width;
    Height = height;
    MultiSamples = multiSamples;
    Format = format;
    ArraySize = arraySize;

#3 KaiserJohan   Members   


Posted 06 August 2014 - 02:25 AM

Thanks for reply, I'll try it out, got a few questions in the meantime though:


1. Why do you have texture formats as "typeless"? Why not for example *_D32_FLOAT?
2. Why are you creating a stencil read-only DSV?
3. Why isnt your texture created with D3D11_RESOURCE_MISC_TEXTURECUBE flag? Instead it looks like you are creating a texture2D array - are the two the same in DirectX11? In OpenGL I'm fairly certain it differentiates between cubemaps and texture2d arrays

Edited by KaiserJohan, 06 August 2014 - 03:31 AM.

#4 unbird   Members   


Posted 06 August 2014 - 12:58 PM

1. This is how you have to setup the format(s) if you also want a shader resource view (actually a must for shadow mapping). You end up with three different formats for the texture, the DSV and the SRV.
2. This is optional and supposed to be an optimization in conjunction with read-only depth stencil states. 
3. Yes, if you want to sample from a TextureCube (HLSL) you need this flag.
It should be noted that one can create a DSV for the whole cube:
dsvDesc.Texture2DArray.ArraySize = 6;
dsvDesc.Texture2DArray.FirstArraySlice = 0;
Individual array slices are then selected using the HLSL system value semantic SV_RenderTargetArrayIndex (geometry shader).
(all this is off the top of my head, untested)

#5 MJP   Moderators   


Posted 06 August 2014 - 05:04 PM

Argh! I had a whole bunch of explanatory text following the code, but it looks like the editor ate it. Sorry about that. sad.png


The typeless format is definitely the trickiest part of this. Basically D3D will make the distinction between depth and color formats, so if you want to alias a texture using both types then you need to use a TYPELESS format when creating the underlying texture resource. Then you use the "D" format when creating  the depth stencil view, and the "R" format when creating the shader resource view. This way the depth stencil view interprets the data as a depth buffer, and the shader resource view interprets it as a color texture.


The code I posted doesn't actually handle cubemaps, it just handles texture arrays. However cubemaps are really just a special case of texture arrays with an array size of 6. You'll definitely want to continue using D3D11_RESOURCE_MISC_TEXTURECUBE flag if you're creating a cubemap. To render to each face individually, you'll want to do something similar to what I'm doing in the code I pasted where you create 6 separate depth stencil views. When you create the DSV, you can essentially bind it to a single slice of a texture array. So you can use that to effectively target a single face. Then just bind the appropriate DSV to your context when rendering. Also note that if you're using a cubemap, you'll want to use D3D11_SRV_DIMENSION_TEXTURECUBE (along with the corresponding TextureCube member of the SRV desc structure) instead of Texture2D or Texture2DArray.


The read-only DSV is just something I've used in a few special cases, you probably won't need it. Normally D3D doesn't let you bind a depth buffer to the context if you also have it simultaneously bound as a shader resource view. This is to prevent read-write ordering conflicts. However if you create a read-only DSV, then you can have it bound as both a depth buffer and an SRV simultaneously since it's read-only in both cases. However you of course can only do depth testing like this, and can't enable depth writes.

Edited by MJP, 06 August 2014 - 05:04 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.