<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tiberiu Petre</title>
    <description>The latest articles on DEV Community by Tiberiu Petre (@petrickah).</description>
    <link>https://dev.to/petrickah</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4262805%2Fd09098e8-f7c4-4ccc-995d-3860d4c7114a.jpg</url>
      <title>DEV Community: Tiberiu Petre</title>
      <link>https://dev.to/petrickah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/petrickah"/>
    <language>en</language>
    <item>
      <title>Gram Engine - Interlude 1.5: Gram Build on Three Platforms</title>
      <dc:creator>Tiberiu Petre</dc:creator>
      <pubDate>Tue, 24 Nov 2026 16:23:44 +0000</pubDate>
      <link>https://dev.to/petrickah/gram-engine-episode-15-gram-build-on-three-platforms-b17</link>
      <guid>https://dev.to/petrickah/gram-engine-episode-15-gram-build-on-three-platforms-b17</guid>
      <description>&lt;p&gt;&lt;em&gt;Drafted with AI assistance from my own notes and code, then edited and fact-checked by me.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To run the ray tracer on three platforms, Gram Build had to do more than pick a compiler. It needed three build modes with different jobs: Debug for development, Release for the game's own tools, and Final for a clean standalone run. It also needed the right compiler for each system.&lt;/p&gt;

&lt;p&gt;I wanted one IDE project that could hold both the engine's C++ and Rust code. Along the way, I typed a zero where the O belonged in one optimization setting, and Xcode couldn't find Cargo because it hadn't inherited Terminal's command path.&lt;/p&gt;

&lt;p&gt;The final test was on a Legion Go S running SteamOS. The game built and ran there too. The fan had its own review of the Debug build.&lt;/p&gt;

&lt;p&gt;The reason I tackled the build first was the ray tracer itself. It was the next engine project that needed to run beyond my Mac. Until then, Gram Build had only one Debug build. If I wanted to test the same renderer on Windows and Linux, the engine needed a build path that could travel with it.&lt;/p&gt;

&lt;p&gt;I gave the three builds different jobs. Debug kept the editor, logs and validation close while I worked. Release was optimized but kept the game's own test panels. Final ran without the editor and validation layers, like a clean standalone game. On Windows, it also opened without a console. Each mode had a different job in the same project.&lt;/p&gt;

&lt;p&gt;The compiler choice had to match the system too. I used Apple clang on the Mac, MSVC for a native Windows build, and GCC on Linux. From the Mac, MinGW could cross-compile for Windows. I kept the choice explicit: if I asked for MSVC and it was missing, Gram should tell me rather than quietly substituting another compiler. On the Mac, I could test that decision without having MSVC installed.&lt;/p&gt;

&lt;p&gt;One typo showed how easily a successful build can hide missing work. I meant the letter O in the link-time optimization setting, but wrote &lt;code&gt;LT0&lt;/code&gt; with a zero. The build still finished. Cargo ignored the misspelled setting without a message, so it looked as if the optimization had been enabled. My first tests repeated the same typo because they copied the values from the code. I rewrote them to check the rule instead. A test should catch a mistake, not inherit it.&lt;/p&gt;

&lt;p&gt;The IDE project was the next bridge. The engine had C++ at its core and Rust for gameplay. I wanted both visible in one project, with a way to rebuild the Rust part and let the running game pick it up. The project tree could show files that were not compiled as C++, so I could navigate the whole engine without pretending it was one language.&lt;/p&gt;

&lt;p&gt;The first Xcode failure looked unhelpful. It worked from Terminal, but Xcode started from Finder and could not find Cargo. It had not inherited Terminal's command-search folders. Once Gram passed that environment into the generated project, Xcode could start the build. There was still a practical split: Xcode handled C++, while I attached VS Code for Rust debugging. Visual Studio could set Rust breakpoints directly.&lt;/p&gt;

&lt;p&gt;Windows brought a different obstacle. Visual Studio would not build a target while its debugger was running. The rebuild moved into the editor instead. Play rebuilt the gameplay library in the background and reloaded it when ready. Pause stopped the systems. Stop restored the saved world. Those buttons kept the test loop moving without stopping the debugger for every change.&lt;/p&gt;

&lt;p&gt;The last platform was a Legion Go S running SteamOS. I first thought I could sideload the game like an Android app, but SteamOS was Linux, not Android, and its official Devkit client would not install on my Mac. I connected over SSH and used an Ubuntu container on the handheld to build the project. It ran on the Legion's AMD GPU. The useful result was that the same project built on a Mac, a Windows machine and a Linux handheld.&lt;/p&gt;

&lt;p&gt;Gram Build did not replace the tools it brought together. It learned enough to make one engine project build in different places, so I could get back to the part I actually wanted to test: the ray tracer. Read the full story in &lt;a href="https://petretiberiu.dev/posts/gram-engine-01-5-gram-build-three-platforms/" rel="noopener noreferrer"&gt;Episode 1.5 on petretiberiu.dev&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>rust</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Gram Engine - Episode 2: Ray Tracing in One Weekend on the GPU</title>
      <dc:creator>Tiberiu Petre</dc:creator>
      <pubDate>Tue, 24 Nov 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/petrickah/gram-engine-episode-2-ray-tracing-in-one-weekend-on-the-gpu-36pa</link>
      <guid>https://dev.to/petrickah/gram-engine-episode-2-ray-tracing-in-one-weekend-on-the-gpu-36pa</guid>
      <description>&lt;p&gt;&lt;em&gt;Drafted with AI assistance from my own notes and code, then edited and fact-checked by me.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In Episode 1, I got a triangle on screen and Rust code that could change while the game was running. But the triangle still lived inside the shader. It had no camera, no scene to belong to, and nothing gameplay code could move.&lt;/p&gt;

