# Writing GPU Shaders in Julia

**URL:** <https://discourse.julialang.org/t/writing-gpu-shaders-in-julia/60669>\
**Category:** GPU\
**Created:** [May 6, 2021, 5:34pm UTC](https://discourse.julialang.org/t/writing-gpu-shaders-in-julia/60669 "2021-05-06T17:34:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [May 6, 2021, 5:34pm UTC](https://discourse.julialang.org/t/writing-gpu-shaders-in-julia/60669/1 "2021-05-06T17:34:55Z")

</div>

I was going over the [GLAbstraction tutorials](https://github.com/JuliaGL/GLAbstraction.jl/tree/master/tutorials) to learn how do do simple animations on the GPU, and things work fairly well.

One pain point, however, is that the shaders needs to be written in a special language, meaning that they clash with the rest of the code. Is it possible to compile shaders from julia code?

Rust has [rust-gpu](https://github.com/EmbarkStudios/rust-gpu), but I could not find anything similar for Julia.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [May 6, 2021, 5:51pm UTC](https://discourse.julialang.org/t/writing-gpu-shaders-in-julia/60669/2 "2021-05-06T17:51:34Z")

</div>

Notice that the rust code in the example looks a lot like shader language code. For some limited subset of Julia syntax, it’s probably relatively straight-forward for a macro to generate a shader langage code string, but that would likely be unsatisfying. It would be sort of like this situation [Aliases for `=` and `==` - #19 by Jeff\_Emanuel](https://discourse.julialang.org/t/aliases-for-and/60004/19)

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [May 6, 2021, 6:51pm UTC](https://discourse.julialang.org/t/writing-gpu-shaders-in-julia/60669/3 "2021-05-06T18:51:45Z")

</div>

There was a PR out for this a few years ago on VulkanCore: [WIP compile julia to spir-v by carnaval · Pull Request #4 · JuliaGPU/VulkanCore.jl · GitHub](https://github.com/JuliaGPU/VulkanCore.jl/pull/4). However, it’s a non-trival effort because LLVM doesn’t have a SPIR-V backend. oneAPI.jl works around this by using [GitHub - KhronosGroup/SPIRV-LLVM-Translator: A tool and a library for bi-directional translation between SPIR-V and LLVM IR](https://github.com/KhronosGroup/SPIRV-LLVM-Translator), but it only supports OpenCL kernels and not OpenGL/Vulkan shader SPIR-V. Rust-GPU had to essentially write their own [backend](https://github.com/EmbarkStudios/rust-gpu/tree/main/crates/rustc_codegen_spirv) from scratch.

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [May 6, 2021, 7:22pm UTC](https://discourse.julialang.org/t/writing-gpu-shaders-in-julia/60669/4 "2021-05-06T19:22:27Z")

</div>

Thanks for these links.

I suppose the best way forward is to wait for this LLVM translator to gain support for shaders, and then we can call it from julia.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [June 25, 2021, 12:14pm UTC](https://discourse.julialang.org/t/writing-gpu-shaders-in-julia/60669/5 "2021-06-25T12:14:53Z")

</div>

I’m pretty much sold on Julia good for GPUs (and TPUs), for calculations, i.e. GPGPU, I understand even the best language for.

And it also has OpenGL and Vulkan support, and I recall 99% as fast as C++ for OpenGL. Would you say as good for (low-level) graphics/shading, i.e. (scanlline) rendering?

It may not matter, as ray-tracing is taking over from scanline rendering, and I haven’t seen any discussion about using Julia for that. Also with higher-level, e.g. game engines in C++, Julia is likely not to take over from graphics soon, mayby game logic? Still curious what your feelings are about Julia (theoretical) capability for graphics, i.e. feature completeness.
