Skip to content

Storage variables shouldn't be required to be structs #2188

Closed

Description

@litherum

Trying to compile this:

[[group(0), binding(0)]] var<storage, read> myArray: array<i32, 8>;

is an error. Instead, the author has to say:

[[block]] struct MyData {
    myArray: array<i32, 8>;
};
[[group(0), binding(0)]] var<storage, read> myBuffer: MyData;

This seems like a lot of characters to type just to get something simple. We should relax this constraint, and if the backend API requires a struct, a single-member struct can be generated around the member type.

So, the WebGPU author could instead write:

[[group(0), binding(0), block]] var<storage, read> myArray: array<i32, 8>;

or even better, just

[[group(0), binding(0)]] var<storage, read> myArray: array<i32, 8>;

Activity

  1. added
    wgsl
    on Oct 16, 2021
  2. ben-clayton commented on Oct 16, 2021

    @ben-clayton
    Contributor

    Whatever relaxation we do for storage should probably also be applied to uniform too.

  3. litherum commented on Oct 19, 2021

    @litherum
    ContributorAuthor

    In particular, it seems like this requirement of all buffer contents being wrapped in a struct, in conjunction with the fact that all pointer parameters have to point to a whole variable, and not a field of a variable, means that atomics are impossible to use? Since they lie within buffers (and therefore within structs), can't be copied out, and the atomic functions all take pointers.

  4. jrprice commented on Oct 19, 2021

    @jrprice
    Contributor

    the fact that all pointer parameters have to point to a whole variable, and not a field of a variable

    This restriction only applies to user-defined functions. Passing a pointer to a field of a variable is allowed for builtin functions, specifically to allow the cases for the atomic functions and for arrayLength.

  5. kdashg commented on Oct 20, 2021

    @kdashg
    Contributor
    WGSL meeting minutes 2021-10-19
    • AB: should be tied to ‘block’ removal.
    • MM: Think we can discuss at the same time, but think it can be treated as separate.
    • AB: Bound up with making clear how to differentiate descriptor-of-array vs. array-of-descriptors. Already in a draft PR queued up for discussion.
    • JG: ok, will defer.
  6. kvark commented on Nov 26, 2021

    @kvark
    Contributor

    @litherum

    all pointer parameters have to point to a whole variable, and not a field of a variable

    where is this coming from?

  7. added this to the V1.0 milestone on Dec 8, 2021
  8. dneto0 commented on Dec 8, 2021

    @dneto0
    Contributor

    @litherum

    all pointer parameters have to point to a whole variable, and not a field of a variable

    where is this coming from?

    In Function restrictions:

    Each argument of pointer type to a user-defined function must be one of:

    • An address-of expression of a variable identifier expression
    • A function parameter

    The limitation comes from base functionality in SPIR-V / Vulkan. You'd have to have a VariablePointers capability to get more flexible behaviour.

  9. dneto0 commented on Dec 8, 2021

    @dneto0
    Contributor

    We've discussed this internally, and Google is in favour of removing this requirement.

  10. added a commit that references this issue on Dec 8, 2021
    64aace6
  11. magcius commented on Dec 14, 2021

    @magcius

    I'd like to at least constrain this to allow for only structs and arrays to be supported in toplevel declarations. Forcing users to wrap a single variable in a struct might seem a bit restrictive, but we should design the language that allows for "the right way" to be the path of least effort. I'll use an example for uniform buffers, but the same logic should apply to storage variables as well, though the use cases are a bit different. Consider:

    [[group(0), binding(0)]] var<uniform> color: vec4<f32>;
    

    The most obvious way to extend this to support two colors would look like this, by adding a single line:

    [[group(0), binding(0)]] var<uniform> color: vec4<f32>;
    [[group(0), binding(1)]] var<uniform> color2: vec4<f32>;
    

    Which might be a poor practice -- it's probably the user would actually want a struct containing two colors. Compare when starting with a struct:

    struct ColorData {
        color: vec4<f32>;
    };
    [[group(0), binding(0)]] var<uniform> color: ColorData;
    

    The obvious place to add a second color is by adding a single line in the struct.

    Especially as users might be used to WebGL-style uniforms, we should try to design the language to guide them towards best practices.

    However, I think arrays are fine -- you can view them as syntactic sugar for structs, almost, so I'm fine with them being allowed in top-level declarations. And they don't provide any resistance in the '1-to-many' case I want to encourage.

  12. dneto0 commented on Dec 14, 2021

    @dneto0
    Contributor

    The proposal at #2401 has 👍 from @m-schuetz and enthusiastic endorsement from @Kangz

  13. magcius commented on Dec 14, 2021

    @magcius

    Then please treat my comment as a 👎 on the proposal if it will allow any type as a toplevel declaration -- not sure the proper place or process to file feedback on the proposal.

  14. litherum commented on Dec 24, 2021

    @litherum
    ContributorAuthor

    I think I would agree with these concerns if WebGPU had GL-style glUniform4fv() calls. However, we don't; the only way to attach data to these bind points is by using buffers. It seems like it would be more natural to add more data into an existing buffer than it would be to allocate a new buffer or create a new bind group. So I don't think I agree with these concerns.

  15. kvark commented on Dec 24, 2021

    @kvark
    Contributor

    We are getting pretty deep into what a hypothetical user would do, but for completeness of this story: they could be having multiple bindings pointing to sub-ranges of the same buffer.

  16. magcius commented on Dec 24, 2021

    @magcius

    Consider someone who is approaching WebGPU by modifying an example. Any user would have to modify both the shader program and the CPU API usage.

    If such a user starts by modifying the CPU API usage, I imagine they would be discouraged, yes.

    However, consider a user who started by modifying the shader text to add another color. They might copy/paste the single line, and then find a way to adapt the CPU API usage to match.

    I would prefer if both these approaches resulted in a similar path.

  17. cwfitzgerald commented on Dec 27, 2021

    @cwfitzgerald
    Contributor

    Small consideration:

    [[group(0), binding(0)]] var<storage, read> myArray: array<i32, 8>;
    

    Is this a single binding of 8 i32s, or 8 bindings which contain i32s. While we don't have binding arrays now, we definitely will in the future. I don't particularly care how we resolve this ambiguity (var<array<storage>, read> maybe -- but not the point), we should just have it in our minds.

  18. kainino0x commented on Dec 28, 2021

    @kainino0x
    Contributor

    To me this reads most easily as a single binding, so I've been pushing for a different, clearer syntax for arrays of bindings.

  19. added a commit that references this issue on Jan 11, 2022
    5fc2495
  20. kdashg commented on Jan 12, 2022

    @kdashg
    Contributor
    WGSL meeting minutes 2022-01-11
    • (PR to make it so: Store type for buffer does not have to be structure #2401)
    • DN: Removes constraints that used to be in the language. Some pushback against having e.g. a whole binding for one scalar, want to authors towards backing things.
    • MM: It’s not like adding a second binding point is free. If an app had a single float, and then another, they have two ways to do it. Neither of those is strictly “free”, as one needs more code in the shader, and one needs more code in the api side. Also, you sort of need more code in the api side than you would in the shader size.
    • DM: I think we want to push people towards packing into fewer bindings.
    • MM: I think that that’s true already without forbidding it, where managing multiple bindings is sort of more work than packing.
    • KG: Let’s deal with it with education.
    • DM: I think it’s a poor api if it needs more education.
    • BC: The argument is you don’t want to encourage people that don’t really know about separation of variables by binding update; you don’t want the mentality of GL uniform. If someone is likely to make that mistake, then they’ll likely update the entire buffers many times, just as much as separate bindings. They’ll update one big fat structure very frequently (which is also bad). Either people know how to use bindings well, or not. This change doesn’t make mistakes worse. Seen lots of code where a single binding is a struct with a single thing in it, and that’s unnecessarily verbose.
    • MM: Concrete example shader. Binding point with a single field in it. In Metal it’s common to have arg-buffers with gigs worth of data; and those are relatively static. But a separate argument / bindpoint which is a time-variable parameter, that is updated very often. That’s a correct, valid, good-style shader that is permitted by this change.
    • DM: If you have another frequently changing thing, where do you put it.
    • MM: Either make a third binding point, or join it with the time-parameter.
    • KG: How much do we care for v1, how much implementation work, how much can we disincentivize/ discourage people to funnel them to the “right” course.
    • DM: I think if we provide this feature, we probably shouldn’t warn about it.
    • MM: DM, do you think we should never add this? Like, not even after V1?
    • DM: I think what we already require is fine/tolerable forever, and is all we need, yes.
    • BC: From implementation perspective, it’s more work to enable arrays but not others. Because target languages that only accept a struct will need work, but then additional work to carve out the array case.
    • DN: Google’s consensus is that we should accept the PR, and remove these restrictions.
    • MM: I wonder if a UA could watch the bindings, and report console messages if applications consistently update the same set of bindings at the same time. “Hey, you might want to coalesce these…”
    • DM: I am weakly against, but if there is consensus otherwise I will not block this
    • DN to land
  21. added a commit that references this issue on Jan 19, 2022
    9338e7a
  22. added a commit that references this issue on Jan 19, 2022
    5a0cbb2
  23. added 2 commits that reference this issue on Jan 24, 2022
    9607502
    99f9fab
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions