Suppress all build warnings
[platform/upstream/Vulkan-LoaderAndValidationLayers.git] / tests / test_environment.cpp
1 /*
2  * Copyright (c) 2015-2016 The Khronos Group Inc.
3  * Copyright (c) 2015-2016 Valve Corporation
4  * Copyright (c) 2015-2016 LunarG, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Chia-I Wu <olvaffe@gmail.com>
19  * Author: Chris Forbes <chrisf@ijw.co.nz>
20  * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
21  * Author: Mark Lobodzinski <mark@lunarg.com>
22  * Author: Mike Stroyan <mike@LunarG.com>
23  * Author: Tobin Ehlis <tobine@google.com>
24  * Author: Tony Barbour <tony@LunarG.com>
25  */
26
27 #include "test_common.h"
28 #include "vktestbinding.h"
29 #include "test_environment.h"
30
31 #if defined(NDEBUG) && defined(__GNUC__)
32 #define U_ASSERT_ONLY __attribute__((unused))
33 #else
34 #define U_ASSERT_ONLY
35 #endif
36
37 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
38
39 namespace vk_testing {
40
41 Environment::Environment() : default_dev_(0) {
42     app_.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
43     app_.pApplicationName = "vk_testing";
44     app_.applicationVersion = 1;
45     app_.pEngineName = "vk_testing";
46     app_.engineVersion = 1;
47     app_.apiVersion = VK_API_VERSION_1_0;
48     app_.pNext = NULL;
49 }
50
51 bool Environment::parse_args(int argc, char **argv) {
52     int i;
53
54     for (i = 1; i < argc; i++) {
55 #define ARG(name) (strcmp(argv[i], name) == 0)
56 #define ARG_P(name) (i < argc - 1 && ARG(name))
57         if (ARG_P("--gpu")) {
58             default_dev_ = atoi(argv[++i]);
59         } else {
60             break;
61         }
62 #undef ARG
63 #undef ARG_P
64     }
65
66     if (i < argc) {
67         std::cout << "invalid argument: " << argv[i] << "\n\n"
68                   << "Usage: " << argv[0] << " <options>\n\n"
69                   << "Options:\n"
70                      "  --gpu <n>  Use GPU<n> as the default GPU\n";
71
72         return false;
73     }
74
75     return true;
76 }
77
78 void Environment::SetUp() {
79
80     uint32_t count;
81     VkResult U_ASSERT_ONLY err;
82     VkInstanceCreateInfo inst_info = {};
83     std::vector<VkExtensionProperties> instance_extensions;
84     std::vector<VkExtensionProperties> device_extensions;
85
86     std::vector<const char *> instance_extension_names;
87     std::vector<const char *> device_extension_names;
88
89     instance_extension_names.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
90     device_extension_names.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
91 #ifdef _WIN32
92     instance_extension_names.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
93 #endif
94 #ifdef VK_USE_PLATFORM_XCB_KHR
95     instance_extension_names.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
96 #endif
97
98     VkBool32 extFound;
99
100     instance_extensions = vk_testing::GetGlobalExtensions();
101
102     for (uint32_t i = 0; i < instance_extension_names.size(); i++) {
103         extFound = 0;
104         for (uint32_t j = 0; j < instance_extensions.size(); j++) {
105             if (!strcmp(instance_extension_names[i], instance_extensions[j].extensionName)) {
106                 extFound = 1;
107             }
108         }
109         ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << instance_extension_names[i]
110                                << " which is necessary to pass this test";
111     }
112     inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
113     inst_info.pNext = NULL;
114     inst_info.pApplicationInfo = &app_;
115     inst_info.enabledExtensionCount = instance_extension_names.size();
116     inst_info.ppEnabledExtensionNames = (instance_extension_names.size()) ? &instance_extension_names[0] : NULL;
117     inst_info.enabledLayerCount = 0;
118     inst_info.ppEnabledLayerNames = NULL;
119     err = vkCreateInstance(&inst_info, NULL, &inst);
120     ASSERT_EQ(VK_SUCCESS, err);
121     err = vkEnumeratePhysicalDevices(inst, &count, NULL);
122     ASSERT_EQ(VK_SUCCESS, err);
123     ASSERT_LE(count, ARRAY_SIZE(gpus));
124     err = vkEnumeratePhysicalDevices(inst, &count, gpus);
125     ASSERT_EQ(VK_SUCCESS, err);
126     ASSERT_GT(count, default_dev_);
127
128     vk_testing::PhysicalDevice phys_dev(gpus[0]);
129     device_extensions = phys_dev.extensions();
130
131     for (uint32_t i = 0; i < device_extension_names.size(); i++) {
132         extFound = 0;
133         for (uint32_t j = 0; j < device_extensions.size(); j++) {
134             if (!strcmp(device_extension_names[i], device_extensions[j].extensionName)) {
135                 extFound = 1;
136             }
137         }
138         ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << device_extension_names[i]
139                                << " which is necessary to pass this test";
140     }
141
142     devs_.reserve(count);
143     for (uint32_t i = 0; i < count; i++) {
144         devs_.push_back(new Device(gpus[i]));
145         if (i == default_dev_) {
146             devs_[i]->init(device_extension_names);
147             ASSERT_NE(true, devs_[i]->graphics_queues().empty());
148         }
149     }
150 }
151
152 void Environment::TearDown() {
153     // destroy devices first
154     for (std::vector<Device *>::iterator it = devs_.begin(); it != devs_.end(); it++)
155         delete *it;
156     devs_.clear();
157
158     if (inst)
159         vkDestroyInstance(inst, NULL);
160 }
161 } // vk_testing namespace