vpr::PipelineCache class

A PipelineCache is a wrapper around a VkPipelineCache that takes care of several important details that are otherwise difficult to handle: saving and loading pipeline cache data from a file, verifying integrity of pipeline cache files, and cleaning up / repairing old, unused, and outdated pipeline cache files.

This object can be used to increase the speed of creating graphics pipeline objects, and is especially useful for pipelines that are:

  • very complex and slow to create/recreate after a swapchain recreation
  • frequently created as part of other objects
  • for use with dynamic shader editing and recompiliation, which requires a pipeline recreation to propagate changes

The pipeline cache is especially helpful when used in MoltenVk - if one is using the runtime Metal shader compiler, then the cache is used to store the converted Metal shader code. By saving and reloading this cache data, then, one can avoid the significant cost of cross-compiling their shaders everytime they restart the program.

Constructors, destructors, conversion operators

PipelineCache(const VkDevice& parent, const VkPhysicalDevice& host_phys_device, const size_t hash_id)
Creates a pipeline cache, or loads a pre-existing one from file.

Public functions

auto Verify(const int8_t* cache_header) const -> bool
Takes a pipeline cache header and checks it for validity.
void LoadCacheFromFile(const char* filename)
Loads and overwrites potential contents with data loaded from the given file.
void MergeCaches(const uint32_t num_caches, const VkPipelineCache* caches)
Merges given cache objects into this object.

Function documentation

vpr::PipelineCache::PipelineCache(const VkDevice& parent, const VkPhysicalDevice& host_phys_device, const size_t hash_id)

Creates a pipeline cache, or loads a pre-existing one from file.

Parameters
parent
host_phys_device This is required, as pipeline caches are related to the driver of the physical device they belong to. Thus, we need it for validation purposes.
hash_id This can be nicely setting by using typeid(owning_type).hash_code(), so that types all share a pipline cache and there is still a unique identifier per type.

void vpr::PipelineCache::MergeCaches(const uint32_t num_caches, const VkPipelineCache* caches)

Merges given cache objects into this object.

Useful to generate pipelines across several threads, but merge their cache data back into a single object for later re-use.

Another potential use can be coalescing all created caches back into a single one at the end of a program, to simplify the re-loading process later.