Skip to content

PColorMeshItem: implement opengl rendering#3090

Merged
j9ac9k merged 2 commits intopyqtgraph:masterfrom
pijyoi:pcmi-opengl
Jul 11, 2024
Merged

PColorMeshItem: implement opengl rendering#3090
j9ac9k merged 2 commits intopyqtgraph:masterfrom
pijyoi:pcmi-opengl

Conversation

@pijyoi
Copy link
Copy Markdown
Contributor

@pijyoi pijyoi commented Jul 7, 2024

PColorMeshItem is unusable when a non-small mesh is used.
Even panning a static PColorMeshItem is very slow. (i.e. just redrawing the rendered QPicture is slow)

Following the example of the recent OpenGL code updates to PlotCurveItem, we can make PColorMeshItem usable by rendering using OpenGL.

An example to demonstrate the speed.
We can use the peegee icons of various sizes to test out the performance.
On my laptop, peegee_256px.png without OpenGL gets a dismal 5 fps; whereas with OpenGL, peegee_512@2x.png hits 30 fps. i.e. an improvement of (1024/256)**2 * 30/5 == 96x

import sys

import numpy as np

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
from pyqtgraph.examples.utils import FrameCounter

qimage = QtGui.QImage(sys.argv[1])
qimage.convertTo(QtGui.QImage.Format.Format_Grayscale8)
z = pg.functions.ndarray_from_qimage(qimage)


Z = z
ny, nx = z.shape
x = np.linspace(0, nx, nx+1, endpoint=True)
y = np.linspace(0, ny, ny+1, endpoint=True)
X, Y = np.meshgrid(x, y)
X += (ny * 0.025) * np.sin(2*np.pi*2*np.linspace(0, 1, ny+1))[:, np.newaxis]
Y += (nx * 0.025) * np.sin(2*np.pi*2*np.linspace(0, 1, nx+1))

pg.setConfigOptions(useOpenGL=True, enableExperimental=True)

pg.mkQApp()
win = pg.PlotWidget()
win.invertY(True)
cmap = pg.colormap.ColorMap(None, [0.0, 1.0])
pcmi = pg.PColorMeshItem(X, Y, Z, colorMap=cmap)
win.addItem(pcmi)
win.setTitle('')
win.show()

cnt = 0
def update():
    global cnt
    cnt = (cnt + 1) % z.shape[1]
    Z = np.roll(z, cnt, axis=1)
    pcmi.setData(None, None, Z)
    framecnt.update()

timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start()

framecnt = FrameCounter()
framecnt.sigFpsUpdate.connect(lambda fps: win.setTitle(f'{fps:.1f} fps'))

pg.exec()

An addition to the PColorMeshItem API. A use-case that I have is that the mesh (XY) is fixed and only the Z values are varying. I have overloaded the setData(None, None, Z) to mean that the existing X, Y should be re-used. This allows to not waste time uploading vertices that have not changed.
(The converse also works: setData(X, Y, None) means that Z should be re-used; although I don't have such a use-case)

One wart is that on termination, the following error message is shown:

QOpenGLTexturePrivate::destroy() called without a current context.
Texture has not been destroyed

This message doesn't show up on a pure QOpenGLWidget implementation. (as opposed to this PR's doing OpenGL painting within a QGraphicsView)

@pijyoi pijyoi force-pushed the pcmi-opengl branch 2 times, most recently from 768450a to 0708ee4 Compare July 9, 2024 22:03
@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Jul 10, 2024

Just want to post the screenshot for the fun of it:

image

@pijyoi pijyoi marked this pull request as ready for review July 11, 2024 12:49
@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Jul 11, 2024

Thanks for this @pijyoi this looks good to me! Merging!

@j9ac9k j9ac9k merged commit 2e7ef93 into pyqtgraph:master Jul 11, 2024
@pijyoi pijyoi deleted the pcmi-opengl branch July 11, 2024 19:46
@pijyoi pijyoi mentioned this pull request Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants