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

Uniform block sizes seem to be rounded up to the nearest 16 on WebGL #2072

Closed
Liamolucko opened this issue Oct 15, 2021 · 5 comments · Fixed by #2808
Closed

Uniform block sizes seem to be rounded up to the nearest 16 on WebGL #2072

Liamolucko opened this issue Oct 15, 2021 · 5 comments · Fixed by #2808
Labels
api: gles Issues with GLES or WebGL area: validation Issues related to validation, diagnostics, and error handling type: bug Something isn't working
Milestone

Comments

@Liamolucko
Copy link
Contributor

Description
WebGL seems to be rounding the size of uniforms up to the nearest 16, causing errors to be thrown when the slightly-smaller buffer is passed to a draw call. Adding some padding to the buffer solves the problem.

Repro steps
None of wgpu's examples use uniforms with alignments less than 16, so I created a toy example which draws a vertical line: https://github.com/Liamolucko/wgpu-uniform-repro. Build with cargo build && wasm-bindgen target/wasm32-unknown-unknown/debug/wgpu-uniform-repro.wasm --target web --out-dir pkg, and then start a http server for the folder.

Expected vs observed behavior
Expected:
image
Observed:
image

WebGL warning: drawArraysInstanced: Buffer for uniform block is smaller than UNIFORM_BLOCK_DATA_SIZE. wgpu-uniform-repro.js:604:25

Platform
wgpu 0.11.0, Firefox 93 (also fails in Chromium), Manjaro Linux, nVidia GeForce GTX 780.

@kvark kvark added the area: validation Issues related to validation, diagnostics, and error handling label Oct 15, 2021
@kvark
Copy link
Member

kvark commented Oct 15, 2021

Interesting. So it sounds like a validation issue? Other backends do not require this.
Best we can do is to have some validation code active on WebGL/GLES specifically and checking for this.

@Liamolucko
Copy link
Contributor Author

Apparently this used to be a layout requirement for uniform buffers, but was removed in gpuweb/gpuweb#1560.

@kvark
Copy link
Member

kvark commented Apr 11, 2022

This isn't a clear as we'd hope. See gpuweb/gpuweb#1558 and https://github.com/austinEng/webgpu-samples/issues/149, which notably has zero responses.
Finally, clarification from @Kangz confirms that we end up requiring 16-byte aligned uniform buffers in the end - gpuweb/gpuweb#425 (comment)

@cwfitzgerald cwfitzgerald added api: gles Issues with GLES or WebGL type: bug Something isn't working labels Jun 4, 2022
@cwfitzgerald cwfitzgerald added this to the Release 0.13 milestone Jun 6, 2022
@grovesNL
Copy link
Collaborator

grovesNL commented Jun 6, 2022

I think gpuweb/gpuweb#425 (comment) is deriving the alignment from the types of the fields though, so we wouldn't necessarily end up with 16-byte alignment for something like SizeOf(struct Block {float x;};), right?

Should we go ahead with having WebGL validation to enforce alignment to UNIFORM_BLOCK_DATA_SIZE (queried from the program)?

We probably need some way to surface this size too, because we don't know the size in advance (i.e. besides guessing 16-byte alignment/rounding up).

@cwfitzgerald
Copy link
Member

Uniform buffers require that all structs be rounded up to 16, so that would still be align 16. The only potential issue is if someone had a uniform buffer of a bare u32. This can be a downlevel feature forcing all assignments to 16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: gles Issues with GLES or WebGL area: validation Issues related to validation, diagnostics, and error handling type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants