1 of 26

RLSL

A Rust to SPIR-V compiler

Maik Klein, Embark Studios

2 of 26

Agenda

  • Embark
  • Why we use Rust?
  • Why Rust on the GPU?
  • The experiment of getting Rust on the GPU
  • Next steps

3 of 26

Embark Studios

We are a Stockholm-based games studio, on a mission to blur the line between playing and making.

4 of 26

Why Rust?

When we started Embark, we chose Rust as our primary language for the long term future we are building. We love the safety and robustness of the language, the ability to write high performance, safe, and (mostly) bug free code and then fearlessly refactor and change it without common lifetime/ownership, memory safety or race condition problems.

That, combined with the openness and collaborative nature of the quickly growing ecosystem of and around Rust with crates.io and the tens of thousands of open source crates with a best-in-class package system, cargo, truly makes Rust a language for the next 40 years.

We believe that by openly sharing our work, issues, and ideas with the community, we'll create more opportunities for collaboration and discussion to bring us toward a great future for Rust and for the games industry in general.

-- Johan Andersson (@repi), CTO, Embark

5 of 26

Why Rust on the GPU?

All the same reasons why Rust is nice on the CPU.

Unified front end.

You benefit from the Rust ecosystem

6 of 26

crates.io

Central repository for hosting and finding libraries

7 of 26

Cargo

Easily add dependencies to your project

Open community

Hypothetical libraries

8 of 26

What is RLSL?

RLSL => Rust Like Shading Language

RLSL is a subset of Rust

TL;DR: Write your shaders in Rust

Highly experimental (Not ready to be used)

https://github.com/MaikKlein/rlsl

9 of 26

Generating SPIR-V

RUST

MIR

SPIR-V

MIR => Mid Level Intermediate Representation

10 of 26

Building your project

Rlsl Integrates seamlessly with cargo

11 of 26

Showcase

12 of 26

13 of 26

Ray sphere intersection

14 of 26

15 of 26

Scattering

16 of 26

Things you get for free

  • Auto completion
  • Formatting
  • Nice compiler error messages
  • Linting

17 of 26

Mapping SPIR-V types

SPIR-V has built in intrinsic types like OpTypeVector

Those can be mapped with attributes #[spirv(Vec4)]

Can be implemented in user land

18 of 26

Testing

1

2

5

9

1

2

3

1

2

5

9

8

2

3

CPU

GPU

  • Compares the output of the CPU and GPU

  • As simple as

Mismatch

5

3

1

1

9

4

8

Input

19 of 26

Challenges

20 of 26

Structured Control Flow

MIR is fully unstructured

Structured control flow has to be recomputed

Many edge cases that are hard to track down

21 of 26

rspirv-cfg

A blend of spirv-cfg and spirv-dis

This project is independent of RLSL and can be used with any SPIR-V file

rspirv-cfg

22 of 26

Pointers can be tricky

SPV_KHR_physical_storage_buffer allows pointers into storage buffers

23 of 26

Mapping algebraic data types

No unions in SPIR-V

No arbitrary bit casts in SPIR-V

This results in an inefficient memory layout for enums

SPIR-V

24 of 26

Promising prototype

Many features have been implemented and proven to work

  • Branches
  • Loops
  • Generics
  • Intrinsics
  • Cargo
  • ...

25 of 26

Closing

We at Embark want to invest in a rusty GPU future

We are looking for partners to collaborate with

Feel free to reach out to me or visit embark.rs

Or send us an email opensource@embark-studios.com

26 of 26

Questions?