[Mesa-dev] [RFC shader-db] Add support for shadertoy tests

Rob Clark robdclark at gmail.com
Tue Jun 16 12:46:50 PDT 2015


Attached script grabs shaders from shadertoy, and dumps them out as
.shader_test files which can be run through shader-db for compiler
testing.

shadertoy only gives you a fragment shader (which works based on
gl_FragCoord), so a generic vertex shader is used.  And a blurb is
inserted for the pre-defined uniforms and main() function (which just
calls shadertoy mainImage() fxn).

---
TODO I guess we'd actually have to parse the shader to figure out if
the sampler uniforms were meant to be 2D/cube/etc.  Maybe we just
commit samplers we get from the script and massage them by hand?

PS. don't make fun of my py too much.. I'm a newb and figuring it
out as I go

 grab-shadertoy.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100755 grab-shadertoy.py

diff --git a/grab-shadertoy.py b/grab-shadertoy.py
new file mode 100755
index 0000000..74e9d10
--- /dev/null
+++ b/grab-shadertoy.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+
+import requests, json
+
+url = 'https://www.shadertoy.com/api/v1/shaders'
+key = '?key=NdnKw7'
+
+# Get the list of shaders
+r = requests.get(url + key)
+j = r.json()
+print('Found ' + str(j['Shaders']) + ' shaders')
+
+shader_ids = j['Results']
+for id in shader_ids:
+    print('Fetching shader: ' + str(id))
+    r = requests.get(url + '/' + id + key)
+    j = r.json()
+    s = j['Shader']
+    info = s['info']
+    print('Name: ' + info['name'])
+    print('Description: ' + info['description'])
+    i = 0;
+    for p in s['renderpass']:
+        fobj = open('shaders/shadertoy/' + str(id) + '_' + str(i) + '.shader_test', 'w')
+        #print('Inputs: ' + str(p['inputs']))
+        #print('Outputs: ' + str(p['outputs']))
+        fobj.write('[require]\n')
+        fobj.write('GLSL >= 1.30\n')
+        fobj.write('\n');
+        fobj.write('[fragment shader]\n')
+        fobj.write('#version 130\n')
+        # Shadertoy inserts some uniforms, so we need to do the same:
+        fobj.write('uniform vec3      iResolution;\n');
+        fobj.write('uniform float     iGlobalTime;\n');
+        fobj.write('uniform float     iChannelTime[4];\n');
+        fobj.write('uniform vec4      iMouse;\n');
+        fobj.write('uniform vec4      iDate;\n');
+        fobj.write('uniform float     iSampleRate;\n');
+        fobj.write('uniform vec3      iChannelResolution[4];\n');
+        # TODO probably need to parse the shader to figure out if 2d/cubemap/etc
+        fobj.write('uniform sampler2D iChannel0;\n');
+        fobj.write('uniform sampler2D iChannel1;\n');
+        fobj.write('uniform sampler2D iChannel2;\n');
+        fobj.write('uniform sampler2D iChannel3;\n');
+        # Actual shadertoy shader body:
+        fobj.write(p['code'])
+        # Shadertoy shader uses mainImage(out vec4 fragColor, in vec2 fragCoord)
+        # so we need to insert a main:
+        fobj.write('\nvoid main() { mainImage(gl_FragColor, gl_FragCoord.xy); }\n')
+        fobj.write('\n\n')
+        # And a generic vertex shader:
+        fobj.write('[vertex shader]\n')
+        fobj.write('#version 130\n')
+        fobj.write('in vec2 position;\n')
+        fobj.write('\n')
+        fobj.write('void main()\n')
+        fobj.write('{\n')
+        fobj.write('   gl_Position = vec4(position, 0.0, 1.0);\n')
+        fobj.write('}\n')
+
+        fobj.close()
+        i = 1 + i
-- 
2.4.2



More information about the mesa-dev mailing list