changeset 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 00f228b57840
children 8a8c2e519637
files pytouhou/lib/opengl.pxd pytouhou/ui/opengl/backend.pyx pytouhou/ui/opengl/background.pyx pytouhou/ui/opengl/gamerenderer.pyx pytouhou/ui/opengl/renderer.pyx pytouhou/ui/opengl/shader.pyx pytouhou/ui/opengl/texture.pyx
diffstat 7 files changed, 59 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/lib/opengl.pxd
+++ b/pytouhou/lib/opengl.pxd
@@ -88,6 +88,10 @@ cdef extern from 'epoxy/gl.h' nogil:
         GL_DEPTH_ATTACHMENT
         GL_FRAMEBUFFER_COMPLETE
 
+        # Debug
+
+        GL_DEBUG_SOURCE_APPLICATION
+
     void glVertexPointer(GLint size, GLenum type_, GLsizei stride, GLvoid *pointer)
     void glTexCoordPointer(GLint size, GLenum type_, GLsizei stride, GLvoid *pointer)
     void glColorPointer(GLint size, GLenum type_, GLsizei stride, GLvoid *pointer)
@@ -160,3 +164,8 @@ cdef extern from 'epoxy/gl.h' nogil:
     void glGenVertexArrays(GLsizei n, GLuint *arrays)
     void glDeleteVertexArrays(GLsizei n, const GLuint *arrays)
     void glBindVertexArray(GLuint array)
+
+    # Debug
+
+    void glPushDebugGroup(GLenum source, GLuint id_, GLsizei length, const char *message)
+    void glPopDebugGroup()
--- a/pytouhou/ui/opengl/backend.pyx
+++ b/pytouhou/ui/opengl/backend.pyx
@@ -4,7 +4,8 @@ from pytouhou.lib.sdl cimport Window
 from pytouhou.lib.opengl cimport \
          (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND,
           GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
-          GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY)
+          GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
+          glPushDebugGroup, GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup)
 
 
 GameRenderer = None
@@ -65,6 +66,7 @@ def create_window(title, x, y, width, he
     window.gl_create_context()
 
     # Initialize OpenGL
+    glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "OpenGL initialisation")
     glEnable(GL_BLEND)
     if is_legacy:
         glEnable(GL_TEXTURE_2D)
@@ -73,5 +75,6 @@ def create_window(title, x, y, width, he
         glEnableClientState(GL_COLOR_ARRAY)
         glEnableClientState(GL_VERTEX_ARRAY)
         glEnableClientState(GL_TEXTURE_COORD_ARRAY)
+    glPopDebugGroup()
 
     return window
--- a/pytouhou/ui/opengl/background.pyx
+++ b/pytouhou/ui/opengl/background.pyx
@@ -22,7 +22,8 @@ from pytouhou.lib.opengl cimport \
           GL_ONE_MINUS_SRC_ALPHA, GL_TEXTURE_2D, glGenBuffers, glEnable,
           glDisable, GL_DEPTH_TEST, glDrawElements, GL_TRIANGLES,
           GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER, glDeleteBuffers,
-          glGenVertexArrays, glDeleteVertexArrays, glBindVertexArray)
+          glGenVertexArrays, glDeleteVertexArrays, glBindVertexArray,
+          glPushDebugGroup, GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup)
 
 from .sprite cimport get_sprite_rendering_data
 from .backend cimport is_legacy, use_vao
@@ -45,15 +46,16 @@ cdef class BackgroundRenderer:
 
     def __init__(self):
         if not is_legacy:
+            glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Background creation")
             glGenBuffers(1, &self.vbo)
             glGenBuffers(1, &self.ibo)
 
             if use_vao:
                 glGenVertexArrays(1, &self.vao)
-
                 glBindVertexArray(self.vao)
                 self.set_state()
                 glBindVertexArray(0)
+            glPopDebugGroup()
 
 
     cdef void set_state(self) nogil:
@@ -70,6 +72,7 @@ cdef class BackgroundRenderer:
 
 
     cdef void render_background(self):
+        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)
@@ -94,6 +97,7 @@ cdef class BackgroundRenderer:
             else:
                 glBindBuffer(GL_ARRAY_BUFFER, 0)
                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
+        glPopDebugGroup()
 
 
     cdef void load(self, background, Renderer renderer):
@@ -140,6 +144,7 @@ cdef class BackgroundRenderer:
             self.vertex_buffer = <Vertex*> realloc(vertex_buffer, nb_vertices * sizeof(Vertex))
             self.indices = <GLushort*> realloc(indices, nb_indices * sizeof(GLushort))
         else:
+            glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Background uploading")
             glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
             glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), vertex_buffer, GL_STATIC_DRAW)
             glBindBuffer(GL_ARRAY_BUFFER, 0)
@@ -147,3 +152,4 @@ cdef class BackgroundRenderer:
             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)
