Example #1
0
def test_context_properties():
    """Test setting context properties"""
    a = default_app
    a.use()
    if a.backend_name.lower() == 'pyglet':
        return  # cannot set more than once on Pyglet
    # stereo, double buffer won't work on every sys
    contexts = [dict(samples=4), dict(stencil_size=8),
                dict(samples=4, stencil_size=8)]
    if a.backend_name.lower() != 'glfw':  # glfw *always* double-buffers
        contexts.append(dict(double_buffer=False, samples=4))
        contexts.append(dict(double_buffer=False))
    else:
        assert_raises(RuntimeError, Canvas, app=a,
                      context=dict(double_buffer=False))
    if a.backend_name.lower() == 'sdl2' and os.getenv('TRAVIS') == 'true':
        raise SkipTest('Travis SDL cannot set context')
    for context in contexts:
        n_items = len(context)
        with Canvas(context=context):
            if os.getenv('TRAVIS', 'false') == 'true':
                # Travis cannot handle obtaining these values
                props = context
            else:
                props = get_gl_configuration()
            assert_equal(len(context), n_items)
            for key, val in context.items():
                assert_equal(val, props[key], key)
    assert_raises(TypeError, Canvas, context='foo')
    assert_raises(KeyError, Canvas, context=dict(foo=True))
    assert_raises(TypeError, Canvas, context=dict(double_buffer='foo'))
Example #2
0
def test_context_properties():
    """Test setting context properties"""
    a = use_app()
    if a.backend_name.lower() == 'pyglet':
        return  # cannot set more than once on Pyglet
    # stereo, double buffer won't work on every sys
    contexts = [dict(samples=4), dict(stencil_size=8),
                dict(samples=4, stencil_size=8)]
    if a.backend_name.lower() != 'glfw':  # glfw *always* double-buffers
        contexts.append(dict(double_buffer=False, samples=4))
        contexts.append(dict(double_buffer=False))
    else:
        assert_raises(RuntimeError, Canvas, app=a,
                      context=dict(double_buffer=False))
    if a.backend_name.lower() == 'sdl2' and os.getenv('TRAVIS') == 'true':
        raise SkipTest('Travis SDL cannot set context')
    for context in contexts:
        n_items = len(context)
        with Canvas(context=context):
            if 'true' in (os.getenv('TRAVIS', ''),
                          os.getenv('APPVEYOR', '').lower()):
                # Travis and Appveyor cannot handle obtaining these values
                props = context
            else:
                props = get_gl_configuration()
            assert_equal(len(context), n_items)
            for key, val in context.items():
                # XXX knownfail for windows samples, and wx (all platforms)
                if key == 'samples':
                    iswx = a.backend_name.lower() == 'wx'
                    if not (sys.platform.startswith('win') or iswx):
                        assert_equal(val, props[key], key)
    assert_raises(TypeError, Canvas, context='foo')
    assert_raises(KeyError, Canvas, context=dict(foo=True))
    assert_raises(TypeError, Canvas, context=dict(double_buffer='foo'))
Example #3
0
def test_context_properties():
    """Test setting context properties"""
    a = use_app()
    if a.backend_name.lower() == 'pyglet':
        return  # cannot set more than once on Pyglet
    # stereo, double buffer won't work on every sys
    configs = [dict(samples=4), dict(stencil_size=8),
               dict(samples=4, stencil_size=8)]
    if a.backend_name.lower() != 'glfw':  # glfw *always* double-buffers
        configs.append(dict(double_buffer=False, samples=4))
        configs.append(dict(double_buffer=False))
    else:
        assert_raises(RuntimeError, Canvas, app=a,
                      config=dict(double_buffer=False))
    if a.backend_name.lower() == 'sdl2' and os.getenv('TRAVIS') == 'true':
        raise SkipTest('Travis SDL cannot set context')
    for config in configs:
        n_items = len(config)
        with Canvas(config=config):
            if 'true' in (os.getenv('TRAVIS', ''),
                          os.getenv('APPVEYOR', '').lower()):
                # Travis and Appveyor cannot handle obtaining these values
                props = config
            else:
                props = get_gl_configuration()
            assert_equal(len(config), n_items)
            for key, val in config.items():
                # XXX knownfail for windows samples, and wx (all platforms)
                if key == 'samples':
                    iswx = a.backend_name.lower() == 'wx'
                    if not (sys.platform.startswith('win') or iswx):
                        assert_equal(val, props[key], key)
    assert_raises(TypeError, Canvas, config='foo')
    assert_raises(KeyError, Canvas, config=dict(foo=True))
    assert_raises(TypeError, Canvas, config=dict(double_buffer='foo'))
