Live data from Hacker News

Interactive intro to shaders

mayerowitz.io

31–40 of 72 posts

Re: Interactive intro to shaders

#31

This is neat! I've gone down the SDF rabbit hole a bit, recently. I'm glad you added some links to iq's site; he's got great stuff. I feel compelled to link to his "happy bouncing" shader, which is phenomenal IMO: https://www.shadertoy.com/view/3lsSzf (with associated 6 hour (!) youtube video on its creation) That's a juicy ~500 lines of code (:

Inigo Quilez is a wizard, the shader community owes so much to him!

Re: Interactive intro to shaders

#32
post #15

The article is quite nice. However, it glosses over the primary problem with shaders. A shader is a pain in the ass that most programs and applications don't want. 3D stuff likes triangles and the GPUs are happy to slot into that abstraction. Shaders are useful to interpolate over those triangles. Triangles are mostly garbage for everybody else. 2D rendering wants paths. Font rendering wants paths or pixmaps. GUI's w…

What part of a pixel/fragment shader does not map very well to pixmaps?

Only interpolating based on the edge? Needing to carefully map float coordinates so you don't wind up with fractional pixels mapping incorrectly? Inability to look at pixels to each side to compute something?

You can work around the limitations in vertex or fragment shaders, but, in many cases, you're having to unwind a lot of what the vertex and fragment processing does.

On of my favorite examples is drawing a screen/pixel-space rectangle and then drawing a border of the rectangle in a different color and specifying the number of pixels that border should be wide. Oof. You have to specify 4 triangles, make sure they don't overlap, specify them in a particular vertex order or annotate them with extra data so you only draw one of the three edges, make sure that you only draw each pixel just once or your blending goes all to crap, etc. Whereas, you can divide the rectangle into chunks, send each chunk through the compute pipeline, and it's stupidly straightforward.

Or, if you want simpler, just draw a line n pixels wide. If you don't have an explicit extension to do this, it's really a pain.

2D graphics simply wants very different graphic and computational primitives compared to 3D.

Re: Interactive intro to shaders

#33
post #23

Very nice, I've tried looking at a couple other tutorials in the past but they always assume more prior knowledge than I have. This really hits the sweet spot for me. Would love to see tutorials on more topics. In particular, a very basic lighting model but with a detailed breakdown on how normals and dot product work together. Yes, there's plenty of info out there, but I think your teaching style would still make it…

That's really nice to hear and it gives motivation to continue! Next article will probably go 3D, but it adds a lot of difficulties. I still need to find out a good small-scale but playful shader project to cover!

Re: Interactive intro to shaders

#34
post #15

The article is quite nice. However, it glosses over the primary problem with shaders. A shader is a pain in the ass that most programs and applications don't want. 3D stuff likes triangles and the GPUs are happy to slot into that abstraction. Shaders are useful to interpolate over those triangles. Triangles are mostly garbage for everybody else. 2D rendering wants paths. Font rendering wants paths or pixmaps. GUI's w…

Having written a 2-d curve renderer, what I want is parallel compute and high bandwidth; gpus deliver. (Especially with newer interfaces that support scatter-write; not sure how much penetration these have in the browser yet.) It's true this is not what you want at a higher level, but it serves as a fine base to implement higher-level abstractions. You could support them in hardware, but it's not at all obvious what the advantages would be; no one complains that cpus don't have architectural support for for-loops.

Edit: upon a reread, I don't really understand what your problem is with gpus. You can ignore the vertex processing pipeline entirely, drawing just a single fullscreen quad (or use a compute shader); the gpu will handle this with aplomb, and this is the sort of thing the linked article is talking about too.

Re: Interactive intro to shaders

#35

I finally found the courage to write and expose myself to the internet. I've always wanted to learn shaders so I thought it would be nice to document my learning and share it with others.

Impeccable timing, I was just about to try and get into shaders. Thank you so much for writing this, I will be reading it on the weekend.

Re: Interactive intro to shaders

#36

“their parallel nature makes them memoryless and stateless. This translates to: “You can’t store or share data between pixels or shader executions.”” This kind of constraint is so liberating for me for some reason. It just narrows the space a lot I guess. I also kind of love not having imports and libraries as that also really simplifies things.

> This kind of constraint is so liberating for me for some reason.

if you like this kind of a thing, erlang is a kind of thing you would like :o)

Re: Interactive intro to shaders

#37

I finally found the courage to write and expose myself to the internet. I've always wanted to learn shaders so I thought it would be nice to document my learning and share it with others.

Welcome to the Internet, superMayo!

If you want to see what the Masters can do with shaders, let me introduce you to Inigo Quilez and his shader art: https://www.youtube.com/watch?v=BFld4EBO2RE

EDIT: I did not notice you are the author of this article. It's very well done, and I've been looking for more approachable and interactive tutorials on the arts of shader coding.

Re: Interactive intro to shaders

#38
post #32

Earlier quoted context omitted.

What part of a pixel/fragment shader does not map very well to pixmaps?

Only interpolating based on the edge? Needing to carefully map float coordinates so you don't wind up with fractional pixels mapping incorrectly? Inability to look at pixels to each side to compute something? You can work around the limitations in vertex or fragment shaders, but, in many cases, you're having to unwind a lot of what the vertex and fragment processing does. On of my favorite examples is drawing a scree…

Interpolation works on neighboring pixels though? Not sure what you mean there. Looking at neighboring pixels of the input pixmap is trivial as well.

The fractional pixel mappings do get you - but I think I recall an integer mode somewhere in GLSL 3.2.

Your example steps outside the shader boundary, though. If you try to use triangles to draw pixmap, of course you'll have trouble. That's why pixel shaders are the right tool for the job.

The only disadvantage they have is that the pixmaps are immutable within a single pass, so you can't (easily) draw the border together with the inner rectangle in one pass. But if you're used to functional programming, you won't even notice this.

I agree that shaders are not the right thing when you need mutability, as you seem to, but working with 2D graphics doesn't necessarily require mutability.

Re: Interactive intro to shaders

#39

I finally found the courage to write and expose myself to the internet. I've always wanted to learn shaders so I thought it would be nice to document my learning and share it with others.

This is very cool, just what i needed actually.

Also your website is very sleek, minimal and your projects are tasteful.

Re: Interactive intro to shaders

#40
post #14

I finally found the courage to write and expose myself to the internet. I've always wanted to learn shaders so I thought it would be nice to document my learning and share it with others.

This article is super fun. Thanks! I was curious if you’ve learned WGSL, the shader language for WebGPU? I’m struggling to wrap my head around what’s similar and different.. a fun and interactive guide like you’ve done here with GLSL would be amazing.

https://webgpufundamentals.org/ is only a few months old and is the best WebGPU resource I've found.
Post reply on HN