src/OrbitVulkanLayer/VulkanLayerControllerTest.cpp
TEST_F(VulkanLayerControllerTest,
       WillCreateDispatchTableOnCreateDeviceWithGameAlreadyEnablingRequiredExtensions) {
  const MockDispatchTable* dispatch_table = controller_.dispatch_table();
  EXPECT_CALL(*dispatch_table, CreateDeviceDispatchTable).Times(1);
  const MockDeviceManager* device_manager = controller_.device_manager();
  EXPECT_CALL(*device_manager, TrackLogicalDevice).Times(1);
  const MockTimerQueryPool* timer_query_pool = controller_.timer_query_pool();
  EXPECT_CALL(*timer_query_pool, InitializeTimerQueryPool).Times(1);

  static constexpr PFN_vkCreateDevice kMockDriverCreateDevice =
      +[](VkPhysicalDevice /*physical_device*/, const VkDeviceCreateInfo* create_info,
          const VkAllocationCallbacks* /*allocator*/, VkDevice* /*instance*/) {
        bool requested_host_extension = false;
        for (uint32_t i = 0; i < create_info->enabledExtensionCount; ++i) {
          if (strcmp(create_info->ppEnabledExtensionNames[i],
                     VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME) == 0) {
            requested_host_extension = true;
            break;
          }
        }
        EXPECT_TRUE(requested_host_extension);
        return VK_SUCCESS;
      };
  static constexpr PFN_vkEnumerateDeviceExtensionProperties
      kFakeEnumerateDeviceExtensionProperties =
          +[](VkPhysicalDevice /*physical_device*/, const char* /*layer_name*/,
              uint32_t* property_count, VkExtensionProperties* properties) -> VkResult {
    CHECK(property_count != nullptr);
    if (properties == nullptr) {
      *property_count = 1;
      return VK_SUCCESS;
    }
    VkExtensionProperties p{VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME,
                            VK_EXT_HOST_QUERY_RESET_SPEC_VERSION};
    *properties = p;
    return VK_SUCCESS;
  };

  PFN_vkGetDeviceProcAddr fake_get_device_proc_addr =
      +[](VkDevice /*device*/, const char * /*name*/) -> PFN_vkVoidFunction { return nullptr; };

  PFN_vkGetInstanceProcAddr fake_get_instance_proc_addr =
      +[](VkInstance /*instance*/, const char* name) -> PFN_vkVoidFunction {
    if (strcmp(name, "vkCreateDevice") == 0) {
      return absl::bit_cast<PFN_vkVoidFunction>(kMockDriverCreateDevice);
    }
    if (strcmp(name, "vkEnumerateDeviceExtensionProperties") == 0) {
      return absl::bit_cast<PFN_vkVoidFunction>(kFakeEnumerateDeviceExtensionProperties);
    }
    return nullptr;
  };

  VkLayerDeviceLink layer_link_1 = {.pfnNextGetInstanceProcAddr = fake_get_instance_proc_addr,
                                    .pfnNextGetDeviceProcAddr = fake_get_device_proc_addr};
  VkLayerDeviceCreateInfo layer_create_info{.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO,
                                            .function = VK_LAYER_LINK_INFO};
  layer_create_info.u.pLayerInfo = &layer_link_1;
  std::string requested_extension_name = VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME;
  std::vector<const char*> requested_extensions{};
  requested_extensions.push_back(requested_extension_name.c_str());
  VkDeviceCreateInfo create_info{.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
                                 .pNext = &layer_create_info,
                                 .enabledExtensionCount = 1,
                                 .ppEnabledExtensionNames = requested_extensions.data()};
  VkDevice created_device;
  VkPhysicalDevice physical_device = {};
  VkResult result =
      controller_.OnCreateDevice(physical_device, &create_info, nullptr, &created_device);
  EXPECT_EQ(result, VK_SUCCESS);
}
// This will test the good case of a call to CreateDevice. Similar to the test case above, but
// this time the game already requested the required extensions. We still ensure that those are
// requested in the next layer's vkCreateDevice.
This will test the good case of a call to CreateDevice. Similar to the test case above, but
this time the game already requested the required extensions. We still ensure that those are
requested in the next layer's vkCreateDevice.
This will test the good case of a call to CreateDevice . Similar to the test case above but this time the game already requested the required extensions . We still ensure that those are requested in the next layer ' s vkCreateDevice .
TEST_F(VulkanLayerControllerTest,
       WillCreateDispatchTableOnCreateDeviceWithGameAlreadyEnablingRequiredExtensions) {
  const MockDispatchTable* dispatch_table = controller_.dispatch_table();
  EXPECT_CALL(*dispatch_table, CreateDeviceDispatchTable).Times(1);
  const MockDeviceManager* device_manager = controller_.device_manager();
  EXPECT_CALL(*device_manager, TrackLogicalDevice).Times(1);
  const MockTimerQueryPool* timer_query_pool = controller_.timer_query_pool();
  EXPECT_CALL(*timer_query_pool, InitializeTimerQueryPool).Times(1);
  static constexpr PFN_vkCreateDevice kMockDriverCreateDevice =
      +[](VkPhysicalDevice , const VkDeviceCreateInfo* create_info,
          const VkAllocationCallbacks* , VkDevice* ) {
        bool requested_host_extension = false;
        for (uint32_t i = 0; i < create_info->enabledExtensionCount; ++i) {
          if (strcmp(create_info->ppEnabledExtensionNames[i],
                     VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME) == 0) {
            requested_host_extension = true;
            break;
          }
        }
        EXPECT_TRUE(requested_host_extension);
        return VK_SUCCESS;
      };
  static constexpr PFN_vkEnumerateDeviceExtensionProperties
      kFakeEnumerateDeviceExtensionProperties =
          +[](VkPhysicalDevice , const char* ,
              uint32_t* property_count, VkExtensionProperties* properties) -> VkResult {
    CHECK(property_count != nullptr);
    if (properties == nullptr) {
      *property_count = 1;
      return VK_SUCCESS;
    }
    VkExtensionProperties p{VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME,
                            VK_EXT_HOST_QUERY_RESET_SPEC_VERSION};
    *properties = p;
    return VK_SUCCESS;
  };
  PFN_vkGetDeviceProcAddr fake_get_device_proc_addr =
      +[](VkDevice , const char * ) -> PFN_vkVoidFunction { return nullptr; };
  PFN_vkGetInstanceProcAddr fake_get_instance_proc_addr =
      +[](VkInstance , const char* name) -> PFN_vkVoidFunction {
    if (strcmp(name, "vkCreateDevice") == 0) {
      return absl::bit_cast<PFN_vkVoidFunction>(kMockDriverCreateDevice);
    }
    if (strcmp(name, "vkEnumerateDeviceExtensionProperties") == 0) {
      return absl::bit_cast<PFN_vkVoidFunction>(kFakeEnumerateDeviceExtensionProperties);
    }
    return nullptr;
  };
  VkLayerDeviceLink layer_link_1 = {.pfnNextGetInstanceProcAddr = fake_get_instance_proc_addr,
                                    .pfnNextGetDeviceProcAddr = fake_get_device_proc_addr};
  VkLayerDeviceCreateInfo layer_create_info{.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO,
                                            .function = VK_LAYER_LINK_INFO};
  layer_create_info.u.pLayerInfo = &layer_link_1;
  std::string requested_extension_name = VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME;
  std::vector<const char*> requested_extensions{};
  requested_extensions.push_back(requested_extension_name.c_str());
  VkDeviceCreateInfo create_info{.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
                                 .pNext = &layer_create_info,
                                 .enabledExtensionCount = 1,
                                 .ppEnabledExtensionNames = requested_extensions.data()};
  VkDevice created_device;
  VkPhysicalDevice physical_device = {};
  VkResult result =
      controller_.OnCreateDevice(physical_device, &create_info, nullptr, &created_device);
  EXPECT_EQ(result, VK_SUCCESS);
}
TEST_F ( VulkanLayerControllerTest , WillCreateDispatchTableOnCreateDeviceWithGameAlreadyEnablingRequiredExtensions ) { const MockDispatchTable * dispatch_table = controller_ . dispatch_table ( ) ; EXPECT_CALL ( * dispatch_table , CreateDeviceDispatchTable ) . Times ( 1 ) ; const MockDeviceManager * device_manager = controller_ . device_manager ( ) ; EXPECT_CALL ( * device_manager , TrackLogicalDevice ) . Times ( 1 ) ; const MockTimerQueryPool * timer_query_pool = controller_ . timer_query_pool ( ) ; EXPECT_CALL ( * timer_query_pool , InitializeTimerQueryPool ) . Times ( 1 ) ; static constexpr PFN_vkCreateDevice kMockDriverCreateDevice = + [ ] ( VkPhysicalDevice , const VkDeviceCreateInfo * create_info , const VkAllocationCallbacks * , VkDevice * ) { bool requested_host_extension = false ; for ( uint32_t i = 0 ; i < create_info -> enabledExtensionCount ; ++ i ) { if ( strcmp ( create_info -> ppEnabledExtensionNames [ i ] , VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME ) == 0 ) { requested_host_extension = true ; break ; } } EXPECT_TRUE ( requested_host_extension ) ; return VK_SUCCESS ; } ; static constexpr PFN_vkEnumerateDeviceExtensionProperties kFakeEnumerateDeviceExtensionProperties = + [ ] ( VkPhysicalDevice , const char * , uint32_t * property_count , VkExtensionProperties * properties ) -> VkResult { CHECK ( property_count != nullptr ) ; if ( properties == nullptr ) { * property_count = 1 ; return VK_SUCCESS ; } VkExtensionProperties p { VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME , VK_EXT_HOST_QUERY_RESET_SPEC_VERSION } ; * properties = p ; return VK_SUCCESS ; } ; PFN_vkGetDeviceProcAddr fake_get_device_proc_addr = + [ ] ( VkDevice , const char * ) -> PFN_vkVoidFunction { return nullptr ; } ; PFN_vkGetInstanceProcAddr fake_get_instance_proc_addr = + [ ] ( VkInstance , const char * name ) -> PFN_vkVoidFunction { if ( strcmp ( name , " " ) == 0 ) { return absl :: bit_cast < PFN_vkVoidFunction > ( kMockDriverCreateDevice ) ; } if ( strcmp ( name , " " ) == 0 ) { return absl :: bit_cast < PFN_vkVoidFunction > ( kFakeEnumerateDeviceExtensionProperties ) ; } return nullptr ; } ; VkLayerDeviceLink layer_link_1 = { . pfnNextGetInstanceProcAddr = fake_get_instance_proc_addr , . pfnNextGetDeviceProcAddr = fake_get_device_proc_addr } ; VkLayerDeviceCreateInfo layer_create_info { . sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO , . function = VK_LAYER_LINK_INFO } ; layer_create_info . u . pLayerInfo = & layer_link_1 ; std :: string requested_extension_name = VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME ; std :: vector < const char * > requested_extensions { } ; requested_extensions . push_back ( requested_extension_name . c_str ( ) ) ; VkDeviceCreateInfo create_info { . sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO , . pNext = & layer_create_info , . enabledExtensionCount = 1 , . ppEnabledExtensionNames = requested_extensions . data ( ) } ; VkDevice created_device ; VkPhysicalDevice physical_device = { } ; VkResult result = controller_ . OnCreateDevice ( physical_device , & create_info , nullptr , & created_device ) ; EXPECT_EQ ( result , VK_SUCCESS ) ; }
This will test the good case of a call to CreateDevice.
This will test the good case of a call to CreateDevice .
/*physical_device*/ /*allocator*/ /*instance*/ /*physical_device*/ /*layer_name*/ /*device*/ /*name*/ /*instance*/