# Crash when creating RenderTargetViews for Cubemap

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.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?

• Have you tried viewDesc.Texture2DArray.ArraySize = 1; to make a view into a single slice of the 6-element array at a time? – DMGregory Jun 29 at 20:22
• 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. – Raildex Jun 29 at 20:42

It looks like you want viewDesc.Texture2DArray.ArraySize = 1; instead of 6.
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.