&lt;p&gt;Episode 2 takes four steps: Vulkan groundwork, a GPU version of the book’s path tracer, two approaches to depth of field, and one step beyond the book—moving the scene into the engine’s own world so Rust could move it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fweiyjens0lju1ifliw8f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fweiyjens0lju1ifliw8f.png" alt="The book’s cover scene before the optimization work" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;At a 1187×762 viewport, the ray tracer runs at 2374×1524 (200% render scale), with 10 bounces and 1 sample per frame: 4.0 FPS (250 ms).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The Profiler panel came later, during the 2.5 interlude. It shows the ray tracer taking 248.68 ms of a 250 ms frame. &lt;a href="https://petretiberiu.dev/posts/gram-engine-02-5-optimizing-for-real-hardware/" rel="noopener noreferrer"&gt;That interlude follows the work behind the profiler and the later performance readings&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 1 - Giving the GPU somewhere to work
&lt;/h2&gt;

&lt;p&gt;A triangle can live entirely in a shader. A ray tracer needs a camera, sphere and material data, and an image for the GPU to fill. A compute shader is a small program that runs on the GPU without drawing a triangle for every result. Before I could trace anything, I had to give that program a path into the scene and back to the editor.&lt;/p&gt;

&lt;p&gt;I gave each frame a clear order: run the game’s work, then draw the editor. The ray tracer wrote to an image shown in the Viewport panel. Claude wrote the render-target and Viewport plumbing; I wrote the image-state tracking that makes each step hand its image to the next one safely.&lt;/p&gt;

&lt;p&gt;I also tried giving compute work its own stream on the GPU. On the Mac, the second job waited for a signal that never came. I returned to one stream and added a barrier—a handoff between compute and rendering—so the two jobs happened in order.&lt;/p&gt;

&lt;p&gt;The next problem was getting data to the shader. I built reusable buffers and a descriptor manager. A descriptor layout says what resources a shader expects; a descriptor set supplies them. Thinking of it as a shape and its filled-in instance made a messy set of Vulkan calls easier to reason about.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkzkyku02j92fw83xoj2b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkzkyku02j92fw83xoj2b.png" alt="The textured square in the Vulkan viewport" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One early C++ bug made the triangle appear by luck: its shader name pointed into a temporary string that had already gone away. Keeping the reference to the real name fixed it. A pointer is only useful while its target is still alive.&lt;/p&gt;

&lt;p&gt;By the end of this chapter, the editor could display an image produced by a compute shader. The ray tracer had somewhere to run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 2 - The book, moved onto the GPU
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://raytracing.github.io/books/RayTracingInOneWeekend.html" rel="noopener noreferrer"&gt;&lt;em&gt;Ray Tracing in One Weekend&lt;/em&gt;&lt;/a&gt; starts as a small CPU renderer that writes an image. On the GPU, I had to replace its recursive bounces with a loop, create shader-side randomness, and make sure the data described in C++ matched what the shader expected.&lt;/p&gt;

&lt;p&gt;I wrote the ray-tracing pass, its main shader, and the supersampling and accumulation shaders. I also used Claude’s chat snippets for the camera basis, shader-side randomness and glass absorption, then wrote, adapted and integrated them. Claude wrote the CPU-side book-scene generator and the FPS counter.&lt;/p&gt;

&lt;p&gt;The first visible bugs came from the data. A deleted sphere stayed on screen because the GPU knew the buffer’s capacity, not how many spheres were still in it. Far spheres could also replace nearer hits. Passing the live count and keeping the closest hit fixed both problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  The blur that called itself FSAA
&lt;/h3&gt;

&lt;p&gt;A filter Gemini called “FSAA” averaged nearby pixels. It blurred the image, but it was not supersampling. Supersampling means rendering at a higher resolution, then averaging the extra pixels down. I made a 2× version that traced four samples for each output pixel and kept it as a reusable post-process.&lt;/p&gt;

&lt;p&gt;I also wanted a still image to get cleaner over time. Accumulation averages each new frame with the ones before it. When a sphere moved or the scene changed, that history had to reset. Otherwise old images lingered and a new sphere seemed to fade in. Clearing the image and starting the count over fixed it.&lt;/p&gt;

&lt;p&gt;The grey result had two causes: the swapchain had already applied gamma correction, and I divided by the sample count twice. Removing those duplicate steps brought the colors back.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqrjgl22sqqtsa0ss3rwq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqrjgl22sqqtsa0ss3rwq.png" alt="Three spheres, two diffuse and one glass, moving under bob in Play" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Captured in Play while &lt;code&gt;bob&lt;/code&gt;, the Rust system that moves the spheres, was active. Each moving frame resets accumulation, leaving one sample per pixel. Chapter 4 returns to &lt;code&gt;bob&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnrw41pg02lj5037l6o0o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnrw41pg02lj5037l6o0o.png" alt="The same static spheres after progressive accumulation" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Materials, glass, and the strange rings
&lt;/h3&gt;

&lt;p&gt;The scene grew from diffuse spheres to metal and glass. I kept material data separate from sphere data, so the shader could choose how each sphere scattered a ray.&lt;/p&gt;

&lt;p&gt;A hollow glass sphere did not need to be two separate spheres. The inner surface points its normal back toward the center, so one glass material can describe the whole shell. I also made the tint depend on how far a ray travels through the glass, rather than applying the same color at every surface.&lt;/p&gt;

&lt;p&gt;The rings inside a thick glass sphere looked like a bug. They were reflections bouncing inside the shell. Raising the bounce limit made the pattern easier to see.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhxxhp36ajij2zo5p93ng.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhxxhp36ajij2zo5p93ng.png" alt="The hollow glass sphere at four bounces" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ci4irwemhij5xrbwxyk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ci4irwemhij5xrbwxyk.png" alt="The hollow glass sphere at twelve bounces, showing more internal reflections" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 3 - Depth of field, twice
&lt;/h2&gt;

&lt;p&gt;The first version followed the camera model in the book. For each pixel, a ray aims at a point on the focus plane, then starts from a random point on the lens. My first attempt moved the start point but kept the direction, so all the rays stayed parallel. Aiming each ray from the lens sample toward the focus point made the blur behave like a camera.&lt;/p&gt;

