Metal renderer for JUCE on macOS

Update (Nov 7): a few things below have changed since this was posted. Windows are now presented through their own CAMetalLayer instead of a grid of CAMetalLayer tiles (the tiles made fullscreen crawl), JUCE_METAL_RENDERER_TILE_SIZE is gone, paint() now only records (path outlines and new glyphs are prepared on the background queue), and the branch was rewritten as a single commit on top of 9.0.5. Details and new numbers in this reply.

Hi all,

At UVI our plugin UIs have a lot of small things moving at the same time (meters, LEDs, waveforms, knobs), and on macOS that has always been more expensive than it should be with JUCE. So we’ve been working on a Metal rendering engine for JUCE on macOS, and we’d like to get it upstream.

Full disclosure on how it was made: the code was written with Claude Opus 5.5, with me driving it. I set the goals and constraints (match CoreGraphics, never be slower than it, stay a drop-in LowLevelGraphicsContext), pointed it at how other renderers solve each problem instead of inventing things, reviewed the design choices, and asked for measurements at every step. That’s also why there’s a separate repo with all the tools we used to check pixels and timings: we didn’t want to rely on anything we couldn’t measure.

It’s on a branch of our fork, one commit on top of 9.0.5:

The tools we used to test and measure it are here:

Why

On macOS today you have two options and neither is great for plugin UIs with lots of small things animating (meters, LEDs, knobs):

  • The default CoreGraphics path goes through a layer-backed NSView, and AppKit merges every dirty rect of the window into a single bounding box. Two meters in opposite corners repainting at 60 Hz means the whole window gets repainted at 60 Hz.
  • JUCE_COREGRAPHICS_RENDER_WITH_MULTIPLE_PAINT_CALLS fixes that (it keeps the dirty rects separate and draws into a CAMetalLayer), but it still rasterises everything with CoreGraphics on the CPU and then uploads and blits the whole window every frame, so it ends up slower.

The Metal renderer keeps the exact list of dirty rects like the second one, but does the drawing on the GPU.

How it works

  • It’s a LowLevelGraphicsContext used by NSViewComponentPeer, enabled with JUCE_METAL_RENDERER=1 (off by default). It shows up as a “Metal Renderer” rendering engine and is picked by default when a Metal device is there. You can switch engines at runtime like before (Software, CoreGraphics, CoreGraphics Metal Layer, Metal).
  • paint() runs on the message thread as usual and only records instances (one quad per shape/glyph/image). Encoding and submission happen on a background queue, up to three frames in flight.
  • The window keeps a retained texture. Only the dirty rects are repainted into it, in one traversal of the component tree.
  • The window is presented as a grid of CAMetalLayer tiles (128 pt by default, JUCE_METAL_RENDERER_TILE_SIZE) and a frame only presents the tiles it touched, because WindowServer’s cost follows the area that changes. Now: the window is presented through its own CAMetalLayer, the retained texture being copied into a drawable each frame.
  • Rects, rounded rects, ellipses and lines are drawn analytically with exact pixel-area anti-aliasing. Strokes only shade the band around the outline, not the inside.
  • Arbitrary paths (fills, strokes, path clips) use coverage counting: edges are accumulated into a float texture and summed per row, which gives exact area coverage for both winding rules.
  • Text: glyphs are rasterised once through JUCE’s own CoreGraphics context into an atlas (with LRU eviction) and placed the way CoreGraphics places them, so text looks identical to the CoreGraphics renderer. Large text goes through the glyph outlines and coverage counting.
  • Linear and radial gradients are interpolated per pixel like CGGradient. Tiled image fills, image clips, clipToPath, transparency layers and the four blend modes are all done on the GPU.
  • Opaque shapes are drawn first with depth, so the pixels they hide are never shaded, and draws are reordered into a few batches when they don’t overlap.
  • Images modified every frame (scopes, meters drawn into an Image) reuse their textures instead of allocating new ones.

What still goes through JUCE’s software renderer (drawn into a small image and composited):

  • colour glyphs (emoji, COLR fonts)
  • image fills using a single-channel image, or with a degenerate transform
  • images bigger than 16384 px, or images whose texture couldn’t be created

In the GraphicsDemo and our own UIs none of those come up.

Matching CoreGraphics

We matched the pixel output against CoreGraphics rather than the software renderer, since that’s what macOS users see today. On every GraphicsDemo test (all clip modes) plus extra scenes (text at fractional scales, thin strokes, blend modes, gradients, transformed shapes and images), most tests differ from CoreGraphics on less than 0.1% of the pixels. Where CoreGraphics isn’t exact itself (curve flattening, rotated edges), we stay within 1/255 of the exact pixel area instead of copying its error.

