c++ - 复制颜色和深度模板缓冲区供以后使用

标签 c++ directx-11

我是 directX 的新手,我的任务是将当前的深度模板和颜色缓冲区复制到纹理中。稍后,该纹理将被复制回颜色/深度模板缓冲区以在旧场景上渲染,而无需渲染洞场景两次。

此代码生成渲染目标:

bool CGraphicsDriverDX11::CreateRenderTargetTexture(UINT nWidth, UINT nHeight, DXGI_FORMAT Format,
                                                ID3D11Texture2D** ppRenderTargetTexture, ID3D11RenderTargetView** ppRenderTargetView,
                                                    ID3D11ShaderResourceView** ppRenderTargetSRV, bool bMultiSample)
{
    D3D11_TEXTURE2D_DESC TextureDesc;
    ZeroMemory(&TextureDesc, sizeof(TextureDesc));
    TextureDesc.Width = nWidth;
    TextureDesc.Height = nHeight;
    TextureDesc.MipLevels = 1;
    TextureDesc.ArraySize = 1;
    TextureDesc.Format = Format;

    if (bMultiSample)
    {
        TextureDesc.SampleDesc.Count = m_nMultiSampleCount;
        TextureDesc.SampleDesc.Quality = m_nMultiSampleQuality;
    }
    else
    {
        TextureDesc.SampleDesc.Count = 1;
        TextureDesc.SampleDesc.Quality = 0;
    }

    TextureDesc.Usage = D3D11_USAGE_DEFAULT;
    TextureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
    TextureDesc.CPUAccessFlags = 0;
    TextureDesc.MiscFlags = 0;

    HRESULT hr = m_pD3D11Device->CreateTexture2D(&TextureDesc, nullptr, ppRenderTargetTexture);

    if (FAILED(hr))
    {
        DebugAssertOnce(UNABLE_TO_CREATE_RENDER_TARGET_TEXTURE);
        return false;
    }

    hr = m_pD3D11Device->CreateRenderTargetView(*ppRenderTargetTexture, nullptr, ppRenderTargetView);

    if (FAILED(hr))
    {
        DebugAssertOnce(UNABLE_TO_CREATE_RENDER_TARGET_VIEW);
        return false;
    }

    if (ppRenderTargetSRV)
    {
        D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
        ZeroMemory(&SRVDesc, sizeof(SRVDesc));

        SRVDesc.Format = TextureDesc.Format;
        SRVDesc.Texture2D.MipLevels = TextureDesc.MipLevels;
        SRVDesc.Texture2D.MostDetailedMip = 0;
        SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;

        hr = m_pD3D11Device->CreateShaderResourceView(*ppRenderTargetTexture, &SRVDesc, ppRenderTargetSRV);

        if (FAILED(hr))
        {
            DebugAssertOnce(UNABLE_TO_CREATE_SHADER_RESOURCE_VIEW);
            return false;
        }
    }

    return true;
}

此代码生成深度缓冲区

bool CGraphicsDriverDX11::CreateDepthTexture(UINT nWidth, UINT nHeight, DXGI_FORMAT Format,
                                             ID3D11Texture2D** ppDepthStencilTexture, ID3D11DepthStencilView** ppDepthStencilView,
                                             ID3D11ShaderResourceView** ppDepthStencilSRV, bool bMultiSample)
{
    D3D11_TEXTURE2D_DESC TextureDesc;
    ZeroMemory(&TextureDesc, sizeof(TextureDesc));
    TextureDesc.Width = nWidth;
    TextureDesc.Height = nHeight;
    TextureDesc.MipLevels = 1;
    TextureDesc.ArraySize = 1;
    TextureDesc.Format = Format;

    if (bMultiSample)
    {
        TextureDesc.SampleDesc.Count = m_nMultiSampleCount;
        TextureDesc.SampleDesc.Quality = m_nMultiSampleQuality;
    }
    else
    {
        TextureDesc.SampleDesc.Count = 1;
        TextureDesc.SampleDesc.Quality = 0;
    }

    TextureDesc.Usage = D3D11_USAGE_DEFAULT;
    TextureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    TextureDesc.CPUAccessFlags = 0;
    TextureDesc.MiscFlags = 0;

    HRESULT hr = m_pD3D11Device->CreateTexture2D(&TextureDesc, nullptr, ppDepthStencilTexture);

    if (FAILED(hr))
    {
        DebugAssertOnce(UNABLE_TO_CREATE_DEPTHBUFFER_TEXTURE);
        return false;
    }

    m_pD3D11Device->CreateDepthStencilView(*ppDepthStencilTexture, nullptr, ppDepthStencilView);

    if (FAILED(hr))
    {
        DebugAssertOnce(UNABLE_TO_CREATE_DEPTHBUFFER_VIEW);
        return false;
    }

    if (ppDepthStencilSRV)
    {
        D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
        ZeroMemory(&SRVDesc, sizeof(SRVDesc));

        SRVDesc.Format = TextureDesc.Format;
        SRVDesc.Texture2D.MipLevels = TextureDesc.MipLevels;
        SRVDesc.Texture2D.MostDetailedMip = 0;
        SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;

        hr = m_pD3D11Device->CreateShaderResourceView(*ppDepthStencilTexture, &SRVDesc, ppDepthStencilSRV);

        if (FAILED(hr))
        {
            DebugAssertOnce(UNABLE_TO_CREATE_SHADER_RESOURCE_VIEW);
            return false;
        }
    }
    return true;
}

现在我尝试复制它:

ResolveSubresource(GetZBufferCopyTexture(), 0, GetDepthStencilBufferTexture(), 0, DXGI_FORMAT_D24_UNORM_S8_UINT);
ResolveSubresource(GetColorCopyTexture(), 0, GetBackBuffer(), 0, DXGI_FORMAT_R8G8B8A8_UNORM);

并尝试将拷贝复制回 rendertarget/depthstencil

ResolveSubresource(GetDepthStencilBufferTexture(), 0, GetZBufferCopyTexture(), 0, DXGI_FORMAT_D24_UNORM_S8_UINT);
ResolveSubresource(GetBackBuffer(), 0, GetColorCopyTexture(), 0, DXGI_FORMAT_R8G8B8A8_UNORM);

但这不能正常工作。我没有看到任何变化。也许我对 directx11 工作原理的理解是完全错误的。 我用 OpenGL 做了这个,我只需要用 blitframebuffer 命令复制 FramebufferObject,它工作得很好。这是同一个项目,所以我确信我以正确的顺序调用了这些命令。但 directx11 对我来说是全新的

编辑: 我还将命令“ResolveSubresource”更改为“CopyResource”,但也没有任何变化

最佳答案

我发现了错误: 我使用了错误的纹理... 现在它工作得很好,顺便说一句,我使用“CopyResource”命令,因为“ResolveSubresource”仅将多重采样资源复制到非多重采样资源中

关于c++ - 复制颜色和深度模板缓冲区供以后使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37786346/

相关文章:

c++ - Directx 11 内存管理

c++ - 绘制多个对象

c++ - DirectX 10/11 常量缓冲区内容未通过

c++ - 按数字和冒号验证字符串

c++ - 为什么 Base-to-Derived 动态转换只允许用于多态类

c++ - 如何从 C/C++ 中的以下函数获取所有参数?

c++ - 快速计算两个数组之间的相等字节数

c++ - 如何在 DirectX 11 中绘制带有曲面 segmentation 的虚线图案 3D 线?

c - ID3D11XEffectMatrixVariable 替换为什么?

c++ - IWebBrowser2:如何强制链接在新窗口中打开?