Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Bizzare shader error


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 DOrmisher   Members   -  Reputation: 100

Like
0Likes
Like

Posted 26 February 2009 - 07:28 AM

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?

Sponsor:

#2 Yann L   Moderators   -  Reputation: 1798

Like
0Likes
Like

Posted 26 February 2009 - 08:40 AM

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.


#3 DOrmisher   Members   -  Reputation: 100

Like
0Likes
Like

Posted 26 February 2009 - 09:08 AM

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??

#4 DOrmisher   Members   -  Reputation: 100

Like
0Likes
Like

Posted 26 February 2009 - 09:10 AM

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...

#5 Yann L   Moderators   -  Reputation: 1798

Like
0Likes
Like

Posted 26 February 2009 - 09:13 AM

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.

#6 GMano   Members   -  Reputation: 160

Like
0Likes
Like

Posted 26 February 2009 - 06:02 PM

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.

#7 Yann L   Moderators   -  Reputation: 1798

Like
0Likes
Like

Posted 26 February 2009 - 06:57 PM

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.


#8 GMano   Members   -  Reputation: 160

Like
0Likes
Like

Posted 26 February 2009 - 07:45 PM

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.


#9 Hello Kitty   Members   -  Reputation: 102

Like
0Likes
Like

Posted 04 March 2009 - 12:40 PM

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.







Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS