Repository: vispy/vispy

Issue:
Create a repository of common shaders or shader templates
The idea is to have a library of common shaders or shader snippets that would save us a lot of typing. For example, instead of writing trivial shaders such as

``` c
void main(){
    gl_FragColor = vec4(1., 1., 1., 1.);
}
```

we could just write something like `vispy.shaderlib.frag_constant(color)`, etc. Idem for simple vertex shaders with just an attribute.

There could be more complex snippets too, to sample from a texture in the fragment shader, to lookup a color in a texture colormap, etc. For 3D, we could have common lighting equations, model-view-projection matrices, etc.

This could be in `vispy.shaders`, `vispy.shaderlib`, `vispy.shaders.lib`, or something else.



Discussion:
We currently have `vispy.scene.shaders`, which implements the shader function system. That might be the best place to begin accumulating useful functions. 

Not sure, this is the shader function system, which is distinct from a collection of reusable shaders (I think these should be strings rather than `Function` instances). @rougier puts them in [`glumpy.shaders`](https://github.com/rougier/glumpy/tree/master/glumpy/shaders), maybe we can use something similar (although `shaders` conflicts with `scene.shaders`).

`vispy.glsl` would be a reasonable place to house basic / fundamental `glsl` code snippets

And would they be `.glsl` files or `.py` files with a bunch of multi-line strings in them? Since we are talking of loads of snippets, I think I vote for the latter.

`.py` files would make for convenient organizing methods, so +1 for that


Resolving patch:
diff --git a/vispy/visuals/glsl/__init__.py b/vispy/visuals/glsl/__init__.py
new file mode 100644
--- /dev/null
+++ b/vispy/visuals/glsl/__init__.py
@@ -0,0 +1 @@
+"""Repository of common GLSL functions."""
diff --git a/vispy/visuals/glsl/antialiasing.py b/vispy/visuals/glsl/antialiasing.py
new file mode 100644
--- /dev/null
+++ b/vispy/visuals/glsl/antialiasing.py
@@ -0,0 +1,153 @@
+# Copyright (c) 2014, Nicolas P. Rougier. All Rights Reserved.
+# Distributed under the (new) BSD License.
+
+"""Antialiasing GLSL functions."""
+
+
+# -----------------------------------------------------------------------------
+# Stroke
+# -----------------------------------------------------------------------------
+
+"""Compute antialiased fragment color for a stroke line.
+
+Inputs
+------
+
+distance (float): Signed distance to border (in pixels).
+
+
+Template variables
+------------------
+
+linewidth (float): Stroke line width (in pixels).
+
+antialias (float): Stroke antialiased area (in pixels).
+
+stroke (vec4): Stroke color.
+
+
+Outputs
+-------
+color (vec4): The final color.
+
+
+"""
+ANTIALIAS_STROKE = """
+vec4 stroke(float distance)
+{
+    vec4 frag_color;
+    float t = $linewidth/2.0 - $antialias;
+    float signed_distance = distance;
+    float border_distance = abs(signed_distance) - t;
+    float alpha = border_distance/$antialias;
+    alpha = exp(-alpha*alpha);
+
+    if( border_distance < 0.0 )
+        frag_color = $stroke;
+    else
+        frag_color = vec4($stroke.rgb, $stroke.a * alpha);
+
+    return frag_color;
+}
+"""
+
+
+# -----------------------------------------------------------------------------
+# Stroke
+# -----------------------------------------------------------------------------
+
+"""Compute antialiased fragment color for an outlined shape.
+
+Inputs
+------
+
+distance (float): Signed distance to border (in pixels).
+
+
+Template variables
+------------------
+
+linewidth (float): Stroke line width (in pixels).
+
+antialias (float): Stroke antialiased area (in pixels).
+
+stroke (vec4): Stroke color.
+
+fill (vec4): Fill color.
+
+
+Outputs
+-------
+color (vec4): The final color.
+
+
+"""
+ANTIALIAS_OUTLINE = """
+vec4 outline(float distance)
+{
+    vec4 frag_color;
+    float t = $linewidth/2.0 - $antialias;
+    float signed_distance = distance;
+    float border_distance = abs(signed_distance) - t;
+    float alpha = border_distance/$antialias;
+    alpha = exp(-alpha*alpha);
+
+    if( border_distance < 0.0 )
+        frag_color = $stroke;
+    else if( signed_distance < 0.0 )
+        frag_color = mix($fill, $stroke, sqrt(alpha));
+    else
+        frag_color = vec4($stroke.rgb, $stroke.a * alpha);
+    return frag_color;
+}
+"""
+
+
+# -----------------------------------------------------------------------------
+# Filled
+# -----------------------------------------------------------------------------
+
+"""Compute antialiased fragment color for a filled shape.
+
+Inputs
+------
+
+distance (float): Signed distance to border (in pixels).
+
+
+Template variables
+------------------
+
+linewidth (float): Stroke line width (in pixels).
+
+antialias (float): Stroke antialiased area (in pixels).
+
+fill (vec4): Fill color.
+
+
+Outputs
+-------
+color (vec4): The final color.
+
+
+"""
+ANTIALIAS_FILLED = """
+vec4 filled(float distance)
+{
+    vec4 frag_color;
+    float t = $linewidth/2.0 - $antialias;
+    float signed_distance = distance;
+    float border_distance = abs(signed_distance) - t;
+    float alpha = border_distance/$antialias;
+    alpha = exp(-alpha*alpha);
+
+    if( border_distance < 0.0 )
+        frag_color = $fill;
+    else if( signed_distance < 0.0 )
+        frag_color = $fill;
+    else
+        frag_color = vec4($fill.rgb, alpha * $fill.a);
+
+    return frag_color;
+}
+"""
diff --git a/vispy/visuals/glsl/color.py b/vispy/visuals/glsl/color.py
new file mode 100644
--- /dev/null
+++ b/vispy/visuals/glsl/color.py
@@ -0,0 +1,40 @@
+"""Color-related GLSL functions."""
+
+
+# -----------------------------------------------------------------------------
+# Colormaps
+# -----------------------------------------------------------------------------
+
+"""Texture lookup for a discrete color map stored in a 1*ncolors 2D texture.
+
+The `get_color()` function returns a RGB color from an index integer
+referring to the colormap.
+
+
+Inputs
+------
+
+index (int): The color index.
+
+
+Template variables
+------------------
+
+$ncolors (int): The number of colors in the colormap.
+
+$colormap (2D texture sampler): The sampler for the 2D 1*ncolors colormap
+    texture.
+
+
+Outputs
+-------
+
+color (vec3): The color.
+
+"""
+COLORMAP_TEXTURE = """
+vec3 get_color(int index) {
+    float x = (float(index) + .5) / float($ncolors);
+    return texture2D($colormap, vec2(x, .5)).rgb;
+}
+"""


Test patch: