Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Antialiasing tests fail with vtk >=9.0.0 #443

Closed
jasonb5 opened this issue Apr 24, 2020 · 9 comments
Closed

Antialiasing tests fail with vtk >=9.0.0 #443

jasonb5 opened this issue Apr 24, 2020 · 9 comments
Milestone

Comments

@jasonb5
Copy link
Contributor

jasonb5 commented Apr 24, 2020

Antiasliasing unittest is failing (https://circleci.com/gh/CDAT/vcs/3293).

https://github.com/CDAT/vcs/blob/master/tests/test_vcs_antialiasing.py

b'======================================================================'
b'FAIL: testAntiAliasing (test_vcs_antialiasing.TestVCSAntialias)'
b'----------------------------------------------------------------------'
b'Traceback (most recent call last):'
b'  File "/home/circleci/project/tests/test_vcs_antialiasing.py", line 11, in testAntiAliasing'
b'    self.assertEqual(x.getantialiasing(), 8)'
b'AssertionError: 0 != 8'
b'-------------------- >> begin captured logging << --------------------'
b'asyncio: DEBUG: Using selector: EpollSelector'
b'--------------------- >> end captured logging << ---------------------'
b'----------------------------------------------------------------------'
b'Ran 1 test in 0.012s'
b'FAILED (failures=1)'
FAILED...cmd: ['nosetests', '-A', 'not vtk_ui', '-s', '/home/circleci/project/tests/test_vcs_antialiasing.py']
@jasonb5
Copy link
Contributor Author

jasonb5 commented Apr 24, 2020

Created environment using:

conda create -n vcs-mesa-18.3.1 -c cdat/label/vtk_dev -c conda-forge/label/vtk_dev -c conda-forge vcs=8.2.2020.04.20.14.05.g2383318 "vtk=9.0.0.rc1" "mesalib=18.3.1" pillow

Conda list conda list "vcs|vtk|mesalib" should produce the following:

# packages in environment at /home/titters/conda/envs/vcs-test:
#
# Name                    Version                   Build  Channel
mesalib                   18.3.1               h590aaf7_0    conda-forge
vcs                       8.2.2020.04.20.14.05.g2383318    pyh9f0ad1d_0    cdat/label/vtk_dev
vtk                       9.0.0.rc1       with_osmesa_py38he42c7bc_4  [mesalib]  conda-forge/label/vtk_dev

It looks like the default antialiasing changed from 8 to 0.

@jasonb5
Copy link
Contributor Author

jasonb5 commented Apr 25, 2020

I ran the following script that generates a baseline image and then attempts to set antialiasing to 8.

Source file: clt.nc

python vcs-test.py 18.3.1

import vcs
import cdms2
import argparse
from PIL import Image, ImageChops

parser = argparse.ArgumentParser()

parser.add_argument('version', type=str)

args = parser.parse_args()

with cdms2.open('clt.nc') as f:
    data = f('clt')

def plot(version, aa=None, gm=None):
    x = vcs.init(bg=True)

    if aa is not None:
        x.setantialiasing(aa)
    else:
        aa = f'baseline'

    x.plot(data, gm, bg=True)

    post_aa = x.backend.renWin.GetMultiSamples()

    fname = f'vcs-aa-mesa_{version}-{aa}-{post_aa}.png'

    x.png(fname)

    x.close()

    return fname

baseline = plot(args.version)
aa0 = plot(args.version, 0)

img1 = Image.open(baseline)
img2 = Image.open(aa0)

diff1 = ImageChops.difference(img1, img2)
diff1.save('vcs-aa-mesa-baseline-aa0.png')

Generated the following plots:

AA set to 0.
vcs-aa-mesa_18 3 1-8-0
AA defaulting to 0 for the baseline though it should be 8.
vcs-aa-mesa_18 3 1-baseline-0
Difference between the above
vcs-aa-mesa-baseline-aa8

The expected output is baseline being generate with aa set to 8 and the second image being generated with aa set to 0 which would cause the third image to show a difference.

Here's the line calling SetMultiSamples, only sets if the render window has been create:

vcs/vcs/VTKPlots.py

Lines 1616 to 1619 in a06bb98

def setantialiasing(self, antialiasing):
self.antialiasing = antialiasing
if self.renWin is not None:
self.renWin.SetMultiSamples(antialiasing)

otherwise it's set when the render window is create:

vcs/vcs/VTKPlots.py

Lines 623 to 636 in a06bb98

def createRenWin(self, *args, **kargs):
if self.contextView is None:
self.contextView = vtk.vtkContextView()
if self.renWin is None:
self.renWin = self.contextView.GetRenderWindow()
self.renWin.SetWindowName("VCS Canvas %i" % self.canvas._canvas_id)
self.renWin.SetAlphaBitPlanes(1)
# turning on Stencil for Labels on iso plots
self.renWin.SetStencilCapable(1)
# turning off antialiasing by default
# mostly so that pngs are same accross platforms
self.renWin.SetMultiSamples(self.antialiasing)

@jasonb5
Copy link
Contributor Author

jasonb5 commented Apr 27, 2020

Installing the following creates a working environment:

conda create -n vcs-mesa-18.3.1_2 -c cdat/label/vtk_dev -c conda-forge/label/vtk_dev -c conda-forge vcs=8.2.2020.04.20.14.05.g2383318 "vtk=8.2.0" "mesalib=18.3.1" pillow

AA set to 0.
vcs-aa-mesa_18 3 1-0-0
AA defaulting to 8 for the baseline.
vcs-aa-mesa_18 3 1-baseline-8
Shows the difference.
vcs-aa-mesa-baseline-aa0

@jasonb5
Copy link
Contributor Author

jasonb5 commented May 5, 2020

NOTE: this is on a local linux machine running ubuntu, another issue is present on a headless machine conda-forge/vtk-feedstock#81.

@muryanto1 Here's the condensed info, in the broken environment we cannot set antialiasing through the SetMultiSamples call but this does work in the working environment. Only difference is the VTK version. The aa_diff.png is blank in the broken environment and shows a difference in the working one.

Broken environment:

conda create -n vcs-mesa-18.3.1-9.0.0 -c cdat/label/vtk_dev -c conda-forge/label/vtk_dev -c conda-forge vtk "mesalib=18.3.1" pillow

Working environment:

conda create -n vcs-mesa-18.3.1-8.2.0 -c cdat/label/vtk_dev -c conda-forge/label/vtk_dev -c conda-forge "vtk=8.2.0" "mesalib=18.3.1" pillow

Scripts:

import vtk
from PIL import ImageChops
from PIL import Image

colors = vtk.vtkNamedColors()

def draw_line(prefix, aa=None):
    renderer = vtk.vtkRenderer()

    window = vtk.vtkRenderWindow()
    window.SetOffScreenRendering(1)
    window.SetSize(640, 480)
    window.AddRenderer(renderer)

    source = vtk.vtkLineSource()
    source.SetPoint1([0.0, 0.0, 0.0])
    source.SetPoint2([640.0, 480.0, 0.0])

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(source.GetOutputPort())

    actor = vtk.vtkActor()
    actor.GetProperty().SetLineWidth(1.0)
    actor.GetProperty().SetColor(colors.GetColor3d('Black'))
    actor.SetMapper(mapper)

    renderer.AddActor(actor)
    renderer.SetBackground(colors.GetColor3d('White'))

    postfix = ''

    if aa is not None:
        window.SetMultiSamples(aa)

        postfix = f'_{aa}'

    value = window.GetMultiSamples()

    postfix = f'{postfix}_{value}'

    window.Render()

    image = vtk.vtkWindowToImageFilter()
    image.SetInput(window)
    image.SetInputBufferTypeToRGB()
    image.ReadFrontBufferOff()
    image.Update()

    filename = f'{prefix}{postfix}.png'

    writer = vtk.vtkPNGWriter()
    writer.SetFileName(filename)
    writer.SetInputConnection(image.GetOutputPort())
    writer.Write()

    return filename

base = draw_line('aa')
aa32 = draw_line('aa', 32)

img1 = Image.open(base)
img2 = Image.open(aa32)

diff = ImageChops.difference(img1, img2)

diff.save('aa_diff.png')

@jasonb5
Copy link
Contributor Author

jasonb5 commented May 6, 2020

Here's the topic on VTK's discourse https://discourse.vtk.org/t/vtk-9-0-0rc3-python-cant-set-multisamples/3223. Will move to a bug once I clarify the usage.

@jasonb5 jasonb5 changed the title Antialiasing tests fail with mesalib=18.3.1=h590aaf7_0 Antialiasing tests fail with vtk >=9.0.0 May 6, 2020
@jasonb5
Copy link
Contributor Author

jasonb5 commented May 12, 2020

Opened a bug on VTK's gitlab https://gitlab.kitware.com/vtk/vtk/-/issues/17891

@jasonb5
Copy link
Contributor Author

jasonb5 commented May 19, 2020

When setantialiasing is called, vcs is calling SetMultiSamples on a RenderWindow.

We had three issues:

  1. Plotting data using the default antialiasing value on linux and osx was displaying jagged outlines as compared to a reference image which showed thicker, smoother lines.
  2. Setting antialiasing in VTK 8.2.0 and 9.0.0 environments created different results (documented above) on linux system
  3. Unittest was failing when checking the default antialiasing value, vcs sets it to 8 but the method returned 0, which is disabled.

Key points from the VTK issue:

  • If SetMultiSamples is called with 32 and your driver/hardware only supports 8 AA (antialiasing) then the VTK library will set the value to 8; the maximum supported value. A subsequent call to GetMultiSamples would return 8 and only values less than or equal to 8 are valid.
  • VTK has changed how they determine the maximum AA value, in 8.2.0 they would call glXChooseFBConfig with a large MultiSample value and continue calling this with smaller values until the call succeeded, in 9.0.0 they now call glGetIntegerv(GL_MAX_SAMPLES, &msamples). I'm not sure of the full effects of this since it's dependent on the OpenGL implementation and driver/hardware which can change.

Issue resolutions:

  1. I'm rechecking this. We're assuming the reference image used the default antialiasing, 8 and the graphics driver/hardware was capable of this.
  2. Aftter some debugging with a VTK developer we determined this was caused due to competing OpenGL libraries; 8.2.0 was using one and 9.0.0 was using the other. These two OpenGL libraries returned different results for the maximum AA value, causing the difference in outputs.
  3. We need to update the unit test, which is being run on headless machine and using software OpenGL. The unittest calls open which calls SetMultiSamples with 8 and we check that the value is 8. This doesn't take into consideration what the maximum possible value is e.g. if the OpenGL implementation doesn't have AA enabled it's possible for it to return 0 or if it's supported value is less than 8. We could possibly use pyopengl or pyglet to get a valid value for the setup.

@jasonb5
Copy link
Contributor Author

jasonb5 commented May 20, 2020

Forgot to mention according to my conversation with the VTK developer antialiasing does not work on headless machines, the behavior should be the same as setting antialiasing to 0 on machines with graphics hardware.

@jasonb5
Copy link
Contributor Author

jasonb5 commented May 21, 2020

Fixes the unittest in e78fed3.

@jasonb5 jasonb5 closed this as completed May 21, 2020
@downiec downiec modified the milestones: 8.2, 8.2.1 Jul 22, 2020
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

No branches or pull requests

2 participants