Skip to content

Instantly share code, notes, and snippets.

@ando-takahiro
Created February 21, 2019 02:04
Show Gist options
  • Select an option

  • Save ando-takahiro/47c628d9ddcee75a4466c8e962388689 to your computer and use it in GitHub Desktop.

Select an option

Save ando-takahiro/47c628d9ddcee75a4466c8e962388689 to your computer and use it in GitHub Desktop.
Based on PyGL headless test and @ototoi 's code
import OpenGL
import OpenGL.platform.egl
OpenGL.platform.PLATFORM = p = OpenGL.platform.egl.EGLPlatform()
from OpenGL import *
from OpenGL.GL import *
from OpenGL.EGL import *
from functools import wraps
import ctypes
DESIRED_ATTRIBUTES = [
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_BLUE_SIZE, 8,
EGL_RED_SIZE,8,
EGL_GREEN_SIZE,8,
EGL_DEPTH_SIZE,24,
EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER,
EGL_CONFIG_CAVEAT, EGL_NONE, # Don't allow slow/non-conformant
]
API_BITS = {
'opengl': EGL_OPENGL_BIT,
'gl': EGL_OPENGL_BIT,
'gles2': EGL_OPENGL_ES2_BIT,
'gles1': EGL_OPENGL_ES_BIT,
'gles': EGL_OPENGL_ES_BIT,
'es2': EGL_OPENGL_ES2_BIT,
'es1': EGL_OPENGL_ES_BIT,
'es': EGL_OPENGL_ES_BIT,
}
API_NAMES = dict([
(k,{
EGL_OPENGL_BIT:EGL_OPENGL_API,
EGL_OPENGL_ES2_BIT:EGL_OPENGL_ES_API,
EGL_OPENGL_ES_BIT:EGL_OPENGL_ES_API
}[v])
for k,v in API_BITS.items()
])
def egltest( size=(1024,1024), name=None, api='es2', attributes=DESIRED_ATTRIBUTES ):
def gltest( function ):
"""Decorator to allow a function to run in a Pygame GLES[1,2,3] context"""
@wraps( function )
def test_function( *args, **named ):
major,minor = ctypes.c_long(),ctypes.c_long()
display = eglGetDisplay(EGL_DEFAULT_DISPLAY)
eglInitialize( display, major, minor)
num_configs = ctypes.c_long()
configs = (EGLConfig*2)()
api_constant = API_NAMES[api.lower()]
local_attributes = attributes[:]
local_attributes.extend( [
EGL_CONFORMANT, API_BITS[api.lower()],
EGL_NONE,
])
print('local_attributes', local_attributes)
local_attributes= arrays.GLintArray.asArray( local_attributes )
eglChooseConfig(display, local_attributes, configs, 2, num_configs)
pbufferAttribs = [
EGL_WIDTH, size[0],
EGL_HEIGHT, size[1],
EGL_NONE
]
arr = (EGLint * len(pbufferAttribs))(*pbufferAttribs)
#pbuffer_attribs = np.array([
# EGL_WIDTH, size[0],
# EGL_HEIGHT, size[1],
# EGL_NONE
#], dtype="i4")
surface = eglCreatePbufferSurface(display, configs[0], arr) #pbuffer_attribs)
if surface == EGL_NO_SURFACE:
raise RuntimeError("failed to eglCreatePbufferSurface")
#eglStatus = eglBindAPI(EGL_OPENGL_API);
#if eglStatus == EGL_FALSE:
# raise RuntimeError("failed to eglBindAPI")
print('API', api_constant)
eglBindAPI(api_constant)
ctx = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, None)
if ctx == EGL_NO_CONTEXT:
raise RuntimeError( 'Unable to create context' )
eglMakeCurrent( display, surface, surface, ctx )
function(*args, **named)
eglSwapBuffers( display, surface )
return test_function
return gltest
# @egltest( api='opengl' )
@egltest()
def test_gl():
glClearColor( 1,0,0, 0 )
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
print('hello')
test_gl()
@ando-takahiro
Copy link
Author

Output:

local_attributes [EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_BLUE_SIZE, 8, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER, EGL_CONFIG_CAVEAT, EGL_NONE, EGL_CONFORMANT, EGL_OPENGL_ES2_BIT, EGL_NONE]
API EGL_OPENGL_ES_API (12448)
hello

@ando-takahiro
Copy link
Author

It runs on nvidia/cudagl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment