Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect validation error with dispatchWorkgroupsIndirect #4632

Open
brendan-duncan opened this issue May 13, 2024 · 3 comments
Open

Incorrect validation error with dispatchWorkgroupsIndirect #4632

brendan-duncan opened this issue May 13, 2024 · 3 comments
Labels
api WebGPU API

Comments

@brendan-duncan
Copy link

The scenario:
A compute shader takes a storage buffer as read_write.
A buffer has both indirect and storage usages.
The buffer is in a BindGroup for use as the storage buffer with the compute shader.
The same buffer is used as the indirect buffer used for calling dispatchWorkgroupsIndirect.

WebGPU will have the validation error

[Buffer (unlabeled)] usage (BufferUsage::(Storage|Indirect)) includes writable usage and another usage in the same synchronization scope.
 - While validating compute pass usage.

This is an incorrect error as the indirect buffer is read and/or copied before the compute is dispatched, and therefore not a race condition on the buffer by the GPU.

Here is a repo example:

dispatch_indirect_error.html.txt

@kainino0x
Copy link
Contributor

kainino0x commented May 18, 2024

I'm not totally sure this is safe. Since dispatches have to broken up into chunks for scheduling (since dispatch can be larger than the number of shader lanes) it does seem possible the driver could read the indirect buffer after it's been written, at the very least to populate the @num_workgroups builtin, but it possibly could cause deeper logic bugs in the driver.

We would have to see what the Vulkan folks say, but even so, it would be hard to know for sure if it's safe in Metal, D3D12, and all existing Vulkan drivers.

@magcius
Copy link

magcius commented Jun 1, 2024

D3D12 with legacy barriers would not support this use case. D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT is a read-only state, and cannot be bound together with a read-write state like D3D12_RESOURCE_STATE_UNORDERED_ACCESS at the same time. D3D12 enhanced barriers can support this, but you need to be careful.

However, as @kainino0x suggests, if the compute shader itself writes over its own dispatch parameters, this is unsafe.

@Kangz
Copy link
Contributor

Kangz commented Jun 4, 2024

Note that we expect most WebGPU implementations do already be doing a copy of the indirect arguments for validation, so it might be safe after all. But that means that it would be unsafe when validation is disabled, which would be weird since the behavior of valid code would change between validation on vs. off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api WebGPU API
Projects
None yet
Development

No branches or pull requests

4 participants