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
32namespace vk
33{
34
35std::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
45bool readApplicationParameters (std::vector<VkApplicationParametersEXT>& appParams, const tcu::CommandLine& cmdLine, const bool readInstanceAppParams)
46{
47	const char* appParamsInputFilePath = cmdLine.getAppParamsInputFilePath();
48
49	if (appParamsInputFilePath == DE_NULL)
50		return false;
51
52	std::ifstream							file(appParamsInputFilePath);
53	std::vector<std::string>				lines;
54	std::vector<VkApplicationParametersEXT>	tmpAppParams;
55
56	if (file.is_open())
57	{
58		std::string line;
59
60		while (std::getline(file, line))
61			lines.push_back(line);
62
63		file.close();
64	}
65	else
66	{
67		TCU_THROW(InternalError, "Application parameters input file not found from --deqp-app-params-input-file");
68		return false;
69	}
70
71	for (const std::string& line : lines)
72	{
73		if (line.empty())
74			continue;
75
76		std::stringstream			sstream(line);
77		std::string					token;
78		std::vector<std::string>	tokens;
79
80		while (std::getline(sstream, token, ','))
81			tokens.push_back(trim(token));
82
83		if (tokens[0] != "instance" && tokens[0] != "device")
84		{
85			TCU_THROW(InternalError, "Invalid create type from --deqp-app-params-input-file");
86			return false;
87		}
88
89		if ((tokens[0] == "instance" && readInstanceAppParams) || (tokens[0] == "device" && !readInstanceAppParams))
90		{
91			if (tokens.size() == 5)
92			{
93				const VkApplicationParametersEXT appParam =
94				{
95					VK_STRUCTURE_TYPE_APPLICATION_PARAMETERS_EXT,				// sType
96					DE_NULL,													// pNext
97					static_cast<deUint32>(std::stoul(tokens[1], nullptr, 16)),	// vendorID
98					static_cast<deUint32>(std::stoul(tokens[2], nullptr, 16)),	// deviceID
99					static_cast<deUint32>(std::stoul(tokens[3], nullptr, 16)),	// key
100					static_cast<deUint64>(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} // vk
128
129#endif // CTS_USES_VULKANSC