6

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.

Zlixine
  • 184
  • 2
  • 15
  • You have so much library code that it's not easy to see what might be the problem. For SO you really need to post a concise code snippet to get real help with it. Alternatively, you can work through the [DIrectX Tool Kit tutorials](https://github.com/Microsoft/DirectXTK/wiki/Getting-Started) and in particular [this one](https://github.com/Microsoft/DirectXTK/wiki/Writing-custom-shaders). – Chuck Walbourn Feb 25 '18 at 03:58
  • @ChuckWalbourn I added the resources creation code is that enough? – Zlixine Feb 25 '18 at 14:07
  • Have you walked through the code to see if each step works as you expect? Have you turned on the debug device and looked for diagnostic messages? Debugging computer graphics programs is a challenge because any number of things can lead to a 'blank screen'. – Chuck Walbourn Feb 26 '18 at 02:11
  • @ChuckWalbourn Actually I walked through the code multiple times, the debug output doesn't complain about anything and all resources creation return S_OK, also I am using DirectX10.1 feature level. btw I ran and captured another log in visual studio graphics debugger, I noticed that at each "Draw" function the PS Shader is bound but has this text written on it "Stage didn't run, no output", weird... – Zlixine Feb 26 '18 at 23:18
  • Check your viewport settings (i.e. ``RSSetViewports``) and the constant buffer matrix values in particular as that means all your geometry was clipped. Graphics debugging at some point becomes a lot of trial and error . – Chuck Walbourn Feb 27 '18 at 06:52
  • @ChuckWalbourn The viewport settings are correct, and there isn't a constant buffer in the PixelShader, also the vertex shader is working fine in transforming the vertices. – Zlixine Feb 28 '18 at 01:48
  • Can you share render doc capture? – Michael Nastenko Mar 02 '18 at 12:50
  • @MichaelNastenko See the answer. – Zlixine Mar 06 '18 at 00:58

1 Answers1

3

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.

Zlixine
  • 184
  • 2
  • 15
  • The depth in the DSV is used to determine if the pixel which should be drawn in the PixelShader is in front or back of the already drawn pixel. If in front, the pixels depth is written to the depthmask in the DSV. So for the first frame it has worked because the depthmask just contained "far away" values. Without clearing to "far away" all pixels got rejected because there's already something on the same depth at this position. when you check your DepthState setting there's probably something like DepthFunc=LESS. LESS_EQUAL should produce some z-fighting artifacts when not clearing the DSV. – Auskennfuchs Mar 08 '18 at 11:12