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

UI texture atlas slice shader #14990

Merged
merged 45 commits into from
Sep 2, 2024

Conversation

ickshonpe
Copy link
Contributor

@ickshonpe ickshonpe commented Aug 31, 2024

Objective

Fixes #14183

Solution

Reimplement the UI texture atlas slicer using a shader.

The problems with #14183 could be fixed more simply by hacking around with the coordinates and scaling but that way is very fragile and might get broken again the next time we make changes to the layout calculations. A shader based solution is more robust, it's impossible for gaps to appear between the image slices with these changes as we're only drawing a single quad.

I've not tried any benchmarks yet but it should much more efficient as well, in the worst cases even hundreds or thousands of times faster.

Maybe could have used the UiMaterialPipeline. I wrote the shader first and used fat vertices and then realised it wouldn't work that way with a UiMaterial. If it's rewritten it so it puts all the slice geometry in uniform buffer, then it might work? Adding the uniform buffer would probably make the shader more complicated though, so don't know if it's even worth it. Instancing is another alternative.

Testing

The examples are working and it seems to match the old API correctly but I've not used the texture atlas slicing API for anything before, I reviewed the PR but that was back in January.

Needs a review by someone who knows the rendering pipeline and wgsl really well because I don't really have any idea what I'm doing.

@alice-i-cecile alice-i-cecile added the S-Needs-Review Needs reviewer attention (from anyone!) to move forward label Aug 31, 2024
Copy link
Member

@mockersf mockersf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't find issues reading the code, and it works, but it wasn't a review in depth.

Would the same change be needed for slices on spites?

@ickshonpe
Copy link
Contributor Author

ickshonpe commented Sep 2, 2024

Couldn't find issues reading the code, and it works, but it wasn't a review in depth.

Would the same change be needed for slices on spites?

Yes, the UI fix seemed much more important though. Once this is merged it should be relatively simple to apply the same changes to the sprite renderer.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a shader for this is reasonable, and I'm happy with the code quality.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 2, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 2, 2024
Merged via the queue into bevyengine:main with commit 01a3b0e Sep 2, 2024
27 checks passed
Comment on lines -41 to -50
let mut flip = Vec2::new(1.0, -1.0);
let [mut flip_x, mut flip_y] = [false; 2];
if image.flip_x {
flip.x *= -1.0;
flip_x = true;
}
if image.flip_y {
flip.y *= -1.0;
flip_y = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ickshonpe thanks for the PR, using a shader is much better than CPU bound logic.
I don't see an equivalent to this flip image logic anywhere in the new code, so it might be missing from the migration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi felix, yes you're right, I took image flipping out temporarily to make things easier and meant to add it back in at the end but completely forgot about it 😓. I think it must be really rare to combine nine slicing with flipping though so it's probably not too critical and shouldn't be difficult to add support in a followup pr.

@alice-i-cecile alice-i-cecile modified the milestone: 0.14.2 Sep 3, 2024
mockersf pushed a commit that referenced this pull request Sep 5, 2024
Fixes #14183

Reimplement the UI texture atlas slicer using a shader.

The problems with #14183 could be fixed more simply by hacking around
with the coordinates and scaling but that way is very fragile and might
get broken again the next time we make changes to the layout
calculations. A shader based solution is more robust, it's impossible
for gaps to appear between the image slices with these changes as we're
only drawing a single quad.

I've not tried any benchmarks yet but it should much more efficient as
well, in the worst cases even hundreds or thousands of times faster.

Maybe could have used the UiMaterialPipeline. I wrote the shader first
and used fat vertices and then realised it wouldn't work that way with a
UiMaterial. If it's rewritten it so it puts all the slice geometry in
uniform buffer, then it might work? Adding the uniform buffer would
probably make the shader more complicated though, so don't know if it's
even worth it. Instancing is another alternative.

The examples are working and it seems to match the old API correctly but
I've not used the texture atlas slicing API for anything before, I
reviewed the PR but that was back in January.

Needs a review by someone who knows the rendering pipeline and wgsl
really well because I don't really have any idea what I'm doing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior C-Performance A change motivated by improving speed, memory usage or compile times S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Thin line in 9-sliced texture when sprite resolution is high
4 participants