Skip to content

Commit

Permalink
Check debug utils availability
Browse files Browse the repository at this point in the history
  • Loading branch information
rokuz committed Jan 29, 2025
1 parent 7399de7 commit 2f1b14b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
75 changes: 56 additions & 19 deletions lvk/vulkan/VulkanClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,12 +971,14 @@ void lvk::VulkanImage::generateMipmap(VkCommandBuffer commandBuffer) const {

const VkImageAspectFlags imageAspectFlags = getImageAspectFlags();

const VkDebugUtilsLabelEXT utilsLabel = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT,
.pLabelName = "Generate mipmaps",
.color = {1.0f, 0.75f, 1.0f, 1.0f},
};
vkCmdBeginDebugUtilsLabelEXT(commandBuffer, &utilsLabel);
if (vkCmdBeginDebugUtilsLabelEXT) {
const VkDebugUtilsLabelEXT utilsLabel = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT,
.pLabelName = "Generate mipmaps",
.color = {1.0f, 0.75f, 1.0f, 1.0f},
};
vkCmdBeginDebugUtilsLabelEXT(commandBuffer, &utilsLabel);
}

const VkImageLayout originalImageLayout = vkImageLayout_;

Expand Down Expand Up @@ -1064,7 +1066,9 @@ void lvk::VulkanImage::generateMipmap(VkCommandBuffer commandBuffer) const {
VK_PIPELINE_STAGE_TRANSFER_BIT, // srcStageMask
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, // dstStageMask
VkImageSubresourceRange{imageAspectFlags, 0, numLevels_, 0, numLayers_});
vkCmdEndDebugUtilsLabelEXT(commandBuffer);
if (vkCmdEndDebugUtilsLabelEXT) {
vkCmdEndDebugUtilsLabelEXT(commandBuffer);
}

vkImageLayout_ = originalImageLayout;
}
Expand Down Expand Up @@ -1980,7 +1984,7 @@ void lvk::CommandBuffer::cmdDispatchThreadGroups(const Dimensions& threadgroupCo
void lvk::CommandBuffer::cmdPushDebugGroupLabel(const char* label, uint32_t colorRGBA) const {
LVK_ASSERT(label);

if (!label) {
if (!label || !vkCmdBeginDebugUtilsLabelEXT) {
return;
}
const VkDebugUtilsLabelEXT utilsLabel = {
Expand All @@ -1998,7 +2002,7 @@ void lvk::CommandBuffer::cmdPushDebugGroupLabel(const char* label, uint32_t colo
void lvk::CommandBuffer::cmdInsertDebugEventLabel(const char* label, uint32_t colorRGBA) const {
LVK_ASSERT(label);

if (!label) {
if (!label || !vkCmdInsertDebugUtilsLabelEXT) {
return;
}
const VkDebugUtilsLabelEXT utilsLabel = {
Expand All @@ -2014,6 +2018,9 @@ void lvk::CommandBuffer::cmdInsertDebugEventLabel(const char* label, uint32_t co
}

void lvk::CommandBuffer::cmdPopDebugGroupLabel() const {
if (!vkCmdEndDebugUtilsLabelEXT) {
return;
}
vkCmdEndDebugUtilsLabelEXT(wrapper_->cmdBuf_);
}

Expand Down Expand Up @@ -3549,7 +3556,9 @@ lvk::VulkanContext::~VulkanContext() {
// Device has to be destroyed prior to Instance
vkDestroyDevice(vkDevice_, nullptr);

vkDestroyDebugUtilsMessengerEXT(vkInstance_, vkDebugUtilsMessenger_, nullptr);
if (vkDebugUtilsMessenger_ != VK_NULL_HANDLE) {
vkDestroyDebugUtilsMessengerEXT(vkInstance_, vkDebugUtilsMessenger_, nullptr);
}
vkDestroyInstance(vkInstance_, nullptr);

glslang_finalize_process();
Expand Down Expand Up @@ -5786,9 +5795,40 @@ void lvk::VulkanContext::createInstance() {
}();
}

uint32_t count = 0;
VK_ASSERT(vkEnumerateInstanceExtensionProperties(nullptr, &count, nullptr));

std::vector<VkExtensionProperties> allInstanceExtensions(count);
VK_ASSERT(vkEnumerateInstanceExtensionProperties(nullptr, &count, allInstanceExtensions.data()));

// check if we have debug utils extension
bool debugUtilsAvailable = true;
{
[this, &debugUtilsAvailable, &allInstanceExtensions]() -> void {
for (const VkExtensionProperties& props : allInstanceExtensions) {
if (!strcmp(props.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME))
return;
}
if (config_.enableValidation) {
for (const char* layer : kDefaultValidationLayers) {
uint32_t extCount = 0;
VK_ASSERT(vkEnumerateInstanceExtensionProperties(layer, &extCount, nullptr));

std::vector<VkExtensionProperties> exts(extCount);
VK_ASSERT(vkEnumerateInstanceExtensionProperties(layer, &extCount, exts.data()));

for (const VkExtensionProperties& props : exts) {
if (!strcmp(props.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME))
return;
}
}
}
debugUtilsAvailable = false; // no validation debug utils extension
}();
}

std::vector<const char*> instanceExtensionNames = {
VK_KHR_SURFACE_EXTENSION_NAME,
VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
#if defined(_WIN32)
VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Expand All @@ -5808,6 +5848,10 @@ void lvk::VulkanContext::createInstance() {
#endif
};

if (debugUtilsAvailable) {
instanceExtensionNames.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
}

if (config_.enableValidation) {
instanceExtensionNames.push_back(VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME); // enabled only for validation
}
Expand Down Expand Up @@ -5912,7 +5956,7 @@ void lvk::VulkanContext::createInstance() {
volkLoadInstance(vkInstance_);

// debug messenger
{
if (debugUtilsAvailable) {
const VkDebugUtilsMessengerCreateInfoEXT ci = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
Expand All @@ -5925,13 +5969,6 @@ void lvk::VulkanContext::createInstance() {
VK_ASSERT(vkCreateDebugUtilsMessengerEXT(vkInstance_, &ci, nullptr, &vkDebugUtilsMessenger_));
}

uint32_t count = 0;
VK_ASSERT(vkEnumerateInstanceExtensionProperties(nullptr, &count, nullptr));

std::vector<VkExtensionProperties> allInstanceExtensions(count);

VK_ASSERT(vkEnumerateInstanceExtensionProperties(nullptr, &count, allInstanceExtensions.data()));

// log available instance extensions
LLOGL("\nVulkan instance extensions:\n");

Expand Down
2 changes: 1 addition & 1 deletion lvk/vulkan/VulkanUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ lvk::Result lvk::compileShader(VkShaderStageFlagBits stage,
}

VkResult lvk::setDebugObjectName(VkDevice device, VkObjectType type, uint64_t handle, const char* name) {
if (!name || !*name) {
if (!name || !*name || !vkSetDebugUtilsObjectNameEXT) {
return VK_SUCCESS;
}
const VkDebugUtilsObjectNameInfoEXT ni = {
Expand Down

0 comments on commit 2f1b14b

Please sign in to comment.