1
\$\begingroup\$

I have two render targets, the back buffer and another target that I am using to draw UI (at the moment this is only a quad on-screen).

I have gone through the graphics debugging and checked that both render targets are being drawn to correctly, but I can't seem to merge them together, or overlay one ontop of the other. I only seem to see the cube being rendered on the screen as this is being drawn directly to the backbuffer.

enter image description here enter image description here

How can I draw my custom render target onto the backbuffer so it will display over the cube?


Create render target:

ID3D11Texture2D* m_renderTargetTexture;
    ID3D11RenderTargetView* m_renderTargetView;
    ID3D11ShaderResourceView* m_shaderResourceView;

    D3D11_TEXTURE2D_DESC textureDesc;
    HRESULT result;
    D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
    D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;


    // Initialize the render target texture description.
    ZeroMemory(&textureDesc, sizeof(textureDesc));

    // Setup the render target texture description.
    textureDesc.Width = 948;//this needs to be the same size as the depthstencilbuffer?
    textureDesc.Height = 907;//this needs to be the same size as the depthstencilbuffer?
    textureDesc.MipLevels = 1;
    textureDesc.ArraySize = 1;
    textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
    textureDesc.SampleDesc.Count = 1;
    textureDesc.Usage = D3D11_USAGE_DEFAULT;
    textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
    textureDesc.CPUAccessFlags = 0;
    textureDesc.MiscFlags = 0;

    // Create the render target texture.
    result = m_deviceResources->GetD3DDevice()->CreateTexture2D(&textureDesc, NULL, &m_renderTargetTexture);
    if (FAILED(result))
    {
        return;
    }

    // Setup the description of the render target view.
    renderTargetViewDesc.Format = textureDesc.Format;
    renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
    renderTargetViewDesc.Texture2D.MipSlice = 0;

    // Create the render target view.
    result = m_deviceResources->GetD3DDevice()->CreateRenderTargetView(m_renderTargetTexture, &renderTargetViewDesc, &m_renderTargetView);
    if (FAILED(result))
    {
        return;
    }

    // Setup the description of the shader resource view.
    shaderResourceViewDesc.Format = textureDesc.Format;
    shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
    shaderResourceViewDesc.Texture2D.MipLevels = 1;

    // Create the shader resource view.
    result = m_deviceResources->GetD3DDevice()->CreateShaderResourceView(m_renderTargetTexture, &shaderResourceViewDesc, &m_shaderResourceView);
    if (FAILED(result))
    {
        return;
    }

Draw to render target:

context->OMSetRenderTargets(1, &m_renderTargetView, m_deviceResources->GetDepthStencilView());
context->ClearRenderTargetView(m_renderTargetView, DirectX::Colors::Blue);
//now drawing to the render target...


// Prepare the constant buffer to send it to the graphics device.
orientation = m_deviceResources->GetOrientationTransform3D();
orientationMatrix = XMLoadFloat4x4(&orientation);

XMMATRIX orthographicMatrix = XMLoadFloat4x4(&m_orthographicMatrix);
XMStoreFloat4x4(
    &m_constantBufferData.projection,
    XMMatrixTranspose(XMMatrixScaling(0.5f, 0.5f, 0.0f) * XMMatrixTranslation(0.5f, -0.5f, 0.0f)) //XMMatrixTranspose(orthographicMatrix * orientationMatrix)
);

context->UpdateSubresource1(
    m_constantBuffer.Get(),
    0,
    NULL,
    &m_constantBufferData,
    0,
    0,
    0
);


////////////////

stride = sizeof(VertexPositionColor);
offset = 0;
context->IASetVertexBuffers(
    0,
    1,
    m_quadVertexBuffer.GetAddressOf(),
    &stride,
    &offset
);

context->IASetIndexBuffer(
    m_quadIndexBuffer.Get(),
    DXGI_FORMAT_R16_UINT, // Each index is one 16-bit unsigned integer (short).
    0
);

context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

context->IASetInputLayout(m_inputLayout.Get());

// Attach our vertex shader.
context->VSSetShader(
    m_vertexShader.Get(),
    nullptr,
    0
);

// Send the constant buffer to the graphics device.
context->VSSetConstantBuffers1(
    0,
    1,
    m_constantBuffer.GetAddressOf(),
    nullptr,
    nullptr
);

// Attach our pixel shader.
context->PSSetShader(
    m_pixelShader.Get(),
    nullptr,
    0
);

// Draw the objects.
context->DrawIndexed(
    6,
    0,
    0
);

I have tried using

context->CopyResource(backBufferResource, renderTargetResource);

However this draws the entire render target to the backbuffer, including the solid background and hides/overwrites the cube.

The expected output is seen in the image below, however this is directly drawing the UI and the world objects directly to the backbuffer instead of their own separate render targets

enter image description here

I have also tried making the UI render target have a transparent background and then using CopyResource but this replaces the entire backbuffer content so the cube no longer exists.

What is the correct way to overlay my UI render target onto the backbuffer?

\$\endgroup\$
  • \$\begingroup\$ Right, so then you draw the contents of that populated render target onto your backbuffer as your final step, correct? \$\endgroup\$ – DMGregory Aug 8 '18 at 14:11
  • \$\begingroup\$ @DMGregory I think that's what I'm missing but I'm not sure how to do that \$\endgroup\$ – user9942393 Aug 8 '18 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.