+            glPopDebugGroup()
--- a/pytouhou/ui/opengl/gamerenderer.pyx
+++ b/pytouhou/ui/opengl/gamerenderer.pyx
@@ -20,7 +20,8 @@ from pytouhou.lib.opengl cimport \
           glEnable, glFogi, glFogf, glFogfv, GL_PROJECTION, GL_MODELVIEW,
           GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START, GL_FOG_END,
           GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat, glViewport, glScissor,
-          GL_SCISSOR_TEST, GL_DEPTH_BUFFER_BIT)
+          GL_SCISSOR_TEST, GL_DEPTH_BUFFER_BIT, glPushDebugGroup,
+          GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup)
 
 from pytouhou.utils.matrix cimport mul, new_identity
 from pytouhou.utils.maths cimport perspective, setup_camera, ortho_2d
@@ -93,9 +94,11 @@ cdef class GameRenderer(Renderer):
         self.render_interface(game.interface, game.boss)
 
         if not is_legacy:
+            glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Scaled rendering")
             self.passthrough_shader.bind()
             self.passthrough_shader.uniform_matrix('mvp', self.interface_mvp)
             self.render_framebuffer(self.framebuffer)
+            glPopDebugGroup()
 
 
     cdef void render_game(self, Game game):
@@ -106,6 +109,8 @@ cdef class GameRenderer(Renderer):
         cdef unsigned char fog_r, fog_g, fog_b
         cdef Matrix *mvp
 
+        glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Game rendering")
+
         game_x, game_y = game.interface.game_pos
         glViewport(game_x, game_y, game.width, game.height)
         glClear(GL_DEPTH_BUFFER_BIT)
@@ -204,6 +209,7 @@ cdef class GameRenderer(Renderer):
             self.render_quads([rect], [(color1, color1, color2, color2)], 0)
 
         glDisable(GL_SCISSOR_TEST)
+        glPopDebugGroup()
 
 
     cdef void render_text(self, dict texts):
@@ -234,6 +240,7 @@ cdef class GameRenderer(Renderer):
 
         elements = []
 
+        glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Interface rendering")
         if is_legacy:
             glMatrixMode(GL_MODELVIEW)
             glLoadMatrixf(<GLfloat*>self.interface_mvp)
@@ -262,3 +269,4 @@ cdef class GameRenderer(Renderer):
         self.render_elements(elements)
         for label in labels:
             label.changed = False
+        glPopDebugGroup()
--- a/pytouhou/ui/opengl/renderer.pyx
+++ b/pytouhou/ui/opengl/renderer.pyx
@@ -33,7 +33,8 @@ from pytouhou.lib.opengl cimport \
           GL_FRAMEBUFFER_COMPLETE, glClear, GL_COLOR_BUFFER_BIT,
           GL_DEPTH_BUFFER_BIT, GLuint, glDeleteTextures,
           GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, glGenVertexArrays,
-          glDeleteVertexArrays, glBindVertexArray)
+          glDeleteVertexArrays, glBindVertexArray, glPushDebugGroup,
+          GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup)
 
 from pytouhou.lib.sdl import SDLError
 
@@ -117,6 +118,7 @@ cdef class Renderer:
         if not is_legacy:
             framebuffer_indices[:] = [0, 1, 2, 2, 3, 0]
 
+            glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Renderer creation")
             glGenBuffers(1, &self.vbo)
             glGenBuffers(1, &self.framebuffer_vbo)
             glGenBuffers(1, &self.framebuffer_ibo)
@@ -134,6 +136,7 @@ cdef class Renderer:
                 glBindVertexArray(self.framebuffer_vao)
                 self.set_framebuffer_state()
                 glBindVertexArray(0)
+            glPopDebugGroup()
 
 
     cdef void set_state(self) nogil:
@@ -204,6 +207,7 @@ cdef class Renderer:
 
             nb_vertices += 4
 
+        glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Elements drawing")
         if is_legacy:
             glVertexPointer(3, GL_SHORT, sizeof(Vertex), &self.vertex_buffer[0].x)
             glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u)
@@ -243,6 +247,7 @@ cdef class Renderer:
 
         if not is_legacy and use_vao:
             glBindVertexArray(0)
+        glPopDebugGroup()
 
 
     cdef void render_quads(self, rects, colors, GLuint texture):
@@ -262,6 +267,7 @@ cdef class Renderer:
             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)
             buf[4*i+3] = Vertex(r.x, r.y + r.h, 0, 0, 0, 1, c4.r, c4.g, c4.b, c4.a)
 
+        glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Quads drawing")
         if is_legacy:
             glVertexPointer(3, GL_SHORT, sizeof(Vertex), &buf[0].x)
             glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &buf[0].u)
