0
\$\begingroup\$

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?

\$\endgroup\$
  • 1
    \$\begingroup\$ Have you tried viewDesc.Texture2DArray.ArraySize = 1; to make a view into a single slice of the 6-element array at a time? \$\endgroup\$ – DMGregory Jun 29 at 20:22
  • \$\begingroup\$ thank you very much! I thought the view Description describes the whole thing instead of a single "piece" (i.e. the single RTV). It makes sense this way. \$\endgroup\$ – Raildex Jun 29 at 20:42
1
\$\begingroup\$

It looks like you want viewDesc.Texture2DArray.ArraySize = 1; instead of 6.

This represents the number of array slices in the view you're taking, counting from the first slice.

When i = 0, this can be any number from 1 (since a view of 0 slices doesn't make sense) to 6 (the whole array). But when i = 1, this can only go up to 5 - trying to view the 6th slice starting at 1 means accessing the seventh item in a 6-item collection, throwing the error you experienced.

To render into one slice at a time, you don't need more than one slice in your view.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.