Skip to content

Commit

Permalink
vk: print error code on failure for vk calls (#8348)
Browse files Browse the repository at this point in the history
Fixes #8186
  • Loading branch information
poweifeng authored Jan 13, 2025
1 parent 37fd83a commit da99a03
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 54 deletions.
5 changes: 3 additions & 2 deletions filament/backend/src/vulkan/VulkanContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ VulkanTimestamps::VulkanTimestamps(VkDevice device) : mDevice(device) {
std::unique_lock<utils::Mutex> lock(mMutex);
tqpCreateInfo.queryCount = mUsed.size() * 2;
VkResult result = vkCreateQueryPool(mDevice, &tqpCreateInfo, VKALLOC, &mPool);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "vkCreateQueryPool error.";
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "vkCreateQueryPool failed."
<< " error=" << static_cast<int32_t>(result);
mUsed.reset();
}

Expand Down Expand Up @@ -136,7 +137,7 @@ VulkanTimestamps::QueryResult VulkanTimestamps::getResult(
vkGetQueryPoolResults(mDevice, mPool, index, 2, dataSize, (void*) result.data(), stride,
VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WITH_AVAILABILITY_BIT);
FILAMENT_CHECK_POSTCONDITION(vkresult == VK_SUCCESS || vkresult == VK_NOT_READY)
<< "vkGetQueryPoolResults error: " << static_cast<int32_t>(vkresult);
<< "vkGetQueryPoolResults error=" << static_cast<int32_t>(vkresult);
if (vkresult == VK_NOT_READY) {
return {0, 0, 0, 0};
}
Expand Down
6 changes: 4 additions & 2 deletions filament/backend/src/vulkan/VulkanDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ DebugUtils::DebugUtils(VkInstance instance, VkDevice device, VulkanContext const
VkResult result = vkCreateDebugUtilsMessengerEXT(instance, &createInfo,
VKALLOC, &mDebugMessenger);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "Unable to create Vulkan debug messenger.";
<< "Unable to create Vulkan debug messenger. error="
<< static_cast<int32_t>(result);
}
#endif // FVK_ENABLED(FVK_DEBUG_VALIDATION)
}
Expand Down Expand Up @@ -231,7 +232,8 @@ VulkanDriver::VulkanDriver(VulkanPlatform* platform, VulkanContext const& contex
VkResult result = createDebugReportCallback(mPlatform->getInstance(), &cbinfo, VKALLOC,
&mDebugCallback);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "Unable to create Vulkan debug callback.";
<< "Unable to create Vulkan debug callback."
<< " error=" << static_cast<int32_t>(result);
}
#endif

Expand Down
8 changes: 5 additions & 3 deletions filament/backend/src/vulkan/VulkanFboCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ VkFramebuffer VulkanFboCache::getFramebuffer(FboKey const& config) noexcept {
mRenderPassRefCount[info.renderPass]++;
VkFramebuffer framebuffer;
VkResult error = vkCreateFramebuffer(mDevice, &info, VKALLOC, &framebuffer);
FILAMENT_CHECK_POSTCONDITION(!error) << "Unable to create framebuffer.";
FILAMENT_CHECK_POSTCONDITION(error == VK_SUCCESS) << "Unable to create framebuffer."
<< " error=" << static_cast<int32_t>(error);
mFramebufferCache[config] = {framebuffer, mCurrentTime};
return framebuffer;
}
Expand Down Expand Up @@ -324,10 +325,11 @@ VkRenderPass VulkanFboCache::getRenderPass(RenderPassKey const& config) noexcept
// Finally, create the VkRenderPass.
VkRenderPass renderPass;
VkResult error = vkCreateRenderPass(mDevice, &renderPassInfo, VKALLOC, &renderPass);
FILAMENT_CHECK_POSTCONDITION(!error) << "Unable to create render pass.";
FILAMENT_CHECK_POSTCONDITION(error == VK_SUCCESS) << "Unable to create render pass."
<< " error=" << error;
mRenderPassCache[config] = {renderPass, mCurrentTime};

#if FVK_ENABLED(FVK_DEBUG_FBO_CACHE)
#if FVK_ENABLED(FVK_DEBUG_FBO_CACHE)
FVK_LOGD << "Created render pass " << renderPass << " with "
<< "samples = " << int(config.samples) << ", "
<< "depth = " << (hasDepth ? 1 : 0) << ", "
Expand Down
4 changes: 3 additions & 1 deletion filament/backend/src/vulkan/VulkanHandles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ VulkanProgram::VulkanProgram(VkDevice device, Program const& builder) noexcept
.pCode = data,
};
VkResult result = vkCreateShaderModule(mDevice, &moduleInfo, VKALLOC, &module);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "Unable to create shader module.";
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "Unable to create shader module."
<< " error=" << static_cast<int32_t>(result);

