Repository: vispy/vispy

Issue:
examples/demo/gloo/shadertoy.py throws up a load of warnings
```
WARNING: Variable iSampleRate is not an active uniform
WARNING: Variable iChannelTime[0] is not an active uniform
WARNING: Variable iChannelTime[1] is not an active uniform
WARNING: Variable iChannelTime[2] is not an active uniform
WARNING: Variable iChannelTime[3] is not an active uniform
WARNING: Variable iChannelResolution[0] is not an active uniform
WARNING: Variable iDate is not an active uniform
WARNING: Program has unset variables: {'iMouse'}
```

This also isn't tested by Travis for some reason or another. Probably because of the above warnings.



Discussion:
These warnings are normal actually. This example accepts any shadertoy code which may or may not implement all uniforms. I guess these warnings could be made more discrete (they are useful for debugging, not for the normal user).

Hm. I don't think we should be throwing warnings in our default demos.

Why isn't this tested by Travis, anyway?


Resolving patch:
diff --git a/examples/demo/gloo/galaxy/galaxy.py b/examples/demo/gloo/galaxy/galaxy.py
--- a/examples/demo/gloo/galaxy/galaxy.py
+++ b/examples/demo/gloo/galaxy/galaxy.py
@@ -113,34 +113,8 @@ class Canvas(app.Canvas):
 
     def __init__(self):
         # setup initial width, height
-        self.width = 800
-        self.height = 600
-        app.Canvas.__init__(self, keys='interactive')
-        self.size = self.width, self.height
+        app.Canvas.__init__(self, keys='interactive', size=(800, 600))
 
-        self._timer = app.Timer('auto', connect=self.update, start=True)
-
-    # create the numpy array corresponding to the shader data
-    def __create_galaxy_vertex_data(self):
-        data = np.zeros(len(galaxy),
-                        dtype=[('a_size', np.float32, 1),
-                               ('a_position', np.float32, 2),
-                               ('a_color_index', np.float32, 1),
-                               ('a_brightness', np.float32, 1),
-                               ('a_type', np.float32, 1)])
-
-        # see shader for parameter explanations
-        data['a_size'] = galaxy['size'] * max(self.width / 800.0,
-                                              self.height / 800.0)
-        data['a_position'] = galaxy['position'] / 13000.0
-
-        data['a_color_index'] = (galaxy['temperature'] - t0) / (t1 - t0)
-        data['a_brightness'] = galaxy['brightness']
-        data['a_type'] = galaxy['type']
-
-        return data
-
-    def on_initialize(self, event):
         # create a new shader program
         self.program = gloo.Program(VERT_SHADER, FRAG_SHADER,
                                     count=len(galaxy))
@@ -159,8 +133,8 @@ def on_initialize(self, event):
 
         self.program['u_colormap'] = colors
 
-        self.projection = perspective(45.0, self.width / float(self.height),
-                                      1.0, 1000.0)
+        w, h = self.size
+        self.projection = perspective(45.0, w / float(h), 1.0, 1000.0)
         self.program['u_projection'] = self.projection
 
         # start the galaxy to some decent point in the future
@@ -173,23 +147,37 @@ def on_initialize(self, event):
         self.program.bind(self.data_vbo)
 
         # setup blending
-        self.__setup_blending_mode()
+        gloo.set_state(clear_color=(0.0, 0.0, 0.03, 1.0),
+                       depth_test=False, blend=True,
+                       blend_func=('src_alpha', 'one'))
+
+        self._timer = app.Timer('auto', connect=self.update, start=True)
 
-    def __setup_blending_mode(self):
-        # set the clear color to almost black
-        gloo.gl.glClearColor(0.0, 0.0, 0.03, 1.0)
-        gloo.gl.glDisable(gloo.gl.GL_DEPTH_TEST)
-        # use additive blend mode so that the
-        # particles overlap and intensify
-        gloo.gl.glEnable(gloo.gl.GL_BLEND)
-        gloo.gl.glBlendFunc(gloo.gl.GL_SRC_ALPHA, gloo.gl.GL_ONE)
+    def __create_galaxy_vertex_data(self):
+        data = np.zeros(len(galaxy),
+                        dtype=[('a_size', np.float32, 1),
+                               ('a_position', np.float32, 2),
+                               ('a_color_index', np.float32, 1),
+                               ('a_brightness', np.float32, 1),
+                               ('a_type', np.float32, 1)])
+
+        # see shader for parameter explanations
+        pw, ph = self.physical_size
+        data['a_size'] = galaxy['size'] * max(pw / 800.0, ph / 800.0)
+        data['a_position'] = galaxy['position'] / 13000.0
+
+        data['a_color_index'] = (galaxy['temperature'] - t0) / (t1 - t0)
+        data['a_brightness'] = galaxy['brightness']
+        data['a_type'] = galaxy['type']
+
+        return data
 
     def on_resize(self, event):
-        self.width, self.height = event.size
         # setup the new viewport
