comparison pytouhou/ui/opengl/renderer.pyx @ 518:75ae628522c9

Use shorts instead of ints for vertex position, reducing the size of a vertex to 20 bytes from 24.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 07 Dec 2013 14:15:43 +0100
parents b3193b43a86c
children c0b3f8709f74
comparison
equal deleted inserted replaced
517:dec43940f092 518:75ae628522c9
19 from pytouhou.lib.opengl cimport \ 19 from pytouhou.lib.opengl cimport \
20 (glVertexPointer, glTexCoordPointer, glColorPointer, 20 (glVertexPointer, glTexCoordPointer, glColorPointer,
21 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc, 21 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc,
22 glBindTexture, glDrawElements, glBindBuffer, glBufferData, 22 glBindTexture, glDrawElements, glBindBuffer, glBufferData,
23 GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE, 23 GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE,
24 GL_UNSIGNED_SHORT, GL_INT, GL_FLOAT, GL_SRC_ALPHA, 24 GL_UNSIGNED_SHORT, GL_SHORT, GL_FLOAT, GL_SRC_ALPHA,
25 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO, GL_TEXTURE_2D, GL_TRIANGLES, 25 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO, GL_TEXTURE_2D, GL_TRIANGLES,
26 glGenBuffers, glBindFramebuffer, glViewport, glDeleteBuffers, 26 glGenBuffers, glBindFramebuffer, glViewport, glDeleteBuffers,
27 glGenTextures, glTexParameteri, glTexImage2D, glGenRenderbuffers, 27 glGenTextures, glTexParameteri, glTexImage2D, glGenRenderbuffers,
28 glBindRenderbuffer, glRenderbufferStorage, glGenFramebuffers, 28 glBindRenderbuffer, glRenderbufferStorage, glGenFramebuffers,
29 glFramebufferTexture2D, glFramebufferRenderbuffer, 29 glFramebufferTexture2D, glFramebufferRenderbuffer,
106 glGenBuffers(1, &self.framebuffer_vbo) 106 glGenBuffers(1, &self.framebuffer_vbo)
107 107
108 108
109 cdef void render_elements(self, elements): 109 cdef void render_elements(self, elements):
110 cdef int key 110 cdef int key
111 cdef int x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, ox, oy 111 cdef short x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, ox, oy
112 cdef float left, right, bottom, top 112 cdef float left, right, bottom, top
113 cdef unsigned char r, g, b, a 113 cdef unsigned char r, g, b, a
114 114
115 nb_elements = find_objects(self, elements) 115 nb_elements = find_objects(self, elements)
116 if not nb_elements: 116 if not nb_elements:
133 133
134 # Pack data in buffer 134 # Pack data in buffer
135 x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = vertices 135 x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = vertices
136 left, right, bottom, top = uvs 136 left, right, bottom, top = uvs
137 r, g, b, a = colors 137 r, g, b, a = colors
138 self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, left, bottom, r, g, b, a) 138 self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, 0, left, bottom, r, g, b, a)
139 self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, right, bottom, r, g, b, a) 139 self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, 0, right, bottom, r, g, b, a)
140 self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, right, top, r, g, b, a) 140 self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, 0, right, top, r, g, b, a)
141 self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, left, top, r, g, b, a) 141 self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, 0, left, top, r, g, b, a)
142 142
143 # Add indices 143 # Add indices
144 rec[next_indice] = nb_vertices 144 rec[next_indice] = nb_vertices
145 rec[next_indice+1] = nb_vertices + 1 145 rec[next_indice+1] = nb_vertices + 1
146 rec[next_indice+2] = nb_vertices + 2 146 rec[next_indice+2] = nb_vertices + 2
150 self.last_indices[key] += 6 150 self.last_indices[key] += 6
151 151
152 nb_vertices += 4 152 nb_vertices += 4
153 153
154 if self.use_fixed_pipeline: 154 if self.use_fixed_pipeline:
155 glVertexPointer(3, GL_INT, sizeof(Vertex), &self.vertex_buffer[0].x) 155 glVertexPointer(3, GL_SHORT, sizeof(Vertex), &self.vertex_buffer[0].x)
156 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u) 156 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u)
157 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r) 157 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r)
158 else: 158 else:
159 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) 159 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
160 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), &self.vertex_buffer[0], GL_DYNAMIC_DRAW) 160 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), &self.vertex_buffer[0], GL_DYNAMIC_DRAW)
161 161
162 #TODO: find a way to use offsetof() instead of those ugly hardcoded values. 162 #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
163 glVertexAttribPointer(0, 3, GL_INT, False, sizeof(Vertex), <void*>0) 163 glVertexAttribPointer(0, 3, GL_SHORT, False, sizeof(Vertex), <void*>0)
164 glEnableVertexAttribArray(0) 164 glEnableVertexAttribArray(0)
165 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>12) 165 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>8)
166 glEnableVertexAttribArray(1) 166 glEnableVertexAttribArray(1)
167 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>20) 167 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>16)
168 glEnableVertexAttribArray(2) 168 glEnableVertexAttribArray(2)
169 169
170 # Don’t change the state when it’s not needed. 170 # Don’t change the state when it’s not needed.
171 previous_blendfunc = -1 171 previous_blendfunc = -1
172 previous_texture = -1 172 previous_texture = -1
204 assert length == len(colors) 204 assert length == len(colors)
205 205
206 for i, r in enumerate(rects): 206 for i, r in enumerate(rects):
207 c1, c2, c3, c4 = colors[i] 207 c1, c2, c3, c4 = colors[i]
208 208
209 buf[4*i] = Vertex(r.x, r.y, 0, 0, 0, c1.r, c1.g, c1.b, c1.a) 209 buf[4*i] = Vertex(r.x, r.y, 0, 0, 0, 0, c1.r, c1.g, c1.b, c1.a)
210 buf[4*i+1] = Vertex(r.x + r.w, r.y, 0, 1, 0, c2.r, c2.g, c2.b, c2.a) 210 buf[4*i+1] = Vertex(r.x + r.w, r.y, 0, 0, 1, 0, c2.r, c2.g, c2.b, c2.a)
211 buf[4*i+2] = Vertex(r.x + r.w, r.y + r.h, 0, 1, 1, c3.r, c3.g, c3.b, c3.a) 211 buf[4*i+2] = Vertex(r.x + r.w, r.y + r.h, 0, 0, 1, 1, c3.r, c3.g, c3.b, c3.a)
212 buf[4*i+3] = Vertex(r.x, r.y + r.h, 0, 0, 1, c4.r, c4.g, c4.b, c4.a) 212 buf[4*i+3] = Vertex(r.x, r.y + r.h, 0, 0, 0, 1, c4.r, c4.g, c4.b, c4.a)
213 213
214 if self.use_fixed_pipeline: 214 if self.use_fixed_pipeline:
215 glVertexPointer(3, GL_INT, sizeof(Vertex), &buf[0].x) 215 glVertexPointer(3, GL_SHORT, sizeof(Vertex), &buf[0].x)
216 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &buf[0].u) 216 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &buf[0].u)
217 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &buf[0].r) 217 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &buf[0].r)
218 else: 218 else:
219 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) 219 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
220 glBufferData(GL_ARRAY_BUFFER, 4 * length * sizeof(Vertex), buf, GL_DYNAMIC_DRAW) 220 glBufferData(GL_ARRAY_BUFFER, 4 * length * sizeof(Vertex), buf, GL_DYNAMIC_DRAW)
221 221
222 #TODO: find a way to use offsetof() instead of those ugly hardcoded values. 222 #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
223 glVertexAttribPointer(0, 3, GL_INT, False, sizeof(Vertex), <void*>0) 223 glVertexAttribPointer(0, 3, GL_SHORT, False, sizeof(Vertex), <void*>0)
224 glEnableVertexAttribArray(0) 224 glEnableVertexAttribArray(0)
225 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>12) 225 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>8)
226 glEnableVertexAttribArray(1) 226 glEnableVertexAttribArray(1)
227 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>20) 227 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>16)
228 glEnableVertexAttribArray(2) 228 glEnableVertexAttribArray(2)
229 229
230 glBindTexture(GL_TEXTURE_2D, texture) 230 glBindTexture(GL_TEXTURE_2D, texture)
231 glDrawElements(GL_TRIANGLES, 6 * length, GL_UNSIGNED_SHORT, indices) 231 glDrawElements(GL_TRIANGLES, 6 * length, GL_UNSIGNED_SHORT, indices)
232 232
247 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 247 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
248 248
249 glBindBuffer(GL_ARRAY_BUFFER, self.framebuffer_vbo) 249 glBindBuffer(GL_ARRAY_BUFFER, self.framebuffer_vbo)
250 250
251 #TODO: find a way to use offsetof() instead of those ugly hardcoded values. 251 #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
252 glVertexAttribPointer(0, 2, GL_INT, False, sizeof(PassthroughVertex), <void*>0) 252 glVertexAttribPointer(0, 2, GL_SHORT, False, sizeof(PassthroughVertex), <void*>0)
253 glEnableVertexAttribArray(0) 253 glEnableVertexAttribArray(0)
254 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(PassthroughVertex), <void*>8) 254 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(PassthroughVertex), <void*>4)
255 glEnableVertexAttribArray(1) 255 glEnableVertexAttribArray(1)
256 256
257 buf[0] = PassthroughVertex(fb.x, fb.y, 0, 1) 257 buf[0] = PassthroughVertex(fb.x, fb.y, 0, 1)
258 buf[1] = PassthroughVertex(fb.x + fb.width, fb.y, 1, 1) 258 buf[1] = PassthroughVertex(fb.x + fb.width, fb.y, 1, 1)
259 buf[2] = PassthroughVertex(fb.x + fb.width, fb.y + fb.height, 1, 0) 259 buf[2] = PassthroughVertex(fb.x + fb.width, fb.y + fb.height, 1, 0)