KhronosGroup / KhronosGroup/SPIRV-Cross

GLSL: Passing global image+sampler as function parameters generates a new global sampler

Open
#1,926 0 comments 0 reactions 0 assignees View on GitHub
needs triage
Dominant language
GLSL
Stars
2.5k
Forks
713
Avg merge
2d 18h
Merged PRs (30d)
16

Description

When passing a global sampler+image to a function, the generated GLSL code will have a new global sampler for that call.

[sample_test.zip](https://github.com/KhronosGroup/SPIRV-Cross/files/8567425/sample_test.zip) (contains: HLSL, SPIRV(Dissembled), GLSL)

Backed by this HLSL for testing:
```
Texture3D volume: register(t1);
SamplerState volume_sampler: register(s1);

float test(Texture3D texture_3d, SamplerState reserved_sampler, float3 uv) {
return texture_3d.Sample(reserved_sampler, uv).r;
}

float4 color: SV_TARGET0 main(float3 uv: TEXCOORD1)
{
float d1 = volume.Sample(volume_sampler, uv).r;
float d2 = test(volume, volume_sampler, uv);
return float4(d1, d2, 0.0, 1.0);
}
```

Generated GLSL:
```
#version 450

layout(binding = 1) uniform sampler3D _72;
layout(binding = 0) uniform sampler3D _77;

layout(location = 0) in vec3 uv;
layout(location = 1) out vec4 _entryPointOutput;

float test(vec3 uv_1, sampler3D SPIRV_Cross_Combinedtexture_3dreserved_sampler)
{
return texture(SPIRV_Cross_Combinedtexture_3dreserved_sampler, uv_1).x;
}

vec4 _main(vec3 uv_1)
{
vec3 param = uv_1;
return vec4(texture(_72, uv_1).x, test(param, _77), 0.0, 1.0);
}

void main()
{
vec3 param = uv;
_entryPointOutput = _main(param);
}
```

If I added another call to `test()`, it will generate yet another global `Sampler3D` with the same binding (binding = 0).

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the sample_test.zip attachment and compare its HLSL, disassembled SPIR-V, and generated GLSL output. Trace how global image and sampler arguments are lowered for calls to test(); done means repeated calls do not create additional global samplers with the same binding.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.