KhronosGroup / KhronosGroup/SPIRV-Cross

Invalid MSL code generated when using build_combined_image_samplers()

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

Description

When transpiling the following GLSL fragment shader with build_combined_image_samplers() to MSL:

```glsl
#version 450
uniform texture2D tex;
uniform sampler smp;
in vec4 uv;
in vec4 color;
out vec4 frag_color;
void main() {
frag_color = texture(sampler2D(tex,smp), uv.xy) * color;
}
```

...the following invalid MSL code is generated, note the duplicate texture ~~and sampler~~ inputs which are assigned to the same bind slots causing errors later in the Metal compiler, also I think the ```tex.sample(smp, ...)``` call should be ```_32.sample(__32Smplr, ...```:

```c++
#include
#include

using namespace metal;

struct main0_out
{
float4 frag_color [[color(0)]];
};

struct main0_in
{
float4 uv [[user(locn0)]];
float4 color [[user(locn1)]];
};

fragment main0_out main0(main0_in in [[stage_in]], texture2d tex [[texture(0)]], texture2d _32 [[texture(0)]], sampler _32Smplr [[sampler(0)]], sampler smp [[sampler(1)]])
{
main0_out out = {};
out.frag_color = tex.sample(smp, in.uv.xy) * in.color;
return out;
}
```

...in contrast, the HLSL code generator seems to do the right thing:

```hlsl
Texture2D _32 : register(t0);
SamplerState __32_sampler : register(s0);

static float4 frag_color;
static float4 uv;
static float4 color;

struct SPIRV_Cross_Input
{
float4 uv : TEXCOORD0;
float4 color : TEXCOORD1;
};

struct SPIRV_Cross_Output
{
float4 frag_color : SV_Target0;
};

void frag_main()
{
frag_color = _32.Sample(__32_sampler, uv.xy) * color;
}

SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
uv = stage_input.uv;
color = stage_input.color;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.frag_color = frag_color;
return stage_output;
}
```

(as for the 'why?': when using build_combined_image_samplers(), the bind slots get assigned the same offset (e.g. texture(0) and sampler(0)), while otherwise, the sampler bind slot is "one off" (texture(0) and sampler(1)), which doesn't quite fit into my engine design (which still assumes that sampler state is part of the texture state, because of GLES2 compatibality).

Cheers and thanks! :)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at build_combined_image_samplers() and reproduce the GLSL-to-MSL example from the issue, comparing its bindings and sampling call with the shown HLSL output. Done means the generated MSL has no duplicate texture or sampler inputs, uses the expected bindings, and samples through the combined sampler.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.