KhronosGroup / KhronosGroup/glslang

Glslang doesn't build spv code with RelaxedPrecision

Open
#4,121 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
3.6k
Forks
989
Avg merge
1d 2h
Merged PRs (30d)
31

Description

I'm using pair of glslang and SPIRV-Cross and compiling simple shader to ES target without optimizer. Input code:
```glsl
#version 450 core

precision mediump float;

layout(set = 0, binding = 1) uniform mediump sampler2D sTexture;

layout(location = 0) in VertexData {
vec4 Color;
vec2 UV;
} In;

layout(location = 0) out mediump vec4 fColor;

void main() {
fColor = In.Color * texture(sTexture, In.UV.st);
}
```

Without any modifications im getting output:
```glsl
#version 300 es
precision mediump float;
precision highp int;

uniform highp sampler2D sTexture;

layout(location = 0) out highp vec4 fColor;
in highp vec4 In_Color;
in highp vec2 In_UV;

void main()
{
fColor = In_Color * texture(sTexture, In_UV);
}
```

If I modify GlslangToSpv.cpp with manually forcing precision to RelaxedPrecision
```cpp
spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type)
{
return spv::Decoration::RelaxedPrecision;
//return TranslatePrecisionDecoration(type.getQualifier().precision);
}
```

Then im getting following output:
```glsl
#version 300 es
precision mediump float;
precision highp int;

uniform mediump sampler2D sTexture;

layout(location = 0) out vec4 fColor;
in vec4 In_Color;
in vec2 In_UV;

void main()
{
vec4 _19 = In_Color;
highp vec4 hp_copy_19 = _19;
vec4 _29 = texture(sTexture, In_UV);
highp vec4 hp_copy_29 = _29;
fColor = hp_copy_19 * hp_copy_29;
}
```

What's the problem, why does it constantly generate highp without forcing? It's clear that I can't use this modification because it affects other places like uniforms, etc.

Contributor guide

Open the contributing guide

Research direction

Start in GlslangToSpv.cpp at TranslatePrecisionDecoration and trace how the GLSL precision qualifier becomes SPIR-V decorations before SPIRV-Cross emits the ES output. Reproduce the shader case from the issue without the manual override, then compare the generated output with the RelaxedPrecision override. Done means the relevant mediump precision is preserved without applying the decoration indiscriminately to uniforms and other types.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.