PyQt5 OpenGL swapBuffers very slow
<p>I am trying to make a small application using PyQt5 and PyOpenGL. Everything works fine, however rendering takes way too long with even only one sphere. I tried different routes to try and optimise the speed of the app, and right now I am using a simple QWindow with an OpenGLSurface. </p>

<p>I managed to figure out that it is the context.swapBuffers call that takes a long time to complete and varies between approx. 0.01s (which is fine) and 0.05s (which is way to long), when displaying 1 sphere with some shading and 240 vertices. </p>

<p>Now my questions are the following: Is this normal? If so, is there a way to speed this process up or is this related to how pyqt works, since it is a python wrap around the library? Basically: is there any way for me to continue developing this program without needing to learn c++. It's quite a simple application that just needs to visualise some atomic structure and be able to manipulate it. </p>

<p>Is there another gui toolkit I could maybe use to have less overhead when working with OpenGL from pyopengl?</p>

<p>This is the definition that does the rendering:</p>

<pre><code>def renderNow(self):
    if not self.isExposed():
        return

    self.m_update_pending = False

    needsInitialize = False

    if self.m_context is None:
        self.m_context = QOpenGLContext(self)
        self.m_context.setFormat(self.requestedFormat())
        self.m_context.create()

        needsInitialize = True

    self.m_context.makeCurrent(self)

    if needsInitialize:
        self.m_gl = self.m_context.versionFunctions()
        self.m_gl.initializeOpenGLFunctions()

        self.initialize()

    self.render()

    self.m_context.swapBuffers(self)

    if self.m_animating:
        self.renderLater()
</code></pre>

<p>I am using OpenGl directly without using Qt opengl definitions, the format for the surface is given by:</p>

<pre><code>fmt = QSurfaceFormat()
fmt.setVersion(4, 2)
fmt.setProfile(QSurfaceFormat.CoreProfile)
fmt.setSamples(4)
fmt.setSwapInterval(1)
QSurfaceFormat.setDefaultFormat(fmt)
</code></pre>

<p><strong>Edit1:</strong>
Some more clarification on how my code works: </p>

<pre><code>def render(self):
    t1 = time.time()
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    wtvMatrix = self.camera.get_wtv_mat()
    transformMatrix = matrices.get_projection_matrix(60, self.width() / self.height(), 0.1, 30, matrix=wtvMatrix)
    transformMatrixLocation = glGetUniformLocation(self.shader,"transformMatrix")
    glUniformMatrix4fv(transformMatrixLocation,1,GL_FALSE,transformMatrix)
    eye_pos_loc = glGetUniformLocation(self.shader, "eye_world_pos0")
    glUniform3f(eye_pos_loc, self.camera.position[0], self.camera.position[1], self.camera.position[2])

    glDrawElementsInstanced(GL_TRIANGLES,self.num_vertices,GL_UNSIGNED_INT,None,self.num_objects)
    print("drawing took:{}".format(time.time()-t1))
    self.frame+=1
    t1=time.time()
    self.m_context.swapBuffers(self)
    print('swapping buffers took:{}'.format(time.time()-t1))
</code></pre>

<p>This is the only drawElementsInstanced that I call. Shaders are set up as follows (sorry for the mess):</p>