Example #4
0
def test_context_properties():
    """Test setting context properties"""
    a = use_app()
    if a.backend_name.lower() == 'pyglet':
        return  # cannot set more than once on Pyglet
    if a.backend_name.lower() == 'osmesa':
        return  # cannot set config on OSMesa

    # stereo, double buffer won't work on every sys
    configs = [dict(samples=4), dict(stencil_size=8),
               dict(samples=4, stencil_size=8)]
    if a.backend_name.lower() != 'glfw':  # glfw *always* double-buffers
        configs.append(dict(double_buffer=False, samples=4))
        configs.append(dict(double_buffer=False))
    else:
        assert_raises(RuntimeError, Canvas, app=a,
                      config=dict(double_buffer=False))
    if a.backend_name.lower() == 'sdl2' and IS_CI:
        raise SkipTest('Travis SDL cannot set context')
    for config in configs:
        n_items = len(config)
        with Canvas(config=config):
            if IS_CI:
                # Travis and Appveyor cannot handle obtaining these values
                props = config
            else:
                props = get_gl_configuration()
            assert len(config) == n_items
            for key, val in config.items():
                # XXX knownfail for windows samples, and wx/tkinter (all platforms)
                if key == 'samples':
                    will_fail_backend = a.backend_name.lower() in ('wx', 'tkinter')
                    if not (sys.platform.startswith('win') or will_fail_backend):
                        assert val == props[key], key
    assert_raises(TypeError, Canvas, config='foo')
    assert_raises(KeyError, Canvas, config=dict(foo=True))
    assert_raises(TypeError, Canvas, config=dict(double_buffer='foo'))
