No description
Find a file
Andros Fenollosa b766212695 Metal: coalesce presents and composite in one command buffer
A redisplay pass runs several update cycles back-to-back (buffer window
plus echo area), and with display sync every present blocked on a
drawable: two blocking presents per keystroke halved typing throughput,
and the present itself paid for a second command buffer.

Presents now coalesce: one landing within ~8 ms of the previous commits
to the static texture and is flushed by a one-shot main-queue block
(or absorbed by the next cycle's present), and the composite pass is
encoded on the same command buffer as the cycle's draws.  The policy's
flush presents through the same coalescing path.

Benchmarks (M1 Pro, font-locked xdisp.c, 120x45): machine-paced typing
goes from 30 to 106 chars/s and sustained scroll from 116 to 475
redisplays/s with vsync off, matching the stock backend's throughput
and total CPU; with vsync on (default) the same visible 60 fps now
costs ~35% less CPU than stock.  README gains the numbers.

Pixel parity, GIF/video playback, the first-tab-switch deferral and the
echo-area tests are all unchanged.
2026-06-04 17:55:53 +02:00
.github/assets README: re-record the sonicboom cursor demo 2026-06-04 11:22:35 +02:00
admin Merge from origin/emacs-31 2026-05-29 11:01:25 +01:00
build-aux Update from Gnulib by running admin/merge-gnulib 2026-05-26 21:57:20 -07:00
cross ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
doc Disable markdown-ts-mode & markdown-ts-view-mode for Emacs 31 2026-06-01 19:11:46 +01:00
etc Support Ansible messages in compilation-mode 2026-06-02 12:43:08 +02:00
exec Fix the Android build again 2026-05-28 11:31:16 +08:00
java Bump master Emacs version to 32.0.50 2026-05-07 17:46:43 +01:00
leim ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
lib Also copy lib/mini-gmp-gnulib.c from Gnulib 2026-05-26 22:21:08 -07:00
lib-src Ignore SO_RCVTIMEO errors in emacsclient 2026-05-31 12:48:42 -07:00
lisp Metal: keep cursor animations alive while Emacs idles 2026-06-04 11:05:21 +02:00
lwlib ARRAYELTS → countof 2026-05-26 21:57:20 -07:00
m4 ; * m4/texinfo.m4 (gl_SET_MAKEINFO): Fix introductory comment. 2026-05-31 14:22:33 +02:00
modules ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
msdos Fix the MSDOS build 2026-05-28 12:59:55 +08:00
nextstep ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
nt Bump master Emacs version to 32.0.50 2026-05-07 17:46:43 +01:00
oldXMenu ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
src Metal: coalesce presents and composite in one command buffer 2026-06-04 17:55:53 +02:00
test Support Ansible messages in compilation-mode 2026-06-02 12:43:08 +02:00
.clang-format ; Configure ObjC for clang-format 2023-10-06 16:52:19 +02:00
.clangd Tweak .clangd to work with gcc-compiled Emacs (bug#78367) 2025-05-11 11:14:32 +01:00
.dir-locals.el Review some attribute macro usage 2026-03-14 15:08:15 +01:00
.gitattributes Merge branch 'main' into icalendar 2026-02-08 16:05:31 -05:00
.gitignore README: add GPU demos (inline video, animated GIF, cursor effects) 2026-06-04 09:50:51 +02:00
.gitlab-ci.yml ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
.mailmap ; * .mailmap: Add email alias 2026-01-18 21:59:38 +01:00
autogen.sh ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
BUGS * BUGS: Minor copy edit. 2024-09-21 13:29:36 +02:00
ChangeLog.1 ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
ChangeLog.2 ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
ChangeLog.3 ; Update exported ChangeLog files and etc/AUTHORS 2026-05-07 20:08:43 +01:00
ChangeLog.4 ; Spelling fixes. 2026-04-29 22:45:51 -07:00
ChangeLog.5 ; * ChangeLog.5: Some fixes and tidying up. 2026-05-07 20:14:12 +01:00
ChangeLog.android ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
config.bat Fix the MSDOS build 2026-05-28 12:59:55 +08:00
configure.ac Fase I: split the GPU backend into neutral policy + driver vtable 2026-06-04 08:54:03 +02:00
CONTRIBUTE ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
COPYING
GNUmakefile ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
INSTALL ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
INSTALL.REPO ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
make-dist ; Add 2026 to copyright years. 2026-01-01 12:54:34 +00:00
Makefile.in Restore runtime dependencies to documentation files 2026-06-01 12:15:04 +08:00
README Bump master Emacs version to 32.0.50 2026-05-07 17:46:43 +01:00
README.md Metal: coalesce presents and composite in one command buffer 2026-06-04 17:55:53 +02:00

emacs-gpu

GNU Emacs with a GPU-accelerated display backend.

On macOS it renders with native Apple Metal: text goes through a GPU glyph atlas, images and inline video are textures, and the whole frame is composited by the GPU instead of CoreGraphics. The output is pixel-accurate against the stock Cocoa backend.

OpenGL support for GNU/Linux and Windows is planned but not implemented yet. The drawing logic is already platform-neutral (src/gfxterm.c) behind a small driver interface (src/gfxdrv.h); an OpenGL driver only needs to implement that interface (src/glterm.c is the documented skeleton). Contributions welcome.

Why a GPU backend?

Beyond raw rendering, it enables things the stock backend cannot do:

  • Inline video playback: AVFoundation decodes straight into Metal textures (zero copies) and the frames are composited inside the buffer, following scrolling and clipped to the window. No xwidgets, no embedded browser.
  • GPU cursor effects (opt-in): expanding rings, comet trails and friends are drawn as a compositor overlay, without ever touching the buffer content underneath.
  • A path to cheap visual effects: buffer transitions, smooth scrolling or any future eye candy is one more shader pass over the composited frame, not a rewrite of the display engine.

Text is rasterized once into a GPU glyph atlas and drawn as textured quads; scrolling moves already-rendered pixels with a texture blit.

Performance

Measured on an Apple M1 Pro (Emacs 32 development build, 120x45 frame, font-locked xdisp.c, same binary with and without the GPU backend; /usr/bin/time -l over scripted workloads):

Workload Stock (Cocoa) GPU, vsync on (default) GPU, vsync off
Sustained scroll, redisplays/s 481 324 475
CPU for 15 s of that scroll 16.0 s 10.5 s 15.5 s
Typing throughput (chars/s, machine-paced) 108 52 106
Idle (8 s) CPU 1.21 s 1.19 s same
Peak RSS ~140 MB ~144 MB same

Honest reading:

  • Machine-paced throughput and CPU cost match the stock backend (vsync off). There is no GPU tax.
  • With vsync on (the default), presents wait for the display refresh: the screen shows the same 60 fps either way, but Emacs burns ~35% less CPU under flat-out scrolling because it stops rendering frames nobody can see. Human-paced input is unaffected (the cap is ~52 machine-paced updates/s; keyboard auto-repeat tops out well below that). (mtl-vsync nil) switches to uncapped, stock-like behavior.
  • Idle cost is identical and the GPU resources add ~4 MB of RSS.

Status: experimental, under active development.

Note: I am not answering issues for now. Feel free to open them as a public record (they will be read eventually), but do not expect a reply at this stage.

Demos

Inline video playing inside a buffer, decoded by AVFoundation straight into Metal textures (mtl-video-insert):

Inline video

An animated GIF playing next to font-locked code scrolling, all composited by the GPU:

Animated GIF and code

GPU cursor effects (mtl-animations), here the sonicboom mode:

Sonicboom cursor

Building on macOS

Requires Xcode (or the Command Line Tools) and the usual Emacs build dependencies (brew install autoconf automake gnutls texinfo pkg-config).

./autogen.sh

SDK=$(xcrun --sdk macosx --show-sdk-path)
CC="xcrun clang" OBJC="xcrun clang" \
CFLAGS="-isysroot $SDK" CPPFLAGS="-isysroot $SDK" OBJCFLAGS="-isysroot $SDK" \
./configure --with-ns --with-mtl

make -j$(sysctl -n hw.ncpu)

The binary is src/emacs (or install the app bundle from nextstep/).

Enabling the GPU backend

Emacs starts with the regular Cocoa backend; switch a frame to Metal with:

(add-to-list 'load-path "/path/to/emacs-gpu/lisp")
(require 'mtl)
(mtl-enable)

Extras once enabled:

(mtl-video-insert "video.mp4" 480 270 t)  ; inline video, follows scrolling
(mtl-animations t)                        ; GPU cursor effects (experimental)
(mtl-draw-stats)                          ; renderer counters

How it works

redisplay engine (xdisp.c, untouched)
        ↓
src/gfxterm.c   platform-neutral drawing policy
        ↓
src/gfxdrv.h    driver interface (~25 ops)
        ↓
src/mtlterm.m   Metal driver: glyph atlas (CoreText → R8 texture),
                render cycle on a persistent texture, AVFoundation
                video through CVMetalTextureCache

License

GNU General Public License v3 or later, same as GNU Emacs.