Psuedocode for anti-aliasing

I’m doing a render in studio i.a. raycasting,

Any pseudocode for something like MSAA or something?

Normally you raycast only once for each pixel. Instead, divide each pixel up in a n by n checkerboard, and do the raycast procedure for each square in the checkerboard. Then you average the n^2 color values that you get out of that to get the final color for that pixel.

2 Likes

So every pixel, to get 4x MSAA you would raycast 4x in what would be 1 pixel and average it?

A 4x MSAA uses 4 samples in the scene, so if you do a 2x2 grid then you are super-sampling in a uniform grid with the same amount of control points as a 4x MSAA. I’m not sure if MSAA uses a uniform grid.

Also note there are more grid types than uniform that you could try:
https://en.wikipedia.org/wiki/Supersampling

1 Like

If you don’t want your code to run as slow as molasses, you probably want to consider using FXAA instead of MSAA (just detect where there are edges by finding where the depth changes suddenly, and slightly blur those areas of the final image).

Can also use some kind of adaptive super-sampling: while going through the image, mark pixels that significantly differ in depth value from one of their neighbours, and perform MSAA on those pixels only as an after-pass.

That’s right… I guess since it’s a full raycaster and not rendering with triangles it’s pretty easy to make it adaptive.