fix: return UINT32_MAX from findMemType on failure, add [[nodiscard]]

The findMemType/findMemoryType helper in auth_screen, loading_screen,
and vk_context returned 0 on failure — a valid memory type index.
Changed to return UINT32_MAX and log an error, so vkAllocateMemory
receives an invalid index and fails cleanly rather than silently
using the wrong memory type.

Add [[nodiscard]] to VkBuffer::uploadToGPU/createMapped and
VkContext::initialize/recreateSwapchain so callers that ignore
failure are flagged at compile time. Suppress with (void) cast at
3 call sites where failure is non-actionable (resize best-effort).
This commit is contained in:
Kelsi 2026-03-27 14:53:29 -07:00
parent 7028dd64c1
commit 5b91ef398e
6 changed files with 13 additions and 10 deletions

View file

@ -24,11 +24,11 @@ public:
VkBuffer& operator=(VkBuffer&& other) noexcept; VkBuffer& operator=(VkBuffer&& other) noexcept;
// Create a GPU-local buffer and upload data via staging // Create a GPU-local buffer and upload data via staging
bool uploadToGPU(VkContext& ctx, const void* data, VkDeviceSize size, [[nodiscard]] bool uploadToGPU(VkContext& ctx, const void* data, VkDeviceSize size,
VkBufferUsageFlags usage); VkBufferUsageFlags usage);
// Create a host-visible buffer (for uniform/dynamic data updated each frame) // Create a host-visible buffer (for uniform/dynamic data updated each frame)
bool createMapped(VmaAllocator allocator, VkDeviceSize size, [[nodiscard]] bool createMapped(VmaAllocator allocator, VkDeviceSize size,
VkBufferUsageFlags usage); VkBufferUsageFlags usage);
// Update mapped buffer contents (only valid for mapped buffers) // Update mapped buffer contents (only valid for mapped buffers)

View file

@ -32,11 +32,11 @@ public:
VkContext(const VkContext&) = delete; VkContext(const VkContext&) = delete;
VkContext& operator=(const VkContext&) = delete; VkContext& operator=(const VkContext&) = delete;
bool initialize(SDL_Window* window); [[nodiscard]] bool initialize(SDL_Window* window);
void shutdown(); void shutdown();
// Swapchain management // Swapchain management
bool recreateSwapchain(int width, int height); [[nodiscard]] bool recreateSwapchain(int width, int height);
// Frame operations // Frame operations
VkCommandBuffer beginFrame(uint32_t& imageIndex); VkCommandBuffer beginFrame(uint32_t& imageIndex);

View file

@ -78,7 +78,8 @@ static uint32_t findMemoryType(VkPhysicalDevice physDevice, uint32_t typeFilter,
return i; return i;
} }
} }
return 0; LOG_ERROR("LoadingScreen: no suitable memory type found");
return UINT32_MAX;
} }
bool LoadingScreen::loadImage(const std::string& path) { bool LoadingScreen::loadImage(const std::string& path) {
@ -420,7 +421,7 @@ void LoadingScreen::render() {
int w = 0, h = 0; int w = 0, h = 0;
SDL_GetWindowSize(sdlWindow, &w, &h); SDL_GetWindowSize(sdlWindow, &w, &h);
if (w > 0 && h > 0) { if (w > 0 && h > 0) {
vkCtx->recreateSwapchain(w, h); (void)vkCtx->recreateSwapchain(w, h);
} }
} }

View file

@ -941,7 +941,7 @@ void Renderer::applyMsaaChange() {
if (!vkCtx->recreateSwapchain(window->getWidth(), window->getHeight())) { if (!vkCtx->recreateSwapchain(window->getWidth(), window->getHeight())) {
LOG_ERROR("MSAA change failed — reverting to 1x"); LOG_ERROR("MSAA change failed — reverting to 1x");
vkCtx->setMsaaSamples(VK_SAMPLE_COUNT_1_BIT); vkCtx->setMsaaSamples(VK_SAMPLE_COUNT_1_BIT);
vkCtx->recreateSwapchain(window->getWidth(), window->getHeight()); (void)vkCtx->recreateSwapchain(window->getWidth(), window->getHeight());
} }
// Recreate all sub-renderer pipelines (they embed sample count from render pass) // Recreate all sub-renderer pipelines (they embed sample count from render pass)
@ -1051,7 +1051,7 @@ void Renderer::beginFrame() {
// Handle swapchain recreation if needed // Handle swapchain recreation if needed
if (vkCtx->isSwapchainDirty()) { if (vkCtx->isSwapchainDirty()) {
vkCtx->recreateSwapchain(window->getWidth(), window->getHeight()); (void)vkCtx->recreateSwapchain(window->getWidth(), window->getHeight());
// Rebuild water resources that reference swapchain extent/views // Rebuild water resources that reference swapchain extent/views
if (waterRenderer) { if (waterRenderer) {
waterRenderer->recreatePipelines(); waterRenderer->recreatePipelines();

View file

@ -1176,7 +1176,8 @@ static uint32_t findMemType(VkPhysicalDevice physDev, uint32_t typeFilter, VkMem
if ((typeFilter & (1 << i)) && (memProps.memoryTypes[i].propertyFlags & props) == props) if ((typeFilter & (1 << i)) && (memProps.memoryTypes[i].propertyFlags & props) == props)
return i; return i;
} }
return 0; LOG_ERROR("VkContext: no suitable memory type found");
return UINT32_MAX;
} }
VkDescriptorSet VkContext::uploadImGuiTexture(const uint8_t* rgba, int width, int height) { VkDescriptorSet VkContext::uploadImGuiTexture(const uint8_t* rgba, int width, int height) {

View file

@ -786,7 +786,8 @@ static uint32_t findMemType(VkPhysicalDevice pd, uint32_t filter, VkMemoryProper
for (uint32_t i = 0; i < mp.memoryTypeCount; i++) { for (uint32_t i = 0; i < mp.memoryTypeCount; i++) {
if ((filter & (1 << i)) && (mp.memoryTypes[i].propertyFlags & props) == props) return i; if ((filter & (1 << i)) && (mp.memoryTypes[i].propertyFlags & props) == props) return i;
} }
return 0; LOG_ERROR("AuthScreen: no suitable memory type found");
return UINT32_MAX;
} }
bool AuthScreen::loadBackgroundImage() { bool AuthScreen::loadBackgroundImage() {