Example #5
0
def render_with_gloo(draw_data):
    """draw ImGui's drawlists with vispy's gloo interface

    Parameters
    ----------
    draw_data: ImDrawData

    """
    global device_objects

    # adapted from imgui_impl_ios.cpp in imgui, to target the minimal GLES API
    d = pymgui.ImDrawData(draw_data)
    io = pymgui.get_io()

    fb_scale = io.display_framebuffer_scale
    fb_width, fb_height = io.display_size

    if fb_width == 0 or fb_height == 0:
        return

    conf = gloo.get_gl_configuration()

    # backup GL state
    last_program = gl.glGetParameter(gl.GL_CURRENT_PROGRAM)
    last_texture = gl.glGetParameter(gl.GL_TEXTURE_BINDING_2D)
    last_array_buffer = gl.glGetParameter(gl.GL_ARRAY_BUFFER_BINDING)
    last_element_array_buffer = gl.glGetParameter(gl.GL_ELEMENT_ARRAY_BUFFER_BINDING)
    last_viewport = gl.glGetParameter(gl.GL_VIEWPORT)

    last_enable_blend = gl.glIsEnabled(gl.GL_BLEND),
    last_blend_equation = gl.glGetParameter(gl.GL_BLEND_EQUATION_RGB),

    last_blend_func = dict(
        last_blend_equation_alpha=gl.glGetParameter(gl.GL_BLEND_EQUATION_ALPHA),
    )

    last_gl_states = dict(
        cull_face = gl.glIsEnabled(gl.GL_CULL_FACE),
        depth_test = gl.glIsEnabled(gl.GL_DEPTH_TEST),
        scissor_test = gl.glIsEnabled(gl.GL_SCISSOR_TEST))

    gloo.set_state(blend=True)
    gloo.set_blend_equation('func_add')
    gloo.set_blend_func(salpha='src_alpha', dalpha='one_minus_src_alpha')
    gloo.set_state(cull_face=False, depth_test=False, scissor_test=True)

    gl.glActiveTexture(gl.GL_TEXTURE0)

    # Setup viewport, orthographic projection matrix
    gloo.set_viewport(0, 0, int(fb_width), int(fb_height))

    p = device_objects['program'] # type: gloo.Program

    ortho_projection = np.array([
        [2./fb_width,   0,                  0,      0],
        [0.,             2./-fb_height,     0,      0],
        [0.,             0.,                -1.,    0],
        [-1.,            1.,                0,      0]
    ])

    vtype = [('a_position', np.float32, 2),
             ('a_uv', np.float32, 2),
             # ('a_color', np.uint8, 4)]
             ('a_color', np.uint32, 1)]

             # pos_dtype = [('a_position', np.float32, 2)]
    # uv_dtype =  [('a_uv', np.float32, 2)]
    # color_dtype = [('a_color', np.uint8, 4)]
    itype = np.uint16

    display_scale = 1.0

    p['u_mvp'] = ortho_projection


    # print("draw {} cmd_list".format(len(d.cmd_lists)))
    for cmd_list in d.cmd_lists:
        vertex_count =C.ImDrawList_GetVertexBufferSize(cmd_list)
        index_count = C.ImDrawList_GetIndexBufferSize(cmd_list)

        vertex_buffer_ptr = C.ImDrawList_GetVertexPtr(cmd_list, 0)
        index_buffer_ptr = C.ImDrawList_GetIndexPtr(cmd_list, 0)

        def to_nparray(ptr, item_count, dtype):
            byte_count = item_count * dtype.itemsize
            as_bytes = ffi.buffer(ptr, byte_count)
            return np.frombuffer(as_bytes, dtype=dtype)

        ig_vertices = to_nparray(vertex_buffer_ptr, vertex_count, np.dtype(vtype))
        ig_indices = to_nparray(index_buffer_ptr, index_count, np.dtype(itype))
        # print("{} vertices to draw".format(len(ig_vertices)))
    #
        p['a_position'] = ig_vertices[:]
        # p['a_uv'] = ig_vertices['a_uv']
        # p['a_color'] = ig_vertices['a_color']
        # device_objects['geometry'] = ig_vertices
    #
    #     gl.glBufferData(gl.GL_ARRAY_BUFFER, ig_vertices, gl.GL_STREAM_DRAW)
    #     gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, indices, gl.GL_STREAM_DRAW)
    #
    #     index_buffer_offset = 0
    #     cmd_size = C.ImDrawList_GetCmdSize(cmd_list)
    #     for cmd_index in range(cmd_size):
    #         draw_cmd_ptr = C.ImDrawList_GetCmdPtr(cmd_list, cmd_index)
    #         draw_cmd = pymgui.ImDrawCmd(draw_cmd_ptr)
    #
    #         if draw_cmd.texture_id:
    #             gl.glBindTexture(gl.GL_TEXTURE_2D, draw_cmd.texture_id)
    #         x1, y1, x2, y2 = draw_cmd.clip_rect
    #
    #         gl.glScissor(
    #             x=int(x1 * display_scale),
    #             y=int(y1 * display_scale),
    #             width=int((x2 - x1) * display_scale),
    #             height=int((y2 - y1) * display_scale)
    #         )
    #
    #         start, stop = index_buffer_offset, draw_cmd.elem_count
    #         # gl.glDrawElements(gl.GL_TRIANGLES, draw_cmd.elem_count, gl.GL_UNSIGNED_SHORT, indices[index_buffer_offset:])
    #         p.draw('triangles', gloo.IndexBuffer(ig_indices[start:stop]))
    #         index_buffer_offset += draw_cmd.elem_count
    #
    # # Restore modified state
    gloo.set_state(**last_gl_states)
    gloo.set_state(blend=last_enable_blend)
    # gloo.set_blend_equation(last_blend_equation)
    # # gl.glBindVertexArray(0)
    # gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)
    gl.glUseProgram(last_program)
