如何将 RTV 输出复制到立方体贴图的一侧?

How to copy the RTV output to the side of a cubemap?

我目前正在 game engine 中实现漫反射(PBR 的基于图像的闪电的一部分)。我到了必须拍摄 HDR 图像并将其转换为 cubemap 的地步。我目前正在使用 EquirectangularToCubemap 着色器并且它工作正常。我能够将 HDR 图像投影到(单位)立方体。现在是我卡住的部分,我无法将这个立方体变成 cubemap。我尝试使用 1 TextureCube6 RenderTargetViewShaderResourceView。我的计划是从不同的视图投影渲染(单位)立方体 6 次,FOV 为 90,以捕获每个渲染目标中的整个侧面,最后 将渲染目标的每个输出复制到纹理立方体的相应侧。 我不知道该怎么做^.
注意:我使用 DirextX11 作为渲染后端。 这是关于我的问题的伪代码(不起作用)

   glm::mat4 captureProjection = glm::perspective(glm::radians(90.0f), 1.0f, 0.1f, 10.0f);
   glm::mat4 captureViews[] =
    {
        glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f,  0.0f,  0.0f), glm::vec3(0.0f, -1.0f,  0.0f)),
        glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f,  0.0f), glm::vec3(0.0f, -1.0f,  0.0f)),
        glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f,  1.0f,  0.0f), glm::vec3(0.0f,  0.0f,  1.0f)),
        glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f,  0.0f), glm::vec3(0.0f,  0.0f, -1.0f)),
        glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f,  0.0f,  1.0f), glm::vec3(0.0f, -1.0f,  0.0f)),
        glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f,  0.0f, -1.0f), glm::vec3(0.0f, -1.0f,  0.0f))
    };
 
    //Create the TextureCube
    D3D11_TEXTURE2D_DESC textureDesc = {};
    textureDesc.Width = 512;
    textureDesc.Height = 512;
    textureDesc.MipLevels = 1;
    textureDesc.ArraySize = 6;
    textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
    textureDesc.CPUAccessFlags = 0;
    textureDesc.SampleDesc.Count = 1;
    textureDesc.SampleDesc.Quality = 0;
    textureDesc.Usage = D3D11_USAGE_DEFAULT;
    textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
    textureDesc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
    ID3D11Texture2D* tex = nullptr;
    DX_CALL(DX11Internal::GetDevice()->CreateTexture2D(&textureDesc, nullptr, &tex));
    // Create the Shader Resource view for the texture cube
    D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
    srvDesc.Format = textureDesc.Format;
    srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
    srvDesc.Texture2D.MostDetailedMip = 0;
    srvDesc.Texture2D.MipLevels = 1;
    DX_CALL(DX11Internal::GetDevice()->CreateShaderResourceView(tex, &srvDesc, &mSRV));
 
 
    //Create the Render target views
    Vector<ID3D11RenderTargetView*> rtvs;
 
    for (Uint i = 0; i < 6; i++)
    {
        D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc = {};
        renderTargetViewDesc.Format = textureDesc.Format;
        renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
        renderTargetViewDesc.Texture2D.MipSlice = 0;
 
        ID3D11RenderTargetView* view = nullptr;
        DX11Internal::GetDevice()->CreateRenderTargetView(tex, &renderTargetViewDesc, &view);
        rtvs.push_back(view);
    }
    tex->Release();
 
    auto deviceContext = DX11Internal::GetDeviceContext();
    for (Uint i = 0; i < 6; ++i)
    {
        float clearColor[4] = { 1.0f, 0.1f, 0.1f, 1.0f };
        deviceContext->ClearRenderTargetView(rtvs[i], clearColor);
        Vault::Get<Shader>("EquirectangularToCubemap.hlsl")->Bind();
 
        auto data = captureProjection * captureViews[i];
        cbuffer->Bind();
        cbuffer->SetData(&data);
        texture->Bind(0);
        tempPipeline->Bind();
        deviceContext->OMSetRenderTargets(1, &rtvs[i], nullptr);
//I am rendering the cube here from different view projection to capture the faces, but I dont't know where to copy the data to the //side of the TextureCube :( [Note that I am doing this only once]
        RenderCommand::DrawIndexed(tempPipeline, 36);
    }

提前致谢!

您在创建渲染视图时没有指定要写入的切片。 切片的正确描述是:

D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc = {};
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
renderTargetViewDesc.Texture2DArray.MipSlice = 0;
renderTargetViewDesc.Texture2DArray.FirstArraySlice =i;
renderTargetViewDesc.Texture2DArray.ArraySize = 1;

(注意:如果使用 MSAA,ViewDimension 将变为 D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY)

还要为你的资源创建着色器视图,因为你想创建一个默认的,你可以传递 NULL 而不是自制的 D3D11_SHADER_RESOURCE_VIEW_DESC

DX_CALL(DX11Internal::GetDevice()->CreateShaderResourceView(tex, NULL, &mSRV));

您还可以创建另一个渲染视图来一次性映射整个立方体贴图:

DX11Internal::GetDevice()->CreateRenderTargetView(tex, NULL, &cubeMapFullView);

但在那种情况下,您将需要使用几何着色器跨切片复制几何(使用 SV_RenderTargetArrayIndex),或使用提供相同类型功能的供应商特定扩展。