I enjoyed @jam2go's video about stylized depth-of-field bokeh where instead of getting blurrier, it looks like it gets more and more JPEG artifacts. But it bothered me that it wasn't emulating JPEG compression directly β it artistically mimics the visual aspects of JPEG compression instead1.
So, here's a Shadertoy that models JPEG compression more closely! It simulates JPEG's frequency-domain quantization, YCoCg transform, and (sort of) 4:2:0 chroma subsampling, in 4 shader passes2. It also has versions of a few of the other stylized depth-of-field effects from @jam2go's video!
The scene here is @blackle's Whack-A-Mole Pistons, modified to add environment lighting. I've included a few links to other JPEG shaders and resources in the shader code comments as well.
-
Which is likely a good choice in production, of course! By focusing on the artistic rather than technical aspects of JPEG compression, you can likely write a faster shader and have more control over the output. @ompuco's JPGPU is sort of a middle ground β it lets you also control the block size and resolution and has more options for the DCT quantization stage, but still simulates JPEG's core algorithm.
-
This could probably be reduced to 1 if each thread did 64 texture reads in one pass instead of 8 texture reads in 4 passes. Or one might use shared memory and warp operations in a single pass if the rendering API and platform supports it; then each thread could only perform 1 texture read, and it would probably be faster. I've included some notes in the shader comments!