illogical compilation loop error

I am making pattern fractals using a loop, and if the pattern inside the loop has normal maths, lines, modf, sin, the loop runs okay. and if I try a CG Perlin pattern, it returns everything 0. so I have found that under SM4, CG compiler unrolls a loop into a limited list of tasks, and because this function ‘"noise3d’" uses the permutation table from a normal script, it seems that the compiler can’t decide to unroll it for anything more than 1 loop, so if I write for(int i = 1; i <2; i++) it returns something, and if it’s more than one, it returns everything Black, =0, as if it didn’t run any maths at all. I attempted to trick it into knowing it had to run the loop by putting something obvious to do in there like rgb.r*=.99;, but the loop simply can’t run with the perlin noise function from scrawkblog, because it uses data outside of the CG.??! it’s frustrating!

             half q =  0;
			  	for(int i = 1; i < 4; i++) 
				{
					if( q <999) {
					q +=  noise3d(pos.x*i*i, pos.y*i*i, pos.z*i*i);}
				}

Try adding [unroll] before the loop.

Thanks! it resulted in an error. i tested unroll Unroll [unroll] [Unroll].

I found this list of CG compiler directives with options like -looplimit N
http://http.developer.nvidia.com/Cg/cgc.html

Is there a list of the options in Unity3d?

There’s a way round, There is a multrifractal hidden in the cupboard. it has legs. the 3d and 4d implementations of perlin noise have some wave shaping loops that are folded together into octaves, i has 3 and ill see if i can make other ones :smile: theres no info on photoshop effects codes somewhere :slight_smile:

Sorry for reviving an old topic, but I am stuck on the same issue. I can not unroll my loops unless I use SM4.0+

Welcome to the club. There are so many basic shader capabilities that we can’t use because of how the compiler does things.

This is disappointing :confused:

There’s often a workaround. Sometimes the compiler generates loop instructions in the assembly code; and I have sometimes just manually unrolled the loop by pasting x copies of it and then replacing counter variables with the values they would have for each iteration; which also lets me optimize it more by eliminating some of the calculations where the counter’s value is 0 (or 1 if multiplying) etc. I did that once with a triple nested loop with three counters and a total of 27 iterations. Not fun, but it worked.

I need about 128 loops maximum though its gonna be very tedious job to unroll it myself, dammit Unity…

Sometimes the compiler generates an actual dynamic loop in the resulting code, so it doesn’t need to be unrolled. I’m not sure exactly how to get it to do that, but I’ve seen it happen.