changeset 611:a6a191e371c7

Add back a GL_QUADS path for legacy applications.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 21 Dec 2014 19:15:59 +0100
parents 1b31169dc344
children 73f134f84c7f
files pytouhou/lib/opengl.pxd pytouhou/ui/opengl/backend.pyx pytouhou/ui/opengl/background.pxd pytouhou/ui/opengl/background.pyx pytouhou/ui/opengl/renderer.pyx
diffstat 5 files changed, 58 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/lib/opengl.pxd
+++ b/pytouhou/lib/opengl.pxd
@@ -82,6 +82,7 @@ cdef extern from 'epoxy/gl.h' nogil:
         GL_NICEST
 
     ctypedef enum GLenum_mode 'GLenum':
+        GL_QUADS
         GL_TRIANGLES
         GL_TRIANGLE_STRIP
 
--- a/pytouhou/ui/opengl/backend.pyx
+++ b/pytouhou/ui/opengl/backend.pyx
@@ -9,7 +9,7 @@ from pytouhou.lib.opengl cimport \
           glPushDebugGroup, GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup,
           epoxy_gl_version, epoxy_is_desktop_gl, epoxy_has_gl_extension,
           GL_PRIMITIVE_RESTART, glPrimitiveRestartIndex, glPixelStorei,
-          GL_PACK_INVERT_MESA, GL_TRIANGLE_STRIP, GL_TRIANGLES)
+          GL_PACK_INVERT_MESA, GL_QUADS, GL_TRIANGLE_STRIP, GL_TRIANGLES)
 
 
 GameRenderer = None
@@ -65,7 +65,9 @@ cdef void discover_features() except *:
     use_pack_invert = epoxy_has_gl_extension('GL_MESA_pack_invert')
     use_scaled_rendering = not is_legacy  #TODO: try to use the EXT framebuffer extension.
 
-    primitive_mode = GL_TRIANGLE_STRIP if use_primitive_restart else GL_TRIANGLES
+    primitive_mode = (GL_QUADS if is_legacy else
+                      GL_TRIANGLE_STRIP if use_primitive_restart else
+                      GL_TRIANGLES)
 
     if not is_legacy:
         if is_desktop:
--- a/pytouhou/ui/opengl/background.pxd
+++ b/pytouhou/ui/opengl/background.pxd
@@ -16,7 +16,7 @@ cdef class BackgroundRenderer:
 
     # For fixed pipeline.
     cdef Vertex *vertex_buffer
-    cdef GLushort *indices
+    cdef GLushort nb_vertices
 
     cdef void set_state(self) nogil
     cdef void render_background(self) nogil
--- a/pytouhou/ui/opengl/background.pyx
+++ b/pytouhou/ui/opengl/background.pyx
@@ -23,7 +23,7 @@ from pytouhou.lib.opengl cimport \
           glDisable, GL_DEPTH_TEST, glDrawElements, GL_UNSIGNED_SHORT,
           GL_ELEMENT_ARRAY_BUFFER, glDeleteBuffers, glGenVertexArrays,
           glDeleteVertexArrays, glBindVertexArray, glPushDebugGroup,
-          GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup)
+          GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup, glDrawArrays)
 
 from .sprite cimport get_sprite_rendering_data
 from .backend cimport primitive_mode, is_legacy, use_debug_group, use_vao, use_primitive_restart
@@ -34,8 +34,6 @@ cdef class BackgroundRenderer:
         if is_legacy:
             if self.vertex_buffer != NULL:
                 free(self.vertex_buffer)
-            if self.indices != NULL:
-                free(self.indices)
         else:
             glDeleteBuffers(1, &self.vbo)
             glDeleteBuffers(1, &self.ibo)
@@ -80,12 +78,10 @@ cdef class BackgroundRenderer:
             glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Background drawing")
 
         if is_legacy:
-            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:
-            indices = NULL
             if use_vao:
                 glBindVertexArray(self.vao)
             else:
@@ -94,7 +90,10 @@ cdef class BackgroundRenderer:
         glEnable(GL_DEPTH_TEST)
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
         glBindTexture(GL_TEXTURE_2D, self.texture)
-        glDrawElements(primitive_mode, self.nb_indices, GL_UNSIGNED_SHORT, indices)
+        if is_legacy:
+            glDrawArrays(primitive_mode, 0, self.nb_vertices)
+        else:
+            glDrawElements(primitive_mode, self.nb_indices, GL_UNSIGNED_SHORT, NULL)
         glDisable(GL_DEPTH_TEST)
 
         if not is_legacy:
@@ -113,7 +112,9 @@ cdef class BackgroundRenderer:
         cdef GLsizei nb_vertices = 0, nb_indices = 0
 
         vertex_buffer = <Vertex*> malloc(65536 * sizeof(Vertex))
-        indices = <GLushort*> malloc(65536 * sizeof(GLushort))
+
+        if not is_legacy:
+            indices = <GLushort*> 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:
@@ -127,37 +128,38 @@ cdef class BackgroundRenderer:
                 # Pack data
                 vertex_buffer[nb_vertices] = Vertex(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, data.left, data.bottom, r, g, b, a)
                 vertex_buffer[nb_vertices+1] = Vertex(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, data.right, data.bottom, r, g, b, a)
