while loop in custom 2D Filter crashes BGE...

EDIT-
MAYBE I FOUND THE REASON:

void main(void)
    {
    float i = 0;
    while (i <= 1)
        {
        i + 1;
        }
    }

that simple 2D filter crashes the BGE :confused:
-EDIT

EDIT2-
oh, boy, that was cheap, the while loop somehow crashes the BGE, so I just replaced it with a for loop
Does anyone know why that is?
The “fixed” code (it still isn’t even close to perfect):

uniform sampler2D bgl_RenderedTexture;
uniform sampler2D bgl_DepthTexture;
uniform float bgl_RenderedTextureWidth;
uniform float bgl_RenderedTextureHeight;


float resX = bgl_RenderedTextureWidth;
float resY = bgl_RenderedTextureHeight;
float currentX = (gl_TexCoord[0].st.x) * resX;
float currentY = (gl_TexCoord[0].st.y) * resY;
vec4 currentColor = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
float currentDepth = texture2D(bgl_DepthTexture, gl_TexCoord[0].st);
float divideBy = 1;
float skip = 1;     // every value above 1 lowers the quality but speeds everything up (+ then you see a pattern if you do not make this value random)
float maxRad = 10;
float focus = 0.9412;
    
float blurAmount(float Depth)                   // not physically accurate (I guess)
    {
    float blur = abs(Depth / focus - 1) * maxRad;
    return blur;
    }

void main(void)
    {
    for (float helpY = currentY - maxRad; helpY <= currentY + maxRad; helpY += skip)
        {
        for (float helpX = currentX - maxRad; helpX <= currentX + maxRad; helpX += skip)
            {                                   // the algorithm starts here
            if (helpX != currentX && helpY != currentY && helpX > 0 && helpX < resX - 1 && helpY > 0 && helpY < resY - 1) //for preventing dark rims
                {
                float vecX = helpX / resX;
                float vecY = helpY / resY;
                float helpDepth = texture2D(bgl_DepthTexture, vec2(vecX, vecY));       //getting the depth and color of the "helpPixel"
                if (currentDepth <= helpDepth)  // if the current pixel is in front of the "helpPixel" then use the blur radius of the current pixel
                       {
                    float rad = blurAmount(currentDepth);
                    if (sqrt((helpX - currentX)*(helpX - currentX) + (helpY - currentY)*(helpY - currentY)) <= rad)
                        {
                        vec4 helpColor = texture2D(bgl_RenderedTexture, vec2(vecX, vecY));
                          currentColor += helpColor;
                        divideBy += 1;
                        }
                      }
                else                            // else use the blur radius of the "helpPixel"
                    {
                    float rad = blurAmount(helpDepth);
                    if (sqrt((helpX - currentX)*(helpX - currentX) + (helpY - currentY)*(helpY - currentY)) <= rad)
                        {
                        vec4 helpColor = texture2D(bgl_RenderedTexture, vec2(vecX, vecY));
                        currentColor += helpColor;
                          divideBy += 1;
                        }
                    }                           // the algorithm ends here
                }
            }
        }
        currentColor /= divideBy;
        gl_FragColor = currentColor;
    }

-EDIT2

Last edit: i made an infinite loop and I didn’t notice it…

I wrote a depth of field shader that crashes the BGE when I want to run the game and I don’t know why, I already proofread it several times but i can’t find a mistake :spin: (notice: that is my first shader, so it might be that the script has a common mistake in it and I’m just not aware of it):

uniform sampler2D bgl_RenderedTexture;
uniform sampler2D bgl_DepthTexture;
uniform float bgl_RenderedTextureWidth;
uniform float bgl_RenderedTextureHeight;


float resX = bgl_RenderedTextureWidth;
float resY = bgl_RenderedTextureHeight;
float currentX = (gl_TexCoord[0].st.x) * resX;
float currentY = (gl_TexCoord[0].st.y) * resY;
vec4 currentColor = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
float currentDepth = texture2D(bgl_DepthTexture, gl_TexCoord[0].st);
float divideBy = 1;
float skip = 1;     // every value above 1 lowers the quality but speeds  everything up (+ then you see a pattern if you do not make this value  random)
float maxRad = 10;
float focus = 0.9412;
    
float blurAmount(float Depth)                   // not physically accurate (I guess)
    {
    float blur = abs(Depth / focus - 1) * maxRad;
    return blur;
    }

