15db71995Sopenharmony_ci/*
25db71995Sopenharmony_ci * Copyright (c) 2021-2022 The Khronos Group Inc.
35db71995Sopenharmony_ci * Copyright (c) 2021-2022 Valve Corporation
45db71995Sopenharmony_ci * Copyright (c) 2021-2022 LunarG, Inc.
55db71995Sopenharmony_ci *
65db71995Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
75db71995Sopenharmony_ci * of this software and/or associated documentation files (the "Materials"), to
85db71995Sopenharmony_ci * deal in the Materials without restriction, including without limitation the
95db71995Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
105db71995Sopenharmony_ci * sell copies of the Materials, and to permit persons to whom the Materials are
115db71995Sopenharmony_ci * furnished to do so, subject to the following conditions:
125db71995Sopenharmony_ci *
135db71995Sopenharmony_ci * The above copyright notice(s) and this permission notice shall be included in
145db71995Sopenharmony_ci * all copies or substantial portions of the Materials.
155db71995Sopenharmony_ci *
165db71995Sopenharmony_ci * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
175db71995Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
185db71995Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
195db71995Sopenharmony_ci *
205db71995Sopenharmony_ci * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
215db71995Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
225db71995Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
235db71995Sopenharmony_ci * USE OR OTHER DEALINGS IN THE MATERIALS.
245db71995Sopenharmony_ci *
255db71995Sopenharmony_ci * Author: Charles Giessen <charles@lunarg.com>
265db71995Sopenharmony_ci */
275db71995Sopenharmony_ci
285db71995Sopenharmony_ci#include "test_environment.h"
295db71995Sopenharmony_ci
305db71995Sopenharmony_ci// Verify that the various ways to get vkGetInstanceProcAddr return the same value
315db71995Sopenharmony_ciTEST(GetProcAddr, VerifyGetInstanceProcAddr) {
325db71995Sopenharmony_ci    FrameworkEnvironment env{};
335db71995Sopenharmony_ci    env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA)).add_physical_device("physical_device_0");
345db71995Sopenharmony_ci    {
355db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
365db71995Sopenharmony_ci        inst.create_info.set_api_version(VK_API_VERSION_1_1);
375db71995Sopenharmony_ci        inst.CheckCreate();
385db71995Sopenharmony_ci
395db71995Sopenharmony_ci        // NOTE: The vulkan_functions are queried using the platform get proc addr from the loader.  So we'll compare
405db71995Sopenharmony_ci        //       that to what is returned by asking it what the various Vulkan get proc addr functions are.
415db71995Sopenharmony_ci        PFN_vkGetInstanceProcAddr gipa_loader = env.vulkan_functions.vkGetInstanceProcAddr;
425db71995Sopenharmony_ci        PFN_vkGetInstanceProcAddr gipa_queried = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
435db71995Sopenharmony_ci            env.vulkan_functions.vkGetInstanceProcAddr(inst.inst, "vkGetInstanceProcAddr"));
445db71995Sopenharmony_ci        ASSERT_EQ(gipa_loader, gipa_queried);
455db71995Sopenharmony_ci    }
465db71995Sopenharmony_ci
475db71995Sopenharmony_ci    {
485db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
495db71995Sopenharmony_ci        inst.create_info.set_api_version(VK_API_VERSION_1_3);
505db71995Sopenharmony_ci        inst.CheckCreate();
515db71995Sopenharmony_ci
525db71995Sopenharmony_ci        // NOTE: The vulkan_functions are queried using the platform get proc addr from the loader.  So we'll compare
535db71995Sopenharmony_ci        //       that to what is returned by asking it what the various Vulkan get proc addr functions are.
545db71995Sopenharmony_ci        PFN_vkGetInstanceProcAddr gipa_loader = env.vulkan_functions.vkGetInstanceProcAddr;
555db71995Sopenharmony_ci        PFN_vkGetInstanceProcAddr gipa_queried = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
565db71995Sopenharmony_ci            env.vulkan_functions.vkGetInstanceProcAddr(inst.inst, "vkGetInstanceProcAddr"));
575db71995Sopenharmony_ci        ASSERT_EQ(gipa_loader, gipa_queried);
585db71995Sopenharmony_ci    }
595db71995Sopenharmony_ci}
605db71995Sopenharmony_ci
615db71995Sopenharmony_ci// Verify that the various ways to get vkGetDeviceProcAddr return the same value
625db71995Sopenharmony_ciTEST(GetProcAddr, VerifyGetDeviceProcAddr) {
635db71995Sopenharmony_ci    FrameworkEnvironment env{};
645db71995Sopenharmony_ci    env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA)).add_physical_device("physical_device_0");
655db71995Sopenharmony_ci
665db71995Sopenharmony_ci    InstWrapper inst{env.vulkan_functions};
675db71995Sopenharmony_ci    inst.create_info.set_api_version(VK_API_VERSION_1_1);
685db71995Sopenharmony_ci    inst.CheckCreate();
695db71995Sopenharmony_ci    VkPhysicalDevice phys_dev = inst.GetPhysDev();
705db71995Sopenharmony_ci
715db71995Sopenharmony_ci    // NOTE: The vulkan_functions are queried using the platform get proc addr from the loader.  So we'll compare
725db71995Sopenharmony_ci    //       that to what is returned by asking it what the various Vulkan get proc addr functions are.
735db71995Sopenharmony_ci    PFN_vkGetDeviceProcAddr gdpa_loader = env.vulkan_functions.vkGetDeviceProcAddr;
745db71995Sopenharmony_ci    PFN_vkGetDeviceProcAddr gdpa_inst_queried = inst.load("vkGetDeviceProcAddr");
755db71995Sopenharmony_ci    ASSERT_EQ(gdpa_loader, gdpa_inst_queried);
765db71995Sopenharmony_ci
775db71995Sopenharmony_ci    DeviceWrapper dev{inst};
785db71995Sopenharmony_ci    dev.CheckCreate(phys_dev);
795db71995Sopenharmony_ci
805db71995Sopenharmony_ci    PFN_vkGetDeviceProcAddr gdpa_dev_queried = dev.load("vkGetDeviceProcAddr");
815db71995Sopenharmony_ci    ASSERT_EQ(gdpa_loader, gdpa_dev_queried);
825db71995Sopenharmony_ci}
835db71995Sopenharmony_ci
845db71995Sopenharmony_ci// Load the global function pointers with and without a NULL vkInstance handle.
855db71995Sopenharmony_ci// Call the function to make sure it is callable, don't care about what is returned.
865db71995Sopenharmony_ciTEST(GetProcAddr, GlobalFunctions) {
875db71995Sopenharmony_ci    FrameworkEnvironment env{};
885db71995Sopenharmony_ci    env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA)).add_physical_device("physical_device_0");
895db71995Sopenharmony_ci
905db71995Sopenharmony_ci    auto& gipa = env.vulkan_functions.vkGetInstanceProcAddr;
915db71995Sopenharmony_ci    // global entry points with NULL instance handle
925db71995Sopenharmony_ci    {
935db71995Sopenharmony_ci        auto EnumerateInstanceExtensionProperties =
945db71995Sopenharmony_ci            reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(gipa(NULL, "vkEnumerateInstanceExtensionProperties"));
955db71995Sopenharmony_ci        handle_assert_has_value(EnumerateInstanceExtensionProperties);
965db71995Sopenharmony_ci        uint32_t ext_count = 0;
975db71995Sopenharmony_ci        ASSERT_EQ(VK_SUCCESS, EnumerateInstanceExtensionProperties("", &ext_count, nullptr));
985db71995Sopenharmony_ci
995db71995Sopenharmony_ci        auto EnumerateInstanceLayerProperties =
1005db71995Sopenharmony_ci            reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(gipa(NULL, "vkEnumerateInstanceLayerProperties"));
1015db71995Sopenharmony_ci        handle_assert_has_value(EnumerateInstanceLayerProperties);
1025db71995Sopenharmony_ci        uint32_t layer_count = 0;
1035db71995Sopenharmony_ci        ASSERT_EQ(VK_SUCCESS, EnumerateInstanceLayerProperties(&layer_count, nullptr));
1045db71995Sopenharmony_ci
1055db71995Sopenharmony_ci        auto EnumerateInstanceVersion = reinterpret_cast<PFN_vkEnumerateInstanceVersion>(gipa(NULL, "vkEnumerateInstanceVersion"));
1065db71995Sopenharmony_ci        handle_assert_has_value(EnumerateInstanceVersion);
1075db71995Sopenharmony_ci        uint32_t api_version = 0;
1085db71995Sopenharmony_ci        EnumerateInstanceVersion(&api_version);
1095db71995Sopenharmony_ci
1105db71995Sopenharmony_ci        auto GetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(gipa(NULL, "vkGetInstanceProcAddr"));
1115db71995Sopenharmony_ci        ASSERT_EQ(GetInstanceProcAddr,
1125db71995Sopenharmony_ci                  reinterpret_cast<PFN_vkGetInstanceProcAddr>(GetInstanceProcAddr(NULL, "vkGetInstanceProcAddr")));
1135db71995Sopenharmony_ci
1145db71995Sopenharmony_ci        auto CreateInstance = reinterpret_cast<PFN_vkCreateInstance>(gipa(NULL, "vkCreateInstance"));
1155db71995Sopenharmony_ci        handle_assert_has_value(CreateInstance);
1165db71995Sopenharmony_ci    }
1175db71995Sopenharmony_ci    // Now create an instance and query the functions again - should work because the instance version is less than 1.2
1185db71995Sopenharmony_ci    for (int i = 0; i <= 2; i++) {
1195db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
1205db71995Sopenharmony_ci        inst.create_info.api_version = VK_MAKE_API_VERSION(0, 1, i, 0);
1215db71995Sopenharmony_ci        inst.CheckCreate();
1225db71995Sopenharmony_ci
1235db71995Sopenharmony_ci        PFN_vkEnumerateInstanceExtensionProperties EnumerateInstanceExtensionProperties =
1245db71995Sopenharmony_ci            inst.load("vkEnumerateInstanceExtensionProperties");
1255db71995Sopenharmony_ci        handle_assert_has_value(EnumerateInstanceExtensionProperties);
1265db71995Sopenharmony_ci        uint32_t ext_count = 0;
1275db71995Sopenharmony_ci        ASSERT_EQ(VK_SUCCESS, EnumerateInstanceExtensionProperties("", &ext_count, nullptr));
1285db71995Sopenharmony_ci
1295db71995Sopenharmony_ci        PFN_vkEnumerateInstanceLayerProperties EnumerateInstanceLayerProperties = inst.load("vkEnumerateInstanceLayerProperties");
1305db71995Sopenharmony_ci        handle_assert_has_value(EnumerateInstanceLayerProperties);
1315db71995Sopenharmony_ci        uint32_t layer_count = 0;
1325db71995Sopenharmony_ci        ASSERT_EQ(VK_SUCCESS, EnumerateInstanceLayerProperties(&layer_count, nullptr));
1335db71995Sopenharmony_ci
1345db71995Sopenharmony_ci        PFN_vkEnumerateInstanceVersion EnumerateInstanceVersion = inst.load("vkEnumerateInstanceVersion");
1355db71995Sopenharmony_ci        handle_assert_has_value(EnumerateInstanceVersion);
1365db71995Sopenharmony_ci        uint32_t api_version = 0;
1375db71995Sopenharmony_ci        EnumerateInstanceVersion(&api_version);
1385db71995Sopenharmony_ci
1395db71995Sopenharmony_ci        PFN_vkGetInstanceProcAddr GetInstanceProcAddr = inst.load("vkGetInstanceProcAddr");
1405db71995Sopenharmony_ci        handle_assert_has_value(GetInstanceProcAddr);
1415db71995Sopenharmony_ci        ASSERT_EQ(GetInstanceProcAddr,
1425db71995Sopenharmony_ci                  reinterpret_cast<PFN_vkGetInstanceProcAddr>(GetInstanceProcAddr(inst, "vkGetInstanceProcAddr")));
1435db71995Sopenharmony_ci
1445db71995Sopenharmony_ci        PFN_vkCreateInstance CreateInstance = inst.load("vkCreateInstance");
1455db71995Sopenharmony_ci        handle_assert_has_value(CreateInstance);
1465db71995Sopenharmony_ci    }
1475db71995Sopenharmony_ci    {
1485db71995Sopenharmony_ci        // Create a 1.3 instance - now everything should return NULL
1495db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
1505db71995Sopenharmony_ci        inst.create_info.api_version = VK_MAKE_API_VERSION(0, 1, 3, 0);
1515db71995Sopenharmony_ci        inst.CheckCreate();
1525db71995Sopenharmony_ci
1535db71995Sopenharmony_ci        PFN_vkEnumerateInstanceExtensionProperties EnumerateInstanceExtensionProperties =
1545db71995Sopenharmony_ci            inst.load("vkEnumerateInstanceExtensionProperties");
1555db71995Sopenharmony_ci        handle_assert_null(EnumerateInstanceExtensionProperties);
1565db71995Sopenharmony_ci
1575db71995Sopenharmony_ci        PFN_vkEnumerateInstanceLayerProperties EnumerateInstanceLayerProperties = inst.load("vkEnumerateInstanceLayerProperties");
1585db71995Sopenharmony_ci        handle_assert_null(EnumerateInstanceLayerProperties);
1595db71995Sopenharmony_ci
1605db71995Sopenharmony_ci        PFN_vkEnumerateInstanceVersion EnumerateInstanceVersion = inst.load("vkEnumerateInstanceVersion");
1615db71995Sopenharmony_ci        handle_assert_null(EnumerateInstanceVersion);
1625db71995Sopenharmony_ci
1635db71995Sopenharmony_ci        PFN_vkCreateInstance CreateInstance = inst.load("vkCreateInstance");
1645db71995Sopenharmony_ci        handle_assert_null(CreateInstance);
1655db71995Sopenharmony_ci
1665db71995Sopenharmony_ci        PFN_vkGetInstanceProcAddr GetInstanceProcAddr = inst.load("vkGetInstanceProcAddr");
1675db71995Sopenharmony_ci        handle_assert_equal(env.vulkan_functions.vkGetInstanceProcAddr, GetInstanceProcAddr);
1685db71995Sopenharmony_ci        ASSERT_EQ(GetInstanceProcAddr,
1695db71995Sopenharmony_ci                  reinterpret_cast<PFN_vkGetInstanceProcAddr>(GetInstanceProcAddr(inst, "vkGetInstanceProcAddr")));
1705db71995Sopenharmony_ci        ASSERT_EQ(GetInstanceProcAddr,
1715db71995Sopenharmony_ci                  reinterpret_cast<PFN_vkGetInstanceProcAddr>(GetInstanceProcAddr(NULL, "vkGetInstanceProcAddr")));
1725db71995Sopenharmony_ci        // get a non pre-instance function pointer
1735db71995Sopenharmony_ci        PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices = inst.load("vkEnumeratePhysicalDevices");
1745db71995Sopenharmony_ci        handle_assert_has_value(EnumeratePhysicalDevices);
1755db71995Sopenharmony_ci
1765db71995Sopenharmony_ci        EnumeratePhysicalDevices = reinterpret_cast<PFN_vkEnumeratePhysicalDevices>(gipa(NULL, "vkEnumeratePhysicalDevices"));
1775db71995Sopenharmony_ci        handle_assert_null(EnumeratePhysicalDevices);
1785db71995Sopenharmony_ci    }
1795db71995Sopenharmony_ci}
1805db71995Sopenharmony_ci
1815db71995Sopenharmony_ciTEST(GetProcAddr, Verify10FunctionsFailToLoadWithSingleDriver) {
1825db71995Sopenharmony_ci    FrameworkEnvironment env{};
1835db71995Sopenharmony_ci    env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2)).add_physical_device({}).set_can_query_GetPhysicalDeviceFuncs(false);
1845db71995Sopenharmony_ci
1855db71995Sopenharmony_ci    InstWrapper inst{env.vulkan_functions};
1865db71995Sopenharmony_ci    inst.CheckCreate(VK_ERROR_INCOMPATIBLE_DRIVER);
1875db71995Sopenharmony_ci}
1885db71995Sopenharmony_ci
1895db71995Sopenharmony_ciTEST(GetProcAddr, Verify10FunctionsLoadWithMultipleDrivers) {
1905db71995Sopenharmony_ci    FrameworkEnvironment env{};
1915db71995Sopenharmony_ci    env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2)).add_physical_device({});
1925db71995Sopenharmony_ci    env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2)).add_physical_device({}).set_can_query_GetPhysicalDeviceFuncs(false);
1935db71995Sopenharmony_ci
1945db71995Sopenharmony_ci    InstWrapper inst{env.vulkan_functions};
1955db71995Sopenharmony_ci    inst.CheckCreate();
1965db71995Sopenharmony_ci
1975db71995Sopenharmony_ci    inst.GetPhysDevs(1);
1985db71995Sopenharmony_ci}
1995db71995Sopenharmony_ci
2005db71995Sopenharmony_ci// Swapchain functions which require a terminator in all cases have situations where the driver may have a
2015db71995Sopenharmony_ci// NULL function pointer but the loader shouldn't abort() if that is the case. Rather, it should log a message
2025db71995Sopenharmony_ci// and return VK_SUCCESS to maintain previous behavior.
2035db71995Sopenharmony_ciTEST(GetDeviceProcAddr, SwapchainFuncsWithTerminator) {
2045db71995Sopenharmony_ci    FrameworkEnvironment env{};
2055db71995Sopenharmony_ci    auto& driver =
2065db71995Sopenharmony_ci        env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA)).setup_WSI().add_physical_device("physical_device_0");
2075db71995Sopenharmony_ci
2085db71995Sopenharmony_ci    InstWrapper inst(env.vulkan_functions);
2095db71995Sopenharmony_ci    inst.create_info.add_extension("VK_EXT_debug_utils");
2105db71995Sopenharmony_ci    inst.create_info.setup_WSI();
2115db71995Sopenharmony_ci    ASSERT_NO_FATAL_FAILURE(inst.CheckCreate());
2125db71995Sopenharmony_ci
2135db71995Sopenharmony_ci    VkSurfaceKHR surface{};
2145db71995Sopenharmony_ci    ASSERT_EQ(VK_SUCCESS, create_surface(inst, surface));
2155db71995Sopenharmony_ci
2165db71995Sopenharmony_ci    DebugUtilsWrapper log{inst};
2175db71995Sopenharmony_ci    ASSERT_EQ(VK_SUCCESS, CreateDebugUtilsMessenger(log));
2185db71995Sopenharmony_ci    auto phys_dev = inst.GetPhysDev();
2195db71995Sopenharmony_ci    {
2205db71995Sopenharmony_ci        DeviceWrapper dev{inst};
2215db71995Sopenharmony_ci        ASSERT_NO_FATAL_FAILURE(dev.CheckCreate(phys_dev));
2225db71995Sopenharmony_ci        DeviceFunctions dev_funcs{env.vulkan_functions, dev};
2235db71995Sopenharmony_ci
2245db71995Sopenharmony_ci        PFN_vkCreateSwapchainKHR CreateSwapchainKHR = dev.load("vkCreateSwapchainKHR");
2255db71995Sopenharmony_ci        PFN_vkCreateSwapchainKHR inst_CreateSwapchainKHR = inst.load("vkCreateSwapchainKHR");
2265db71995Sopenharmony_ci        PFN_vkGetDeviceGroupSurfacePresentModesKHR GetDeviceGroupSurfacePresentModesKHR =
2275db71995Sopenharmony_ci            dev.load("vkGetDeviceGroupSurfacePresentModesKHR");
2285db71995Sopenharmony_ci        PFN_vkCreateSharedSwapchainsKHR CreateSharedSwapchainsKHR = dev.load("vkCreateSharedSwapchainsKHR");
2295db71995Sopenharmony_ci        ASSERT_FALSE(CreateSwapchainKHR);
2305db71995Sopenharmony_ci        ASSERT_TRUE(inst_CreateSwapchainKHR);
2315db71995Sopenharmony_ci        ASSERT_FALSE(GetDeviceGroupSurfacePresentModesKHR);
2325db71995Sopenharmony_ci        ASSERT_FALSE(CreateSharedSwapchainsKHR);
2335db71995Sopenharmony_ci
2345db71995Sopenharmony_ci        VkSwapchainCreateInfoKHR info{};
2355db71995Sopenharmony_ci        info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
2365db71995Sopenharmony_ci        info.surface = surface;
2375db71995Sopenharmony_ci
2385db71995Sopenharmony_ci        VkSwapchainKHR swapchain{};
2395db71995Sopenharmony_ci        if (CreateSwapchainKHR) CreateSwapchainKHR(dev.dev, &info, nullptr, &swapchain);
2405db71995Sopenharmony_ci        ASSERT_FALSE(
2415db71995Sopenharmony_ci            log.find("vkCreateSwapchainKHR: Driver's function pointer was NULL, returning VK_SUCCESS. Was the VK_KHR_swapchain "
2425db71995Sopenharmony_ci                     "extension enabled?"));
2435db71995Sopenharmony_ci        log.logger.clear();
2445db71995Sopenharmony_ci        if (dev_funcs.vkDestroySwapchainKHR) dev_funcs.vkDestroySwapchainKHR(dev.dev, swapchain, nullptr);
2455db71995Sopenharmony_ci        // try to call the vkCreateSwapchainKHR acquired from the instance - this *should* abort due to not enabling the extension
2465db71995Sopenharmony_ci        if (inst_CreateSwapchainKHR) {
2475db71995Sopenharmony_ci            ASSERT_DEATH(inst_CreateSwapchainKHR(dev.dev, &info, nullptr, &swapchain),
2485db71995Sopenharmony_ci                         "vkCreateSwapchainKHR: Driver's function pointer was NULL, returning VK_SUCCESS. Was the VK_KHR_swapchain "
2495db71995Sopenharmony_ci                         "extension enabled?");
2505db71995Sopenharmony_ci        }
2515db71995Sopenharmony_ci        log.logger.clear();
2525db71995Sopenharmony_ci        if (dev_funcs.vkDestroySwapchainKHR) dev_funcs.vkDestroySwapchainKHR(dev.dev, swapchain, nullptr);
2535db71995Sopenharmony_ci
2545db71995Sopenharmony_ci        VkDeviceGroupPresentModeFlagsKHR modes{};
2555db71995Sopenharmony_ci        if (GetDeviceGroupSurfacePresentModesKHR) GetDeviceGroupSurfacePresentModesKHR(dev.dev, surface, &modes);
2565db71995Sopenharmony_ci
2575db71995Sopenharmony_ci        if (CreateSharedSwapchainsKHR) CreateSharedSwapchainsKHR(dev.dev, 1, &info, nullptr, &swapchain);
2585db71995Sopenharmony_ci    }
2595db71995Sopenharmony_ci    driver.physical_devices.at(0).add_extensions({"VK_KHR_swapchain", "VK_KHR_display_swapchain", "VK_EXT_debug_marker"});
2605db71995Sopenharmony_ci    {
2615db71995Sopenharmony_ci        DeviceWrapper dev{inst};
2625db71995Sopenharmony_ci        dev.create_info.add_extensions({"VK_KHR_swapchain", "VK_KHR_display_swapchain", "VK_EXT_debug_marker"});
2635db71995Sopenharmony_ci        ASSERT_NO_FATAL_FAILURE(dev.CheckCreate(phys_dev));
2645db71995Sopenharmony_ci        DeviceFunctions dev_funcs{env.vulkan_functions, dev};
2655db71995Sopenharmony_ci
2665db71995Sopenharmony_ci        PFN_vkCreateSwapchainKHR CreateSwapchainKHR = dev.load("vkCreateSwapchainKHR");
2675db71995Sopenharmony_ci        PFN_vkCreateSwapchainKHR inst_CreateSwapchainKHR = inst.load("vkCreateSwapchainKHR");
2685db71995Sopenharmony_ci        PFN_vkGetDeviceGroupSurfacePresentModesKHR GetDeviceGroupSurfacePresentModesKHR =
2695db71995Sopenharmony_ci            dev.load("vkGetDeviceGroupSurfacePresentModesKHR");
2705db71995Sopenharmony_ci        PFN_vkCreateSharedSwapchainsKHR CreateSharedSwapchainsKHR = dev.load("vkCreateSharedSwapchainsKHR");
2715db71995Sopenharmony_ci        ASSERT_TRUE(CreateSwapchainKHR);
2725db71995Sopenharmony_ci        ASSERT_TRUE(inst_CreateSwapchainKHR);
2735db71995Sopenharmony_ci        ASSERT_TRUE(GetDeviceGroupSurfacePresentModesKHR);
2745db71995Sopenharmony_ci        ASSERT_TRUE(CreateSharedSwapchainsKHR);
2755db71995Sopenharmony_ci
2765db71995Sopenharmony_ci        VkSwapchainCreateInfoKHR info{};
2775db71995Sopenharmony_ci        info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
2785db71995Sopenharmony_ci        info.surface = surface;
2795db71995Sopenharmony_ci
2805db71995Sopenharmony_ci        VkSwapchainKHR swapchain{};
2815db71995Sopenharmony_ci        if (CreateSwapchainKHR) CreateSwapchainKHR(dev.dev, &info, nullptr, &swapchain);
2825db71995Sopenharmony_ci        ASSERT_FALSE(
2835db71995Sopenharmony_ci            log.find("vkCreateSwapchainKHR: Driver's function pointer was NULL, returning VK_SUCCESS. Was the VK_KHR_swapchain "
2845db71995Sopenharmony_ci                     "extension enabled?"));
2855db71995Sopenharmony_ci        log.logger.clear();
2865db71995Sopenharmony_ci        if (dev_funcs.vkDestroySwapchainKHR) dev_funcs.vkDestroySwapchainKHR(dev.dev, swapchain, nullptr);
2875db71995Sopenharmony_ci        if (inst_CreateSwapchainKHR) inst_CreateSwapchainKHR(dev.dev, &info, nullptr, &swapchain);
2885db71995Sopenharmony_ci        ASSERT_FALSE(
2895db71995Sopenharmony_ci            log.find("vkCreateSwapchainKHR: Driver's function pointer was NULL, returning VK_SUCCESS. Was the VK_KHR_swapchain "
2905db71995Sopenharmony_ci                     "extension enabled?"));
2915db71995Sopenharmony_ci        log.logger.clear();
2925db71995Sopenharmony_ci        if (dev_funcs.vkDestroySwapchainKHR) dev_funcs.vkDestroySwapchainKHR(dev.dev, swapchain, nullptr);
2935db71995Sopenharmony_ci
2945db71995Sopenharmony_ci        VkDeviceGroupPresentModeFlagsKHR modes{};
2955db71995Sopenharmony_ci        if (GetDeviceGroupSurfacePresentModesKHR) GetDeviceGroupSurfacePresentModesKHR(dev.dev, surface, &modes);
2965db71995Sopenharmony_ci
2975db71995Sopenharmony_ci        if (CreateSharedSwapchainsKHR) CreateSharedSwapchainsKHR(dev.dev, 1, &info, nullptr, &swapchain);
2985db71995Sopenharmony_ci    }
2995db71995Sopenharmony_ci    env.vulkan_functions.vkDestroySurfaceKHR(inst.inst, surface, nullptr);
3005db71995Sopenharmony_ci}
3015db71995Sopenharmony_ci
3025db71995Sopenharmony_ci// Verify that the various ways to get vkGetDeviceProcAddr return the same value
3035db71995Sopenharmony_ciTEST(GetProcAddr, PreserveLayerGettingVkCreateDeviceWithNullInstance) {
3045db71995Sopenharmony_ci    FrameworkEnvironment env{};
3055db71995Sopenharmony_ci    env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA)).add_physical_device("physical_device_0");
3065db71995Sopenharmony_ci
3075db71995Sopenharmony_ci    env.add_implicit_layer(TestLayerDetails(ManifestLayer{}.add_layer(ManifestLayer::LayerDescription{}
3085db71995Sopenharmony_ci                                                                          .set_name("VK_LAYER_technically_buggy_layer")
3095db71995Sopenharmony_ci                                                                          .set_description("actually_layer_1")
3105db71995Sopenharmony_ci                                                                          .set_lib_path(TEST_LAYER_PATH_EXPORT_VERSION_2)
3115db71995Sopenharmony_ci                                                                          .set_disable_environment("if_you_can")),
3125db71995Sopenharmony_ci                                            "buggy_layer_1.json"));
3135db71995Sopenharmony_ci    env.get_test_layer().set_buggy_query_of_vkCreateDevice(true);
3145db71995Sopenharmony_ci    InstWrapper inst{env.vulkan_functions};
3155db71995Sopenharmony_ci    inst.create_info.set_api_version(VK_API_VERSION_1_1);
3165db71995Sopenharmony_ci    inst.CheckCreate();
3175db71995Sopenharmony_ci    VkPhysicalDevice phys_dev = inst.GetPhysDev();
3185db71995Sopenharmony_ci
3195db71995Sopenharmony_ci    DeviceWrapper dev{inst};
3205db71995Sopenharmony_ci    dev.CheckCreate(phys_dev);
3215db71995Sopenharmony_ci}
3225db71995Sopenharmony_ci
3235db71995Sopenharmony_ci// The following tests - AppQueries11FunctionsWhileOnlyEnabling10, AppQueries12FunctionsWhileOnlyEnabling11, and
3245db71995Sopenharmony_ci// AppQueries13FunctionsWhileOnlyEnabling12 - check that vkGetDeviceProcAddr only returning functions from core versions up to
3255db71995Sopenharmony_ci// the apiVersion declared in VkApplicationInfo. Function querying should succeed if VK_KHR_maintenance_5 is not enabled, and they
3265db71995Sopenharmony_ci// should return zero when that extension is enabled.
3275db71995Sopenharmony_ci
3285db71995Sopenharmony_ciTEST(GetDeviceProcAddr, AppQueries11FunctionsWhileOnlyEnabling10) {
3295db71995Sopenharmony_ci    FrameworkEnvironment env{};
3305db71995Sopenharmony_ci    auto& driver =
3315db71995Sopenharmony_ci        env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2, VK_API_VERSION_1_1))
3325db71995Sopenharmony_ci            .set_icd_api_version(VK_API_VERSION_1_1)
3335db71995Sopenharmony_ci            .add_physical_device(
3345db71995Sopenharmony_ci                PhysicalDevice{}.set_api_version(VK_API_VERSION_1_1).add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME).finish());
3355db71995Sopenharmony_ci
3365db71995Sopenharmony_ci    std::vector<const char*> functions = {"vkGetDeviceQueue2", "vkCmdDispatchBase", "vkCreateDescriptorUpdateTemplate"};
3375db71995Sopenharmony_ci    for (const auto& f : functions) {
3385db71995Sopenharmony_ci        driver.physical_devices.back().add_device_function(VulkanFunction{f, [] {}});
3395db71995Sopenharmony_ci    }
3405db71995Sopenharmony_ci    {  // doesn't enable the feature or extension
3415db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
3425db71995Sopenharmony_ci        inst.create_info.set_api_version(1, 0, 0);
3435db71995Sopenharmony_ci        inst.CheckCreate();
3445db71995Sopenharmony_ci
3455db71995Sopenharmony_ci        DeviceWrapper dev{inst};
3465db71995Sopenharmony_ci        dev.CheckCreate(inst.GetPhysDev());
3475db71995Sopenharmony_ci        for (const auto& f : functions) {
3485db71995Sopenharmony_ci            ASSERT_NE(nullptr, dev->vkGetDeviceProcAddr(dev.dev, f));
3495db71995Sopenharmony_ci        }
3505db71995Sopenharmony_ci    }
3515db71995Sopenharmony_ci    {  // doesn't enable the feature
3525db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
3535db71995Sopenharmony_ci        inst.create_info.set_api_version(1, 0, 0);
3545db71995Sopenharmony_ci        inst.CheckCreate();
3555db71995Sopenharmony_ci
3565db71995Sopenharmony_ci        DeviceWrapper dev{inst};
3575db71995Sopenharmony_ci        dev.create_info.add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);
3585db71995Sopenharmony_ci        dev.CheckCreate(inst.GetPhysDev());
3595db71995Sopenharmony_ci        for (const auto& f : functions) {
3605db71995Sopenharmony_ci            ASSERT_NE(nullptr, dev->vkGetDeviceProcAddr(dev.dev, f));
3615db71995Sopenharmony_ci        }
3625db71995Sopenharmony_ci    }
3635db71995Sopenharmony_ci    {  // enables the feature and extension
3645db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
3655db71995Sopenharmony_ci        inst.create_info.set_api_version(1, 0, 0);
3665db71995Sopenharmony_ci        inst.CheckCreate();
3675db71995Sopenharmony_ci
3685db71995Sopenharmony_ci        VkPhysicalDeviceMaintenance5FeaturesKHR features{};
3695db71995Sopenharmony_ci        features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES_KHR;
3705db71995Sopenharmony_ci        features.maintenance5 = VK_TRUE;
3715db71995Sopenharmony_ci
3725db71995Sopenharmony_ci        DeviceWrapper dev{inst};
3735db71995Sopenharmony_ci        dev.create_info.add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);
3745db71995Sopenharmony_ci        dev.create_info.dev.pNext = &features;
3755db71995Sopenharmony_ci        dev.CheckCreate(inst.GetPhysDev());
3765db71995Sopenharmony_ci        for (const auto& f : functions) {
3775db71995Sopenharmony_ci            ASSERT_EQ(nullptr, dev->vkGetDeviceProcAddr(dev.dev, f));
3785db71995Sopenharmony_ci        }
3795db71995Sopenharmony_ci    }
3805db71995Sopenharmony_ci}
3815db71995Sopenharmony_ci
3825db71995Sopenharmony_ciTEST(GetDeviceProcAddr, AppQueries12FunctionsWhileOnlyEnabling11) {
3835db71995Sopenharmony_ci    FrameworkEnvironment env{};
3845db71995Sopenharmony_ci    auto& driver =
3855db71995Sopenharmony_ci        env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2, VK_API_VERSION_1_2))
3865db71995Sopenharmony_ci            .set_icd_api_version(VK_API_VERSION_1_2)
3875db71995Sopenharmony_ci            .add_physical_device(
3885db71995Sopenharmony_ci                PhysicalDevice{}.set_api_version(VK_API_VERSION_1_2).add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME).finish());
3895db71995Sopenharmony_ci    std::vector<const char*> functions = {"vkCmdDrawIndirectCount", "vkCmdNextSubpass2", "vkGetBufferDeviceAddress",
3905db71995Sopenharmony_ci                                          "vkGetDeviceMemoryOpaqueCaptureAddress"};
3915db71995Sopenharmony_ci    for (const auto& f : functions) {
3925db71995Sopenharmony_ci        driver.physical_devices.back().add_device_function(VulkanFunction{f, [] {}});
3935db71995Sopenharmony_ci    }
3945db71995Sopenharmony_ci    {  // doesn't enable the feature or extension
3955db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
3965db71995Sopenharmony_ci        inst.create_info.set_api_version(1, 1, 0);
3975db71995Sopenharmony_ci        inst.CheckCreate();
3985db71995Sopenharmony_ci
3995db71995Sopenharmony_ci        DeviceWrapper dev{inst};
4005db71995Sopenharmony_ci        dev.CheckCreate(inst.GetPhysDev());
4015db71995Sopenharmony_ci
4025db71995Sopenharmony_ci        for (const auto& f : functions) {
4035db71995Sopenharmony_ci            ASSERT_NE(nullptr, dev->vkGetDeviceProcAddr(dev.dev, f));
4045db71995Sopenharmony_ci        }
4055db71995Sopenharmony_ci    }
4065db71995Sopenharmony_ci    {  // doesn't enable the feature
4075db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
4085db71995Sopenharmony_ci        inst.create_info.set_api_version(1, 1, 0);
4095db71995Sopenharmony_ci        inst.CheckCreate();
4105db71995Sopenharmony_ci
4115db71995Sopenharmony_ci        DeviceWrapper dev{inst};
4125db71995Sopenharmony_ci        dev.create_info.add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);
4135db71995Sopenharmony_ci        dev.CheckCreate(inst.GetPhysDev());
4145db71995Sopenharmony_ci
4155db71995Sopenharmony_ci        for (const auto& f : functions) {
4165db71995Sopenharmony_ci            ASSERT_NE(nullptr, dev->vkGetDeviceProcAddr(dev.dev, f));
4175db71995Sopenharmony_ci        }
4185db71995Sopenharmony_ci    }
4195db71995Sopenharmony_ci    {  // enables the feature and extension
4205db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
4215db71995Sopenharmony_ci        inst.create_info.set_api_version(1, 1, 0);
4225db71995Sopenharmony_ci        inst.CheckCreate();
4235db71995Sopenharmony_ci
4245db71995Sopenharmony_ci        VkPhysicalDeviceMaintenance5FeaturesKHR features{};
4255db71995Sopenharmony_ci        features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES_KHR;
4265db71995Sopenharmony_ci        features.maintenance5 = VK_TRUE;
4275db71995Sopenharmony_ci
4285db71995Sopenharmony_ci        DeviceWrapper dev{inst};
4295db71995Sopenharmony_ci        dev.create_info.add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);
4305db71995Sopenharmony_ci        dev.create_info.dev.pNext = &features;
4315db71995Sopenharmony_ci        dev.CheckCreate(inst.GetPhysDev());
4325db71995Sopenharmony_ci
4335db71995Sopenharmony_ci        for (const auto& f : functions) {
4345db71995Sopenharmony_ci            ASSERT_EQ(nullptr, dev->vkGetDeviceProcAddr(dev.dev, f));
4355db71995Sopenharmony_ci        }
4365db71995Sopenharmony_ci    }
4375db71995Sopenharmony_ci}
4385db71995Sopenharmony_ci
4395db71995Sopenharmony_ciTEST(GetDeviceProcAddr, AppQueries13FunctionsWhileOnlyEnabling12) {
4405db71995Sopenharmony_ci    FrameworkEnvironment env{};
4415db71995Sopenharmony_ci    auto& driver =
4425db71995Sopenharmony_ci        env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2, VK_API_VERSION_1_3))
4435db71995Sopenharmony_ci            .set_icd_api_version(VK_API_VERSION_1_3)
4445db71995Sopenharmony_ci            .add_physical_device(
4455db71995Sopenharmony_ci                PhysicalDevice{}.set_api_version(VK_API_VERSION_1_3).add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME).finish());
4465db71995Sopenharmony_ci    std::vector<const char*> functions = {"vkCreatePrivateDataSlot", "vkGetDeviceBufferMemoryRequirements", "vkCmdWaitEvents2",
4475db71995Sopenharmony_ci                                          "vkGetDeviceImageSparseMemoryRequirements"};
4485db71995Sopenharmony_ci
4495db71995Sopenharmony_ci    for (const auto& f : functions) {
4505db71995Sopenharmony_ci        driver.physical_devices.back().add_device_function(VulkanFunction{f, [] {}});
4515db71995Sopenharmony_ci    }
4525db71995Sopenharmony_ci    {  // doesn't enable the feature or extension
4535db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
4545db71995Sopenharmony_ci        inst.create_info.set_api_version(1, 2, 0);
4555db71995Sopenharmony_ci        inst.CheckCreate();
4565db71995Sopenharmony_ci
4575db71995Sopenharmony_ci        DeviceWrapper dev{inst};
4585db71995Sopenharmony_ci        dev.CheckCreate(inst.GetPhysDev());
4595db71995Sopenharmony_ci
4605db71995Sopenharmony_ci        for (const auto& f : functions) {
4615db71995Sopenharmony_ci            ASSERT_NE(nullptr, dev->vkGetDeviceProcAddr(dev.dev, f));
4625db71995Sopenharmony_ci        }
4635db71995Sopenharmony_ci    }
4645db71995Sopenharmony_ci    {  // doesn't enable the feature
4655db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
4665db71995Sopenharmony_ci        inst.create_info.set_api_version(1, 2, 0);
4675db71995Sopenharmony_ci        inst.CheckCreate();
4685db71995Sopenharmony_ci
4695db71995Sopenharmony_ci        DeviceWrapper dev{inst};
4705db71995Sopenharmony_ci        dev.create_info.add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);
4715db71995Sopenharmony_ci        dev.CheckCreate(inst.GetPhysDev());
4725db71995Sopenharmony_ci
4735db71995Sopenharmony_ci        for (const auto& f : functions) {
4745db71995Sopenharmony_ci            ASSERT_NE(nullptr, dev->vkGetDeviceProcAddr(dev.dev, f));
4755db71995Sopenharmony_ci        }
4765db71995Sopenharmony_ci    }
4775db71995Sopenharmony_ci    {  // enables the feature and extension
4785db71995Sopenharmony_ci        InstWrapper inst{env.vulkan_functions};
4795db71995Sopenharmony_ci        inst.create_info.set_api_version(1, 2, 0);
4805db71995Sopenharmony_ci        inst.CheckCreate();
4815db71995Sopenharmony_ci
4825db71995Sopenharmony_ci        VkPhysicalDeviceMaintenance5FeaturesKHR features{};
4835db71995Sopenharmony_ci        features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES_KHR;
4845db71995Sopenharmony_ci        features.maintenance5 = VK_TRUE;
4855db71995Sopenharmony_ci
4865db71995Sopenharmony_ci        DeviceWrapper dev{inst};
4875db71995Sopenharmony_ci        dev.create_info.add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);
4885db71995Sopenharmony_ci        dev.create_info.dev.pNext = &features;
4895db71995Sopenharmony_ci        dev.CheckCreate(inst.GetPhysDev());
4905db71995Sopenharmony_ci
4915db71995Sopenharmony_ci        for (const auto& f : functions) {
4925db71995Sopenharmony_ci            ASSERT_EQ(nullptr, dev->vkGetDeviceProcAddr(dev.dev, f));
4935db71995Sopenharmony_ci        }
4945db71995Sopenharmony_ci    }
4955db71995Sopenharmony_ci}
496