annotate pytouhou/ui/opengl/renderer.pyx @ 638:a806f28e94fc

Add anm0 support.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 03 Jul 2019 22:17:04 +0200
parents ec972eb44391
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
15 from libc.stdlib cimport malloc, free
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
16 from libc.string cimport memset
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
17 from os.path import join
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
19 from pytouhou.lib.opengl cimport \
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
20 (glVertexPointer, glTexCoordPointer, glColorPointer,
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
21 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc,
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
22 glBindTexture, glDrawElements, glBindBuffer, glBufferData,
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
23 GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE,
518
75ae628522c9 Use shorts instead of ints for vertex position, reducing the size of a vertex to 20 bytes from 24.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
24 GL_UNSIGNED_SHORT, GL_SHORT, GL_FLOAT, GL_SRC_ALPHA,
593
974decb8df4f Only selects between GL_TRIANGLE_STRIP and GL_TRIANGLES once, in the backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
25 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D, glGenBuffers,
974decb8df4f Only selects between GL_TRIANGLE_STRIP and GL_TRIANGLES once, in the backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
26 glDeleteBuffers, GLuint, glDeleteTextures, glGenVertexArrays,
974decb8df4f Only selects between GL_TRIANGLE_STRIP and GL_TRIANGLES once, in the backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
27 glDeleteVertexArrays, glBindVertexArray, glPushDebugGroup,
611
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
28 GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup, glDrawArrays)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
30 from pytouhou.lib.sdl import SDLError
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
31
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
32 from pytouhou.game.element cimport Element
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 from .sprite cimport get_sprite_rendering_data
593
974decb8df4f Only selects between GL_TRIANGLE_STRIP and GL_TRIANGLES once, in the backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
34 from .backend cimport primitive_mode, is_legacy, use_debug_group, use_vao, use_primitive_restart
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
35
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
36 from pytouhou.utils.helpers import get_logger
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
37
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
38 logger = get_logger(__name__)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
41 cdef class Texture:
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
42 def __cinit__(self, GLuint texture, Renderer renderer):
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
43 self.texture = texture
515
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
44
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
45 # Find an unused key in the textures array.
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
46 for key in range(MAX_TEXTURES):
515
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
47 if renderer.textures[key] == 0:
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
48 break
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
49 else:
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
50 raise MemoryError('Too many textures currently loaded, consider increasing MAX_TEXTURES (currently %d).' % MAX_TEXTURES)
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
51
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
52 self.key = key
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
53 self.pointer = &renderer.textures[key]
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
54 self.pointer[0] = texture
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
55 for i in range(2):
515
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
56 renderer.indices[key][i] = self.indices[i]
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
57
537
63440d1e0717 Fix a segfault happening at exit time on Windows.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 532
diff changeset
58 #XXX: keep a reference so that when __dealloc__ is called self.pointer is still valid.
63440d1e0717 Fix a segfault happening at exit time on Windows.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 532
diff changeset
59 self.renderer = renderer
63440d1e0717 Fix a segfault happening at exit time on Windows.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 532
diff changeset
60
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
61 def __dealloc__(self):
515
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
62 if self.texture:
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
63 glDeleteTextures(1, &self.texture)
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
64 if self.pointer != NULL:
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
65 self.pointer[0] = 0
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
66 # The dangling pointers in renderer.indices doesn’t matter, since we
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
67 # won’t use them if no texture is loaded in that slot.
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
68
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
69
468
feecdb4a8928 Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 464
diff changeset
70 cdef long find_objects(Renderer self, object elements) except -1:
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
71 # Don’t type element as Element, or else the overriding of objects won’t work.
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
72 cdef Element obj
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
73 cdef long i = 0
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
74 for element in elements:
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
75 for obj in element.objects:
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
76 sprite = obj.sprite
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
77 if sprite and sprite.visible:
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
78 # warning: no reference is preserved on the object—assuming the object will not die accidentally
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
79 self.elements[i] = <PyObject*>obj
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
80 i += 1
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
81 if i >= 640*3-4:
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
82 return i
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
83 return i
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 cdef class Renderer:
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 def __dealloc__(self):
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 559
diff changeset
88 if not is_legacy:
462
a71b912b45b7 Render to framebuffers first, and reposition some interface elements in the game area.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 461
diff changeset
89 glDeleteBuffers(1, &self.vbo)
a71b912b45b7 Render to framebuffers first, and reposition some interface elements in the game area.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 461
diff changeset
90
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
91 if use_vao:
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
92 glDeleteVertexArrays(1, &self.vao)
627
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
93 glDeleteVertexArrays(1, &self.text_vao)
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
94
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96 def __init__(self, resource_loader):
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
97 self.texture_manager = TextureManager(resource_loader, self, Texture)
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
98 font_name = join(resource_loader.game_dir, 'font.ttf')
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
99 try:
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
100 self.font_manager = FontManager(font_name, 16, self, Texture)
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
101 except SDLError:
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
102 self.font_manager = None
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
103 logger.error('Font file “%s” not found, disabling text rendering altogether.', font_name)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
104
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 559
diff changeset
105 if not is_legacy:
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
106 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
107 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Renderer creation")
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
108
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
109 glGenBuffers(1, &self.vbo)
627
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
110 glGenBuffers(1, &self.text_vbo)
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
111
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
112 if use_vao:
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
113 glGenVertexArrays(1, &self.vao)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
114 glBindVertexArray(self.vao)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
115 self.set_state()
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
116
627
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
117 glGenVertexArrays(1, &self.text_vao)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
118 glBindVertexArray(self.text_vao)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
119 self.set_text_state()
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
120
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
121 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
122 glPopDebugGroup()
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
123
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
124
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
125 cdef void set_state(self) nogil:
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
126 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
127
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
128 #TODO: find a way to use offsetof() instead of those ugly substractions.
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
129 glVertexAttribPointer(0, 3, GL_SHORT, False, sizeof(Vertex), <void*>0)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
130 glEnableVertexAttribArray(0)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
131 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>8)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
132 glEnableVertexAttribArray(1)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
133 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>16)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
134 glEnableVertexAttribArray(2)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
135
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
136 glBindBuffer(GL_ARRAY_BUFFER, 0)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
137
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
138
627
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
139 cdef void set_text_state(self) nogil:
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
140 glBindBuffer(GL_ARRAY_BUFFER, self.text_vbo)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
141
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
142 #TODO: find a way to use offsetof() instead of those ugly substractions.
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
143 glVertexAttribPointer(0, 2, GL_SHORT, False, sizeof(Vertex), <void*>0)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
144 glEnableVertexAttribArray(0)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
145 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>4)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
146 glEnableVertexAttribArray(1)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
147 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>12)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
148 glEnableVertexAttribArray(2)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
149
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
150 glBindBuffer(GL_ARRAY_BUFFER, 0)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
151
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
152
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 611
diff changeset
153 cdef bint render_elements(self, elements) except True:
532
dacdcca59b66 Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 520
diff changeset
154 cdef Element element
301
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
155
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
156 nb_elements = find_objects(self, elements)
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
157 if not nb_elements:
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 611
diff changeset
158 return False
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
159
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
160 nb_vertices = 0
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
161 memset(self.last_indices, 0, sizeof(self.last_indices))
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
162
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
163 for element_idx in range(nb_elements):
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
164 element = <object>self.elements[element_idx]
532
dacdcca59b66 Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 520
diff changeset
165 ox, oy = <short>element.x, <short>element.y
dacdcca59b66 Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 520
diff changeset
166 data = get_sprite_rendering_data(element.sprite)
dacdcca59b66 Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 520
diff changeset
167 key = data.key
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
168
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
169 blendfunc = key & 1
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
170 texture = key >> 1
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
171
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
172 rec = self.indices[texture][blendfunc]
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
173 next_indice = self.last_indices[key]
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
174
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
175 # Pack data in buffer
532
dacdcca59b66 Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 520
diff changeset
176 x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = <short>data.pos[0], <short>data.pos[1], <short>data.pos[2], <short>data.pos[3], <short>data.pos[4], <short>data.pos[5], <short>data.pos[6], <short>data.pos[7], <short>data.pos[8], <short>data.pos[9], <short>data.pos[10], <short>data.pos[11]
dacdcca59b66 Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 520
diff changeset
177 r, g, b, a = data.color[0], data.color[1], data.color[2], data.color[3]
dacdcca59b66 Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 520
diff changeset
178 self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, 0, data.left, data.bottom, r, g, b, a)
dacdcca59b66 Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 520
diff changeset
179 self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, 0, data.right, data.bottom, r, g, b, a)
585
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
180 self.vertex_buffer[nb_vertices+2] = Vertex(x4 + ox, y4 + oy, z4, 0, data.left, data.top, r, g, b, a)
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
181 self.vertex_buffer[nb_vertices+3] = Vertex(x3 + ox, y3 + oy, z3, 0, data.right, data.top, r, g, b, a)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
182
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
183 # Add indices
611
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
184 if is_legacy:
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
185 rec[next_indice] = nb_vertices
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
186 rec[next_indice+1] = nb_vertices + 1
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
187 rec[next_indice+2] = nb_vertices + 3
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
188 rec[next_indice+3] = nb_vertices + 2
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
189 self.last_indices[key] += 4
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
190 elif use_primitive_restart:
585
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
191 rec[next_indice] = nb_vertices
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
192 rec[next_indice+1] = nb_vertices + 1
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
193 rec[next_indice+2] = nb_vertices + 2
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
194 rec[next_indice+3] = nb_vertices + 3
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
195 rec[next_indice+4] = 0xFFFF
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
196 self.last_indices[key] += 5
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
197 else:
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
198 rec[next_indice] = nb_vertices
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
199 rec[next_indice+1] = nb_vertices + 1
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
200 rec[next_indice+2] = nb_vertices + 2
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
201 rec[next_indice+3] = nb_vertices + 1
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
202 rec[next_indice+4] = nb_vertices + 2
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
203 rec[next_indice+5] = nb_vertices + 3
e0166cda75d5 Use primitive-restart to lower the size of our ibo, if supported.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
204 self.last_indices[key] += 6
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
205
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
206 nb_vertices += 4
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
207
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
208 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
209 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Elements drawing")
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
210
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 559
diff changeset
211 if is_legacy:
518
75ae628522c9 Use shorts instead of ints for vertex position, reducing the size of a vertex to 20 bytes from 24.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
212 glVertexPointer(3, GL_SHORT, sizeof(Vertex), &self.vertex_buffer[0].x)
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
213 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u)
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
214 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r)
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
215 else:
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
216 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
217 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), &self.vertex_buffer[0], GL_DYNAMIC_DRAW)
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
218 glBindBuffer(GL_ARRAY_BUFFER, 0)
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
219
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
220 if use_vao:
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
221 glBindVertexArray(self.vao)
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
222 else:
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
223 self.set_state()
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
224
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
225 # Don’t change the state when it’s not needed.
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
226 previous_blendfunc = -1
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
227 previous_texture = -1
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
228
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
229 for key in range(2 * MAX_TEXTURES):
449
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
230 nb_indices = self.last_indices[key]
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
231 if not nb_indices:
d56536ef28e8 Improve render_elements’ speed a lot, and fix it in some corner cases. Thanks liori!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
232 continue
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
233
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
234 blendfunc = key & 1
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
235 texture = key >> 1
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
236
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
237 if blendfunc != previous_blendfunc:
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
238 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
239 if texture != previous_texture:
515
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
240 glBindTexture(GL_TEXTURE_2D, self.textures[texture])
593
974decb8df4f Only selects between GL_TRIANGLE_STRIP and GL_TRIANGLES once, in the backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
241 glDrawElements(primitive_mode, nb_indices, GL_UNSIGNED_SHORT, self.indices[texture][blendfunc])
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
242
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
243 previous_blendfunc = blendfunc
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
244 previous_texture = texture
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
245
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
246 glBindTexture(GL_TEXTURE_2D, 0)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
247
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 559
diff changeset
248 if not is_legacy and use_vao:
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
249 glBindVertexArray(0)
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
250
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
251 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
252 glPopDebugGroup()
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
253
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
254
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 611
diff changeset
255 cdef bint render_quads(self, rects, colors, GLuint texture) except True:
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
256 # There is nothing that batch more than two quads on the same texture, currently.
627
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
257 cdef TextVertex buf[8]
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
258 cdef unsigned short indices[12]
611
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
259
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
260 if not is_legacy:
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
261 if use_primitive_restart:
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
262 indices[:] = [0, 1, 2, 3, 0xffff, 4, 5, 6, 7, 0, 0, 0]
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
263 else:
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
264 indices[:] = [0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4]
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
265
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
266 length = len(rects)
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
267 assert length == len(colors)
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
268
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
269 for i, r in enumerate(rects):
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
270 c1, c2, c3, c4 = colors[i]
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
271
627
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
272 buf[4*i] = TextVertex(r.x, r.y, 0, 0, c1.r, c1.g, c1.b, c1.a)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
273 buf[4*i+1] = TextVertex(r.x + r.w, r.y, 1, 0, c2.r, c2.g, c2.b, c2.a)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
274 buf[4*i+2] = TextVertex(r.x + r.w, r.y + r.h, 1, 1, c3.r, c3.g, c3.b, c3.a)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
275 buf[4*i+3] = TextVertex(r.x, r.y + r.h, 0, 1, c4.r, c4.g, c4.b, c4.a)
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
276
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
277 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
278 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Quads drawing")
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
279
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 559
diff changeset
280 if is_legacy:
627
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
281 glVertexPointer(2, GL_SHORT, sizeof(TextVertex), &buf[0].x)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
282 glTexCoordPointer(2, GL_FLOAT, sizeof(TextVertex), &buf[0].u)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
283 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(TextVertex), &buf[0].r)
461
6af3854ed826 Make NativeText work with the fixed pipeline.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
284 else:
627
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
285 glBindBuffer(GL_ARRAY_BUFFER, self.text_vbo)
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
286 glBufferData(GL_ARRAY_BUFFER, 4 * length * sizeof(TextVertex), buf, GL_DYNAMIC_DRAW)
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
287 glBindBuffer(GL_ARRAY_BUFFER, 0)
461
6af3854ed826 Make NativeText work with the fixed pipeline.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
288
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
289 if use_vao:
627
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
290 glBindVertexArray(self.text_vao)
558
94725968dabb Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 539
diff changeset
291 else:
627
ec972eb44391 Use a specific vbo/vao pair for text rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 617
diff changeset
292 self.set_text_state()
461
6af3854ed826 Make NativeText work with the fixed pipeline.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 456
diff changeset
293
539
d6d9a711253d Fix text sometimes being drawn with a wrong blend func.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 537
diff changeset
294 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
456
cae1ae9de430 Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 449
diff changeset
295 glBindTexture(GL_TEXTURE_2D, texture)
611
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
296
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
297 if is_legacy:
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
298 glDrawArrays(primitive_mode, 0, 4 * length)
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
299 else:
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
300 nb_indices = 5 * length - 1 if use_primitive_restart else 6 * length
a6a191e371c7 Add back a GL_QUADS path for legacy applications.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
301 glDrawElements(primitive_mode, nb_indices, GL_UNSIGNED_SHORT, indices)
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
302
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
303 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
304 glPopDebugGroup()