Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spirv-opt: changes variable precision from relaxed/mediump to highp in some cases #3471

Closed
pixelflinger opened this issue Jun 26, 2020 · 2 comments

Comments

@pixelflinger
Copy link

I found a scenario where a mediump variable is turned in to a highp after running spirv-opt.

#version 310 es
precision mediump float;
precision mediump int;
uniform mediump sampler2D tex;

layout(location = 0) in highp vec4 vertex;
layout(location = 0) uniform vec4 value;
layout(location = 0) out vec4 fragColor;

void main() {
        vec4 x = textureLod(tex, vertex.xy, 0.0); // specifying mediump here doesn't fix the problem
	if (max(x.r, x.g) < 2.0) {
		fragColor = vec4(0.0);
		return;
	}
	fragColor = vec4(1.0);
}

will produce:

#version 310 es
precision mediump float;
precision highp int;

layout(binding = 0) uniform mediump sampler2D tex;

layout(location = 0) in highp vec4 vertex;
layout(location = 0) out vec4 fragColor;

void main()
{
    do
    {
        highp vec4 _21 = textureLod(tex, vertex.xy, 0.0); // Note the highp here!!!
        if (mod(_21.x, _21.y) < 2.0)
        {
            fragColor = vec4(0.0);
            break;
        }
        fragColor = vec4(1.0);
        break;
    } while(false);
}

It looks like the conditions for it to happen are that the variable must be from reading a texture (e.g. replacing with a mediump uniform doesn't cause the problem), and that variable is fed to a two-parameter build-in (e.g. this doesn't happen with abs()).

I checked that the highp is not there right after calling glslang, but appears after running spirv-opt:

  glslangValidator --target-env opengl  test.frag -o test.spv
  spirv-opt -O test.spv -o test.opt.spv
  spirv-cross --es test.opt.spv 
@alan-baker
Copy link
Contributor

glslang isn't currently adding RelaxedPrecision to the result of textureLod and spirv-opt just eliminates x by promoting it to registers. That's why spirv-cross is making _21 highp.

@alan-baker
Copy link
Contributor

Filed KhronosGroup/glslang#2298.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants