bgfx icon indicating copy to clipboard operation
bgfx copied to clipboard

Shaderc create two same texture uniform in compile to HLSL

Open junjie020 opened this issue 2 years ago • 0 comments

Describe the bug I have a simple fs shader like this:

$input v_texcoord0
#include <bgfx_shader.sh>
SAMPLER2D(s_tex, 0);

void main()
{
    vec4 color = texture2D(s_tex, v_texcoord0);
	gl_FragColor = color;
}

After shader successful compile, and do:

bgfx::ShaderHandle handle = bgfx::createShader(...);

bgfx::UniformHandle_t u[16];
int n = bgfx::getShaderUniforms(handle, u, 16); //it will reture 2, and I expect it return 

for (int ii = 0; ii < n; ++ii)
{
  bgfx::UniformInfo info;
  bgfx::getUniformInfo(u[ii], &info);
  print("name:%s, type:%d, num:%d", info.name, info.type, info.num);  // it will print 2 same result
}

I saw the code in shaderc: https://github.com/bkaradzic/bgfx/blob/master/tools/shaderc/shaderc_hlsl.cpp#L517

It check D3D_SIT_SAMPLER and D3D_SIT_TEXTURE.

Because, SAMPLER2D defined in HLSL as two uniform: https://github.com/bkaradzic/bgfx/blob/master/src/bgfx_shader.sh#L401 (here will generate two uniform: s_texSampler, s_texTexture)

so, that check will enter twice: https://github.com/bkaradzic/bgfx/blob/master/tools/shaderc/shaderc_hlsl.cpp#L517

and will generate 2 uniforms with the same name for texture: https://github.com/bkaradzic/bgfx/blob/master/tools/shaderc/shaderc_hlsl.cpp#L526

In runtime, uniform in bgfx is hashed by uniform name, so it just only one uniform generated.

But bgfx::getShaderUniforms and will always return 2, but actually it only one uniform.

I think that is a bug for shaderc.

Is just only check D3D_SIT_TEXTURE here, is the bug fixed? https://github.com/bkaradzic/bgfx/blob/master/tools/shaderc/shaderc_hlsl.cpp#L517

junjie020 avatar Apr 28 '22 09:04 junjie020