D3D11: Creating a cube map from 6 images

前端 未结 2 1986
借酒劲吻你
借酒劲吻你 2021-02-06 14:36

How do I create a cube map in D3D11 from 6 images? All the examples I\'ve found use only one .dds. Specifically, how do I upload individual faces of the cube texture?

2条回答
  •  自闭症患者
    2021-02-06 14:57

    I know this question is old, and there is already a solution.

    Here is a code example that loads 6 textures from disk and puts them together as a cubemap:

    Precondition:

    ID3D11ShaderResourceView* srv = 0;
    ID3D11Resource* srcTex[6];
    

    Pointer to a ShaderResourceView and an array filled with the six textures from disc. I use the order right, left, top, bottom, front, back.

    // Each element in the texture array has the same format/dimensions.
    D3D11_TEXTURE2D_DESC texElementDesc;
    ((ID3D11Texture2D*)srcTex[0])->GetDesc(&texElementDesc);
    
    D3D11_TEXTURE2D_DESC texArrayDesc;
    texArrayDesc.Width = texElementDesc.Width;
    texArrayDesc.Height = texElementDesc.Height;
    texArrayDesc.MipLevels = texElementDesc.MipLevels;
    texArrayDesc.ArraySize = 6;
    texArrayDesc.Format = texElementDesc.Format;
    texArrayDesc.SampleDesc.Count = 1;
    texArrayDesc.SampleDesc.Quality = 0;
    texArrayDesc.Usage = D3D11_USAGE_DEFAULT;
    texArrayDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    texArrayDesc.CPUAccessFlags = 0;
    texArrayDesc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
    
    ID3D11Texture2D* texArray = 0;
    if (FAILED(pd3dDevice->CreateTexture2D(&texArrayDesc, 0, &texArray)))
        return false;
    
    // Copy individual texture elements into texture array.
    ID3D11DeviceContext* pd3dContext;
    pd3dDevice->GetImmediateContext(&pd3dContext);
    D3D11_BOX sourceRegion;
    
    //Here i copy the mip map levels of the textures
    for (UINT x = 0; x < 6; x++)
    {
        for (UINT mipLevel = 0; mipLevel < texArrayDesc.MipLevels; mipLevel++)
        {
            sourceRegion.left = 0;
            sourceRegion.right = (texArrayDesc.Width >> mipLevel);
            sourceRegion.top = 0;
            sourceRegion.bottom = (texArrayDesc.Height >> mipLevel);
            sourceRegion.front = 0;
            sourceRegion.back = 1;
    
            //test for overflow
            if (sourceRegion.bottom == 0 || sourceRegion.right == 0)
                break;
    
            pd3dContext->CopySubresourceRegion(texArray, D3D11CalcSubresource(mipLevel, x, texArrayDesc.MipLevels), 0, 0, 0, srcTex[x], mipLevel, &sourceRegion);
        }
    }
    
    // Create a resource view to the texture array.
    D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;
    viewDesc.Format = texArrayDesc.Format;
    viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
    viewDesc.TextureCube.MostDetailedMip = 0;
    viewDesc.TextureCube.MipLevels = texArrayDesc.MipLevels;
    
    if (FAILED(pd3dDevice->CreateShaderResourceView(texArray, &viewDesc, &srv)))
        return false;
    

    If anyone reads this question again, maybe try this one. Warning: this function is not threadsafe, because i have to use the deviceContext.

提交回复
热议问题