&lt;p&gt;Then I mixed up two distances. The lens’s focal length is measured in millimetres; the distance to the focused subject is measured in metres. I used one setting for both, and got a 2.2 mm lens with a roughly 160-degree fisheye view. The physical camera conversion fixed this, and an editor control could exaggerate blur, which is hard to see at normal camera settings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1asogbm4qjqs333vhzug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1asogbm4qjqs333vhzug.png" alt="Depth of field produced by rays passing through a lens" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I also tried the game-engine approach: render the image first, then blur it using a depth image to tell which parts are in focus. It can make the picture look out of focus, but it cannot reveal what a blurred foreground object hid behind itself. That is the trade-off: the lens changes the rays before they hit the scene; the post-process only has the image already rendered.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4v9d65j502gqkrwcb3b9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4v9d65j502gqkrwcb3b9.png" alt="Depth of field applied as a post-process" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I wrote both depth-of-field shader paths and their sampling logic, working from guided snippets, and built the reusable post-process framework they used. Claude wrote the pass scaffold, the physical camera conversion and the editor’s blur control.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 4 - The scene becomes the engine’s
&lt;/h2&gt;

&lt;p&gt;The spheres began as a list inside the ray tracer. I moved them into the engine’s &lt;code&gt;World&lt;/code&gt;: entities are the objects, and components are the data attached to them. That made the spheres editable in the Inspector and available to Rust gameplay code.&lt;/p&gt;

&lt;p&gt;The GPU still needed its data in compact arrays. Each frame, the engine packs the &lt;code&gt;World&lt;/code&gt; for the ray tracer and compares it with the previous version. Accumulation resets only when the scene really changes. One apparent ghost sphere was simpler than it looked: Play had rebuilt the Rust library, but the C++ game was still an old binary.&lt;/p&gt;

&lt;p&gt;Then Rust started moving the scene. A system in &lt;code&gt;bob.rs&lt;/code&gt; moves the small spheres up and down while the game runs. Pause lets the image accumulate again; Stop puts the spheres back. Seeing the objects move made the new engine plumbing feel less like plumbing.&lt;/p&gt;

&lt;p&gt;I wrote the vector type, components, typed World access, scene packing, Rust iteration and &lt;code&gt;bob&lt;/code&gt;, often starting from guided snippets. Claude wrote the engine-to-Rust iteration step and a component lifetime constraint.&lt;/p&gt;

&lt;p&gt;The pass generator came from a practical irritation: too much repeated setup for each shader, and too many ways for C++ and the shader to disagree. Each shader now has a &lt;code&gt;.pass.ron&lt;/code&gt; description beside it. &lt;code&gt;gram build&lt;/code&gt; reads that description and generates matching C++ and shader declarations.&lt;/p&gt;

&lt;p&gt;I wrote the generator’s first stage: the data model and layout calculation. Claude wrote the later input, buffer and build-file stages and the matrix type.&lt;/p&gt;

&lt;h2&gt;
  
  
  The cover scene, and where it goes next
&lt;/h2&gt;

&lt;p&gt;The book’s final scene has 486 spheres: a ground, a field of smaller spheres, and three larger ones. Claude wrote its CPU-side generator with a fixed seed, so it produces the same arrangement each run.&lt;/p&gt;

&lt;p&gt;The first reading was about 8 FPS, but I did not record its viewport size. The new opening capture is at 1187×762 and reads 4.0 FPS with the ray tracer at 200% scale (2374×1524), 10 bounces and one sample per frame.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr7lwpibic4klyfrvlimi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr7lwpibic4klyfrvlimi.png" alt="The same cover scene after the optimization work in the 2.5 interlude" width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In the 2.5 interlude, the same 1187×762 viewport uses a bounding volume hierarchy (BVH), built with the surface-area heuristic (SAH), at 100% render scale and six bounces: 107.2 FPS (9.33 ms). The FSR 1 checkbox is visible, but at 100% the upscaler does not run, so this reading is without FSR.&lt;/em&gt; The screenshots share a viewport size and can be compared visually. FPS numbers are counter readings, not a benchmark; the original viewport was not recorded, and the later image also uses different render scale and bounce count. This is a later result, not work completed in Episode 2. &lt;a href="https://petretiberiu.dev/posts/gram-engine-02-5-optimizing-for-real-hardware/" rel="noopener noreferrer"&gt;The interlude covers the measurements and hardware work behind it&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What’s next
&lt;/h2&gt;

&lt;p&gt;The 2.5 interlude follows the measurements and the move to real hardware. Episode 3 then opens with &lt;code&gt;VulkanShaderPass&lt;/code&gt; and moves from spheres to triangles.&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>cpp</category>
      <category>rust</category>
      <category>vulkan</category>
    </item>
    <item>
      <title>Gram Engine - Interlude 2.5: Optimizing for Real Hardware</title>
      <dc:creator>Tiberiu Petre</dc:creator>
      <pubDate>Tue, 24 Nov 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/petrickah/gram-engine-interlude-25-optimizing-for-real-hardware-24ca</link>
      <guid>https://dev.to/petrickah/gram-engine-interlude-25-optimizing-for-real-hardware-24ca</guid>
      <description>&lt;p&gt;&lt;em&gt;Drafted with AI assistance from my own notes and code, then edited and fact-checked by me.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://petretiberiu.dev/posts/gram-engine-02-ray-tracing-in-one-weekend-on-the-gpu/" rel="noopener noreferrer"&gt;Episode 2&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  A tree over the spheres
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;min&lt;/code&gt;/&lt;code&gt;max&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;Whole path&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnwandj3rebezdgpcdgvv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnwandj3rebezdgpcdgvv.png" alt="Heat map without the BVH, in Whole path mode" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;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.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxm3q9coov1px6w1kr6bk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxm3q9coov1px6w1kr6bk.png" alt="Heat map with the SAH BVH, in Whole path mode" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;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.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;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.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measure before optimizing the next thing
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fast isn’t real-time
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  A real target
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nc"&gt;Devices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"legion"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;devices&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nc"&gt;Device&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"legion"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"legion"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Projects/gram-engine"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;container&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"gram"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"~/.gram-env"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Device&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"windows-device"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"windows-device"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"gram-engine"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Windows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Claude wrote &lt;code&gt;gram deploy&lt;/code&gt; at my choice, and I tested it on the device. A successful Linux deployment reports five steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That log line came from the first direct run, before the Steam launcher was in place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ImGui: Scale Changed; Old: 0 -&amp;gt; New: 1.75

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;systemd-inhibit --what=idle:sleep&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Windows device over SSH
&lt;/h2&gt;

