# Rendering in Swift

**URL:** <https://forums.swift.org/t/rendering-in-swift/64990>\
**Category:** Community Showcase\
**Tags:** rendering\
**Created:** [May 15, 2023, 10:03am UTC](https://forums.swift.org/t/rendering-in-swift/64990 "2023-05-15T10:03:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gonsolo](https://avatars.discourse-cdn.com/v4/letter/g/3e96dc/32.png) [@gonsolo](https://forums.swift.org/u/gonsolo)\
**Post date:** [May 15, 2023, 10:03am UTC](https://forums.swift.org/t/rendering-in-swift/64990/1 "2023-05-15T10:03:18Z")

</div>

I released a new version of my renderer written in Swift and C++.

TLDR: Render Moana 20 times faster!

This is [v0.1.0](https://github.com/gonsolo/gonzales/releases/tag/v0.1.0-alpha) of the Gonzales renderer.

- Thanks to Embree (and other optimizations) rendering is 20 times faster now. Instead of taking 26 hours like in 2021 gonzales finished after 78 minutes.
- All scenes from [Bitterli’s resources](https://benedikt-bitterli.me/resources/) can be rendered now.
- A power-based light sampler. This is especially important for the [spaceship](https://benedikt-bitterli.me/resources/images/spaceship.png) as there are lots of tiny lights in the screen in the cockpit.
- [A Debian package for ptex](https://mentors.debian.net/package/ptex/).
- Reworked dependencies on Embree, OpenImageIO and Ptex relying on existing Debian packages.
- Lots of new materials like CoatedDiffuse, Conductor, Dielectric and Hair to be able to render scenes for PBRTv4.
- Adding volume integration (e.g. the [Volumetric Caustic from Bitterli](https://benedikt-bitterli.me/resources/images/volumetric-caustic.png)).
- OpenImageIO for most of texturing, caching and image writing.

There are still lots of things to do:

- Ray differentials.
- Parallel parsing (shouldn’t be too difficult using [Swift’s structured concurrency](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency/) for [PBRT’s Import statement](https://pbrt.org/fileformat-v4#include-import)).
- Getting all PBRTv4 scenes to work (most of them should work, the rest should be relatively easy).
- Bump and displacement mapping.
- Memory usage goes up when using Embree. I have yet to investigate this. Also mapping materials etc. for Embree is done very quick’n’dirty.
- A denoiser.
- Better sampling (low-discrepancy, blue noise, progressive multi-jittered)
- GPU rendering; this will be probably next on my plate.

You can read about it in my blog:

> **[An Update on the Gonzales Renderer](https://gonsoloblog.wordpress.com/2023/05/15/an-update-on-the-gonzales-renderer/)**
>
> Moana (1920×800 pixels, 64spp) rendered with Gonzales on an AMD Threadripper 1920x workstation (12 cores/24 threads) with 64GB RAM and 80GB swap space in 78 minutes. TLDR: Render Moana 20 time…

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [May 15, 2023, 1:50pm UTC](https://forums.swift.org/t/rendering-in-swift/64990/2 "2023-05-15T13:50:52Z")

</div>

Awesome!

I just took a very brief look at the code, and it seems that when rendering, you create a `DispatchGroup` and [submit the tile-rendering jobs](https://github.com/gonsolo/gonzales/blob/fbdac96ff0bafb1f2d76d1341ed600961da3f2a1/Sources/gonzales/Core/Renderer.swift#L67) to the global (concurrent) dispatch queue.

The design is quite nice and straightforward, and it _seems_ at first glance like it might not be too disruptive to port to Swift concurrency. You could add the tile-rendering jobs to a `TaskGroup` and merge the results as they become available, and - perhaps more interesting IMO - you could batch the rendering jobs using `async let`. I've heard that one of the benefits of Swift concurrency is that when a function uses `async let`, it has a known "amount" of structured concurrency (e.g. 8 structured child tasks), and the compiler can optimise how it allocates the stack that each child task requires, but I think you need some really heavy workloads (like this) to see the benefit.

Have you started experimenting with anything like that? If/when you do, please do post some results and don't hesitate to ask questions. I think lots of us here would be very interested to see how Swift concurrency scales in a project like this.

---

<div class="post-metadata">

**Author:** ![gonsolo](https://avatars.discourse-cdn.com/v4/letter/g/3e96dc/32.png) [@gonsolo](https://forums.swift.org/u/gonsolo)\
**Post date:** [May 15, 2023, 2:19pm UTC](https://forums.swift.org/t/rendering-in-swift/64990/3 "2023-05-15T14:19:48Z")

</div>

The tile rendering predates Swift's structured concurrency. I'd love to convert it to `async let` and I _think_ it should be only a modest change. But I also have lots of other things on my plate so let's see when I get to this. It might be also a good beginner's task.

There is also the `Import` statement which currently only calls `include` at [func import](https://github.com/gonsolo/gonzales/blob/fbdac96ff0bafb1f2d76d1341ed600961da3f2a1/Sources/gonzales/Api/Api.swift#LL108C14-L108C14) to (sequentially) read scene files. `Import` can be used to parallelize reading of huge scene files, see [Matt Pharr's blog](https://pharr.org/matt/blog/2021/04/11/moana-time-to-first-pixel). Currently Moana takes around 17 minutes to load, I believe this can be cut down to under 2 minutes.

---

<div class="post-metadata">

**Author:** ![gonsolo](https://avatars.discourse-cdn.com/v4/letter/g/3e96dc/32.png) [@gonsolo](https://forums.swift.org/u/gonsolo)\
**Post date:** [November 11, 2026, 8:46pm UTC](https://forums.swift.org/t/rendering-in-swift/64990/4 "2026-11-11T20:46:56Z")

</div>

> [@gonsolo](#):
>
> Currently Moana takes around 17 minutes to load, I believe this can be cut down to under 2 minutes.

It's 13 seconds now (if everything is correctly preprocessed): [https://youtu.be/dMYG1hy7kVY?si=DJmQ4vw0tdEm5u4O](https://youtu.be/dMYG1hy7kVY?si=DJmQ4vw0tdEm5u4O)

And it's not Swift anymore.