#if FVK_ENABLED(FVK_DEBUG_DEBUG_UTILS)
std::string name{ builder.getName().c_str(), builder.getName().size() };
Expand Down
5 changes: 3 additions & 2 deletions filament/backend/src/vulkan/VulkanSamplerCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ VkSampler VulkanSamplerCache::getSampler(SamplerParams params) noexcept {
.unnormalizedCoordinates = VK_FALSE
};
VkSampler sampler;
VkResult error = vkCreateSampler(mDevice, &samplerInfo, VKALLOC, &sampler);
FILAMENT_CHECK_POSTCONDITION(!error) << "Unable to create sampler.";
VkResult result = vkCreateSampler(mDevice, &samplerInfo, VKALLOC, &sampler);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "Unable to create sampler."
<< " error=" << static_cast<int32_t>(result);
mCache.insert({params, sampler});
return sampler;
}
Expand Down
4 changes: 2 additions & 2 deletions filament/backend/src/vulkan/VulkanSwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void VulkanSwapChain::present() {
VkResult const result = mPlatform->present(swapChain, mCurrentSwapIndex, finishedDrawing);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR ||
result == VK_ERROR_OUT_OF_DATE_KHR)
<< "Cannot present in swapchain.";
<< "Cannot present in swapchain. error=" << static_cast<int32_t>(result);
}

// We presented the last acquired buffer.
Expand Down Expand Up @@ -144,7 +144,7 @@ void VulkanSwapChain::acquire(bool& resized) {
mCurrentSwapIndex = imageSyncData.imageIndex;
mExplicitImageReadyWait = imageSyncData.explicitImageReadyWait;
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR)
<< "Cannot acquire in swapchain.";
<< "Cannot acquire in swapchain. error=" << static_cast<int32_t>(result);
if (imageSyncData.imageReadySemaphore != VK_NULL_HANDLE) {
mCommands->injectDependency(imageSyncData.imageReadySemaphore);
}
Expand Down
19 changes: 11 additions & 8 deletions filament/backend/src/vulkan/VulkanTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ VulkanTexture::VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice,
this->samples = samples;
imageInfo.samples = (VkSampleCountFlagBits) samples;

