-
Notifications
You must be signed in to change notification settings - Fork 967
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
hal/gl: Allow push constants trough emulation #2400
Conversation
fde08fd
to
41e18c1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit unexpected. What I thought we would be doing is - using freestanding uniforms for push constants. That's what gfx-hal did, IIRC. But instead this PR tries to do this via a uniform buffer.
The current approach isn't going to work from the point of view of synchronization between GPU and CPU. Within the same submission, multiple different draw calls would want to overwrite the mapped buffer contents, bind it, and use it on GPU. This means 100% data race between CPU and GPU.
Should have probably checked if
Could you elaborate from what I read from the OpenGL wiki about synchronization, https://www.khronos.org/opengl/wiki/Synchronization#Implicit_synchronization
So unless I'm reading wrong (which is definitely possible), writing to a mapped buffer is implicitly synchronized, even between draw calls |
First of all, this "will halt" is a terrible thing, and we should not expose a feature that results in that happening potentially many times per frame. |
Okay, then I'll go with the Since |
Yes. It's not far from what this PR is doing already. |
@kvark I've hit a problem, to use the |
I've tried fiddling around with the shaders and glsl only allows to use directly the struct without the wrapping block if it also drops the layout qualifier, which isn't that great since then we get implementation defined layouts. |
Right, naga should not generate them in a uniform block. |
Previously this was done with UBOs but this posed some problems when integrating with wgpu (see gfx-rs/wgpu#2400)
41e18c1
to
704e6fb
Compare
@kvark I've update it to use freestanding uniforms, it's basically a copy of what gfx-hal does. |
704e6fb
to
3795042
Compare
3795042
to
bee4898
Compare
@kvark I've updated the PR with your recommendations, I think the annoying |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall, thank you for doing this!
Just a few smaller corrections are left to do
ec30a7b
to
6c3b456
Compare
@kvark I've finished applying your suggestions, only one problem remains, |
@JCapucho let's |
Uses freestanding uniforms for push constants
6c3b456
to
a9bc25e
Compare
@kvark everything seems to be working now, deno is failing because it's not picking up the patch for naga but once everything is merged upstream it should be fixed. Are we good to go? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good!
So the last thing here is to change the naga dependency back to upstream.
Weird, deno stuff fails but the rest is fine? https://github.com/gfx-rs/wgpu/runs/4900108750?check_suite_focus=true#step:4:255 |
Previously this was done with UBOs but this posed some problems when integrating with wgpu (see gfx-rs/wgpu#2400)
@kvark I've updated to latest naga, it includes quite some shader changes beacuse of the attribute changes so I've made it into another commit but it can be all squashed |
ca0ef31
to
f0121d2
Compare
@kvark seems to me like it was just ci being flaky due to caching |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for updating the shaders to the new syntax!
Connections
Depends on gfx-rs/naga#1672 so bumps
naga
revision to pick it upDescription
Push constants are a great way to pass small amounts of data to the gpu between draw calls.
But maintaining two code paths for backends that support push constants and those who don't can be prohibitive to picking them.
So a good middle ground is to emulate push constants in the secondary backends, this is done by using a uniform buffer for push constants.
Implementation notes:
The buffer is always kept mapped and when a
set_push_constants
call is issued the mapping is written to.Push constants stages are ignored.
The uniform buffer uses the binding 0 in the shader.
TODOS:
glMapBufferRange
as a*mut u32
for the slice, but according to https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glMapBufferRange.xhtml alignment is not guaranteed so it might need to be changed to a*mut u8
andset_push_constants
will need to convert the slice fromu32
tou8
glMapBufferRange
doesn't give much information but has anGL_MAP_UNSYNCHRONIZED_BIT
so it leads me to believe that synchronization is implicit, but if someone who knows opengl better could confirm it would be helpfulTesting
I tested on a modified cube example
As well as a modified branch of https://gitlab.com/veloren/veloren
Note: I don't have much experience with openGL so the code might not be the best.