Skip to content

[SPIR-V] Specialization constants don't work if the shader uses the implicit Globals UBO #4542

Closed

Description

Title

[SPIR-V] Specialization constants don't work if the shader uses the implicit Globals UBO

Functional impact

Specialization constants get placed into the $Globals UBO as regular constants when the $Globals UBO is present. This is incorrect and makes them practically unusable.

Minimal repro steps

  1. Compile the following shader with dxc.exe -T ps_6_0 -E main -spirv repro_shader.txt:
[[ vk::constant_id ( 1 )]] const int mySpecializationConstant = 0;

//cbuffer NamedCBuffer {
float myGlobal;
//};

float4 main(float4 col : COLOR) : SV_Target0
{
	if(mySpecializationConstant)
		return 0.xxxx;
	
	return myGlobal.xxxx;
}

Expected result

The SPIR-V should contain a specialization constant named mySpecializationConstant and the $Globals UBO should contain only one member named myGlobal

Actual result

The SPIR-V doesn't contain any specialization constants and it places mySpecializationConstant into the $Globals UBO (see full output in spec_constant_simple_repro.spv.txt):

               OpName %type__Globals "type.$Globals"
               OpMemberName %type__Globals 0 "mySpecializationConstant"
               OpMemberName %type__Globals 1 "myGlobal"
               OpName %_Globals "$Globals"
               OpName %out_var_SV_Target0 "out.var.SV_Target0"
               OpName %main "main"
               OpDecorate %out_var_SV_Target0 Location 0
               OpDecorate %_Globals DescriptorSet 0
               OpDecorate %_Globals Binding 0
               OpMemberDecorate %type__Globals 0 Offset 0
               OpMemberDecorate %type__Globals 1 Offset 4
               OpDecorate %type__Globals Block

Also, the compiler produces a warning pointing to the wrong name myGlobal:

spec_constant_simple_repro.txt:1:65: warning: variable 'myGlobal' will be placed in $Globals so initializer ignored
[[ vk::constant_id ( 1 )]] const int mySpecializationConstant = 0;
                                                                ^

Further technical details

  • Used DXC from the latest official built release v1.6.2112
  • Placing the constant myGlobal into a named cbuffer (as is commented out in the repro shader) instead of into the implicit $Globals cbuffer fixes the issue

Activity

  1. added
    spirv
    on Jul 14, 2022
  2. chenyangchenyang commented on Aug 25, 2023

    @chenyangchenyang

    Has Any Update?

  3. s-perron commented on Jul 26, 2024

    @s-perron
    Collaborator

    Yes, we should fix this. I'll look into it next week.

  4. moved this from For Google to Triaged in HLSL Triageon Jul 26, 2024
  5. s-perron commented on Jul 29, 2024

    @s-perron
    Collaborator

    push constants have the same problem.

    We should update this function:

    bool shouldSkipInStructLayout(const Decl *decl) {

    We need to add checks for external variables that would have an early return before this call:

    var = declIdMapper.createExternVar(decl);

  6. added a commit that references this issue on Jul 29, 2024
    203db79
  7. added a commit that references this issue on Jul 31, 2024
    bb373fb
  8. added a commit that references this issue on Jul 24, 2025
    ed339f9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

spirv

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions