I created a shader quine, a fragment shader without input (textures or models) that produces a copy of its own source code as its output, on Shadertoy.

You can find (the full source of) the fragment shader here: https://www.shadertoy.com/view/MlGcRz.

The ‘trick’ of this shader is contained in the data of array c. The shader can decode array c to get font-data and display characters, decode a part of the data that contains the shader code itself, and finally, the shader is also able to display the data of the array as the data itself:

int y, i;ivec2 d;int[]c=int[116](0x007a995e,0x0083f840,0x009a9c40,0x006e5840,
0x0043f4dc,0x006659c0,0x0066595e,0x000c5661,0x006a595a,0x007a9a66,0x00f14938,
0x0062493f,0x00924918,0x00fe4918,0x00a2cb18,0x00145f84,0x3813813c,0x0003d100,
0x0087f840,0x0066bb5a,0x0085e000,0x00330604,0x00010800,0x00020000,0x00624918,
0x00f2081c,0x00024784,0x0001e840,0x0085e100,0x000047a1,0x00014800,0x00894200,
0x00214880,0x0023e208,0x00008208,0x00f2081c,0x00024784,0x0087f000,0x0003f840,
0x00918624,0x000ccc00,0x00916724,0x00a3b9d8,0x00514514,0x00310a24,0x00e0423c,
0x001a9080,0x00024000,0x00c766e3,0x00c8d17f,0x0052ca00,0x0083f040,0x0003f000,
0x0000413c,0x00000000,0x2cd9ab51,0x11791d96,0x3608c395,0x1ab5178d,0x11acc9a5,
0x010656ad,0xfefd4986,0x1161579b,0x11290d8d,0x132904ad,0x1a65850e,0x0430e576,
0x0e5562b6,0x1b2f608c,0x1546b35c,0x0b50230e,0x314569d7,0x19cd8c8e,0x17b5845a,
0x2c5cb8ac,0x0daec79b,0x211689d7,0x08a2c5cd,0x0d5871e9,0x0e551af0,0x1615408c,
0x2b29e6c8,0x1410c395,0x0582c5cd,0x275cbd34,0x171480e0,0x0182cd34,0x00b841c5,
0x1fb2532f,0x2532e0c3,0x05845a2c,0x2c820985,0x2a1a9170,0x20b2f0c6,0x2e1c00c1,
0x2b45494c,0x000c18ac,0x211686c8,0x20826086,0x06a45c11,0x14bc31aa,0x2b454aec,
0x1b0c38ac,0x2b6c1070,0x2f02e02b,0x2e06baec,0x2bb2f243,0x02b8006b,0x1194cbc2,
0x20981068,0x2c8860e0,0x0506a129,0x17360826,0x0d846a67,0x1b06ab17,0xfefbf75e)
;void mainImage(out vec4 a,vec2 b){d=ivec2(b.x,iResolution.y-b.y);y=d.x/5+d.y
/8*77,d%=ivec2(5,8);a=vec4(d.y>5||b.x>385.||y>1574?0:c[y<33?c[y/5+55]>>y%5*6&
63:y>1307?c[(i=y-1308)/5+62]>>i%5*6&63:(y=(i=y-33)%11)==0?0:y==1?39:y==10?22:
c[i/11]>>36-y*4&15]>>d.x*6+d.y&1);}Code language: GLSL (glsl)

A much better, more extensive, and very readable explanation of (a slightly longer version of) this shader can be found in this blog post: “Dissecting a Shader Quine“.

Similar posts

If you like this post, you may also like one of my other posts:

A shader quine