Problem with a function that can be inlined #6

Antymon opened this Issue Aug 12, 2012 · 2 comments


None yet
2 participants

Antymon commented Aug 12, 2012

glsl2agal fails with the conversion of glsl code posted below. If I inline the function atanSurrogate everything goes well, otherwise I get "Internal error" exception in RegisterAssigner.assignRegisters(). I am also not sure if I should implement inverse trigonometric functions on my own, maybe some not working approximations are provided?

Vertex shader does nothing (this is a 2D effect). Here is a fragment shader:

ifdef GL_ES

precision highp float;


uniform float time;
uniform vec2 resolution;

float u( float x )
if (x > 0.0) return 1.0;
else return 0.0;

float atanSurrogate(float y, float x)
float z = y/x;
float ab = abs(z);
float res;

if (ab<0.9626995623)
    return z/(1.0 + 0.28*z*z);
else if(ab > 1.094294782)
    res = - z/(z*z + 0.28);

    if (z>0.0) return res + 3.14159/2.0;
    else return res - 3.14159/2.0;
    res = 0.5*z;
    if (z>0.0) return res + 0.283;
    else return res - 0.283;


void main(void)
vec2 p = (2.0*gl_FragCoord.xy-resolution)/resolution.y;

float a = atanSurrogate(p.x, p.y);

float r = length(p)*.75;

float w = cos(3.1415927*time-r*2.0);
float h = 0.5+0.5*cos(12.0*a-w*7.0+r*8.0);
float d = 0.25+0.75*pow(h,1.0*r)*(0.7+0.3*w);

float col = u( d-r ) * sqrt(1.0-r/d)*r*2.5;
col *= 1.25+0.25*cos((12.0*a-w*7.0+r*8.0)/2.0);
col *= 1.0 - 0.35*(0.5+0.5*sin(r*30.0))*(0.5+0.5*cos(12.0*a-w*7.0+r*8.0));
gl_FragColor = vec4(
    col-h*0.5+r*.2 + 0.35*h*(1.0-r),
    col-h*r + 0.1*h*(1.0-r),



alexmac commented Nov 25, 2012

If I copy the shader into the basic shader compiler example app and replace the call to atanSurrogate with atan() it appears to compile fine, however the shader overall ends up using too many temporary registers and so isn't useable.

Looks like some more work needs to be done on the optimizer, but as a work around you might be able to reduce the number of temporaries yourself by refactoring the code...


alexmac commented Jan 22, 2013

I've added this testcase to tests/regression_000007.fs, it now compiles without modification but it still uses a couple too many temporaries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment