Skip to content

wgsl: Sketch design for arrays of buffer-bindings #2105

Open

Description

@dneto0

We need a sensible design for supporting arrays-of-buffers

Arrays-of-bindings (for buffers) are an important newer feature of APIs, supporting a bindless style.

These can't be included in WebGPU 1.0, but the language design for that future feature may influence the 1.0 language.
So this is tagged for "1.0" for now.

This discussion builds on the two solutions so far for #1534:

What Vulkan has

  • Vulkan supports a single-dimensional array of buffers.
    * Vulkan’s main feature bit: descriptorIndexing, and fine grain features in VkPhysicalDeviceDescriptorIndexingFeatures
  • Each element of the array corresponds to a descriptor binding point. The 0th element maps to the given descriptorSet and binding attribute, and binding numbers increase by 1 for each successive array element.
    • Indexing into these is limited, by default, with opt-in extra features:
      • Indexing by a SPIR-V constant expression
      • Indexing by a dynamically computed but still uniform value
      • Indexing by a dynamically computed value, not necessarily uniform.
  • An extra feature is to allow a runtime-sized array of these descriptors. see the runtimeDescriptorArray subfeature in VkPhysicalDeviceDescriptorIndexingFeatures
  • Note: The design below does not contemplate these gradations of functionality.

Design sketch: arrays of buffers for WGSL

Key idea, while avoiding a block attribute: mark the container to say its elements are buffer-roots.

This covers both sized-arrays of buffers and dynamically-sized arrays of buffers.

Observations/questions about arrays of buffers

  • Layout does not make sense, since it’s not contiguous storage.
    • Perhaps stride does not apply: no [[stride(N)]] attribute, or stride parameterization of the array type?
  • Are the array types constructible?
    • What if the same type is used for array-of-buffer and internally?
    • Perhaps allow them to be “constructible”, but limit what operations can be done on them, based on buffer-array variable concept, as below.
  • Maybe it’s not really about the type, but about its use for a buffer-array variable.
    • Principle: Allow reuse of a structure type for other things.
    • This is allowed in options 0 and 4a.

Syntax and inference

Below are several options for how buffer-arrays are spelled out in WGSL source.

struct B {
  a: i32;
  data: array<f32>;
};

[[group(0),binding(0)]]
var<storage> single: B;

/* 
   Option 0: Don't have syntax. Implicit all the way down.
   Idea: 
   - Infer the fact that these are buffer-array variables from the fact that
     they are variables in the uniform or storage storage class, and are
     arrays around a struct.
   - Least fuss for users. Not much more work for tooling.
*/

[[group(1),binding(0)]] var<storage> sized: array<B,10>;  
[[group(2),binding(0)]] var<storage> dynsized: dynamic_array<B>;



/* 
   Option 4b: Different syntax keyword for introducing the type.
          4b: Different words.
              But is it dynamic_buffer_array (more logical)
              or buffer_dynamic_array (looks more composed, i.e. "just add dynamic_ before array"
*/

[[group(1),binding(0)]] var<storage> sized: buffer_array<B,10>;
[[group(2),binding(0)]] var<storage> dynsized: dynamic_buffer_array<B>;



/* 

   Option 5: Alternate syntax for struct to indicate it's only usable for a binding.
   Some spec overhead, but also clearly delineates what can and cannot be done.

 */

block ssbo_interface {
  x : dynamic_array<f32>;
};

[[group(0), binding(0)]] var<storage>  sized : array<ssbo_interface, 8>;
[[group(0), binding(1)]] var<storage>  dynsized : dynamic_array<ssbo_interface>;