@@ -279,6 +285,7 @@ cdef class Renderer:
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
         glBindTexture(GL_TEXTURE_2D, texture)
         glDrawElements(GL_TRIANGLES, 6 * length, GL_UNSIGNED_SHORT, indices)
+        glPopDebugGroup()
 
 
     cdef void render_framebuffer(self, Framebuffer fb):
@@ -286,6 +293,8 @@ cdef class Renderer:
 
         assert not is_legacy
 
+        glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Framebuffer drawing")
+
         glBindFramebuffer(GL_FRAMEBUFFER, 0)
         glViewport(self.x, self.y, self.width, self.height)
         glBlendFunc(GL_ONE, GL_ZERO)
@@ -313,6 +322,7 @@ cdef class Renderer:
             glBindVertexArray(0)
         else:
             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
+        glPopDebugGroup()
 
 
 cdef class Framebuffer:
@@ -322,6 +332,8 @@ cdef class Framebuffer:
         self.width = width
         self.height = height
 
+        glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Framebuffer creation")
+
         glGenTextures(1, &self.texture)
         glBindTexture(GL_TEXTURE_2D, self.texture)
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
@@ -346,5 +358,7 @@ cdef class Framebuffer:
         assert glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE
         glBindFramebuffer(GL_FRAMEBUFFER, 0)
 
+        glPopDebugGroup()
+
     cpdef bind(self):
         glBindFramebuffer(GL_FRAMEBUFFER, self.fbo)
--- a/pytouhou/ui/opengl/shader.pyx
+++ b/pytouhou/ui/opengl/shader.pyx
@@ -15,7 +15,8 @@ from pytouhou.lib.opengl cimport \
           GL_COMPILE_STATUS, GL_INFO_LOG_LENGTH, glGetShaderInfoLog,
           glAttachShader, glLinkProgram, glGetProgramiv, glGetProgramInfoLog,
           GL_LINK_STATUS, glUseProgram, glGetUniformLocation, glUniform1fv,
-          glUniform4fv, glUniformMatrix4fv, glBindAttribLocation)
+          glUniform4fv, glUniformMatrix4fv, glBindAttribLocation,
+          glPushDebugGroup, GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup)
 
 from libc.stdlib cimport malloc, free
 from .backend cimport shader_header
@@ -29,6 +30,8 @@ cdef class Shader:
     # vert and frag take arrays of source strings the arrays will be
     # concattenated into one string by OpenGL
     def __init__(self, vert=None, frag=None):
+        glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Program creation")
+
         # create the program handle
         self.handle = glCreateProgram()
         # we are not linked yet
@@ -50,6 +53,8 @@ cdef class Shader:
         # attempt to link the program
         self.link()
 
+        glPopDebugGroup()
+
     cdef void create_shader(self, const GLchar *string, GLenum shader_type):
         cdef GLint temp
         cdef const GLchar *strings[2]
--- a/pytouhou/ui/opengl/texture.pyx
+++ b/pytouhou/ui/opengl/texture.pyx
@@ -16,7 +16,8 @@ from pytouhou.lib.opengl cimport \
          (glTexParameteri, GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER,
           GL_LINEAR, GL_BGRA, GL_RGBA, GL_RGB, GL_LUMINANCE, GL_UNSIGNED_BYTE,
           GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_UNSIGNED_SHORT_4_4_4_4,
-          glGenTextures, glBindTexture, glTexImage2D, GL_TEXTURE_2D, GLuint)
+          glGenTextures, glBindTexture, glTexImage2D, GL_TEXTURE_2D, GLuint,
+          glPushDebugGroup, GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup)
 
 from pytouhou.lib.sdl cimport load_png, create_rgb_surface
 from pytouhou.lib.sdl import SDLError
@@ -37,13 +38,17 @@ cdef class TextureManager:
 
 
     cdef void load(self, dict anms):
+        glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Texture loading")
         for anm in sorted(anms.values(), key=is_ascii):
+            glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Loading textures from ANM %s" % anm)
             for entry in anm:
                 if entry.texture is None:
                     texture = decode_png(self.loader, entry.first_name, entry.secondary_name)
                 elif not isinstance(entry.texture, self.texture_class):
                     texture = entry.texture
                 entry.texture = self.texture_class(load_texture(texture), self.renderer)
+            glPopDebugGroup()
+        glPopDebugGroup()
         anms.clear()
 
 
@@ -61,6 +66,7 @@ cdef class FontManager:
     cdef void load(self, dict labels):
         cdef NativeText label
 
+        glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Text rendering")
         for i, label in labels.items():
             if label.texture is None:
                 try:
@@ -81,6 +87,7 @@ cdef class FontManager:
 
                 texture = Texture(label.width, label.height, -4, surface.pixels)
                 label.texture = self.texture_class(load_texture(texture), self.renderer)
+        glPopDebugGroup()
 
 
 cdef decode_png(loader, first_name, secondary_name):