DEV Community

Tiberiu Petre
Tiberiu Petre

Posted on Originally published at petretiberiu.dev on

Gram Engine - Interlude 2.5: Optimizing for Real Hardware

Drafted with AI assistance from my own notes and code, then edited and fact-checked by me.

Episode 2 ends with the book’s cover scene running at about 8 frames per second (FPS) on my M4 Pro. Before the next episode moved from spheres to triangles, I wanted to know where the time went and make the engine usable beyond my development Mac.

This interlude has two halves. First, I measured and optimized the ray tracer on the M4 Pro. Then I took the engine to a Legion Go S and a Windows device. The Legion Go S was a real target, not the trigger: the BVH was already part of the path toward triangles, and the slow Mac frame was what made me start measuring.

A tree over the spheres

The scene had 486 spheres. Without an acceleration structure, every ray checked every sphere at every bounce. A bounding volume hierarchy, or BVH, puts boxes around groups of spheres. If a ray misses a box, it can skip that whole group.

I had expected to split boxes. The idea that made the builder click was simpler: split the list of spheres, then put a box around each new group. I wrote the builder and the validator’s first counting step, working through the algorithm with guided snippets. Claude added the remaining checks, the build timing and the comparison with the engine’s own sphere order. Thinking of the validator as the split in reverse made it easier to follow: count what went into each branch, then check that its children still account for the same scene.

On the GPU, I replaced recursion with a fixed-size list of boxes to visit—a traversal stack. The first version ran slower and lost spheres. My min/max mistakes made almost every box look hit. Claude’s sphere-order bug left the uploaded data in a different order from the one used to build the tree. Once those were fixed, the BVH and the plain loop produced the same image.

The ground sphere was so large that its box made the other boxes less useful, so large spheres were tested separately, before walking the tree. With the tree in place, the frame went from 190 ms to 77 ms: 5.3 to 12.9 FPS at 2× viewport resolution and 10 bounces.

I wrote the first heat-map shader and its box and sphere counters. Claude added the checkbox. The next day, Claude refactored the view for captures, adding thresholds and a Whole path mode. The view made the improvement visible: without the BVH, even a ray through the sky tested all 486 spheres. With it, the sky needed only a box test and the ground sphere.

Heat map without the BVH, in Whole path mode

Whole path follows every bounce and averages the counts across frames. Blue marks sphere tests. Each segment checks all 486 spheres, so this image mostly shows path length. Even rays through the sky make all 486 tests, which is why it stays dark blue. A pixel reaches white at the threshold of 2,874 sphere tests, about six segments within the capture’s 10-bounce limit. The white patches under and between spheres, and through the glass, mark rays that bounce many times before escaping.

Heat map with the SAH BVH, in Whole path mode

The same scene, now with BVH (SAH). Red marks box tests, blue sphere tests, and purple overlap. The thresholds are 183 box tests and 20 sphere tests. The rectangular red areas reveal the boxes in the tree: rays passing through them register the box tests.

Brightness is relative to each image’s threshold, so compare the values, not the colors: 2,874 sphere tests without the BVH versus 183 box tests and 20 sphere tests with it. The thresholds are values chosen for these captures, not measured maxima.

Then the FPS stopped climbing. The heat map showed that the search was no longer the main cost. At 1× resolution, four bounces and one sample, the scene reached 86 FPS. The next gains had to come from tracing fewer pixels or doing less work per ray.

I also tried a surface-area heuristic, or SAH. Instead of splitting a group in half, it compares the surface area of the boxes and how many spheres each split would contain. I wrote it and changed the traversal to test the nearer child first, working from guided snippets. At 1×, six bounces and one sample, the midpoint tree reached 46 FPS and SAH reached 49. The difference was small on a regular grid of spheres, but that did not make the more careful split useless; uneven geometry would give it more to work with.

I tried shrinking the GPU’s traversal stack from 32 slots to 16. In Debug, FPS nearly doubled. The tree was only nine levels deep, so 16 slots were enough. The dynamically indexed stack did not stay in fast registers; it spilled into slower memory. The validator now rejects a tree that is too deep for the stack, because a GPU write past the end can silently corrupt data instead of crashing.

Measure before optimizing the next thing

To see where the frame went, I proposed a GPU profiler—a panel that measures how long each GPU step takes—and wrote its timestamp support and renderer connection. Claude added the pass names and the Profiler panel, and fixed two timing bugs.

The first report said the GPU was working for 365 ms inside an 8.8 ms frame. That was impossible, but the ratio was a clue: the Mac showed the same GPU through two drivers, and they counted time differently. The profiler had used the wrong clock. With the selected device’s clock, the ray tracer took about 8.5 ms of the 8.8 ms frame; every other pass was below 0.1 ms.

The next question was how many pixels I was asking it to trace. At 100% render scale, the ray tracer took about 10 ms. At 50%, it took 3.5 ms because it traced a quarter as many pixels. The image was softer, so I asked Claude to integrate FSR 1, AMD’s upscaler: it rebuilds the image with attention to edges, then sharpens it. The integration used AMD’s shader code unchanged. At 50%, the full ray-tracing and upscaling path took about 4.4 ms; a separate 100% reading was about 11 ms. The lower-resolution image looked much clearer than ordinary bilinear scaling.

