blob: 6a988d00ec7c7fee913f52870b11651394546ff5 [file] [log] [blame]
Karl Schultz6addd812016-02-02 17:17:23 -07001/*
2 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
5 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06006 * 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
Karl Schultz6addd812016-02-02 17:17:23 -07009 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Karl Schultz6addd812016-02-02 17:17:23 -070011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * 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.
Karl Schultz6addd812016-02-02 17:17:23 -070017 *
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
Tony Barbour92400bb2015-03-02 16:38:52 -070027#include "test_common.h"
Tony Barbour92400bb2015-03-02 16:38:52 -070028#include "test_environment.h"
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060029
30#if defined(NDEBUG) && defined(__GNUC__)
31#define U_ASSERT_ONLY __attribute__((unused))
32#else
33#define U_ASSERT_ONLY
34#endif
Tony Barbour92400bb2015-03-02 16:38:52 -070035
36#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
37
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060038namespace vk_testing {
Tony Barbour92400bb2015-03-02 16:38:52 -070039
Karl Schultz6addd812016-02-02 17:17:23 -070040Environment::Environment() : default_dev_(0) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060041 app_.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Chia-I Wu3432a0c2015-10-27 18:04:07 +080042 app_.pApplicationName = "vk_testing";
43 app_.applicationVersion = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060044 app_.pEngineName = "vk_testing";
Tony Barbour92400bb2015-03-02 16:38:52 -070045 app_.engineVersion = 1;
Jon Ashburnc5012ff2016-03-22 13:57:46 -060046 app_.apiVersion = VK_API_VERSION_1_0;
Courtney Goeltzenleuchteraca235e2015-06-23 08:54:19 -060047 app_.pNext = NULL;
Tony Barbour92400bb2015-03-02 16:38:52 -070048}
49
Karl Schultz6addd812016-02-02 17:17:23 -070050bool Environment::parse_args(int argc, char **argv) {
Tony Barbour92400bb2015-03-02 16:38:52 -070051 int i;
52
53 for (i = 1; i < argc; i++) {
54#define ARG(name) (strcmp(argv[i], name) == 0)
55#define ARG_P(name) (i < argc - 1 && ARG(name))
56 if (ARG_P("--gpu")) {
57 default_dev_ = atoi(argv[++i]);
58 } else {
59 break;
60 }
61#undef ARG
62#undef ARG_P
63 }
64
65 if (i < argc) {
Karl Schultz6addd812016-02-02 17:17:23 -070066 std::cout << "invalid argument: " << argv[i] << "\n\n"
67 << "Usage: " << argv[0] << " <options>\n\n"
68 << "Options:\n"
69 " --gpu <n> Use GPU<n> as the default GPU\n";
Tony Barbour92400bb2015-03-02 16:38:52 -070070
71 return false;
72 }
73
74 return true;
75}
76
Karl Schultz6addd812016-02-02 17:17:23 -070077void Environment::SetUp() {
Tony Barbour92400bb2015-03-02 16:38:52 -070078 uint32_t count;
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060079 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060080 VkInstanceCreateInfo inst_info = {};
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060081 std::vector<VkExtensionProperties> instance_extensions;
82 std::vector<VkExtensionProperties> device_extensions;
83
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -060084 std::vector<const char *> instance_extension_names;
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060085 std::vector<const char *> device_extension_names;
86
Ian Elliottc11750d2015-10-30 13:24:12 -060087 instance_extension_names.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
88 device_extension_names.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
Tony Barbourfc7a0a62016-04-05 11:42:41 -060089#ifdef _WIN32
Mark Lobodzinski0a4c1702016-09-07 16:33:17 -060090 instance_extension_names.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
Tony Barbourfc7a0a62016-04-05 11:42:41 -060091#endif
92#ifdef VK_USE_PLATFORM_XCB_KHR
93 instance_extension_names.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
94#endif
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060095
Courtney Goeltzenleuchtercd2a0992015-07-09 11:44:38 -060096 VkBool32 extFound;
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060097
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -060098 instance_extensions = vk_testing::GetGlobalExtensions();
99
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600100 for (uint32_t i = 0; i < instance_extension_names.size(); i++) {
101 extFound = 0;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600102 for (uint32_t j = 0; j < instance_extensions.size(); j++) {
Mark Lobodzinski0a4c1702016-09-07 16:33:17 -0600103 if (!strcmp(instance_extension_names[i], instance_extensions[j].extensionName)) {
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600104 extFound = 1;
105 }
106 }
Mark Lobodzinski0a4c1702016-09-07 16:33:17 -0600107 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << instance_extension_names[i]
Karl Schultz6addd812016-02-02 17:17:23 -0700108 << " which is necessary to pass this test";
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600109 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600110 inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600111 inst_info.pNext = NULL;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800112 inst_info.pApplicationInfo = &app_;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700113 inst_info.enabledExtensionCount = instance_extension_names.size();
Mark Lobodzinski0a4c1702016-09-07 16:33:17 -0600114 inst_info.ppEnabledExtensionNames = (instance_extension_names.size()) ? &instance_extension_names[0] : NULL;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700115 inst_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600116 inst_info.ppEnabledLayerNames = NULL;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800117 err = vkCreateInstance(&inst_info, NULL, &inst);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600118 ASSERT_EQ(VK_SUCCESS, err);
Jon Ashburn83a64252015-04-15 11:31:12 -0600119 err = vkEnumeratePhysicalDevices(inst, &count, NULL);
120 ASSERT_EQ(VK_SUCCESS, err);
121 ASSERT_LE(count, ARRAY_SIZE(gpus));
122 err = vkEnumeratePhysicalDevices(inst, &count, gpus);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600123 ASSERT_EQ(VK_SUCCESS, err);
Tony Barbour92400bb2015-03-02 16:38:52 -0700124 ASSERT_GT(count, default_dev_);
125
Chia-I Wu999f0482015-07-03 10:32:05 +0800126 vk_testing::PhysicalDevice phys_dev(gpus[0]);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600127 device_extensions = phys_dev.extensions();
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600128
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600129 for (uint32_t i = 0; i < device_extension_names.size(); i++) {
130 extFound = 0;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600131 for (uint32_t j = 0; j < device_extensions.size(); j++) {
Mark Lobodzinski0a4c1702016-09-07 16:33:17 -0600132 if (!strcmp(device_extension_names[i], device_extensions[j].extensionName)) {
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600133 extFound = 1;
134 }
135 }
Mark Lobodzinski0a4c1702016-09-07 16:33:17 -0600136 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << device_extension_names[i]
Karl Schultz6addd812016-02-02 17:17:23 -0700137 << " which is necessary to pass this test";
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600138 }
139
Tony Barbour92400bb2015-03-02 16:38:52 -0700140 devs_.reserve(count);
141 for (uint32_t i = 0; i < count; i++) {
142 devs_.push_back(new Device(gpus[i]));
143 if (i == default_dev_) {
Tony Barbour4c70d102016-08-08 16:06:56 -0600144 devs_[i]->init(device_extension_names);
Tony Barbour92400bb2015-03-02 16:38:52 -0700145 ASSERT_NE(true, devs_[i]->graphics_queues().empty());
146 }
147 }
148}
149
Karl Schultz6addd812016-02-02 17:17:23 -0700150void Environment::TearDown() {
Tony Barbour92400bb2015-03-02 16:38:52 -0700151 // destroy devices first
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700152 for (std::vector<Device *>::iterator it = devs_.begin(); it != devs_.end(); it++) delete *it;
Tony Barbour92400bb2015-03-02 16:38:52 -0700153 devs_.clear();
154
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700155 if (inst) vkDestroyInstance(inst, NULL);
Tony Barbour92400bb2015-03-02 16:38:52 -0700156}
Dave Houlton6c72f352018-02-06 17:49:16 -0700157} // namespace vk_testing