Skip to content

make antialiasing optional for paintGL in PlotCurveItem#1932

Merged
j9ac9k merged 3 commits intopyqtgraph:masterfrom
marsipu:gl_antialias
Jul 30, 2021
Merged

make antialiasing optional for paintGL in PlotCurveItem#1932
j9ac9k merged 3 commits intopyqtgraph:masterfrom
marsipu:gl_antialias

Conversation

@marsipu
Copy link
Copy Markdown
Contributor

@marsipu marsipu commented Jul 29, 2021

Summary

This PR makes antialiasing optional when OpenGL is used with PlotCurveItem as described in #1926.

I checked for antialias in self._exportOpts as it is done in paint. Is this necessary for this to be inside paint/paintGL or could it also be in e.g. updateData to improve performance a little bit?

gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If aa is False, you would need to glDisable whatever got enabled when aa is True.
I tried out this PR with #1910 and toggling AA from False->True->False would get a plot different from the initial plot.

Copy link
Copy Markdown
Contributor Author

@marsipu marsipu Jul 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pijyoi Thank you for the hint, I haven't thought of that. I am no OpenGL-expert so I just reverted to the default-values from the OpenGL-documentation. Now this lets me toggle between aa-states in your (really nice) example.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In GLGridItem.py (l.68-72), there is a similar passage. Do you think it has the same problem then and should I fix it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 3 other places that setup GL_LINE_SMOOTH: GLAxisItem, GLGridItem, GLLinePlotItem. GLAxisItem doesn't enable blending, but would probably get it enabled by GLGridItem. A proper fix might require all GLGraphicsItem(s) to disable everything that they have enabled before they exit their paint.

I would suggest to leave them as is. Don't modify things that we aren't able to test ourselves. Or raise a separate issue.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably after painting with antialiasing on, GL_LINE_SMOOTH should be disabled.
If we take a peek at: https://code.woboq.org/qt6/qtbase/src/opengl/qopenglpaintengine.cpp.html#_ZN28QOpenGL2PaintEngineExPrivate12resetGLStateEv
GL_LINE_SMOOTH happens to be one of the things that Qt library doesn't disable for us.
On the other hand, GL_BLEND is disabled for us, so we didn't actually need to disable it for non-antialiased mode.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so should I remove those lines again?

gl.glDisable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_ONE, gl.GL_ZERO)
gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_DONT_CARE)

Copy link
Copy Markdown
Contributor Author

@marsipu marsipu Jul 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems to work too, but performance doesn't seem to be influenced by just removing the lines. Do you think we could introduce an attribute like _aa_state to PlotCurveItem which then calls the glEnable/glDisable-functions only when the state changes?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that when you have called beginNativePainting(), Qt makes little guarantee about the state. So things that we need, we need to set again for each frame. E.g. What would happen if we created 2 PlotWidgets in a single QMainWindow, one with antialiasing enabled and the other disabled? I would guess that because of the shared OpenGL context, they would see the last set setting of GL_LINE_SMOOTH.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should I just remove the above lines to have a bit less to call when AA is disabled?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Based on my understanding, the only line needed would be

gl.glDisable(gl.GL_LINE_SMOOTH)

Btw, it's not so much for performance to reduce the number of lines here. Code like this tends to get copied around, so if we can, we keep the code to only the bits necessary.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Jul 30, 2021

Thanks for the PR @marsipu and thanks for the review/feedback @pijyoi !

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.

3 participants