annotate pytouhou/ui/opengl/backend.pyx @ 579:b8df946d394d

Add grouping for OpenGL calls, making traces much more readable.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 17 Aug 2014 16:16:58 +0200
parents 1be60813f7cb
children 6e79756b7f42
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):
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
15 global flavor, version, major, minor, double_buffer, is_legacy, 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
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
30 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
31
556
c34b23e29d16 Make the OpenGL flavor and version options work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 555
diff changeset
32 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
33 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
34
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
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 #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
47
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 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
49
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 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
52 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
53 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
54 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
55 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
56 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
57 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
58
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 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
60
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 #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
62 if not is_legacy:
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 flags |= sdl.WINDOW_RESIZABLE
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 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
66 window.gl_create_context()
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 # Initialize OpenGL
579
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 559
diff changeset
69 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "OpenGL initialisation")
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 glEnable(GL_BLEND)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 if is_legacy:
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 glEnable(GL_TEXTURE_2D)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 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
74 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
75 glEnableClientState(GL_COLOR_ARRAY)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 glEnableClientState(GL_VERTEX_ARRAY)
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
579
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 559
diff changeset
78 glPopDebugGroup()
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 return window