Skip to main content
added 146 characters in body
Source Link
philB
  • 333
  • 2
  • 11

I have slightly changed the CreateDepthStencilViewFromArray function so that the same can be used to create eithreither an array of 2D or a 2Darray of depthstencils (envetually both by removing the compiler options).

DWORD MaxWanted = 3;
ID3D11Texture2D* pTex;

//create a Texture2DArray of 3 slices in typeless format compatible 
//with depthstencil format. Pass the number of slices MaxWanted to 
//TexDesc.ArraySize member and the TexDesc.BindFlags as
// D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE 
//when creating the texture to be able to create depthstencils and 
//shaderresources from it.

pTex = CreateTexture2DArray(Width, Height, DXGI_FORMAT_R16_TYPELESS, D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE, MaxWanted);
ID3D11DepthStencilView* pDSVArray  = NULL;
ID3D11DepthStencilView* pDSV[MaxWanted];
if (pTex)
{
//create a ShaderResource2DArray of MaxWanted slices in R16 format. Pass the number of 
//slices MaxWanted to ShaderDesc.ArraySize member when creating the 
//ShaderResource;
    pSRVDSVArray = CreateShaderResource2DArray(pTex, DXGI_FORMAT_R16_UNORM, MaxWanted);
        
#ifdef SM_CASCADE2DARRAY
        //create a DepthStencil as 2DArray of MaxWanted slices in D16 format.  
        //See function details below. An equivalent function can be used for creating 
        //shaderresource array
        pDSVArray = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, MaxWanted, 0);
#endif
#ifdef SM_CASCADEARRAY_OF_2D
        for (DWORD c = 0; c < MaxWanted; c++)
        {
            pDSV[c] = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, 1, c);
        }
#endif
        pTex->Release();
    }


//To create DSV as array of 2D from Texture2DArray:
//  FirstSlice = index of slice in the range [0.. MaxWanted -1]
//  ArraySize = 1;
//To create DSV as 2DArray from Texture2DArray:
//  FirstSlice = 0
//  ArraySize = MaxWanted;
//MipSlice always 0, I don't use mipmaps

ID3D11DepthStencilView* CreateDepthStencilViewFromArray(ID3D11Texture2D* pText2D, DXGI_FORMAT Format, DWORD ArraySize, DWORD FirstSlice)
{
    ID3D11DepthStencilView* pDSV = NULL;

    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    ZeroMemory(&descDSV, sizeof(descDSV));
    descDSV.Format = Format;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
    descDSV.Texture2D.MipSlice = 0;
    descDSV.Texture2DArray.FirstArraySlice = FirstSlice;
    descDSV.Texture2DArray.ArraySize = ArraySize;
    descDSV.Texture2DArray.MipSlice = 0;
    HRESULT hr = gpD11->CreateDepthStencilView(pText2D, &descDSV, &pDSV);
    if (FAILED(hr))
    {
        OutputDebugStringA("CreateDepthStencilViewFromArray failed hresult invalidarg\n");
        return NULL;
    }
    return pDSV;
}

I have slightly changed the CreateDepthStencilViewFromArray function so that the same can be used to create eithr an array of 2D or a 2Darray of depthstencils.

DWORD MaxWanted = 3;
ID3D11Texture2D* pTex;

//create a Texture2DArray of 3 slices in typeless format compatible 
//with depthstencil format. Pass the number of slices MaxWanted to 
//TexDesc.ArraySize member and the TexDesc.BindFlags as
// D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE 
//when creating the texture to be able to create depthstencils and 
//shaderresources from it.

pTex = CreateTexture2DArray(Width, Height, DXGI_FORMAT_R16_TYPELESS, D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE, MaxWanted);
if (pTex)
{
//create a ShaderResource2DArray of MaxWanted slices in R16 format. Pass the number of 
//slices MaxWanted to ShaderDesc.ArraySize member when creating the 
//ShaderResource;
    pSRVDSVArray = CreateShaderResource2DArray(pTex, DXGI_FORMAT_R16_UNORM, MaxWanted);
        
#ifdef SM_CASCADE2DARRAY
        //create a DepthStencil as 2DArray of MaxWanted slices in D16 format.  
        //See function details below. An equivalent function can be used for creating 
        //shaderresource array
        pDSVArray = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, MaxWanted, 0);