Numbers (M1 MacBook Pro, 60 Hz)

How we measured: DemoRunner built in Release from the branch, GraphicsDemo with its default settings (position and rotation animated, no clipping), the engine switched at runtime from DemoRunner’s settings. For each engine and each of the 14 tests: 2 s to settle, then a 6 s window. CPU is the process CPU time (all threads) divided by the frames drawn; GPU is the union of each process’s GPU intervals in an Instruments trace (Metal Application + GPU), divided the same way. Frame rate is the demo’s own “Actual FPS”. The scripts that do all this are in the bench repo.

Per test, app CPU / app GPU in ms per frame (60 fps unless noted):

Test CoreGraphics Metal OpenGL Software CG Metal Layer
Paths: Radial Gradients 2.3 / 0.33 2.1 / 0.87 3.3 / 1.44 6.2 / 0.38 6.4 / 0.46
Paths: Linear Gradients 2.6 / 0.34 3.3 / 0.96 3.4 / 1.42 6.3 / 0.40 6.2 / 0.46
Paths: Solid 3.1 / 0.31 3.3 / 0.92 3.4 / 1.44 5.6 / 0.40 5.3 / 0.57
Fill Types: Rectangles 3.0 / 0.25 2.9 / 0.86 3.0 / 1.41 6.3 / 0.38 6.2 / 0.51
Paths: Stroked 2.7 / 0.22 3.1 / 0.85 3.7 / 1.49 5.8 / 0.39 5.2 / 0.56
Images: RGB 2.8 / 0.24 2.9 / 0.90 2.7 / 1.41 6.9 / 0.32 9.3 / 0.49
Images: RGB Tiled 14.4 / 0.60 3.2 / 1.07 2.7 / 1.42 9.6 / 0.35 13.6 / 0.46
Images: ARGB 2.6 / 0.24 3.2 / 0.78 2.7 / 1.40 6.8 / 0.33 8.8 / 0.48
Images: ARGB Tiled 14.0 / 0.54 3.1 / 1.03 2.7 / 1.42 9.0 / 0.33 13.7 / 0.44
Blur 6.2 / 0.44 5.3 / 0.85 5.4 / 1.60 9.5 / 0.49 11.9 / 0.59
Glyphs 3.3 / 0.21 3.1 / 0.53 2.9 / 1.37 5.1 / 0.39 4.0 / 0.50
SVG 4.2 / 1.33 3.8 / 1.08 9.1 / 8.34 25.8 / 0.29 (30 fps) 43.8 / 0.44 (19 fps)
Lines 3.8 / 0.97 3.1 / 1.07 4.3 / 2.03 6.9 / 0.35 6.4 / 0.46
Shapes 3.1 / 0.26 2.5 / 0.73 3.2 / 1.42 6.0 / 0.38 5.9 / 0.57

Things worth pointing out:

  • Metal and stock CoreGraphics are level on CPU. On GPU, Metal is ~0.6 ms per frame above on these light scenes, about the same on every test, which points to a fixed cost per frame (most likely presenting the tiles and the retained texture) rather than the drawing itself. That’s the next thing we want to bring down.
  • Metal is the flattest: its worst test is 6.2 ms (Blur, where the demo itself computes the blur on the CPU). CoreGraphics jumps to 14-15 ms on tiled images, OpenGL to 17.5 ms on SVG, and the two CPU-based engines can’t hold 60 fps on SVG.
  • Against JUCE’s OpenGL renderer, Metal is lower on every column: app GPU -37%, WindowServer CPU -25%, WindowServer GPU -40%.
  • Where Metal’s own time goes (its JUCE_METAL_RENDERER_STATS output on these tests): ~0.4 ms of paint() recording on the message thread, ~0.3 ms of encoding on a background thread, ~0.9 ms of GPU of which 0.2 ms is the copy into the tiles and 0.3-0.4 ms the path coverage on the path tests.

A window with 1,200 widgets (rounded rect, outline and a label each), closer to a real plugin UI. Labels are cached (see the side note below), except for the CoreGraphics frame rate with 2 areas, which was measured without the cache:

Load Engine fps Paint CPU+GPU power
2 areas repainting CoreGraphics ~30 ~18 ms per repaint (whole window) ~1.3 W
2 areas repainting Metal 60 < 1 ms (the 2 areas) ~0.2-0.3 W
8 areas repainting CoreGraphics ~1.35 W
8 areas repainting Metal 60 ~0.25 W
Whole window CoreGraphics 60 5.1 ms ~2.1 W
Whole window Metal 60 2.9 ms ~1.45 W

