# A compiler from OCaml to graphics shader languages

*2026-10-01 — idea*


Status: Ongoing
Level: PartII
Year: 2026
Project: OxCaml Labs
Supervisors: Anil Madhavapeddy, Patrick Ferris
Students: Rohan Wadhawan


Graphics programming on the GPU splits the rendering pipeline into standalone
shader programs. The usual way to use them is to hold the shader source as a
string, which the GPU driver compiles when the program runs. This is rarely a
performance problem, since shaders are small enough that compiling them takes
negligible time. The cost is that simple syntax errors and type mistakes, which
a compiler would normally catch, only appear once the whole program is running.

Graphics programming in OCaml is no different. Libraries such as
[tgls](https://github.com/dbuenzli/tgls), which binds native OpenGL, and
[Js\_of\_ocaml](https://ocsigen.org/js_of_ocaml/), which targets WebGL and
WebGPU, both take shaders as strings, so none of the OCaml type checker's
benefits reach them. The [Sarek](https://github.com/mathiasbourgoin/SPOC)
library already fixes this for GPGPU kernels that target CUDA and OpenCL. The
user writes a kernel in OCaml syntax inside an extension node, and the compiler
replaces it with the transpiled string for the target language.

## Project structure

This project proposes a similar library for graphics shaders.
There are two main high level graphics shader languages. The first is GLSL,
used with OpenGL and WebGL. The second is WGSL, used with WebGPU.

The project will build a new library, `Shaders_of_ocaml`, that lets shader
programs be written inline as native OCaml. The library compiles them to shader
code strings, which are then passed to the existing graphics libraries. The
OCaml compiler then type checks every shader.

The core of the library will be an embedded domain-specific language that uses
Generalised Algebraic Data Types to represent shader programs. This lets the
standard OCaml type checker enforce the type correctness of a shader at compile
time. A transpiler from this language will then be written for each of the two
target languages, GLSL and WGSL.

---
Canonical: https://anil.recoil.org/ideas/ocaml-to-shaders
Type: idea
License: CC BY 4.0 <https://creativecommons.org/licenses/by/4.0/>
Tags: ocaml, oxcaml, programming, hardware
