def test_wrappers(): """Test gloo wrappers""" with Canvas(): gl.use_gl('desktop debug') gloo.clear('#112233') # make it so that there's something non-zero # check presets assert_raises(ValueError, gloo.set_state, preset='foo') for state in gloo.get_state_presets().keys(): gloo.set_state(state) assert_raises(ValueError, gloo.set_blend_color, (0., 0.)) # bad color assert_raises(TypeError, gloo.set_hint, 1, 2) # need strs # this doesn't exist in ES 2.0 namespace assert_cmd_raises(ValueError, gloo.set_hint, 'fog_hint', 'nicest') # test bad enum assert_raises(RuntimeError, gloo.set_line_width, -1) # check read_pixels x = gloo.read_pixels() assert_true(isinstance(x, np.ndarray)) assert_true(isinstance(gloo.read_pixels((0, 0, 1, 1)), np.ndarray)) assert_raises(ValueError, gloo.read_pixels, (0, 0, 1)) # bad port y = gloo.read_pixels(alpha=False, out_type=np.ubyte) assert_equal(y.shape, x.shape[:2] + (3,)) assert_array_equal(x[..., :3], y) y = gloo.read_pixels(out_type='float') assert_allclose(x/255., y) # now let's (indirectly) check our set_* functions viewport = (0, 0, 1, 1) blend_color = (0., 0., 0.) _funs = dict(viewport=viewport, # checked hint=('generate_mipmap_hint', 'nicest'), depth_range=(1., 2.), front_face='cw', # checked cull_face='front', line_width=1., polygon_offset=(1., 1.), blend_func=('zero', 'one'), blend_color=blend_color, blend_equation='func_add', scissor=(0, 0, 1, 1), stencil_func=('never', 1, 2, 'back'), stencil_mask=4, stencil_op=('zero', 'zero', 'zero', 'back'), depth_func='greater', depth_mask=True, color_mask=(True, True, True, True), sample_coverage=(0.5, True)) gloo.set_state(**_funs) gloo.clear((1., 1., 1., 1.), 0.5, 1) gloo.flush() gloo.finish() # check some results assert_array_equal(gl.glGetParameter(gl.GL_VIEWPORT), viewport) assert_equal(gl.glGetParameter(gl.GL_FRONT_FACE), gl.GL_CW) assert_equal(gl.glGetParameter(gl.GL_BLEND_COLOR), blend_color + (1,))
def _test_enabling_disabling(): # Enabling/disabling gl.glEnable(gl.GL_DEPTH_TEST) assert_equal(gl.glIsEnabled(gl.GL_DEPTH_TEST), True) assert_equal(gl.glGetParameter(gl.GL_DEPTH_TEST), 1) gl.glDisable(gl.GL_DEPTH_TEST) assert_equal(gl.glIsEnabled(gl.GL_DEPTH_TEST), False) assert_equal(gl.glGetParameter(gl.GL_DEPTH_TEST), 0) # gl.glEnable(gl.GL_BLEND) assert_equal(gl.glIsEnabled(gl.GL_BLEND), True) assert_equal(gl.glGetParameter(gl.GL_BLEND), 1) gl.glDisable(gl.GL_BLEND) assert_equal(gl.glIsEnabled(gl.GL_BLEND), False) assert_equal(gl.glGetParameter(gl.GL_BLEND), 0) gl.check_error()
def _test_setting_parameters(): # Set some parameters and get result clr = 1.0, 0.1, 0.2, 0.7 gl.glClearColor(*clr) assert_almost_equal(gl.glGetParameter(gl.GL_COLOR_CLEAR_VALUE), clr) # gl.glCullFace(gl.GL_FRONT) assert_equal(gl.glGetParameter(gl.GL_CULL_FACE_MODE), gl.GL_FRONT) gl.glCullFace(gl.GL_BACK) assert_equal(gl.glGetParameter(gl.GL_CULL_FACE_MODE), gl.GL_BACK) # gl.glDepthFunc(gl.GL_NOTEQUAL) assert_equal(gl.glGetParameter(gl.GL_DEPTH_FUNC), gl.GL_NOTEQUAL) # val = 0.2, 0.3 gl.glDepthRange(*val) assert_almost_equal(gl.glGetParameter(gl.GL_DEPTH_RANGE), val) gl.check_error()
def _prepare_draw(self, view): if self._symbol is None: return False view.view_program['u_px_scale'] = view.transforms.pixel_scale if self.scaling: tr = view.transforms.get_transform('visual', 'document').simplified scale = np.linalg.norm((tr.map([1, 0]) - tr.map([0, 0]))[:2]) view.view_program['u_scale'] = scale else: view.view_program['u_scale'] = 1 from vispy.gloo import gl view.view_program['u_viewport'] = gl.glGetParameter(gl.GL_VIEWPORT) view.view_program['u_SimulatePointCoord'] = True
def _test_setting_stuff(): # Set stuff to touch functions gl.glClear(gl.GL_COLOR_BUFFER_BIT) # gl.glBlendColor(1.0, 1.0, 1.0, 1.0) gl.glBlendEquation(gl.GL_FUNC_ADD) gl.glBlendEquationSeparate(gl.GL_FUNC_ADD, gl.GL_FUNC_ADD) gl.glBlendFunc(gl.GL_ONE, gl.GL_ZERO) gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ZERO, gl.GL_ONE, gl.GL_ZERO) # gl.glClearColor(0.0, 0.0, 0.0, 1.0) gl.glClearDepth(1) gl.glClearStencil(0) # gl.glColorMask(True, True, True, True) gl.glDepthMask(False) gl.glStencilMask(255) gl.glStencilMaskSeparate(gl.GL_FRONT, 128) # gl.glStencilFunc(gl.GL_ALWAYS, 0, 255) gl.glStencilFuncSeparate(gl.GL_FRONT, gl.GL_ALWAYS, 0, 255) gl.glStencilOp(gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP) gl.glStencilOpSeparate(gl.GL_FRONT, gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP) # gl.glFrontFace(gl.GL_CW) gl.glHint(gl.GL_GENERATE_MIPMAP_HINT, gl.GL_FASTEST) gl.glLineWidth(2.0) gl.glPolygonOffset(0.0, 0.0) gl.glSampleCoverage(1.0, False) # And getting stuff try: with use_log_level('error', print_msg=False): r, p = gl.glGetShaderPrecisionFormat(gl.GL_FRAGMENT_SHADER, gl.GL_HIGH_FLOAT) gl.check_error() # Sometimes the func is there but OpenGL errs except Exception: pass # accept if the function is not there ... # We should catch RuntimeError and GL.error.NullFunctionError, # but PyOpenGL may not be available. # On Travis this function was not there on one machine according # to PyOpenGL, but our desktop backend worked fine ... # v = gl.glGetParameter(gl.GL_VERSION) assert_true(isinstance(v, string_types)) assert_true(len(v) > 0) gl.check_error()
def _check_result(assert_result=True): """Test the color of each quadrant by picking the center pixel of each quadrant and comparing it with the reference color. """ # Take screenshot x, y, w, h = gl.glGetParameter(gl.GL_VIEWPORT) data = gl.glReadPixels(x, y, w, h, gl.GL_RGB, gl.GL_UNSIGNED_BYTE) im = np.frombuffer(data, np.uint8) im.shape = h, w, 3 # Get center pixel from each quadrant pix1 = tuple(im[int(1*h/4), int(1*w/4)]) pix2 = tuple(im[int(3*h/4), int(1*w/4)]) pix3 = tuple(im[int(3*h/4), int(3*w/4)]) pix4 = tuple(im[int(1*h/4), int(3*w/4)]) # print(pix1, pix2, pix3, pix4) if assert_result: # Test their value assert_equal(pix1, (0, 0, 0)) assert_equal(pix2, (255, 0, 0)) assert_equal(pix3, (0, 255, 0)) assert_equal(pix4, (0, 0, 255))
def _check_result(assert_result=True): """ Test the color of each quadrant by picking the center pixel of each quadrant and comparing it with the reference color. """ # Take screenshot x, y, w, h = gl.glGetParameter(gl.GL_VIEWPORT) data = gl.glReadPixels(x, y, w, h, gl.GL_RGB, gl.GL_UNSIGNED_BYTE) im = np.frombuffer(data, np.uint8) im.shape = h, w, 3 # Get center pixel from each quadrant pix1 = tuple(im[int(1*h/4), int(1*w/4)]) pix2 = tuple(im[int(3*h/4), int(1*w/4)]) pix3 = tuple(im[int(3*h/4), int(3*w/4)]) pix4 = tuple(im[int(1*h/4), int(3*w/4)]) #print(pix1, pix2, pix3, pix4) if assert_result: # Test their value assert_equal(pix1, (0, 0, 0)) assert_equal(pix2, (255, 0, 0)) assert_equal(pix3, (0, 255, 0)) assert_equal(pix4, (0, 0, 255))
def get_gl_extensions() -> str: """Get basic info about the Gl capabilities of this machine""" with _opengl_context(): return gl.glGetParameter(gl.GL_EXTENSIONS)
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)
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)