#endif
#ifdef SM_CASCADEARRAY_OF_2D
        for (DWORD c = 0; c < MaxWanted; c++)
        {
            pDSV[c] = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, 1, c);
        }
#endif
        pTex->Release();
    }


//To create DSV as array of 2D from Texture2DArray:
//  FirstSlice = index of slice in the range [0.. MaxWanted -1]
//  ArraySize = 1;
//To create DSV as 2DArray from Texture2DArray:
//  FirstSlice = 0
//  ArraySize = MaxWanted;
//MipSlice always 0, I don't use mipmaps

ID3D11DepthStencilView* CreateDepthStencilViewFromArray(ID3D11Texture2D* pText2D, DXGI_FORMAT Format, DWORD ArraySize, DWORD FirstSlice)
{
    ID3D11DepthStencilView* pDSV = NULL;

    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    ZeroMemory(&descDSV, sizeof(descDSV));
    descDSV.Format = Format;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
    descDSV.Texture2D.MipSlice = 0;
    descDSV.Texture2DArray.FirstArraySlice = FirstSlice;
    descDSV.Texture2DArray.ArraySize = ArraySize;
    descDSV.Texture2DArray.MipSlice = 0;
    HRESULT hr = gpD11->CreateDepthStencilView(pText2D, &descDSV, &pDSV);
    if (FAILED(hr))
    {
        OutputDebugStringA("CreateDepthStencilViewFromArray failed hresult invalidarg\n");
        return NULL;
    }
    return pDSV;
}

I have slightly changed the CreateDepthStencilViewFromArray function so that the same can be used to create either an array of 2D or a 2Darray of depthstencils (envetually both by removing the compiler options).

DWORD MaxWanted = 3;
ID3D11Texture2D* pTex;

//create a Texture2DArray of 3 slices in typeless format compatible 
//with depthstencil format. Pass the number of slices MaxWanted to 
//TexDesc.ArraySize member and the TexDesc.BindFlags as
// D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE 
//when creating the texture to be able to create depthstencils and 
//shaderresources from it.

pTex = CreateTexture2DArray(Width, Height, DXGI_FORMAT_R16_TYPELESS, D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE, MaxWanted);
ID3D11DepthStencilView* pDSVArray  = NULL;
ID3D11DepthStencilView* pDSV[MaxWanted];
if (pTex)
{
//create a ShaderResource2DArray of MaxWanted slices in R16 format. Pass the number of 
//slices MaxWanted to ShaderDesc.ArraySize member when creating the 
//ShaderResource;
    pSRVDSVArray = CreateShaderResource2DArray(pTex, DXGI_FORMAT_R16_UNORM, MaxWanted);
        
#ifdef SM_CASCADE2DARRAY
        //create a DepthStencil as 2DArray of MaxWanted slices in D16 format.  
        //See function details below. An equivalent function can be used for creating 
        //shaderresource array
        pDSVArray = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, MaxWanted, 0);
#endif
#ifdef SM_CASCADEARRAY_OF_2D
        for (DWORD c = 0; c < MaxWanted; c++)
        {
            pDSV[c] = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, 1, c);
        }
#endif
        pTex->Release();
    }


//To create DSV as array of 2D from Texture2DArray:
//  FirstSlice = index of slice in the range [0.. MaxWanted -1]
//  ArraySize = 1;
//To create DSV as 2DArray from Texture2DArray:
//  FirstSlice = 0
//  ArraySize = MaxWanted;
//MipSlice always 0, I don't use mipmaps

