Merge vk-gl-cts/vulkan-cts-1.1.3 into vk-gl-cts/master
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / api / vktApiDeviceInitializationTests.cpp
1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 Google Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Device Initialization Tests
22  *//*--------------------------------------------------------------------*/
23
24 #include "vktApiDeviceInitializationTests.hpp"
25 #include "vktTestCaseUtil.hpp"
26
27 #include "vkDefs.hpp"
28 #include "vkPlatform.hpp"
29 #include "vkStrUtil.hpp"
30 #include "vkRef.hpp"
31 #include "vkRefUtil.hpp"
32 #include "vkQueryUtil.hpp"
33 #include "vkMemUtil.hpp"
34 #include "vkDeviceUtil.hpp"
35 #include "vkApiVersion.hpp"
36
37 #include "tcuTestLog.hpp"
38 #include "tcuResultCollector.hpp"
39 #include "tcuCommandLine.hpp"
40
41 #include "deUniquePtr.hpp"
42 #include "deStringUtil.hpp"
43
44 #include <vector>
45
46 namespace vkt
47 {
48 namespace api
49 {
50
51 namespace
52 {
53
54 using namespace vk;
55 using namespace std;
56 using std::vector;
57 using tcu::TestLog;
58
59 tcu::TestStatus createInstanceTest (Context& context)
60 {
61         tcu::TestLog&                           log                                             = context.getTestContext().getLog();
62         tcu::ResultCollector            resultCollector                 (log);
63         const char*                                     appNames[]                              = { "appName", DE_NULL, "",  "app, name", "app(\"name\"", "app~!@#$%^&*()_+name", "app\nName", "app\r\nName" };
64         const char*                                     engineNames[]                   = { "engineName", DE_NULL, "",  "engine. name", "engine\"(name)", "eng~!@#$%^&*()_+name", "engine\nName", "engine\r\nName" };
65         const int                                       patchNumbers[]                  = { 0, 1, 2, 3, 4, 5, 13, 4094, 4095 };
66         const deUint32                          appVersions[]                   = { 0, 1, (deUint32)-1 };
67         const deUint32                          engineVersions[]                = { 0, 1, (deUint32)-1 };
68         const PlatformInterface&        platformInterface               = context.getPlatformInterface();
69         const deUint32                          apiVersion                              = context.getUsedApiVersion();
70         vector<VkApplicationInfo>       appInfos;
71
72         // test over appName
73         for (int appNameNdx = 0; appNameNdx < DE_LENGTH_OF_ARRAY(appNames); appNameNdx++)
74         {
75                 const VkApplicationInfo appInfo =
76                 {
77                         VK_STRUCTURE_TYPE_APPLICATION_INFO,             // VkStructureType                              sType;
78                         DE_NULL,                                                                // const void*                                  pNext;
79                         appNames[appNameNdx],                                   // const char*                                  pAppName;
80                         0u,                                                                             // deUint32                                             appVersion;
81                         "engineName",                                                   // const char*                                  pEngineName;
82                         0u,                                                                             // deUint32                                             engineVersion;
83                         apiVersion,                                                             // deUint32                                             apiVersion;
84                 };
85
86                 appInfos.push_back(appInfo);
87         }
88
89         // test over engineName
90         for (int engineNameNdx = 0; engineNameNdx < DE_LENGTH_OF_ARRAY(engineNames); engineNameNdx++)
91         {
92                 const VkApplicationInfo appInfo =
93                 {
94                         VK_STRUCTURE_TYPE_APPLICATION_INFO,             // VkStructureType                              sType;
95                         DE_NULL,                                                                // const void*                                  pNext;
96                         "appName",                                                              // const char*                                  pAppName;
97                         0u,                                                                             // deUint32                                             appVersion;
98                         engineNames[engineNameNdx],                             // const char*                                  pEngineName;
99                         0u,                                                                             // deUint32                                             engineVersion;
100                         apiVersion,                                                             // deUint32                                             apiVersion;
101                 };
102
103                 appInfos.push_back(appInfo);
104         }
105
106         // test over appVersion
107         for (int appVersionNdx = 0; appVersionNdx < DE_LENGTH_OF_ARRAY(appVersions); appVersionNdx++)
108         {
109                 const VkApplicationInfo appInfo =
110                 {
111                         VK_STRUCTURE_TYPE_APPLICATION_INFO,             // VkStructureType                              sType;
112                         DE_NULL,                                                                // const void*                                  pNext;
113                         "appName",                                                              // const char*                                  pAppName;
114                         appVersions[appVersionNdx],                             // deUint32                                             appVersion;
115                         "engineName",                                                   // const char*                                  pEngineName;
116                         0u,                                                                             // deUint32                                             engineVersion;
117                         apiVersion,                                                             // deUint32                                             apiVersion;
118                 };
119
120                 appInfos.push_back(appInfo);
121         }
122
123         // test over engineVersion
124         for (int engineVersionNdx = 0; engineVersionNdx < DE_LENGTH_OF_ARRAY(engineVersions); engineVersionNdx++)
125         {
126                 const VkApplicationInfo appInfo =
127                 {
128                         VK_STRUCTURE_TYPE_APPLICATION_INFO,             // VkStructureType                              sType;
129                         DE_NULL,                                                                // const void*                                  pNext;
130                         "appName",                                                              // const char*                                  pAppName;
131                         0u,                                                                             // deUint32                                             appVersion;
132                         "engineName",                                                   // const char*                                  pEngineName;
133                         engineVersions[engineVersionNdx],               // deUint32                                             engineVersion;
134                         apiVersion,                                                             // deUint32                                             apiVersion;
135                 };
136
137                 appInfos.push_back(appInfo);
138         }
139         const deUint32  manjorNum       = unpackVersion(apiVersion).majorNum;
140         const deUint32  minorNum        = unpackVersion(apiVersion).minorNum;
141
142         // patch component of api version checking (should be ignored by implementation)
143         for (int patchVersion = 0; patchVersion < DE_LENGTH_OF_ARRAY(patchNumbers); patchVersion++)
144         {
145                 const VkApplicationInfo appInfo =
146                 {
147                         VK_STRUCTURE_TYPE_APPLICATION_INFO,                                                                     // VkStructureType                              sType;
148                         DE_NULL,                                                                                                                        // const void*                                  pNext;
149                         "appName",                                                                                                                      // const char*                                  pAppName;
150                         0u,                                                                                                                                     // deUint32                                             appVersion;
151                         "engineName",                                                                                                           // const char*                                  pEngineName;
152                         0u,                                                                                                                                     // deUint32                                             engineVersion;
153                         VK_MAKE_VERSION(manjorNum, minorNum, patchNumbers[patchVersion]),       // deUint32                                             apiVersion;
154                 };
155
156                 appInfos.push_back(appInfo);
157         }
158
159         // test when apiVersion is 0
160         {
161                 const VkApplicationInfo appInfo =
162                 {
163                         VK_STRUCTURE_TYPE_APPLICATION_INFO,             // VkStructureType                              sType;
164                         DE_NULL,                                                                // const void*                                  pNext;
165                         "appName",                                                              // const char*                                  pAppName;
166                         0u,                                                                             // deUint32                                             appVersion;
167                         "engineName",                                                   // const char*                                  pEngineName;
168                         0u,                                                                             // deUint32                                             engineVersion;
169                         0u,                                                                             // deUint32                                             apiVersion;
170                 };
171
172                 appInfos.push_back(appInfo);
173         }
174
175         // run the tests!
176         for (size_t appInfoNdx = 0; appInfoNdx < appInfos.size(); ++appInfoNdx)
177         {
178                 const VkApplicationInfo&                appInfo                                 = appInfos[appInfoNdx];
179                 const VkInstanceCreateInfo              instanceCreateInfo              =
180                 {
181                         VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // VkStructureType                              sType;
182                         DE_NULL,                                                                // const void*                                  pNext;
183                         (VkInstanceCreateFlags)0u,                              // VkInstanceCreateFlags                flags;
184                         &appInfo,                                                               // const VkApplicationInfo*             pAppInfo;
185                         0u,                                                                             // deUint32                                             layerCount;
186                         DE_NULL,                                                                // const char*const*                    ppEnabledLayernames;
187                         0u,                                                                             // deUint32                                             extensionCount;
188                         DE_NULL,                                                                // const char*const*                    ppEnabledExtensionNames;
189                 };
190
191                 log << TestLog::Message << "Creating instance with appInfo: " << appInfo << TestLog::EndMessage;
192
193                 try
194                 {
195                         const Unique<VkInstance> instance(createInstance(platformInterface, &instanceCreateInfo));
196                         log << TestLog::Message << "Succeeded" << TestLog::EndMessage;
197                 }
198                 catch (const vk::Error& err)
199                 {
200                         resultCollector.fail("Failed, Error code: " + de::toString(err.getMessage()));
201                 }
202         }
203
204         return tcu::TestStatus(resultCollector.getResult(), resultCollector.getMessage());
205 }
206
207 tcu::TestStatus createInstanceWithInvalidApiVersionTest (Context& context)
208 {
209         tcu::TestLog&                           log                                             = context.getTestContext().getLog();
210         tcu::ResultCollector            resultCollector                 (log);
211         const PlatformInterface&        platformInterface               = context.getPlatformInterface();
212
213         deUint32                                        instanceApiVersion              = 0u;
214         context.getPlatformInterface().enumerateInstanceVersion(&instanceApiVersion);
215
216         const ApiVersion                        apiVersion                              = unpackVersion(instanceApiVersion);
217
218         const deUint32                          invalidMajorVersion             = (1 << 10) - 1;
219         const deUint32                          invalidMinorVersion             = (1 << 10) - 1;
220         vector<ApiVersion>                      invalidApiVersions;
221
222         invalidApiVersions.push_back(ApiVersion(invalidMajorVersion, apiVersion.minorNum, apiVersion.patchNum));
223         invalidApiVersions.push_back(ApiVersion(apiVersion.majorNum, invalidMinorVersion, apiVersion.patchNum));
224
225         for (size_t apiVersionNdx = 0; apiVersionNdx < invalidApiVersions.size(); apiVersionNdx++)
226         {
227                 const VkApplicationInfo appInfo                                 =
228                 {
229                         VK_STRUCTURE_TYPE_APPLICATION_INFO,                     // VkStructureType                              sType;
230                         DE_NULL,                                                                        // const void*                                  pNext;
231                         "appName",                                                                      // const char*                                  pAppName;
232                         0u,                                                                                     // deUint32                                             appVersion;
233                         "engineName",                                                           // const char*                                  pEngineName;
234                         0u,                                                                                     // deUint32                                             engineVersion;
235                         pack(invalidApiVersions[apiVersionNdx]),        // deUint32                                             apiVersion;
236                 };
237                 const VkInstanceCreateInfo instanceCreateInfo   =
238                 {
239                         VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,         // VkStructureType                              sType;
240                         DE_NULL,                                                                        // const void*                                  pNext;
241                         (VkInstanceCreateFlags)0u,                                      // VkInstanceCreateFlags                flags;
242                         &appInfo,                                                                       // const VkApplicationInfo*             pAppInfo;
243                         0u,                                                                                     // deUint32                                             layerCount;
244                         DE_NULL,                                                                        // const char*const*                    ppEnabledLayernames;
245                         0u,                                                                                     // deUint32                                             extensionCount;
246                         DE_NULL,                                                                        // const char*const*                    ppEnabledExtensionNames;
247                 };
248
249
250                 log << TestLog::Message
251                         << "API version reported by enumerateInstanceVersion: " << apiVersion
252                         << ", api version used to create instance: " << invalidApiVersions[apiVersionNdx]
253                         << TestLog::EndMessage;
254
255                 {
256                         VkInstance                      instance                                = (VkInstance)0;
257                         const VkResult          result                                  = platformInterface.createInstance(&instanceCreateInfo, DE_NULL/*pAllocator*/, &instance);
258                         const bool                      gotInstance                             = !!instance;
259
260                         if (instance)
261                         {
262                                 const InstanceDriver    instanceIface(platformInterface, instance);
263                                 instanceIface.destroyInstance(instance, DE_NULL/*pAllocator*/);
264                         }
265
266                         if (apiVersion.majorNum == 1 && apiVersion.minorNum == 0)
267                         {
268                                 if (result == VK_ERROR_INCOMPATIBLE_DRIVER)
269                                 {
270                                         TCU_CHECK(!gotInstance);
271                                         log << TestLog::Message << "Pass, instance creation with invalid apiVersion is rejected" << TestLog::EndMessage;
272                                 }
273                                 else
274                                         resultCollector.fail("Fail, instance creation with invalid apiVersion is not rejected");
275                         }
276                         else if (apiVersion.majorNum == 1 && apiVersion.minorNum >= 1)
277                         {
278                                 if (result == VK_SUCCESS)
279                                 {
280                                         TCU_CHECK(gotInstance);
281                                         log << TestLog::Message << "Pass, instance creation with nonstandard apiVersion succeeds for Vulkan 1.1" << TestLog::EndMessage;
282                                 }
283                                 else if (result == VK_ERROR_INCOMPATIBLE_DRIVER)
284                                 {
285                                         resultCollector.fail("Fail, In Vulkan 1.1 instance creation must not return VK_ERROR_INCOMPATIBLE_DRIVER.");
286                                 }
287                                 else
288                                 {
289                                         std::ostringstream message;
290                                         message << "Fail, createInstance failed with " << result;
291                                         resultCollector.fail(message.str().c_str());
292                                 }
293                         }
294                 }
295         }
296
297         return tcu::TestStatus(resultCollector.getResult(), resultCollector.getMessage());
298 }
299
300 tcu::TestStatus createInstanceWithNullApplicationInfoTest (Context& context)
301 {
302         tcu::TestLog&                           log                                             = context.getTestContext().getLog();
303         tcu::ResultCollector            resultCollector                 (log);
304         const PlatformInterface&        platformInterface               = context.getPlatformInterface();
305
306         const VkInstanceCreateInfo              instanceCreateInfo              =
307         {
308                 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // VkStructureType                              sType;
309                 DE_NULL,                                                                // const void*                                  pNext;
310                 (VkInstanceCreateFlags)0u,                              // VkInstanceCreateFlags                flags;
311                 DE_NULL,                                                                // const VkApplicationInfo*             pAppInfo;
312                 0u,                                                                             // deUint32                                             layerCount;
313                 DE_NULL,                                                                // const char*const*                    ppEnabledLayernames;
314                 0u,                                                                             // deUint32                                             extensionCount;
315                 DE_NULL,                                                                // const char*const*                    ppEnabledExtensionNames;
316         };
317
318         log << TestLog::Message << "Creating instance with NULL pApplicationInfo" << TestLog::EndMessage;
319
320         try
321         {
322                 const Unique<VkInstance> instance(createInstance(platformInterface, &instanceCreateInfo));
323                 log << TestLog::Message << "Succeeded" << TestLog::EndMessage;
324         }
325         catch (const vk::Error& err)
326         {
327                 resultCollector.fail("Failed, Error code: " + de::toString(err.getMessage()));
328         }
329
330         return tcu::TestStatus(resultCollector.getResult(), resultCollector.getMessage());
331 }
332
333 tcu::TestStatus createInstanceWithUnsupportedExtensionsTest (Context& context)
334 {
335         tcu::TestLog&                                           log                                             = context.getTestContext().getLog();
336         const PlatformInterface&                        platformInterface               = context.getPlatformInterface();
337         const char*                                                     enabledExtensions[]             = {"VK_UNSUPPORTED_EXTENSION", "THIS_IS_NOT_AN_EXTENSION"};
338         const deUint32                                          apiVersion                              = context.getUsedApiVersion();
339         const VkApplicationInfo                         appInfo                                 =
340         {
341                 VK_STRUCTURE_TYPE_APPLICATION_INFO,                                             // VkStructureType                              sType;
342                 DE_NULL,                                                                                                // const void*                                  pNext;
343                 "appName",                                                                                              // const char*                                  pAppName;
344                 0u,                                                                                                             // deUint32                                             appVersion;
345                 "engineName",                                                                                   // const char*                                  pEngineName;
346                 0u,                                                                                                             // deUint32                                             engineVersion;
347                 apiVersion,                                                                                             // deUint32                                             apiVersion;
348         };
349         const VkInstanceCreateInfo                      instanceCreateInfo              =
350         {
351                 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,                                 // VkStructureType                              sType;
352                 DE_NULL,                                                                                                // const void*                                  pNext;
353                 (VkInstanceCreateFlags)0u,                                                              // VkInstanceCreateFlags                flags;
354                 &appInfo,                                                                                               // const VkApplicationInfo*             pAppInfo;
355                 0u,                                                                                                             // deUint32                                             layerCount;
356                 DE_NULL,                                                                                                // const char*const*                    ppEnabledLayernames;
357                 DE_LENGTH_OF_ARRAY(enabledExtensions),                                  // deUint32                                             extensionCount;
358                 enabledExtensions,                                                                              // const char*const*                    ppEnabledExtensionNames;
359         };
360
361         log << TestLog::Message << "Enabled extensions are: " << TestLog::EndMessage;
362
363         for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(enabledExtensions); ndx++)
364                 log << TestLog::Message << enabledExtensions[ndx] <<  TestLog::EndMessage;
365
366         {
367                 VkInstance              instance        = (VkInstance)0;
368                 const VkResult  result          = platformInterface.createInstance(&instanceCreateInfo, DE_NULL/*pAllocator*/, &instance);
369                 const bool              gotInstance     = !!instance;
370
371                 if (instance)
372                 {
373                         const InstanceDriver    instanceIface   (platformInterface, instance);
374                         instanceIface.destroyInstance(instance, DE_NULL/*pAllocator*/);
375                 }
376
377                 if (result == VK_ERROR_EXTENSION_NOT_PRESENT)
378                 {
379                         TCU_CHECK(!gotInstance);
380                         return tcu::TestStatus::pass("Pass, creating instance with unsupported extension was rejected.");
381                 }
382                 else
383                         return tcu::TestStatus::fail("Fail, creating instance with unsupported extensions succeeded.");
384         }
385 }
386
387 enum
388 {
389         UTF8ABUSE_LONGNAME = 0,
390         UTF8ABUSE_BADNAMES,
391         UTF8ABUSE_OVERLONGNUL,
392         UTF8ABUSE_OVERLONG,
393         UTF8ABUSE_ZALGO,
394         UTF8ABUSE_CHINESE,
395         UTF8ABUSE_EMPTY,
396         UTF8ABUSE_MAX
397 };
398
399 string getUTF8AbuseString (int index)
400 {
401         switch (index)
402         {
403         case UTF8ABUSE_LONGNAME:
404                 // Generate a long name.
405                 {
406                         std::string longname;
407                         longname.resize(65535, 'k');
408                         return longname;
409                 }
410
411         case UTF8ABUSE_BADNAMES:
412                 // Various illegal code points in utf-8
413                 return string(
414                         "Illegal bytes in UTF-8: "
415                         "\xc0 \xc1 \xf5 \xf6 \xf7 \xf8 \xf9 \xfa \xfb \xfc \xfd \xfe \xff"
416                         "illegal surrogates: \xed\xad\xbf \xed\xbe\x80");
417
418         case UTF8ABUSE_OVERLONGNUL:
419                 // Zero encoded as overlong, not exactly legal but often supported to differentiate from terminating zero
420                 return string("UTF-8 encoded nul \xC0\x80 (should not end name)");
421
422         case UTF8ABUSE_OVERLONG:
423                 // Some overlong encodings
424                 return string(
425                         "UTF-8 overlong \xF0\x82\x82\xAC \xfc\x83\xbf\xbf\xbf\xbf \xf8\x87\xbf\xbf\xbf "
426                         "\xf0\x8f\xbf\xbf");
427
428         case UTF8ABUSE_ZALGO:
429                 // Internet "zalgo" meme "bleeding text"
430                 return string(
431                         "\x56\xcc\xb5\xcc\x85\xcc\x94\xcc\x88\xcd\x8a\xcc\x91\xcc\x88\xcd\x91\xcc\x83\xcd\x82"
432                         "\xcc\x83\xcd\x90\xcc\x8a\xcc\x92\xcc\x92\xcd\x8b\xcc\x94\xcd\x9d\xcc\x98\xcc\xab\xcc"
433                         "\xae\xcc\xa9\xcc\xad\xcc\x97\xcc\xb0\x75\xcc\xb6\xcc\xbe\xcc\x80\xcc\x82\xcc\x84\xcd"
434                         "\x84\xcc\x90\xcd\x86\xcc\x9a\xcd\x84\xcc\x9b\xcd\x86\xcd\x92\xcc\x9a\xcd\x99\xcd\x99"
435                         "\xcc\xbb\xcc\x98\xcd\x8e\xcd\x88\xcd\x9a\xcc\xa6\xcc\x9c\xcc\xab\xcc\x99\xcd\x94\xcd"
436                         "\x99\xcd\x95\xcc\xa5\xcc\xab\xcd\x89\x6c\xcc\xb8\xcc\x8e\xcc\x8b\xcc\x8b\xcc\x9a\xcc"
437                         "\x8e\xcd\x9d\xcc\x80\xcc\xa1\xcc\xad\xcd\x9c\xcc\xba\xcc\x96\xcc\xb3\xcc\xa2\xcd\x8e"
438                         "\xcc\xa2\xcd\x96\x6b\xcc\xb8\xcc\x84\xcd\x81\xcc\xbf\xcc\x8d\xcc\x89\xcc\x85\xcc\x92"
439                         "\xcc\x84\xcc\x90\xcd\x81\xcc\x93\xcd\x90\xcd\x92\xcd\x9d\xcc\x84\xcd\x98\xcd\x9d\xcd"
440                         "\xa0\xcd\x91\xcc\x94\xcc\xb9\xcd\x93\xcc\xa5\xcd\x87\xcc\xad\xcc\xa7\xcd\x96\xcd\x99"
441                         "\xcc\x9d\xcc\xbc\xcd\x96\xcd\x93\xcc\x9d\xcc\x99\xcc\xa8\xcc\xb1\xcd\x85\xcc\xba\xcc"
442                         "\xa7\x61\xcc\xb8\xcc\x8e\xcc\x81\xcd\x90\xcd\x84\xcd\x8c\xcc\x8c\xcc\x85\xcd\x86\xcc"
443                         "\x84\xcd\x84\xcc\x90\xcc\x84\xcc\x8d\xcd\x99\xcd\x8d\xcc\xb0\xcc\xa3\xcc\xa6\xcd\x89"
444                         "\xcd\x8d\xcd\x87\xcc\x98\xcd\x8d\xcc\xa4\xcd\x9a\xcd\x8e\xcc\xab\xcc\xb9\xcc\xac\xcc"
445                         "\xa2\xcd\x87\xcc\xa0\xcc\xb3\xcd\x89\xcc\xb9\xcc\xa7\xcc\xa6\xcd\x89\xcd\x95\x6e\xcc"
446                         "\xb8\xcd\x8a\xcc\x8a\xcd\x82\xcc\x9b\xcd\x81\xcd\x90\xcc\x85\xcc\x9b\xcd\x80\xcd\x91"
447                         "\xcd\x9b\xcc\x81\xcd\x81\xcc\x9a\xcc\xb3\xcd\x9c\xcc\x9e\xcc\x9d\xcd\x99\xcc\xa2\xcd"
448                         "\x93\xcd\x96\xcc\x97\xff");
449
450         case UTF8ABUSE_CHINESE:
451                 // Some Chinese glyphs.
452                 // "English equivalent: The devil is in the details", https://en.wikiquote.org/wiki/Chinese_proverbs
453                 return string(
454                         "\xe8\xaf\xbb\xe4\xb9\xa6\xe9\xa1\xbb\xe7\x94\xa8\xe6\x84\x8f\xef\xbc\x8c\xe4\xb8\x80"
455                         "\xe5\xad\x97\xe5\x80\xbc\xe5\x8d\x83\xe9\x87\x91\x20");
456
457         default:
458                 DE_ASSERT(index == UTF8ABUSE_EMPTY);
459                 // Also try an empty string.
460                 return string("");
461         }
462 }
463
464 tcu::TestStatus createInstanceWithExtensionNameAbuseTest (Context& context)
465 {
466         const PlatformInterface&        platformInterface       = context.getPlatformInterface();
467         const char*                                     extensionList[1]        = { 0 };
468         const deUint32                          apiVersion                      = context.getUsedApiVersion();
469         deUint32                                        failCount                       = 0;
470
471         for (int i = 0; i < UTF8ABUSE_MAX; i++)
472         {
473                 string abuseString      = getUTF8AbuseString(i);
474                 extensionList[0]        = abuseString.c_str();
475
476                 const VkApplicationInfo         appInfo =
477                 {
478                         VK_STRUCTURE_TYPE_APPLICATION_INFO,                             // VkStructureType                      sType;
479                         DE_NULL,                                                                                // const void*                          pNext;
480                         "appName",                                                                              // const char*                          pAppName;
481                         0u,                                                                                             // deUint32                                     appVersion;
482                         "engineName",                                                                   // const char*                          pEngineName;
483                         0u,                                                                                             // deUint32                                     engineVersion;
484                         apiVersion,                                                                             // deUint32                                     apiVersion;
485                 };
486
487                 const VkInstanceCreateInfo      instanceCreateInfo =
488                 {
489                         VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,                 // VkStructureType                      sType;
490                         DE_NULL,                                                                                // const void*                          pNext;
491                         (VkInstanceCreateFlags)0u,                                              // VkInstanceCreateFlags        flags;
492                         &appInfo,                                                                               // const VkApplicationInfo*     pAppInfo;
493                         0u,                                                                                             // deUint32                                     layerCount;
494                         DE_NULL,                                                                                // const char*const*            ppEnabledLayernames;
495                         1u,                                                                                             // deUint32                                     extensionCount;
496                         extensionList,                                                                  // const char*const*            ppEnabledExtensionNames;
497                 };
498
499                 {
500                         VkInstance              instance        = (VkInstance)0;
501                         const VkResult  result          = platformInterface.createInstance(&instanceCreateInfo, DE_NULL/*pAllocator*/, &instance);
502                         const bool              gotInstance     = !!instance;
503
504                         if (instance)
505                         {
506                                 const InstanceDriver instanceIface(platformInterface, instance);
507                                 instanceIface.destroyInstance(instance, DE_NULL/*pAllocator*/);
508                         }
509
510                         if (result != VK_ERROR_EXTENSION_NOT_PRESENT)
511                                 failCount++;
512
513                         TCU_CHECK(!gotInstance);
514                 }
515         }
516
517         if (failCount > 0)
518                 return tcu::TestStatus::fail("Fail, creating instances with unsupported extensions succeeded.");
519
520         return tcu::TestStatus::pass("Pass, creating instances with unsupported extensions were rejected.");
521 }
522
523 tcu::TestStatus createInstanceWithLayerNameAbuseTest (Context& context)
524 {
525         const PlatformInterface&        platformInterface       = context.getPlatformInterface();
526         const char*                                     layerList[1]            = { 0 };
527         const deUint32                          apiVersion                      = context.getUsedApiVersion();
528         deUint32                                        failCount                       = 0;
529
530         for (int i = 0; i < UTF8ABUSE_MAX; i++)
531         {
532                 string abuseString      = getUTF8AbuseString(i);
533                 layerList[0]            = abuseString.c_str();
534
535                 const VkApplicationInfo         appInfo =
536                 {
537                         VK_STRUCTURE_TYPE_APPLICATION_INFO,             // VkStructureType                      sType;
538                         DE_NULL,                                                                // const void*                          pNext;
539                         "appName",                                                              // const char*                          pAppName;
540                         0u,                                                                             // deUint32                                     appVersion;
541                         "engineName",                                                   // const char*                          pEngineName;
542                         0u,                                                                             // deUint32                                     engineVersion;
543                         apiVersion,                                                             // deUint32                                     apiVersion;
544                 };
545
546                 const VkInstanceCreateInfo      instanceCreateInfo =
547                 {
548                         VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // VkStructureType                      sType;
549                         DE_NULL,                                                                // const void*                          pNext;
550                         (VkInstanceCreateFlags)0u,                              // VkInstanceCreateFlags        flags;
551                         &appInfo,                                                               // const VkApplicationInfo*     pAppInfo;
552                         1u,                                                                             // deUint32                                     layerCount;
553                         layerList,                                                              // const char*const*            ppEnabledLayernames;
554                         0u,                                                                             // deUint32                                     extensionCount;
555                         DE_NULL,                                                                // const char*const*            ppEnabledExtensionNames;
556                 };
557
558                 {
559                         VkInstance              instance        = (VkInstance)0;
560                         const VkResult  result          = platformInterface.createInstance(&instanceCreateInfo, DE_NULL/*pAllocator*/, &instance);
561                         const bool              gotInstance     = !!instance;
562
563                         if (instance)
564                         {
565                                 const InstanceDriver instanceIface(platformInterface, instance);
566                                 instanceIface.destroyInstance(instance, DE_NULL/*pAllocator*/);
567                         }
568
569                         if (result != VK_ERROR_LAYER_NOT_PRESENT)
570                                 failCount++;
571
572                         TCU_CHECK(!gotInstance);
573                 }
574         }
575
576         if (failCount > 0)
577                 return tcu::TestStatus::fail("Fail, creating instances with unsupported layers succeeded.");
578
579         return tcu::TestStatus::pass("Pass, creating instances with unsupported layers were rejected.");
580 }
581
582 tcu::TestStatus createDeviceTest (Context& context)
583 {
584         const PlatformInterface&                platformInterface               = context.getPlatformInterface();
585         const Unique<VkInstance>                instance                                (createDefaultInstance(platformInterface, context.getUsedApiVersion()));
586         const InstanceDriver                    instanceDriver                  (platformInterface, instance.get());
587         const VkPhysicalDevice                  physicalDevice                  = chooseDevice(instanceDriver, instance.get(), context.getTestContext().getCommandLine());
588         const deUint32                                  queueFamilyIndex                = 0;
589         const deUint32                                  queueCount                              = 1;
590         const deUint32                                  queueIndex                              = 0;
591         const float                                             queuePriority                   = 1.0f;
592
593         const vector<VkQueueFamilyProperties> queueFamilyProperties     = getPhysicalDeviceQueueFamilyProperties(instanceDriver, physicalDevice);
594
595         const VkDeviceQueueCreateInfo   deviceQueueCreateInfo   =
596         {
597                 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
598                 DE_NULL,
599                 (VkDeviceQueueCreateFlags)0u,
600                 queueFamilyIndex,                                               //queueFamilyIndex;
601                 queueCount,                                                             //queueCount;
602                 &queuePriority,                                                 //pQueuePriorities;
603         };
604         const VkDeviceCreateInfo                deviceCreateInfo        =
605         {
606                 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,   //sType;
607                 DE_NULL,                                                                //pNext;
608                 (VkDeviceCreateFlags)0u,
609                 1,                                                                              //queueRecordCount;
610                 &deviceQueueCreateInfo,                                 //pRequestedQueues;
611                 0,                                                                              //layerCount;
612                 DE_NULL,                                                                //ppEnabledLayerNames;
613                 0,                                                                              //extensionCount;
614                 DE_NULL,                                                                //ppEnabledExtensionNames;
615                 DE_NULL,                                                                //pEnabledFeatures;
616         };
617
618         const Unique<VkDevice>                  device                                  (createDevice(platformInterface, *instance, instanceDriver, physicalDevice, &deviceCreateInfo));
619         const DeviceDriver                              deviceDriver                    (platformInterface, instance.get(), device.get());
620         const VkQueue                                   queue                                   = getDeviceQueue(deviceDriver, *device,  queueFamilyIndex, queueIndex);
621
622         VK_CHECK(deviceDriver.queueWaitIdle(queue));
623
624         return tcu::TestStatus::pass("Pass");
625 }
626
627 tcu::TestStatus createMultipleDevicesTest (Context& context)
628 {
629         tcu::TestLog&                                                                           log                                             = context.getTestContext().getLog();
630         tcu::ResultCollector                                                            resultCollector                 (log);
631         const int                                                                                       numDevices                              = 5;
632         const PlatformInterface&                                                        platformInterface               = context.getPlatformInterface();
633         const Unique<VkInstance>                                                        instance                                (createDefaultInstance(platformInterface, context.getUsedApiVersion()));
634         const InstanceDriver                                                            instanceDriver                  (platformInterface, instance.get());
635         const VkPhysicalDevice                                                          physicalDevice                  = chooseDevice(instanceDriver, instance.get(), context.getTestContext().getCommandLine());
636         const vector<VkQueueFamilyProperties>                           queueFamilyProperties   = getPhysicalDeviceQueueFamilyProperties(instanceDriver, physicalDevice);
637         const deUint32                                                                          queueFamilyIndex                = 0;
638         const deUint32                                                                          queueCount                              = 1;
639         const deUint32                                                                          queueIndex                              = 0;
640         const float                                                                                     queuePriority                   = 1.0f;
641         const VkDeviceQueueCreateInfo                                           deviceQueueCreateInfo   =
642         {
643                 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
644                 DE_NULL,
645                 (VkDeviceQueueCreateFlags)0u,                                   //flags;
646                 queueFamilyIndex,                                                               //queueFamilyIndex;
647                 queueCount,                                                                             //queueCount;
648                 &queuePriority,                                                                 //pQueuePriorities;
649         };
650         const VkDeviceCreateInfo                                                        deviceCreateInfo                =
651         {
652                 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,                   //sType;
653                 DE_NULL,                                                                                //pNext;
654                 (VkDeviceCreateFlags)0u,
655                 1,                                                                                              //queueRecordCount;
656                 &deviceQueueCreateInfo,                                                 //pRequestedQueues;
657                 0,                                                                                              //layerCount;
658                 DE_NULL,                                                                                //ppEnabledLayerNames;
659                 0,                                                                                              //extensionCount;
660                 DE_NULL,                                                                                //ppEnabledExtensionNames;
661                 DE_NULL,                                                                                //pEnabledFeatures;
662         };
663         vector<VkDevice>                                                                        devices(numDevices, (VkDevice)DE_NULL);
664
665         try
666         {
667                 for (int deviceNdx = 0; deviceNdx < numDevices; deviceNdx++)
668                 {
669                         const VkResult result = instanceDriver.createDevice(physicalDevice, &deviceCreateInfo, DE_NULL/*pAllocator*/, &devices[deviceNdx]);
670
671                         if (result != VK_SUCCESS)
672                         {
673                                 resultCollector.fail("Failed to create Device No." + de::toString(deviceNdx) + ", Error Code: " + de::toString(result));
674                                 break;
675                         }
676
677                         {
678                                 const DeviceDriver      deviceDriver    (platformInterface, instance.get(), devices[deviceNdx]);
679                                 const VkQueue           queue                   = getDeviceQueue(deviceDriver, devices[deviceNdx], queueFamilyIndex, queueIndex);
680
681                                 VK_CHECK(deviceDriver.queueWaitIdle(queue));
682                         }
683                 }
684         }
685         catch (const vk::Error& error)
686         {
687                 resultCollector.fail(de::toString(error.getError()));
688         }
689         catch (...)
690         {
691                 for (int deviceNdx = (int)devices.size()-1; deviceNdx >= 0; deviceNdx--)
692                 {
693                         if (devices[deviceNdx] != (VkDevice)DE_NULL)
694                         {
695                                 DeviceDriver deviceDriver(platformInterface, instance.get(), devices[deviceNdx]);
696                                 deviceDriver.destroyDevice(devices[deviceNdx], DE_NULL/*pAllocator*/);
697                         }
698                 }
699
700                 throw;
701         }
702
703         for (int deviceNdx = (int)devices.size()-1; deviceNdx >= 0; deviceNdx--)
704         {
705                 if (devices[deviceNdx] != (VkDevice)DE_NULL)
706                 {
707                         DeviceDriver deviceDriver(platformInterface, instance.get(), devices[deviceNdx]);
708                         deviceDriver.destroyDevice(devices[deviceNdx], DE_NULL/*pAllocator*/);
709                 }
710         }
711
712         return tcu::TestStatus(resultCollector.getResult(), resultCollector.getMessage());
713 }
714
715 tcu::TestStatus createDeviceWithUnsupportedExtensionsTest (Context& context)
716 {
717         tcu::TestLog&                                   log                                             = context.getTestContext().getLog();
718         const PlatformInterface&                platformInterface               = context.getPlatformInterface();
719         const Unique<VkInstance>                instance                                (createDefaultInstance(platformInterface, context.getUsedApiVersion()));
720         const InstanceDriver                    instanceDriver                  (platformInterface, instance.get());
721         const char*                                             enabledExtensions[]             = {"VK_UNSUPPORTED_EXTENSION", "THIS_IS_NOT_AN_EXTENSION", "VK_DONT_SUPPORT_ME"};
722         const VkPhysicalDevice                  physicalDevice                  = chooseDevice(instanceDriver, instance.get(), context.getTestContext().getCommandLine());
723         const float                                             queuePriority                   = 1.0f;
724         const VkDeviceQueueCreateInfo   deviceQueueCreateInfo   =
725         {
726                 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
727                 DE_NULL,
728                 (VkDeviceQueueCreateFlags)0u,
729                 0,                                                                              //queueFamilyIndex;
730                 1,                                                                              //queueCount;
731                 &queuePriority,                                                 //pQueuePriorities;
732         };
733         const VkDeviceCreateInfo                deviceCreateInfo                =
734         {
735                 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,   //sType;
736                 DE_NULL,                                                                //pNext;
737                 (VkDeviceCreateFlags)0u,
738                 1,                                                                              //queueRecordCount;
739                 &deviceQueueCreateInfo,                                 //pRequestedQueues;
740                 0,                                                                              //layerCount;
741                 DE_NULL,                                                                //ppEnabledLayerNames;
742                 DE_LENGTH_OF_ARRAY(enabledExtensions),  //extensionCount;
743                 enabledExtensions,                                              //ppEnabledExtensionNames;
744                 DE_NULL,                                                                //pEnabledFeatures;
745         };
746
747         log << TestLog::Message << "Enabled extensions are: " << TestLog::EndMessage;
748
749         for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(enabledExtensions); ndx++)
750                 log << TestLog::Message << enabledExtensions[ndx] <<  TestLog::EndMessage;
751
752         {
753                 VkDevice                device          = (VkDevice)0;
754                 const VkResult  result          = instanceDriver.createDevice(physicalDevice, &deviceCreateInfo, DE_NULL/*pAllocator*/, &device);
755                 const bool              gotDevice       = !!device;
756
757                 if (device)
758                 {
759                         const DeviceDriver      deviceIface     (platformInterface, instance.get(), device);
760                         deviceIface.destroyDevice(device, DE_NULL/*pAllocator*/);
761                 }
762
763                 if (result == VK_ERROR_EXTENSION_NOT_PRESENT)
764                 {
765                         TCU_CHECK(!gotDevice);
766                         return tcu::TestStatus::pass("Pass, create device with unsupported extension is rejected.");
767                 }
768                 else
769                         return tcu::TestStatus::fail("Fail, create device with unsupported extension but succeed.");
770         }
771 }
772
773 deUint32 getGlobalMaxQueueCount(const vector<VkQueueFamilyProperties>& queueFamilyProperties)
774 {
775         deUint32 maxQueueCount = 0;
776
777         for (deUint32 queueFamilyNdx = 0; queueFamilyNdx < (deUint32)queueFamilyProperties.size(); queueFamilyNdx++)
778         {
779                 maxQueueCount = de::max(maxQueueCount, queueFamilyProperties[queueFamilyNdx].queueCount);
780         }
781
782         return maxQueueCount;
783 }
784
785 tcu::TestStatus createDeviceWithVariousQueueCountsTest (Context& context)
786 {
787         tcu::TestLog&                                                   log                                             = context.getTestContext().getLog();
788         const int                                                               queueCountDiff                  = 1;
789         const PlatformInterface&                                platformInterface               = context.getPlatformInterface();
790         const Unique<VkInstance>                                instance                                (createDefaultInstance(platformInterface, context.getUsedApiVersion()));
791         const InstanceDriver                                    instanceDriver                  (platformInterface, instance.get());
792         const VkPhysicalDevice                                  physicalDevice                  = chooseDevice(instanceDriver, instance.get(), context.getTestContext().getCommandLine());
793         const vector<VkQueueFamilyProperties>   queueFamilyProperties   = getPhysicalDeviceQueueFamilyProperties(instanceDriver, physicalDevice);
794         const vector<float>                                             queuePriorities                 (getGlobalMaxQueueCount(queueFamilyProperties), 1.0f);
795         vector<VkDeviceQueueCreateInfo>                 deviceQueueCreateInfos;
796
797         for (deUint32 queueFamilyNdx = 0; queueFamilyNdx < (deUint32)queueFamilyProperties.size(); queueFamilyNdx++)
798         {
799                 const deUint32 maxQueueCount = queueFamilyProperties[queueFamilyNdx].queueCount;
800
801                 for (deUint32 queueCount = 1; queueCount <= maxQueueCount; queueCount += queueCountDiff)
802                 {
803                         const VkDeviceQueueCreateInfo queueCreateInfo =
804                         {
805                                 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
806                                 DE_NULL,
807                                 (VkDeviceQueueCreateFlags)0u,
808                                 queueFamilyNdx,
809                                 queueCount,
810                                 queuePriorities.data()
811                         };
812
813                         deviceQueueCreateInfos.push_back(queueCreateInfo);
814                 }
815         }
816
817         for (size_t testNdx = 0; testNdx < deviceQueueCreateInfos.size(); testNdx++)
818         {
819                 const VkDeviceQueueCreateInfo&  queueCreateInfo         = deviceQueueCreateInfos[testNdx];
820                 const VkDeviceCreateInfo                deviceCreateInfo        =
821                 {
822                         VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,   //sType;
823                         DE_NULL,                                                                //pNext;
824                         (VkDeviceCreateFlags)0u,
825                         1,                                                                              //queueRecordCount;
826                         &queueCreateInfo,                                               //pRequestedQueues;
827                         0,                                                                              //layerCount;
828                         DE_NULL,                                                                //ppEnabledLayerNames;
829                         0,                                                                              //extensionCount;
830                         DE_NULL,                                                                //ppEnabledExtensionNames;
831                         DE_NULL,                                                                //pEnabledFeatures;
832                 };
833                 const Unique<VkDevice>                  device                          (createDevice(platformInterface, *instance, instanceDriver, physicalDevice, &deviceCreateInfo));
834                 const DeviceDriver                              deviceDriver            (platformInterface, instance.get(), device.get());
835                 const deUint32                                  queueFamilyIndex        = deviceCreateInfo.pQueueCreateInfos->queueFamilyIndex;
836                 const deUint32                                  queueCount                      = deviceCreateInfo.pQueueCreateInfos->queueCount;
837
838                 for (deUint32 queueIndex = 0; queueIndex < queueCount; queueIndex++)
839                 {
840                         const VkQueue           queue   = getDeviceQueue(deviceDriver, *device, queueFamilyIndex, queueIndex);
841                         VkResult                        result;
842
843                         TCU_CHECK(!!queue);
844
845                         result = deviceDriver.queueWaitIdle(queue);
846                         if (result != VK_SUCCESS)
847                         {
848                                 log << TestLog::Message
849                                         << "vkQueueWaitIdle failed"
850                                         << ",  queueIndex = " << queueIndex
851                                         << ", queueCreateInfo " << queueCreateInfo
852                                         << ", Error Code: " << result
853                                         << TestLog::EndMessage;
854                                 return tcu::TestStatus::fail("Fail");
855                         }
856                 }
857         }
858         return tcu::TestStatus::pass("Pass");
859 }
860
861 tcu::TestStatus createDeviceFeatures2Test (Context& context)
862 {
863         const PlatformInterface&                vkp                                             = context.getPlatformInterface();
864         const Unique<VkInstance>                instance                                (createInstanceWithExtension(vkp, context.getUsedApiVersion(), "VK_KHR_get_physical_device_properties2"));
865         const InstanceDriver                    vki                                             (vkp, instance.get());
866         const VkPhysicalDevice                  physicalDevice                  = chooseDevice(vki, instance.get(), context.getTestContext().getCommandLine());
867         const deUint32                                  queueFamilyIndex                = 0;
868         const deUint32                                  queueCount                              = 1;
869         const deUint32                                  queueIndex                              = 0;
870         const float                                             queuePriority                   = 1.0f;
871
872         const InstanceDriver                                    instanceDriver                  (vkp, instance.get());
873         const vector<VkQueueFamilyProperties>   queueFamilyProperties   = getPhysicalDeviceQueueFamilyProperties(instanceDriver, physicalDevice);
874
875         VkPhysicalDeviceFeatures2               enabledFeatures;
876         const VkDeviceQueueCreateInfo   deviceQueueCreateInfo   =
877         {
878                 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
879                 DE_NULL,
880                 (VkDeviceQueueCreateFlags)0u,
881                 queueFamilyIndex,
882                 queueCount,
883                 &queuePriority,
884         };
885         const VkDeviceCreateInfo                deviceCreateInfo        =
886         {
887                 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
888                 &enabledFeatures,
889                 (VkDeviceCreateFlags)0u,
890                 1,
891                 &deviceQueueCreateInfo,
892                 0,
893                 DE_NULL,
894                 0,
895                 DE_NULL,
896                 DE_NULL,
897         };
898
899         // Populate enabledFeatures
900         enabledFeatures.sType           = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
901         enabledFeatures.pNext           = DE_NULL;
902
903         vki.getPhysicalDeviceFeatures2(physicalDevice, &enabledFeatures);
904
905         {
906                 const Unique<VkDevice>  device          (createDevice(vkp, *instance, vki, physicalDevice, &deviceCreateInfo));
907                 const DeviceDriver              vkd                     (vkp, instance.get(), device.get());
908                 const VkQueue                   queue           = getDeviceQueue(vkd, *device, queueFamilyIndex, queueIndex);
909
910                 VK_CHECK(vkd.queueWaitIdle(queue));
911         }
912
913         return tcu::TestStatus::pass("Pass");
914 }
915
916 struct Feature
917 {
918         const char*     name;
919         size_t          offset;
920 };
921
922 #define FEATURE_ITEM(MEMBER) {#MEMBER, DE_OFFSET_OF(VkPhysicalDeviceFeatures, MEMBER)}
923
924 tcu::TestStatus createDeviceWithUnsupportedFeaturesTest (Context& context)
925 {
926         tcu::TestLog&                           log                                             = context.getTestContext().getLog();
927         tcu::ResultCollector            resultCollector                 (log);
928         const PlatformInterface&        platformInterface               = context.getPlatformInterface();
929         const Move<VkInstance>          instance                                (createDefaultInstance(platformInterface, context.getUsedApiVersion()));
930         const InstanceDriver            instanceDriver                  (platformInterface, instance.get());
931         const VkPhysicalDevice          physicalDevice                  = chooseDevice(instanceDriver, instance.get(), context.getTestContext().getCommandLine());
932         const deUint32                          queueFamilyIndex                = 0;
933         const deUint32                          queueCount                              = 1;
934         const float                                     queuePriority                   = 1.0f;
935         VkPhysicalDeviceFeatures        physicalDeviceFeatures;
936
937         const vector<VkQueueFamilyProperties>   queueFamilyProperties   = getPhysicalDeviceQueueFamilyProperties(instanceDriver, physicalDevice);
938
939         instanceDriver.getPhysicalDeviceFeatures(physicalDevice, &physicalDeviceFeatures);
940
941         static const Feature features[] =
942         {
943                 FEATURE_ITEM(robustBufferAccess),
944                 FEATURE_ITEM(fullDrawIndexUint32),
945                 FEATURE_ITEM(imageCubeArray),
946                 FEATURE_ITEM(independentBlend),
947                 FEATURE_ITEM(geometryShader),
948                 FEATURE_ITEM(tessellationShader),
949                 FEATURE_ITEM(sampleRateShading),
950                 FEATURE_ITEM(dualSrcBlend),
951                 FEATURE_ITEM(logicOp),
952                 FEATURE_ITEM(multiDrawIndirect),
953                 FEATURE_ITEM(drawIndirectFirstInstance),
954                 FEATURE_ITEM(depthClamp),
955                 FEATURE_ITEM(depthBiasClamp),
956                 FEATURE_ITEM(fillModeNonSolid),
957                 FEATURE_ITEM(depthBounds),
958                 FEATURE_ITEM(wideLines),
959                 FEATURE_ITEM(largePoints),
960                 FEATURE_ITEM(alphaToOne),
961                 FEATURE_ITEM(multiViewport),
962                 FEATURE_ITEM(samplerAnisotropy),
963                 FEATURE_ITEM(textureCompressionETC2),
964                 FEATURE_ITEM(textureCompressionASTC_LDR),
965                 FEATURE_ITEM(textureCompressionBC),
966                 FEATURE_ITEM(occlusionQueryPrecise),
967                 FEATURE_ITEM(pipelineStatisticsQuery),
968                 FEATURE_ITEM(vertexPipelineStoresAndAtomics),
969                 FEATURE_ITEM(fragmentStoresAndAtomics),
970                 FEATURE_ITEM(shaderTessellationAndGeometryPointSize),
971                 FEATURE_ITEM(shaderImageGatherExtended),
972                 FEATURE_ITEM(shaderStorageImageExtendedFormats),
973                 FEATURE_ITEM(shaderStorageImageMultisample),
974                 FEATURE_ITEM(shaderStorageImageReadWithoutFormat),
975                 FEATURE_ITEM(shaderStorageImageWriteWithoutFormat),
976                 FEATURE_ITEM(shaderUniformBufferArrayDynamicIndexing),
977                 FEATURE_ITEM(shaderSampledImageArrayDynamicIndexing),
978                 FEATURE_ITEM(shaderStorageBufferArrayDynamicIndexing),
979                 FEATURE_ITEM(shaderStorageImageArrayDynamicIndexing),
980                 FEATURE_ITEM(shaderClipDistance),
981                 FEATURE_ITEM(shaderCullDistance),
982                 FEATURE_ITEM(shaderFloat64),
983                 FEATURE_ITEM(shaderInt64),
984                 FEATURE_ITEM(shaderInt16),
985                 FEATURE_ITEM(shaderResourceResidency),
986                 FEATURE_ITEM(shaderResourceMinLod),
987                 FEATURE_ITEM(sparseBinding),
988                 FEATURE_ITEM(sparseResidencyBuffer),
989                 FEATURE_ITEM(sparseResidencyImage2D),
990                 FEATURE_ITEM(sparseResidencyImage3D),
991                 FEATURE_ITEM(sparseResidency2Samples),
992                 FEATURE_ITEM(sparseResidency4Samples),
993                 FEATURE_ITEM(sparseResidency8Samples),
994                 FEATURE_ITEM(sparseResidency16Samples),
995                 FEATURE_ITEM(sparseResidencyAliased),
996                 FEATURE_ITEM(variableMultisampleRate),
997                 FEATURE_ITEM(inheritedQueries)
998         };
999
1000         const int       numFeatures             = DE_LENGTH_OF_ARRAY(features);
1001         int                     numErrors               = 0;
1002
1003         for (int featureNdx = 0; featureNdx < numFeatures; featureNdx++)
1004         {
1005                 // Test only features that are not supported.
1006                 if (*(((VkBool32*)((deUint8*)(&physicalDeviceFeatures) + features[featureNdx].offset))))
1007                         continue;
1008
1009                 VkPhysicalDeviceFeatures enabledFeatures;
1010
1011                 for (int i = 0; i < numFeatures; i++)
1012                         *((VkBool32*)((deUint8*)(&enabledFeatures) + features[i].offset)) = (i == featureNdx ? VK_TRUE : VK_FALSE);
1013
1014                 const VkDeviceQueueCreateInfo   deviceQueueCreateInfo   =
1015                 {
1016                         VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
1017                         DE_NULL,
1018                         (VkDeviceQueueCreateFlags)0u,
1019                         queueFamilyIndex,
1020                         queueCount,
1021                         &queuePriority
1022                 };
1023                 const VkDeviceCreateInfo                deviceCreateInfo                =
1024                 {
1025                         VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1026                         DE_NULL,
1027                         (VkDeviceCreateFlags)0u,
1028                         1,
1029                         &deviceQueueCreateInfo,
1030                         0,
1031                         DE_NULL,
1032                         0,
1033                         DE_NULL,
1034                         &enabledFeatures
1035                 };
1036
1037                 VkDevice                device;
1038                 const VkResult  res     = instanceDriver.createDevice(physicalDevice, &deviceCreateInfo, DE_NULL, &device);
1039
1040                 if (res != VK_ERROR_FEATURE_NOT_PRESENT)
1041                 {
1042                         numErrors++;
1043                         resultCollector.fail("Not returning VK_ERROR_FEATURE_NOT_PRESENT when creating device with feature "
1044                                                                  + de::toString(features[featureNdx].name) + ", which was reported as unsupported.");
1045                 }
1046         }
1047
1048         if (numErrors > 1)
1049                 return tcu::TestStatus(resultCollector.getResult(), "Enabling " + de::toString(numErrors) + " unsupported features didn't return VK_ERROR_FEATURE_NOT_PRESENT.");
1050         else
1051                 return tcu::TestStatus(resultCollector.getResult(), resultCollector.getMessage());
1052 }
1053
1054 tcu::TestStatus createDeviceQueue2Test (Context& context)
1055 {
1056         if (!context.contextSupports(vk::ApiVersion(1, 1, 0)))
1057                 TCU_THROW(NotSupportedError, "Vulkan 1.1 is not supported");
1058
1059         const PlatformInterface&                                platformInterface               = context.getPlatformInterface();
1060         const VkInstance                                                instance                                = context.getInstance();
1061         const InstanceDriver                                    instanceDriver                  (platformInterface, instance);
1062         const VkPhysicalDevice                                  physicalDevice                  = context.getPhysicalDevice();
1063         const deUint32                                                  queueFamilyIndex                = context.getUniversalQueueFamilyIndex();
1064         const deUint32                                                  queueCount                              = 1;
1065         const deUint32                                                  queueIndex                              = 0;
1066         const float                                                             queuePriority                   = 1.0f;
1067
1068         VkPhysicalDeviceProtectedMemoryFeatures protectedMemoryFeature  =
1069         {
1070                 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES,    // VkStructureType                                      sType;
1071                 DE_NULL,                                                                                                                // void*                                                        pNext;
1072                 VK_FALSE                                                                                                                // VkBool32                                                     protectedMemory;
1073         };
1074
1075         VkPhysicalDeviceFeatures2                               features2;
1076         deMemset(&features2, 0, sizeof(features2));
1077         features2.sType                                                                                                 = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1078         features2.pNext                                                                                                 = &protectedMemoryFeature;
1079
1080         instanceDriver.getPhysicalDeviceFeatures2(physicalDevice, &features2);
1081         if (protectedMemoryFeature.protectedMemory == VK_FALSE)
1082                 TCU_THROW(NotSupportedError, "Protected memory feature is not supported");
1083
1084         const VkDeviceQueueCreateInfo                   deviceQueueCreateInfo   =
1085         {
1086                 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,                                             // VkStructureType                                      sType;
1087                 DE_NULL,                                                                                                                // const void*                                          pNext;
1088                 VK_DEVICE_QUEUE_CREATE_PROTECTED_BIT,                                                   // VkDeviceQueueCreateFlags                     flags;
1089                 queueFamilyIndex,                                                                                               // deUint32                                                     queueFamilyIndex;
1090                 queueCount,                                                                                                             // deUint32                                                     queueCount;
1091                 &queuePriority,                                                                                                 // const float*                                         pQueuePriorities;
1092         };
1093         const VkDeviceCreateInfo                                deviceCreateInfo                =
1094         {
1095                 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,                                                   // VkStructureType                                      sType;
1096                 &features2,                                                                                                             // const void*                                          pNext;
1097                 (VkDeviceCreateFlags)0u,                                                                                // VkDeviceCreateFlags                          flags;
1098                 1,                                                                                                                              // deUint32                                                     queueCreateInfoCount;
1099                 &deviceQueueCreateInfo,                                                                                 // const VkDeviceQueueCreateInfo*       pQueueCreateInfos;
1100                 0,                                                                                                                              // deUint32                                                     enabledLayerCount;
1101                 DE_NULL,                                                                                                                // const char* const*                           ppEnabledLayerNames;
1102                 0,                                                                                                                              // deUint32                                                     enabledExtensionCount;
1103                 DE_NULL,                                                                                                                // const char* const*                           ppEnabledExtensionNames;
1104                 DE_NULL,                                                                                                                // const VkPhysicalDeviceFeatures*      pEnabledFeatures;
1105         };
1106         const VkDeviceQueueInfo2                                deviceQueueInfo2                =
1107         {
1108                 VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,                                                  // VkStructureType                                      sType;
1109                 DE_NULL,                                                                                                                // const void*                                          pNext;
1110                 VK_DEVICE_QUEUE_CREATE_PROTECTED_BIT,                                                   // VkDeviceQueueCreateFlags                     flags;
1111                 queueFamilyIndex,                                                                                               // deUint32                                                     queueFamilyIndex;
1112                 queueIndex,                                                                                                             // deUint32                                                     queueIndex;
1113         };
1114
1115         {
1116                 const Unique<VkDevice>                          device                                  (createDevice(platformInterface, instance, instanceDriver, physicalDevice, &deviceCreateInfo));
1117                 const DeviceDriver                                      deviceDriver                    (platformInterface, instance, device.get());
1118                 const VkQueue                                           queue2                                  = getDeviceQueue2(deviceDriver, *device, &deviceQueueInfo2);
1119
1120                 VK_CHECK(deviceDriver.queueWaitIdle(queue2));
1121         }
1122
1123         return tcu::TestStatus::pass("Pass");
1124 }
1125
1126 tcu::TestStatus createDeviceQueue2UnmatchedFlagsTest (Context& context)
1127 {
1128         if (!context.contextSupports(vk::ApiVersion(1, 1, 0)))
1129                 TCU_THROW(NotSupportedError, "Vulkan 1.1 is not supported");
1130
1131         const PlatformInterface&                platformInterface               = context.getPlatformInterface();
1132         const VkInstance                                instance                                = context.getInstance();
1133         const InstanceDriver                    instanceDriver                  (platformInterface, instance);
1134         const VkPhysicalDevice                  physicalDevice                  = context.getPhysicalDevice();
1135
1136         // Check if VK_DEVICE_QUEUE_CREATE_PROTECTED_BIT flag can be used.
1137         {
1138                 VkPhysicalDeviceProtectedMemoryFeatures         protectedFeatures;
1139                 protectedFeatures.sType         = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES;
1140                 protectedFeatures.pNext         = DE_NULL;
1141
1142                 VkPhysicalDeviceFeatures2                                       deviceFeatures;
1143                 deviceFeatures.sType            = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1144                 deviceFeatures.pNext            = &protectedFeatures;
1145
1146                 instanceDriver.getPhysicalDeviceFeatures2(physicalDevice, &deviceFeatures);
1147                 if (!protectedFeatures.protectedMemory)
1148                 {
1149                         TCU_THROW(NotSupportedError, "protectedMemory feature is not supported, no queue creation flags available");
1150                 }
1151         }
1152
1153         const deUint32                                                  queueFamilyIndex                = context.getUniversalQueueFamilyIndex();
1154         const deUint32                                                  queueCount                              = 1;
1155         const deUint32                                                  queueIndex                              = 0;
1156         const float                                                             queuePriority                   = 1.0f;
1157         const VkDeviceQueueCreateInfo                   deviceQueueCreateInfo   =
1158         {
1159                 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,     // VkStructureType                                      sType;
1160                 DE_NULL,                                                                        // const void*                                          pNext;
1161                 (VkDeviceQueueCreateFlags)0u,                           // VkDeviceQueueCreateFlags                     flags;
1162                 queueFamilyIndex,                                                       // deUint32                                                     queueFamilyIndex;
1163                 queueCount,                                                                     // deUint32                                                     queueCount;
1164                 &queuePriority,                                                         // const float*                                         pQueuePriorities;
1165         };
1166         VkPhysicalDeviceProtectedMemoryFeatures protectedFeatures               =
1167         {
1168                 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES,    // VkStructureType                              sType;
1169                 DE_NULL,                                                                                                                // void*                                                pNext;
1170                 VK_TRUE                                                                                                                 // VkBool32                                             protectedMemory;
1171         };
1172
1173         VkPhysicalDeviceFeatures                                emptyDeviceFeatures;
1174         deMemset(&emptyDeviceFeatures, 0, sizeof(emptyDeviceFeatures));
1175
1176         const VkPhysicalDeviceFeatures2                 deviceFeatures                  =
1177         {
1178                 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,                                   // VkStructureType                              sType;
1179                 &protectedFeatures,                                                                                             // void*                                                pNext;
1180                 emptyDeviceFeatures                                                                                             // VkPhysicalDeviceFeatures             features;
1181         };
1182
1183         const VkDeviceCreateInfo                                deviceCreateInfo                =
1184         {
1185                 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,           // VkStructureType                                      sType;
1186                 &deviceFeatures,                                                        // const void*                                          pNext;
1187                 (VkDeviceCreateFlags)0u,                                        // VkDeviceCreateFlags                          flags;
1188                 1,                                                                                      // deUint32                                                     queueCreateInfoCount;
1189                 &deviceQueueCreateInfo,                                         // const VkDeviceQueueCreateInfo*       pQueueCreateInfos;
1190                 0,                                                                                      // deUint32                                                     enabledLayerCount;
1191                 DE_NULL,                                                                        // const char* const*                           ppEnabledLayerNames;
1192                 0,                                                                                      // deUint32                                                     enabledExtensionCount;
1193                 DE_NULL,                                                                        // const char* const*                           ppEnabledExtensionNames;
1194                 DE_NULL,                                                                        // const VkPhysicalDeviceFeatures*      pEnabledFeatures;
1195         };
1196         const VkDeviceQueueInfo2                                deviceQueueInfo2                =
1197         {
1198                 VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,          // VkStructureType                                      sType;
1199                 DE_NULL,                                                                        // const void*                                          pNext;
1200                 VK_DEVICE_QUEUE_CREATE_PROTECTED_BIT,           // VkDeviceQueueCreateFlags                     flags;
1201                 queueFamilyIndex,                                                       // deUint32                                                     queueFamilyIndex;
1202                 queueIndex,                                                                     // deUint32                                                     queueIndex;
1203         };
1204
1205         {
1206                 const Unique<VkDevice>          device                                  (createDevice(platformInterface, instance, instanceDriver, physicalDevice, &deviceCreateInfo));
1207                 const DeviceDriver                      deviceDriver                    (platformInterface, instance, device.get());
1208                 const VkQueue                           queue2                                  = getDeviceQueue2(deviceDriver, *device, &deviceQueueInfo2);
1209
1210                 if (queue2 != DE_NULL)
1211                         return tcu::TestStatus::fail("Fail, getDeviceQueue2 should return VK_NULL_HANDLE when flags in VkDeviceQueueCreateInfo and VkDeviceQueueInfo2 are different.");
1212
1213                 const VkQueue                           queue                                   = getDeviceQueue(deviceDriver, *device,  queueFamilyIndex, queueIndex);
1214
1215                 VK_CHECK(deviceDriver.queueWaitIdle(queue));
1216         }
1217
1218         return tcu::TestStatus::pass("Pass");
1219 }
1220
1221 // Allocation tracking utilities
1222 struct  AllocTrack
1223 {
1224         bool                                            active;
1225         bool                                            wasAllocated;
1226         void*                                           alignedStartAddress;
1227         char*                                           actualStartAddress;
1228         size_t                                          requestedSizeBytes;
1229         size_t                                          actualSizeBytes;
1230         VkSystemAllocationScope         allocScope;
1231         deUint64                                        userData;
1232
1233         AllocTrack()
1234                 : active                                (false)
1235                 , wasAllocated                  (false)
1236                 , alignedStartAddress   (DE_NULL)
1237                 , actualStartAddress    (DE_NULL)
1238                 , requestedSizeBytes    (0)
1239                 , actualSizeBytes               (0)
1240                 , allocScope                    (VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)
1241                 , userData(0)                   {}
1242 };
1243
1244 // Global vector to track allocations. This will be resized before each test and emptied after
1245 // However, we have to globally define it so the allocation callback functions work properly
1246 std::vector<AllocTrack> g_allocatedVector;
1247 bool                                    g_intentionalFailEnabled        = false;
1248 deUint32                                g_intenionalFailIndex           = 0;
1249 deUint32                                g_intenionalFailCount           = 0;
1250 size_t                                  g_allocationsCount                      = 0;
1251
1252 void freeAllocTracker (void)
1253 {
1254         g_allocatedVector.clear();
1255         g_allocationsCount = 0;
1256 }
1257
1258 void initAllocTracker (size_t size, deUint32 intentionalFailIndex = (deUint32)~0)
1259 {
1260         if (g_allocatedVector.size() > 0)
1261                 freeAllocTracker();
1262
1263         g_allocatedVector.resize(size);
1264
1265         if (intentionalFailIndex != (deUint32)~0)
1266         {
1267                 g_intentionalFailEnabled        = true;
1268                 g_intenionalFailIndex           = intentionalFailIndex;
1269                 g_intenionalFailCount           = 0;
1270         }
1271         else
1272         {
1273                 g_intentionalFailEnabled        = false;
1274                 g_intenionalFailIndex           = 0;
1275                 g_intenionalFailCount           = 0;
1276         }
1277
1278         g_allocationsCount = 0;
1279 }
1280
1281 bool isAllocTrackerEmpty ()
1282 {
1283         bool success            = true;
1284         bool wasAllocated       = false;
1285
1286         for (deUint32 vectorIdx = 0; vectorIdx < g_allocatedVector.size(); vectorIdx++)
1287         {
1288                 if (g_allocatedVector[vectorIdx].active)
1289                         success = false;
1290                 else if (!wasAllocated && g_allocatedVector[vectorIdx].wasAllocated)
1291                         wasAllocated = true;
1292         }
1293
1294         if (!g_intentionalFailEnabled && !wasAllocated)
1295                 success = false;
1296
1297         return success;
1298 }
1299
1300 VKAPI_ATTR void *VKAPI_CALL allocCallbackFunc (void *pUserData, size_t size, size_t alignment, VkSystemAllocationScope allocationScope)
1301 {
1302         if (g_intentionalFailEnabled)
1303                 if (++g_intenionalFailCount >= g_intenionalFailIndex)
1304                         return DE_NULL;
1305
1306         for (deUint32 vectorIdx = 0; vectorIdx < g_allocatedVector.size(); vectorIdx++)
1307         {
1308                 if (!g_allocatedVector[vectorIdx].active)
1309                 {
1310                         g_allocatedVector[vectorIdx].requestedSizeBytes         = size;
1311                         g_allocatedVector[vectorIdx].actualSizeBytes            = size + (alignment - 1);
1312                         g_allocatedVector[vectorIdx].alignedStartAddress        = DE_NULL;
1313                         g_allocatedVector[vectorIdx].actualStartAddress         = new char[g_allocatedVector[vectorIdx].actualSizeBytes];
1314
1315                         if (g_allocatedVector[vectorIdx].actualStartAddress != DE_NULL)
1316                         {
1317                                 deUint64 addr   =       (deUint64)g_allocatedVector[vectorIdx].actualStartAddress;
1318                                 addr                    +=      (alignment - 1);
1319                                 addr                    &=      ~(alignment - 1);
1320                                 g_allocatedVector[vectorIdx].alignedStartAddress        = (void *)addr;
1321                                 g_allocatedVector[vectorIdx].allocScope                         = allocationScope;
1322                                 g_allocatedVector[vectorIdx].userData                           = (deUint64)pUserData;
1323                                 g_allocatedVector[vectorIdx].active                                     = true;
1324                                 g_allocatedVector[vectorIdx].wasAllocated                       = true;
1325                         }
1326
1327                         g_allocationsCount++;
1328                         return g_allocatedVector[vectorIdx].alignedStartAddress;
1329                 }
1330         }
1331         return DE_NULL;
1332 }
1333
1334 VKAPI_ATTR void VKAPI_CALL freeCallbackFunc (void *pUserData, void *pMemory)
1335 {
1336         DE_UNREF(pUserData);
1337
1338         for (deUint32 vectorIdx = 0; vectorIdx < g_allocatedVector.size(); vectorIdx++)
1339         {
1340                 if (g_allocatedVector[vectorIdx].active && g_allocatedVector[vectorIdx].alignedStartAddress == pMemory)
1341                 {
1342                         delete[] g_allocatedVector[vectorIdx].actualStartAddress;
1343                         g_allocatedVector[vectorIdx].active = false;
1344                         break;
1345                 }
1346         }
1347 }
1348
1349 VKAPI_ATTR void *VKAPI_CALL reallocCallbackFunc (void *pUserData, void *pOriginal, size_t size, size_t alignment, VkSystemAllocationScope allocationScope)
1350 {
1351         if (pOriginal != DE_NULL)
1352         {
1353                 for (deUint32 vectorIdx = 0; vectorIdx < g_allocatedVector.size(); vectorIdx++)
1354                 {
1355                         if (g_allocatedVector[vectorIdx].active && g_allocatedVector[vectorIdx].alignedStartAddress == pOriginal)
1356                         {
1357                                 if (size == 0)
1358                                 {
1359                                         freeCallbackFunc(pUserData, pOriginal);
1360                                         return DE_NULL;
1361                                 }
1362                                 else if (size < g_allocatedVector[vectorIdx].requestedSizeBytes)
1363                                         return pOriginal;
1364                                 else
1365                                 {
1366                                         void *pNew = allocCallbackFunc(pUserData, size, alignment, allocationScope);
1367
1368                                         if (pNew != DE_NULL)
1369                                         {
1370                                                 size_t copySize = size;
1371
1372                                                 if (g_allocatedVector[vectorIdx].requestedSizeBytes < size)
1373                                                         copySize = g_allocatedVector[vectorIdx].requestedSizeBytes;
1374
1375                                                 memcpy(pNew, pOriginal, copySize);
1376                                                 freeCallbackFunc(pUserData, pOriginal);
1377                                         }
1378                                         return pNew;
1379                                 }
1380                         }
1381                 }
1382                 return DE_NULL;
1383         }
1384         else
1385                 return allocCallbackFunc(pUserData, size, alignment, allocationScope);
1386 }
1387
1388 tcu::TestStatus createInstanceDeviceIntentionalAllocFail (Context& context)
1389 {
1390         const PlatformInterface&        vkp                                     = context.getPlatformInterface();
1391         const deUint32                          chosenDevice            = context.getTestContext().getCommandLine().getVKDeviceId() - 1;
1392         VkInstance                                      instance                        = DE_NULL;
1393         VkDevice                                        device                          = DE_NULL;
1394         deUint32                                        physicalDeviceCount     = 0;
1395         deUint32                                        queueFamilyCount        = 0;
1396         deUint32                                        queueFamilyIndex        = 0;
1397         const float                                     queuePriority           = 0.0f;
1398         const VkAllocationCallbacks     allocationCallbacks     =
1399         {
1400                 DE_NULL,                                                                // userData
1401                 allocCallbackFunc,                                              // pfnAllocation
1402                 reallocCallbackFunc,                                    // pfnReallocation
1403                 freeCallbackFunc,                                               // pfnFree
1404                 DE_NULL,                                                                // pfnInternalAllocation
1405                 DE_NULL                                                                 // pfnInternalFree
1406         };
1407         const VkApplicationInfo         appInfo                         =
1408         {
1409                 VK_STRUCTURE_TYPE_APPLICATION_INFO,             // sType
1410                 DE_NULL,                                                                // pNext
1411                 "appName",                                                              // pApplicationName
1412                 0u,                                                                             // applicationVersion
1413                 "engineName",                                                   // pEngineName
1414                 0u,                                                                             // engineVersion
1415                 VK_API_VERSION_1_0                                              // apiVersion
1416         };
1417         const VkInstanceCreateInfo      instanceCreateInfo      =
1418         {
1419                 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
1420                 DE_NULL,                                                                // pNext
1421                 (VkInstanceCreateFlags)0u,                              // flags
1422                 &appInfo,                                                               // pApplicationInfo
1423                 0u,                                                                             // enabledLayerCount
1424                 DE_NULL,                                                                // ppEnabledLayerNames
1425                 0u,                                                                             // enabledExtensionCount
1426                 DE_NULL                                                                 // ppEnabledExtensionNames
1427         };
1428         deUint32                                        failIndex                       = 0;
1429         VkResult                                        result                          = VK_SUCCESS;
1430         size_t                                          max_allowed_alloc       = 0;
1431
1432         do
1433         {
1434                 if (max_allowed_alloc == 0)
1435                 {
1436                         if (result != VK_SUCCESS)
1437                                 return tcu::TestStatus::fail("Could not create instance and device");
1438
1439                         initAllocTracker(99999);
1440                 }
1441                 else
1442                 {
1443                         initAllocTracker(max_allowed_alloc, failIndex++);
1444
1445                         if (failIndex >= static_cast<deUint32>(max_allowed_alloc))
1446                                 return tcu::TestStatus::fail("Out of retries, could not create instance and device");
1447                 }
1448
1449                 result = vkp.createInstance(&instanceCreateInfo, &allocationCallbacks, &instance);
1450
1451                 if (result == VK_ERROR_OUT_OF_HOST_MEMORY)
1452                 {
1453                         if (!isAllocTrackerEmpty())
1454                                 return tcu::TestStatus::fail("Allocations still remain, failed on index " + de::toString(failIndex));
1455
1456                         freeAllocTracker();
1457                         continue;
1458                 }
1459                 else if (result != VK_SUCCESS)
1460                         return tcu::TestStatus::fail("createInstance returned " + de::toString(result));
1461
1462                 const InstanceDriver            instanceDriver  (vkp, instance);
1463                 const InstanceInterface&        vki                             (instanceDriver);
1464
1465                 result = vki.enumeratePhysicalDevices(instance, &physicalDeviceCount, DE_NULL);
1466
1467                 if (result == VK_ERROR_OUT_OF_HOST_MEMORY)
1468                 {
1469                         vki.destroyInstance(instance, &allocationCallbacks);
1470
1471                         if (!isAllocTrackerEmpty())
1472                                 return tcu::TestStatus::fail("Allocations still remain, failed on index " + de::toString(failIndex));
1473
1474                         freeAllocTracker();
1475                         continue;
1476                 }
1477                 else if (result != VK_SUCCESS)
1478                         return tcu::TestStatus::fail("enumeratePhysicalDevices returned " + de::toString(result));
1479
1480                 vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
1481
1482                 result = vki.enumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data());
1483
1484                 if (result == VK_ERROR_OUT_OF_HOST_MEMORY)
1485                 {
1486                         vki.destroyInstance(instance, &allocationCallbacks);
1487
1488                         if (!isAllocTrackerEmpty())
1489                                 return tcu::TestStatus::fail("Allocations still remain, failed on index " + de::toString(failIndex));
1490
1491                         freeAllocTracker();
1492                         continue;
1493                 }
1494                 else if (result != VK_SUCCESS)
1495                         return tcu::TestStatus::fail("enumeratePhysicalDevices returned " + de::toString(result));
1496
1497                 vki.getPhysicalDeviceQueueFamilyProperties(physicalDevices[chosenDevice], &queueFamilyCount, DE_NULL);
1498
1499                 if (queueFamilyCount == 0u)
1500                         return tcu::TestStatus::fail("getPhysicalDeviceQueueFamilyProperties returned zero queue families");
1501
1502                 vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
1503
1504                 vki.getPhysicalDeviceQueueFamilyProperties(physicalDevices[chosenDevice], &queueFamilyCount, queueFamilies.data());
1505
1506                 if (queueFamilyCount == 0u)
1507                         return tcu::TestStatus::fail("getPhysicalDeviceQueueFamilyProperties returned zero queue families");
1508
1509                 for (deUint32 i = 0; i < queueFamilyCount; i++)
1510                 {
1511                         if (queueFamilies[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
1512                         {
1513                                 queueFamilyIndex = i;
1514                                 break;
1515                         }
1516                 }
1517
1518                 const VkDeviceQueueCreateInfo   deviceQueueCreateInfo   =
1519                 {
1520                         VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,     // sType
1521                         DE_NULL,                                                                        // pNext
1522                         (VkDeviceQueueCreateFlags)0u,                           // flags
1523                         queueFamilyIndex,                                                       // queueFamilyIndex
1524                         1u,                                                                                     // queueCount
1525                         &queuePriority                                                          // pQueuePriorities
1526                 };
1527                 const VkDeviceCreateInfo                deviceCreateInfo                =
1528                 {
1529                         VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,           // sType
1530                         DE_NULL,                                                                        // pNext
1531                         (VkDeviceCreateFlags)0u,                                        // flags
1532                         1u,                                                                                     // queueCreateInfoCount
1533                         &deviceQueueCreateInfo,                                         // pQueueCreateInfos
1534                         0u,                                                                                     // enabledLayerCount
1535                         DE_NULL,                                                                        // ppEnabledLayerNames
1536                         0u,                                                                                     // enabledExtensionCount
1537                         DE_NULL,                                                                        // ppEnabledExtensionNames
1538                         DE_NULL                                                                         // pEnabledFeatures
1539                 };
1540
1541                 result = vki.createDevice(physicalDevices[chosenDevice], &deviceCreateInfo, &allocationCallbacks, &device);
1542
1543                 if (result == VK_ERROR_OUT_OF_HOST_MEMORY)
1544                 {
1545                         vki.destroyInstance(instance, &allocationCallbacks);
1546
1547                         if (!isAllocTrackerEmpty())
1548                                 return tcu::TestStatus::fail("Allocations still remain, failed on index " + de::toString(failIndex));
1549
1550                         freeAllocTracker();
1551                         continue;
1552                 }
1553                 else if (result != VK_SUCCESS)
1554                         return tcu::TestStatus::fail("VkCreateDevice returned " + de::toString(result));
1555
1556                 DeviceDriver(vkp, instance, device).destroyDevice(device, &allocationCallbacks);
1557                 vki.destroyInstance(instance, &allocationCallbacks);
1558                 if (max_allowed_alloc == 0)
1559                 {
1560                         max_allowed_alloc       = g_allocationsCount + 100;
1561                         result                          = VK_ERROR_OUT_OF_HOST_MEMORY;
1562                 }
1563                 freeAllocTracker();
1564         }
1565         while (result == VK_ERROR_OUT_OF_HOST_MEMORY);
1566
1567         return tcu::TestStatus::pass("Pass");
1568 }
1569
1570 } // anonymous
1571
1572 tcu::TestCaseGroup* createDeviceInitializationTests (tcu::TestContext& testCtx)
1573 {
1574         de::MovePtr<tcu::TestCaseGroup> deviceInitializationTests (new tcu::TestCaseGroup(testCtx, "device_init", "Device Initialization Tests"));
1575
1576         addFunctionCase(deviceInitializationTests.get(), "create_instance_name_version",                                        "", createInstanceTest);
1577         addFunctionCase(deviceInitializationTests.get(), "create_instance_invalid_api_version",                         "", createInstanceWithInvalidApiVersionTest);
1578         addFunctionCase(deviceInitializationTests.get(), "create_instance_null_appinfo",                                        "", createInstanceWithNullApplicationInfoTest);
1579         addFunctionCase(deviceInitializationTests.get(), "create_instance_unsupported_extensions",                      "", createInstanceWithUnsupportedExtensionsTest);
1580         addFunctionCase(deviceInitializationTests.get(), "create_instance_extension_name_abuse",                        "", createInstanceWithExtensionNameAbuseTest);
1581         addFunctionCase(deviceInitializationTests.get(), "create_instance_layer_name_abuse",                            "", createInstanceWithLayerNameAbuseTest);
1582         addFunctionCase(deviceInitializationTests.get(), "create_device",                                                                       "", createDeviceTest);
1583         addFunctionCase(deviceInitializationTests.get(), "create_multiple_devices",                                                     "", createMultipleDevicesTest);
1584         addFunctionCase(deviceInitializationTests.get(), "create_device_unsupported_extensions",                        "", createDeviceWithUnsupportedExtensionsTest);
1585         addFunctionCase(deviceInitializationTests.get(), "create_device_various_queue_counts",                          "", createDeviceWithVariousQueueCountsTest);
1586         addFunctionCase(deviceInitializationTests.get(), "create_device_features2",                                                     "", createDeviceFeatures2Test);
1587         addFunctionCase(deviceInitializationTests.get(), "create_device_unsupported_features",                          "", createDeviceWithUnsupportedFeaturesTest);
1588         addFunctionCase(deviceInitializationTests.get(), "create_device_queue2",                                                        "", createDeviceQueue2Test);
1589         addFunctionCase(deviceInitializationTests.get(), "create_device_queue2_unmatched_flags",                        "", createDeviceQueue2UnmatchedFlagsTest);
1590         addFunctionCase(deviceInitializationTests.get(), "create_instance_device_intentional_alloc_fail",       "", createInstanceDeviceIntentionalAllocFail);
1591
1592         return deviceInitializationTests.release();
1593 }
1594
1595 } // api
1596 } // vkt