Skip to content

Do we need [[block]] annotations? #1008

Closed

Description

@kdashg

Related to #1004.

It might be possible to remove [[block]] annotations if we can expect implementations to reasonably infer them and synthesize them as-needed on backends.

Activity

  1. added
    wgsl
    on Aug 18, 2020
  2. dneto0 commented on Aug 19, 2020

    @dneto0
    Contributor

    Conceptually the block decoration on resources indicates "this object maps 1-1 to a binding slot".

    So a consideration is what do we do when we can have arrays of resources. Without the block decoration, we have an ambiguity to resolve.
    You can have sized or unsized arrays of those resources. The latter case is common when using a "bindless" approach.

  3. kvark commented on Aug 19, 2020

    @kvark
    Contributor

    Conceptually the block decoration on resources indicates "this object maps 1-1 to a binding slot".

    I'm confused now. You mentioned that [[block]] is only for uniform buffers, and it doesn't support dynamic arrays. But storage buffers with dynamic arrays are also struct declarations that are 1-1 to a binding slot.

  4. dneto0 commented on Aug 19, 2020

    @dneto0
    Contributor

    I'm not explaining it clearly.

    A block-decorated struct describes the content of a binding. That's true for uniform-buffer and storage-buffer.
    The last element of the struct of a storage buffer may be an unsized array.
    (From memory, the

    What I'm saying is that, with an extension, Vulkan can do unsized arrays of those block-decorated structs.
    The use case is bindless-style programming, where only the binding entries accessed from the descriptor array need to have been bound.

    Here's an example in GLSL for a compute shader:

    #version 450
    
    // This extension allows non-uniform indexing of the runtime-arrays of UBO and SSBO
    // See SPIR-V capability RuntimeDescriptorArray
    #extension GL_EXT_nonuniform_qualifier : require
    
    layout(set=0,binding=0) uniform U {
      uint a;
      float data[10]; // sized, hence OpTypeArray
    } myuni [];       // becomes OpTypeRuntimeArray
    
    layout(set=0,binding=1) buffer B {
      uint a;
      float data[];   // unsized allowed.  Becomes OpTypeRuntimeArray
    } mybuf [];       // becomes OpTypeRuntimeArray
    
    void main() {
      uint i = myuni[0].a;
      mybuf[i].data[0] = myuni[i*i].data[0];
    }

    Compiling through "glslangValidator -V", we get this SPIR-V assembly:

                   OpCapability Shader
                   OpCapability RuntimeDescriptorArray
                   OpExtension "SPV_EXT_descriptor_indexing"
              %1 = OpExtInstImport "GLSL.std.450"
                   OpMemoryModel Logical GLSL450
                   OpEntryPoint GLCompute %main "main"
                   OpExecutionMode %main LocalSize 1 1 1
                   OpSource GLSL 450
                   OpSourceExtension "GL_EXT_nonuniform_qualifier"
                   OpName %main "main"
                   OpName %i "i"
                   OpName %U "U"
                   OpMemberName %U 0 "a"
                   OpMemberName %U 1 "data"
                   OpName %myuni "myuni"
                   OpName %B "B"
                   OpMemberName %B 0 "a"
                   OpMemberName %B 1 "data"
                   OpName %mybuf "mybuf"
                   OpDecorate %_arr_float_uint_10 ArrayStride 16
                   OpMemberDecorate %U 0 Offset 0
                   OpMemberDecorate %U 1 Offset 16
                   OpDecorate %U Block
                   OpDecorate %myuni DescriptorSet 0
                   OpDecorate %myuni Binding 0
                   OpDecorate %_runtimearr_float ArrayStride 4
                   OpMemberDecorate %B 0 Offset 0
                   OpMemberDecorate %B 1 Offset 4
                   OpDecorate %B BufferBlock
                   OpDecorate %mybuf DescriptorSet 0
                   OpDecorate %mybuf Binding 1
           %void = OpTypeVoid
              %3 = OpTypeFunction %void
           %uint = OpTypeInt 32 0
    %_ptr_Function_uint = OpTypePointer Function %uint
          %float = OpTypeFloat 32
        %uint_10 = OpConstant %uint 10
    %_arr_float_uint_10 = OpTypeArray %float %uint_10
              %U = OpTypeStruct %uint %_arr_float_uint_10
    %_runtimearr_U = OpTypeRuntimeArray %U
    %_ptr_Uniform__runtimearr_U = OpTypePointer Uniform %_runtimearr_U
          %myuni = OpVariable %_ptr_Uniform__runtimearr_U Uniform
            %int = OpTypeInt 32 1
          %int_0 = OpConstant %int 0
    %_ptr_Uniform_uint = OpTypePointer Uniform %uint
    %_runtimearr_float = OpTypeRuntimeArray %float
              %B = OpTypeStruct %uint %_runtimearr_float
    %_runtimearr_B = OpTypeRuntimeArray %B
    %_ptr_Uniform__runtimearr_B = OpTypePointer Uniform %_runtimearr_B
          %mybuf = OpVariable %_ptr_Uniform__runtimearr_B Uniform
          %int_1 = OpConstant %int 1
    %_ptr_Uniform_float = OpTypePointer Uniform %float
           %main = OpFunction %void None %3
              %5 = OpLabel
              %i = OpVariable %_ptr_Function_uint Function
             %19 = OpAccessChain %_ptr_Uniform_uint %myuni %int_0 %int_0
             %20 = OpLoad %uint %19
                   OpStore %i %20
             %26 = OpLoad %uint %i
             %28 = OpLoad %uint %i
             %29 = OpLoad %uint %i
             %30 = OpIMul %uint %28 %29
             %32 = OpAccessChain %_ptr_Uniform_float %myuni %30 %int_1 %int_0
             %33 = OpLoad %float %32
             %34 = OpAccessChain %_ptr_Uniform_float %mybuf %26 %int_1 %int_0
                   OpStore %34 %33
                   OpReturn
                   OpFunctionEnd
    
  5. dneto0 commented on Sep 14, 2021

    @dneto0
    Contributor

    See #2104

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wgsl

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions