You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Storage variables shouldn't be required to be structs #2188
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.
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.
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.
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:
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.
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.
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.
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.
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.
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.
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
Activity
ben-clayton commentedon Oct 16, 2021
Whatever relaxation we do for
storageshould probably also be applied touniformtoo.litherum commentedon Oct 19, 2021
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.
jrprice commentedon Oct 19, 2021
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.kdashg commentedon Oct 20, 2021
WGSL meeting minutes 2021-10-19
kvark commentedon Nov 26, 2021
@litherum
where is this coming from?
dneto0 commentedon Dec 8, 2021
In Function restrictions:
The limitation comes from base functionality in SPIR-V / Vulkan. You'd have to have a VariablePointers capability to get more flexible behaviour.
dneto0 commentedon Dec 8, 2021
We've discussed this internally, and Google is in favour of removing this requirement.
Store type for buffer does not have to be structure
magcius commentedon Dec 14, 2021
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:
The most obvious way to extend this to support two colors would look like this, by adding a single line:
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:
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.
dneto0 commentedon Dec 14, 2021
The proposal at #2401 has 👍 from @m-schuetz and enthusiastic endorsement from @Kangz
magcius commentedon Dec 14, 2021
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.
litherum commentedon Dec 24, 2021
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.
kvark commentedon Dec 24, 2021
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.
magcius commentedon Dec 24, 2021
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.
cwfitzgerald commentedon Dec 27, 2021
Small consideration:
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.kainino0x commentedon Dec 28, 2021
To me this reads most easily as a single binding, so I've been pushing for a different, clearer syntax for arrays of bindings.
Store type for buffer does not have to be structure
kdashg commentedon Jan 12, 2022
WGSL meeting minutes 2022-01-11
Store type for buffer does not have to be structure
Store type for buffer does not have to be structure (#2401)
Store type for buffer does not have to be structure (gpuweb#2401)
Store type for buffer does not have to be structure (gpuweb#2401)
refactor(bars): use structs for uniform buffers