# HG changeset patch # User Emmanuel Gil Peyrot # Date 1386450998 -3600 # Node ID b18f0bd30ad0a5eedd976ee5af3c45700c105b89 # Parent 75ae628522c9cbc83e68d71b542e238319daf614 Optimise background rendering. diff --git a/pytouhou/lib/opengl.pxd b/pytouhou/lib/opengl.pxd --- a/pytouhou/lib/opengl.pxd +++ b/pytouhou/lib/opengl.pxd @@ -20,6 +20,7 @@ IF USE_GLEW: cdef extern from 'GL/gl.h' nogil: ctypedef unsigned int GLuint + ctypedef unsigned short GLushort ctypedef int GLint ctypedef float GLfloat ctypedef float GLclampf @@ -32,6 +33,7 @@ cdef extern from 'GL/gl.h' nogil: ctypedef enum GLenum: GL_ARRAY_BUFFER + GL_ELEMENT_ARRAY_BUFFER GL_STATIC_DRAW GL_DYNAMIC_DRAW GL_UNSIGNED_BYTE diff --git a/pytouhou/ui/opengl/background.pxd b/pytouhou/ui/opengl/background.pxd --- a/pytouhou/ui/opengl/background.pxd +++ b/pytouhou/ui/opengl/background.pxd @@ -1,4 +1,4 @@ -from pytouhou.lib.opengl cimport GLuint +from pytouhou.lib.opengl cimport GLuint, GLushort, GLsizei from .renderer cimport Renderer cdef struct Vertex: @@ -9,10 +9,15 @@ cdef struct Vertex: cdef class BackgroundRenderer: cdef GLuint texture - cdef unsigned short blendfunc, nb_vertices + cdef GLsizei nb_indices + cdef unsigned int use_fixed_pipeline + + # For modern GL. + cdef GLuint vbo, ibo + + # For fixed pipeline. cdef Vertex *vertex_buffer - cdef unsigned int use_fixed_pipeline, vbo - cdef object background + cdef GLushort *indices cdef void render_background(self) except * cdef void load(self, background, Renderer renderer) except * diff --git a/pytouhou/ui/opengl/background.pyx b/pytouhou/ui/opengl/background.pyx --- a/pytouhou/ui/opengl/background.pyx +++ b/pytouhou/ui/opengl/background.pyx @@ -19,20 +19,19 @@ from pytouhou.lib.opengl cimport \ glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc, glBindTexture, glBindBuffer, glBufferData, GL_ARRAY_BUFFER, GL_STATIC_DRAW, GL_UNSIGNED_BYTE, GL_FLOAT, GL_SRC_ALPHA, - GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D, glGenBuffers, - glEnable, glDisable, GL_DEPTH_TEST, glDrawArrays, GL_QUADS) + GL_ONE_MINUS_SRC_ALPHA, GL_TEXTURE_2D, glGenBuffers, glEnable, + glDisable, GL_DEPTH_TEST, glDrawElements, GL_TRIANGLES, + GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER) from .sprite cimport get_sprite_rendering_data cdef class BackgroundRenderer: - def __cinit__(self): - # Allocate buffers - self.vertex_buffer = malloc(65536 * sizeof(Vertex)) - - def __dealloc__(self): - free(self.vertex_buffer) + if self.vertex_buffer != NULL: + free(self.vertex_buffer) + if self.indices != NULL: + free(self.indices) def __init__(self, use_fixed_pipeline): @@ -40,15 +39,19 @@ cdef class BackgroundRenderer: if not use_fixed_pipeline: glGenBuffers(1, &self.vbo) + glGenBuffers(1, &self.ibo) cdef void render_background(self): if self.use_fixed_pipeline: + indices = self.indices glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].x) glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u) glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r) else: glBindBuffer(GL_ARRAY_BUFFER, self.vbo) + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ibo) + indices = NULL #TODO: find a way to use offsetof() instead of those ugly hardcoded values. glVertexAttribPointer(0, 3, GL_FLOAT, False, sizeof(Vertex), 0) @@ -59,23 +62,22 @@ cdef class BackgroundRenderer: glEnableVertexAttribArray(2) glEnable(GL_DEPTH_TEST) - glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[self.blendfunc]) + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glBindTexture(GL_TEXTURE_2D, self.texture) - glDrawArrays(GL_QUADS, 0, self.nb_vertices) + glDrawElements(GL_TRIANGLES, self.nb_indices, GL_UNSIGNED_SHORT, indices) glDisable(GL_DEPTH_TEST) if not self.use_fixed_pipeline: glBindBuffer(GL_ARRAY_BUFFER, 0) + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) cdef void load(self, background, Renderer renderer): cdef float ox, oy, oz, ox2, oy2, oz2 - cdef unsigned short nb_vertices = 0 - cdef Vertex* vertex_buffer + cdef GLsizei nb_vertices = 0, nb_indices = 0 - self.background = background - - vertex_buffer = self.vertex_buffer + vertex_buffer = malloc(65536 * sizeof(Vertex)) + indices = malloc(65536 * sizeof(GLushort)) for ox, oy, oz, model_id, model in background.object_instances: for ox2, oy2, oz2, width_override, height_override, sprite in model: @@ -85,19 +87,38 @@ cdef class BackgroundRenderer: left, right, bottom, top = uvs r, g, b, a = colors + # Pack data vertex_buffer[nb_vertices] = Vertex(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, left, bottom, r, g, b, a) vertex_buffer[nb_vertices+1] = Vertex(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, right, bottom, r, g, b, a) vertex_buffer[nb_vertices+2] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, right, top, r, g, b, a) vertex_buffer[nb_vertices+3] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, left, top, r, g, b, a) + # Add indices + indices[nb_indices] = nb_vertices + indices[nb_indices+1] = nb_vertices + 1 + indices[nb_indices+2] = nb_vertices + 2 + indices[nb_indices+3] = nb_vertices + 2 + indices[nb_indices+4] = nb_vertices + 3 + indices[nb_indices+5] = nb_vertices + nb_vertices += 4 + nb_indices += 6 + + # We only need to keep the rendered vertices and indices in memory, + # either in RAM or in VRAM, they will never change until we implement + # background animation. self.texture = renderer.textures[key >> 1] - self.blendfunc = key & 1 - self.nb_vertices = nb_vertices - self.vertex_buffer = realloc(vertex_buffer, nb_vertices * sizeof(Vertex)) + self.nb_indices = nb_indices - if not self.use_fixed_pipeline: + if self.use_fixed_pipeline: + self.vertex_buffer = realloc(vertex_buffer, nb_vertices * sizeof(Vertex)) + self.indices = realloc(indices, nb_indices * sizeof(GLushort)) + else: glBindBuffer(GL_ARRAY_BUFFER, self.vbo) - glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), &self.vertex_buffer[0], GL_STATIC_DRAW) + glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), vertex_buffer, GL_STATIC_DRAW) glBindBuffer(GL_ARRAY_BUFFER, 0) + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ibo) + glBufferData(GL_ELEMENT_ARRAY_BUFFER, nb_indices * sizeof(GLushort), indices, GL_STATIC_DRAW) + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) diff --git a/pytouhou/ui/opengl/gamerenderer.pxd b/pytouhou/ui/opengl/gamerenderer.pxd --- a/pytouhou/ui/opengl/gamerenderer.pxd +++ b/pytouhou/ui/opengl/gamerenderer.pxd @@ -9,6 +9,7 @@ cdef class GameRenderer(Renderer): cdef Shader game_shader, background_shader, interface_shader, passthrough_shader cdef Framebuffer framebuffer cdef BackgroundRenderer background_renderer + cdef object background cdef void render_game(self, Game game) except * cdef void render_text(self, dict texts) except * diff --git a/pytouhou/ui/opengl/gamerenderer.pyx b/pytouhou/ui/opengl/gamerenderer.pyx --- a/pytouhou/ui/opengl/gamerenderer.pyx +++ b/pytouhou/ui/opengl/gamerenderer.pyx @@ -57,6 +57,7 @@ cdef class GameRenderer(Renderer): def load_background(self, background): + self.background = background if background is not None: self.background_renderer = BackgroundRenderer(self.use_fixed_pipeline) self.background_renderer.load(background, self) @@ -114,7 +115,7 @@ cdef class GameRenderer(Renderer): self.render_elements([game.spellcard_effect]) elif self.background_renderer is not None: - back = self.background_renderer.background + back = self.background x, y, z = back.position_interpolator.values dx, dy, dz = back.position2_interpolator.values fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values