&lt;p&gt;For this remote test, I used a laptop because it did not require a second monitor.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Smart App Control let the hot-reload library through once, then blocked a later unsigned copy. The message said:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;An Application Control policy has blocked this file

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The blocked file was &lt;code&gt;GameLogic_1.dll&lt;/code&gt;, 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 &lt;code&gt;cmd&lt;/code&gt;, which collected the log, not from the game. Running it through &lt;code&gt;conhost.exe --headless&lt;/code&gt; kept the log without opening another window.&lt;/p&gt;

&lt;p&gt;The Windows device followed runtime UI scale changes immediately. I also tried &lt;code&gt;gram deploy --debug&lt;/code&gt;, 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it stands
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Episode 3 opens with &lt;code&gt;VulkanShaderPass&lt;/code&gt; 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.&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>cpp</category>
      <category>rust</category>
      <category>vulkan</category>
    </item>
    <item>
      <title>Gram Engine - Episode 1: Hello Triangle</title>
      <dc:creator>Tiberiu Petre</dc:creator>
      <pubDate>Wed, 04 Nov 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/petrickah/gram-engine-episode-1-hello-triangle-546i</link>
      <guid>https://dev.to/petrickah/gram-engine-episode-1-hello-triangle-546i</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frjetxrl2jcepjeb3387d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frjetxrl2jcepjeb3387d.png" alt="The Hello Triangle running in the Gram editor: outliner, inspector and systems panel around a Vulkan triangle" width="800" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI assistance from my own notes and code, then edited and fact-checked by me.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In Episode 0 I built a build tool. A good one which grew from a Hello World compiler into something that rebuilds itself, handles C++ and Rust projects, cross-compiles for three platforms and manages dependencies.&lt;/p&gt;

&lt;p&gt;But a build tool is not a game engine. After Wave 0 shipped, I still had exactly zero things on a screen.&lt;/p&gt;

&lt;p&gt;Episode 1 fixes that. It has two halves, and they are very different kinds of work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Chapter 1 is the C++ half.&lt;/strong&gt; Vulkan, a window, a triangle, three operating systems. It is visual, it is satisfying, and it is full of bugs you can see.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chapter 2 is the Rust half.&lt;/strong&gt; No pixels at all, just a shared library, an ID table, and the plumbing that lets gameplay code live in a language that can be recompiled while the game is running.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One half you can show someone. The other half is what makes the engine worth building.&lt;/p&gt;




&lt;h2&gt;
  
  
  Chapter 1 - The C++ side: getting a triangle on screen
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The goal, and why it is harder than it sounds
&lt;/h3&gt;

&lt;p&gt;Open a window, draw a triangle. Every Vulkan tutorial starts here, and every one of them is about a thousand lines of setup before anything appears.&lt;/p&gt;

&lt;p&gt;Vulkan is explicit on purpose. It does not guess what you want. You tell it which GPU to use, how many frames to keep in flight, how to hand images to the screen, and what to do when the window changes size. In exchange you get control that OpenGL never gave you, and a stack trace for every mistake.&lt;/p&gt;

&lt;p&gt;I wrote this part myself, step by step, with code review between each step. That turned out to matter, because most of the bugs were not Vulkan bugs. They were ordinary C++ bugs wearing a Vulkan costume.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building it in pieces
&lt;/h3&gt;

&lt;p&gt;The first version was one file. By the third day &lt;code&gt;VulkanRenderer.cpp&lt;/code&gt; was 511 lines long and still had no triangle on screen, it was a clear signal to split it up.&lt;/p&gt;

&lt;p&gt;The split is one class per Vulkan resource, and each class follows one rule: &lt;strong&gt;if my dependencies are not ready, I stay invalid instead of crashing.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VulkanInstance → VulkanSurface → VulkanDevice → VulkanSwapchain
                                              → VulkanShader
                                              → VulkanPipeline
                                              → VulkanFrame

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;IsValid()&lt;/code&gt; convention sounds like a small thing. It is the difference between “the app crashed somewhere in a 500-line file” and “the swapchain says it could not create itself, here is why.”&lt;/p&gt;

&lt;h3&gt;
  
  
  Three bugs that were really one bug
&lt;/h3&gt;

&lt;p&gt;While refactoring, three separate crashes had the same root cause: &lt;strong&gt;a reference pointing at something that had already been destroyed.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A set of pointers pretending to be strings.&lt;/strong&gt; I kept device extension names in a &lt;code&gt;std::set&amp;lt;const char*&amp;gt;&lt;/code&gt;. That compares memory addresses, not text, and the pointers pointed into local vectors that no longer existed. Fix: &lt;code&gt;std::vector&amp;lt;std::string&amp;gt;&lt;/code&gt;, with &lt;code&gt;const char*&lt;/code&gt; views created when needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A shader destroyed by a comma.&lt;/strong&gt; I created a &lt;code&gt;VulkanShader&lt;/code&gt; inside an &lt;code&gt;if&lt;/code&gt; condition and then put it into a vector with &lt;code&gt;{a, b}&lt;/code&gt;. The &lt;code&gt;initializer_list&lt;/code&gt; copies through a temporary, and the temporary’s destructor destroyed the Vulkan handles. That one produced &lt;code&gt;VulkanShader&lt;/code&gt; as move-only, &lt;code&gt;vk::UniqueShaderModule&lt;/code&gt;, and a rule I now follow everywhere.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A &lt;code&gt;const vk::Device&amp;amp;&lt;/code&gt; bound to a local.&lt;/strong&gt; Vulkan handles are small values. Copy them. Pointing at them is asking for trouble.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The lesson stuck with me for the rest of the project: &lt;strong&gt;in C++, a reference is a promise that something else will stay alive. Most of my bugs were promises I could not keep.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The magenta screen
&lt;/h3&gt;