VkResult error = vkCreateImage(mState->mDevice, &imageInfo, VKALLOC, &mState->mTextureImage);
if (error || FVK_ENABLED(FVK_DEBUG_TEXTURE)) {
VkResult result = vkCreateImage(mState->mDevice, &imageInfo, VKALLOC, &mState->mTextureImage);
if (result == VK_SUCCESS || FVK_ENABLED(FVK_DEBUG_TEXTURE)) {
FVK_LOGD << "vkCreateImage: "
<< "image = " << mState->mTextureImage << ", "
<< "result = " << error << ", "
<< "result = " << result << ", "
<< "handle = " << utils::io::hex << mState->mTextureImage << utils::io::dec << ", "
<< "extent = " << w << "x" << h << "x"<< depth << ", "
<< "mipLevels = " << int(levels) << ", "
Expand All @@ -311,7 +311,8 @@ VulkanTexture::VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice,
<< "target = " << static_cast<int>(target) <<", "
<< "format = " << mState->mVkFormat << utils::io::endl;
}
FILAMENT_CHECK_POSTCONDITION(!error) << "Unable to create image.";
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "Unable to create image."
<< " error=" << static_cast<int32_t>(result);

// Allocate memory for the VkImage and bind it.
VkMemoryRequirements memReqs = {};
Expand All @@ -332,11 +333,13 @@ VulkanTexture::VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice,
.allocationSize = memReqs.size,
.memoryTypeIndex = memoryTypeIndex,
};
error = vkAllocateMemory(mState->mDevice, &allocInfo, nullptr, &mState->mTextureImageMemory);
FILAMENT_CHECK_POSTCONDITION(!error) << "Unable to allocate image memory.";
error = vkBindImageMemory(mState->mDevice, mState->mTextureImage, mState->mTextureImageMemory,
result = vkAllocateMemory(mState->mDevice, &allocInfo, nullptr, &mState->mTextureImageMemory);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "Unable to allocate image memory."
<< " error=" << static_cast<int32_t>(result);
result = vkBindImageMemory(mState->mDevice, mState->mTextureImage, mState->mTextureImageMemory,
0);
FILAMENT_CHECK_POSTCONDITION(!error) << "Unable to bind image.";
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "Unable to bind image."
<< " error=" << static_cast<int32_t>(result);

// Spec out the "primary" VkImageView that shaders use to sample from the image.
mPrimaryViewRange = mState->mFullViewRange;
Expand Down
5 changes: 3 additions & 2 deletions filament/backend/src/vulkan/platform/VulkanPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ VkInstance createInstance(ExtensionSet const& requiredExts) {

VkResult result = vkCreateInstance(&instanceCreateInfo, VKALLOC, &instance);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "Unable to create Vulkan instance. Result=" << result;
<< "Unable to create Vulkan instance. error=" << static_cast<int32_t>(result);
return instance;
}

Expand Down Expand Up @@ -390,7 +390,8 @@ VkDevice createLogicalDevice(VkPhysicalDevice physicalDevice,
deviceCreateInfo.pNext = pNext;

VkResult result = vkCreateDevice(physicalDevice, &deviceCreateInfo, VKALLOC, &device);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "vkCreateDevice error=" << result << ".";
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkCreateDevice error=" << static_cast<int32_t>(result);

return device;
}
Expand Down
14 changes: 8 additions & 6 deletions filament/backend/src/vulkan/platform/VulkanPlatformAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ VulkanPlatform::ImageData allocateExternalImage(void* externalBuffer,

VkResult result = vkCreateImage(device, &imageInfo, allocator, &data.first);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkCreateImage failed with error=" << result;
<< "vkCreateImage failed with error=" << static_cast<int32_t>(result);

// Allocate the memory
VkImportAndroidHardwareBufferInfoANDROID androidHardwareBufferInfo = {
Expand All @@ -162,7 +162,7 @@ VulkanPlatform::ImageData allocateExternalImage(void* externalBuffer,
.memoryTypeIndex = metadata.memoryTypeBits};
result = vkAllocateMemory(device, &allocInfo, allocator, &data.second);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkAllocateMemory failed with error=" << result;
<< "vkAllocateMemory failed with error=" << static_cast<int32_t>(result);

return data;
}
Expand Down Expand Up @@ -197,9 +197,11 @@ VulkanPlatform::ExternalImageMetadata VulkanPlatform::getExternalImageMetadataIm
};
VkResult result = vkGetAndroidHardwareBufferPropertiesANDROID(device, buffer, &properties);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkGetAndroidHardwareBufferProperties failed with error=" << result;
<< "vkGetAndroidHardwareBufferProperties failed with error="
<< static_cast<int32_t>(result);

FILAMENT_CHECK_POSTCONDITION(metadata.format == formatInfo.format);
FILAMENT_CHECK_POSTCONDITION(metadata.format == formatInfo.format)
<< "mismatched image format for external image (AHB)";
metadata.externalFormat = formatInfo.externalFormat;
metadata.allocationSize = properties.allocationSize;
metadata.memoryTypeBits = properties.memoryTypeBits;
Expand All @@ -212,7 +214,7 @@ VulkanPlatform::ImageData VulkanPlatform::createExternalImageImpl(void* external
ImageData data = allocateExternalImage(externalImage, device, allocator, metadata);
VkResult result = vkBindImageMemory(device, data.first, data.second, 0);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkBindImageMemory error=" << result << ".";
<< "vkBindImageMemory error=" << static_cast<int32_t>(result);
return data;
}

Expand All @@ -234,7 +236,7 @@ VulkanPlatform::SurfaceBundle VulkanPlatform::createVkSurfaceKHR(void* nativeWin
VkResult const result =
vkCreateAndroidSurfaceKHR(instance, &createInfo, VKALLOC, (VkSurfaceKHR*) &surface);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkCreateAndroidSurfaceKHR with error=" << result;
<< "vkCreateAndroidSurfaceKHR with error=" << static_cast<int32_t>(result);
return {surface, extent};
}
}
34 changes: 18 additions & 16 deletions filament/backend/src/vulkan/platform/VulkanPlatformApple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,24 @@
createInfo.pView = (__bridge void*) nsview;
VkResult result = vkCreateMacOSSurfaceMVK((VkInstance) instance, &createInfo, VKALLOC,
(VkSurfaceKHR*) &surface);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "vkCreateMacOSSurfaceMVK error.";
#elif defined(FILAMENT_IOS) && defined(METAL_AVAILABLE)
CAMetalLayer* metalLayer = (CAMetalLayer*) nativeWindow;
// Create the VkSurface.
FILAMENT_CHECK_POSTCONDITION(vkCreateIOSSurfaceMVK)
<< "Unable to load vkCreateIOSSurfaceMVK function.";
VkIOSSurfaceCreateInfoMVK createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
createInfo.pNext = NULL;
createInfo.flags = 0;
createInfo.pView = metalLayer;
VkResult result = vkCreateIOSSurfaceMVK((VkInstance) instance, &createInfo, VKALLOC,
(VkSurfaceKHR*) &surface);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "vkCreateIOSSurfaceMVK error.";
#endif
return std::make_tuple(surface, VkExtent2D {});
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkCreateMacOSSurfaceMVK. error=" << static_cast<int32_t>(result);
#elif defined(FILAMENT_IOS) && defined(METAL_AVAILABLE)
CAMetalLayer* metalLayer = (CAMetalLayer*) nativeWindow;
// Create the VkSurface.
FILAMENT_CHECK_POSTCONDITION(vkCreateIOSSurfaceMVK)
<< "Unable to load vkCreateIOSSurfaceMVK function.";
VkIOSSurfaceCreateInfoMVK createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
createInfo.pNext = NULL;
createInfo.flags = 0;
createInfo.pView = metalLayer;
VkResult result = vkCreateIOSSurfaceMVK((VkInstance) instance, &createInfo, VKALLOC,
(VkSurfaceKHR*) &surface);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkCreateIOSSurfaceMVK failed. error=" << static_cast<int32_t>(result);
#endif
return std::make_tuple(surface, VkExtent2D{});
}

} // namespace filament::backend
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ VulkanPlatform::SurfaceBundle VulkanPlatform::createVkSurfaceKHR(void* nativeWin
VkResult const result = vkCreateXcbSurfaceKHR(instance, &createInfo, VKALLOC,
(VkSurfaceKHR*) &surface);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkCreateXcbSurfaceKHR error.";
<< "vkCreateXcbSurfaceKHR error=" << static_cast<int32_t>(result);
}
#endif
#if defined(FILAMENT_SUPPORTS_XLIB)
Expand All @@ -190,7 +190,7 @@ VulkanPlatform::SurfaceBundle VulkanPlatform::createVkSurfaceKHR(void* nativeWin
VkResult const result = vkCreateXlibSurfaceKHR(instance, &createInfo, VKALLOC,
(VkSurfaceKHR*) &surface);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkCreateXlibSurfaceKHR error.";
<< "vkCreateXlibSurfaceKHR error=" << static_cast<int32_t>(result);
}
#endif
#elif defined(WIN32)
Expand All @@ -210,9 +210,11 @@ VulkanPlatform::SurfaceBundle VulkanPlatform::createVkSurfaceKHR(void* nativeWin
};
VkResult const result = vkCreateWin32SurfaceKHR(instance, &createInfo, nullptr,
(VkSurfaceKHR*) &surface);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "vkCreateWin32SurfaceKHR error.";
#endif
return std::make_tuple(surface, extent);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkCreateWin32SurfaceKHR failed."
<< " error=" << static_cast<int32_t>(result);
#endif
return std::make_tuple(surface, extent);
}

} // namespace filament::backend
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ std::tuple<VkImage, VkDeviceMemory> createImageAndMemory(VulkanContext const& co
.memoryTypeIndex = memoryTypeIndex,
};
result = vkAllocateMemory(device, &allocInfo, nullptr, &imageMemory);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "Unable to allocate image memory.";
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "Unable to allocate image memory."
<< " error=" << static_cast<int32_t>(result);
result = vkBindImageMemory(device, image, imageMemory, 0);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "Unable to bind image.";
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "Unable to bind image."
<< " error=" << static_cast<int32_t>(result);
return std::tuple(image, imageMemory);
}

Expand Down Expand Up @@ -247,8 +249,8 @@ VkResult VulkanPlatformSurfaceSwapChain::create() {
.oldSwapchain = mSwapchain,
};
VkResult result = vkCreateSwapchainKHR(mDevice, &createInfo, VKALLOC, &mSwapchain);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "vkCreateSwapchainKHR error: " << static_cast<int32_t>(result);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "vkCreateSwapchainKHR failed."
<< " error=" << static_cast<int32_t>(result);

mSwapChainBundle.colors = enumerate(vkGetSwapchainImagesKHR, mDevice, mSwapchain);
mSwapChainBundle.colorFormat = surfaceFormat.format;
Expand All @@ -274,7 +276,9 @@ VkResult VulkanPlatformSurfaceSwapChain::create() {
for (uint32_t i = 0; i < IMAGE_READY_SEMAPHORE_COUNT; ++i) {
VkResult result = vkCreateSemaphore(mDevice, &semaphoreCreateInfo, nullptr,
mImageReady + i);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "Failed to create semaphore";
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS)
<< "Failed to create semaphore."
<< " error=" << static_cast<int32_t>(result);
}

return result;
Expand Down

0 comments on commit da99a03

Please sign in to comment.