{"article":{"slug":"pixels-on-a-screen","title":"Pixels on a screen","subtitle":null,"summary":"Ben Griffiths explains how to render filled bezier shapes and SVGs, contrasting Skia's triangulate-and-rasterize approach with Cairo's flattening into line segments, and argues that 300 lines of a little-used language might be worth more than a million lines of C++.","content_type":"blog_post","language":"en","canonical_url":"https://whitebeard.blog/posts/pixels-on-a-screen/","author":{"name":"Ben Griffiths","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"Whitebeard","url":"https://whitebeard.blog/","listing_slug":null,"listing":null},"topics":[{"name":"graphics","slug":"graphics","url":"https://listedarticles.com/topics/graphics"},{"name":"programming","slug":"programming","url":"https://listedarticles.com/topics/programming"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":999,"reading_minutes":4,"published_at":"2026-10-27T16:51:50.000Z","added_at":"2026-11-17T10:46:33.421Z","updated_at":"2026-11-17T10:46:33.421Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/pixels-on-a-screen","markdown_url":"https://listedarticles.com/articles/pixels-on-a-screen.md","example":false,"citation":"Ben Griffiths, Whitebeard. \"Pixels on a screen.\" 27 Oct 2026. https://whitebeard.blog/posts/pixels-on-a-screen/ (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://whitebeard.blog/posts/pixels-on-a-screen/"},"body_markdown":"How do you render an SVG? Well, let’s start somewhere a bit simpler than the SVG spec: how do you render a set of filled bezier shapes? We can take the approach that [Skia](https://en.wikipedia.org/wiki/Skia_Graphics_Engine) does, decompose the shape into triangles and ask the GPU to rasterize (turn them into screen pixels), or we can take the approach that [Cairo](https://en.wikipedia.org/wiki/Cairo_%28graphics%29) does, flatten them into line segments and then turn them into pixels ourselves.\n\nBoth Cairo and Skia are mammoth (100,000 lines +) C/C++ libraries with lots of hand-tuned algorithms and memory management. But the maths is pretty simple, all adding-upseys and the odd divide. Is there a better way to express the rasterization pipeline with an amount of code that a mere human could read and understand in a few hours?\n\n[Dan Amelang](https://github.com/damelang) thought so and this was the basis of his [Gezira/Nile](https://github.com/damelang/nile) project. We can express the pipeline in a higher level language, he called it Nile and, in source-code form, it looks a bit like this:\n\n```\nDecomposeBeziers : Bezier >> EdgeSample\n    ∀ (A, B, C)\n        inside = (⌊ A ⌋ = ⌊ C ⌋ ∨ ⌈ A ⌉ = ⌈ C ⌉)\n        if inside.x ∧ inside.y\n            P = ⌊ A ⌋ ◁ ⌊ C ⌋\n            w = P.x + 1 - (C.x ~ A.x)\n            h = C.y - A.y\n            >> (P.x + 1/2, P.y + 1/2, w × h, h)\n        else\n            ABBC    = (A ~ B) ~ (B ~ C)\n            min     = ⌊ ABBC ⌋\n            max     = ⌈ ABBC ⌉\n            nearmin = | ABBC - min | < 0.1\n            nearmax = | ABBC - max | < 0.1\n            M       = {min if nearmin, max if nearmax, ABBC}\n            << (M, B ~ C, C) << (A, A ~ B, M)\n```\n\nThis is a Nile kernel. It receives `Bezier` tokens from a queue and emits `EdgeSample` tokens; it can also put tokens back on the queue (a kind of recursion). This one looks at beziers and if the curve fits inside one pixel it emits a sample, otherwise it splits it and puts both halves back on the queue. Kernels can be composed into bigger ‘pipelines’. And the upshot is you can end up with a whole pipeline like this:\n\n```\nRasterize : Bezier >> CoverageSpan\n    ⇒ DecomposeBeziers → SortBy (@x) → SortBy (@y) → CombineEdgeSamples\n```\n\nBeziers go in one side and `CoverageSpan`s (draw a line at x, y of length l covering c%) come out the other.\n\nThe language stays direct and tiny. It’s not entirely memory-less, kernels can keep their own internal state. All the work of optimisation and memory-management and execution happens in the compiler.\n\nI’ve been working on a compiler/optimiser/backend for Nile for about ten years. Little experiments here and there. Reading and re-reading the source code and filing away research I’ve read that might be useful. I’ve finally got something I can show: [a tiger](https://techbelly.github.io/midlands/tiger.html). Let me just apologise to everyone I’ve bored in the pub with these ideas in the meantime.\n\nThe Nile code that renders that entire tiger is around 300 lines long, most of it reusing Amelang’s Gezira. The same 300 lines compile to three backends - JavaScript, WASM and WebGPU. The current pipelines are very much Cairo-shaped but I’m interested in making a pipeline that spews out triangles and letting the GPU do its rasterizing job.\n\nThe compiler, umgeni, is very much a work in progress and it’s, um, interesting.\n\nI’ve tried to represent the language semantics with another little language I’m calling mooi. The Nile semantics, written in mooi, can act as a kind of symbolic interpreter: if you run values through it, it’ll interpret them; if you put symbols through it, you’ll get some IR out. It has a small prelude of useful functions to represent mapping and folding and writes against a small domain of eleven operations that the backends understand. The semantics of Nile (mostly! work in progress, remember) only exist as a handful of Mooi files. I have no idea if this is a good idea but I wanted to see if I could extend the idea of Nile for graphics to Mooi for compilation. We’ll see.\n\nThere are a number of stages to making this actually put pixels on an actual screen in a reasonable amount of time.\n\nFirst the Nile code is preprocessed into a form without significant whitespace (Python does something similar) and I parse it to an AST.\n\nThe umgeni elaborator’s job is to take that AST and generate an IR (intermediate representation) out of it. It uses mooi to do most of the work but it also does some inlining and defunctionalisation substitutions itself. The IR is scalar, all types and structure gone. Before I hand it off to the backends, I do a few optimisation passes.\n\nI ‘fuse’ kernels together, removing unnecessary intermediate queues and joining consecutive sorts. I do some tail call optimisations so that a kernel whose last act is to requeue a token just loops round instead. And also, I run a CSE (common subexpression elimination) pass to remove unnecessary assignments. Recent experiments include some dead-code-elimination and constant folding. Phew. Thank god I took [this compiler course](https://www.cs.cornell.edu/courses/cs6120/2020fa/self-guided/) last year.\n\nOne of the coolest things I’ve just added is automatic memoisation where I can skip a whole host of calculations if they’re the same as the last frame. It doesn’t help the demos much, but for real world use I think it’ll be a big help.\n\nI think this is going to be, has already been, a fun project. And almost totally useless: the rendering engines hand-tuned by experts against known hardware models might be significantly quicker (but only by a factor of 50 or so).\n\nHow do you render an SVG? Use Cairo or Skia. But if you want to understand how rendering works, maybe this project is for you. It might also give you a glimpse into a future where systems are made not from million-line programs no-one will ever have enough time on this earth to read but from clever compilers and simple direct languages you can grok in an afternoon.\n\nMy next job is to simplify the compiler/optimiser. Make it more readable and easier to understand. Then, I’ll put it away for another ten years. Will probably still bore people in pubs with it. Did I mention there was [a tiger](https://techbelly.github.io/midlands/tiger.html)?\n","body_html":"<p>How do you render an SVG? Well, let’s start somewhere a bit simpler than the SVG spec: how do you render a set of filled bezier shapes? We can take the approach that <a href=\"https://en.wikipedia.org/wiki/Skia_Graphics_Engine\" rel=\"nofollow ugc noopener\">Skia</a> does, decompose the shape into triangles and ask the GPU to rasterize (turn them into screen pixels), or we can take the approach that <a href=\"https://en.wikipedia.org/wiki/Cairo_%28graphics%29\" rel=\"nofollow ugc noopener\">Cairo</a> does, flatten them into line segments and then turn them into pixels ourselves.</p>\n<p>Both Cairo and Skia are mammoth (100,000 lines +) C/C++ libraries with lots of hand-tuned algorithms and memory management. But the maths is pretty simple, all adding-upseys and the odd divide. Is there a better way to express the rasterization pipeline with an amount of code that a mere human could read and understand in a few hours?</p>\n<p><a href=\"https://github.com/damelang\" rel=\"nofollow ugc noopener\">Dan Amelang</a> thought so and this was the basis of his <a href=\"https://github.com/damelang/nile\" rel=\"nofollow ugc noopener\">Gezira/Nile</a> project. We can express the pipeline in a higher level language, he called it Nile and, in source-code form, it looks a bit like this:</p>\n<pre><code>DecomposeBeziers : Bezier &gt;&gt; EdgeSample\n    ∀ (A, B, C)\n        inside = (⌊ A ⌋ = ⌊ C ⌋ ∨ ⌈ A ⌉ = ⌈ C ⌉)\n        if inside.x ∧ inside.y\n            P = ⌊ A ⌋ ◁ ⌊ C ⌋\n            w = P.x + 1 - (C.x ~ A.x)\n            h = C.y - A.y\n            &gt;&gt; (P.x + 1/2, P.y + 1/2, w × h, h)\n        else\n            ABBC    = (A ~ B) ~ (B ~ C)\n            min     = ⌊ ABBC ⌋\n            max     = ⌈ ABBC ⌉\n            nearmin = | ABBC - min | &lt; 0.1\n            nearmax = | ABBC - max | &lt; 0.1\n            M       = {min if nearmin, max if nearmax, ABBC}\n            &lt;&lt; (M, B ~ C, C) &lt;&lt; (A, A ~ B, M)</code></pre>\n<p>This is a Nile kernel. It receives <code>Bezier</code> tokens from a queue and emits <code>EdgeSample</code> tokens; it can also put tokens back on the queue (a kind of recursion). This one looks at beziers and if the curve fits inside one pixel it emits a sample, otherwise it splits it and puts both halves back on the queue. Kernels can be composed into bigger ‘pipelines’. And the upshot is you can end up with a whole pipeline like this:</p>\n<pre><code>Rasterize : Bezier &gt;&gt; CoverageSpan\n    ⇒ DecomposeBeziers → SortBy (@x) → SortBy (@y) → CombineEdgeSamples</code></pre>\n<p>Beziers go in one side and <code>CoverageSpan</code>s (draw a line at x, y of length l covering c%) come out the other.</p>\n<p>The language stays direct and tiny. It’s not entirely memory-less, kernels can keep their own internal state. All the work of optimisation and memory-management and execution happens in the compiler.</p>\n<p>I’ve been working on a compiler/optimiser/backend for Nile for about ten years. Little experiments here and there. Reading and re-reading the source code and filing away research I’ve read that might be useful. I’ve finally got something I can show: <a href=\"https://techbelly.github.io/midlands/tiger.html\" rel=\"nofollow ugc noopener\">a tiger</a>. Let me just apologise to everyone I’ve bored in the pub with these ideas in the meantime.</p>\n<p>The Nile code that renders that entire tiger is around 300 lines long, most of it reusing Amelang’s Gezira. The same 300 lines compile to three backends - JavaScript, WASM and WebGPU. The current pipelines are very much Cairo-shaped but I’m interested in making a pipeline that spews out triangles and letting the GPU do its rasterizing job.</p>\n<p>The compiler, umgeni, is very much a work in progress and it’s, um, interesting.</p>\n<p>I’ve tried to represent the language semantics with another little language I’m calling mooi. The Nile semantics, written in mooi, can act as a kind of symbolic interpreter: if you run values through it, it’ll interpret them; if you put symbols through it, you’ll get some IR out. It has a small prelude of useful functions to represent mapping and folding and writes against a small domain of eleven operations that the backends understand. The semantics of Nile (mostly! work in progress, remember) only exist as a handful of Mooi files. I have no idea if this is a good idea but I wanted to see if I could extend the idea of Nile for graphics to Mooi for compilation. We’ll see.</p>\n<p>There are a number of stages to making this actually put pixels on an actual screen in a reasonable amount of time.</p>\n<p>First the Nile code is preprocessed into a form without significant whitespace (Python does something similar) and I parse it to an AST.</p>\n<p>The umgeni elaborator’s job is to take that AST and generate an IR (intermediate representation) out of it. It uses mooi to do most of the work but it also does some inlining and defunctionalisation substitutions itself. The IR is scalar, all types and structure gone. Before I hand it off to the backends, I do a few optimisation passes.</p>\n<p>I ‘fuse’ kernels together, removing unnecessary intermediate queues and joining consecutive sorts. I do some tail call optimisations so that a kernel whose last act is to requeue a token just loops round instead. And also, I run a CSE (common subexpression elimination) pass to remove unnecessary assignments. Recent experiments include some dead-code-elimination and constant folding. Phew. Thank god I took <a href=\"https://www.cs.cornell.edu/courses/cs6120/2020fa/self-guided/\" rel=\"nofollow ugc noopener\">this compiler course</a> last year.</p>\n<p>One of the coolest things I’ve just added is automatic memoisation where I can skip a whole host of calculations if they’re the same as the last frame. It doesn’t help the demos much, but for real world use I think it’ll be a big help.</p>\n<p>I think this is going to be, has already been, a fun project. And almost totally useless: the rendering engines hand-tuned by experts against known hardware models might be significantly quicker (but only by a factor of 50 or so).</p>\n<p>How do you render an SVG? Use Cairo or Skia. But if you want to understand how rendering works, maybe this project is for you. It might also give you a glimpse into a future where systems are made not from million-line programs no-one will ever have enough time on this earth to read but from clever compilers and simple direct languages you can grok in an afternoon.</p>\n<p>My next job is to simplify the compiler/optimiser. Make it more readable and easier to understand. Then, I’ll put it away for another ten years. Will probably still bore people in pubs with it. Did I mention there was <a href=\"https://techbelly.github.io/midlands/tiger.html\" rel=\"nofollow ugc noopener\">a tiger</a>?</p>","headings":[]}}