comparison pytouhou/ui/opengl/backend.pyx @ 583:47cf4e3d159d

Use libepoxy to discover the actual GL version we are using, and available extensions.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 05 Oct 2014 20:38:06 +0200
parents 6e79756b7f42
children e0166cda75d5
comparison
equal deleted inserted replaced
582:6e79756b7f42 583:47cf4e3d159d
3 3
4 from pytouhou.lib.opengl cimport \ 4 from pytouhou.lib.opengl cimport \
5 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND, 5 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND,
6 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST, 6 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
7 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY, 7 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
8 glPushDebugGroup, GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup) 8 glPushDebugGroup, GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup,
9 epoxy_gl_version, epoxy_is_desktop_gl, epoxy_has_gl_extension)
9 10
10 11
11 GameRenderer = None 12 GameRenderer = None
12 13
13 14
14 def init(options): 15 def init(options):
15 global flavor, version, major, minor, double_buffer, is_legacy, use_debug_group, use_vao, shader_header, GameRenderer 16 '''
17 Initialize the OpenGL module, and raise if something bad prevents it from
18 working.
19 '''
16 20
17 flavor_name = options['flavor'] 21 global profile, major, minor, double_buffer, is_legacy, GameRenderer
18 assert flavor_name in ('core', 'es', 'compatibility', 'legacy') 22
19 flavor = (sdl.GL_CONTEXT_PROFILE_CORE if flavor_name == 'core' else 23 flavor = options['flavor']
20 sdl.GL_CONTEXT_PROFILE_ES if flavor_name == 'es' else 24 assert flavor in ('core', 'es', 'compatibility', 'legacy')
21 sdl.GL_CONTEXT_PROFILE_COMPATIBILITY) 25 profile = (sdl.GL_CONTEXT_PROFILE_CORE if flavor == 'core' else
26 sdl.GL_CONTEXT_PROFILE_ES if flavor == 'es' else
27 sdl.GL_CONTEXT_PROFILE_COMPATIBILITY)
22 28
23 version = str(options['version']) 29 version = str(options['version'])
24 assert len(version) == 3 and version[1] == '.' 30 assert len(version) == 3 and version[1] == '.'
25 major = int(version[0]) 31 major = int(version[0])
26 minor = int(version[2]) 32 minor = int(version[2])
27 33
28 maybe_double_buffer = options['double-buffer'] 34 maybe_double_buffer = options['double-buffer']
29 double_buffer = maybe_double_buffer if maybe_double_buffer is not None else -1 35 double_buffer = maybe_double_buffer if maybe_double_buffer is not None else -1
30 use_debug_group = (major == 4 and minor >= 3) or major > 4
31 use_vao = (major == 3 and minor >= 1) or major > 3
32 36
33 is_legacy = flavor_name == 'legacy' 37 is_legacy = flavor == 'legacy'
34 is_gles = flavor_name == 'es' 38 is_gles = flavor == 'es'
35
36 if not is_gles:
37 try:
38 glsl_version = {'2.0': 110, '2.1': 120, '3.0': 130, '3.1': 140, '3.2': 150}[version]
39 except KeyError:
40 assert (major == 3 and minor == 3) or major > 3
41 glsl_version = 100 * major + 10 * minor
42 shader_header = '#version %d\n' % glsl_version
43 else:
44 glsl_version = {'2.0': 100, '3.0': 300}[version]
45 shader_header = '#version %d\n\nprecision highp float;\n' % glsl_version
46 39
47 #TODO: check for framebuffer/renderbuffer support. 40 #TODO: check for framebuffer/renderbuffer support.
48 41
49 from pytouhou.ui.opengl.gamerenderer import GameRenderer 42 from pytouhou.ui.opengl.gamerenderer import GameRenderer
50 43
51 44
45 def discover_features():
46 '''Discover which features are supported by our context.'''
47
48 global use_debug_group, use_vao, shader_header
49
50 version = epoxy_gl_version()
51 is_desktop = epoxy_is_desktop_gl()
52
53 use_debug_group = (is_desktop and version >= 43) or epoxy_has_gl_extension('GL_KHR_debug')
54 use_vao = (is_desktop and version >= 30) or epoxy_has_gl_extension('GL_ARB_vertex_array_object')
55
56 if is_desktop:
57 # gl_FragColor isn’t supported anymore starting with GLSL 4.2.
58 if version >= 42:
59 version = 41
60 try:
61 glsl_version = {20: 110, 21: 120, 30: 130, 31: 140, 32: 150}[version]
62 except KeyError:
63 assert version >= 33
64 glsl_version = version * 10
65 shader_header = '#version %d\n\n' % glsl_version
66 else:
67 # The attribute keyword isn’t supported past GLSL ES 3.0.
68 if version >= 30:
69 version = 20
70 glsl_version = {20: '100', 30: '300 es'}[version]
71 shader_header = '#version %s\n\nprecision highp float;\n\n' % glsl_version
72
73
52 def create_window(title, x, y, width, height): 74 def create_window(title, x, y, width, height):
53 sdl.gl_set_attribute(sdl.GL_CONTEXT_PROFILE_MASK, flavor) 75 '''Create a window (using SDL) and an OpenGL context.'''
76
77 sdl.gl_set_attribute(sdl.GL_CONTEXT_PROFILE_MASK, profile)
54 sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, major) 78 sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, major)
55 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, minor) 79 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, minor)
56 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24) 80 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24)
57 if double_buffer >= 0: 81 if double_buffer >= 0:
58 sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, double_buffer) 82 sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, double_buffer)
63 if not is_legacy: 87 if not is_legacy:
64 flags |= sdl.WINDOW_RESIZABLE 88 flags |= sdl.WINDOW_RESIZABLE
65 89
66 window = Window(title, x, y, width, height, flags) 90 window = Window(title, x, y, width, height, flags)
67 window.gl_create_context() 91 window.gl_create_context()
92
93 discover_features()
68 94
69 if use_debug_group: 95 if use_debug_group:
70 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "OpenGL initialisation") 96 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "OpenGL initialisation")
71 97
72 # Initialize OpenGL 98 # Initialize OpenGL