1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7  * Copyright (c) 2016 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Random uniform block layout case.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "vktRandomUniformBlockCase.hpp"
27 #include "deRandom.hpp"
28 
29 namespace vkt
30 {
31 namespace ubo
32 {
33 
34 namespace
35 {
36 
genName(char first,char last,int ndx)37 static std::string genName (char first, char last, int ndx)
38 {
39 	std::string	str			= "";
40 	int			alphabetLen	= last - first + 1;
41 
42 	while (ndx > alphabetLen)
43 	{
44 		str.insert(str.begin(), (char)(first + ((ndx - 1) % alphabetLen)));
45 		ndx = (ndx - 1) / alphabetLen;
46 	}
47 
48 	str.insert(str.begin(), (char)(first + (ndx % (alphabetLen + 1)) - 1));
49 
50 	return str;
51 }
52 
53 } // anonymous
54 
RandomUniformBlockCase(tcu::TestContext & testCtx,const std::string & name,const std::string & description,BufferMode bufferMode,deUint32 features,deUint32 seed)55 RandomUniformBlockCase::RandomUniformBlockCase (tcu::TestContext&	testCtx,
56 												const std::string&	name,
57 												const std::string&	description,
58 												BufferMode			bufferMode,
59 												deUint32			features,
60 												deUint32			seed)
61 	: UniformBlockCase		(testCtx, name, description, bufferMode, LOAD_FULL_MATRIX, (features & FEATURE_OUT_OF_ORDER_OFFSETS) != 0u)
62 	, m_features			(features)
63 	, m_maxVertexBlocks		((features & FEATURE_VERTEX_BLOCKS)		? 4 : 0)
64 	, m_maxFragmentBlocks	((features & FEATURE_FRAGMENT_BLOCKS)	? 4 : 0)
65 	, m_maxSharedBlocks		((features & FEATURE_SHARED_BLOCKS)		? 4 : 0)
66 	, m_maxInstances		((features & FEATURE_INSTANCE_ARRAYS)	? 3 : 0)
67 	, m_maxArrayLength		((features & FEATURE_ARRAYS)			? 8 : 0)
68 	, m_maxStructDepth		((features & FEATURE_STRUCTS)			? 2 : 0)
69 	, m_maxBlockMembers		(5)
70 	, m_maxStructMembers	(4)
71 	, m_seed				(seed)
72 	, m_blockNdx			(1)
73 	, m_uniformNdx			(1)
74 	, m_structNdx			(1)
75 {
76 	de::Random rnd(m_seed);
77 
78 	int numShared		= m_maxSharedBlocks				> 0	? rnd.getInt(1, m_maxSharedBlocks)				: 0;
79 	int numVtxBlocks	= m_maxVertexBlocks-numShared	> 0	? rnd.getInt(1, m_maxVertexBlocks - numShared)	: 0;
80 	int	numFragBlocks	= m_maxFragmentBlocks-numShared	> 0 ? rnd.getInt(1, m_maxFragmentBlocks - numShared): 0;
81 
82 	for (int ndx = 0; ndx < numShared; ndx++)
83 		generateBlock(rnd, DECLARE_VERTEX | DECLARE_FRAGMENT);
84 
85 	for (int ndx = 0; ndx < numVtxBlocks; ndx++)
86 		generateBlock(rnd, DECLARE_VERTEX);
87 
88 	for (int ndx = 0; ndx < numFragBlocks; ndx++)
89 		generateBlock(rnd, DECLARE_FRAGMENT);
90 
91 	init();
92 }
93 
generateBlock(de::Random & rnd,deUint32 layoutFlags)94 void RandomUniformBlockCase::generateBlock (de::Random& rnd, deUint32 layoutFlags)
95 {
96 	DE_ASSERT(m_blockNdx <= 'z' - 'a');
97 
98 	const float		instanceArrayWeight	= 0.3f;
99 	UniformBlock&	block				= m_interface.allocBlock(std::string("Block") + (char)('A' + m_blockNdx));
100 	int				numInstances		= (m_maxInstances > 0 && rnd.getFloat() < instanceArrayWeight) ? rnd.getInt(0, m_maxInstances) : 0;
101 	int				numUniforms			= rnd.getInt(1, m_maxBlockMembers);
102 
103 	if (numInstances > 0)
104 		block.setArraySize(numInstances);
105 
106 	if (numInstances > 0 || rnd.getBool())
107 		block.setInstanceName(std::string("block") + (char)('A' + m_blockNdx));
108 
109 	// Layout flag candidates.
110 	std::vector<deUint32> layoutFlagCandidates;
111 	layoutFlagCandidates.push_back(0);
112 
113 	if (m_features & FEATURE_STD140_LAYOUT)
114 		layoutFlagCandidates.push_back(LAYOUT_STD140);
115 
116 	if (m_features & FEATURE_STD430_LAYOUT)
117 		layoutFlagCandidates.push_back(LAYOUT_STD430);
118 
119 	if (m_features & FEATURE_SCALAR_LAYOUT)
120 		layoutFlagCandidates.push_back(LAYOUT_SCALAR);
121 
122 	if (m_features & FEATURE_16BIT_STORAGE)
123 		layoutFlags |= LAYOUT_16BIT_STORAGE;
124 
125 	if (m_features & FEATURE_8BIT_STORAGE)
126 		layoutFlags |= LAYOUT_8BIT_STORAGE;
127 
128 	layoutFlags |= rnd.choose<deUint32>(layoutFlagCandidates.begin(), layoutFlagCandidates.end());
129 
130 	if (m_features & FEATURE_MATRIX_LAYOUT)
131 	{
132 		static const deUint32 matrixCandidates[] = { 0, LAYOUT_ROW_MAJOR, LAYOUT_COLUMN_MAJOR };
133 		layoutFlags |= rnd.choose<deUint32>(&matrixCandidates[0], &matrixCandidates[DE_LENGTH_OF_ARRAY(matrixCandidates)]);
134 	}
135 
136 	block.setFlags(layoutFlags);
137 
138 	for (int ndx = 0; ndx < numUniforms; ndx++)
139 		generateUniform(rnd, block);
140 
141 	m_blockNdx += 1;
142 }
143 
generateUniform(de::Random & rnd,UniformBlock & block)144 void RandomUniformBlockCase::generateUniform (de::Random& rnd, UniformBlock& block)
145 {
146 	const float		unusedVtxWeight		= 0.15f;
147 	const float		unusedFragWeight	= 0.15f;
148 	bool			unusedOk			= (m_features & FEATURE_UNUSED_UNIFORMS) != 0;
149 	deUint32		flags				= 0;
150 	std::string		name				= genName('a', 'z', m_uniformNdx);
151 	VarType			type				= generateType(rnd, 0, true);
152 
153 	flags |= (unusedOk && rnd.getFloat() < unusedVtxWeight)		? UNUSED_VERTEX		: 0;
154 	flags |= (unusedOk && rnd.getFloat() < unusedFragWeight)	? UNUSED_FRAGMENT	: 0;
155 
156 	block.addUniform(Uniform(name, type, flags));
157 
158 	m_uniformNdx += 1;
159 }
160 
generateType(de::Random & rnd,int typeDepth,bool arrayOk)161 VarType RandomUniformBlockCase::generateType (de::Random& rnd, int typeDepth, bool arrayOk)
162 {
163 	const float structWeight	= 0.1f;
164 	const float arrayWeight		= 0.1f;
165 
166 	if (typeDepth < m_maxStructDepth && rnd.getFloat() < structWeight)
167 	{
168 		const float				unusedVtxWeight		= 0.15f;
169 		const float				unusedFragWeight	= 0.15f;
170 		bool					unusedOk			= (m_features & FEATURE_UNUSED_MEMBERS) != 0;
171 		std::vector<VarType>	memberTypes;
172 		int						numMembers = rnd.getInt(1, m_maxStructMembers);
173 
174 		// Generate members first so nested struct declarations are in correct order.
175 		for (int ndx = 0; ndx < numMembers; ndx++)
176 			memberTypes.push_back(generateType(rnd, typeDepth+1, true));
177 
178 		StructType& structType = m_interface.allocStruct(std::string("s") + genName('A', 'Z', m_structNdx));
179 		m_structNdx += 1;
180 
181 		DE_ASSERT(numMembers <= 'Z' - 'A');
182 		for (int ndx = 0; ndx < numMembers; ndx++)
183 		{
184 			deUint32 flags = 0;
185 
186 			flags |= (unusedOk && rnd.getFloat() < unusedVtxWeight)		? UNUSED_VERTEX		: 0;
187 			flags |= (unusedOk && rnd.getFloat() < unusedFragWeight)	? UNUSED_FRAGMENT	: 0;
188 
189 			structType.addMember(std::string("m") + (char)('A' + ndx), memberTypes[ndx], flags);
190 		}
191 
192 		return VarType(&structType, m_shuffleUniformMembers ? static_cast<deUint32>(LAYOUT_OFFSET) : 0u);
193 	}
194 	else if (m_maxArrayLength > 0 && arrayOk && rnd.getFloat() < arrayWeight)
195 	{
196 		const bool	arraysOfArraysOk	= (m_features & FEATURE_ARRAYS_OF_ARRAYS) != 0;
197 		const int	arrayLength			= rnd.getInt(1, m_maxArrayLength);
198 		VarType		elementType			= generateType(rnd, typeDepth, arraysOfArraysOk);
199 		return VarType(elementType, arrayLength);
200 	}
201 	else
202 	{
203 		std::vector<glu::DataType> typeCandidates;
204 
205 		typeCandidates.push_back(glu::TYPE_FLOAT);
206 		typeCandidates.push_back(glu::TYPE_INT);
207 		typeCandidates.push_back(glu::TYPE_UINT);
208 		typeCandidates.push_back(glu::TYPE_BOOL);
209 
210 		if (m_features & FEATURE_16BIT_STORAGE) {
211 			typeCandidates.push_back(glu::TYPE_UINT16);
212 			typeCandidates.push_back(glu::TYPE_INT16);
213 			typeCandidates.push_back(glu::TYPE_FLOAT16);
214 		}
215 
216 		if (m_features & FEATURE_8BIT_STORAGE) {
217 			typeCandidates.push_back(glu::TYPE_UINT8);
218 			typeCandidates.push_back(glu::TYPE_INT8);
219 		}
220 
221 		if (m_features & FEATURE_VECTORS)
222 		{
223 			typeCandidates.push_back(glu::TYPE_FLOAT_VEC2);
224 			typeCandidates.push_back(glu::TYPE_FLOAT_VEC3);
225 			typeCandidates.push_back(glu::TYPE_FLOAT_VEC4);
226 			typeCandidates.push_back(glu::TYPE_INT_VEC2);
227 			typeCandidates.push_back(glu::TYPE_INT_VEC3);
228 			typeCandidates.push_back(glu::TYPE_INT_VEC4);
229 			typeCandidates.push_back(glu::TYPE_UINT_VEC2);
230 			typeCandidates.push_back(glu::TYPE_UINT_VEC3);
231 			typeCandidates.push_back(glu::TYPE_UINT_VEC4);
232 			typeCandidates.push_back(glu::TYPE_BOOL_VEC2);
233 			typeCandidates.push_back(glu::TYPE_BOOL_VEC3);
234 			typeCandidates.push_back(glu::TYPE_BOOL_VEC4);
235 			if (m_features & FEATURE_16BIT_STORAGE)
236 			{
237 				typeCandidates.push_back(glu::TYPE_FLOAT16_VEC2);
238 				typeCandidates.push_back(glu::TYPE_FLOAT16_VEC3);
239 				typeCandidates.push_back(glu::TYPE_FLOAT16_VEC4);
240 				typeCandidates.push_back(glu::TYPE_INT16_VEC2);
241 				typeCandidates.push_back(glu::TYPE_INT16_VEC3);
242 				typeCandidates.push_back(glu::TYPE_INT16_VEC4);
243 				typeCandidates.push_back(glu::TYPE_UINT16_VEC2);
244 				typeCandidates.push_back(glu::TYPE_UINT16_VEC3);
245 				typeCandidates.push_back(glu::TYPE_UINT16_VEC4);
246 			}
247 			if (m_features & FEATURE_8BIT_STORAGE)
248 			{
249 				typeCandidates.push_back(glu::TYPE_INT8_VEC2);
250 				typeCandidates.push_back(glu::TYPE_INT8_VEC3);
251 				typeCandidates.push_back(glu::TYPE_INT8_VEC4);
252 				typeCandidates.push_back(glu::TYPE_UINT8_VEC2);
253 				typeCandidates.push_back(glu::TYPE_UINT8_VEC3);
254 				typeCandidates.push_back(glu::TYPE_UINT8_VEC4);
255 			}
256 		}
257 
258 		if (m_features & FEATURE_MATRICES)
259 		{
260 			typeCandidates.push_back(glu::TYPE_FLOAT_MAT2);
261 			typeCandidates.push_back(glu::TYPE_FLOAT_MAT2X3);
262 			typeCandidates.push_back(glu::TYPE_FLOAT_MAT3X2);
263 			typeCandidates.push_back(glu::TYPE_FLOAT_MAT3);
264 			typeCandidates.push_back(glu::TYPE_FLOAT_MAT3X4);
265 			typeCandidates.push_back(glu::TYPE_FLOAT_MAT4X2);
266 			typeCandidates.push_back(glu::TYPE_FLOAT_MAT4X3);
267 			typeCandidates.push_back(glu::TYPE_FLOAT_MAT4);
268 		}
269 
270 		glu::DataType	type	= rnd.choose<glu::DataType>(typeCandidates.begin(), typeCandidates.end());
271 		deUint32		flags	= (m_shuffleUniformMembers ? static_cast<deUint32>(LAYOUT_OFFSET) : 0u);
272 
273 		if (glu::dataTypeSupportsPrecisionModifier(type))
274 		{
275 			// Precision.
276 			static const deUint32 precisionCandidates[] = { PRECISION_LOW, PRECISION_MEDIUM, PRECISION_HIGH };
277 			flags |= rnd.choose<deUint32>(&precisionCandidates[0], &precisionCandidates[DE_LENGTH_OF_ARRAY(precisionCandidates)]);
278 		}
279 
280 		return VarType(type, flags);
281 	}
282 }
283 
284 } // ubo
285 } // vkt
286