xref: /aosp_15_r20/external/deqp/external/vulkancts/framework/vulkan/vkAppParamsUtil.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 /*-------------------------------------------------------------------------
2  * Vulkan CTS Framework
3  * --------------------
4  *
5  * Copyright (c) 2022 NVIDIA CORPORATION, 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 Vulkan SC utilities
22  *//*--------------------------------------------------------------------*/
23 
24 #include "vkAppParamsUtil.hpp"
25 
26 #include <fstream>
27 #include <string>
28 #include <sstream>
29 
30 #ifdef CTS_USES_VULKANSC
31 
32 namespace vk
33 {
34 
trim(const std::string & original)35 std::string trim(const std::string &original)
36 {
37     static const std::string whiteSigns = " \t";
38     const auto beg                      = original.find_first_not_of(whiteSigns);
39     if (beg == std::string::npos)
40         return std::string();
41     const auto end = original.find_last_not_of(whiteSigns);
42     return original.substr(beg, end - beg + 1);
43 }
44 
readApplicationParameters(std::vector<VkApplicationParametersEXT> & appParams,const tcu::CommandLine & cmdLine,const bool readInstanceAppParams)45 bool readApplicationParameters(std::vector<VkApplicationParametersEXT> &appParams, const tcu::CommandLine &cmdLine,
46                                const bool readInstanceAppParams)
47 {
48     const char *appParamsInputFilePath = cmdLine.getAppParamsInputFilePath();
49 
50     if (appParamsInputFilePath == DE_NULL)
51         return false;
52 
53     std::ifstream file(appParamsInputFilePath);
54     std::vector<std::string> lines;
55     std::vector<VkApplicationParametersEXT> tmpAppParams;
56 
57     if (file.is_open())
58     {
59         std::string line;
60 
61         while (std::getline(file, line))
62             lines.push_back(line);
63 
64         file.close();
65     }
66     else
67     {
68         TCU_THROW(InternalError, "Application parameters input file not found from --deqp-app-params-input-file");
69         return false;
70     }
71 
72     for (const std::string &line : lines)
73     {
74         if (line.empty())
75             continue;
76 
77         std::stringstream sstream(line);
78         std::string token;
79         std::vector<std::string> tokens;
80 
81         while (std::getline(sstream, token, ','))
82             tokens.push_back(trim(token));
83 
84         if (tokens[0] != "instance" && tokens[0] != "device")
85         {
86             TCU_THROW(InternalError, "Invalid create type from --deqp-app-params-input-file");
87             return false;
88         }
89 
90         if ((tokens[0] == "instance" && readInstanceAppParams) || (tokens[0] == "device" && !readInstanceAppParams))
91         {
92             if (tokens.size() == 5)
93             {
94                 const VkApplicationParametersEXT appParam = {
95                     VK_STRUCTURE_TYPE_APPLICATION_PARAMETERS_EXT,              // sType
96                     DE_NULL,                                                   // pNext
97                     static_cast<uint32_t>(std::stoul(tokens[1], nullptr, 16)), // vendorID
98                     static_cast<uint32_t>(std::stoul(tokens[2], nullptr, 16)), // deviceID
99                     static_cast<uint32_t>(std::stoul(tokens[3], nullptr, 16)), // key
100                     static_cast<uint64_t>(std::stoul(tokens[4], nullptr, 16))  // value
101                 };
102 
103                 tmpAppParams.push_back(appParam);
104             }
105             else
106             {
107                 TCU_THROW(InternalError, "Invalid input format from --deqp-app-params-input-file");
108                 return false;
109             }
110         }
111     }
112 
113     if (tmpAppParams.empty())
114         return false;
115 
116     appParams = tmpAppParams;
117 
118     for (size_t ndx = 0; ndx < appParams.size(); ++ndx)
119     {
120         if (ndx != appParams.size() - 1)
121             appParams[ndx].pNext = &appParams[ndx + 1];
122     }
123 
124     return true;
125 }
126 
127 } // namespace vk
128 
129 #endif // CTS_USES_VULKANSC