diff --git a/src/gfxstream/guest/vulkan/gfxstream_vk_device.cpp b/src/gfxstream/guest/vulkan/gfxstream_vk_device.cpp index 30da826bbcc..135022b9e75 100644 --- a/src/gfxstream/guest/vulkan/gfxstream_vk_device.cpp +++ b/src/gfxstream/guest/vulkan/gfxstream_vk_device.cpp @@ -331,25 +331,21 @@ VkResult gfxstream_vk_CreateInstance(const VkInstanceCreateInfo* pCreateInfo, vk_free(pAllocator, instance); return vk_error(NULL, result); } - uint32_t initialEnabledExtensionCount = pCreateInfo->enabledExtensionCount; - const char* const* initialPpEnabledExtensionNames = pCreateInfo->ppEnabledExtensionNames; + + // Full local copy of pCreateInfo + VkInstanceCreateInfo localCreateInfo = *pCreateInfo; std::vector filteredExts = filteredInstanceExtensionNames( - pCreateInfo->enabledExtensionCount, pCreateInfo->ppEnabledExtensionNames); - // Temporarily modify createInfo for the encoder call - VkInstanceCreateInfo* mutableCreateInfo = (VkInstanceCreateInfo*)pCreateInfo; - mutableCreateInfo->enabledExtensionCount = static_cast(filteredExts.size()); - mutableCreateInfo->ppEnabledExtensionNames = filteredExts.data(); + localCreateInfo.enabledExtensionCount, localCreateInfo.ppEnabledExtensionNames); + localCreateInfo.enabledExtensionCount = static_cast(filteredExts.size()); + localCreateInfo.ppEnabledExtensionNames = filteredExts.data(); auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder(); - result = vkEnc->vkCreateInstance(pCreateInfo, nullptr, &instance->internal_object, + result = vkEnc->vkCreateInstance(&localCreateInfo, nullptr, &instance->internal_object, true /* do lock */); if (VK_SUCCESS != result) { vk_free(pAllocator, instance); return vk_error(NULL, result); } - // Revert the createInfo the user-set data - mutableCreateInfo->enabledExtensionCount = initialEnabledExtensionCount; - mutableCreateInfo->ppEnabledExtensionNames = initialPpEnabledExtensionNames; } struct vk_instance_dispatch_table dispatch_table; @@ -446,11 +442,11 @@ VkResult gfxstream_vk_CreateDevice(VkPhysicalDevice physicalDevice, * and associated bugs. Mesa VK runtime also checks this, so we have to filter out before * reaches it. */ - VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT* swapchainMaintenance1Features = + VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT* mutableSwapchainMaintenance1Features = vk_find_struct(const_cast(pCreateInfo), PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT); - if (swapchainMaintenance1Features) { - swapchainMaintenance1Features->swapchainMaintenance1 = VK_FALSE; + if (mutableSwapchainMaintenance1Features) { + mutableSwapchainMaintenance1Features->swapchainMaintenance1 = VK_FALSE; } const VkAllocationCallbacks* pMesaAllocator = @@ -459,19 +455,17 @@ VkResult gfxstream_vk_CreateDevice(VkPhysicalDevice physicalDevice, pMesaAllocator, sizeof(struct gfxstream_vk_device), GFXSTREAM_DEFAULT_ALIGN, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); result = gfxstream_device ? VK_SUCCESS : VK_ERROR_OUT_OF_HOST_MEMORY; if (VK_SUCCESS == result) { - uint32_t initialEnabledExtensionCount = pCreateInfo->enabledExtensionCount; - const char* const* initialPpEnabledExtensionNames = pCreateInfo->ppEnabledExtensionNames; + // Full local copy of pCreateInfo + VkDeviceCreateInfo localCreateInfo = *pCreateInfo; + std::vector filteredExts = filteredDeviceExtensionNames( - pCreateInfo->enabledExtensionCount, pCreateInfo->ppEnabledExtensionNames); - // Temporarily modify createInfo for the encoder call - VkDeviceCreateInfo* mutableCreateInfo = (VkDeviceCreateInfo*)pCreateInfo; - mutableCreateInfo->enabledExtensionCount = static_cast(filteredExts.size()); - mutableCreateInfo->ppEnabledExtensionNames = filteredExts.data(); + localCreateInfo.enabledExtensionCount, localCreateInfo.ppEnabledExtensionNames); + localCreateInfo.enabledExtensionCount = static_cast(filteredExts.size()); + localCreateInfo.ppEnabledExtensionNames = filteredExts.data(); /* pNext = VkPhysicalDeviceGroupProperties */ std::vector initialPhysicalDeviceList; - VkPhysicalDeviceGroupProperties* mutablePhysicalDeviceGroupProperties = vk_find_struct( - const_cast(pCreateInfo), PHYSICAL_DEVICE_GROUP_PROPERTIES); + VkPhysicalDeviceGroupProperties* mutablePhysicalDeviceGroupProperties = vk_find_struct(&localCreateInfo, PHYSICAL_DEVICE_GROUP_PROPERTIES); if (mutablePhysicalDeviceGroupProperties) { // Temporarily modify the VkPhysicalDeviceGroupProperties structure to use translated // VkPhysicalDevice references for the encoder call @@ -487,12 +481,10 @@ VkResult gfxstream_vk_CreateDevice(VkPhysicalDevice physicalDevice, } auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder(); - result = vkEnc->vkCreateDevice(gfxstream_physicalDevice->internal_object, pCreateInfo, + result = vkEnc->vkCreateDevice(gfxstream_physicalDevice->internal_object, &localCreateInfo, pAllocator, &gfxstream_device->internal_object, true /* do lock */); - // Revert the createInfo the user-set data - mutableCreateInfo->enabledExtensionCount = initialEnabledExtensionCount; - mutableCreateInfo->ppEnabledExtensionNames = initialPpEnabledExtensionNames; + if (mutablePhysicalDeviceGroupProperties) { // Revert the physicalDevice list in VkPhysicalDeviceGroupProperties to the user-set // data diff --git a/src/gfxstream/guest/vulkan_enc/ResourceTracker.h b/src/gfxstream/guest/vulkan_enc/ResourceTracker.h index 1462a7bf02f..f6631d164c0 100644 --- a/src/gfxstream/guest/vulkan_enc/ResourceTracker.h +++ b/src/gfxstream/guest/vulkan_enc/ResourceTracker.h @@ -80,6 +80,16 @@ typedef uint64_t zx_koid_t; #include "gfxstream/guest/goldfish_sync.h" #endif +#define vk_filter_struct(__start, __sType) { \ + auto* curr = reinterpret_cast(__start); \ + while (curr != nullptr) { \ + if (curr->pNext != nullptr && curr->pNext->sType == VK_STRUCTURE_TYPE_##__sType) { \ + curr->pNext = curr->pNext->pNext; \ + } \ + curr = curr->pNext; \ + } \ +} \ + // This should be ABI identical with the variant in ResourceTracker.h struct GfxStreamVkFeatureInfo { bool hasDirectMem;