ID3D11DepthStencilView* CreateDepthStencilViewFromArray(ID3D11Texture2D* pText2D, DXGI_FORMAT Format, DWORD ArraySize, DWORD FirstSlice)
{
    ID3D11DepthStencilView* pDSV = NULL;

    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    ZeroMemory(&descDSV, sizeof(descDSV));
    descDSV.Format = Format;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
    descDSV.Texture2D.MipSlice = 0;
    descDSV.Texture2DArray.FirstArraySlice = FirstSlice;
    descDSV.Texture2DArray.ArraySize = ArraySize;
    descDSV.Texture2DArray.MipSlice = 0;
    HRESULT hr = gpD11->CreateDepthStencilView(pText2D, &descDSV, &pDSV);
    if (FAILED(hr))
    {
        OutputDebugStringA("CreateDepthStencilViewFromArray failed hresult invalidarg\n");
        return NULL;
    }
    return pDSV;
}
added 146 characters in body
Source Link
philB
  • 333
  • 2
  • 11
DWORD MaxWanted = 3;
ID3D11Texture2D* pTex;

//create a Texture2DArray of 3 slices in typeless format compatible 
//with depthstencil format. Pass the number of slices MaxWanted to 
//TexDesc.ArraySize member and the TexDesc.BindFlags as
// D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE 
//when creating the texture;texture to be able to create depthstencils and 
//shaderresources from it.

pTex = CreateTexture2DArray(Width, Height, DXGI_FORMAT_R16_TYPELESS, D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE, MaxWanted);
if (pTex)
{
//create a ShaderResource2DArray of MaxWanted slices in R16 format. Pass the number of 
//slices MaxWanted to ShaderDesc.ArraySize member when creating the 
//ShaderResource;
    pSRVDSVArray = CreateShaderResource2DArray(pTex, DXGI_FORMAT_R16_UNORM, MaxWanted);
        
#ifdef SM_CASCADE2DARRAY
        //create a DepthStencil as 2DArray of MaxWanted slices in D16 format.  
        //See function details below. An equivalent function can be used for creating 
        //shaderresource array
        pDSVArray = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, MaxWanted, 0);
#endif
#ifdef SM_CASCADEARRAY_OF_2D
        for (DWORD c = 0; c < MaxWanted; c++)
        {
            pDSV[c] = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, 1, c);
        }
#endif
        pTex->Release();
    }


//To create DSV as array of 2D from Texture2DArray:
//  FirstSlice = index of slice in the range [0.. MaxWanted -1]
//  ArraySize = 1;
//To create DSV as 2DArray from Texture2DArray:
//  FirstSlice = 0
//  ArraySize = MaxWanted;
//MipSlice always 0, I don't use mipmaps

ID3D11DepthStencilView* CreateDepthStencilViewFromArray(ID3D11Texture2D* pText2D, DXGI_FORMAT Format, DWORD ArraySize, DWORD FirstSlice)
{
    ID3D11DepthStencilView* pDSV = NULL;
    ID3D11Resource* pRes;

    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    ZeroMemory(&descDSV, sizeof(descDSV));
    descDSV.Format = Format;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
    descDSV.Texture2D.MipSlice = 0;
    descDSV.Texture2DArray.FirstArraySlice = FirstSlice;
    descDSV.Texture2DArray.ArraySize = ArraySize;
    descDSV.Texture2DArray.MipSlice = 0;
    HRESULT hr = gpD11->CreateDepthStencilView(pText2D, &descDSV, &pDSV);
    if (FAILED(hr))
    {
        OutputDebugStringA("CreateDepthStencilViewFromArray failed hresult invalidarg\n");
        return NULL;
    }
    return pDSV;
}
DWORD MaxWanted = 3;
ID3D11Texture2D* pTex;

//create a Texture2DArray of 3 slices in typeless format compatible 
//with depthstencil format. Pass the number of slices MaxWanted to 
//TexDesc.ArraySize member when creating the texture;