&lt;p&gt;Shaders compile from HLSL to SPIR-V with &lt;code&gt;dxc&lt;/code&gt;. On Linux I tried building DXC from source and hit a missing backend; then I moved to macOS and discovered the Vulkan SDK ships &lt;code&gt;dxc&lt;/code&gt; on every platform. One file, two entry points, compiled at build time. Done.&lt;/p&gt;

&lt;p&gt;The first frame was bright magenta: the classic “something is very wrong” color. Three problems stacked on top of each other:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the command buffer was submitted &lt;strong&gt;before&lt;/strong&gt; recording ended,&lt;/li&gt;
&lt;li&gt;the same fence was used for acquire and submit,&lt;/li&gt;
&lt;li&gt;one &lt;code&gt;renderFinished&lt;/code&gt; semaphore was shared across all images.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fix all three and:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;19 October, commit &lt;code&gt;5ac1f41&lt;/code&gt;: “I have the triangle, point up and colored.”&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is the moment the project stopped being an abstraction.&lt;/p&gt;

&lt;h3&gt;
  
  
  Three platforms, three drivers
&lt;/h3&gt;

&lt;p&gt;The triangle renders on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Windows&lt;/strong&gt; : natively, on an RTX 5060 Ti, from a &lt;code&gt;.exe&lt;/code&gt; cross-compiled on a Mac,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linux&lt;/strong&gt; : WSL2, on &lt;code&gt;llvmpipe&lt;/code&gt;, which means the CPU is doing the graphics work,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;macOS&lt;/strong&gt; : MoltenVK on an M4 Pro.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Getting there was mostly about accepting that each platform has its own personality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;macOS needed MoltenVK’s portability extensions&lt;/strong&gt; (&lt;code&gt;VK_KHR_portability_enumeration&lt;/code&gt;, &lt;code&gt;VK_KHR_portability_subset&lt;/code&gt;), and it has no geometry shader stage at all. Instead of &lt;code&gt;#ifdef&lt;/code&gt; scattered everywhere, the code has capability macros: &lt;code&gt;GRAM_PLATFORM_APPLE&lt;/code&gt;, &lt;code&gt;GRAM_GEOMETRY_SHADERS&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Windows had two GPUs&lt;/strong&gt; : an Intel UHD 770 and the RTX, but my device scoring was hiding one of them. Fixing the score also fixed a &lt;code&gt;uint32_t&lt;/code&gt; underflow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WSL had a lie for an executable path.&lt;/strong&gt; &lt;code&gt;argv[0]&lt;/code&gt; was just &lt;code&gt;GramEngine.exe&lt;/code&gt;, so shaders could not be found. Fix: a real &lt;code&gt;GetExecutableDirectory()&lt;/code&gt; per platform.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resizing and minimizing&lt;/strong&gt; broke the swapchain. &lt;code&gt;ErrorOutOfDateKHR&lt;/code&gt; on resize, an assert on minimize. The fix was a proper &lt;code&gt;Recreate()&lt;/code&gt; path and treating a 0×0 window as “try again later.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The workflow ended up reversed from what I expected: &lt;strong&gt;develop on macOS, cross-compile the Windows executable there, test it natively on the PC.&lt;/strong&gt; MoltenVK is a subset of Vulkan, so what works on the Mac usually works everywhere.&lt;/p&gt;

&lt;h3&gt;
  
  
  Beyond the triangle: the tools that make the next wave possible
&lt;/h3&gt;

&lt;p&gt;A triangle alone would have been a short article. What made this half of Wave 1 worthwhile was everything around it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A logger&lt;/strong&gt; built on spdlog, with check macros that read like assertions (&lt;code&gt;GramCheck(x)&lt;/code&gt; means “I expect x to be true”). This one had a spectacular bug: my error macro returned &lt;code&gt;false&lt;/code&gt; on success too, so fences were never created and nothing rendered. The fix was separating logging from control flow, which is a distinction I now understand in my bones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An ImGui overlay&lt;/strong&gt; with docking and multi-viewport. It gives me an inspector, a systems panel, and a place to put debug buttons without writing UI code for each one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A layer system&lt;/strong&gt; : &lt;code&gt;Layer&amp;lt;Owner&amp;gt;&lt;/code&gt; / &lt;code&gt;LayerOwner&amp;lt;Owner&amp;gt;&lt;/code&gt;, one layer per type, started in registration order and stopped in reverse. It is how the renderer, the ImGui backend and the game’s own UI coexist without knowing about each other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frames in flight&lt;/strong&gt; , so the GPU is never waiting for the CPU to finish a frame. Four objects leaked at shutdown because I used &lt;code&gt;reserve&lt;/code&gt; where I needed &lt;code&gt;resize&lt;/code&gt;, just a one-word bug that took an afternoon.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the end of Chapter 1, the C++ side had a working window, a triangle, an editor overlay and a clean shutdown on three platforms.&lt;/p&gt;

&lt;p&gt;What it did not have was gameplay.&lt;/p&gt;




&lt;h2&gt;
  
  
  Chapter 2 - The Rust side: gameplay that reloads
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The question that took three tries
&lt;/h3&gt;

&lt;p&gt;I wanted gameplay logic in Rust. Not because Rust is fashionable, but because recompiling a game to change one number is a terrible way to work. I wanted the same thing Unity and Unreal give you: edit the logic, see the change.&lt;/p&gt;

&lt;p&gt;That means hot reload. And hot reload across a language boundary is genuinely hard, because the moment you unload a library, everything that pointed into it becomes poison.&lt;/p&gt;

