Stream: beginners

Topic: Shaders in Roc?


view this post on Zulip Francesco Orsenigo (Feb 23 2022 at 14:23):

I assume that writing graphics/hardware-acceleration shaders is not a goal of Roc, at least in such early phase.

However, I was wondering if in principle Roc can express highly performant shaders.

Would be possible to implement a Spir-V/HLSL/Metal emitter without having to write crazy dedicated optimizations?

To me this would be very interesting because it would allow sharing code between the host application and the shader, allowing both more consistent code and better shader tooling.

As far as I know, Rust is the only language attempting to do this.

view this post on Zulip Anton (Feb 23 2022 at 14:42):

I don't know the answer but I wanted to link the rust project I assume you're talking about: rust-gpu. I also found this Spir-V gen WIP in futhark that could be relevant.

view this post on Zulip Jorge Acereda (Feb 23 2022 at 18:25):

Scopes also generates Spir-V: https://hg.sr.ht/~duangle/scopes https://www.youtube.com/watch?v=lUQpaa3bd54

view this post on Zulip Brendan Hansknecht (Feb 23 2022 at 18:43):

I would assume that roc would be a rough language for shaders. It doesn't have the right native concepts (matrices, implicit parallelism, lots of math calculations, GPU style control flow). I think something like futhark will always be a better choice.

Not saying it wouldn't be possible. Just that it would be a shimmed in afterthought.

view this post on Zulip Francesco Orsenigo (Feb 24 2022 at 05:00):

Brendan Hansknecht said:

I would assume that roc would be a rough language for shaders. It doesn't have the right native concepts (matrices, implicit parallelism, lots of math calculations, GPU style control flow). I think something like futhark will always be a better choice.

Can I pick your brain a bit more on this?

I was thinking that a limit would be the impossibility to express efficient shader code without explicit mutability (I understand that Roc is immutable and the only mutability happens "under the hood")

view this post on Zulip Brendan Hansknecht (Feb 24 2022 at 07:44):

I'll just start by saying that I only have limited knowledge in this area. I have much more knowledge about tensor compilation and hardware, but that is not the same.

The huge gain of directly representating vectors, matrices, and tensors is in easy extraction of parallelism. When optimizing at the matrix operation level, it is much much easy to know the goal, combine ops, reduce copying, and run in parallel. To do a matrix multiplication in roc would take recursive functions and rough to follow logic. The compiler would need to recognize that your recursive function was really a matrix multiply in order to extract the parallelism. That is much harder goal for the compiler and generally does not work well.

Sure with roc, every function is pure. So if you want to compile a function that runs on each pixel, the functions could be run in parallel. But generally, you don't really want a function per pixel, you want one merged function that operates on all pixels. It might need to do masking and smart temporary creation in order to not waste tons of operations recomputing shared things between the per pixel functions. So in this case, roc may work, but likely still a lot of optimizer work to really build the desired output kernel.

For GPU style control flow, I mean that GPUs have very limited control flow and tend to do a lot of extra work with masking as opposed to doing actual control flow. Mapping a when or if to a GPU wrong could totally ruin performance.

view this post on Zulip Brendan Hansknecht (Feb 24 2022 at 07:45):

Also, I would highly advise looking into futhark. It has a lot of the concepts that I mentioned here. Looking at it should show what is missing in Roc.

view this post on Zulip Francesco Orsenigo (Feb 24 2022 at 08:17):

Thank you, I think I have a better idea.
I'm designing my language to have linear algebra as first-class citizen, and I assume Roc could do something similar.
Control flows are a problem and I've been thinking about this for a long time.
I'll check Futhark.


Last updated: Jul 06 2025 at 12:14 UTC