Power is the whole machine (CPU + GPU, powermetrics), so it includes WindowServer; idle is ~0.15 W. With 2 or 8 small areas, CoreGraphics repaints the merged bounding box, i.e. the whole window, every time.

Side note we ran into while measuring: with more than 128 different labels on screen, Graphics::drawText’s arrangement cache misses on every frame and text layout becomes ~90% of the message thread, whatever the renderer. Without caching the labels, CoreGraphics drops to ~34 fps on whole-window repaints and Metal spends 12 ms per frame in paint(), almost all of it in drawText.

Testing

  • The bench repo renders the same scenes through each engine, captures the window as WindowServer shows it, and compares the captures pixel by pixel (CoreGraphics, Software and Metal).
  • There are self-checking scenes for partial repaints (stale content shows up immediately) and for images rewritten every frame.
  • Everything above also runs clean with the Metal validation layer, including live resize.

Status

  • Off unless JUCE_METAL_RENDERER is set, so it doesn’t change anything for existing projects.
  • On light scenes there’s still a fixed GPU cost of about 0.5 ms per frame above stock CoreGraphics. That’s what we want to look at next.
  • The bench tools aren’t specific to Metal: they compare any engine against CoreGraphics, the software renderer or the exact pixel area, and report CPU/GPU time per frame. They could be useful for Direct2D too.

JUCE team, we’re happy to reshape this however suits you (names, file layout, options, splitting it into smaller pieces) and to keep working on it with you.

4 Likes

This is really interesting and I’m sure we’ll take a closer look at some point. However, we’re all at ADC for most of next week (if you’re there please come and say hi), and we have quite a few other things on our plate.

I quickly checked out the repo, unfortuately I’m seeing an issue in which it gets into a crawling state, the CPU time per frame remians low but it drops to probably less than 1fps. The easiest way I’ve found to get into the state is just go into full screen and switch to the ComponentDiagnosticDemo. If I don’t go into full screen it’s fine most of the time but does still occasionaly happen, I have to switch renderers to get out of it.

While it was working I took a look at the examples in that demo. Most of them seemed to run faster on the CoreGraphics renderer for me, quite signifcantly in a fair number of cases. A few were a little faster on the metal renderer for example Cheap, Grid, and some of the Image ones. Some examples that stood out to me that were slower:

  • Complex path (either the cached or not-cached versions)
  • Graphics Demo (Shapes)
  • Graphics Demo (Lines)
  • Font (animated transform)
  • Widgets Demo

Thanks for trying it! I won’t be at ADC this year, but have a good one.

Both points were real, and the branch is updated. Heads-up: I rewrote its history so that everything is one commit on top of 9.0.5 again (the fixes are folded into it, and it was force-pushed). If you already have it checked out, git fetch then git reset --hard <remote>/metal-renderer rather than pulling.

The crawl

Reproduced here. The window was presented as a grid of CAMetalLayer tiles, so that a frame only presented the tiles it touched. Once a frame presents a few dozen of them, WindowServer starts dropping the presents and nextDrawable waits for its one-second timeout. In fullscreen that’s every frame (126 tiles here), and windowed it happened now and then from about 35 tiles, which matches what you saw.

It now presents through the window’s own CAMetalLayer, copying the frame into one drawable, which is what other toolkits drawing with Metal do. Fullscreen ComponentDiagnosticsDemo holds 60 fps here, and windowed too. Presenting the whole window costs a bit more on small repaints than the tiles did, but it still comes out below CoreGraphics. On a window with two small areas repainting at 60 Hz, app + WindowServer CPU and GPU time goes from 891 ms/s with CoreGraphics to 715 ms/s, and on full-window repaints from 1495 to 957.

The slower examples

ComponentDiagnosticsDemo times paint() on the message thread. With the CoreGraphics renderer the layer draws asynchronously, so paint() only records the CoreGraphics calls and the actual rasterisation happens later on a Core Animation thread. That’s why CoreGraphics shows such small numbers there. Still, you were right that we were doing too much in paint(): stroking and flattening paths, and rasterising every new glyph. That work now happens on the background queue that already encodes the frame, so paint() only records, same split as Core Animation. The pixels don’t change (text is bit-identical).

The demo’s paint time in ms, M1 MacBook Pro, macOS 13.7, 60 Hz:

Example CoreGraphics Metal before Metal now
Complex path (not-cached) 0.038 0.075 0.008
Complex path (cached) 0.046 0.116 0.003
Complex path (with rounded corners) 0.119 1.188 0.035
Graphics demo (Shapes) 0.089 0.072 0.013
Graphics demo (Lines) 0.060 0.052 0.018
Font (animated transform) 0.024 0.902 0.029
Widgets demo (*) 0.99 0.77 0.61

