Beispiel #1
0
 def test_detach(self):
     vert = VertexShader("A")
     frag = FragmentShader("B")
     program = Program(vert, frag)
     program.detach(frag)
     assert len(program.shaders) == 1
     assert program.shaders[0].code == "A"
Beispiel #2
0
    def test_setitem(self):
        vert = VertexShader("")
        frag = FragmentShader("")

        program = Program(vert, frag)
        with self.assertRaises(NameError):
            program["A"] = 1
Beispiel #3
0
 def test_uniform(self):
     vert = VertexShader("uniform float A;")
     frag = FragmentShader("uniform float A; uniform vec4 B;")
     program = Program(vert, frag)
     assert ("A", gl.GL_FLOAT) in program.all_uniforms
     assert ("B", gl.GL_FLOAT_VEC4) in program.all_uniforms
     assert len(program.all_uniforms) == 2
 def test_init(self):
     shader = VertexShader()
     assert shader._handle == 0
     assert shader._need_update is False
     assert shader._valid is False
     assert shader.code is None
     assert shader.source is None
Beispiel #5
0
    def test_set_attribute_vec4(self):
        vert = VertexShader("attribute vec4 color;")
        frag = FragmentShader("")

        program = Program(vert, frag)
        with self.assertRaises(ValueError):
            program["color"] = np.array(3, dtype=np.float32)

        program = Program(vert, frag)
        with self.assertRaises(ValueError):
            program["color"] = np.array((100, 5), dtype=np.float32)

        program = Program(vert, frag)
        program["color"] = ClientVertexBuffer(
            np.zeros((100, 4), dtype=np.float32))
        assert program._attributes["color"].count == 100

        program = Program(vert, frag)
        program["color"] = ClientVertexBuffer(
            np.zeros((100, 1, 4), dtype=np.float32))
        assert program._attributes["color"].count == 100

        program = Program(vert, frag)
        program["color"] = ClientVertexBuffer(
            np.zeros(100, dtype=(np.float32, 4)))
        assert program._attributes["color"].count == 100
Beispiel #6
0
 def test_uniform(self):
     vert = VertexShader("uniform float A;")
     frag = FragmentShader("uniform float A; uniform vec4 B;")
     program = Program(vert, frag)
     assert program.uniforms[0].name == 'A'
     assert program.uniforms[0].gtype == gl.GL_FLOAT
     assert program.uniforms[1].name == 'B'
     assert program.uniforms[1].gtype == gl.GL_FLOAT_VEC4
Beispiel #7
0
 def test_attach(self):
     vert = VertexShader("A")
     frag = FragmentShader("B")
     program = Program(vert)
     program.attach(frag)
     assert len(program.shaders) == 2
     assert program.shaders[0].code == "A"
     assert program.shaders[1].code == "B"
Beispiel #8
0
    def test_setitem(self):
        vert = VertexShader("")
        frag = FragmentShader("")

        program = Program(vert, frag)

        def modifier(p):
            p["A"] = 1
        self.assertRaises(NameError, modifier, program)
Beispiel #9
0
    def test_failed_build(self):
        vert = VertexShader("A")
        frag = FragmentShader("B")

        program = Program(vert=vert)
        self.assertRaises(RuntimeError, program.activate)

        program = Program(frag=frag)
        self.assertRaises(RuntimeError, program.activate)
Beispiel #10
0
    def test_setitem(self):
        vert = VertexShader("")
        frag = FragmentShader("")

        program = Program(vert, frag)
        #with self.assertRaises(ValueError):
        #    program["A"] = 1
        with use_log_level('error', record=True, print_msg=False):
            self.assertRaises(KeyError, program.__setitem__, "A", 1)
Beispiel #11
0
    def test_failed_build(self):
        vert = VertexShader("A")
        frag = FragmentShader("B")

        program = Program(vert=vert)
        program._need_create = False  # fool program that it already exists
        self.assertRaises(ValueError, program.activate)

        program = Program(frag=frag)
        program._need_create = False  # fool program that it already exists
        self.assertRaises(ValueError, program.activate)
