1 registered members (AndrewAMD),
177
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
unrolling for loop
#453429
07/26/15 12:49
07/26/15 12:49
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
OP
Serious User
|
OP
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi, I have written a small shader that contains a for loop to sample an image by rows but I am getting an unrolling error that is breaking my nuts. Here is the shader:
float4 vecSkill1;
texture mtlSkin1;
sampler texSampler = sampler_state { Texture = <mtlSkin1>; Mipfilter = Point; Minfilter = Point; Magfilter = Point; AddressU = Wrap; AddressV = Wrap; };
float4 PS ( in float2 inTex: TEXCOORD0 ) : COLOR0
{
inTex.xy *= vecSkill1.xy;
float4 color1 = 0;
int layerCount = vecSkill1.z;
for ( int layer=0; layer<layerCount; layer+=1 )
{
float4 color2 = tex2D ( texSampler, inTex.xy );
color1.rgb = lerp ( color1.rgb, color2.rgb, color2.a );
color1.a = max ( color1.a, color2.a );
inTex.y += vecSkill1.w;
}
return color1;
}
I set the material skills as follows:
mtlRenderer->skill1 = floatd ( bmap_width(bmp), bmap_width(bmpRenderer) ); // width proportion between render target (bmp) and color sampler (bmpRenderer)
mtlRenderer->skill2 = floatd ( bmap_height(bmp), bmap_height(bmpRenderer) ); // height proportion between render target (bmp) and color sampler (bmpRenderer)
mtlRenderer->skill3 = floatv ( 2 ); // number of loops
mtlRenderer->skill4 = floatd ( 1, bmap_height(bmpRenderer) / bmap_height(bmp) ); // height of render target inside color sampler 0<>1 height range
mtlRenderer->skin1 = bmpRenderer;
bmap_process ( bmp, NULL, mtlRenderer );
And the error: I checked layerCount and contains the correct value. How the hell can the for loop be unable to unroll? I am totally lost. Any idea would be appreciated. Salud!
Last edited by txesmi; 07/26/15 12:59.
|
|
|
Re: unrolling for loop
[Re: txesmi]
#453431
07/26/15 13:52
07/26/15 13:52
|
Joined: Sep 2003
Posts: 6,861 Kiel (Germany)
Superku
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
|
Loops in shaders are not the same loops as in regular programming languages. Let's say you write: for(i = 0; i < 5; i++) { value += i; Color += tex2D(...,i*0.25); } into your code, then the compiler will unroll the for loop statement to something like value += 0; Color += tex2D(...,0*0.25); value += 1; Color += tex2D(...,1*0.25); value += 2; Color += tex2D(...,2*0.25); value += 3; Color += tex2D(...,3*0.25); value += 4; Color += tex2D(...,4*0.25);
As a result, shaders don't like dynamic for loop ranges that much. You can try one of 2 approaches (that I am aware of/ I use):
for ( int layer=0; layer<8; layer+=1 ) { if(layer < layerCount) {...} }
or
for ( int layer=0; layer<8; layer+=1 ) { multiply with another value, let's say float array_active_var[8]; which is non-zero if used }
The first way should be faster but if loops are special in shader language too and are not as fast as in regular programming.
Salud!
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual Check out my new game: Pogostuck: Rage With Your Friends
|
|
|
Re: unrolling for loop
[Re: Superku]
#453435
07/26/15 17:31
07/26/15 17:31
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
OP
Serious User
|
OP
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Thank you Superku, I ultimately decided to build a material for each layer count and forget about dynamic loops. By the way I tryed to add an integer to the material through SetInt DX function. It fails in the compilation too. I am pretty confused because iLights or iWeights are also integers containing dynamic ranges and are commonly used into for loops with no troubles. i força al canut
Last edited by txesmi; 07/26/15 17:35.
|
|
|
|