简体   繁体   中英

Reset glContext of a pyqt5 widget

i am using glumpy to visualize graphs. Glumpy takes care of uploading data to the GPU using openGl. I am using the "pyqt5" backend.

I encounterd some problems using glumpy. If i bind buffers on the gpu frequently, the program starts to run very slow. I deleted the glumpy programs cpu side (python del ), also i do not keep any reference and gc should take care of it. By doing a heap profiling, figured out, that the only reason for the performance loss is not dereferenced memory on the GPU.

Glumpy is using the glContext of QGLWidget. here

        if context.isValid():
            self._native_window = QtOpenGL.QGLWidget(context)
        else:
            self._native_window = QtOpenGL.QGLWidget(__glformat__)

Reload the whole widget does not work for me.

Is there any way to reload/reinstanciate/refresh the glContext so it unbinds all the buffers?

I solved it.

First of all, the allocated memory on the GPU was not the reason for my performance issues. However, glumpy can "free" GPU memory by the build in GLObject.deactivate() method. (I had to write the deactivate() method on the label-renderer agg-glyph-collection myself.

Every GLObject has this method and for me the gloo.Program.deactivate() method solved it by setting the size of the binded buffer to 0.

My issues were caused by the _event_stack in the app.window.event class. You can modify the _event.stack by attach(event_handler) and the remove_handlers(handler) method. If you delete an (shader-)program, the event_handlers are not automatically detached. It is an event stack, but has also layers (dicts), the remove_handlers method will find the first layer (frame) where one of the given handlers is attached and delete all occurrence of any given handler but only in this layer/frame .

So for my purpose, the solution was to detach all of my programs separately.

    self.program_points.deactivate()
    self.skip_plane_program.deactivate()
    self.axes_program.deactivate()
    self.labels_graph_program.deactivate()
    self.labels_axis_program.deactivate()
    # this is necessary because glumpy breaks symmetry between push and pop on event_stack
    window.remove_handlers(self.program_points['transform'])
    window.remove_handlers(self.axes_program['transform'])
    window.remove_handlers(self.labels_graph_program['transform'])
    window.remove_handlers(self.skip_plane_program['transform'])
    window.remove_handlers(self.labels_axis_program['transform'])

This does not work as expected:

 window.remove_handlers(self.program_points['transform'], self.axes_program['transform'], self.labels_graph_program['transform'], self.labels_axis_program['transform'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM