galacean / engine

A typescript interactive engine, support 2D, 3D, animation, physics, built on WebGL and glTF.
https://galacean.antgroup.com/engine
MIT License
4.2k stars 299 forks source link

glsl compile error with GLSL ES 3.00 #2047

Closed eyworldwide closed 6 months ago

eyworldwide commented 6 months ago

Describe the bug I think it's a glsl code compile bug. Support for GLSL ES 3.00 is not very good.

To Reproduce

The source code of frag shader:

precision highp float;

in vec4 vColor;
in vec2 vPosition;
out vec4 fragColor;

void main () {
  float A = -dot(vPosition, vPosition);
  if (A < -4.0) discard;
  float B = exp(A) * vColor.a;
  fragColor = vec4(B * vColor.rgb, B);
}

The ShaderFactory.convertTo300 method converts the code to:

  #version 300 es
  #define GRAPHICS_API_WEBGL2
....

 precision highp float;

 in vec4 vColor;
 in vec2 vPosition;
 out vec4 fragColor;

 // This line is NOT I expect:
 out vec4 glFragColor;

 void main() {
   float A = -dot(vPosition, vPosition);
   if (A < -4.0) discard;
   float B = exp(A) * vColor.a;
   fragColor = vec4(B * vColor.rgb, B);
 }

Screenshots

image

Expected behavior

  1. Keep the GLSL ES 3.00 code unchanged. OR
  2. Covert to a lower version can compile.

Perhaps giving the user the option to choose would be a wise decision.

PS: String replacement is an unsafe approach for handling GLSL syntax. But I have no idea if there's a better approach to solve the problem.

zhuxudong commented 6 months ago

current compile workflow: image

will case some problom:

maybe the final solution? image

eyworldwide commented 6 months ago

@zhuxudong @Sway007 I think WebGPU should also be taken into consideration. This is the way to ensure that our architecture is future-proof.

flowchart LR
shaderLab --> AST{AST Compile};
AST --WebGL2--> GLSL300;
AST --WebGL1--> GLSL100;
AST --WebGPU--> WGSL;

@zhuxudong For now, you just need to apply a quick patch.

zhuxudong commented 6 months ago

@zhuxudong @Sway007 I think WebGPU should also be taken into consideration. This is the way to ensure that our architecture is future-proof.

flowchart LR
shaderLab --> AST{AST Compile};
AST --WebGL2--> GLSL300;
AST --WebGL1--> GLSL100;
AST --WebGPU--> WGSL;

@zhuxudong For now, you just need to apply a quick patch.

yes,the source of the AST is not only the shaderLab, such as you original glsl100 or glsl300 or wgsl, I don't know if we should consider this

eyworldwide commented 6 months ago

yes,the source of the AST is not only the shaderLab, such as you original glsl100 or glsl300 or wgsl, I don't know if we should consider this

I think choosing to establish a standard language is a better option. It makes no sense to support such a wide array of syntaxes. These languages should serve as compiled outputs, rather than inputs.