misc: Change vkEnumerateGpus to vkEnumeratePhysicalDevices

Conflicts:
	loader/loader.h
diff --git a/include/vkLayer.h b/include/vkLayer.h
index 53460c7..e9e8b03 100644
--- a/include/vkLayer.h
+++ b/include/vkLayer.h
@@ -30,7 +30,7 @@
     PFN_vkGetProcAddr GetProcAddr;
     PFN_vkCreateInstance CreateInstance;
     PFN_vkDestroyInstance DestroyInstance;
-    PFN_vkEnumerateGpus EnumerateGpus;
+    PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
     PFN_vkGetGpuInfo GetGpuInfo;
     PFN_vkCreateDevice CreateDevice;
     PFN_vkDestroyDevice DestroyDevice;
diff --git a/include/vulkan.h b/include/vulkan.h
index d80c15a..4e91b43 100644
--- a/include/vulkan.h
+++ b/include/vulkan.h
@@ -87,7 +87,6 @@
 VK_DEFINE_SUBCLASS_HANDLE(VkFramebuffer, VkObject)
 VK_DEFINE_SUBCLASS_HANDLE(VkRenderPass, VkObject)
 
-#define VK_MAX_PHYSICAL_GPUS       16
 #define VK_MAX_PHYSICAL_GPU_NAME   256
 #define VK_MAX_EXTENSION_NAME      256
 
@@ -2200,7 +2199,7 @@
 // API functions
 typedef VkResult (VKAPI *PFN_vkCreateInstance)(const VkInstanceCreateInfo* pCreateInfo, VkInstance* pInstance);
 typedef VkResult (VKAPI *PFN_vkDestroyInstance)(VkInstance instance);
-typedef VkResult (VKAPI *PFN_vkEnumerateGpus)(VkInstance instance, uint32_t maxGpus, uint32_t* pGpuCount, VkPhysicalGpu* pGpus);
+typedef VkResult (VKAPI *PFN_vkEnumeratePhysicalDevices)(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalGpu* pPhysicalDevices);
 typedef VkResult (VKAPI *PFN_vkGetGpuInfo)(VkPhysicalGpu gpu, VkPhysicalGpuInfoType infoType, size_t* pDataSize, void* pData);
 typedef void *   (VKAPI *PFN_vkGetProcAddr)(VkPhysicalGpu gpu, const char * pName);
 typedef VkResult (VKAPI *PFN_vkCreateDevice)(VkPhysicalGpu gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice);
@@ -2326,11 +2325,10 @@
 VkResult VKAPI vkDestroyInstance(
     VkInstance                                  instance);
 
-VkResult VKAPI vkEnumerateGpus(
+VkResult VKAPI vkEnumeratePhysicalDevices(
     VkInstance                                  instance,
-    uint32_t                                    maxGpus,
-    uint32_t*                                   pGpuCount,
-    VkPhysicalGpu*                              pGpus);
+    uint32_t*                                   pPhysicalDeviceCount,
+    VkPhysicalGpu*                              pPhysicalDevices);
 
 VkResult VKAPI vkGetGpuInfo(
     VkPhysicalGpu                               gpu,
diff --git a/layers/param_checker.cpp b/layers/param_checker.cpp
index fae24d1..5408ee7 100644
--- a/layers/param_checker.cpp
+++ b/layers/param_checker.cpp
@@ -139,13 +139,6 @@
     return result;
 }
 
