fix: add VkSampler cache to prevent sampler exhaustion crash

Validation layers revealed 9965 VkSamplers allocated against a device
limit of 4000 — every VkTexture created its own sampler even when
configurations were identical. This exhausted NVIDIA's sampler pool
and caused intermittent SIGSEGV in vkCmdBeginRenderPass.

Add a thread-safe sampler cache in VkContext that deduplicates samplers
by FNV-1a hash of all 14 VkSamplerCreateInfo fields. All texture,
render target, renderer, water, and loading screen sampler creation
now goes through getOrCreateSampler(). Textures set ownsSampler_=false
so shared samplers aren't double-freed.

Also auto-disable anisotropy in the cache when the physical device
doesn't support the samplerAnisotropy feature, fixing the validation
error VUID-VkSamplerCreateInfo-anisotropyEnable-01070.
This commit is contained in:
Kelsi 2026-03-24 11:44:54 -07:00
parent 1556559211
commit a152023e5e
10 changed files with 194 additions and 40 deletions

View file

@ -40,10 +40,7 @@ void LoadingScreen::shutdown() {
// ImGui manages descriptor set lifetime
bgDescriptorSet = VK_NULL_HANDLE;
}
if (bgSampler) {
vkDestroySampler(device, bgSampler, nullptr);
bgSampler = VK_NULL_HANDLE;
}
bgSampler = VK_NULL_HANDLE; // Owned by VkContext sampler cache
if (bgImageView) {
vkDestroyImageView(device, bgImageView, nullptr);
bgImageView = VK_NULL_HANDLE;
@ -94,7 +91,7 @@ bool LoadingScreen::loadImage(const std::string& path) {
if (bgImage) {
VkDevice device = vkCtx->getDevice();
vkDeviceWaitIdle(device);
if (bgSampler) { vkDestroySampler(device, bgSampler, nullptr); bgSampler = VK_NULL_HANDLE; }
bgSampler = VK_NULL_HANDLE; // Owned by VkContext sampler cache
if (bgImageView) { vkDestroyImageView(device, bgImageView, nullptr); bgImageView = VK_NULL_HANDLE; }
if (bgImage) { vkDestroyImage(device, bgImage, nullptr); bgImage = VK_NULL_HANDLE; }
if (bgMemory) { vkFreeMemory(device, bgMemory, nullptr); bgMemory = VK_NULL_HANDLE; }
@ -230,7 +227,7 @@ bool LoadingScreen::loadImage(const std::string& path) {
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
vkCreateSampler(device, &samplerInfo, nullptr, &bgSampler);
bgSampler = vkCtx->getOrCreateSampler(samplerInfo);
}
// Register with ImGui as a texture