Skip to content

Push constant size incorrectly padded to 16 alignment #290

@danginsburg

Description

@danginsburg

Given the following shader (https://shader-slang.com/slang-playground/?target=SPIRV&code=eJwrLikqTS5RcEksSazmUlBQyMwrUaiw5qq15uKKji7LtrIqKC3OiE_OzysuScwriY0Fq1QACTpDxay5gsJDUitKSotSDV1sgPrtFNLj_UtLCkpLQIbkleaWZBSlJqYUaxjqAKFmLFdZfmaKQnJ-LlBFqm9iZp6GJhfIbpiuaINYBVsUK_RALgIAaEQ55Q):

struct Data{
   int x;
};

[[vk::push_constant]] Data pushConstant;
RWTexture1D<int> g_Output;

[numthreads(1,1,1)]
void computeMain()
{
  g_Output[0] = pushConstant.x;
}

With spir-v output:

; SPIR-V
; Version: 1.5
; Generator: Khronos; 40
; Bound: 23
; Schema: 0
               OpCapability Image1D
               OpCapability Shader
               OpMemoryModel Logical GLSL450
               OpEntryPoint GLCompute %computeMain "main" %pushConstant %g_Output
               OpExecutionMode %computeMain LocalSize 1 1 1
               OpSource Slang 1
               OpName %Data_std430 "Data_std430"
               OpMemberName %Data_std430 0 "x"
               OpName %pushConstant "pushConstant"
               OpName %g_Output "g_Output"
               OpName %computeMain "computeMain"
               OpDecorate %Data_std430 Block
               OpMemberDecorate %Data_std430 0 Offset 0
               OpDecorate %g_Output Binding 0
               OpDecorate %g_Output DescriptorSet 0
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
        %int = OpTypeInt 32 1
%Data_std430 = OpTypeStruct %int
%_ptr_PushConstant_Data_std430 = OpTypePointer PushConstant %Data_std430
      %int_0 = OpConstant %int 0
%_ptr_PushConstant_int = OpTypePointer PushConstant %int
         %13 = OpTypeImage %int 1D 2 0 0 2 R32i
%_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
      %v4int = OpTypeVector %int 4
       %uint = OpTypeInt 32 0
     %uint_0 = OpConstant %uint 0
%pushConstant = OpVariable %_ptr_PushConstant_Data_std430 PushConstant
   %g_Output = OpVariable %_ptr_UniformConstant_13 UniformConstant
%computeMain = OpFunction %void None %3
          %4 = OpLabel
         %11 = OpAccessChain %_ptr_PushConstant_int %pushConstant %int_0
         %12 = OpLoad %int %11
         %14 = OpLoad %13 %g_Output
         %18 = OpCompositeConstruct %v4int %12 %12 %12 %12
               OpImageWrite %14 %uint_0 %18
               OpReturn
               OpFunctionEnd

spirv-reflect will report that the size of the push constant is 16 bytes instead of 4 bytes:

generator       : Khronos SPIR-V Tools Assembler
source lang     : Unknown
source lang ver : 1
source file     :
entry point     : main (stage=CS)
local size      : (1, 1, 1)


  Push constant blocks: 1

    0:
      spirv id : 2
      name     : pushConstant (Data_std430)
          // size = 16, padded size = 16
          struct Data_std430 {
              int x; // abs offset = 0, rel offset = 0, size = 4, padded size = 16
          } pushConstant;



  Descriptor bindings: 1

    Binding 0.0
      spirv id : 3
      set      : 0
      binding  : 0
      type     : VK_DESCRIPTOR_TYPE_STORAGE_IMAGE (UAV)
      count    : 1
      accessed : true
      name     : g_Output

I do not believe the push constant should be padded to be a multiple of 16-bytes and instead should be reported as a size of 4 bytes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions