Last active
August 29, 2015 13:59
-
-
Save almarklein/10625041 to your computer and use it in GitHub Desktop.
Testing warmup issue for vispy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# ----------------------------------------------------------------------------- | |
# Copyright (c) 2014, Vispy Development Team. All Rights Reserved. | |
# Distributed under the (new) BSD License. See LICENSE.txt for more info. | |
# ----------------------------------------------------------------------------- | |
from __future__ import print_function | |
import sys | |
import ctypes | |
import numpy as np | |
import OpenGL.GL as gl | |
try: | |
import glfw | |
HAVE_GLFW = True | |
except ImportError: | |
HAVE_GLFW = False | |
try: | |
import pyglet | |
HAVE_PYGLET = True | |
except ImportError: | |
HAVE_PYGLET = False | |
try: | |
if sys.version_info > (3,): | |
raise ImportError() | |
import OpenGL.GLUT as glut | |
HAVE_GLUT = True | |
except ImportError: | |
HAVE_GLUT = False | |
try: | |
# Note: having both imported can result in segfaults! | |
from PySide import QtGui, QtOpenGL | |
HAVE_QT = True | |
except ImportError: | |
try: | |
from PyQt4 import QtGui, QtOpenGL | |
HAVE_QT = True | |
except ImportError: | |
HAVE_QT = False | |
class pyglet_app(object): | |
def __enter__(self): | |
self.window = pyglet.window.Window(width=256, height=256, visible=False) | |
gl.glViewport(0,0,256,256) | |
return self | |
def __exit__(self, type, value, traceback): | |
self.window.close() | |
#pyglet.app.exit() | |
class glut_app(object): | |
def __enter__(self): | |
glut.glutInit() | |
glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA) | |
self.id = glut.glutCreateWindow('GLUT') | |
glut.glutReshapeWindow(256, 256) | |
gl.glViewport(0,0,256,256) | |
return self | |
def __exit__(self, type, value, traceback): | |
glut.glutSetWindow(self.id) | |
glut.glutHideWindow() | |
glut.glutDestroyWindow(self.id) # Tghis does not even work | |
class qt_app(object): | |
def __enter__(self): | |
self.app = QtGui.QApplication.instance() | |
if self.app is None: | |
self.app = QtGui.QApplication([]) | |
self.window = QtOpenGL.QGLWidget() | |
self.window.resize(256, 256) | |
self.window.show() | |
self.window.makeCurrent() | |
gl.glViewport(0,0,256,256) | |
return self | |
def __exit__(self, type, value, traceback): | |
self.window.close() | |
#self.window.destroy() | |
class glfw_app(object): | |
def __enter__(self): | |
glfw.glfwInit() | |
glfw.glfwWindowHint(glfw.GLFW_VISIBLE, gl.GL_FALSE) | |
self.window = glfw.glfwCreateWindow(256, 256, "GLFW", None, None) | |
glfw.glfwMakeContextCurrent(self.window) | |
gl.glViewport(0,0,256,256) | |
return self | |
def __exit__(self, type, value, traceback): | |
glfw.glfwTerminate() | |
def screenshot(): | |
x, y, w, h = gl.glGetInteger(gl.GL_VIEWPORT) | |
gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1) | |
I = gl.glReadPixels(x, y, w, h, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) | |
gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 4) | |
if not isinstance(I, np.ndarray): | |
I = np.frombuffer(I, np.uint8) | |
I.shape = h, w, 4 | |
return np.flipud(I) | |
def build_program(vertex_code, fragment_code, data): | |
program = gl.glCreateProgram() | |
vertex = gl.glCreateShader(gl.GL_VERTEX_SHADER) | |
fragment = gl.glCreateShader(gl.GL_FRAGMENT_SHADER) | |
gl.glShaderSource(vertex, vertex_code) | |
gl.glShaderSource(fragment, fragment_code) | |
gl.glCompileShader(vertex) | |
gl.glCompileShader(fragment) | |
gl.glAttachShader(program, vertex) | |
gl.glAttachShader(program, fragment) | |
gl.glLinkProgram(program) | |
gl.glDetachShader(program, vertex) | |
gl.glDetachShader(program, fragment) | |
gl.glUseProgram(program) | |
buffer = gl.glGenBuffers(1) | |
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer) | |
gl.glBufferData(gl.GL_ARRAY_BUFFER, data.nbytes, data, gl.GL_DYNAMIC_DRAW) | |
stride = data.strides[0] | |
offset = ctypes.c_void_p(0) | |
loc = gl.glGetAttribLocation(program, "position".encode('utf-8')) | |
gl.glEnableVertexAttribArray(loc) | |
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer) | |
gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, False, stride, offset) | |
offset = ctypes.c_void_p(data.dtype["position"].itemsize) | |
loc = gl.glGetAttribLocation(program, "color".encode('utf-8')) | |
gl.glEnableVertexAttribArray(loc) | |
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer) | |
gl.glVertexAttribPointer(loc, 4, gl.GL_FLOAT, False, stride, offset) | |
return program | |
vertex_code = """ | |
attribute vec2 position; | |
attribute vec4 color; | |
varying vec4 v_color; | |
void main() | |
{ | |
gl_Position = vec4(position, 0.0, 1.0); | |
v_color = color; | |
} """ | |
fragment_code = """ | |
varying vec4 v_color; | |
void main() | |
{ | |
gl_FragColor = v_color; | |
} """ | |
data = np.zeros(4, [("position", np.float32, 2),("color", np.float32, 4)]) | |
data['color'] = (1, 0, 0, 1) | |
data['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] | |
# GLUT + PyOpenGL.GL : clear test | |
# ----------------------------------------------------------------------------- | |
def test_glut_clear(): | |
with glut_app(): | |
_, _, w, h = gl.glGetInteger(gl.GL_VIEWPORT) | |
print("GLUT backend (%3dx%3d): clear test" % (w,h), end=' ') | |
gl.glClearColor(1,0,0,1) | |
gl.glClear(gl.GL_COLOR_BUFFER_BIT) | |
if np.allclose(screenshot(), np.ones((256,256,4))*[255,0,0,255]): | |
print("passed") | |
return False | |
else: | |
print("failed") | |
return True | |
# GLFW + PyOpenGL.GL : clear test | |
# ----------------------------------------------------------------------------- | |
def test_glfw_clear(): | |
with glfw_app(): | |
_, _, w, h = gl.glGetInteger(gl.GL_VIEWPORT) | |
print("GLFW backend (%3dx%3d): clear test" % (w,h), end=' ') | |
gl.glClearColor(0,1,0,1) | |
gl.glClear(gl.GL_COLOR_BUFFER_BIT) | |
if np.allclose(screenshot(), np.ones((256,256,4))*[0,255,0,255]): | |
print("passed") | |
return False | |
else: | |
print("failed") | |
return True | |
# Pyglet + PyOpenGL.GL : clear test | |
# ----------------------------------------------------------------------------- | |
def test_pyglet_clear(): | |
with pyglet_app(): | |
_, _, w, h = gl.glGetInteger(gl.GL_VIEWPORT) | |
print("Pyglet backend (%3dx%3d): clear test" % (w,h), end=' ') | |
gl.glClearColor(0,0,1,1) | |
gl.glClear(gl.GL_COLOR_BUFFER_BIT) | |
if np.allclose(screenshot(), np.ones((256,256,4))*[0,0,255,255]): | |
print("passed") | |
return False | |
else: | |
print("failed") | |
return True | |
# Qt + PyOpenGL.GL : clear test | |
# ----------------------------------------------------------------------------- | |
def test_qt_clear(): | |
with qt_app() as app: | |
_, _, w, h = gl.glGetInteger(gl.GL_VIEWPORT) | |
print("Qt backend (%3dx%3d): clear test" % (w,h), end=' ') | |
gl.glClearColor(0,1,1,1) | |
gl.glClear(gl.GL_COLOR_BUFFER_BIT) | |
if np.allclose(screenshot(), np.ones((256,256,4))*[0,255,255,255]): | |
print("passed") | |
return False | |
else: | |
print("failed") | |
return True | |
# GLUT + PyOpenGL.GL : draw test | |
# ----------------------------------------------------------------------------- | |
def test_glut_draw(): | |
with glut_app() as app: | |
data['color'] = (1, 0, 0, 1) | |
build_program(vertex_code, fragment_code, data) | |
_, _, w, h = gl.glGetInteger(gl.GL_VIEWPORT) | |
print("GLUT backend (%3dx%3d): draw test" % (w,h), end=' ') | |
gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4) | |
#glut.glutSwapBuffers() | |
#glut.glutSwapBuffers() | |
if np.allclose(screenshot(), np.ones((256,256,4))*[255,0,0,255]): | |
print("passed") | |
return False | |
else: | |
print("failed") | |
return True | |
# GLFW + PyOpenGL.GL : draw test | |
# ----------------------------------------------------------------------------- | |
def test_glfw_draw(): | |
with glfw_app() as app: | |
data['color'] = (0, 1, 0, 1) | |
build_program(vertex_code, fragment_code, data) | |
_, _, w, h = gl.glGetInteger(gl.GL_VIEWPORT) | |
print("GLFW backend (%3dx%3d): draw test" % (w,h), end=' ') | |
gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4) | |
#glfw.glfwSwapBuffers(app.window) | |
#glfw.glfwSwapBuffers(app.window) | |
if np.allclose(screenshot(), np.ones((256,256,4))*[0,255,0,255]): | |
print("passed") | |
return False | |
else: | |
print("failed") | |
return True | |
# Pyglet + PyOpenGL.GL : draw test | |
# ----------------------------------------------------------------------------- | |
def test_pyglet_draw(): | |
with pyglet_app() as app: | |
data['color'] = (0, 0, 1, 1) | |
build_program(vertex_code, fragment_code, data) | |
_, _, w, h = gl.glGetInteger(gl.GL_VIEWPORT) | |
print("Pyglet backend (%3dx%3d): draw test" % (w,h), end=' ') | |
gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4) | |
#app.window.flip() | |
#app.window.flip() | |
if np.allclose(screenshot(), np.ones((256,256,4))*[0,0,255,255]): | |
print("passed") | |
return False | |
else: | |
print("failed") | |
return True | |
# Qt + PyOpenGL.GL : draw test | |
# ----------------------------------------------------------------------------- | |
def test_qt_draw(): | |
with qt_app() as app: | |
data['color'] = (0, 1, 1, 1) | |
build_program(vertex_code, fragment_code, data) | |
_, _, w, h = gl.glGetInteger(gl.GL_VIEWPORT) | |
print("Qt backend (%3dx%3d): draw test" % (w,h), end=' ') | |
gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4) | |
# app.window.swapBuffers() | |
# app.window.swapBuffers() | |
if np.allclose(screenshot(), np.ones((256,256,4))*[0,255,255,255]): | |
print("passed") | |
return False | |
else: | |
print("failed") | |
return True | |
TESTS = { | |
'Qt': (HAVE_QT, test_qt_clear, test_qt_draw), | |
'Pyglet': (HAVE_PYGLET, test_pyglet_clear, test_pyglet_draw), | |
'GLUT': (HAVE_GLUT, test_glut_clear, test_glut_draw), | |
'GLFW': (HAVE_GLFW, test_glfw_clear, test_glfw_draw), | |
} | |
if __name__ == '__main__': | |
N = 20 | |
results = dict([(i, 0) for i in TESTS]) | |
for iter in range(N): | |
backends = list(TESTS.keys()) | |
import random | |
random.shuffle(backends) | |
for backend in backends: | |
have, clear, draw = TESTS[backend] | |
if not have: | |
print("Skipping backend:", backend) | |
continue | |
results[backend] += clear() | |
results[backend] += draw() | |
# Show result | |
print('-'*20) | |
print('Ran %i tests:' % N) | |
for backend, errors in results.items(): | |
if not TESTS[backend][0]: | |
continue | |
p = 100 * (2*N-errors) / (2*N) | |
print('%s, %i errors %0.0f%% ok' % (backend, errors, p)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment