Crash when creating RenderTargetViews for Cubemap

by Raildex   Last Updated June 29, 2020 20:13 PM - source

I want to create a cubemap and I want to render to it and later sample from it for reflections. However, It crashes when I try to create the faces for the cubemap. Here is my code:

RenderTargetCube::RenderTargetCube(ID3D11Device* device, int resolution, DXGI_FORMAT format) {

    D3D11_TEXTURE2D_DESC desc = {};
    desc.Width = resolution;
    desc.Height = resolution;
    desc.MipLevels = 1;
    desc.ArraySize = 6;
    desc.Format = format;
    desc.SampleDesc.Count = 1;
    desc.SampleDesc.Quality = 0;
    desc.Usage = D3D11_USAGE_DEFAULT;
    desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
    desc.CPUAccessFlags = 0;
    desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;

    HRESULT res = device->CreateTexture2D(&desc, NULL, &Texture);
    throwIfFailed(res);

    D3D11_RENDER_TARGET_VIEW_DESC viewDesc = {};
    viewDesc.Format = desc.Format;
    viewDesc.Texture2DArray.ArraySize = 6;
    viewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
    for (int i = 0; i < 6; i++) {
        viewDesc.Texture2DArray.FirstArraySlice = D3D11CalcSubresource(0, i, 1);
        res = device->CreateRenderTargetView(Texture.Get(), &viewDesc, RenderTargetView[i].GetAddressOf()); // crash here
        throwIfFailed(res);

    }

and here is the message I receive:

D3D11 ERROR: ID3D11Device::CreateRenderTargetView: The Dimensions of the View are invalid due to at least one of the following conditions. MipSlice (value = 0) must be between 0 and MipLevels-1 of the Texture Resource, 0, inclusively. FirstArraySlice (value = 1) must be between 0 and ArraySize-1 of the Texture Resource, 5, inclusively. With the current FirstArraySlice, ArraySize (value = 6) must be between 1 and 5, inclusively, or -1 to default to all slices from FirstArraySlice, in order that the View fit on the Texture. [ STATE_CREATION ERROR #137: CREATERENDERTARGETVIEW_INVALIDDIMENSIONS]

I dont quite understand the error message, especially the last part about ArraySize, why does it want it to be between 1 and 5 and not 0 to 5?



Related Questions


DirectX 11 - Skybox Texture messed up

Updated January 10, 2019 21:13 PM