void main(void)
    {
    float helpY = currentY - maxRad;
    while (helpY <= currentY + maxRad)
        {
        float helpX = currentX - maxRad;
        while (helpX <= currentX + maxRad)
            {                                   // the algorithm starts here
            if (helpX != currentX && helpY != currentY  && helpX > 0 && helpX < resX - 1 && helpY  > 0 && helpY < resY - 1) //for preventing dark rims
                {
                float vecX = helpX / resX;
                float vecY = helpY / resY;
                float helpDepth = texture2D(bgl_DepthTexture, vec2(vecX,  vecY));       //getting the depth and color of the "helpPixel"
                if (currentDepth <= helpDepth)  // if the current  pixel is in front of the "helpPixel" then use the blur radius of the  current pixel
                       {
                    float rad = blurAmount(currentDepth);
                    if (sqrt((helpX - currentX)*(helpX - currentX) + (helpY - currentY)*(helpY - currentY)) <= rad)
                        {
                        vec4 helpColor = texture2D(bgl_RenderedTexture, vec2(vecX, vecY));
                        currentColor += helpColor;
                        divideBy += 1;
                        }
                      }
                else                            // else use the blur radius of the "helpPixel"
                    {
                    float rad = blurAmount(helpDepth);
                    if (sqrt((helpX - currentX)*(helpX - currentX) + (helpY - currentY)*(helpY - currentY)) <= rad)
                        {
                        vec4 helpColor = texture2D(bgl_RenderedTexture, vec2(vecX, vecY));
                        currentColor += helpColor;
                        divideBy += 1;
                        }
                    }                           // the algorithm ends here
                }
            helpX + skip;
            }
        helpY + skip;
        }
        currentColor /= divideBy;
        gl_FragColor = currentColor;
    }

Can you find the reason why it crashes the Game Engine? (notice: (maybe that is important) when i hit p or click on one of the start buttons i hear the music of my game for some time until it crashes, however it doesn’t render even one single frame and my gpu gets used by 100% (so maybe this script has an infinite loop in it and i don’t know it :confused:))

Better use code tags when posting code.

btw.

“while” is a good point to start looking for infinite or long lasting loops.

  1. Your script calls too many texture2D
  2. Your script uses so many crowded (expensive) maths

Why not just try:


uniform sampler2D bgl_RenderedTexture;
uniform sampler2D bgl_DepthTexture;
uniform float bgl_RenderedTextureWidth;

void main(){
    vec2 cen = vec2(.5,.5);
    vec4 d = texture2D(bgl_DepthTexture,gl_TexCoord[0].st);
    vec4 c = texture2D(bgl_DepthTexture,cen);
    vec4 f = d-c;
    
    f = abs(f);
    f = clamp(f,0,1.0);
    f /= 4.;
    
    float radius = .04;
    float sample = 4.;
    
    vec4 color;
    for (int i = -int(sample); i < int(sample); ++i) {
        for (int j = -int(sample); j < int(sample); ++j) {
            color += texture2D(bgl_RenderedTexture, vec2(gl_TexCoord[0].st.x + ((i)*radius)*f.r, gl_TexCoord[0].st.y + ((j)*radius)*f.r));
        }
    }
    color /= sample*sample*4;
    gl_FragColor = color;
}

Thanks for your answer :slight_smile:
However I don’t want this dof shader to be fast but “accurate” (as accurate you can get with a post processing filter) so i don’t want to use this shader as realtime dof in a game but I just want to experiment (maybe I’ll try later, when I got it to work, to make it both accurate and fast)
And why is it not just slow instead of crashing especially when I render at a ridiculous low resoultion like 32x24?

try to run this 2d filter :eek::

void main(void)
    {
    float helpY = 0;
    while (helpY <= 1)
        {
        helpY + 1;
        }
    }

I don’t know if you already spotted it or it is a simple typing mistake but in the body of your while loop the index is never incremented. The pasted source says:

helpX + 1
helpY + 1

but you probably meant:

helpX += 1
helpY += 1

As you did in the for loop.

Thanks, pgi, that’s it :slight_smile:
but why don’t i just get an error message by the compiler?

Because it is absolutely valid syntax and processing.

It is just not what you thought it is.

The compiler does not know what you want, so it can’t check “semantics”.

i +1

does what it says - it adds 1 to whatever I contains. lets assume i==5 then you get:

5 + 1

which results in 6. No problem the processing is fine.

What you miss (because it looks pretty similar), is the assignment to a variable. This is a additional operation.


"i += 1" 

which is the same as

"i = i + 1" 

(with other but more explicit notation)

What it does:

  • It does what you wrote above - it adds the value of a variable with a constant. -> first operation
  • It assigns the result to a variable -> second operation

So it looks like typing error which resulted in valid code with different processing.

Thanks a lot, the mistery is solved xD