-VK_LAYER_EXPORT VkResult VKAPI vkEnumerateGpus(VkInstance instance, uint32_t maxGpus, uint32_t* pGpuCount, VkPhysicalGpu* pGpus)
-{
-
-    VkResult result = nextTable.EnumerateGpus(instance, maxGpus, pGpuCount, pGpus);
-    return result;
-}
-
 VK_LAYER_EXPORT VkResult VKAPI vkGetGpuInfo(VkPhysicalGpu gpu, VkPhysicalGpuInfoType infoType, size_t* pDataSize, void* pData)
 {
     pCurObj = (VkBaseLayerObject *) gpu;
diff --git a/loader/loader.c b/loader/loader.c
index 20646a7..b71a39a 100644
--- a/loader/loader.c
+++ b/loader/loader.c
@@ -69,9 +69,9 @@
     const struct loader_scanned_icds *scanned_icds;
 
     VkLayerDispatchTable *loader_dispatch;
-    uint32_t layer_count[VK_MAX_PHYSICAL_GPUS];
-    struct loader_layers layer_libs[VK_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
-    VkBaseLayerObject *wrappedGpus[VK_MAX_PHYSICAL_GPUS];
+    uint32_t layer_count[MAX_GPUS_FOR_LAYER];
+    struct loader_layers layer_libs[MAX_GPUS_FOR_LAYER][MAX_LAYER_LIBRARIES];
+    VkBaseLayerObject *wrappedGpus[MAX_GPUS_FOR_LAYER];
     uint32_t gpu_count;
     VkBaseLayerObject *gpus;
 
@@ -85,7 +85,7 @@
     PFN_vkGetProcAddr GetProcAddr;
     PFN_vkCreateInstance CreateInstance;
     PFN_vkDestroyInstance DestroyInstance;
-    PFN_vkEnumerateGpus EnumerateGpus;
+    PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
     PFN_vkGetGlobalExtensionInfo GetGlobalExtensionInfo;
     VkInstance instance;
     struct loader_scanned_icds *next;
@@ -443,7 +443,7 @@
     LOOKUP(fp_gpa, GetProcAddr);
     LOOKUP(fp_create_inst, CreateInstance);
     LOOKUP(fp_destroy_inst, DestroyInstance);
-    LOOKUP(fp_enumerate, EnumerateGpus);
+    LOOKUP(fp_enumerate, EnumeratePhysicalDevices);
     LOOKUP(fp_get_global_ext_info, GetGlobalExtensionInfo);
 #undef LOOKUP
 
@@ -457,7 +457,7 @@
     new_node->GetProcAddr = fp_gpa;
     new_node->CreateInstance = fp_create_inst;
     new_node->DestroyInstance = fp_destroy_inst;
-    new_node->EnumerateGpus = fp_enumerate;
+    new_node->EnumeratePhysicalDevices = fp_enumerate;
     new_node->GetGlobalExtensionInfo = fp_get_global_ext_info;
     new_node->extension_count = 0;
     new_node->extensions = NULL;
@@ -1017,7 +1017,7 @@
     struct layer_name_pair *pLayerNames;
     if (!icd)
         return 0;
-    assert(gpu_index < VK_MAX_PHYSICAL_GPUS);
+    assert(gpu_index < MAX_GPUS_FOR_LAYER);
 
     gpu = icd->gpus + gpu_index;
     /* activate any layer libraries */
@@ -1194,73 +1194,90 @@
     return VK_SUCCESS;
 }
 
-LOADER_EXPORT VkResult VKAPI vkEnumerateGpus(
+LOADER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
 
         VkInstance                                instance,
-        uint32_t                                    maxGpus,
-        uint32_t*                                   pGpuCount,
-        VkPhysicalGpu*                           pGpus)
+        uint32_t*                                 pPhysicalDeviceCount,
+        VkPhysicalGpu*                            pPhysicalDevices)
 {
     struct loader_instance *ptr_instance = (struct loader_instance *) instance;
     struct loader_icd *icd;
-    uint32_t count = 0;
+    uint32_t n, count = 0;
     VkResult res;
 
     //in spirit of VK don't error check on the instance parameter
     icd = ptr_instance->icds;
-    while (icd) {
-        VkPhysicalGpu gpus[VK_MAX_PHYSICAL_GPUS];
-        VkBaseLayerObject * wrapped_gpus;
-        PFN_vkGetProcAddr get_proc_addr = icd->scanned_icds->GetProcAddr;
-        uint32_t n, max = maxGpus - count;
-
-        if (max > VK_MAX_PHYSICAL_GPUS) {
-            max = VK_MAX_PHYSICAL_GPUS;
+    if (pPhysicalDevices == NULL) {
+        while (icd) {
+            res = icd->scanned_icds->EnumeratePhysicalDevices(
+                                                icd->scanned_icds->instance,
+                                                &n, NULL);
+            if (res != VK_SUCCESS)
+                return res;
+            icd->gpu_count = n;
+            count += n;
+            icd = icd->next;
         }
 
-        res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance,
-                                               max, &n,
-                                               gpus);
-        if (res == VK_SUCCESS && n) {
-            wrapped_gpus = (VkBaseLayerObject*) malloc(n *
-                                        sizeof(VkBaseLayerObject));
-            icd->gpus = wrapped_gpus;
-            icd->gpu_count = n;
-            icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n *
-                                        sizeof(VkLayerDispatchTable));
-            for (unsigned int i = 0; i < n; i++) {
-                (wrapped_gpus + i)->baseObject = gpus[i];
-                (wrapped_gpus + i)->pGPA = get_proc_addr;
-                (wrapped_gpus + i)->nextObject = gpus[i];
-                memcpy(pGpus + count, gpus, sizeof(*pGpus));
-                loader_init_dispatch_table(icd->loader_dispatch + i,
-                                           get_proc_addr, gpus[i]);
+        ptr_instance->total_gpu_count = count;
 
-                /* Verify ICD compatibility */
-                if (!valid_loader_magic_value(gpus[i])) {
-                    loader_log(VK_DBG_MSG_WARNING, 0,
+    } else
+    {
+        VkPhysicalGpu* gpus;
+        if (*pPhysicalDeviceCount < ptr_instance->total_gpu_count)
+            return VK_ERROR_INVALID_VALUE;
+        gpus = malloc( sizeof(VkPhysicalGpu) *  *pPhysicalDeviceCount);
+        if (!gpus)
+            return VK_ERROR_OUT_OF_MEMORY;
+        while (icd) {
+            VkBaseLayerObject * wrapped_gpus;
+            PFN_vkGetProcAddr get_proc_addr = icd->scanned_icds->GetProcAddr;
+
+            res = icd->scanned_icds->EnumeratePhysicalDevices(
+                                            icd->scanned_icds->instance,
+                                            &n,
+                                            gpus);
+            if (res == VK_SUCCESS && n) {
+                wrapped_gpus = (VkBaseLayerObject*) malloc(n *
+                                            sizeof(VkBaseLayerObject));
+                icd->gpus = wrapped_gpus;
+                icd->gpu_count = n;
+                icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n *
+                                        sizeof(VkLayerDispatchTable));
+                for (unsigned int i = 0; i < n; i++) {
+                    (wrapped_gpus + i)->baseObject = gpus[i];
+                    (wrapped_gpus + i)->pGPA = get_proc_addr;
+                    (wrapped_gpus + i)->nextObject = gpus[i];
+                    memcpy(pPhysicalDevices + count, gpus, sizeof(*pPhysicalDevices));
+                    loader_init_dispatch_table(icd->loader_dispatch + i,
+                                               get_proc_addr, gpus[i]);
+
+                    /* Verify ICD compatibility */
+                    if (!valid_loader_magic_value(gpus[i])) {
+                        loader_log(VK_DBG_MSG_WARNING, 0,
                             "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
-                    assert(0);
+                        assert(0);
+                    }
+
+                    const VkLayerDispatchTable **disp;
+                    disp = (const VkLayerDispatchTable **) gpus[i];
+                    *disp = icd->loader_dispatch + i;
+                    loader_activate_layers(icd, i, ptr_instance->extension_count,
+                        (const char *const*) ptr_instance->extension_names);
                 }
 
-                const VkLayerDispatchTable **disp;
-                disp = (const VkLayerDispatchTable **) gpus[i];
-                *disp = icd->loader_dispatch + i;
-                loader_activate_layers(icd, i, ptr_instance->extension_count,
-                        (const char *const*) ptr_instance->extension_names);
+                count += n;
+
+                if (count >= *pPhysicalDeviceCount) {
+                    break;
+                }
             }
 
-            count += n;
-
-            if (count >= maxGpus) {
-                break;
-            }
+            icd = icd->next;
         }
-
-        icd = icd->next;
     }
 
-    *pGpuCount = count;
+    *pPhysicalDeviceCount = count;
 
     return (count > 0) ? VK_SUCCESS : res;
 }
diff --git a/loader/loader.h b/loader/loader.h
index b615989..79d77c8 100644
--- a/loader/loader.h
+++ b/loader/loader.h
@@ -68,6 +68,7 @@
 }
 
 struct loader_instance {
+    uint32_t total_gpu_count;
     struct loader_icd *icds;
     struct loader_instance *next;
     uint32_t  extension_count;
@@ -77,5 +78,6 @@
 extern uint32_t loader_activate_layers(struct loader_icd *icd, uint32_t gpu_index, uint32_t ext_count, const char *const* ext_names);
 extern struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu, uint32_t *gpu_index);
 #define MAX_LAYER_LIBRARIES 64
+#define MAX_GPUS_FOR_LAYER 16
 
 #endif /* LOADER_H */
diff --git a/tests/image_tests.cpp b/tests/image_tests.cpp
index d2b2e69..326a8c8 100644
--- a/tests/image_tests.cpp
+++ b/tests/image_tests.cpp
@@ -63,6 +63,8 @@
 #include "vktestbinding.h"
 #include "test_common.h"
 
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
 class VkImageTest : public ::testing::Test {
 public:
     void CreateImage(uint32_t w, uint32_t h);
@@ -76,7 +78,7 @@
 protected:
     vk_testing::Device *m_device;
     VkApplicationInfo app_info;
-    VkPhysicalGpu objs[VK_MAX_PHYSICAL_GPUS];
+    VkPhysicalGpu objs[16];
     uint32_t gpu_count;
     VkInstance inst;
     VkImage m_image;
@@ -102,8 +104,10 @@
         inst_info.ppEnabledExtensionNames = NULL;
         err = vkCreateInstance(&inst_info, &this->inst);
         ASSERT_VK_SUCCESS(err);
-        err = vkEnumerateGpus(this->inst, VK_MAX_PHYSICAL_GPUS,
-                               &this->gpu_count, objs);
+        err = vkEnumeratePhysicalDevices(this->inst, &this->gpu_count, NULL);
+        ASSERT_VK_SUCCESS(err);
+        ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many GPUs";
+        err = vkEnumeratePhysicalDevices(this->inst, &this->gpu_count, objs);
         ASSERT_VK_SUCCESS(err);
         ASSERT_GE(this->gpu_count, 1) << "No GPU available";
 
diff --git a/tests/init.cpp b/tests/init.cpp
index 554b9c8..de02b34 100644
--- a/tests/init.cpp
+++ b/tests/init.cpp
@@ -65,6 +65,8 @@
 #include "test_common.h"
 #include "icd-spv.h"
 
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
 class XglTest : public ::testing::Test {
 public:
     void CreateImageTest();
@@ -78,7 +80,7 @@
 protected:
     VkApplicationInfo app_info;
     VkInstance inst;
-    VkPhysicalGpu objs[VK_MAX_PHYSICAL_GPUS];
+    VkPhysicalGpu objs[16];
     uint32_t gpu_count;
 
     uint32_t m_device_id;
@@ -107,8 +109,10 @@
         inst_info.ppEnabledExtensionNames = NULL;
         err = vkCreateInstance(&inst_info, &inst);
         ASSERT_VK_SUCCESS(err);
-        err = vkEnumerateGpus(inst, VK_MAX_PHYSICAL_GPUS, &this->gpu_count,
-                               objs);
+        err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
+        ASSERT_VK_SUCCESS(err);
+        ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many GPUs";
+        err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
         ASSERT_VK_SUCCESS(err);
         ASSERT_GE(this->gpu_count, 1) << "No GPU available";
 
@@ -136,7 +140,7 @@
 TEST(Initialization, vkEnumerateGpus) {
     VkApplicationInfo app_info = {};
     VkInstance inst;
-    VkPhysicalGpu objs[VK_MAX_PHYSICAL_GPUS];
+    VkPhysicalGpu objs[16];
     uint32_t gpu_count;
     VkResult err;
     vk_testing::PhysicalGpu *gpu;
@@ -160,7 +164,10 @@
 
     err = vkCreateInstance(&inst_info, &inst);
     ASSERT_VK_SUCCESS(err);
-    err = vkEnumerateGpus(inst, VK_MAX_PHYSICAL_GPUS, &gpu_count, objs);
+    err = vkEnumeratePhysicalDevices(inst, &gpu_count, NULL);
+    ASSERT_VK_SUCCESS(err);
+    ASSERT_LE(gpu_count, ARRAY_SIZE(objs)) << "Too many GPUs";
+    err = vkEnumeratePhysicalDevices(inst, &gpu_count, objs);
     ASSERT_VK_SUCCESS(err);
     ASSERT_GE(gpu_count, 1) << "No GPU available";
 
diff --git a/tests/test_environment.cpp b/tests/test_environment.cpp
index 6d34cf7..03beb12 100644
--- a/tests/test_environment.cpp
+++ b/tests/test_environment.cpp
@@ -60,7 +60,10 @@
     inst_info.ppEnabledExtensionNames = NULL;
     err = vkCreateInstance(&inst_info, &inst);
     ASSERT_EQ(VK_SUCCESS, err);
-    err = vkEnumerateGpus(inst, ARRAY_SIZE(gpus), &count, gpus);
+    err = vkEnumeratePhysicalDevices(inst, &count, NULL);
+    ASSERT_EQ(VK_SUCCESS, err);
+    ASSERT_LE(count, ARRAY_SIZE(gpus));
+    err = vkEnumeratePhysicalDevices(inst, &count, gpus);
     ASSERT_EQ(VK_SUCCESS, err);
     ASSERT_GT(count, default_dev_);
 
@@ -92,7 +95,10 @@
 
     err = vkCreateInstance(&instInfo, &inst);
     ASSERT_EQ(VK_SUCCESS, err);
-    err = vkEnumerateGpus(inst, ARRAY_SIZE(gpus), &count, gpus);
+    err = vkEnumeratePhysicalDevices(inst, &count, NULL);
+    ASSERT_EQ(VK_SUCCESS, err);
+    ASSERT_LE(count, ARRAY_SIZE(gpus));
+    err = vkEnumeratePhysicalDevices(inst, &count, gpus);
     ASSERT_EQ(VK_SUCCESS, err);
     ASSERT_GT(count, default_dev_);
 
diff --git a/tests/test_environment.h b/tests/test_environment.h
index b2ab934..2081992 100644
--- a/tests/test_environment.h
+++ b/tests/test_environment.h
@@ -18,7 +18,7 @@
 
     const std::vector<Device *> &devices() { return devs_; }
     Device &default_device() { return *(devs_[default_dev_]); }
-    VkPhysicalGpu gpus[VK_MAX_PHYSICAL_GPUS];
+    VkPhysicalGpu gpus[16];
 
 private:
     VkApplicationInfo app_;
diff --git a/tests/vkrenderframework.cpp b/tests/vkrenderframework.cpp
index 42ed4fe..6c1de3b 100644
--- a/tests/vkrenderframework.cpp
+++ b/tests/vkrenderframework.cpp
@@ -27,6 +27,8 @@
 
 #include "vkrenderframework.h"
 
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
 VkRenderFramework::VkRenderFramework() :
     m_cmdBuffer( VK_NULL_HANDLE ),
     m_renderPass(VK_NULL_HANDLE),
@@ -68,8 +70,10 @@
     instInfo.ppEnabledExtensionNames = NULL;
     err = vkCreateInstance(&instInfo, &this->inst);
     ASSERT_VK_SUCCESS(err);
-    err = vkEnumerateGpus(inst, VK_MAX_PHYSICAL_GPUS, &this->gpu_count,
-                           objs);
+    err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
+    ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
+    ASSERT_VK_SUCCESS(err);
+    err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
     ASSERT_VK_SUCCESS(err);
     ASSERT_GE(this->gpu_count, 1) << "No GPU available";
 
diff --git a/tests/vkrenderframework.h b/tests/vkrenderframework.h
index 0c2777b..55a4769 100644
--- a/tests/vkrenderframework.h
+++ b/tests/vkrenderframework.h
@@ -100,7 +100,7 @@
 protected:
     VkApplicationInfo                    app_info;
     VkInstance                            inst;
-    VkPhysicalGpu                        objs[VK_MAX_PHYSICAL_GPUS];
+    VkPhysicalGpu                        objs[16];
     uint32_t                                gpu_count;
     VkDeviceObj                           *m_device;
     VkCmdBuffer                            m_cmdBuffer;
diff --git a/vk-generate.py b/vk-generate.py
index 120d7b4..1147620 100755
--- a/vk-generate.py
+++ b/vk-generate.py
@@ -49,7 +49,7 @@
         return out_objs and out_objs[-1] == proto.params[-1]
 
     def _is_loader_special_case(self, proto):
-        if proto.name in ["GetProcAddr", "EnumerateGpus", "EnumerateLayers", "DbgRegisterMsgCallback", "DbgUnregisterMsgCallback", "DbgSetGlobalOption", "DestroyInstance"]:
+        if proto.name in ["GetProcAddr", "EnumeratePhysicalDevices", "EnumerateLayers", "DbgRegisterMsgCallback", "DbgUnregisterMsgCallback", "DbgSetGlobalOption", "DestroyInstance"]:
             return True
         return not self.is_dispatchable_object_first_param(proto)
 
@@ -348,7 +348,7 @@
 
         # we could get the list from argv if wanted
         self.intercepted = [proto.name for proto in self.protos
-                if proto.name not in ["EnumerateGpus"]]
+                if proto.name not in ["EnumeratePhysicalDevices"]]
 
         for proto in self.protos:
             if proto.name == "GetProcAddr":
diff --git a/vulkan.py b/vulkan.py
index 4c51bd4..3e49704 100755
--- a/vulkan.py
+++ b/vulkan.py
@@ -225,11 +225,10 @@
         Proto("VkResult", "DestroyInstance",
             [Param("VkInstance", "instance")]),
 
-        Proto("VkResult", "EnumerateGpus",
+        Proto("VkResult", "EnumeratePhysicalDevices",
             [Param("VkInstance", "instance"),
-             Param("uint32_t", "maxGpus"),
-             Param("uint32_t*", "pGpuCount"),
-             Param("VkPhysicalGpu*", "pGpus")]),
+             Param("uint32_t*", "pPhysicalDeviceCount"),
+             Param("VkPhysicalGpu*", "pPhysicalDevices")]),
 
         Proto("VkResult", "GetGpuInfo",
             [Param("VkPhysicalGpu", "gpu"),