• Advertisement
Sign in to follow this  

Bizzare shader error

This topic is 3247 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

Hi, something strange is happening that I don't understand. This is the code for my shader that does a guassian blur:
void main( in PS_Input i, out PS_Output o )
{
	float4 TotalColour = 0;
    float texelSize = float( 1.0f / TexWidth );
    
    TotalColour += tex2D( Tex0, float2( i.TexCoord0.x, i.TexCoord0.y ) ) * ( KernelSize+1.0f ) / (100.0f/KernelSize);
    
    int limit = KernelSize;
    
	// Loop as many times as there are kernels
	for( int p=1; p <= KernelSize; p++ )
    {
		TotalColour += tex2D( Tex0, float2( i.TexCoord0.x - ( p*texelSize ), i.TexCoord0.y) ) * (KernelSize+1 -p) / (100.0f/KernelSize);
		TotalColour += tex2D( Tex0, float2( i.TexCoord0.x + ( p*texelSize ), i.TexCoord0.y) ) * (KernelSize+1 -p) / (100.0f/KernelSize);
    }
    
	o.Colour.rgba = TotalColour;
    o.Colour.a = 0.3f;
}
When I try to compile I am getting an error and subsequent warning: warning X3553: Can't use gradient instructions in loops with break, forcing loop to unroll error X3511: Unable to unroll loop, loop does not appear to terminate in a timely manner (1024 iterations) However, if I change the loop parameter to an integer rather than the KernelSize constant it runs and works perfectly. This is the code with that one change:
void main( in PS_Input i, out PS_Output o )
{
	float4 TotalColour = 0;
    float texelSize = float( 1.0f / TexWidth );
    
    TotalColour += tex2D( Tex0, float2( i.TexCoord0.x, i.TexCoord0.y ) ) * ( KernelSize+1.0f ) / (100.0f/KernelSize);
    
    int limit = KernelSize;
    
	// Loop as many times as there are kernels
	for( int p=1; p <= 5; p++ )
    {
		TotalColour += tex2D( Tex0, float2( i.TexCoord0.x - ( p*texelSize ), i.TexCoord0.y) ) * (KernelSize+1 -p) / (100.0f/KernelSize);
		TotalColour += tex2D( Tex0, float2( i.TexCoord0.x + ( p*texelSize ), i.TexCoord0.y) ) * (KernelSize+1 -p) / (100.0f/KernelSize);
    }
    
	o.Colour.rgba = TotalColour;
    o.Colour.a = 0.3f;
}
That is the only thing that is stopping the shader from working correctly, and without that, I can't have any sort of flexibility with the shader. Anybody any ideas why this is happening?

Share this post


Link to post
Share on other sites
Advertisement
OK, this is a bit difficult to explain. First, the reason why the second one works, is because it is automatically unrolled. You loop a constant amount of iteration, so the compiler can silently unroll without side effects.

Now, the reason the first doesn't work as expected, is due to the fact that derivatives (as used by a gradient instruction, such as tex2D) are undefined within a conditional statement (such as the one implicitly used by the loop).

You can solve this by either not using derivatives at all (ie. supplying a constant LOD through tex2DLod rather than the tex2D) or by manually computing the derivatives outside of the loop, and supplying them explicitly. If you only want to blur a screen aligned rectangle, then the best method is the former one.

Share this post


Link to post
Share on other sites
Ok thanks for the reply I understand. I just looked up tex2Dlod on msdn, and it states that the second input parameter, i.e. the UV coords, should be a float4. How can there be 4 part coordinate for a texture UV??

Share this post


Link to post
Share on other sites
I just solved the problem by making it a float4 and adding a couple of 1.0f's in the constructor and it seems to work. Don't really understand why though...

Share this post


Link to post
Share on other sites
Quote:
Original post by DOrmisher
Ok thanks for the reply I understand. I just looked up tex2Dlod on msdn, and it states that the second input parameter, i.e. the UV coords, should be a float4. How can there be 4 part coordinate for a texture UV??

Because some texture operations require 3-dimensional coordinates (UVW, for example volume textures) or 4-dimensional homogeneous ones (projective texturing). If you don't need the last two components, you should set them to 0 and 1, respectively.

Share this post


Link to post
Share on other sites
Quote:
Original post by Yann L
Quote:
Original post by DOrmisher
Ok thanks for the reply I understand. I just looked up tex2Dlod on msdn, and it states that the second input parameter, i.e. the UV coords, should be a float4. How can there be 4 part coordinate for a texture UV??

Because some texture operations require 3-dimensional coordinates (UVW, for example volume textures) or 4-dimensional homogeneous ones (projective texturing). If you don't need the last two components, you should set them to 0 and 1, respectively.


Actually, when using tex2Dlod you should put the mip level that you want to sample in the 4th component of the texture coordinate.

Share this post


Link to post
Share on other sites
Quote:

Actually, when using tex2Dlod you should put the mip level that you want to sample in the 4th component of the texture coordinate.

I was more referring to the general question of why texture coordinates can have three or four components. But yeah, tex2Dlod specifically uses a somewhat broken and inconsistent semantic.

Share this post


Link to post
Share on other sites
Quote:
Original post by Yann L
Quote:

Actually, when using tex2Dlod you should put the mip level that you want to sample in the 4th component of the texture coordinate.

I was more referring to the general question of why texture coordinates can have three or four components. But yeah, tex2Dlod specifically uses a somewhat broken and inconsistent semantic.


The tex bias funcions also use the w component as the bias value, and the tex proj functions use w for the projective divide. It is best to check the docs when first using any of the tex functions, because the use of the w component is quite inconsistent.

Share this post


Link to post
Share on other sites
I think the error was refering to the fact that "5" is defined, but "KernelSize" is a variable with a unknown value. The compiler does not know if KernelSize > 1024


As for the UV coords, both of these lines of code are the same:

cDiffuseMap.rgb = tex2D(diffuseMapSampler,baseTC.xy);
cDiffuseMap.rgb = tex2D(diffuseMapSampler,float4(baseTC.xy, 0, 1.0));

The extra zw coords are ignored.


Share this post


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

  • Advertisement