annotate pytouhou/ui/opengl/backend.pyx @ 582:6e79756b7f42

Don’t call gl*DebugGroup if it isn’t exposed by the driver.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 05 Oct 2014 17:46:51 +0200
parents b8df946d394d
children 47cf4e3d159d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 from pytouhou.lib cimport sdl
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 from pytouhou.lib.sdl cimport Window
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 from pytouhou.lib.opengl cimport \
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND,
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
579
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 559
diff changeset
7 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 559
diff changeset
8 glPushDebugGroup, GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup)
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 GameRenderer = None
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 def init(options):
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
15 global flavor, version, major, minor, double_buffer, is_legacy, use_debug_group, use_vao, shader_header, GameRenderer
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
556
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
17 flavor_name = options['flavor']
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
18 assert flavor_name in ('core', 'es', 'compatibility', 'legacy')
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
19 flavor = (sdl.GL_CONTEXT_PROFILE_CORE if flavor_name == 'core' else
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
20 sdl.GL_CONTEXT_PROFILE_ES if flavor_name == 'es' else
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
21 sdl.GL_CONTEXT_PROFILE_COMPATIBILITY)
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
556
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
23 version = str(options['version'])
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
24 assert len(version) == 3 and version[1] == '.'
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
25 major = int(version[0])
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
26 minor = int(version[2])
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27
554
653a9f087673 Make both double- and single-buffer available on the CLI, but default to SDL’s default.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
28 maybe_double_buffer = options['double-buffer']
653a9f087673 Make both double- and single-buffer available on the CLI, but default to SDL’s default.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
29 double_buffer = maybe_double_buffer if maybe_double_buffer is not None else -1
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
30 use_debug_group = (major == 4 and minor >= 3) or major > 4
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 557
diff changeset
31 use_vao = (major == 3 and minor >= 1) or major > 3
554
653a9f087673 Make both double- and single-buffer available on the CLI, but default to SDL’s default.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
32
556
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
33 is_legacy = flavor_name == 'legacy'
559
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
34 is_gles = flavor_name == 'es'
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35
559
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
36 if not is_gles:
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
37 try:
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
38 glsl_version = {'2.0': 110, '2.1': 120, '3.0': 130, '3.1': 140, '3.2': 150}[version]
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
39 except KeyError:
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
40 assert (major == 3 and minor == 3) or major > 3
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
41 glsl_version = 100 * major + 10 * minor
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
42 shader_header = '#version %d\n' % glsl_version
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
43 else:
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
44 glsl_version = {'2.0': 100, '3.0': 300}[version]
1be60813f7cb Get OpenGL ES 2.0 to work thanks to libepoxy. PCB textures will need swizzle in the shaders since BGRA isn’t natively supported on GLES.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 558
diff changeset
45 shader_header = '#version %d\n\nprecision highp float;\n' % glsl_version
557
0f2af7552462 Don’t hardcode GLSL version in our shaders, instead make them dependent on GL version.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 556
diff changeset
46
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 #TODO: check for framebuffer/renderbuffer support.
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 from pytouhou.ui.opengl.gamerenderer import GameRenderer
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 def create_window(title, x, y, width, height):
556
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
53 sdl.gl_set_attribute(sdl.GL_CONTEXT_PROFILE_MASK, flavor)
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, major)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, minor)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24)
554
653a9f087673 Make both double- and single-buffer available on the CLI, but default to SDL’s default.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
57 if double_buffer >= 0:
653a9f087673 Make both double- and single-buffer available on the CLI, but default to SDL’s default.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
58 sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, double_buffer)
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 flags = sdl.WINDOW_SHOWN | sdl.WINDOW_OPENGL
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 #TODO: legacy can support one of the framebuffer extensions.
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 if not is_legacy:
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 flags |= sdl.WINDOW_RESIZABLE
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 window = Window(title, x, y, width, height, flags)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 window.gl_create_context()
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
69 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
70 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "OpenGL initialisation")
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
71
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 # Initialize OpenGL
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 glEnable(GL_BLEND)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 if is_legacy:
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 glEnable(GL_TEXTURE_2D)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 glHint(GL_FOG_HINT, GL_NICEST)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 glEnableClientState(GL_COLOR_ARRAY)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 glEnableClientState(GL_VERTEX_ARRAY)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
81
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
82 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
83 glPopDebugGroup()
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 return window