Beispiel #12
0
 def test_ignore_comments(self):
     shader = VertexShader("""
         attribute vec4 color; attribute float x;
         // attribute float y;
         attribute float z; //attribute float w;
     """)
     names = [attr[0] for attr in shader.attributes]
     assert_in("color", names)
     assert_in("x", names)
     assert_in("z", names)
     assert_not_in("y", names)
     assert_not_in("w", names)
Beispiel #13
0
 def test_set_vars(self):
     vert = VertexShader("attribute vec4 color;")
     frag = FragmentShader("")
     program = Program(vert, frag)
     arr = np.array((100, 5), dtype=np.float32)
     assert_raises(ValueError, program.set_vars, arr)
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color', np.float32, 4)])
     data = np.zeros(100, dtype=dtype)
     arr = VertexBuffer(data)
     program.set_vars(arr)
     assert_raises(TypeError, program.set_vars, 'hello')
     program.set_vars(dict(color=arr, fake=arr))
Beispiel #14
0
    def test_set_attribute_float(self):

        vert = VertexShader("attribute float f;")
        frag = FragmentShader("")

        program = Program(vert, frag)
        program["f"] = VertexBuffer(np.zeros(100, dtype=np.float32))
        assert program._attributes["f"].count == 100

        program = Program(vert, frag)
        program["f"] = ClientVertexBuffer(
            np.zeros((100, 1, 1), dtype=np.float32))
        assert program._attributes["f"].count == 100

        program = Program(vert, frag)
        with self.assertRaises(ValueError):
            program["f"] = np.zeros((100, 1, 1), dtype=np.float32)
Beispiel #15
0
    def test_set_attribute_float(self):

        vert = VertexShader("attribute float f;")
        frag = FragmentShader("")

        program = Program(vert, frag)
        program["f"] = VertexBuffer(np.zeros(100, dtype=np.float32))
        assert program._attributes["f"].count == 100

        program = Program(vert, frag)
        program.set_vars(f=ClientVertexBuffer(np.zeros((100, 1, 1),
                                                       dtype=np.float32)))
        assert_raises(NameError, program.set_vars, junk='foo')
        assert program._attributes["f"].count == 100

        program = Program(vert, frag)

        def modifier(p):
            p["f"] = np.zeros((100, 1, 1), dtype=np.float32)
        self.assertRaises(ValueError, modifier, program)
Beispiel #16
0
 def test_uniform_array(self):
     shader = VertexShader("uniform float color[2];")
     uniforms = shader._get_uniforms()
     assert uniforms == [("color[0]", gl.GL_FLOAT),
                         ("color[1]", gl.GL_FLOAT)]
Beispiel #17
0
 def test_attribute_float(self):
     shader = VertexShader("attribute float color;")
     attributes = shader._get_attributes()
     assert attributes == [("color", gl.GL_FLOAT)]
Beispiel #18
0
 def test_attribute_vec4(self):
     shader = VertexShader("attribute vec4 color;")
     attributes = shader._get_attributes()
     assert attributes == [("color", gl.GL_FLOAT_VEC4)]
Beispiel #19
0
 def test_attributes(self):
     vert = VertexShader("attribute float A;")
     frag = FragmentShader("")
     program = Program(vert, frag)
     assert program.attributes[0].name == 'A'
     assert program.attributes[0].gtype == gl.GL_FLOAT
 def test_setcode(self):
     shader = VertexShader()
     shader.set_code("")
     assert shader._need_update
Beispiel #21
0
 def test_uniform_vec4(self):
     shader = VertexShader("uniform vec4 color;")
     uniforms = shader._get_uniforms()
     assert uniforms == [("color", gl.GL_FLOAT_VEC4)]
Beispiel #22
0
 def test_delete_no_context(self):
     shader = VertexShader()
     shader.delete()