-        gloo.set_viewport(0, 0, self.width, self.height)
+        gloo.set_viewport(0, 0, *event.physical_size)
         # recompute the projection matrix
-        self.projection = perspective(45.0, self.width / float(self.height),
+        w, h = event.size
+        self.projection = perspective(45.0, w / float(h),
                                       1.0, 1000.0)
         self.program['u_projection'] = self.projection
 
diff --git a/examples/demo/gloo/shadertoy.py b/examples/demo/gloo/shadertoy.py
--- a/examples/demo/gloo/shadertoy.py
+++ b/examples/demo/gloo/shadertoy.py
@@ -13,6 +13,10 @@
 
 """
 
+# NOTE: This example throws warnings about variables not being used;
+# this is normal because only some shadertoy examples make use of all
+# variables, and the GPU may compile some of them away.
+
 import sys
 from datetime import datetime, time
 import numpy as np
@@ -43,7 +47,7 @@
 uniform sampler2D iChannel2;             // input channel. XX = 2D/Cube
 uniform sampler2D iChannel3;             // input channel. XX = 2D/Cube
 uniform vec3      iChannelResolution[4]; // channel resolution (in pixels)
-uniform float     iChannelTime[4];       // channel playback time (in seconds)
+uniform float     iChannelTime[4];       // channel playback time (in sec)
 
 %s
 """
@@ -79,6 +83,7 @@ def __init__(self, shadertoy=None):
 
         self.program["position"] = [(-1, -1), (-1, 1), (1, 1),
                                     (-1, -1), (1, 1), (1, -1)]
+        self.program['iMouse'] = 0, 0, 0, 0
 
         self.program['iSampleRate'] = 44100.
         for i in range(4):
@@ -114,7 +119,7 @@ def on_mouse_move(self, event):
 
     def on_timer(self, event):
         self.program['iGlobalTime'] = event.elapsed
-        self.program['iDate'] = get_idate()  # used in some shadertoy examples
+        self.program['iDate'] = get_idate()  # used in some shadertoy exs
         self.update()
 
     def on_resize(self, event):
diff --git a/examples/tutorial/app/app_events.py b/examples/tutorial/app/app_events.py
--- a/examples/tutorial/app/app_events.py
+++ b/examples/tutorial/app/app_events.py
@@ -16,9 +16,6 @@ def __init__(self, *args, **kwargs):
         app.Canvas.__init__(self, *args, **kwargs)
         self.title = 'App demo'
 
-    def on_initialize(self, event):
-        print('initializing!')
-
     def on_close(self, event):
         print('closing!')
 


Test patch:
diff --git a/examples/benchmark/scene_test_1.py b/examples/benchmark/scene_test_1.py
--- a/examples/benchmark/scene_test_1.py
+++ b/examples/benchmark/scene_test_1.py
@@ -96,15 +96,12 @@ def __init__(self, **kwargs):
 
         self.width, self.height = self.size
         gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1])
+        gloo.set_state(clear_color='black', blend=True,
+                       blend_func=('src_alpha', 'one_minus_src_alpha'))
 
         self._tr = TransformSystem(self)
-
         self.show()
 
-    def on_initialize(self, event):
-        gloo.set_state(clear_color='black', blend=True,
-                       blend_func=('src_alpha', 'one_minus_src_alpha'))
-
     def on_resize(self, event):
         self.width, self.height = event.size
         gloo.set_viewport(0, 0, event.physical_size[0], event.physical_size[1])
@@ -276,7 +273,7 @@ def __init__(self, data):
         self._y_transform = y_transform
 
         colormap = Function(DISCRETE_CMAP)
-        cmap = np.random.uniform(size=(1, nsignals, 3), 
+        cmap = np.random.uniform(size=(1, nsignals, 3),
                                  low=.5, high=.9).astype(np.float32)
         tex = gloo.Texture2D((cmap * 255).astype(np.uint8))
         colormap['colormap'] = Variable('uniform sampler2D u_colormap', tex)
@@ -324,21 +321,21 @@ class Signals(SignalsVisual, scene.visuals.Node):
         v_index = a_index;
     }
     """
-    
+
     def draw(self, transform_system):
         self._program.vert['transform'] = transform_system.get_full_transform()
         self._program.draw('line_strip')
-    
+
 
 if __name__ == '__main__':
     data = np.random.normal(size=(128, 1000)).astype(np.float32)
-    
-    pzcanvas = PanZoomCanvas(position=(400, 300), size=(800, 600), 
+
+    pzcanvas = PanZoomCanvas(position=(400, 300), size=(800, 600),
                              title="PanZoomCanvas")
     visual = SignalsVisual(data)
     pzcanvas.add_visual('signal', visual)
-    
-    scanvas = scene.SceneCanvas(show=True, keys='interactive', 
+
+    scanvas = scene.SceneCanvas(show=True, keys='interactive',
                                 title="SceneCanvas")
     svisual = Signals(data)
     view = scanvas.central_widget.add_view()