Jump to content
  • Advertisement
Sign in to follow this  
amadeus12

what did I wrong about computeshader

This topic is 542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wrote computeshader to blur image.

But it returns black texture.

actually I'm not using FX file and effect library.

I use hlsl file and I bind it to pipeline myself with direct api.

I have 2 hlsl files which do vertical, horizontal blur.

here my CPU code which executes computeshader.

void BoxApp::callComputeShaderandBlur(ID3D11DeviceContext * dc, ID3D11ShaderResourceView * inputSRV, ID3D11UnorderedAccessView * inputUAV, int blurcount)
{

    for (int i = 0; i < blurcount; i++)
    {
        dc->CSSetShader(m_CSH, 0, 0);
        dc->CSSetShaderResources(0, 1, &inputSRV);
        dc->CSSetUnorderedAccessViews(0, 1, &mBlurOutPutTexUAV, 0);
      
        UINT numGroupsX = (UINT)ceilf(m_Width / 256.0f);
        dc->Dispatch(numGroupsX, m_Height, 1);
       
        dc->CSSetShaderResources(1, 0, 0);
        dc->CSSetUnorderedAccessViews(1, 0, 0, 0);

        dc->CSSetShader(m_CSV, 0, 0);
        dc->CSSetShaderResources(0, 1, &mBlurOutPutTexSRV);
        dc->CSSetUnorderedAccessViews(0, 1, &inputUAV, 0);
        UINT numGroupY = (UINT)ceilf(m_Height / 256.0f);
        dc->Dispatch(m_Width, numGroupY, 1);

        dc->CSSetShaderResources(1, 0, 0);
        dc->CSSetUnorderedAccessViews(1, 0, 0, 0);

    }
    dc->CSSetShaderResources(1, 0, 0);
    dc->CSSetUnorderedAccessViews(1, 0, 0, 0);
    dc->CSSetShader(0, 0, 0);
}

If I don't call this function, everything is fine. (I rendered my scene to off screen redertarget and use this texture as quad texture. and render it to real rendertarget. it worked fined)

That means there's problem in ComputeShader code.

Every resource and view isn't null pointer, I checked it.

all HRESULTs are S_OK.

 

here my 2 shader codes
 

this is CSH.hlsl

static float gWeights[11] =
{
    0.05f, 0.05f, 0.1f, 0.1f, 0.1f, 0.2f, 0.1f, 0.1f, 0.1f, 0.05f, 0.05f,
};
static const int gBlurRadius = 5;
Texture2D gInput;
RWTexture2D<float4> gOutput;
#define N 256
#define CacheSize (N + 2*gBlurRadius)
groupshared float4 gCache[CacheSize];

[numthreads(N, 1, 1)]
void main(int3 groupThreadID : SV_GroupThreadID,
    int3 dispatchThreadID : SV_DispatchThreadID)
{
    //
    // Fill local thread storage to reduce bandwidth.  To blur 
    // N pixels, we will need to load N + 2*BlurRadius pixels
    // due to the blur radius.
    //

    // This thread group runs N threads.  To get the extra 2*BlurRadius pixels, 
    // have 2*BlurRadius threads sample an extra pixel.
    if (groupThreadID.x < gBlurRadius)
    {
        // Clamp out of bound samples that occur at image borders.
        int x = max(dispatchThreadID.x - gBlurRadius, 0);
        gCache[groupThreadID.x] = gInput[int2(x, dispatchThreadID.y)];
    }
    if (groupThreadID.x >= N - gBlurRadius)
    {
        // Clamp out of bound samples that occur at image borders.
        int x = min(dispatchThreadID.x + gBlurRadius, gInput.Length.x - 1);
        gCache[groupThreadID.x + 2 * gBlurRadius] = gInput[int2(x, dispatchThreadID.y)];
    }

    // Clamp out of bound samples that occur at image borders.
    gCache[groupThreadID.x + gBlurRadius] = gInput[min(dispatchThreadID.xy, gInput.Length.xy - 1)];

    // Wait for all threads to finish.
    GroupMemoryBarrierWithGroupSync();

    //
    // Now blur each pixel.
    //

    float4 blurColor = float4(0, 0, 0, 0);

    [unroll]
    for (int i = -gBlurRadius; i <= gBlurRadius; ++i)
    {
        int k = groupThreadID.x + gBlurRadius + i;

        blurColor += gWeights[i + gBlurRadius] * gCache[k];
    }

    gOutput[dispatchThreadID.xy] = blurColor;
}

and this is CSV

 

static float gWeights[11] =
{
        0.05f, 0.05f, 0.1f, 0.1f, 0.1f, 0.2f, 0.1f, 0.1f, 0.1f, 0.05f, 0.05f,
};
static const int gBlurRadius = 5;
Texture2D gInput;
RWTexture2D<float4> gOutput;

#define N 256
#define CacheSize (256 + 2*5)
groupshared float4 gCache[CacheSize];


[numthreads(1, N, 1)]
void main(int3 groupThreadID : SV_GroupThreadID,
    int3 dispatchThreadID : SV_DispatchThreadID)
{
    //
    // Fill local thread storage to reduce bandwidth.  To blur 
    // N pixels, we will need to load N + 2*BlurRadius pixels
    // due to the blur radius.
    //

    // This thread group runs N threads.  To get the extra 2*BlurRadius pixels, 
    // have 2*BlurRadius threads sample an extra pixel.
    if (groupThreadID.y < gBlurRadius)
    {
        // Clamp out of bound samples that occur at image borders.
        int y = max(dispatchThreadID.y - gBlurRadius, 0);
        gCache[groupThreadID.y] = gInput[int2(dispatchThreadID.x, y)];
    }
    if (groupThreadID.y >= N - gBlurRadius)
    {
        // Clamp out of bound samples that occur at image borders.
        int y = min(dispatchThreadID.y + gBlurRadius, gInput.Length.y - 1);
        gCache[groupThreadID.y + 2 * gBlurRadius] = gInput[int2(dispatchThreadID.x, y)];
    }

    // Clamp out of bound samples that occur at image borders.
    gCache[groupThreadID.y + gBlurRadius] = gInput[min(dispatchThreadID.xy, gInput.Length.xy - 1)];


    // Wait for all threads to finish.
    GroupMemoryBarrierWithGroupSync();

    //
    // Now blur each pixel.
    //

    float4 blurColor = float4(0, 0, 0, 0);

    [unroll]
    for (int i = -gBlurRadius; i <= gBlurRadius; ++i)
    {
        int k = groupThreadID.y + gBlurRadius + i;

        blurColor += gWeights[i + gBlurRadius] * gCache[k];
    }

    gOutput[dispatchThreadID.xy] = blurColor;
}
 

 

sorry about poor english.

plz help I'm really sad...

I spend whole day for this...

It doesn't work..

feels bad man..

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!