&lt;p&gt;I went through three designs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;When&lt;/th&gt;
&lt;th&gt;The plan&lt;/th&gt;
&lt;th&gt;Why it did not survive&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;September&lt;/td&gt;
&lt;td&gt;Rust owns &lt;code&gt;main()&lt;/code&gt;, C++ is a static library&lt;/td&gt;
&lt;td&gt;Inverts the engine; the renderer should not be a guest in the gameplay runtime&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Late September&lt;/td&gt;
&lt;td&gt;A &lt;code&gt;bevy_ecs&lt;/code&gt; world in a static Rust crate, systems in a reloaded &lt;code&gt;cdylib&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;TypeId&lt;/code&gt;s drift, and any surviving data pointing into the unloaded library crashes on drop&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Adopted 28 October&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;C++ owns everything. Rust is a stateless &lt;code&gt;cdylib&lt;/code&gt;.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;This one stuck&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The final idea is one sentence: &lt;strong&gt;all state lives in C++; the reloadable library holds only logic.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is the “state in the host, code in the DLL” pattern from Handmade Hero, just across a language boundary instead of inside one C++ program. Nothing in surviving memory points into the Rust library, because Rust stores nothing. When the library unloads, it takes nothing with it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Two tables and one symbol
&lt;/h3&gt;

&lt;p&gt;The library exports exactly one symbol: &lt;code&gt;game_get_api()&lt;/code&gt;. Everything else goes through two tables of function pointers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C++ fills &lt;code&gt;HostAPI&lt;/code&gt;&lt;/strong&gt; , represents what the engine offers to the game:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;HostAPI&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// C++ hands Rust its own "this"&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LogLevel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;RegisterSystem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Descriptor&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ObjectID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ComponentID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Rust fills &lt;code&gt;GameAPI&lt;/code&gt;&lt;/strong&gt; , represent what the game offers to the engine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="cp"&gt;#[repr(C)]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;GameAPI&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// checked before anything else&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;on_load&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s2"&gt;"C"&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;HostAPI&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;GameAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;on_unload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s2"&gt;"C"&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;run_system&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s2"&gt;"C"&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kd"&gt;mut&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important bit is what is &lt;strong&gt;not&lt;/strong&gt; there: no game-specific function names. &lt;code&gt;GameAPI&lt;/code&gt; is the same for every game and never grows. The engine does not need to know that &lt;code&gt;greet&lt;/code&gt; or &lt;code&gt;spin&lt;/code&gt; exist.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reloading without crashing
&lt;/h3&gt;

&lt;p&gt;The loader is an Application layer, and it does the same five things every time:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Copy &lt;code&gt;libGameLogic.dylib&lt;/code&gt; to &lt;code&gt;hot/libGameLogic_&amp;lt;N&amp;gt;.dylib&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Load &lt;strong&gt;the copy&lt;/strong&gt; , never the original.&lt;/li&gt;
&lt;li&gt;Find &lt;code&gt;game_get_api&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Check the version.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;on_load&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every part of that is a scar from a real failure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Copy-on-load&lt;/strong&gt; because Windows locks a loaded DLL, so the build cannot overwrite it, and on macOS, writing over a mapped library can crash the process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A new name on every load&lt;/strong&gt; because macOS often does not actually unload a Rust dylib. &lt;code&gt;dlopen&lt;/code&gt; on a path that is already in memory hands you the old image, with old statics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Atomic artifacts&lt;/strong&gt; : &lt;code&gt;gram build&lt;/code&gt; writes &lt;code&gt;&amp;lt;name&amp;gt;.partial&lt;/code&gt; and renames it over the old file. The watcher never sees a half-written library, and macOS does not kill a running process whose executable was rewritten in place.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automatic reload&lt;/strong&gt; : every 0.5 s the loader compares the file’s write time with the one it last loaded. If they differ, the game reloads itself. No button needed, but now there is one too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And panics never cross the boundary. Every exported function wraps its body in &lt;code&gt;catch_unwind&lt;/code&gt;, and the panic hook logs &lt;code&gt;file:line:col: message&lt;/code&gt; through the engine’s logger. A system that panics logs a line, gets skipped that frame, and the game keeps running.&lt;/p&gt;

&lt;h3&gt;
  
  
  The ECS: entities that never alias
&lt;/h3&gt;

&lt;p&gt;Now the part I was most curious about.&lt;/p&gt;

&lt;p&gt;An ECS (entity-component-system) sounds abstract until you build one. The idea is simple: an &lt;strong&gt;entity&lt;/strong&gt; is just an ID. A &lt;strong&gt;component&lt;/strong&gt; is plain data attached to an entity. A &lt;strong&gt;system&lt;/strong&gt; is a function that reads and writes components.&lt;/p&gt;

&lt;p&gt;No MonoBehaviour. No &lt;code&gt;Update()&lt;/code&gt; method on every object. Just data in memory and functions that walk it.&lt;/p&gt;

&lt;p&gt;The C++ &lt;code&gt;World&lt;/code&gt; does the storage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;An &lt;code&gt;ObjectID&lt;/code&gt; is an index plus a generation.&lt;/strong&gt; When an entity dies, its slot goes on a free list, and the generation increments. A stale ID is detected instead of silently pointing at a new entity. This one detail prevents an entire category of “I deleted it and it came back” bugs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Components are stored per type&lt;/strong&gt; , packed in a contiguous array, with an owner table to map entity → slot. Destroying an entity swaps the last element into its place.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A component’s address is not stable.&lt;/strong&gt; The array moves when it grows, which is why Rust only ever holds a pointer for the duration of one call.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Components described once
&lt;/h3&gt;

&lt;p&gt;Here is the part that feels like magic the first time you watch it work.&lt;/p&gt;

