1
\$\begingroup\$

I want to create a full screen Triangle/Quad so I can blur the box that is the quad I made. I want to do this in the vertex buffer. I tried this code:

    struct VSQuadOut {
    float4 position : SV_POSITION;
    float2 uv: TEXCOORD;
};

// outputs a full screen triangle with screen-space coordinates
// input: three empty vertices
VSQuadOut VSQuad( uint vertexID : SV_VertexID ){
    VSQuadOut result;
    result.uv = float2((vertexID << 1) & 2, vertexID & 2);
    result.position = float4(result.uv * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);
    return result;
}

I want something like this enter image description here

Any ideas?

\$\endgroup\$
  • \$\begingroup\$ Do you want to know how to do the blur shader, or just how to do the quad? \$\endgroup\$ – Tinfoilboy Jul 16 '16 at 3:52
1
\$\begingroup\$

That is my blur shader, it does a simple gaussian blur first in x direction. Then changing rendertarget to be the texture, and render in y direction to the final rendertarget.

//////////////////////////
//       OUTPUT

struct VertexOut
{
    float4 PosH     : SV_POSITION;
    float2 Tex      : TEXCOORD;
};

//////////////////////////
//     Screen Quad

VertexOut VS(uint id : SV_VertexId)
{
    VertexOut vout;
    vout.Tex = float2(id % 2, (id % 4) >> 1);
    vout.PosH = float4((vout.Tex.x - 0.5f) * 2, -(vout.Tex.y - 0.5f) * 2, 0, 1);
    return vout;
}

//////////////////////////
//        Blur
float pixelOffset[7] = 
{
    -3, -2, -1, 0, 1, 2, 3
};

float blurFactor[7] =
{
    1, 3, 5, 9, 5, 3, 1
};

float4 PS_BlurX(VertexOut pin) : SV_TARGET
{
    float4 sum = float4(0.0f, 0.0f, 0.0f, 0.0f);

    [unroll]
    for (int i = 0; i < 7; i++)
    {
        float4 col = txBlur.Sample(sampleLinear, pin.Tex + float2(pixelOffset[i], 0));
        sum += col * blurFactor[i];
    }
    return sum / 27;
}

float4 PS_BlurY(VertexOut pin) : SV_TARGET
{
    float4 sum = float4(0.0f, 0.0f, 0.0f, 0.0f);

    [unroll]
    for (int i = 0; i < 7; i++)
    {
        float4 col = txBlur.Sample(sampleLinear, pin.Tex + float2(0, pixelOffset[i]));
        sum += col * blurFactor[i];
    }
    return sum / 27;
}

//////////////////////////
//      TECHNIQUES

technique11 BlurTech
{
    pass P0
    {
        SetVertexShader(CompileShader(vs_5_0, VS()));
        SetPixelShader(CompileShader(ps_5_0, PS_BlurX()));
        SetDepthStencilState(noDepth, 0);
        SetRasterizerState(0);
    }

    pass P1
    {
        SetVertexShader(CompileShader(vs_5_0, VS()));
        SetPixelShader(CompileShader(ps_5_0, PS_BlurY()));
        SetDepthStencilState(noDepth, 0);
        SetRasterizerState(0);
    }
};

And here is the source code:

//render screen quads
    pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
    pd3dImmediateContext->IASetVertexBuffers(0, 0, NULL, NULL, NULL);
    pd3dImmediateContext->IASetInputLayout(NULL);

    //set viewport
    pd3dImmediateContext->RSSetViewports(1, &g_Viewport.Blur);

    //blur x
    pd3dImmediateContext->OMSetRenderTargets(1, &g_RTV.BlurTemp, NULL);
    BlurShader::txBlur->SetResource(g_SRV.Blur);
    BlurShader::blurTech->GetPassByIndex(0)->Apply(0, pd3dImmediateContext);
    pd3dImmediateContext->Draw(4, 0);

    //free shader resource
    BlurShader::txBlur->SetResource(0);
    BlurShader::blurTech->GetPassByIndex(0)->Apply(0, pd3dImmediateContext);

    //blur y
    pd3dImmediateContext->OMSetRenderTargets(1, &g_RTV.Blur, NULL);
    BlurShader::txBlur->SetResource(g_SRV.BlurTemp);
    BlurShader::blurTech->GetPassByIndex(1)->Apply(0, pd3dImmediateContext);
    pd3dImmediateContext->Draw(4, 0);

    //free shader resource
    BlurShader::txBlur->SetResource(0);
    BlurShader::blurTech->GetPassByIndex(0)->Apply(0, pd3dImmediateContext);
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.