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

标签 directx directx-11 hlsl

我目前正在我的 game engine 中实现漫反射(PBR 基于图像的闪电的一部分) 。我已经到了必须拍摄HDR图像并将其转换为立方体贴图的地步。我目前正在使用 EquirectangleToCubemap 着色器,它工作正常。我能够将 HDR 图像投影到(单位)立方体。现在到了我陷入困境的部分,我无法将此立方体转换为立方体贴图。我尝试使用1个TextureCube6 RenderTargetViewShaderResourceView。我的计划是从 FOV 为 90 的不同 View 投影渲染(单位)立方体 6 次,以捕获每个渲染目标中的整个侧面,最后复制每个渲染目标的输出到纹理立方体的相应边。 我不知道该怎么做^。
注意:我使用 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);
    }

提前致谢!

最佳答案

创建渲染 View 时,您没有指定要写入哪些切片。 切片的正确描述是:

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)

还要为您的资源创建着色器 View ,因为您想创建默认的着色器 View ,所以您可以传递 NULL 而不是自制的 D3D11_SHADER_RESOURCE_VIEW_DESC

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

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

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

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

关于directx - 如何将 RTV 输出复制到立方体贴图的一侧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67145557/

相关文章:

DirectX 11 编译单个 HLSL 文件(无效果)

c++ - 如何随着时间不断创建对象? C++游戏

c++ - 如何在 DirectX 9 中将 XMMATRIX 转换为 D3DMATRIX?

directx-11 - 一次渲染到完整的 3D 渲染目标

glsl - HLSL为什么具有语义?

c++ - 尝试创建直接 x 11 窗口时出现链接器错误

c# - 更新到 Sharpdx 2.6.2 后无法编译 hlsl 着色器

c++ - 错误 LNK2019 : unresolved external symbol _wWinMain@16 referenced in function ___tmainCRTStartup

directx - 如何在 MinGW 中编译 DirectX 11 应用程序

debugging - NSight 图形调试无法启动