-                vertex_buffer[nb_vertices+2] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, data.left, data.top, r, g, b, a)
-                vertex_buffer[nb_vertices+3] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, data.right, data.top, r, g, b, a)
+                vertex_buffer[nb_vertices+2] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, data.right, data.top, r, g, b, a)
+                vertex_buffer[nb_vertices+3] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, data.left, data.top, r, g, b, a)
 
-                # Add indices
-                if use_primitive_restart:
-                    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 + 3
-                    indices[nb_indices+4] = 0xFFFF
-                else:
-                    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 + 1
-                    indices[nb_indices+4] = nb_vertices + 2
-                    indices[nb_indices+5] = nb_vertices + 3
+                if not is_legacy:
+                    # Add indices
+                    if use_primitive_restart:
+                        indices[nb_indices] = nb_vertices
+                        indices[nb_indices+1] = nb_vertices + 1
+                        indices[nb_indices+2] = nb_vertices + 3
+                        indices[nb_indices+3] = nb_vertices + 2
+                        indices[nb_indices+4] = 0xFFFF
+                    else:
+                        indices[nb_indices] = nb_vertices
+                        indices[nb_indices+1] = nb_vertices + 1
+                        indices[nb_indices+2] = nb_vertices + 3
+                        indices[nb_indices+3] = nb_vertices + 1
+                        indices[nb_indices+4] = nb_vertices + 2
+                        indices[nb_indices+5] = nb_vertices + 3
+
+                    nb_indices += 5 if use_primitive_restart else 6
 
                 nb_vertices += 4
-                nb_indices += 5 if use_primitive_restart else 6
+
+        self.texture = textures[key >> 1]
 
         # 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 = textures[key >> 1]
-        self.nb_indices = nb_indices
-
         if is_legacy:
             self.vertex_buffer = <Vertex*> realloc(vertex_buffer, nb_vertices * sizeof(Vertex))
-            self.indices = <GLushort*> realloc(indices, nb_indices * sizeof(GLushort))
+            self.nb_vertices = nb_vertices
         else:
             if use_debug_group:
                 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Background uploading")
@@ -170,5 +172,7 @@ cdef class BackgroundRenderer:
             glBufferData(GL_ELEMENT_ARRAY_BUFFER, nb_indices * sizeof(GLushort), indices, GL_STATIC_DRAW)
             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
 
+            self.nb_indices = nb_indices
+
             if use_debug_group:
                 glPopDebugGroup()
--- a/pytouhou/ui/opengl/renderer.pyx
+++ b/pytouhou/ui/opengl/renderer.pyx
@@ -25,7 +25,7 @@ from pytouhou.lib.opengl cimport \
           GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D, glGenBuffers,
           glDeleteBuffers, GLuint, glDeleteTextures, glGenVertexArrays,
           glDeleteVertexArrays, glBindVertexArray, glPushDebugGroup,
-          GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup)
+          GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup, glDrawArrays)
 
 from pytouhou.lib.sdl import SDLError
 
@@ -161,7 +161,13 @@ cdef class Renderer:
             self.vertex_buffer[nb_vertices+3] = Vertex(x3 + ox, y3 + oy, z3, 0, data.right, data.top, r, g, b, a)
 
             # Add indices
-            if use_primitive_restart:
+            if is_legacy:
+                rec[next_indice] = nb_vertices
+                rec[next_indice+1] = nb_vertices + 1
+                rec[next_indice+2] = nb_vertices + 3
+                rec[next_indice+3] = nb_vertices + 2
+                self.last_indices[key] += 4
+            elif use_primitive_restart:
                 rec[next_indice] = nb_vertices
                 rec[next_indice+1] = nb_vertices + 1
                 rec[next_indice+2] = nb_vertices + 2
@@ -230,10 +236,12 @@ cdef class Renderer:
         # There is nothing that batch more than two quads on the same texture, currently.
         cdef Vertex buf[8]
         cdef unsigned short indices[12]
-        if use_primitive_restart:
-            indices[:] = [0, 1, 2, 3, 0xffff, 4, 5, 6, 7, 0, 0, 0]
-        else:
-            indices[:] = [0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4]
+
+        if not is_legacy:
+            if use_primitive_restart:
+                indices[:] = [0, 1, 2, 3, 0xffff, 4, 5, 6, 7, 0, 0, 0]
+            else:
+                indices[:] = [0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4]
 
         length = len(rects)
         assert length == len(colors)
@@ -263,10 +271,14 @@ cdef class Renderer:
             else:
                 self.set_state()
 
-        nb_indices = 5 * length - 1 if use_primitive_restart else 6 * length
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
         glBindTexture(GL_TEXTURE_2D, texture)
-        glDrawElements(primitive_mode, nb_indices, GL_UNSIGNED_SHORT, indices)
+
+        if is_legacy:
+            glDrawArrays(primitive_mode, 0, 4 * length)
+        else:
+            nb_indices = 5 * length - 1 if use_primitive_restart else 6 * length
+            glDrawElements(primitive_mode, nb_indices, GL_UNSIGNED_SHORT, indices)
 
         if use_debug_group:
             glPopDebugGroup()