pTex = CreateTexture2DArray(Width, Height, DXGI_FORMAT_R16_TYPELESS, D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE, MaxWanted);
if (pTex)
{
//create a ShaderResource2DArray of MaxWanted slices in R16 format. Pass the number of 
//slices MaxWanted to ShaderDesc.ArraySize member when creating the 
//ShaderResource;
    pSRVDSVArray = CreateShaderResource2DArray(pTex, DXGI_FORMAT_R16_UNORM, MaxWanted);
        
#ifdef SM_CASCADE2DARRAY
        //create a DepthStencil as 2DArray of MaxWanted slices in D16 format.  
        //See function details below. An equivalent function can be used for creating 
        //shaderresource array
        pDSVArray = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, MaxWanted, 0);
#endif
#ifdef SM_CASCADEARRAY_OF_2D
        for (DWORD c = 0; c < MaxWanted; c++)
        {
            pDSV[c] = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, 1, c);
        }
#endif
        pTex->Release();
    }


//To create DSV as array of 2D from Texture2DArray:
//  FirstSlice = index of slice in the range [0.. MaxWanted -1]
//  ArraySize = 1;
//To create DSV as 2DArray from Texture2DArray:
//  FirstSlice = 0
//  ArraySize = MaxWanted;
//MipSlice always 0, I don't use mipmaps

ID3D11DepthStencilView* CreateDepthStencilViewFromArray(ID3D11Texture2D* pText2D, DXGI_FORMAT Format, DWORD ArraySize, DWORD FirstSlice)
{
    ID3D11DepthStencilView* pDSV = NULL;
    ID3D11Resource* pRes;

    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    ZeroMemory(&descDSV, sizeof(descDSV));
    descDSV.Format = Format;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
    descDSV.Texture2D.MipSlice = 0;
    descDSV.Texture2DArray.FirstArraySlice = FirstSlice;
    descDSV.Texture2DArray.ArraySize = ArraySize;
    descDSV.Texture2DArray.MipSlice = 0;
    HRESULT hr = gpD11->CreateDepthStencilView(pText2D, &descDSV, &pDSV);
    if (FAILED(hr))
    {
        OutputDebugStringA("CreateDepthStencilViewFromArray failed hresult invalidarg\n");
        return NULL;
    }
    return pDSV;
}
DWORD MaxWanted = 3;
ID3D11Texture2D* pTex;

//create a Texture2DArray of 3 slices in typeless format compatible 
//with depthstencil format. Pass the number of slices MaxWanted to 
//TexDesc.ArraySize member and the TexDesc.BindFlags as
// D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE 
//when creating the texture to be able to create depthstencils and 
//shaderresources from it.

pTex = CreateTexture2DArray(Width, Height, DXGI_FORMAT_R16_TYPELESS, D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE, MaxWanted);
if (pTex)
{
//create a ShaderResource2DArray of MaxWanted slices in R16 format. Pass the number of 
//slices MaxWanted to ShaderDesc.ArraySize member when creating the 
//ShaderResource;
    pSRVDSVArray = CreateShaderResource2DArray(pTex, DXGI_FORMAT_R16_UNORM, MaxWanted);
        
#ifdef SM_CASCADE2DARRAY
        //create a DepthStencil as 2DArray of MaxWanted slices in D16 format.  
        //See function details below. An equivalent function can be used for creating 
        //shaderresource array
        pDSVArray = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, MaxWanted, 0);
#endif
#ifdef SM_CASCADEARRAY_OF_2D
        for (DWORD c = 0; c < MaxWanted; c++)
        {
            pDSV[c] = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, 1, c);
        }
#endif
        pTex->Release();
    }


//To create DSV as array of 2D from Texture2DArray:
//  FirstSlice = index of slice in the range [0.. MaxWanted -1]
//  ArraySize = 1;
//To create DSV as 2DArray from Texture2DArray:
//  FirstSlice = 0
//  ArraySize = MaxWanted;
//MipSlice always 0, I don't use mipmaps

