google / shaderc

A collection of tools, libraries, and tests for Vulkan shader compilation.
Other
1.85k stars 362 forks source link

Add warning for implicit conversion of floats/ints #1319

Closed gaaclarke closed 1 year ago

gaaclarke commented 1 year ago

When authoring glsl shaders that are compiled with shaderc I was doing some prototyping in shadertoy.com and noticed that our shaderc compiled glsl code had implicit conversions between floats and ints which is not allowed by the shadertoy.com glsl compiler.

I also noticed that in certain instances the implicit conversions have runtime costs in the generated shaders too (as verified by malioc).

I've looked over the shaderc api many times and I don't think there is the ability to turn on implicit conversions as warnings. So, for performance and better compatibility with other glsl compilers it would be nice if we could make this a optional warning in shaderc.

linked issue: https://github.com/flutter/flutter/issues/122324

dneto0 commented 1 year ago

The implicit conversion warning/error exists in Shadertoy because Shadertoy is using WebGL, and WebGL uses the ES form of GLSSL.

GLSL must start with a #version annotation. It seems Shadertoy inserts the #version line for you.

The lowest version of GLSL ES that can be compiled to SPIR-V is #version 310 es. Put that at the top of your shader to get the error you want.

So, for example save this to a.frag:

$ cat a.frag
#version 310 es

void main() {
  int i = 1;
  highp float x = i;
}

$ glslc a.frag
a.frag:5: error: '=' :  cannot convert from ' temp mediump int' to ' temp highp float'
1 error generated.