Monday, September 11, 2017

Slow Shader compile time

Leave a Comment

I'm using three.js/r87. Chrome 60.0.3112.113.

I build 2 meshes, each using a new ShaderMaterial, so that's 2 ShaderMaterials that are created. The vertex shader and fragment shader are the same in both, in fact the materials are identical except that I set side=THREE.FrontSide in one case and side=THREE.BackSide in the other case. (I'm using the workaround suggested here:https://github.com/mrdoob/three.js/issues/2476#issuecomment-9076268). So actually they are not totally identical, one vertex shader has #define FLIP_SIDED and the other does not. My shaders don't use FLIP_SIDED, so this extra flag is unnecessary.

So this vertex shader gets compiled 2 times, once for each material. But the 2nd time it is compiled it is quite slow - 5 seconds or so each time.

How do I know? By using Chrome performance analyzer. Then I confirmed it by setting a break in the three.js code and putting time statements around the slow statement and having it print out the shader that is slow to compile, e.g.:

return function WebGLShader( gl, type, string ) {      var shader = gl.createShader( type );      gl.shaderSource( shader, string );     gl.compileShader( shader );      var t0 = performance.now();     if ( gl.getShaderParameter( shader, gl.COMPILE_STATUS ) === false ) {          console.error( 'THREE.WebGLShader: Shader couldn\'t compile.' );      }     var t1 = performance.now();     if ((t1 - t0) > 1000) {         console.log("------------------Slow compile.----------------");         console.log(string);     }     //  .....etc.... 

}

The shader that gets printed out is the vertex shader, on the SECOND TIME it is compiled. The vertex shader is quite simple:

varying vec2 vUv;  uniform float morphTargetInfluences[ 4 ]; void main() {     vec4 mvPosition = modelViewMatrix * vec4(position, 1.0 );     gl_Position = projectionMatrix * mvPosition;     vUv = uv; } 

So I'm looking for advice how to debug this slow compile time. Additionally, answers to any of the following questions would be enlightening:

1 - Why is it the vertex shader that is slow to compile?
2 - Why is it slow the second time it compiles?
3 - Since my shaders don't use FLIP_SIDED, is there a way to not have the flag included in the shader (and thereby avoid an extra compile)?

Any suggestions or pointers would be useful.

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment