PDA

View Full Version : Shader crashes at certain screen resolution



TG1234
09-09-2016, 05:24 PM
This is a strange problem that has been sending me mad for the past week. I have some shader code (relatively simple 2D fractal) that links and compiules without errors and seemed to run fine. That is until I tried to run it at 4K screen size. Then it causes the hosting application to crash with the default Windows "Appname has stopped working" with a Close button.

It seems to always crash when the coordinates of the pixel being calculated is near the middle of the screen. This is where the c.x and c.y values will be near the origin (0,0). But if I force them to 0 the shader runs! I cannot work out which line of code or lines causes the combination that crashes the host app.

Any ideas onto why this code may fail just at 4K size? I just ran it on 8K size and it failed too. This is on both an NVidia GTX 1080 and a Radeon R7 370. Both Windows 10.

Is there any try/catch sort of logic I can implement in a shader? Then within the calc code I could have a try/catch and color the pixel red if it fails and carry on?

Any ideas or obvious code errors I may have made? Anything to help my increasing madness with trying to work this out.

I apologize that this is a very vague plea for help.

Here is the shader code. All that needs passing as uniforms is the screen resolution. The rest is self contained.



#version 400

//resolution is uniform matching screen resolution passed to shader
uniform vec2 resolution;

double xmin=-2.44362353292181;
double xmax=2.90551149588477;
double ymin=-2.1650767249537;
double ymax=0.934078382361111;
double bailout=8000000;
int maxiters=1024;

//supersampling amount
const int samplepixels=3;

double sqrsamplepixels=samplepixels*samplepixels;
double bailout_squared=bailout*bailout;
double magnitude,r1,r2,g1,g2,b1,b2,tweenval;
float realiters;
vec4 finalcol,col;
int superx,supery;
double stepx=(xmax-xmin)/resolution.x/samplepixels;
double stepy=(ymax-ymin)/resolution.y/samplepixels;
int colval,colval1,colval2;
dvec2 z,c;
//triangle inequality average coloring
double sum,sum2,ac,il,lp,az2,lowbound,f,index,tr,ti;
int fractalPower;
double rval,gval,bval,rval1,gval1,bval1,rval2,gval2,bval2 ;

void main(void)
{
fractalPower=2;

finalcol=vec4(0,0,0,0);
for (supery=0;supery<samplepixels;supery++)
{
for (superx=0;superx<samplepixels;superx++)
{
c.x = xmin+gl_FragCoord.x/resolution.x*(xmax-xmin)+(stepx*double(superx));
c.y = ymax-gl_FragCoord.y/resolution.y*(ymax-ymin)+(stepy*double(supery));
int i;
z = dvec2(0.0,0.0);

//triangle inequality average coloring
sum = 0;
sum2 = 0;
ac = length(c);
il = 1.0 / log(fractalPower);
lp = log(float(log(float(bailout)) / fractalPower));
az2 = 0.0;
lowbound = 0.0;
f = 0.0;
index = 0.0;

for(i=0; i<maxiters; i++)
{
double x = z.x * z.x - z.y * z.y -c.x;
double y = -2.0 * abs(z.y * z.x) - c.y;

magnitude=(x * x + y * y);
if(magnitude>bailout_squared) break;
z.x = x;
z.y = y;

//tia
sum2=sum;
if (i>0) {
tr=z.x-c.x;
ti=z.y-c.y;
az2=sqrt(tr * tr + ti * ti);
lowbound=abs(az2 - ac);
sum+=((length(z)-lowbound)/(az2+ac-lowbound));
}

}

if (i==maxiters) {
col=vec4(0.0,0.0,0.0,1.0);
} else {
//triangle inequality average
sum=sum/i;
sum2=sum2/(i-1.0);
f=il*lp - il*log(log(float(length(z))));
index=sum2+(sum-sum2)*(f+1.0);
realiters=float(255*index);
colval1= int(mod(realiters,255));
colval2= int(mod((colval1+1),255));
tweenval=fract(realiters);
rval1 =colval1/255.0;
gval1 =colval1/255.0;
bval1 =colval1/255.0;
rval2 =colval2/255.0;
gval2 =colval2/255.0;
bval2 =colval2/255.0;
rval =rval1 +((rval2 - rval1)*tweenval);
gval =gval1 +((gval2 - gval1)*tweenval);
bval =bval1 +((bval2 - bval1)*tweenval);
col=vec4(rval,gval,bval,1.0);
}
finalcol+=col;
}
}
gl_FragColor = vec4(finalcol/double(sqrsamplepixels));
}


Some more info too. If I remark out this part of the code



//triangle inequality average
sum=sum/i;
sum2=sum2/(i-1.0);
f=il*lp - il*log(log(float(length(z))));
index=sum2+(sum-sum2)*(f+1.0);
realiters=float(255*index);
colval1= int(mod(realiters,255));
colval2= int(mod((colval1+1),255));
tweenval=fract(realiters);
rval1 =colval1/255.0;
gval1 =colval1/255.0;
bval1 =colval1/255.0;
rval2 =colval2/255.0;
gval2 =colval2/255.0;
bval2 =colval2/255.0;
rval =rval1 +((rval2 - rval1)*tweenval);
gval =gval1 +((gval2 - gval1)*tweenval);
bval =bval1 +((bval2 - bval1)*tweenval);
col=vec4(rval,gval,bval,1.0);


and replace it with something like this (to get a display going)



col=vec4(1.0,1.0,1.0,1.0);


then it works fine and does not crash. So maybe something with that above snippet? Anything look strange? Code that compiles yet could cause issues?

TG1234
09-16-2016, 10:32 PM
Looks like this was caused by code outside the shader and not the shader code itself.
A mod can delete this thread as there does not seem to be a way for me to delete it.