&lt;p&gt;A component is described in a &lt;code&gt;.ron&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Component(
    name: "Greeting",
    fields: [
        Field(name: "text", ty: Str(64)),
        Field(name: "scale", ty: F32),
    ],
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;gram build&lt;/code&gt; runs a generator over the whole project before compiling. From that one definition it writes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a Rust &lt;code&gt;#[repr(C)]&lt;/code&gt; struct, with compile-time asserts on size and field offsets,&lt;/li&gt;
&lt;li&gt;a matching C++ struct, with &lt;code&gt;static_assert&lt;/code&gt;s,&lt;/li&gt;
&lt;li&gt;a descriptor table: names, types, offsets, sizes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two languages, one source of truth, and the compilers check each other. If the layouts disagree, the build fails, but not the game, at runtime, three hours later.&lt;/p&gt;

&lt;p&gt;It also means &lt;strong&gt;the inspector is generic&lt;/strong&gt;. It does not know what a &lt;code&gt;Greeting&lt;/code&gt; is. It walks the descriptor table and draws a widget per field. Add a field to the schema, rebuild and restart, and it appears in the inspector. Without writing any C++: the generated header brings the new descriptor, and the inspector only knows how to draw field types. A reload alone isn’t enough: a schema change alters the data’s layout, so it needs a restart.&lt;/p&gt;

&lt;p&gt;That is the same idea as Anvil’s &lt;code&gt;.mold&lt;/code&gt; files at Ubisoft, and it is the first time in this project that I have built something that felt like real engine architecture rather than a tutorial with extra steps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query, and the borrow checker’s blind spot
&lt;/h3&gt;

&lt;p&gt;Gameplay code should not need &lt;code&gt;unsafe&lt;/code&gt;, raw pointers, or &lt;code&gt;extern "C"&lt;/code&gt;. It looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;greet1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;EventTarget&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="kc"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="kc"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;info!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Greetings from Rust: {}!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="kc"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_mut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello world from Rust"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there is a problem Rust’s compiler cannot see. Two &lt;code&gt;ObjectID&lt;/code&gt;s could name the same component. The borrow checker only knows about Rust references and it has no idea that two integers point at the same C++ memory.&lt;/p&gt;

&lt;p&gt;So the check happens at runtime, using the same rules &lt;code&gt;RefCell&lt;/code&gt; uses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;thread_local!&lt;/code&gt; list tracks active borrows.&lt;/li&gt;
&lt;li&gt;Two reads are fine. A write conflicts with anything on the same entity and component.&lt;/li&gt;
&lt;li&gt;Different entities never conflict, which is what lets you swap a value between two entities.&lt;/li&gt;
&lt;li&gt;The guard releases on &lt;code&gt;Drop&lt;/code&gt;, including during a panic unwind.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The guard is taken &lt;strong&gt;before&lt;/strong&gt; the reference exists, so two references to the same memory never exist at the same time.&lt;/p&gt;

&lt;p&gt;This is the kind of design that looks obvious in hindsight and is not obvious at all while you are designing it. It is also the part of the Rust code I am proudest of.&lt;/p&gt;

&lt;h3&gt;
  
  
  Systems, and the macro that writes the boring part
&lt;/h3&gt;

&lt;p&gt;A system is an ordinary Rust function. The game lists them in one place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nf"&gt;export_game!&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"greet"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;greeter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;greet1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;greeter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;greet2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That macro expands into the &lt;code&gt;GameAPI&lt;/code&gt; table, the registration calls, the dispatcher, and &lt;code&gt;game_get_api&lt;/code&gt; with every one of them wrapped in the panic guard. The gameplay code stays plain Rust.&lt;/p&gt;

&lt;p&gt;Behind the scenes, C++ owns the registry and the schedule. On &lt;code&gt;on_load&lt;/code&gt;, Rust sends one descriptor per system (name, stage, event, index). Every frame, C++ walks the update systems and then dispatches any queued events. The “Greet” button in the inspector does not call Rust, it &lt;strong&gt;emits an event&lt;/strong&gt; , and whoever subscribed to it runs.&lt;/p&gt;

&lt;p&gt;That distinction matters more than it sounds. The UI does not know what gameplay exists. It just emits &lt;code&gt;"greet"&lt;/code&gt; and something, someday, might listen.&lt;/p&gt;

&lt;h3&gt;
  
  
  What it looks like when it works
&lt;/h3&gt;

&lt;p&gt;The proof of concept is deliberately unglamorous:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The editor shows an entity with a &lt;code&gt;Greeting&lt;/code&gt; component: one text field and a number.&lt;/li&gt;
&lt;li&gt;You type something in the inspector.&lt;/li&gt;
&lt;li&gt;You change a log message in Rust and rebuild.&lt;/li&gt;
&lt;li&gt;The game reloads by itself with no restart, and the new logic runs.&lt;/li&gt;
&lt;li&gt;The text you typed is still there.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That sequence is the whole point of Wave 1. Everything in Chapter 2 exists to make step 5 true.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bugs from the Rust half
&lt;/h3&gt;

&lt;p&gt;The mistakes here were a different species, less “temporary lifetime” and more “the compiler and the runtime disagree about what just happened”:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A missing field in a designated initializer is silently zeroed.&lt;/strong&gt; Twice. Once it put a null function pointer in &lt;code&gt;HostAPI&lt;/code&gt; and aborted the load. The second time it left &lt;code&gt;EventTarget&lt;/code&gt; out of the frame context, so update systems saw entity &lt;code&gt;{0, 0}&lt;/code&gt; as their target, which is a live entity. The fix was explicit &lt;code&gt;InvalidObjectID&lt;/code&gt; plus a debug print that made it visible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Layout arithmetic.&lt;/strong&gt; My first generator used a field’s &lt;em&gt;size&lt;/em&gt; as the next field’s offset, and started the struct alignment at 0. A unit test caught it. Now both compilers check the generator’s output, so a mistake fails at build time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;#[track_caller]&lt;/code&gt; stops at closures.&lt;/strong&gt; The panic pointed inside &lt;code&gt;borrow.rs&lt;/code&gt; instead of the game’s code. Fix: compute the conflict inside the closure, panic outside it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Drop&lt;/code&gt; has to find its own borrow, not a conflicting one.&lt;/strong&gt; Searching with the conflict condition could remove a different borrow entirely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;let _ = guard&lt;/code&gt; drops immediately. &lt;code&gt;let _borrow = guard&lt;/code&gt; keeps it alive.&lt;/strong&gt; One character is the difference between having protection and having none.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An ImGui layer that returns &lt;code&gt;false&lt;/code&gt; skips every layer after it.&lt;/strong&gt; A panel returned its “open” state, so closing it also removed the panels drawn after it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A macro must not lean on the caller’s imports.&lt;/strong&gt; &lt;code&gt;export_game!&lt;/code&gt; only compiled because the prelude happened to glob-import the trait it used. Fixing the prelude would have broken every game. Now everything goes through &lt;code&gt;$crate::&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of those cost real time. Most of them are now covered by 23 unit tests in &lt;code&gt;gram-core&lt;/code&gt; and 7 command-level tests for the generator.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where Wave 1 stands
&lt;/h2&gt;

&lt;p&gt;The C++ half is done: a Vulkan 1.3 renderer on three platforms, shaders compiled at build time, a docking ImGui editor with an outliner, an inspector and a systems panel, a logger, and a layer system.&lt;/p&gt;

&lt;p&gt;The Rust half is done and working: a stateless &lt;code&gt;cdylib&lt;/code&gt;, automatic reload, panics contained at the boundary, a generational-ID ECS, components generated from schemas, runtime-checked borrows, systems with real parameter types, and events.&lt;/p&gt;

&lt;p&gt;What is deliberately &lt;strong&gt;not&lt;/strong&gt; done yet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Iterating a component&lt;/strong&gt; (&lt;code&gt;Query::iter&lt;/code&gt;): it needs a new host function and a version bump. Wave 2 uses it when Rust starts moving spheres.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Typed access from C++&lt;/strong&gt; : today C++ reaches components through &lt;code&gt;void*&lt;/code&gt; and a &lt;code&gt;ComponentID&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System ordering inside a stage&lt;/strong&gt; : registration order for now.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More schema field types&lt;/strong&gt; : &lt;code&gt;Vec3&lt;/code&gt; first.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What’s next
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Wave 2 is the ray tracer.&lt;/strong&gt; A compute shader pipeline, spheres first owned by C++, then driven from Rust entities. That is the first time gameplay logic will actually move something visible on screen, and the first real test of whether the architecture from Chapter 2 holds up.&lt;/p&gt;

&lt;p&gt;I am looking forward to it. The plumbing is done. Now I get to make something move.&lt;/p&gt;




&lt;p&gt;Two chapters, one wave. One half is pixels and platforms and the particular joy of seeing a triangle appear. The other half is contracts, IDs and the quiet satisfaction of changing code in a running game.&lt;/p&gt;

&lt;p&gt;Neither half works without the other. That is what an engine is, I think: it’s not a renderer, not an editor, but the agreement between them.&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>vulkan</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Gram Engine — Episode 0: Gram Build</title>
      <dc:creator>Tiberiu Petre</dc:creator>
      <pubDate>Wed, 16 Sep 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/petrickah/gram-engine-episode-0-gram-build-2e96</link>
      <guid>https://dev.to/petrickah/gram-engine-episode-0-gram-build-2e96</guid>
      <description>&lt;p&gt;&lt;em&gt;Drafted with AI assistance from my own notes and code, then edited and fact-checked by me.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Before I wrote any rendering code, I started with a tool to build Gram Engine. Gram Build's first job was to turn its source code into something that ran.&lt;/p&gt;

&lt;p&gt;Over time, it grew beyond that job. I could use it for other C++ and Rust projects too, not just a game engine. This is the story of how a tool built for Gram Engine became useful well beyond it.&lt;/p&gt;

&lt;p&gt;The reason was practical. Before the engine could open a window or draw a triangle, its source had to become a program that ran. I wanted to understand that step and make it repeatable as the project grew. So I began with a small tool for building the engine instead of starting with the renderer.&lt;/p&gt;

&lt;p&gt;The first useful version built one C++ file or one Rust file. Hello World ran on Linux and Windows. That was a real milestone, but it also showed the limit: one file is enough to prove a build can work, not enough for a real engine. The project would have many files and rely on outside libraries.&lt;/p&gt;

&lt;p&gt;The turn came when I noticed a mismatch. I had described Gram Build as a tool that could build itself, but I was still using a separate program to build Gram. I did not want the claim to outrun what it actually did. I changed the project so Gram could treat its own source like another project, then used it to build itself. It worked. The tool had proved its central promise on its own code.&lt;/p&gt;

&lt;p&gt;As the engine grew, it needed more than one-file examples. I could wait until the next engine work demanded support for larger C++ projects and outside libraries, or solve the build problem first. I chose to do it early because the missing support would otherwise block the work I wanted to start. Gram Build expanded because the project put real problems in front of it, not because a universal tool had been in the plan from day one.&lt;/p&gt;

&lt;p&gt;The first problems were small but revealing. A new project ended up in the wrong place, and a different one failed with an error that looked like a missing dependency. In both cases, the obvious explanation was wrong. I learned to check the starting assumptions before adding another fix.&lt;/p&gt;

&lt;p&gt;Later, I found that a cleanup step could remove far more than I intended. Another build failure came from a missing tool on the machine, not a broken compiler. The lesson was to follow each problem to what actually happened, and to test the behavior a person expects rather than just the outcome the code seemed to produce.&lt;/p&gt;

&lt;p&gt;By the end, Gram Build had grown beyond its original home. I could also use it for a separate C++ socket-programming project with nothing to do with an engine or a game. The tool could start, build and run projects, and it had learned to support work beyond the engine that gave it a reason to exist.&lt;/p&gt;

&lt;p&gt;I did not set out to build a universal tool. I kept solving the next real problem: first getting a build to work, then making the tool build itself, then helping it handle larger projects. What began as groundwork for Gram Engine became useful beyond games. Read the full article in &lt;a href="https://petretiberiu.dev/posts/gram-engine-00-gram-build/" rel="noopener noreferrer"&gt;Episode 0 on petretiberiu.dev&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;The repo is private for now, but the articles are public. If building in the open resonates — Wave 1’s article follows once a triangle actually renders on screen. Until then: &lt;code&gt;gram build&lt;/code&gt;, run the binary, watch it print Hello World. Then watch it rebuild itself. Then realize you can use it for anything.&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>rust</category>
    </item>
  </channel>
</rss>