ID3D11DepthStencilView* CreateDepthStencilViewFromArray(ID3D11Texture2D* pText2D, DXGI_FORMAT Format, DWORD ArraySize, DWORD FirstSlice)
{
    ID3D11DepthStencilView* pDSV = NULL;

    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    ZeroMemory(&descDSV, sizeof(descDSV));
    descDSV.Format = Format;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
    descDSV.Texture2D.MipSlice = 0;
    descDSV.Texture2DArray.FirstArraySlice = FirstSlice;
    descDSV.Texture2DArray.ArraySize = ArraySize;
    descDSV.Texture2DArray.MipSlice = 0;
    HRESULT hr = gpD11->CreateDepthStencilView(pText2D, &descDSV, &pDSV);
    if (FAILED(hr))
    {
        OutputDebugStringA("CreateDepthStencilViewFromArray failed hresult invalidarg\n");
        return NULL;
    }
    return pDSV;
}
added 3273 characters in body
Source Link
philB
  • 333
  • 2
  • 11

To complete the answer as suggested by LudoProf: the following code generates either an array of 2D depthstencils or a 2Darray of depthstencil from a texture2Darray. For use in shaders a shaderresource is also created as a 2DArray but one can create it as an array of 2D as well. Finally one can create at the same time an array of 2D Depthstencil AND a depthstencil 2Darray from the same texture. This can be usefull if the same surfaces are used for a regular single shadowmap where only one DSV is needed (and its shaderresource) or cascaded shadows (what I'm doing).

I have slightly changed the CreateDepthStencilViewFromArray function so that the same can be used to create eithr an array of 2D or a 2Darray of depthstencils.

DWORD MaxWanted = 3;
ID3D11Texture2D* pTex;

//create a Texture2DArray of 3 slices in typeless format compatible 
//with depthstencil format. Pass the number of slices MaxWanted to 
//TexDesc.ArraySize member when creating the texture;

pTex = CreateTexture2DArray(Width, Height, DXGI_FORMAT_R16_TYPELESS, D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE, MaxWanted);
if (pTex)
{
//create a ShaderResource2DArray of MaxWanted slices in R16 format. Pass the number of 
//slices MaxWanted to ShaderDesc.ArraySize member when creating the 
//ShaderResource;
    pSRVDSVArray = CreateShaderResource2DArray(pTex, DXGI_FORMAT_R16_UNORM, MaxWanted);
        
#ifdef SM_CASCADE2DARRAY
        //create a DepthStencil as 2DArray of MaxWanted slices in D16 format.  
        //See function details below. An equivalent function can be used for creating 
        //shaderresource array
        pDSVArray = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, MaxWanted, 0);
#endif
#ifdef SM_CASCADEARRAY_OF_2D
        for (DWORD c = 0; c < MaxWanted; c++)
        {
            pDSV[c] = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, 1, c);
        }
#endif
        pTex->Release();
    }


//To create DSV as array of 2D from Texture2DArray:
//  FirstSlice = index of slice in the range [0.. MaxWanted -1]
//  ArraySize = 1;
//To create DSV as 2DArray from Texture2DArray:
//  FirstSlice = 0
//  ArraySize = MaxWanted;
//MipSlice always 0, I don't use mipmaps

ID3D11DepthStencilView* CreateDepthStencilViewFromArray(ID3D11Texture2D* pText2D, DXGI_FORMAT Format, DWORD ArraySize, DWORD FirstSlice)
{
    ID3D11DepthStencilView* pDSV = NULL;
    ID3D11Resource* pRes;

    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    ZeroMemory(&descDSV, sizeof(descDSV));
    descDSV.Format = Format;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
    descDSV.Texture2D.MipSlice = 0;
    descDSV.Texture2DArray.FirstArraySlice = FirstSlice;
    descDSV.Texture2DArray.ArraySize = ArraySize;
    descDSV.Texture2DArray.MipSlice = 0;
    HRESULT hr = gpD11->CreateDepthStencilView(pText2D, &descDSV, &pDSV);
    if (FAILED(hr))
    {
        OutputDebugStringA("CreateDepthStencilViewFromArray failed hresult invalidarg\n");
        return NULL;
    }
    return pDSV;
}

