LEGACY_GL_EMULATION generate wrong shaders #4884

Open
caiiiycuk opened this Issue Jan 24, 2017 · 4 comments

Projects

None yet

3 participants

@caiiiycuk
Contributor
caiiiycuk commented Jan 24, 2017 edited

Hi! I am porting new project, that uses legacy opengl. For now, I can`t see anything in game because LEGACY_GL_EMULATION produces wrong shaders, like this:

precision mediump float;
varying vec4 v_texCoord0;
varying vec4 v_texCoord1;
uniform sampler2D u_texUnit0;
uniform mat4 u_textureMatrix0;
uniform sampler2D u_texUnit1;
uniform mat4 u_textureMatrix1;
varying vec4 v_color;
void main()
{
  vec4 tej_env0_result = v_color * texture2D(u_texUnit0, (u_textureMatrix0 * v_texCoord0).xy);
  
  vec4 tej_env1_texload0 = texture2D(u_texUnit1, (u_textureMatrix1 * v_texCoord1);
float tej_env1_colorSrc2 = tej_env1_texload0.xy).a;
vec3 tej_env1_color = tej_env0_result.rgb * tej_env1_colorSrc2 + tej_env1_texload0.xy).rgb * (1.0 - tej_env1_colorSrc2);
float tej_env1_alpha = tej_env1_texload0.xy).a * tej_env0_result.a;
vec4 tej_env1_result = vec4(tej_env1_color, tej_env1_alpha);
  
  gl_FragColor = tej_env1_result;
}

In this shader parentheses are not balanced (for example on line 13). May be I can fix by my self, but I don`t have good understanding of LEGACY_GL_EMULATION implementation yet.

vec4 tej_env1_texload0 = texture2D(u_texUnit1, (u_textureMatrix1 * v_texCoord1);
float tej_env1_colorSrc2 = tej_env1_texload0.xy).a;

branch: incoming
os: ubuntu

@caiiiycuk
Contributor

Ok, I found where problem is. If I comment this, then error is disappeared:

        // As an optimization, merge duplicate identical texture loads to one var.
        /*
        while(load = texLoadRegex.exec(lines)) {
          var texLoadExpr = load[1];
          var secondOccurrence = lines.slice(load.index+1).indexOf(texLoadExpr);
          if (secondOccurrence != -1) { // And also has a second occurrence of same load expression..
            // Create new var to store the common load.
            var prefix = TEXENVJIT_NAMESPACE_PREFIX + 'env' + texUnitID + "_";
            var texLoadVar = prefix + 'texload' + loadCounter++;
            var texLoadLine = 'vec4 ' + texLoadVar + ' = ' + texLoadExpr + ';\n';
            texLoadLines += texLoadLine + '\n'; // Store the generated texture load statements in a temp string to not confuse regex search in progress.
            lines = lines.split(texLoadExpr).join(texLoadVar);
            // Reset regex search, since we modified the string.
            texLoadRegex = /(texture.*\(.*\))/g;
          }
        }*/
@kripken
Owner
kripken commented Jan 24, 2017

I didn't write the texenv stuff myself, not sure what's going wrong there. But since you found the relevant code, and it's not that long, we should be able to figure it out. If you get stuck, maybe try to make a reduced testcase and I can take a look.

@juj
Collaborator
juj commented Jan 25, 2017

By first glance, this seems to be somehow caused by nested parentheses in regex searches. Iirc regexes have difficult time in matching nesting levels. Perhaps it might be possible to optimise the regexes to detect and at least not kick in when this type of scenario occurs.

@caiiiycuk
Contributor

I can prepare test case and maybe some fix. Since my projects work fine it is not critical. So, I plan do this on the next week when I have time.

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