Sometimes, initializing variables of a formula can require a large number of calculations. Since you would normally initialize variables in the init section of a fractal formula or a coloring algorithm (or the transform section of a transformation), these calculations are performed again and again for every pixel.
Often, they only depend on parameter settings, and therefore the results are the same for every pixel. To avoid doing many repeated calculations, you can move them to the global section. This is a special section at the start of a formula that is executed once per image.
Use the global section to perform per-image calculations and store the results in variables that can be read in other sections. Variables declared here are treated as read-only in other sections, so you cannot use this to share variables across pixels (that would not work reliably).
In the following example, the global section is used to pre-calculate an array of random values that is the same for every pixel. These random values are subsequently used to disturb a standard Mandelbrot set.
MandelbrotModified { global: float values[#maxiter] int i = 0 int seed = 12345678 while i < 100 seed = random(seed) values[i] = seed / #randomrange i = i + 1 endwhile init: z = (0,0) int iter = 0 ; "i" is already taken loop: z = sqr(z) + #pixel + values[iter] iter = iter + 1 bailout: |z| 4 }
Notes
Next: Image parameters