User:Barche/Painter Cache

From K-3D

Jump to: navigation, search

This document gives an overview of how painters can use and share cached data. This method is used for the VBO and SDS painters.

Cached data

Every mesh shown in the viewport is drawn using a painter. For certain types of painters, it can be useful to keep a data-cache associated with each mesh it has to draw. The VBO painters, for example, keep a vertex buffer cached, and the subdivision surface painters keep a cache containing the subdivided mesh. This cached data should have the following properties:

  • One cache for each mesh, so if multiple painters paint the same mesh, the same cached data is used
  • Every change transmitted by the pipeline must be applied to the cache
  • When the pipeline transmits a change, the cache should only be updated once, not once for every painter using it
  • When a mesh is removed from the scene, the cached data should also be removed
  • When all painters using a cache are removed, the cached data should also be removed

These criteria are enforced by the painter_cache class, which stores cached data that derives from scheduler, both declared in modules/opengl_painters/painter_cache.h

To create cached data that can be stored in painter_cache, it has to derive from scheduler, which at the very least means the on_execute method must be implemented and the constructor must take a const k3d::mesh* const argument. The different on_schedule methods can be overridden as well. The idea of the scheduler class is that the schedule methods get called when a change is detected, and the execute method does the heavy work once the cached data is needed, and does so only once no matter how many clients exist.

From the painter side, only two calls are needed:

get_data<data_type>(const k3d::mesh* const Mesh, k3d::inode* Painter)

This call obtains the data from the cache, and makes sure the retrieved data is always up-to-date. It is typically called in the selection and drawing functions of the painter

schedule_data<data_type>(const k3d::mesh* const Mesh, k3d::iunknown* Hint, k3d::inode* Painter)

This call indicates the source data changed, and should be updated on the next request.