0
votes

Question: How can I render to target through HLSL by returning a struct with semantic mappings?

Hi there. I'm new to DirectX11 and am trying to set up render to texture so I can implement deferred shading. I've spent over a day scouring the net for a solution to my problem, but have had no luck so far.

Currently, I can render my scene to a single texture through HLSL and I finish by rendering that texture to a full-screen quad without problems. However if I bind multiple render targets, all I ever see is the clear color for my targets, the pixel shader never seems to get to any bound textures. I have D3D11_CREATE_DEVICE_DEBUG set with no warnings/errors at runtime and I've tried using the Visual Studio 2013 graphics debugging tool, but am having a difficult time understanding what the issue is. Here is probably the relevant source code:

Shader Code: (compiled with vs_5_0, ps_5_0)

// vertex shader
cbuffer mTransformationBuffer
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
};

struct VertexInputType
{
    float4 position : POSITION;
    float4 color    : COLOR;
    float3 normal   : NORMAL;
};
struct PixelInputType
{
    float4 position : SV_POSITION;
    float4 color    : COLOR;
    float3 normal   : NORMAL;
};
// this does not seem to work!
struct PixelOutputType
{
    float4 position : SV_TARGET0;
    float4 normal   : SV_TARGET1;
};


PixelInputType VertexEntry(VertexInputType input)
{
    input.position.w = 1.0f;

    PixelInputType output;
    output.position = mul(input.position, worldMatrix);
    output.normal = mul(input.normal, (float3x3)worldMatrix);
    output.normal = normalize(output.normal);

    output.color = input.color;

    return output;
}

// fragment shader
PixelOutputType FragmentEntry(PixelInputType input)
{
    PixelOutputType output;

    // render as white so I can see MRT working
    output.position = float4(1.0f, 1.0f, 1.0f, 1.0f);
    output.normal = float4(1.0f, 1.0f, 1.0f, 1.0f);

    return output;
}

Render Target Creation:

void RenderTargetManager::createTarget(const std::string& name, unsigned int width, unsigned int height, DXGI_FORMAT format)
{
unsigned int hash = hashString(name);

RenderTarget target;

// create the texture to render to
target.mTexture = TextureUtils::createTexture2d(mDevice, width, height, format, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);

// create the render target view
D3D11_RENDER_TARGET_VIEW_DESC rtViewDesc;
memset(&rtViewDesc, 0, sizeof(rtViewDesc));

rtViewDesc.Format = format;
rtViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;

HRESULT ret = S_OK;
ret = mDevice.CreateRenderTargetView(target.mTexture, &rtViewDesc, &target.mRenderTargetView);
ASSERT(ret == S_OK, "Failed to create a render target view.");

// create the shader resource view
D3D11_SHADER_RESOURCE_VIEW_DESC srViewDesc;
memset(&srViewDesc, 0, sizeof(srViewDesc));

srViewDesc.Format = format;
srViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srViewDesc.Texture2D.MipLevels = 1;

ret = mDevice.CreateShaderResourceView(target.mTexture, &srViewDesc, &target.mShaderResourceView);
ASSERT(ret == S_OK, "Failed to create a shader resource view.");

mRenderTargetMap[hash] = target;
}

Setting Render Targets:

void RenderTargetManager::setTargets(ID3D11DepthStencilView* depthStencilView, unsigned int numTargets, ...)
{
    va_list args;
    va_start(args, numTargets);

    std::vector<ID3D11RenderTargetView*> renderTargetViews;

    for (unsigned int i = 0; i < numTargets; ++i)
    {
        std::string renderTargetName = va_arg(args, char*);
        unsigned int hash = hashString(renderTargetName);

        auto it = mRenderTargetMap.find(hash);
        if (it != mRenderTargetMap.end())
        {
            renderTargetViews.push_back(it->second.mRenderTargetView);
        }
        else
        {
            LOGW("Render target view '%s' not found.", renderTargetName.c_str());
        }
    }

    va_end(args);

    if (!renderTargetViews.empty())
    {
        ASSERT(renderTargetViews.size() == numTargets, "Failed to set render targets. Not all targets found.");
        mDeviceContext.OMSetRenderTargets(renderTargetViews.size(), &renderTargetViews[0], depthStencilView);
    }
}

My pixel shader will write to any one of these targets if it returns a float4, returning a struct never seems to work and results in only the clear color for the render target. I have also tried this as the return struct without success:

struct PixelOutputType
{
    float4 position : SV_TARGET
};

Thank you for taking the time to read this. I really appreciate the help.

-T

EDIT: I have also tried using a static array of ID3D11RenderTargetView*, with the same issues. EDIT2: I also enabled DirectX Control Panel output and see nothing out of the ordinary.

1

1 Answers

0
votes

After a lot of tests and frustration, I finally discovered my issue ;__;

There were no problems with my textures, views, or states, but there was an issue with the shader and SV_POSITION.

I was mistakenly trying to use my pixel shader input 'float4 position : SV_TARGET' as my output position and only transformed it to world coordinates in the vertex shader. After thinking about this for a while I realized that this MUST be the fully transformed point as an input to the pixel shader and sure enough, fully transforming it fixed the issue.

THIS:

output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);

instead of this:

output.position = mul(input.position, worldMatrix);
//output.position = mul(output.position, viewMatrix);
//output.position = mul(output.position, projectionMatrix);