Search code examples
c++renderingtexturesrenderdirectx-11

dx11 Rendering to texture shows only clearcolor


Im trying to create a game view for my game engine using ImGui framework,however when i try to render texture,there is only clear color displayed on the screen.

My plan

  • Creating Render Target Texture(I think i done this part right(i think))
  • Writing my scene into target texture(I presume this is where i failed,only clear color displayed on the screen,my test triangle not displayed)
  • Crearting a shader resource view(I tested this and the next step via loading image from the disk,it shows no problem here)
  • Display it on İmgui::Image()

Using render texture only clear color and Imgui Editor elements displayed on the screen when set back to normal rendering,my test triangle appears on the screen.

My Render Texture Creation

D3D11_TEXTURE2D_DESC textureDesc;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;

ZeroMemory(&textureDesc, sizeof(textureDesc));
textureDesc.Width = 1920;
textureDesc.Height = 1080;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;

renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;

DLE_Graphics::CurrentGraphics->pDevice->CreateTexture2D(&textureDesc, NULL, pTargetTexture.GetAddressOf());


shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;

DLE_Graphics::CurrentGraphics->pDevice->CreateShaderResourceView(this->pTargetTexture.Get(), &shaderResourceViewDesc, this->pResourceView.GetAddressOf());

// (re)-create the render target view
DLE_Graphics::CurrentGraphics->pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(pTargetTexture.GetAddressOf()));
DLE_Graphics::CurrentGraphics->pDevice->CreateRenderTargetView(pTargetTexture.Get(), &renderTargetViewDesc, pRenderTargetView.GetAddressOf());



// create the depth and stencil buffer
D3D11_TEXTURE2D_DESC dsd;
pTargetTexture->GetDesc(&dsd);
dsd.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dsd.Usage = D3D11_USAGE_DEFAULT;
dsd.BindFlags = D3D11_BIND_DEPTH_STENCIL;

DLE_Graphics::CurrentGraphics->pDevice->CreateTexture2D(&dsd, NULL, pDepthTexture.GetAddressOf());
DLE_Graphics::CurrentGraphics->pDevice->CreateDepthStencilView(pDepthTexture.Get(), NULL, pDepthView.GetAddressOf());

// activate the depth and stencil buffer
DLE_Graphics::CurrentGraphics->pContext->OMSetRenderTargets(1, pRenderTargetView.GetAddressOf(), pDepthView.Get());

My Rendering Code

const FLOAT clr[] = { 0.0f, 0.5f, 0.5f, 0.0f };
    pContext->ClearRenderTargetView(pRenderTexture->GetRenderView().Get(), clr);
    pContext->ClearDepthStencilView(pRenderTexture->GetDepthView().Get(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 0u, 0u);
    pContext->OMSetRenderTargets(1u, pRenderTexture->GetRenderView().GetAddressOf(), pRenderTexture->GetDepthView().Get());
    pContext->DrawIndexed(size, 0u, 0u);

    pSwapChain->Present(0u, 0u);

If anyone curious this is my swap chain description

DXGI_SWAP_CHAIN_DESC description = {};
    description.BufferDesc.Width = 1920;
    description.BufferDesc.Height = 1080;
    description.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    description.BufferDesc.RefreshRate.Numerator = 60;
    description.BufferDesc.RefreshRate.Denominator = 1;
    description.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
    description.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    description.SampleDesc.Count = 1;
    description.SampleDesc.Quality = 0;
    description.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT ;
    description.BufferCount = 1;
    description.OutputWindow = hWnd;
    description.Windowed = TRUE;
    description.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
    description.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

When i use my render texture and create a resource view and use ImGui::Image to show there is no image,not even clear color.If i use my image on the disk for the ImGui::Image it displays the image,i dont what im doing wrong here

Here's my imgui code

ImGui::Begin("Spectrum Observer");
    ImVec2 pos = ImGui::GetCursorScreenPos();
    //ImGui::Image(texture->GetResource(), ImVec2(512, 512)); // my image from the disk
    ImGui::Image(pGraphics->pRenderTexture->GetResourceView().Get(), ImVec2(512, 512)); // render texture 
    ImGui::End();
    ImGui::Render();
    ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());

I appreciate every input possible,thank you


Solution

  • I managed to solve the triangle rendering problem via changing ClearDepthStencilView() 0u to 1u

    const FLOAT clr[] = { 0.0f, 0.5f, 0.5f, 0.0f };
        pContext->ClearRenderTargetView(pRenderTexture->GetRenderView().Get(), clr);
        pContext->ClearDepthStencilView(pRenderTexture->GetDepthView().Get(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1u, 0u);
        pContext->OMSetRenderTargets(1u, pRenderTexture->GetRenderView().GetAddressOf(), pRenderTexture->GetDepthView().Get());
        pContext->DrawIndexed(size, 0u, 0u);
    
        pSwapChain->Present(0u, 0u);
    

    Appearently i was clearing the depth buffer to 0 which indicates all depth values nearest possiible which leads to failing ztest.