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

Metal optimizations #2062

Merged
merged 10 commits into from
Aug 9, 2024
Merged

Conversation

qwerasd205
Copy link
Collaborator

This PR significantly reworks the Metal shaders for memory efficiency. Additionally I've adjusted the formatting of the shader code for readability, and normalized the naming conventions.

There are two main changes that affect memory efficiency.

  1. Background cells are now a static sized buffer of cols * rows total uchar4s representing the cell colors, which are drawn with a single fullscreen shader.
  2. I've taken steps to optimize the foreground (text) cell vertex data, namely:
    • Background color (4 bytes) has been removed, since it can be fetched from the bg color buffer based on grid position.
    • I demoted the glyph bearings from [2]i32 to [2]i16, it seems like a sane expectation to me that the bearings will never be more than 32K pixels in a given direction.
    • I have reorganized the structs to be as compact as possible in memory, so that the size (with padding) is now 32 bytes, as opposed to the prior size which was something like 56 bytes.

Beyond this, I have added a couple other small optimizations, such as a fullscreen vertex shader that uses a single triangle, and using pixel coordinates to sample the glyph texture so that conversion to normalized coordinates isn't necessary.

Results

Experimentally I saw a ~20% reduction in total GPU memory use, and a ~80% reduction in max allocated buffer size.

TODO

Caution

There is a very real chance that this PR could have issues under macOS 13 for Intel processors, due to the known bugginess of the macOS 13 Intel Metal drivers. Comprehensive testing on an Intel mac running macOS 13 must be performed before this can be merged.

  • Verify correct behavior on an Intel mac under macOS 13. DO NOT MERGE WITHOUT TESTING

- Significant changes to optimize memory usage.
- Adjusted formatting of the metal shader code to improve readability.
- Normalized naming conventions in shader code.
- Abstracted repetitive code for attribute descriptors to a helper
function.
@clason
Copy link
Collaborator

clason commented Aug 8, 2024

@mitchellh I'm happy to test the builds on my Intel macOS 13 potato, but that machine is not set up for Zig builds. Could you enable the build workflows for this PR so I can grab the artefacts for testing?

(I'm also open for suggestions what to test specifically to exercise all the codepaths.)

@mitchellh
Copy link
Contributor

Apparently I can't build the release from 3rd party forks :( Let me see what I can do.

Copy link
Contributor

@mitchellh mitchellh left a comment

Choose a reason for hiding this comment

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

Looks amazing, some really tiny comments. I'll try to figure out the Intel builds.

src/renderer/metal/cell.zig Show resolved Hide resolved
src/renderer/metal/cell.zig Outdated Show resolved Hide resolved
src/renderer/metal/cell.zig Outdated Show resolved Hide resolved
@@ -845,6 +678,41 @@ fn initImagePipeline(device: objc.Object, library: objc.Object) !objc.Object {
return pipeline_state;
}

fn autoAttribute(T: type, attrs: objc.Object) void {
Copy link
Contributor

Choose a reason for hiding this comment

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

Well well well aren't we fancy. ❤️

@mitchellh
Copy link
Contributor

mitchellh commented Aug 8, 2024

CleanShot 2024-08-08 at 14 48 17@2x

asciinema: bug.cast.zip

font-size = 15
font-family = JetBrains Mono
background-opacity = 0.95
background-blur-radius = 20
macos-titlebar-style = tabs
mouse-hide-while-typing = true
window-save-state = never

@mitchellh mitchellh merged commit 33d9c04 into ghostty-org:main Aug 9, 2024
17 of 19 checks passed
@mitchellh mitchellh deleted the metal-optimizations branch August 9, 2024 01:56
@clason
Copy link
Collaborator

clason commented Aug 9, 2024

That's one way, I guess :) Happy to report this build looks fine and even fixes the regression with the earlier build I reported on Discord.

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

Successfully merging this pull request may close these issues.

3 participants