(*) Widgets moves a lot from one reading to the next (its numbers depend on what is animating at that moment), so that row is the median of several runs alternating the engines; the others come from one pass over the 54 examples.

Over all 54 examples, the median paint time is 0.051 ms with CoreGraphics, 0.100 before and 0.027 now. Since the demo’s number hides CoreGraphics’ real cost, we also measured whole-process time over the same run, averaged over the 54 examples, in ms per second:

CoreGraphics Metal now
App CPU 317 270
App GPU 55 65
WindowServer CPU 272 277
WindowServer GPU 34 35

The extra GPU time is mostly the copy of the frame into the drawable.

If you still see the crawl or something slower on your side, could you tell me which machine and macOS version? The scripts that produced these numbers are in GitHub - UVI-Dev/juce-renderer-bench: Rendering engine benchmarks and pixel comparison for JUCE on macOS · GitHub.

2 Likes

@otristan I love the idea of the Metal renderer but I would also appreciate it if we completely avoid AI written messages on the forum. It’s not pleasant to read (at least for me).

Even if the agents do the work I would really prefer we keep interaction human.

3 Likes

to be fair, it’s against the community guidelines.

1 Like

This post is a actually a mix. I use AI to correct my poor english as well.
The rationale was to give all the required info to the JUCE team but I can understand your point.

@kunitoki Sorry to be rude, but I suppose promoting a clone of Juce on the juce forum itself is probably not the best idea either so I think we are even :slight_smile: Still Yup showed me it was possible to get good performance on Mac as well, so thanks for that.

Sorry, but what you posted wasn’t an AI that fixed grammar issues. It was a full on agent output that was clearly copy pasted into a forum message including all the classic Claude/Codex conversational elements that look like an answer to a prompt.

1 Like

The intro is actually mine, and I never said that it was only used to fix grammar here. I said I use it in that case as well. was not refering to this post.

I think the problem is that - aside from community guidelines - AI generated output can be extremely dense and difficult to parse quickly.

And I think people resent having to read through goopy text especially if they know it’s not written by another human.

We get into the situation where I have to copy your post and ask my AI to briefly summarize it…which could have been done in the first place?

Fair enough.
The rationale is that this post was merely aimed at the Juce team with explanation of why and what. Somehting AI does very well and in a way faster/cleaner manner that I would take.

The summary for a traditional Juce user was that I was pissed that Juce was having issue on OSX with classic plugin UI with meter and stuff.
Seeing that it was possible to be done as done by Yup and that the Juce team was relunctant to do it at first glance, I tried to do it myself with promising results.
Give it a shot with the Juce demo see if you have better performance and don’t see any downside on your app than with the default CoreGraphics backend and voila !

I for one don’t want to see random AI post either in that forum especially promotonial ones, but a total ban for bug report or stuff like that might be counter productive IMO.

my 2 cents

The way to do this is attach agent output in a text file.
We do it all the time at work since people know your agent output can be fed to another agent and it’s useful in that way.

Your work on the Metal renderer is important!

3 Likes

Performance issues with JUCE UI these days are almost always user error IME.

There are hundreds of JUCE plugins out there with meters and almost all perferm perfectly well on macOS. Being “pissed” at JUCE isn’t going to give your feature request any more traction.

I’m seeing zero difference between the CoreGraphics renderer and your Metal renderer in the component diagnostics demo…

1 Like

Use the current CG renderer. animate couple of meters in different area of the screen and the whole UI gets repainted. Simple as that. Not a user error here.
Maybe you don’t need this for your plugins but others do.

I clearly had the issue with this product

and has to rely on JUCE_COREGRAPHICS_RENDER_WITH_MULTIPLE_PAINT_CALLS but this degrades a lot the whole performance.

Maybe you didn’t see it but there were other report.

So you’re calling setBufferedToImage() on all the stuff in the middle between the meters that doesn’t change frequently?

If there are couple of component that will do the trick, but this is way slower if not.
Been there, done that…

This is a vibe-coded demo with animated strips on the sides and a bunch of expensive components in the middle that don’t change.

Note the “Buffer centre components to image” toggle

This is with the CoreGraphics renderer.

BufferedToImageDemo.h (8.7 KB)

The Metal renderer is quicker, <0.1ms, but that level of performance isn’t really needed unless you’re doing really complex UIs with almost the entire window animated.

Great example. I will investigate this. Thanks !

11ms to paint a bunch of rectangles and lines? jeez you must be kidding me

Look at Retrace running 2568x1862 HiDPI window at 120fps (true v-sync) in around 3ms :smiley:

(well youtube can just do 60fps but anyway)

i was threatened to be banned if i continued breaking the forum rules. AI posts are against it too so i’m just being a good citizen.