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
If one tries to use it as a read only, which could make sense when passing the storage buffer to subsequent shaders, then one gets an error, due to how WGSL validation works at the moment.
// This is currently not possible, since it leads to an error
@group(0) @binding(0) var<storage, read> buffer : MiscData;
Error while parsing WGSL: :26:42 error: atomic variables in <storage> address space must have read_write access mode
@group(0) @binding(0) var<storage, read> buffer : MiscData;
^^^^^^^^^^^^^
:16:1 note: atomic sub-type of 'MiscData' is declared here
num_fine_shapes: atomic<i32>,
^^^^^
Current workaround
This means, if one wants to run multiple compute shaders that only read from an atomic variable in parallel, then one has to resort to creating an extra struct, with exactly the same layout and alignment, but a non-atomic variable.
structMiscData {
num_fine_shapes: i32, // I believe the spec says that this is valid
...
};
@group(0) @binding(0) var<storage, read> buffer : MiscData;
That is inconvenient, and could easily lead to either bugs (developer defined the secondary struct incorrectly) or performance left on the table (developer just used read_write).
Request
You could open an issue for that as a feature request. The validation on atomic built-in functions could be changed to have the access restrictions instead of the type. Only atomicLoad would be allowed in the read-only case. Hopefully, drivers would just optimize them to regular loads.
quote from alan-baker
Would it be possible to change the validation on atomic built-in functions to allow for atomicLoad in the read-only case?
The text was updated successfully, but these errors were encountered:
Worth noting that atomicLoad is emulated on D3D12. The good news is that HLSL doesn't use an explicit atomic type, but a WGSL compiler would likely need to specialize load function call tree for the read vs read/write cases.
KG: A struct has an atomic in it. We can put that in read-write storage, but you’re not allowed to put it in read-only storage, because our rules don’t permit it.
AB: This came out of a discussion where the author wanted to re-use structures, but wanted to remove the atomicness in one case, wanted to reuse the atomics in read-only storage.
AB: This would take some work in our backends, so we would like this not to be a priority.
JB: That’s Mozilla’s opinion too. Do it later if we want it later.
Problem
Currently, when one defines a struct with an
atomic
, one has to use it as aread_write
.If one tries to use it as a
read
only, which could make sense when passing the storage buffer to subsequent shaders, then one gets an error, due to how WGSL validation works at the moment.Current workaround
This means, if one wants to run multiple compute shaders that only read from an atomic variable in parallel, then one has to resort to creating an extra struct, with exactly the same layout and alignment, but a non-atomic variable.
That is inconvenient, and could easily lead to either bugs (developer defined the secondary struct incorrectly) or performance left on the table (developer just used read_write).
Request
Would it be possible to change the validation on atomic built-in functions to allow for
atomicLoad
in the read-only case?The text was updated successfully, but these errors were encountered: