VTK Chart Overlay Doesn't work with VTK 9

Hey everyone, I’m trying to overlay a chart on a 3d scene. This works in VTK versions < 9. Additionally I found the chart overlay example online doesn’t work with VTK 9.

Example: https://lorensen.github.io/VTKExamples/site/Cxx/Plotting/ChartsOn3DScene/

Is this a bug? Is there any workaround? Here’s the call stack:

vtkRenderingContext2D-9.0.dll!vtkContextActor::RenderOverlay(vtkViewport * viewport) Line 230	C++
vtkRenderingOpenGL2-9.0.dll!vtkOpenGLRenderer::UpdateGeometry(vtkFrameBufferObjectBase * fbo) Line 406	C++
vtkRenderingOpenGL2-9.0.dll!vtkOpenGLRenderer::DeviceRender() Line 263	C++
vtkRenderingCore-9.0.dll!vtkRenderer::Render() Line 378	C++
vtkRenderingCore-9.0.dll!vtkRendererCollection::Render() Line 53	C++
vtkRenderingCore-9.0.dll!vtkRenderWindow::DoStereoRender() Line 340	C++
vtkRenderingCore-9.0.dll!vtkRenderWindow::Render() Line 299	C++
vtkRenderingOpenGL2-9.0.dll!vtkOpenGLRenderWindow::Render() Line 2042	C++
ChartsOn3DScene.exe!main(int __formal, char * * __formal) Line 119	C++

In particular on ln230 of vtkContextActor.cxx it tries to access a NULL device from the context resulting in an access violation reading location thrown.

This appears to already be fixed on master and only present on the VTK 9 release.

Do you know which change is fixing the problem you encountered? Should this fix be included in 9.0.1? We are getting similar problems, but they aren’t resolved with 9.0.1, so I’m wondering whether this is a different issue, or whether the fix just isn’t included in 9.0.1 yet…

Sadly I can’t quite remember what fixed it. It was a build change. The VTK examples cmake on the website was different that that of the VTK repository itself. One worked the other didn’t. I think I one was just missing some modules in its cmake script. I’d ensure your building and linking to RenderingContext2D, ViewsContext2D, RenderingContextOpenGL2. And calling autoinit in your cmake. I believe I had found the autoinit of these was missing and caused the opengl context to be null.

I suspected a missing module as well initially… I’ll try if adding one of those fixes it, thanks for the fast answer!

Could not fix it that way yet - adding RenderingContext2D and ViewsContext2D does not help, and RenderingContextOpenGL2 doesn’t seem to exist in VTK >= 9.0.0…

Is there an easy way to figure out which library is missing if such a crash occurs? I find such errors quite hard to debug, especially if the required libraries change between versions…

Could not fix it that way yet - adding RenderingContext2D and ViewsContext2D does not help, and RenderingContextOpenGL2 doesn’t seem to exist in VTK >= 9.0.0…

It does exist in VTK 9.0.0, but due to the build system changes in VTK 9.0.0 it’s not implicitly enabled the same way as it was in some circumstances with the earlier build system. For my build, I had to manually enable this module with

-DVTK_MODULE_ENABLE_VTK_RenderingContextOpenGL2=YES

See some of the hits here for people who ran into the same gotcha: Search results for 'RenderingContextOpenGL2' - VTK

Hope that helps.

Thanks for the pointer - beating myself up now because I didn’t think of checking the available options earlier (I do have VTK_MODULE_ENABLE_VTK_RenderingContextOpenGL2 as setting shown in cmake, it wasn’t enabled yet)! Unfortunately, this also didn’t help yet, our code still crashes at vtkContext2D::GetDevice returning a nullptr… so I guess I might miss yet another module?
Here’s the list of VTK components which I currently pass into autoinit (several of course are probably only needed for unrelated purposes):
VTK::FiltersModeling;VTK::InteractionImage;VTK::InteractionWidgets;VTK::ImagingStatistics;VTK::IOGeometry;VTK::IOMovie;VTK::RenderingAnnotation;VTK::RenderingContextOpenGL2;VTK::RenderingImage;VTK::RenderingVolumeOpenGL2;VTK::RenderingQt;VTK::ViewsContext2D;VTK::ViewsInfovis;VTK::ChartsCore;VTK::CommonColor;VTK::CommonComputationalGeometry;VTK::GUISupportQt;VTK::FiltersExtraction;VTK::FiltersGeometry;VTK::FiltersHybrid;VTK::FiltersStatistics;VTK::ImagingHybrid;VTK::InfovisLayout;VTK::IOXML;VTK::RenderingContext2D;VTK::RenderingOpenVR;VTK::theora;VTK::ogg;VTK::IOOggTheora