<pre><code>VERTEX_SHADER = compileShader("""#version 410
                        layout(location = 0) in vec3 vertex_position;
                        layout(location = 1) in vec3 vertex_colour;
                        layout(location = 2) in vec3 vertex_normal;
                        layout(location = 3) in mat4 model_mat;
                        layout(location = 7) in float mat_specular_intensity;
                        layout(location = 8) in float mat_specular_power;
                        uniform mat4 transformMatrix;
                        uniform vec3 eye_world_pos0;
                        out vec3 normal0;
                        out vec3 colour;
                        out vec3 world_pos;
                        out float specular_intensity;
                        out float specular_power;
                        out vec3 eye_world_pos;
                        void main () {

                        colour = vertex_colour;
                        normal0 = (model_mat*vec4(vertex_normal,0.0)).xyz;
                        world_pos = (model_mat*vec4(vertex_position,1.0)).xyz;
                        eye_world_pos = eye_world_pos0;
                        specular_intensity = mat_specular_intensity;
                        specular_power = mat_specular_power;
                        gl_Position = transformMatrix*model_mat*vec4(vertex_position,1.0);


                        }""", GL_VERTEX_SHADER)

        FRAGMENT_SHADER = compileShader("""#version 410
                        in vec3 colour;
                        in vec3 normal0;
                        in vec3 world_pos;
                        in float specular_intensity;
                        in float specular_power;
                        in vec3 eye_world_pos;

                        out vec4 frag_colour;

                        struct directional_light {
                            vec3 colour;
                            float amb_intensity;
                            float diff_intensity;
                            vec3 direction;
                        };

                        uniform directional_light gdirectional_light;

                        void main () {

                        vec4 ambient_colour = vec4(gdirectional_light.colour * gdirectional_light.amb_intensity,1.0f);
                        vec3 light_direction = -gdirectional_light.direction;
                        vec3 normal = normalize(normal0);

                        float diffuse_factor = dot(normal,light_direction);


                        vec4 diffuse_colour = vec4(0,0,0,0);
                        vec4 specular_colour = vec4(0,0,0,0);

                        if (diffuse_factor&gt;0){
                            diffuse_colour = vec4(gdirectional_light.colour,1.0f) * gdirectional_light.diff_intensity*diffuse_factor;
                            vec3 vertex_to_eye = normalize(eye_world_pos-world_pos);
                            vec3 light_reflect = normalize(reflect(gdirectional_light.direction,normal));
                            float specular_factor = dot(vertex_to_eye, light_reflect);
                            if(specular_factor&gt;0) {
                                specular_factor = pow(specular_factor,specular_power);
                                specular_colour = vec4(gdirectional_light.colour*specular_intensity*specular_factor,1.0f);
                            }
                        }


                        frag_colour = vec4(colour,1.0)*(ambient_colour+diffuse_colour+specular_colour);
                        }""", GL_FRAGMENT_SHADER)
</code></pre>

<p>Now the code that I use when I want to rotate the scene is the following (the camera updates etc are as normally done afaik):</p>

<pre><code>def mouseMoveEvent(self, event):

    dx = event.x() - self.lastPos.x()
    dy = event.y() - self.lastPos.y()
    self.lastPos = event.pos()
    if event.buttons() &amp; QtCore.Qt.RightButton:
        self.camera.mouse_update(dx,dy)
    elif event.buttons()&amp; QtCore.Qt.LeftButton:
        pass

    self.renderNow()
</code></pre>

<p>Some final info: All vertex info needed in the shaders is given through a vao that I initialized and bound earlier in the initialize definition, does not contain too many objects (I'm just testing and it uses an icosahedron with 2 subdivisions to render a sphere, also, I removed the duplicate vertices but that did not do anything since that really should not be the bottleneck I think).  </p>

<p>To answer some questions: I did try with varius different versions of opengl just for gigglez, no changes, tried without vsync, nothing changes, tried with different sample sizes, no changes. </p>

<p><strong>Edit2:</strong>
Might be a clue: the swapBuffers takes around 0.015s most of the time, but when I start moving around a lot, it stutters and jumps up to 0.05s for some renders. Why is this happening? From what I understand, every render has to process all the data anyways?</p>

<p>By the way OpenGL works, the rendering commands you submit are sent to the GPU and executed asynchronously (frankly even the process of sending them to the GPU is asynchronous). When you request to display the back buffer by a call to <code>swapBuffers</code> the display driver must wait till the content of the back buffer finishes rendering (i.e. all previously issued commands finish executing), and only then it can swap the buffers.<sup>â </sup></p>

<p>If you experience low frame rate then you shall optimize your rendering code, that is the stuff you submit to the GPU. Switching to C++ will not help you here (though it would be a great idea independently).</p>

<p><strong>EDIT:</strong> You say that when you do nothing then your <code>swapBuffers</code> executes in 0.015 seconds, which is suspiciously ~1/60th of a second. It implies that your rendering code is efficient enough to render at 60 FPS and you have no reason to optimize it yet. What probably happens is that your call to <code>renderNow()</code> from <code>mouseMoveEvent</code> causes re-rendering the scene more than 60 times per second, which is redundant. Instead you should call <code>renderLater()</code> in <code>mouseMoveEvent</code>, and restructure your code accordingly.</p>

<p><strong>NOTE:</strong> you call <code>swapBuffers</code> twice, once in <code>render()</code> and once in <code>renderNow()</code> immediately after.</p>

<p><strong>DISCLAIMER:</strong> I'm not familiar with PyOpenGL. </p>

<hr>

<p>â  <code>swapBuffer</code> may also execute asynchronously, but even then if the display driver swaps buffers faster than you can render you will eventually block on the <code>swapBuffer</code> call.</p>

python qt opengl pyqt pyopengl