The equivalent function for shader resources will use the D3D11_SHADER_RESOURCE_VIEW_DESC structure with the corresponding D3D11_SRV_DIMENSION_TEXTURE2DARRAY for the D3D11_SHADER_RESOURCE_VIEW_DESC.ViewDimension member

To complete the answer as suggested by LudoProf: the following code generates either an array of 2D depthstencils or a 2Darray of depthstencil from a texture2Darray. For use in shaders a shaderresource is also created as a 2DArray but one can create it as an array of 2D as well. Finally one can create at the same time an array of 2D Depthstencil AND a depthstencil 2Darray from the same texture. This can be usefull if the same surfaces are used for a regular single shadowmap where only one DSV is needed (and its shaderresource) or cascaded shadows (what I'm doing).

I have slightly changed the CreateDepthStencilViewFromArray function so that the same can be used to create eithr an array of 2D or a 2Darray of depthstencils.

DWORD MaxWanted = 3;
ID3D11Texture2D* pTex;

//create a Texture2DArray of 3 slices in typeless format compatible 
//with depthstencil format. Pass the number of slices MaxWanted to 
//TexDesc.ArraySize member when creating the texture;

pTex = CreateTexture2DArray(Width, Height, DXGI_FORMAT_R16_TYPELESS, D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE, MaxWanted);
if (pTex)
{
//create a ShaderResource2DArray of MaxWanted slices in R16 format. Pass the number of 
//slices MaxWanted to ShaderDesc.ArraySize member when creating the 
//ShaderResource;
    pSRVDSVArray = CreateShaderResource2DArray(pTex, DXGI_FORMAT_R16_UNORM, MaxWanted);
        
#ifdef SM_CASCADE2DARRAY
        //create a DepthStencil as 2DArray of MaxWanted slices in D16 format.  
        //See function details below. An equivalent function can be used for creating 
        //shaderresource array
        pDSVArray = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, MaxWanted, 0);
#endif
#ifdef SM_CASCADEARRAY_OF_2D
        for (DWORD c = 0; c < MaxWanted; c++)
        {
            pDSV[c] = CreateDepthStencilViewFromArray(pTex, DXGI_FORMAT_D16_UNORM, 1, c);
        }
#endif
        pTex->Release();
    }


//To create DSV as array of 2D from Texture2DArray:
//  FirstSlice = index of slice in the range [0.. MaxWanted -1]
//  ArraySize = 1;
//To create DSV as 2DArray from Texture2DArray:
//  FirstSlice = 0
//  ArraySize = MaxWanted;
//MipSlice always 0, I don't use mipmaps

ID3D11DepthStencilView* CreateDepthStencilViewFromArray(ID3D11Texture2D* pText2D, DXGI_FORMAT Format, DWORD ArraySize, DWORD FirstSlice)
{
    ID3D11DepthStencilView* pDSV = NULL;
    ID3D11Resource* pRes;

    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    ZeroMemory(&descDSV, sizeof(descDSV));
    descDSV.Format = Format;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
    descDSV.Texture2D.MipSlice = 0;
    descDSV.Texture2DArray.FirstArraySlice = FirstSlice;
    descDSV.Texture2DArray.ArraySize = ArraySize;
    descDSV.Texture2DArray.MipSlice = 0;
    HRESULT hr = gpD11->CreateDepthStencilView(pText2D, &descDSV, &pDSV);
    if (FAILED(hr))
    {
        OutputDebugStringA("CreateDepthStencilViewFromArray failed hresult invalidarg\n");
        return NULL;
    }
    return pDSV;
}

The equivalent function for shader resources will use the D3D11_SHADER_RESOURCE_VIEW_DESC structure with the corresponding D3D11_SRV_DIMENSION_TEXTURE2DARRAY for the D3D11_SHADER_RESOURCE_VIEW_DESC.ViewDimension member

Source Link
philB
  • 333
  • 2
  • 11
Loading