My final M4 Pro reading was 280–295 FPS at 1280×720 output, 50% render scale, FSR 1, sharpening at 0.2, and V-Sync—the display’s frame-rate cap—off. These are counter readings at the settings shown, not a benchmark. The original ~8 FPS reading had no recorded viewport size, so I cannot make a direct comparison.

Fast isn’t real-time

A still image can keep accumulating samples until it looks clean. Press Play, and the spheres move. The accumulation resets each frame, leaving one ray per pixel. FSR sees the speckle as detail and sharpens it. More FPS does not fix the noise.

Games handle this differently: they reuse information from earlier frames, remove noise with a denoiser, and often mix ordinary rendering with some ray tracing. Those are larger systems than this path tracer. A denoiser and hardware ray queries stayed on the backlog. The result was a fast still image, not a real-time path tracer.

A real target

I wanted to run and debug the engine on the Legion Go S. I did not measure the optimized scene there, so there is no post-optimization FPS claim for the handheld.

The first surprise was the interface scale. I thought it was set to 150%; a diagnostic log showed 190%. Once I logged the actual window and display scale, I could make the editor’s text and controls match the rest of the screen. I wrote the window-mode controls and settings panel; Claude helped separate the startup choice from the window’s live state. I tested Windowed, Borderless and Fullscreen. A dropdown appeared in the middle of the screen. Multi-viewport opened it as a separate X11 window; KWin, the Linux window manager, placed that window itself through XWayland. Disabling multi-viewport on Linux fixed the issue.

I also needed a repeatable way to send builds to the handheld. macOS could not build Linux, so gram deploy sent the sources to the device, built them there, launched the game through Steam and returned its log. I wanted Devices.ron beside Gram.ron: one Git-ignored list of deployment devices, not a per-machine settings file. This example uses SSH aliases, not addresses:

Devices(
    default: Some("legion"),
    devices: [
        Device(
            name: "legion",
            host: "legion",
            dir: "Projects/gram-engine",
            container: Some("gram"),
            env: Some("~/.gram-env"),
        ),
        Device(
            name: "windows-device",
            host: "windows-device",
            dir: "gram-engine",
            os: Windows,
        ),
    ],
)

Enter fullscreen mode Exit fullscreen mode

Claude wrote gram deploy at my choice, and I tested it on the device. A successful Linux deployment reports five steps:

gram deploy [1/5]: copy the sources to legion
gram deploy [2/5]: build gram on legion
gram deploy [3/5]: build debug on legion
gram deploy [4/5]: install the Steam launcher on legion
gram deploy [5/5]: run build/gram-engine/linux/debug/Game on legion through Steam

Enter fullscreen mode Exit fullscreen mode

That log line came from the first direct run, before the Steam launcher was in place:

ImGui: Scale Changed; Old: 0 -> New: 1.75

Enter fullscreen mode Exit fullscreen mode

Steam’s Game Mode only showed games launched by Steam. A small launcher installed by deploy and added once as a Non-Steam Game let Steam start the game on the handheld’s screen while its log continued to stream back to the Mac. I could open the Steam overlay with the controller and stop at breakpoints in both C++ and Rust, in Desktop Mode or Game Mode.

One debugging surprise was sleep. At a breakpoint, the game stopped drawing and taking input, so SteamOS treated the console as idle and dimmed the screen. I pressed the power button to wake it; that suspended the device and broke the connection. A dim screen wakes with a touch or any button, not the power button. The fix was for the Steam-launched wrapper to hold systemd-inhibit --what=idle:sleep while the game was being debugged. An inhibit request sent through SSH was refused because polkit, the desktop permission system, grants it only to the on-screen session; the Steam-launched wrapper belongs to that session. Steam still dims the display, but the console no longer goes to sleep at a breakpoint.

A Windows device over SSH

For this remote test, I used a laptop because it did not require a second monitor.

All three Windows builds—Debug, Release and Final—ran. Starting the game directly over SSH put it in Windows session 0, outside the logged-in desktop. A scheduled task marked to use the interactive session launched it visibly instead. I also had to add a firewall rule for SSH; installing OpenSSH had not done that.

Smart App Control let the hot-reload library through once, then blocked a later unsigned copy. The message said:

An Application Control policy has blocked this file

Enter fullscreen mode Exit fullscreen mode

The blocked file was GameLogic_1.dll, confirmed in the Code Integrity log. I turned Smart App Control off on the test device; shipping to players would require signing. A separate black window came from cmd, which collected the log, not from the game. Running it through conhost.exe --headless kept the log without opening another window.

The Windows device followed runtime UI scale changes immediately. I also tried gram deploy --debug, but LLDB could not find where Windows had loaded its modules. ASLR—address randomization—moves modules at runtime, and the breakpoints did not fire. I left that path aside. After a Windows update removed OpenSSH Server and reinstalling it stalled, the device became inaccessible. I stopped there instead of turning device setup into another project.

Where it stands

I finished with a much faster still image on the M4 Pro, a repeatable way to deploy and debug on the Legion Go S, and three Windows builds that ran. Windows remote debugging is still set aside after the ASLR attempt, and a later update left SSH inaccessible. I still have no post-optimization FPS reading for the Legion Go S; deploying from a Linux host remains on the backlog.

Episode 3 opens with VulkanShaderPass and moves on to triangles. This interlude was the preparation: understand what the current path tracer costs, make its still image faster on the Mac, and build a route to test the engine on portable hardware.

Top comments (0)