Example #6
0
def render_with_gloo(draw_data):
    """draw ImGui's drawlists with vispy's gloo interface

    Parameters
    ----------
    draw_data: ImDrawData

    """
    global device_objects

    # adapted from imgui_impl_ios.cpp in imgui, to target the minimal GLES API
    d = pymgui.ImDrawData(draw_data)
    io = pymgui.get_io()

    fb_scale = io.display_framebuffer_scale
    fb_width, fb_height = io.display_size

    if fb_width == 0 or fb_height == 0:
        return

    conf = gloo.get_gl_configuration()

    # backup GL state
    last_program = gl.glGetParameter(gl.GL_CURRENT_PROGRAM)
    last_texture = gl.glGetParameter(gl.GL_TEXTURE_BINDING_2D)
    last_array_buffer = gl.glGetParameter(gl.GL_ARRAY_BUFFER_BINDING)
    last_element_array_buffer = gl.glGetParameter(
        gl.GL_ELEMENT_ARRAY_BUFFER_BINDING)
    last_viewport = gl.glGetParameter(gl.GL_VIEWPORT)

    last_enable_blend = gl.glIsEnabled(gl.GL_BLEND),
    last_blend_equation = gl.glGetParameter(gl.GL_BLEND_EQUATION_RGB),

    last_blend_func = dict(last_blend_equation_alpha=gl.glGetParameter(
        gl.GL_BLEND_EQUATION_ALPHA), )

    last_gl_states = dict(cull_face=gl.glIsEnabled(gl.GL_CULL_FACE),
                          depth_test=gl.glIsEnabled(gl.GL_DEPTH_TEST),
                          scissor_test=gl.glIsEnabled(gl.GL_SCISSOR_TEST))

    gloo.set_state(blend=True)
    gloo.set_blend_equation('func_add')
    gloo.set_blend_func(salpha='src_alpha', dalpha='one_minus_src_alpha')
    gloo.set_state(cull_face=False, depth_test=False, scissor_test=True)

    gl.glActiveTexture(gl.GL_TEXTURE0)

    # Setup viewport, orthographic projection matrix
    gloo.set_viewport(0, 0, int(fb_width), int(fb_height))

    p = device_objects['program']  # type: gloo.Program

    ortho_projection = np.array([[2. / fb_width, 0, 0, 0],
                                 [0., 2. / -fb_height, 0, 0], [0., 0., -1., 0],
                                 [-1., 1., 0, 0]])

    vtype = [
        ('a_position', np.float32, 2),
        ('a_uv', np.float32, 2),
        # ('a_color', np.uint8, 4)]
        ('a_color', np.uint32, 1)
    ]

    # pos_dtype = [('a_position', np.float32, 2)]
    # uv_dtype =  [('a_uv', np.float32, 2)]
    # color_dtype = [('a_color', np.uint8, 4)]
    itype = np.uint16

    display_scale = 1.0

    p['u_mvp'] = ortho_projection

    # print("draw {} cmd_list".format(len(d.cmd_lists)))
    for cmd_list in d.cmd_lists:
        vertex_count = C.ImDrawList_GetVertexBufferSize(cmd_list)
        index_count = C.ImDrawList_GetIndexBufferSize(cmd_list)

        vertex_buffer_ptr = C.ImDrawList_GetVertexPtr(cmd_list, 0)
        index_buffer_ptr = C.ImDrawList_GetIndexPtr(cmd_list, 0)

        def to_nparray(ptr, item_count, dtype):
            byte_count = item_count * dtype.itemsize
            as_bytes = ffi.buffer(ptr, byte_count)
            return np.frombuffer(as_bytes, dtype=dtype)

        ig_vertices = to_nparray(vertex_buffer_ptr, vertex_count,
                                 np.dtype(vtype))
        ig_indices = to_nparray(index_buffer_ptr, index_count, np.dtype(itype))
        # print("{} vertices to draw".format(len(ig_vertices)))
        #
        p['a_position'] = ig_vertices[:]
        # p['a_uv'] = ig_vertices['a_uv']
        # p['a_color'] = ig_vertices['a_color']
        # device_objects['geometry'] = ig_vertices
    #
    #     gl.glBufferData(gl.GL_ARRAY_BUFFER, ig_vertices, gl.GL_STREAM_DRAW)
    #     gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, indices, gl.GL_STREAM_DRAW)
    #
    #     index_buffer_offset = 0
    #     cmd_size = C.ImDrawList_GetCmdSize(cmd_list)
    #     for cmd_index in range(cmd_size):
    #         draw_cmd_ptr = C.ImDrawList_GetCmdPtr(cmd_list, cmd_index)
    #         draw_cmd = pymgui.ImDrawCmd(draw_cmd_ptr)
    #
    #         if draw_cmd.texture_id:
    #             gl.glBindTexture(gl.GL_TEXTURE_2D, draw_cmd.texture_id)
    #         x1, y1, x2, y2 = draw_cmd.clip_rect
    #
    #         gl.glScissor(
    #             x=int(x1 * display_scale),
    #             y=int(y1 * display_scale),
    #             width=int((x2 - x1) * display_scale),
    #             height=int((y2 - y1) * display_scale)
    #         )
    #
    #         start, stop = index_buffer_offset, draw_cmd.elem_count
    #         # gl.glDrawElements(gl.GL_TRIANGLES, draw_cmd.elem_count, gl.GL_UNSIGNED_SHORT, indices[index_buffer_offset:])
    #         p.draw('triangles', gloo.IndexBuffer(ig_indices[start:stop]))
    #         index_buffer_offset += draw_cmd.elem_count
    #
    # # Restore modified state
    gloo.set_state(**last_gl_states)
    gloo.set_state(blend=last_enable_blend)
    # gloo.set_blend_equation(last_blend_equation)
    # # gl.glBindVertexArray(0)
    # gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)
    gl.glUseProgram(last_program)