Beispiel #23
0
    def test_set_uniform_vec4(self):
        vert = VertexShader("uniform vec4 color;")
        frag = FragmentShader("")

        program = Program(vert, frag)
        program["color"] = 1, 1, 1, 1
Beispiel #24
0
def _test_application(backend):
    """Test application running"""
    app = Application()
    assert_raises(ValueError, app.use, 'foo')
    app.use(backend)
    wrong = 'Glut' if app.backend_name != 'Glut' else 'Pyglet'
    assert_raises(RuntimeError, app.use, wrong)
    app.process_events()
    if backend is not None:
        # "in" b/c "qt" in "PySide (qt)"
        assert_true(backend in app.backend_name)
    print(app)  # test __repr__

    # Canvas
    pos = [0, 0, 1, 1]
    # Use "with" statement so failures don't leave open window
    # (and test context manager behavior)
    with Canvas(title='me', app=app, show=True, position=pos) as canvas:
        assert_true(canvas.app is app)
        assert_true(canvas.native)
        print(canvas.size >= (1, 1))
        canvas.resize(90, 90)
        canvas.move(1, 1)
        assert_equal(canvas.title, 'me')
        canvas.title = 'you'
        canvas.position = (0, 0)
        canvas.size = (100, 100)
        canvas.connect(on_mouse_move)
        assert_raises(ValueError, canvas.connect, _on_mouse_move)
        canvas.show()
        assert_raises(ValueError, canvas.connect, on_nonexist)

        # screenshots
        ss = _screenshot()
        assert_array_equal(ss.shape[2], 3)  # XXX other dimensions not correct?
        # XXX it would be good to do real checks, but sometimes the
        # repositionings don't "take" (i.e., lead to random errors)
        assert_equal(len(canvas._backend._vispy_get_geometry()), 4)
        assert_equal(len(canvas.size), 2)
        assert_equal(len(canvas.position), 2)

        # GLOO: should have an OpenGL context already, so these should work
        vert = VertexShader("void main (void) {gl_Position = pos;}")
        frag = FragmentShader("void main (void) {gl_FragColor = pos;}")
        program = Program(vert, frag)
        assert_raises(ShaderError, program.activate)

        vert = VertexShader("uniform vec4 pos;"
                            "void main (void) {gl_Position = pos;}")
        frag = FragmentShader("uniform vec4 pos;"
                              "void main (void) {gl_FragColor = pos;}")
        program = Program(vert, frag)
        uniform = program.uniforms[0]
        uniform.set_data([1, 2, 3, 4])
        program.activate()  # should print
        uniform.upload(program)
        program.detach(vert, frag)
        assert_raises(ShaderError, program.detach, vert)
        assert_raises(ShaderError, program.detach, frag)

        vert = VertexShader("attribute vec4 pos;"
                            "void main (void) {gl_Position = pos;}")
        frag = FragmentShader("void main (void) {}")
        program = Program(vert, frag)
        attribute = program.attributes[0]
        attribute.set_data([1, 2, 3, 4])
        program.activate()
        attribute.upload(program)
        # cannot get element count
        assert_raises(ProgramError, program.draw, 'POINTS')

        # use a real program
        vert = ("uniform mat4 u_model;"
                "attribute vec2 a_position; attribute vec4 a_color;"
                "varying vec4 v_color;"
                "void main (void) {v_color = a_color;"
                "gl_Position = u_model * vec4(a_position, 0.0, 1.0);"
                "v_color = a_color;}")
        frag = "void main() {gl_FragColor = vec4(0, 0, 0, 1);}"
        n, p = 250, 50
        T = np.random.uniform(0, 2 * np.pi, n)
        position = np.zeros((n, 2), dtype=np.float32)
        position[:, 0] = np.cos(T)
        position[:, 1] = np.sin(T)
        color = np.ones((n, 4), dtype=np.float32) * (1, 1, 1, 1)
        data = np.zeros(n * p, [('a_position', np.float32, 2),
                                ('a_color', np.float32, 4)])
        data['a_position'] = np.repeat(position, p, axis=0)
        data['a_color'] = np.repeat(color, p, axis=0)

        program = Program(vert, frag)
        program.set_vars(VertexBuffer(data))
        program['u_model'] = np.eye(4, dtype=np.float32)
        program.draw('POINTS')  # different codepath if no call to activate()
        subset = ElementBuffer(np.arange(10, dtype=np.uint32))
        program.draw('POINTS', subset=subset)

        # bad programs
        frag_bad = ("varying vec4 v_colors")  # no semicolon
        program = Program(vert, frag_bad)
        assert_raises(ShaderError, program.activate)
        frag_bad = None  # no fragment code. no main is not always enough
        program = Program(vert, frag_bad)
        assert_raises(ProgramError, program.activate)

        # Timer
        timer = Timer(interval=0.001, connect=on_mouse_move, iterations=2,
                      start=True, app=app)
        timer.start()
        timer.interval = 0.002
        assert_equal(timer.interval, 0.002)
        assert_true(timer.running)
        timer.stop()
        assert_true(not timer.running)
        assert_true(timer.native)
        timer.disconnect()

        # test that callbacks take reasonable inputs
        _test_callbacks(canvas)

        # cleanup
        canvas.swap_buffers()
        canvas.update()
        # put this in even though __exit__ will call it to make sure we don't
        # have problems calling it multiple times
        canvas.close()
    app.quit()
    app.quit()  # make sure it doesn't break if a user does something silly
 def test_delete_no_context(self):
     shader = VertexShader()
     shader.delete()
 def test_init(self):
     shader = VertexShader()
     assert shader._target == gl.GL_VERTEX_SHADER
 def test_uniform_vec4(self):
     shader = VertexShader("uniform vec4 color;")
     uniforms = shader._get_uniforms()
     assert uniforms == [("color", gl.GL_FLOAT_VEC4)]
 def test_sourcecode(self):
     code = "/* Code */"
     shader = VertexShader(code)
     assert shader.code == code
     assert shader.source == "<string>"
 def test_attribute_float(self):
     shader = VertexShader("attribute float color;")
     attributes = shader._get_attributes()
     assert attributes == [("color", gl.GL_FLOAT)]
 def test_empty_build(self):
     shader = VertexShader()
     assert shader._code is None
