blob: b0417965001dd4d3f6e3ce32ec9c34d6b4366ab1 [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 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
15 *
Karl Schultz6addd812016-02-02 17:17:23 -070016 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
24 *
25 * Author: Chia-I Wu <olvaffe@gmail.com>
26 * Author: Chris Forbes <chrisf@ijw.co.nz>
27 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
28 * Author: Mark Lobodzinski <mark@lunarg.com>
29 * Author: Mike Stroyan <mike@LunarG.com>
30 * Author: Tobin Ehlis <tobine@google.com>
31 * Author: Tony Barbour <tony@LunarG.com>
32 */
33
Tony Barbour92400bb2015-03-02 16:38:52 -070034#include "test_common.h"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060035#include "vktestbinding.h"
Tony Barbour92400bb2015-03-02 16:38:52 -070036#include "test_environment.h"
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060037
38#if defined(NDEBUG) && defined(__GNUC__)
39#define U_ASSERT_ONLY __attribute__((unused))
40#else
41#define U_ASSERT_ONLY
42#endif
Tony Barbour92400bb2015-03-02 16:38:52 -070043
44#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
45
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060046namespace vk_testing {
Tony Barbour92400bb2015-03-02 16:38:52 -070047
Karl Schultz6addd812016-02-02 17:17:23 -070048Environment::Environment() : default_dev_(0) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060049 app_.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Chia-I Wu3432a0c2015-10-27 18:04:07 +080050 app_.pApplicationName = "vk_testing";
51 app_.applicationVersion = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060052 app_.pEngineName = "vk_testing";
Tony Barbour92400bb2015-03-02 16:38:52 -070053 app_.engineVersion = 1;
Jon Ashburnc5012ff2016-03-22 13:57:46 -060054 app_.apiVersion = VK_API_VERSION_1_0;
Courtney Goeltzenleuchteraca235e2015-06-23 08:54:19 -060055 app_.pNext = NULL;
Tony Barbour92400bb2015-03-02 16:38:52 -070056}
57
Karl Schultz6addd812016-02-02 17:17:23 -070058bool Environment::parse_args(int argc, char **argv) {
Tony Barbour92400bb2015-03-02 16:38:52 -070059 int i;
60
61 for (i = 1; i < argc; i++) {
62#define ARG(name) (strcmp(argv[i], name) == 0)
63#define ARG_P(name) (i < argc - 1 && ARG(name))
64 if (ARG_P("--gpu")) {
65 default_dev_ = atoi(argv[++i]);
66 } else {
67 break;
68 }
69#undef ARG
70#undef ARG_P
71 }
72
73 if (i < argc) {
Karl Schultz6addd812016-02-02 17:17:23 -070074 std::cout << "invalid argument: " << argv[i] << "\n\n"
75 << "Usage: " << argv[0] << " <options>\n\n"
76 << "Options:\n"
77 " --gpu <n> Use GPU<n> as the default GPU\n";
Tony Barbour92400bb2015-03-02 16:38:52 -070078
79 return false;
80 }
81
82 return true;
83}
84
Karl Schultz6addd812016-02-02 17:17:23 -070085void Environment::SetUp() {
Tony Barbour92400bb2015-03-02 16:38:52 -070086
87 uint32_t count;
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060088 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060089 VkInstanceCreateInfo inst_info = {};
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060090 std::vector<VkExtensionProperties> instance_extensions;
91 std::vector<VkExtensionProperties> device_extensions;
92
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -060093 std::vector<const char *> instance_extension_names;
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060094 std::vector<const char *> device_extension_names;
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -060095 std::vector<const char *> device_layer_names;
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -060096
Ian Elliottc11750d2015-10-30 13:24:12 -060097 instance_extension_names.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
98 device_extension_names.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
Tony Barbourfc7a0a62016-04-05 11:42:41 -060099#ifdef _WIN32
100 instance_extension_names.push_back(
101 VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
102#endif
103#ifdef VK_USE_PLATFORM_XCB_KHR
104 instance_extension_names.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
105#endif
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600106
Courtney Goeltzenleuchtercd2a0992015-07-09 11:44:38 -0600107 VkBool32 extFound;
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600108
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600109 instance_extensions = vk_testing::GetGlobalExtensions();
110
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600111 for (uint32_t i = 0; i < instance_extension_names.size(); i++) {
112 extFound = 0;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600113 for (uint32_t j = 0; j < instance_extensions.size(); j++) {
Karl Schultz6addd812016-02-02 17:17:23 -0700114 if (!strcmp(instance_extension_names[i],
115 instance_extensions[j].extensionName)) {
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600116 extFound = 1;
117 }
118 }
Karl Schultz6addd812016-02-02 17:17:23 -0700119 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named "
120 << instance_extension_names[i]
121 << " which is necessary to pass this test";
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600122 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600123 inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600124 inst_info.pNext = NULL;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800125 inst_info.pApplicationInfo = &app_;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700126 inst_info.enabledExtensionCount = instance_extension_names.size();
Karl Schultz6addd812016-02-02 17:17:23 -0700127 inst_info.ppEnabledExtensionNames =
128 (instance_extension_names.size()) ? &instance_extension_names[0] : NULL;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700129 inst_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600130 inst_info.ppEnabledLayerNames = NULL;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800131 err = vkCreateInstance(&inst_info, NULL, &inst);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600132 ASSERT_EQ(VK_SUCCESS, err);
Jon Ashburn83a64252015-04-15 11:31:12 -0600133 err = vkEnumeratePhysicalDevices(inst, &count, NULL);
134 ASSERT_EQ(VK_SUCCESS, err);
135 ASSERT_LE(count, ARRAY_SIZE(gpus));
136 err = vkEnumeratePhysicalDevices(inst, &count, gpus);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600137 ASSERT_EQ(VK_SUCCESS, err);
Tony Barbour92400bb2015-03-02 16:38:52 -0700138 ASSERT_GT(count, default_dev_);
139
Chia-I Wu999f0482015-07-03 10:32:05 +0800140 vk_testing::PhysicalDevice phys_dev(gpus[0]);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600141 device_extensions = phys_dev.extensions();
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600142
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600143 for (uint32_t i = 0; i < device_extension_names.size(); i++) {
144 extFound = 0;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600145 for (uint32_t j = 0; j < device_extensions.size(); j++) {
Karl Schultz6addd812016-02-02 17:17:23 -0700146 if (!strcmp(device_extension_names[i],
147 device_extensions[j].extensionName)) {
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600148 extFound = 1;
149 }
150 }
Karl Schultz6addd812016-02-02 17:17:23 -0700151 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named "
152 << device_extension_names[i]
153 << " which is necessary to pass this test";
Courtney Goeltzenleuchter553b9cb2015-06-24 15:03:26 -0600154 }
155
Tony Barbour92400bb2015-03-02 16:38:52 -0700156 devs_.reserve(count);
157 for (uint32_t i = 0; i < count; i++) {
158 devs_.push_back(new Device(gpus[i]));
159 if (i == default_dev_) {
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600160 devs_[i]->init(device_layer_names, device_extension_names);
Tony Barbour92400bb2015-03-02 16:38:52 -0700161 ASSERT_NE(true, devs_[i]->graphics_queues().empty());
162 }
163 }
164}
165
Karl Schultz6addd812016-02-02 17:17:23 -0700166void Environment::TearDown() {
Tony Barbour92400bb2015-03-02 16:38:52 -0700167 // destroy devices first
Karl Schultz6addd812016-02-02 17:17:23 -0700168 for (std::vector<Device *>::iterator it = devs_.begin(); it != devs_.end();
169 it++)
Tony Barbour92400bb2015-03-02 16:38:52 -0700170 delete *it;
171 devs_.clear();
172
Courtney Goeltzenleuchter607a0972015-07-06 15:45:16 -0600173 if (inst)
Chia-I Wuf7458c52015-10-26 21:10:41 +0800174 vkDestroyInstance(inst, NULL);
Tony Barbour92400bb2015-03-02 16:38:52 -0700175}
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600176} // vk_testing namespace