Activity

  1. added
    wgsl
    on Sep 14, 2021
  2. added this to the V1.0 milestone on Sep 14, 2021
  3. kainino0x commented on Sep 14, 2021

    @kainino0x
    Contributor

    I think it's best to leave open the possibility of not requiring every binding to be a struct, but instead allowing arbitrary storable types to be used as buffer-roots. For example:
    var<storage> data: i32;
    var<storage> data: MyStruct;
    var<storage> data: array<i32, 5>;
    var<storage> data: array<MyStruct, 5>;

    I think option 0 closes it, unless modified to add an annotation specifying it's an array, e.g.
    [[group(0), binding_array_start(0)]] var<storage> data: array<MyStruct, 5>;
    or
    [[group(0), binding(0), binding_array]] var<storage> data: array<MyStruct, 5>.

    FWIW I'm pretty sure bindings arrays need to extend to textures as well. This would work there too:
    [[group(0), binding_array_start(0)]] var texs: array<texture_2d<f32>, 5>;
    [[group(0), binding_array_start(0)]] var texs: array<texture_2d_array<f32>, 5>;

    or with 4b:
    [[group(0), binding(0)]] var texs: array_of_texture_2d<f32>;
    [[group(0), binding(0)]] var texs: array_of_texture_2d_array<f32>; // lol

  4. kvark commented on Sep 14, 2021

    @kvark
    Contributor

    Anybody explain to me why we have Options: 0, 4b, and 5?
    One option that is missing here is:

    • keep [[block]].
    • arrays of [[block]] are considered binding arrays
    • arrays of non-block are just value arrays
  5. kainino0x commented on Sep 14, 2021

    @kainino0x
    Contributor

    Anybody explain to me why we have Options: 0, 4b, and 5?

    We had an internal doc that loosely brainstormed a bunch of different possible syntaxes with smallish differences. These three were the contenders to start discussion from, but kept the numbering.

    • arrays of non-block are just value arrays

    Or could be [[block]] array<...>

  6. dneto0 commented on Dec 8, 2021

    @dneto0
    ContributorAuthor

    Other discussions have progressed:

    That essentially eliminates options 0 and 5.
    So this points to having something along the lines of 4: special syntax to indicate it's an binding array.

    I hear there's discussion about arrays of textures and samplers also, so we can aim for something general, e.g binding_array<T,N> declares an array of bindings, where each binding contains a T.

     // 10 bindings, each holding an i32.  Silly, but ok.
    [[group(0),binding(0)]] var<storage> binding_array<i32,10>; 
    
  7. dneto0 commented on Dec 8, 2021

    @dneto0
    ContributorAuthor

    Should extend the example:

    [[group(0),binding(1)]] var binding_array<texture2d,3>; // array of three sampled textures
    

    Base Vulkan/SPIR-V functionality restricts the indexing into such arrays (e.g. must be uniform value) so it's useful to consider them their own type, so we have a hook to hang the restriction on.

  8. dneto0 commented on Mar 8, 2022

    @dneto0
    ContributorAuthor

    I think we have good options for adding this feature after v1 ships.
    It's a fundamental feature, so at very worst we can add an enable, which allows us to use whatever syntax makes sense.

    Recommend pushing out of v1 milestone. I think I put it in v1 when i was concerned about implications for removing the block and stride attributes. Those have been solved.

  9. modified the milestones: V1.0, post-V1 on Mar 9, 2022
  10. kdashg commented on Mar 9, 2022

    @kdashg
    Contributor
    WGSL meeting minutes 2022-03-08
    • DN: I think this isn’t pressing for v1. I was pushing for this before when things were different, but now I don’t think we need it for v1, and that we’d be safe to do this post-v1 if desired.
    • MM: +1
  11. dneto0 commented on Sep 27, 2023

    @dneto0
    ContributorAuthor

    Related: #822 is about support for arrays of textures. That has some discussion of syntax.
    I'm tempted to close this issue in favour of that one, as long as the solution there generalizes to other bound resource kinds.

  12. kdashg commented on Nov 28, 2023

    @kdashg
    Contributor
    WGSL 2023-11-28 Minutes
    • Do we want this to stay in M1?
    • KG: Propose move to M2.
    • AB: Regression vs. GL. But not at the top of our list.
    • KG: Moved to M2.
    • TT: Think texture array is regression from GL, not buffers.
    • AB: Want to design both at the same time.
    • AB: Arrays of textures is Support for arrays of textures #822 (and is currently M1)
  13. kdashg commented on Dec 6, 2023

    @kdashg
    Contributor
  14. devshgraphicsprogramming commented on Jan 26, 2025

    @devshgraphicsprogramming

    How about a BDA-like-API emulation? Since it seems the WebGPU API will always be playing catch-up to Vulkan, Metal and DX12 so may as well skip the immediate teething pain step of each of those APIs which are arrays of buffers and progress straight onto uint64_t virtual addresses.

    In DX12 one could emulate Vulkan's BDA via an array of ByteAddressBuffers in special (hidden/reserved) global (instance/context wide) descriptor set where you'd use the lower 32 bits of the uint64_t to denote a byte and the upper to denote the ByteAddressBuffer you'd use (or one can do the uvec2 BDA shenanigans GLSL/SPIR-V allow for the exotic GPUs without uint64_t support).

    Then any buffer you'd make with the DEVICE_ADDRESS usage would be allocated a slot in such reserved descriptor array.

    Obvioulsy this means nonUniformEXT around every bufffer load/store, but you'd get the range checks and robustness behaviour you'd usually have with SSBOs.

    P.S. Ofc if Metal and/or VK ever get robust BDA, you could implement the whole thing as "just BDA".

  15. modified the milestones: Milestone 4+, Milestone 2 on Oct 2, 2025
  16. jimblandy commented on Feb 9, 2026

    @jimblandy
    Contributor

    I think this design work will be subsumed by the bindless effort. Do we really want to add this as a distinct feature before bindless lands?

  17. dneto0 commented on Feb 10, 2026

    @dneto0
    ContributorAuthor

    Moved to M3, per agreement in WGSL meeting 2026-02-10

  18. jimblandy commented on Feb 11, 2026

    @jimblandy
    Contributor
    minutes from WGSL committee meeting 2026-2-10
    • JB: I'd like the committee to decide whether this is subsumed by bindless and can be closed.
    • (Corentin, offline): this is technically possible to support without bindless and a functionality regression compared to WebGL (at least for textures / samplers, not sure for buffers).
    • (dneto, offline): I think we should revisit this after bindless. Bindless is a higher priority: it offers greater functionality and I think is preferrable.
    • DN: Filed as a stake in the ground to have folks start thinking. Lower priority then bindless. If folks have bindless they'll use that first. Think bindless is more important to work on first. Think the strongest argument for this is the notion that it's a regression from webgl
    • JB: Given the bindless timelines, does it make sense for M3? Mozilla would like this in M3
    • DN: Sounds good
    • JB: Resolved, move to M3
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

    • Status
      Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions