31b66fe3392e13136fe6b3bbc3459ed009a6c1c2
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / api / vktApiToolingInfoTests.cpp
1 /*-------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2019 Advanced Micro Devices, Inc.
6 * Copyright (c) 2019 The Khronos Group Inc.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 *//*!
21 * \file
22 * \brief VK_EXT_tooling_info tests
23 *//*--------------------------------------------------------------------*/
24
25 #include "vktCustomInstancesDevices.hpp"
26 #include "vkDeviceUtil.hpp"
27 #include "vktApiToolingInfoTests.hpp"
28 #include "vktTestGroupUtil.hpp"
29 #include "vktTestCaseUtil.hpp"
30 #include "vkQueryUtil.hpp"
31 #include "vkStrUtil.hpp"
32 #include "vkTypeUtil.hpp"
33 #include "tcuTestLog.hpp"
34 #include <iostream>
35 #include <string>
36 #include <vector>
37
38 #include <string.h>
39
40 using namespace vk;
41
42 namespace vkt
43 {
44 namespace api
45 {
46 namespace
47 {
48
49 bool validateToolPurposeFlagBits (const VkToolPurposeFlagsEXT purposes)
50 {
51         const VkToolPurposeFlagsEXT validPurposes =     VK_TOOL_PURPOSE_VALIDATION_BIT_EXT                      |
52                                                                                                 VK_TOOL_PURPOSE_PROFILING_BIT_EXT                       |
53                                                                                                 VK_TOOL_PURPOSE_TRACING_BIT_EXT                         |
54                                                                                                 VK_TOOL_PURPOSE_ADDITIONAL_FEATURES_BIT_EXT     |
55                                                                                                 VK_TOOL_PURPOSE_MODIFYING_FEATURES_BIT_EXT      |
56                                                                                                 VK_TOOL_PURPOSE_DEBUG_REPORTING_BIT_EXT         |
57                                                                                                 VK_TOOL_PURPOSE_DEBUG_MARKERS_BIT_EXT;
58         return (purposes | validPurposes) == validPurposes;
59 }
60
61 void checkSupport (Context& context)
62 {
63         context.requireDeviceFunctionality("VK_EXT_tooling_info");
64 }
65
66 CustomInstance createCustomInstance (Context& context, bool allowLayers)
67 {
68         std::vector<const char*>                enabledLayers;
69         std::vector<std::string>                enabledLayersStr;
70         const std::vector<std::string>  enabledExtensions;
71
72         const deUint32                                  apiVersion      = context.getUsedApiVersion();
73         const vk::PlatformInterface&    vkp                     = context.getPlatformInterface();
74
75         if (allowLayers)
76         {
77                 enabledLayers = getValidationLayers(context.getPlatformInterface());
78                 enabledLayersStr = std::vector<std::string>(begin(enabledLayers), end(enabledLayers));
79         }
80
81         Move<VkInstance> instance = vk::createDefaultInstance(vkp, apiVersion, enabledLayersStr, enabledExtensions, DE_NULL);
82         return CustomInstance(context, instance, allowLayers);
83 }
84
85 bool checkToolsProperties (Context& context, const std::vector<VkPhysicalDeviceToolPropertiesEXT>& deviceToolPropertiesEXTArray)
86 {
87         tcu::TestLog&   testLog = context.getTestContext().getLog();
88         bool                    result  = true;
89
90         for (size_t i = 0; i < deviceToolPropertiesEXTArray.size(); ++i)
91         {
92                 size_t nameSize         = strnlen(deviceToolPropertiesEXTArray[i].name, VK_MAX_EXTENSION_NAME_SIZE);
93                 size_t versionSize      = strnlen(deviceToolPropertiesEXTArray[i].version, VK_MAX_EXTENSION_NAME_SIZE);
94                 size_t descSize         = strnlen(deviceToolPropertiesEXTArray[i].description, VK_MAX_DESCRIPTION_SIZE);
95                 size_t layerSize        = strnlen(deviceToolPropertiesEXTArray[i].layer, VK_MAX_EXTENSION_NAME_SIZE);
96
97                 result = result && (deviceToolPropertiesEXTArray[i].sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TOOL_PROPERTIES_EXT);
98                 result = result && validateToolPurposeFlagBits(deviceToolPropertiesEXTArray[i].purposes);
99                 result = result && ((nameSize > 0)              && (nameSize < VK_MAX_EXTENSION_NAME_SIZE));
100                 result = result && ((versionSize > 0)   && (versionSize < VK_MAX_EXTENSION_NAME_SIZE));
101                 result = result && ((descSize > 0)              && (descSize < VK_MAX_DESCRIPTION_SIZE));
102                 result = result && ((layerSize == 0)    || (layerSize < VK_MAX_EXTENSION_NAME_SIZE));
103
104                 if (result == false)
105                 {
106                         testLog << tcu::TestLog::Message << "Tool validation failed" << tcu::TestLog::EndMessage;
107                         testLog << tcu::TestLog::Message << "Tool name: " << deviceToolPropertiesEXTArray[i].name << tcu::TestLog::EndMessage;
108                         testLog << tcu::TestLog::Message << "Version: " << deviceToolPropertiesEXTArray[i].version << tcu::TestLog::EndMessage;
109                         testLog << tcu::TestLog::Message << "Description: " << deviceToolPropertiesEXTArray[i].description << tcu::TestLog::EndMessage;
110                         testLog << tcu::TestLog::Message << "Purposes: " << getToolPurposeFlagsEXTStr(deviceToolPropertiesEXTArray[i].purposes) << tcu::TestLog::EndMessage;
111                         if (layerSize > 0)
112                         {
113                                 testLog << tcu::TestLog::Message << "Corresponding Layer: " << deviceToolPropertiesEXTArray[i].layer << tcu::TestLog::EndMessage;
114                         }
115
116                         break;
117                 }
118         }
119         return result;
120 }
121
122 tcu::TestStatus validateGetter (Context& context)
123 {
124         tcu::TestLog& testLog = context.getTestContext().getLog();
125
126         VkResult result         = VK_SUCCESS;
127         deUint32 toolCount      = 0;
128
129         result = context.getInstanceInterface().getPhysicalDeviceToolPropertiesEXT(context.getPhysicalDevice(), &toolCount, DE_NULL);
130
131         if(result != VK_SUCCESS)
132         {
133                 testLog << tcu::TestLog::Message << "getPhysicalDeviceToolPropertiesEXT wrong result code" << tcu::TestLog::EndMessage;
134                 return tcu::TestStatus::fail("Fail");
135         }
136
137         if (toolCount > 0)
138         {
139                 deUint32 toolCountSecondCall = toolCount;
140
141                 std::vector<VkPhysicalDeviceToolPropertiesEXT>  deviceToolPropertiesEXTArray(toolCountSecondCall);
142
143                 result = context.getInstanceInterface().getPhysicalDeviceToolPropertiesEXT(context.getPhysicalDevice(), &toolCountSecondCall, &deviceToolPropertiesEXTArray[0]);
144
145                 if (result != VK_SUCCESS)
146                 {
147                         testLog << tcu::TestLog::Message << "getPhysicalDeviceToolPropertiesEXT wrong result code" << tcu::TestLog::EndMessage;
148                         return tcu::TestStatus::fail("Fail");
149                 }
150
151                 if (toolCountSecondCall != toolCount)
152                 {
153                         testLog << tcu::TestLog::Message << "Got different tools count on the second call" << tcu::TestLog::EndMessage;
154                         return tcu::TestStatus::fail("Fail");
155                 }
156
157                 toolCountSecondCall++;
158
159                 deviceToolPropertiesEXTArray.resize(toolCountSecondCall);
160
161                 result = context.getInstanceInterface().getPhysicalDeviceToolPropertiesEXT(context.getPhysicalDevice(), &toolCountSecondCall, &deviceToolPropertiesEXTArray[0]);
162
163                 if (result != VK_SUCCESS)
164                 {
165                         testLog << tcu::TestLog::Message << "getPhysicalDeviceToolPropertiesEXT wrong result code" << tcu::TestLog::EndMessage;
166                         return tcu::TestStatus::fail("Fail");
167                 }
168
169                 if (toolCountSecondCall != toolCount)
170                 {
171                         testLog << tcu::TestLog::Message << "Bigger array causes an error" << tcu::TestLog::EndMessage;
172                         return tcu::TestStatus::fail("Fail");
173                 }
174
175                 toolCountSecondCall = 0;
176
177                 result = context.getInstanceInterface().getPhysicalDeviceToolPropertiesEXT(context.getPhysicalDevice(), &toolCountSecondCall, &deviceToolPropertiesEXTArray[0]);
178
179                 if (result != VK_INCOMPLETE)
180                 {
181                         testLog << tcu::TestLog::Message << "getPhysicalDeviceToolPropertiesEXT wrong result code" << tcu::TestLog::EndMessage;
182                         return tcu::TestStatus::fail("Fail");
183                 }
184
185                 if (toolCountSecondCall != 0)
186                 {
187                         testLog << tcu::TestLog::Message << "Zero array causes an error" << tcu::TestLog::EndMessage;
188                         return tcu::TestStatus::fail("Fail");
189                 }
190         }
191
192         if (toolCount > 1)
193         {
194                 deUint32 toolCountSecondCall = toolCount / 2;
195
196                 std::vector<VkPhysicalDeviceToolPropertiesEXT>  deviceToolPropertiesEXTArray(toolCountSecondCall);
197
198                 result = context.getInstanceInterface().getPhysicalDeviceToolPropertiesEXT(context.getPhysicalDevice(), &toolCountSecondCall, &deviceToolPropertiesEXTArray[0]);
199
200                 if (result != VK_INCOMPLETE)
201                 {
202                         testLog << tcu::TestLog::Message << "getPhysicalDeviceToolPropertiesEXT wrong result code" << tcu::TestLog::EndMessage;
203                         return tcu::TestStatus::fail("Fail");
204                 }
205
206                 if (toolCountSecondCall != (toolCount / 2))
207                 {
208                         testLog << tcu::TestLog::Message << "Smaller array causes an error" << tcu::TestLog::EndMessage;
209                         return tcu::TestStatus::fail("Fail");
210                 }
211         }
212
213         return tcu::TestStatus::pass("Pass");
214 }
215
216 tcu::TestStatus validateToolsProperties (Context& context)
217 {
218         deUint32 toolCount      = 0;
219
220         VK_CHECK(context.getInstanceInterface().getPhysicalDeviceToolPropertiesEXT(context.getPhysicalDevice(), &toolCount, DE_NULL));
221
222         if (toolCount > 0)
223         {
224                 std::vector<VkPhysicalDeviceToolPropertiesEXT>  deviceToolPropertiesEXTArray(toolCount);
225
226                 VK_CHECK(context.getInstanceInterface().getPhysicalDeviceToolPropertiesEXT(context.getPhysicalDevice(), &toolCount, &deviceToolPropertiesEXTArray[0]));
227
228                 if (checkToolsProperties(context, deviceToolPropertiesEXTArray) == false)
229                         return tcu::TestStatus::fail("Fail");
230         }
231
232         return tcu::TestStatus::pass("Pass");
233 }
234
235 tcu::TestStatus validateInstanceLayers (Context& context)
236 {
237         const std::vector<const char*>  layers                  = getValidationLayers(context.getPlatformInterface());
238         bool                                                    qualityWarning  = false;
239
240         {
241                 deUint32                        toolCount               = 0;
242                 CustomInstance          instance                (createCustomInstance(context, true));
243                 VkPhysicalDevice        physicalDevice  = chooseDevice(instance.getDriver(), instance, context.getTestContext().getCommandLine());
244
245                 VK_CHECK(instance.getDriver().getPhysicalDeviceToolPropertiesEXT(physicalDevice, &toolCount, DE_NULL));
246
247                 if (toolCount < layers.size())
248                         qualityWarning = true;
249
250                 if (toolCount > 0)
251                 {
252                         std::vector<VkPhysicalDeviceToolPropertiesEXT>  deviceToolPropertiesEXTArray(toolCount);
253
254                         VK_CHECK(context.getInstanceInterface().getPhysicalDeviceToolPropertiesEXT(physicalDevice, &toolCount, &deviceToolPropertiesEXTArray[0]));
255
256                         if (checkToolsProperties(context, deviceToolPropertiesEXTArray) == false)
257                                 return tcu::TestStatus::fail("Fail");
258
259                         for (size_t layerNdx = 0; layerNdx < layers.size(); ++layerNdx)
260                         {
261                                 deUint32 count = 0u;
262
263                                 for (deUint32 toolNdx = 0; toolNdx < toolCount; ++toolNdx)
264                                 {
265                                         if (strcmp(layers[layerNdx], deviceToolPropertiesEXTArray[toolNdx].layer) == 0)
266                                                 count++;
267                                 }
268
269                                 if (count != 1)
270                                 {
271                                         qualityWarning = true;
272                                         break;
273                                 }
274                         }
275                 }
276         }
277
278         {
279                 deUint32                        toolCount               = 0;
280                 CustomInstance          instance                (createCustomInstance(context, false));
281                 VkPhysicalDevice        physicalDevice  = chooseDevice(instance.getDriver(), instance, context.getTestContext().getCommandLine());
282
283                 VK_CHECK(instance.getDriver().getPhysicalDeviceToolPropertiesEXT(physicalDevice, &toolCount, DE_NULL));
284
285                 if (toolCount > 0)
286                 {
287                         std::vector<VkPhysicalDeviceToolPropertiesEXT>  deviceToolPropertiesEXTArray(toolCount);
288
289                         VK_CHECK(context.getInstanceInterface().getPhysicalDeviceToolPropertiesEXT(physicalDevice, &toolCount, &deviceToolPropertiesEXTArray[0]));
290
291                         if (checkToolsProperties(context, deviceToolPropertiesEXTArray) == false)
292                                 return tcu::TestStatus::fail("Fail");
293
294                         for (size_t layerNdx = 0; layerNdx < layers.size(); ++layerNdx)
295                         {
296                                 for (deUint32 toolNdx = 0; toolNdx < toolCount; ++toolNdx)
297                                 {
298                                         if (strcmp(layers[layerNdx], deviceToolPropertiesEXTArray[toolNdx].layer) == 0)
299                                         {
300                                                 qualityWarning  = true;
301                                                 layerNdx                = layers.size();
302                                                 break;
303                                         }
304                                 }
305                         }
306                 }
307         }
308
309         if (qualityWarning)
310         {
311                 return tcu::TestStatus(QP_TEST_RESULT_QUALITY_WARNING, "Layers have been reported wrong");
312         }
313         else
314         {
315                 return tcu::TestStatus::pass("Pass");
316         }
317 }
318
319 void createTestCases (tcu::TestCaseGroup* group)
320 {
321         addFunctionCase(group, "validate_getter", "Validate getPhysicalDeviceToolPropertiesEXT", checkSupport, validateGetter);
322         addFunctionCase(group, "validate_tools_properties","Validate tools properties", checkSupport, validateToolsProperties);
323         addFunctionCase(group, "validate_instance_layers", "Validate instance layers", checkSupport, validateInstanceLayers);
324 }
325
326 } // anonymous
327
328 tcu::TestCaseGroup*     createToolingInfoTests (tcu::TestContext& testCtx)
329 {
330         return createTestGroup(testCtx, "tooling_info", "VK_EXT_tooling_info tests", createTestCases);
331 }
332
333 } // api
334 } // vkt