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

Optimized gamma correction on Apple Silicon #1590

Merged

Conversation

colincornaby
Copy link
Contributor

This is a follow up to #1581. Previous pull request added an optional path for Metal 2.3 - this PR uses it.

In Metal - we have to manually do gamma correction. Traditionally - this has meant:

  • Rendering the main render pass into an intermediate buffer we create.
  • Ending the main render pass.
  • Creating a new render pass that binds the actual framebuffer as the render target, and the intermediate buffer as a source texture.
  • Executing a shader that passes through pixels from the intermediate buffer to the final buffer - doing the gamma correction.

Apple Silicon can read and write safely from a framebuffer - because each portion of the framebuffer is directly owned by a GPU core in its tile memory. So we don't need two passes, and we don't need an intermediate buffer. We render straight to the main framebuffer, and then execute a shader that reads each pixel value from the main buffer, and writes the corrected value back directly to the framebuffer. This improves performance (render pass changes on Apple Silicon can be expensive) and reduces memory use.

On macOS - Apple Silicon functionality requires shaders compiled against Metal 2.3. Metal 2.3 shaders also only run on macOS 11 - so we need to maintain Metal 2.1 shaders for earlier macOS versions. (macOS 11 is the first version to support Apple Silicon Macs, so we don't need to worry about an Apple Silicon Mac running Metal 2.1.)

Additionally - we need to do a check to make sure the GPU is an Apple GPU before attempting to load the optimized shader. This check has been surfaced to the device class as a tile memory capability.

Copy link
Member

@dpogue dpogue left a comment

Choose a reason for hiding this comment

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

LGTM

@Hoikas Hoikas merged commit 0116eff into H-uru:master Jul 2, 2024
17 checks passed
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.

None yet

3 participants