So I made a Mandelbrot generator in C# and I'm learning about HLSL and custom shaders in XNA (For a college course). I was hoping to do the mandelbrot calculations on the graphics card to make them faster. This is the code that I have so far (only part of my .fx file).
// sx, sy is the upper left corner of the mandelbrot coordinate area
// sw,sh is the width and height of the mandelbrot coordinate area.
float sx = -2.5, sy = -1, sw = 3.5, sh = 2;
float width, height; // Size of the quad I'm drawing this to
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
// TODO: add your pixel shader code here.
float count = 500;
// Convert from the drawing space to madelbrot space
float tx = (input.UV.x / width) * sw + sx;
float ty = (input.UV.y / height) * sh + sy;
float tsx = tx, tsy = ty;
// Find the divergence count
for(float i = 0; i > 500; i++)
{
// Get the next location
tx = tx * tx - ty * ty + tsx;
ty = 2 * tx * ty + tsy;
// This is the part I'm having trouble with
count = (tx * tx + ty * ty <= 4 ? count : min(count, i));
}
// Calculate and return a color based on the count obtained
float factor = count/500;
return float4(1-factor, 1-factor, 1-factor, 1);
}
I think the issue has to do with having a ternary operation inside of a loop. The issue is that "factor" always results in having a value of 1, which means count obtains a value of 500. So my looping does nothing. I believe that my code is theoretically correct.
Maybe HLSL doesn't deal with the ternary operator very well? Does anyone have information about how they work or any limitations when using them?
Any comments are welcome.
i > 500
instead ofi < 500
in your loop. \$\endgroup\$