Beispiel #31
0
 def test_uniform_float(self):
     shader = VertexShader("uniform float color;")
     uniforms = shader._get_uniforms()
     assert uniforms == [("color", gl.GL_FLOAT)]
 def test_uniform_float(self):
     shader = VertexShader("uniform float color;")
     uniforms = shader._get_uniforms()
     assert uniforms == [("color", gl.GL_FLOAT)]
Beispiel #33
0
 def test_init_from_shader(self):
     program = Program(VertexShader("A"), FragmentShader("B"))
     assert len(program.shaders) == 2
     assert program.shaders[0].code == "A"
     assert program.shaders[1].code == "B"
 def test_uniform_array(self):
     shader = VertexShader("uniform float color[2];")
     uniforms = shader._get_uniforms()
     assert uniforms == [("color[0]", gl.GL_FLOAT),
                         ("color[1]", gl.GL_FLOAT)]
Beispiel #35
0
 def test_unique_shader(self):
     vert = VertexShader("A")
     frag = FragmentShader("B")
     program = Program([vert, vert], [frag, frag, frag])
     assert len(program.shaders) == 2
 def test_attribute_vec4(self):
     shader = VertexShader("attribute vec4 color;")
     attributes = shader._get_attributes()
     assert attributes == [("color", gl.GL_FLOAT_VEC4)]
Beispiel #37
0
 def test_setcode(self):
     shader = VertexShader()
     shader.set_code("")
     assert shader._need_update