Bizzare shader error

Started by
7 comments, last by Hello Kitty 15 years, 1 month ago
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?
She got a hat and all that hat says is asshole
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.
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??
She got a hat and all that hat says is asshole
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...
She got a hat and all that hat says is asshole
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.
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.
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.
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.
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.


-goodbye-

This topic is closed to new replies.

Advertisement