c++directxgame-enginedirectx-11directx-10

DirectX 11 Render To Texture


basically I am trying to render a scene to a texture as in this ogl tutorial here but in DirectX 11, and I faced some issues:

  1. Absolutely nothing is rendered when I launch the program IDK why.
  2. The only thing the texture displays 'correctly' is the clear color. debug build
  3. I have examined the executable in RenderDoc, and in the captured frame the back buffer draws the quad and the texture on it displays the scene correctly! renderdoc capture output

Source code peak:

                D3D11_TEXTURE2D_DESC texDesc;
            ZeroMemory(&texDesc, sizeof(D3D11_TEXTURE2D_DESC));

            texDesc.Width = Data.Width;
            texDesc.Height = Data.Height;
            texDesc.Format = R32G32B32A32_FLOAT;
            texDesc.Usage = D3D11_USAGE_DEFAULT;
            texDesc.SampleDesc.Count = 1;
            texDesc.SampleDesc.Quality = 0;
            texDesc.CPUAccessFlags = 0;
            texDesc.ArraySize = 1;
            texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
            texDesc.MiscFlags = 0;
            texDesc.MipLevels = 1;

            if (Data.Img_Data_Buf == NULL)
            {
                if (FAILED(DX11Context::GetDevice()->CreateTexture2D(&texDesc, NULL, &result->tex2D)))
                {
                    Log.Error("[DirectX] Texture2D Creation Failed for Null-ed Texture2D!\n");
                    return;
                }
                D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
                srvDesc.Format = texDesc.Format;
                srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
                srvDesc.Texture2D.MostDetailedMip = 0;
                srvDesc.Texture2D.MipLevels = 1;

                DX11Context::GetDevice()->CreateShaderResourceView(result->tex2D, &srvDesc, &result->resourceView);

                return;
            }
            //depth stencil texture 
            D3D11_TEXTURE2D_DESC texDesc;
            {
                texDesc.Width = size.x;
                texDesc.Height = size.y;
                texDesc.MipLevels = 1;
                texDesc.ArraySize = 1;
                texDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
                texDesc.SampleDesc.Count = 1;
                texDesc.SampleDesc.Quality = 0;
                texDesc.Usage = D3D11_USAGE_DEFAULT;
                texDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
                texDesc.CPUAccessFlags = 0;
                texDesc.MiscFlags = 0;
            }
            if (FAILED(API::DirectX::DX11Context::GetDevice()->CreateTexture2D(&texDesc, nullptr, &depthstenciltex)))
            {
                Log.Error("[DX11RenderTarget] Failed to create DepthStencilTexture for render-target!\n");
                //Return or the next call will fail too
                return;
            }
            if (FAILED(API::DirectX::DX11Context::GetDevice()->CreateDepthStencilView(depthstenciltex, nullptr, &depthstencilview)))
            {
                Log.Error("[DX11RenderTarget] Failed to create DepthStencilView for render-target!\n");
            }

            //render target
            D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
            ZeroMemory(&renderTargetViewDesc, sizeof(D3D11_RENDER_TARGET_VIEW_DESC));
            renderTargetViewDesc.Format = texDesc.Format;

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

            ID3D11RenderTargetView* rtv;

            if (FAILED(API::DirectX::DX11Context::GetDevice()->CreateRenderTargetView(texture->tex2D, &renderTargetViewDesc, &rtv)))
            {
                Log.Error("[DX11RenderTarget] Failed to create render-target-view (RTV)!\n");
                return;
            }

            //binding
            Context->OMSetRenderTargets(1, &rtv, rt->depthstenciltex);

Shaders:

    std::string VertexShader = R"(struct VertexInputType
{
    float4 position : POSITION;
    float2 tex : TEXCOORD;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD;
};

cbuffer NE_Camera : register(b0)
{
    matrix Model;
    matrix View;
    matrix Projection;
};

PixelInputType main(VertexInputType input)
{
    PixelInputType output;

    // Calculate the position of the vertex against the world, view, and projection matrices.
    output.position = mul(Model, input.position);
    output.position = mul(View, output.position);
    output.position = mul(Projection, output.position);

    // Store the input texture for the pixel shader to use.
    output.tex = input.tex;
    return output;
})";

    std::string PixelShader = R"(
struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD;
};

Texture2D NE_Tex_Diffuse : register(t0);
SamplerState NE_Tex_Diffuse_Sampler : register(s0);

float4 main(PixelInputType input) : SV_TARGET
{
    return NE_Tex_Diffuse.Sample(NE_Tex_Diffuse_Sampler, input.tex);
}
)";
    std::string ScreenVertexShader = R"(struct VertexInputType
{
    float2 position : POSITION;
    float2 tex : TEXCOORD;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD;
};


PixelInputType main(VertexInputType input)
{
    PixelInputType output;

    // CalcSulate the position of the vertex against the world, view, and projection matrices.
    output.position = float4(input.position.x,input.position.y,0.0f,1.0f);
    // Store the input texture for the pixel shader to use.
    output.tex = input.tex;
    return output;
})";

    std::string ScreenPixelShader = R"(
struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD;
};

Texture2D ScreenTexture : register(t0);
SamplerState ScreenTexture_Sampler : register(s0);

float4 main(PixelInputType input) : SV_TARGET
{
    return float4(ScreenTexture.Sample(ScreenTexture_Sampler, input.tex).rgb, 1.0f);
}
)";

Full Source Code

Also I captured a frame with visual studio graphics debugger, and noticed that the render to texture draw call has the PS shader with "stage didn't run, no output".

Note: I know that the scene should be flipped in DirectX.


Solution

  • I have found the bug causing this problem, I wasn't clearing the depth stencil view at rendering, I wonder why is clearing the DSV essential for RenderTarget output.