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, which binds native OpenGL, and 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 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.
1 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.