I’m not familiar with the internals of VTK’s module system, so in the following I’m probably missing loads of intricate stuff; I just can say that from a user’s perspective, I do find this behavior of having functionality that compiles fine, but then crashes at runtime quite annoying; my gut tells me there should be a better way - VTK telling you what modules to auto-configure (or even automatically doing that), or at least telling me on startup of the specific functionality, that currently no implementation of this functionality is available (and ideally the names of the modules which can provide it). I guess if this could be done easily, it would have been done already. I hope this could be done at some point in the future, I would find it extremely helpful!

Or is there at least a list of modules and for what they are required? I’ve skimmed the VTK Module System description, but for me as a user I don’t see any actionable information in there regarding my issue - i.e. information on which modules I need to autoinit for specific functionality to work.

Here’s our current list in Imstk which allows us to do chart overlays in VTK 9.

  -DVTK_MODULE_ENABLE_VTK_ChartsCore:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_FiltersCore:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_FiltersExtraction:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_FiltersGeneral:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_FiltersModeling:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_ImagingGeneral:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_ImagingStencil:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_IOExport:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_IOGeometry:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_IOImport:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_IOLegacy:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_IOPLY:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_IOParallel:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_IOParallelXML:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_IOXML:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_InteractionStyle:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_InteractionWidgets:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_RenderingAnnotation:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_RenderingContext2D:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_RenderingContextOpenGL2:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_RenderingOpenGL2:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_RenderingOpenVR:STRING=${VTK_ENABLE_OPENVR}
  -DVTK_MODULE_ENABLE_VTK_RenderingVolumeOpenGL2:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_ViewsContext2D:STRING=YES
  -DVTK_MODULE_ENABLE_VTK_glew:STRING=YES
  -DVTK_BUILD_EXAMPLES:STRING=DONT_WANT
  -DVTK_BUILD_TESTING:STRING=OFF
  -DVTK_GROUP_ENABLE_StandAlone:STRING=DONT_WANT
  -DVTK_GROUP_ENABLE_Rendering:STRING=DONT_WANT

And when autoinit is run on the project it produces these preprocessor definitions for my system.

vtkRenderingContext2D_AUTOINIT_INCLUDE=“C:/Repos/iMSTKAW/buildWin64/Innerbuild/CMakeFiles/vtkModuleAutoInit_f0a3767b8e2952ee742886044a92ae4c.h”
vtkRenderingCore_AUTOINIT_INCLUDE=“C:/Repos/iMSTKAW/buildWin64/Innerbuild/CMakeFiles/vtkModuleAutoInit_f0a3767b8e2952ee742886044a92ae4c.h”
vtkRenderingVolume_AUTOINIT_INCLUDE=“C:/Repos/iMSTKAW/buildWin64/Innerbuild/CMakeFiles/vtkModuleAutoInit_f0a3767b8e2952ee742886044a92ae4c.h”

Additionally you will find the example within the VTK repo directory works but the example on the website does not.

Finally found the problem: I need to do the vtk_module_autoinit directly on the library using the chart overlay.

Previously, we had only done the vtk_module_autoinit on our core library (a .dll /.so) which was used from all our other libraries and which exports VTK libraries by using PUBLIC in the target_link_library call with VTK libraries…

@codeling That makes sense. Glad you found the problem.

BTW I agree that it would be good with some kind of warning/error mechanism for when you enable some piece of functionality (e.g. RenderingContext2D), but there’s no implementation module for it (e.g. RenderingContextOpenGL2) enabled, since that’s sort of a non-sensical config.

…though I guess in theory you could provide your own implementation, but I’m not sure that is so common.

On reflecting more on this more, I guess this is because the INCLUDE(${VTK_USE_FILE}) we used in VTK versions < 9 probably did that automatically on a global level.

A bit late to the party, but FindNeededModules.py usually works quite well in finding what modules your code needs. It is not guarenteed to find all needed modules but it does a pretty good job. I didn’t notice any issues with ChartsOn3DScene. Building and testing seems to work Ok.

Thanks for the hint, that sounds like something I should try!