H-uru / Plasma

Cyan Worlds's Plasma game engine
http://h-uru.github.io/Plasma/
GNU General Public License v3.0
202 stars 80 forks source link

Optimized gamma correction on Apple Silicon #1590

Closed colincornaby closed 5 days ago

colincornaby commented 1 month ago

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:

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.