perf(pipeline): shared, disk-persisted VkPipelineCache (#69)

vkCreateComputePipelines and vkCreateRayTracingPipelinesKHR were passed
VK_NULL_HANDLE, so the driver couldn't reuse compiled shader binaries
across the pipelines built at startup (4 UI shaders + user/RT pipelines)
or across runs.

Add one process-wide Device::pipelineCache. LoadPipelineCache (called
from Initialize) seeds it from pipeline_cache.bin when the file's
VkPipelineCacheHeaderVersionOne header matches this device's
vendorID / deviceID / pipelineCacheUUID; a stale or foreign blob is
discarded so the driver never rejects it. SavePipelineCache is registered
with std::atexit, serialising the cache at process shutdown without any
explicit teardown hook. Both create sites now pass the cache; a null
handle stays valid, so no create-site null check is needed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
catbot 2026-06-16 18:23:25 +00:00
commit a3a6dd2006
4 changed files with 144 additions and 4 deletions

View file

@ -161,6 +161,27 @@ export namespace Crafter {
inline static VkPhysicalDeviceMemoryProperties memoryProperties;
// Core physical-device properties, captured once at Initialize from the
// VkPhysicalDeviceProperties2 query. Kept because the pipeline-cache
// persistence path needs vendorID / deviceID / pipelineCacheUUID to
// decide whether an on-disk blob was written by this exact device.
inline static VkPhysicalDeviceProperties deviceProperties = {};
// One process-wide pipeline cache fed to every vkCreate*Pipelines call
// (compute UI/user shaders + RT pipelines). Pipeline compilation is a
// one-time cold-start cost, not a per-frame one; a single shared cache
// lets the driver reuse compiled shader binaries across the several
// pipelines built at startup and — once persisted to disk (see
// LoadPipelineCache / SavePipelineCache) — across runs. VK_NULL_HANDLE
// until LoadPipelineCache runs; passing VK_NULL_HANDLE to a create call
// is valid and simply means "no cache", so the create sites need no
// null check.
inline static VkPipelineCache pipelineCache = VK_NULL_HANDLE;
// Where the serialized cache lives. Relative to the working directory,
// matching the gpu_crash_dump-* convention. Set before Initialize to
// relocate it.
inline static std::filesystem::path pipelineCachePath = "pipeline_cache.bin";
inline static VkPhysicalDeviceDescriptorHeapPropertiesEXT descriptorHeapProperties = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_HEAP_PROPERTIES_EXT
};
@ -199,6 +220,23 @@ export namespace Crafter {
// ComputeShader read the offset off the pipeline they record.
static void CheckVkResult(VkResult result);
// ─── Pipeline cache persistence (issue #69) ─────────────────────
// LoadPipelineCache creates `pipelineCache`, seeding it from
// pipelineCachePath when the file exists *and* its header matches this
// device (PipelineCacheDataCompatible) — a stale or foreign blob is
// discarded so the driver never rejects it. Called once from
// Initialize. SavePipelineCache writes the driver's current cache blob
// back out; Initialize registers it with std::atexit so the cache is
// serialized at process shutdown without any explicit teardown call.
static void LoadPipelineCache();
static void SavePipelineCache();
// True when `data` is a VkPipelineCache blob whose header was written by
// a device with the same vendorID / deviceID / pipelineCacheUUID as
// `deviceProperties`. Pure logic over the standard 32-byte cache header,
// so it is driven directly by the PipelineCacheValidation test with no
// GPU device. A blob too short to hold the header is incompatible.
static bool PipelineCacheDataCompatible(std::span<const std::byte> data);
// Selects a memory type index from typeBits that satisfies `required`.
// When `preferred` bits are also given, a type satisfying both is
// chosen first; if none exists we fall back to required-only rather