## Menger Sponge Explorer

Show off your games, demos and other (playable) creations.
raidho36
Party member
Posts: 1685
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Menger Sponge Explorer

OK so I've narrowed down the problem, the offending code is the sponge generator function which uses uniform inside a loop - this causes crash. Even if you replace it with a constant, setting higher number of iterations will also crash it. GPUs don't like loops! You need to unroll them whenever possible, because the most bizzare things can screw up shader compiler optimization logic. From what I gather, apparently it's totally normal for faulty shader to hard crash the whole program.

Code: Select all

//Menger Sponge Fractal Distance
vec2 MengerSponge(vec3 p){
float orbit = 1e20;

#define iteration() \
p = abs(p);\
if(p.x<p.y) p.xy = p.yx;\
if(p.x<p.z) p.xz = p.zx;\
if(p.y<p.z) p.zy = p.yz;\
p.z -=  .5*Offset.z*(Scale-1.)/Scale;\
p.z  = -abs(p.z);\
p.z +=  .5*Offset.z*(Scale-1.)/Scale;\
p *= m;\
p   *= Scale;\
p.x -= Offset.x*(Scale-1.);\
p.y -= Offset.y*(Scale-1.);\
orbit = min(orbit,length(p))\

iteration();
iteration();
iteration();
iteration();
iteration();
iteration();
iteration();

vec3 del  = abs(p) - Offset;
float dis  = min(max(del.x,max(del.y,del.z)),0.0) + length(max(del,0.0));
dis *= pow(Scale, float(-7));

return vec2(dis,orbit);
}
Also, I wanted to do a deep diving but the picture becomes extremely blurry past 7 iterations and you can barely make out any deeper details, too bad.

GijsB
Party member
Posts: 379
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

### Re: Menger Sponge Explorer

raidho36 wrote:No I in fact believe it's your graphics card - 5 bucks says it's Nvidia. There could be very serious problems with the shader, but your GPU will try munching through them instead of raising errors, obscuring the very existence of the very real issues from you. For this single reason, Nvidia GPUs are the worst choice for graphics code development. But I digress; I'm not the only person who has it crash on them, and the evidence (runs fine for most but not on ATI and probably Intel) suggests that there are problems with the shader that Nvidia obscures but that are apparent on other vendor graphics, being a very common problem for Nvidia owners who try developing graphics code.
raidho36 wrote:OK so I've narrowed down the problem, the offending code is the sponge generator function which uses uniform inside a loop - this causes crash. Even if you replace it with a constant, setting higher number of iterations will also crash it. GPUs don't like loops! You need to unroll them whenever possible, because the most bizzare things can screw up shader compiler optimization logic. From what I gather, apparently it's totally normal for faulty shader to hard crash the whole program.

Also, I wanted to do a deep diving but the picture becomes extremely blurry past 7 iterations and you can barely make out any deeper details, too bad.
GPU's dont like loops indeed, but the solution is rather strange... Will try to add it when I get home. Also floating point errors become quickly apparent and therefor the fractal/render becomes blurry.

raidho36
Party member
Posts: 1685
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Menger Sponge Explorer

GijsB wrote: Does https://www.shadertoy.com/view/lttGzr work? Also can you specify your graphics card?
That one works, but only if number of iterations is under 5 - then it crashes the browser. Also there's nothing strange about unrolling loops, it's a very commmon practice. My GPU is R9 280X.

zorg
Party member
Posts: 2379
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Menger Sponge Explorer

My GPU can handle 5-6 with 60 FPS, and from those to 10 with 30 FPS. Didn't crash.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

GijsB
Party member
Posts: 379
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

### Re: Menger Sponge Explorer

Added scancodes and raidho36's fix in version 4. Anyone have a good GUI library I could add with sliders for better controls ?

raidho36
Party member
Posts: 1685
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Menger Sponge Explorer

I took liberty and modified the code somewhat (and the style extremely heavily). Now you can adjust iteration depth, also camera rotation is not limited to X and Y axis (press HOME to horizon). I also changed some controls: SHIFT reduces acceleration rate, SPACE is brake, E and Q are ascend and descend, PLUS and MINUS adjust iteration depth (it goes to ten, then it's floating point precision errors galore). Epsilon value is adjusted automatically based on iteration depth - too high causes aliasing, too low causes rays to pass through solids.

Also check out the ayylium planet I found!
Attachments
sponge.love
Screenshot from 2017-01-02 17-32-56.jpg (235.37 KiB) Viewed 1212 times

GijsB
Party member
Posts: 379
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

### Re: Menger Sponge Explorer

raidho36 wrote:I took liberty and modified the code somewhat (and the style extremely heavily). Now you can adjust iteration depth, also camera rotation is not limited to X and Y axis (press HOME to horizon). I also changed some controls: SHIFT reduces acceleration rate, SPACE is brake, E and Q are ascend and descend, PLUS and MINUS adjust iteration depth (it goes to ten, then it's floating point precision errors galore). Epsilon value is adjusted automatically based on iteration depth - too high causes aliasing, too low causes rays to pass through solids.

Also check out the ayylium planet I found!
Awesome! Although the camera keeps moving when rendering thus destroying the render. (but you did add a brake so propably it was deliberately). The aliasing is not actually epsilons fault, Backstep causes aliasing and should always be higher or equal to epsilon! Also 'Delta' should always be smalller or equal to epsilon or else normal calculation will be wrong on details smaller than epsilon.

raidho36
Party member
Posts: 1685
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Menger Sponge Explorer

Oh yeah I thought I could make the high quality rendering work in real-time but it didn't fly so well, and I forgot to put back the guard code. The brake is there to stop the camera where you need it, otherwise it just flies all over the place, even with fractally increased sensitivity!

I've left in the code responsible for manual epsilon control, you can screw around with it and yep it's definitely epsilon that causes aliasing. Not to mention, the only place where backstep is even used is high quality rendering. Aye I've removed delta, I just use epsilon instead. Not that those two even need to be different.

GijsB
Party member
Posts: 379
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

### Re: Menger Sponge Explorer

raidho36 wrote:Oh yeah I thought I could make the high quality rendering work in real-time but it didn't fly so well, and I forgot to put back the guard code. The brake is there to stop the camera where you need it, otherwise it just flies all over the place, even with fractally increased sensitivity!

I've left in the code responsible for manual epsilon control, you can screw around with it and yep it's definitely epsilon that causes aliasing. Not to mention, the only place where backstep is even used is high quality rendering. Aye I've removed delta, I just use epsilon instead. Not that those two even need to be different.
Ah jep facepalm I was thinking of shadow aliasing. And indeed Delta and Epsilon do not need to be different in this case.
raidho36 wrote:I took liberty and modified the code somewhat (and the style extremely heavily). Now you can adjust iteration depth, also camera rotation is not limited to X and Y axis (press HOME to horizon). I also changed some controls: SHIFT reduces acceleration rate, SPACE is brake, E and Q are ascend and descend, PLUS and MINUS adjust iteration depth (it goes to ten, then it's floating point precision errors galore). Epsilon value is adjusted automatically based on iteration depth - too high causes aliasing, too low causes rays to pass through solids.

Also check out the ayylium planet I found!
Attachments
Menger Sponge.love