• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2017 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 SPIR-V Assembly Tests for images and samplers.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "vktSpvAsmImageSamplerTests.hpp"
25 #include "vktSpvAsmComputeShaderCase.hpp"
26 #include "vktSpvAsmComputeShaderTestUtil.hpp"
27 #include "vktSpvAsmGraphicsShaderTestUtil.hpp"
28 
29 #include "vkImageUtil.hpp"
30 #include "tcuTextureUtil.hpp"
31 #include "tcuVectorUtil.hpp"
32 
33 namespace vkt
34 {
35 namespace SpirVAssembly
36 {
37 
38 using namespace vk;
39 using std::map;
40 using std::string;
41 using std::vector;
42 using tcu::IVec3;
43 using tcu::RGBA;
44 using tcu::Vec4;
45 
46 namespace
47 {
48 enum TestType
49 {
50 	TESTTYPE_LOCAL_VARIABLES = 0,
51 	TESTTYPE_PASS_IMAGE_TO_FUNCTION,
52 	TESTTYPE_PASS_SAMPLER_TO_FUNCTION,
53 	TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION,
54 	TESTTYPE_OPTYPEIMAGE_MISMATCH,
55 
56 	TESTTYPE_LAST
57 };
58 
59 enum ReadOp
60 {
61 	READOP_IMAGEREAD = 0,
62 	READOP_IMAGEFETCH,
63 	READOP_IMAGESAMPLE,
64 	READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD,
65 	READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD,
66 
67 	READOP_LAST
68 };
69 
70 enum DescriptorType
71 {
72 	DESCRIPTOR_TYPE_STORAGE_IMAGE = 0,								// Storage image
73 	DESCRIPTOR_TYPE_SAMPLED_IMAGE,									// Sampled image
74 	DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,							// Combined image sampler
75 	DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES,		// Combined image sampler with separate shader variables
76 	DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS,	// Combined image sampler where image and sampler variables are taken from two different desciptors
77 
78 	DESCRIPTOR_TYPE_LAST
79 };
80 
81 enum DepthProperty
82 {
83 	DEPTH_PROPERTY_NON_DEPTH = 0,
84 	DEPTH_PROPERTY_DEPTH,
85 	DEPTH_PROPERTY_UNKNOWN,
86 
87 	DEPTH_PROPERTY_LAST
88 };
89 
isValidTestCase(TestType testType,DescriptorType descriptorType,ReadOp readOp)90 bool isValidTestCase (TestType testType, DescriptorType descriptorType, ReadOp readOp)
91 {
92 	// Check valid descriptor type and test type combinations
93 	switch (testType)
94 	{
95 		case TESTTYPE_PASS_IMAGE_TO_FUNCTION:
96 			if (descriptorType != DESCRIPTOR_TYPE_STORAGE_IMAGE									&&
97 				descriptorType != DESCRIPTOR_TYPE_SAMPLED_IMAGE									&&
98 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES		&&
99 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS)
100 					return false;
101 			break;
102 
103 		case TESTTYPE_PASS_SAMPLER_TO_FUNCTION:
104 			if (descriptorType != DESCRIPTOR_TYPE_SAMPLED_IMAGE									&&
105 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES		&&
106 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS)
107 				return false;
108 			break;
109 
110 		case TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION:
111 			if (descriptorType != DESCRIPTOR_TYPE_SAMPLED_IMAGE									&&
112 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES		&&
113 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS)
114 				return false;
115 			break;
116 
117 		default:
118 			break;
119 	}
120 
121 	// Check valid descriptor type and read operation combinations
122 	switch (readOp)
123 	{
124 		case READOP_IMAGEREAD:
125 			if (descriptorType != DESCRIPTOR_TYPE_STORAGE_IMAGE)
126 				return false;
127 			break;
128 
129 		case READOP_IMAGEFETCH:
130 			if (descriptorType != DESCRIPTOR_TYPE_SAMPLED_IMAGE									&&
131 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER						&&
132 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES		&&
133 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS)
134 				return false;
135 			break;
136 
137 		case READOP_IMAGESAMPLE:
138 		case READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD:
139 		case READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD:
140 			if (descriptorType != DESCRIPTOR_TYPE_SAMPLED_IMAGE									&&
141 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER						&&
142 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES		&&
143 				descriptorType != DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS)
144 				return false;
145 			break;
146 
147 		default:
148 			break;
149 	}
150 
151 	// Check valid test type and readOp combination
152 	switch (testType)
153 	{
154 		case TESTTYPE_OPTYPEIMAGE_MISMATCH:
155 			// OPTYPEIMAGE_MISTMATCH does not test DEPTH formats
156 			if (readOp == READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD									||
157 				readOp == READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD)
158 				return false;
159 			break;
160 
161 		default:
162 			break;
163 	}
164 
165 	return true;
166 }
167 
getTestTypeName(TestType testType)168 const char* getTestTypeName (TestType testType)
169 {
170 	switch (testType)
171 	{
172 		case TESTTYPE_LOCAL_VARIABLES:
173 			return "all_local_variables";
174 
175 		case TESTTYPE_PASS_IMAGE_TO_FUNCTION:
176 			return "pass_image_to_function";
177 
178 		case TESTTYPE_PASS_SAMPLER_TO_FUNCTION:
179 			return "pass_sampler_to_function";
180 
181 		case TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION:
182 			return "pass_image_and_sampler_to_function";
183 
184 		case TESTTYPE_OPTYPEIMAGE_MISMATCH:
185 			return "optypeimage_mismatch";
186 
187 		default:
188 			DE_FATAL("Unknown test type");
189 			return "";
190 	}
191 }
192 
getReadOpName(ReadOp readOp)193 const char* getReadOpName (ReadOp readOp)
194 {
195 	switch (readOp)
196 	{
197 		case READOP_IMAGEREAD:
198 			return "imageread";
199 
200 		case READOP_IMAGEFETCH:
201 			return "imagefetch";
202 
203 		case READOP_IMAGESAMPLE:
204 			return "imagesample";
205 
206 		case READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD:
207 			return "imagesample_dref_implicit_lod";
208 
209 		case READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD:
210 			return "imagesample_dref_explicit_lod";
211 
212 		default:
213 			DE_FATAL("Unknown readop");
214 			return "";
215 	}
216 }
217 
getDescriptorName(DescriptorType descType)218 const char* getDescriptorName (DescriptorType descType)
219 {
220 	switch (descType)
221 	{
222 		case DESCRIPTOR_TYPE_STORAGE_IMAGE:
223 			return "storage_image";
224 
225 		case DESCRIPTOR_TYPE_SAMPLED_IMAGE:
226 			return "sampled_image";
227 
228 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
229 			return "combined_image_sampler";
230 
231 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES:
232 			return "combined_image_sampler_separate_variables";
233 
234 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS:
235 			return "combined_image_sampler_separate_descriptors";
236 
237 		default:
238 			DE_FATAL("Unknown descriptor type");
239 			return "";
240 	}
241 }
242 
getDepthPropertyName(DepthProperty depthProperty)243 const char* getDepthPropertyName (DepthProperty depthProperty)
244 {
245 	switch (depthProperty)
246 	{
247 		case DEPTH_PROPERTY_NON_DEPTH:
248 			return "non_depth";
249 
250 		case DEPTH_PROPERTY_DEPTH:
251 			return "depth";
252 
253 		case DEPTH_PROPERTY_UNKNOWN:
254 			return "unknown";
255 
256 		default:
257 			DE_FATAL("Unknown depth property");
258 			return "";
259 	}
260 }
261 
getVkDescriptorType(DescriptorType descType)262 VkDescriptorType getVkDescriptorType (DescriptorType descType)
263 {
264 	switch (descType)
265 	{
266 		case DESCRIPTOR_TYPE_STORAGE_IMAGE:
267 			return VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
268 
269 		case DESCRIPTOR_TYPE_SAMPLED_IMAGE:
270 			return VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
271 
272 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
273 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES:
274 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS:
275 			return VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
276 
277 		default:
278 			DE_FATAL("Unknown descriptor type");
279 			return VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
280 	}
281 }
282 
getImageFormat(ReadOp readOp)283 VkFormat getImageFormat (ReadOp readOp)
284 {
285 	switch (readOp)
286 	{
287 		case READOP_IMAGEREAD:
288 		case READOP_IMAGEFETCH:
289 		case READOP_IMAGESAMPLE:
290 			return VK_FORMAT_R32G32B32A32_SFLOAT;
291 
292 		case READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD:
293 		case READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD:
294 			return VK_FORMAT_D32_SFLOAT;
295 
296 		default:
297 			DE_FATAL("Unknown readop");
298 			return VK_FORMAT_UNDEFINED;
299 	}
300 }
301 
302 // Get variables that are declared in the read function, ie. not passed as parameters
getFunctionDstVariableStr(ReadOp readOp,DescriptorType descType,TestType testType)303 std::string getFunctionDstVariableStr (ReadOp readOp, DescriptorType descType, TestType testType)
304 {
305 	const bool passNdx = (testType == TESTTYPE_LOCAL_VARIABLES)					|| (testType == TESTTYPE_OPTYPEIMAGE_MISMATCH);
306 	const bool passImg = ((testType == TESTTYPE_PASS_IMAGE_TO_FUNCTION)			|| (testType == TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION));
307 	const bool passSmp = ((testType == TESTTYPE_PASS_SAMPLER_TO_FUNCTION)		|| (testType == TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION));
308 
309 	std::string result = "";
310 
311 	switch (descType)
312 	{
313 		case DESCRIPTOR_TYPE_STORAGE_IMAGE:
314 		{
315 			switch (readOp)
316 			{
317 				case READOP_IMAGEREAD:
318 					if (passNdx)
319 						return	"           %func_img = OpLoad %Image %InputData\n";
320 					break;
321 
322 				default:
323 					DE_FATAL("Not possible");
324 					break;
325 			}
326 			break;
327 		}
328 		case DESCRIPTOR_TYPE_SAMPLED_IMAGE:
329 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES:
330 		{
331 			switch (readOp)
332 			{
333 				case READOP_IMAGEFETCH:
334 					if (passNdx)
335 						return	"           %func_img = OpLoad %Image %InputData\n";
336 
337 					if (passSmp && !passImg)
338 						return	"           %func_tmp = OpLoad %Image %InputData\n"
339 								"           %func_smi = OpSampledImage %SampledImage %func_tmp %func_smp\n"
340 								"           %func_img = OpImage %Image %func_smi\n";
341 
342 					if (passSmp && passImg)
343 						return	"           %func_smi = OpSampledImage %SampledImage %func_tmp %func_smp\n"
344 								"           %func_img = OpImage %Image %func_smi\n";
345 					break;
346 
347 				case READOP_IMAGESAMPLE:
348 				case READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD:
349 				case READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD:
350 					if (passNdx)
351 						return	"           %func_img = OpLoad %Image %InputData\n"
352 								"           %func_smp = OpLoad %Sampler %SamplerData\n"
353 								"           %func_smi = OpSampledImage %SampledImage %func_img %func_smp\n";
354 
355 					if (passImg && !passSmp)
356 						return	"           %func_smp = OpLoad %Sampler %SamplerData\n"
357 								"           %func_smi = OpSampledImage %SampledImage %func_img %func_smp\n";
358 
359 					if (passSmp && !passImg)
360 						return	"           %func_img = OpLoad %Image %InputData\n"
361 								"           %func_smi = OpSampledImage %SampledImage %func_img %func_smp\n";
362 
363 					if (passSmp && passImg)
364 						return	"           %func_smi = OpSampledImage %SampledImage %func_img %func_smp\n";
365 					break;
366 
367 				default:
368 					DE_FATAL("Not possible");
369 			}
370 			break;
371 		}
372 
373 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
374 		{
375 			switch (readOp)
376 			{
377 				case READOP_IMAGEFETCH:
378 					if (passNdx)
379 						return	"           %func_smi = OpLoad %SampledImage %InputData\n"
380 								"           %func_img = OpImage %Image %func_smi\n";
381 					break;
382 
383 				case READOP_IMAGESAMPLE:
384 				case READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD:
385 				case READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD:
386 					if (passNdx)
387 						return	"           %func_smi = OpLoad %SampledImage %InputData\n";
388 					break;
389 
390 				default:
391 					DE_FATAL("Not possible");
392 			}
393 			break;
394 		}
395 
396 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS:
397 		{
398 			switch (readOp)
399 			{
400 				case READOP_IMAGEFETCH:
401 					if (passNdx)
402 						return	"           %func_img = OpLoad %Image %InputData2\n";
403 
404 					if (passSmp && !passImg)
405 						return	"           %func_tmp = OpLoad %Image %InputData2\n"
406 								"           %func_smi = OpSampledImage %SampledImage %func_tmp %func_smp\n"
407 								"           %func_img = OpImage %Image %func_smi\n";
408 
409 					if (passSmp && passImg)
410 						return	"           %func_smi = OpSampledImage %SampledImage %func_tmp %func_smp\n"
411 								"           %func_img = OpImage %Image %func_smi\n";
412 					break;
413 
414 				case READOP_IMAGESAMPLE:
415 				case READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD:
416 				case READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD:
417 					if (passNdx)
418 						return	"           %func_img = OpLoad %Image %InputData2\n"
419 								"           %func_smp = OpLoad %Sampler %SamplerData\n"
420 								"           %func_smi = OpSampledImage %SampledImage %func_img %func_smp\n";
421 
422 					if (passImg && !passSmp)
423 						return	"           %func_smp = OpLoad %Sampler %SamplerData\n"
424 								"           %func_smi = OpSampledImage %SampledImage %func_img %func_smp\n";
425 
426 					if (passSmp && !passImg)
427 						return	"           %func_img = OpLoad %Image %InputData2\n"
428 								"           %func_smi = OpSampledImage %SampledImage %func_img %func_smp\n";
429 
430 					if (passSmp && passImg)
431 						return	"           %func_smi = OpSampledImage %SampledImage %func_img %func_smp\n";
432 					break;
433 
434 				default:
435 					DE_FATAL("Not possible");
436 			}
437 			break;
438 		}
439 
440 		default:
441 			DE_FATAL("Unknown descriptor type");
442 	}
443 
444 	return result;
445 }
446 
447 // Get variables that are passed to the read function
getFunctionSrcVariableStr(ReadOp readOp,DescriptorType descType,TestType testType)448 std::string getFunctionSrcVariableStr (ReadOp readOp, DescriptorType descType, TestType testType)
449 {
450 	const bool passImg = ((testType == TESTTYPE_PASS_IMAGE_TO_FUNCTION)			|| (testType == TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION));
451 	const bool passSmp = ((testType == TESTTYPE_PASS_SAMPLER_TO_FUNCTION)		|| (testType == TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION));
452 
453 	string result = "";
454 
455 	switch (descType)
456 	{
457 		case DESCRIPTOR_TYPE_STORAGE_IMAGE:
458 		{
459 			switch (readOp)
460 			{
461 				case READOP_IMAGEREAD:
462 					if (passImg)
463 						result +=	"           %call_img = OpLoad %Image %InputData\n";
464 					break;
465 
466 				default:
467 					DE_FATAL("Not possible");
468 			}
469 			break;
470 		}
471 		case DESCRIPTOR_TYPE_SAMPLED_IMAGE:
472 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES:
473 		{
474 			switch (readOp)
475 			{
476 				case READOP_IMAGEFETCH:
477 				case READOP_IMAGESAMPLE:
478 				case READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD:
479 				case READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD:
480 					if (passImg)
481 						result +=	"           %call_img = OpLoad %Image %InputData\n";
482 
483 					if (passSmp)
484 						result +=	"           %call_smp = OpLoad %Sampler %SamplerData\n";
485 					break;
486 
487 				default:
488 					DE_FATAL("Not possible");
489 			}
490 			break;
491 		}
492 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
493 		{
494 			break;
495 		}
496 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS:
497 		{
498 			switch (readOp)
499 			{
500 				case READOP_IMAGEFETCH:
501 				case READOP_IMAGESAMPLE:
502 				case READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD:
503 				case READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD:
504 					if (passImg)
505 						result +=	"           %call_img = OpLoad %Image %InputData2\n";
506 
507 					if (passSmp)
508 						result +=	"           %call_smp = OpLoad %Sampler %SamplerData\n";
509 					break;
510 
511 				default:
512 					DE_FATAL("Not possible");
513 			}
514 			break;
515 		}
516 		default:
517 			DE_FATAL("Unknown descriptor type");
518 	}
519 
520 	return result;
521 }
522 
523 // Get parameter types for OpTypeFunction
getFunctionParamTypeStr(TestType testType)524 std::string getFunctionParamTypeStr (TestType testType)
525 {
526 	const bool passImg = ((testType == TESTTYPE_PASS_IMAGE_TO_FUNCTION)			|| (testType == TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION));
527 	const bool passSmp = ((testType == TESTTYPE_PASS_SAMPLER_TO_FUNCTION)		|| (testType == TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION));
528 
529 	string result = "";
530 
531 	if (passImg)
532 		result += " %Image";
533 
534 	if (passSmp)
535 		result += " %Sampler";
536 
537 	return result;
538 }
539 
540 // Get argument names for OpFunctionCall
getFunctionSrcParamStr(TestType testType)541 std::string getFunctionSrcParamStr (TestType testType)
542 {
543 	const bool passImg = ((testType == TESTTYPE_PASS_IMAGE_TO_FUNCTION)			|| (testType == TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION));
544 	const bool passSmp = ((testType == TESTTYPE_PASS_SAMPLER_TO_FUNCTION)		|| (testType == TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION));
545 
546 	string result = "";
547 
548 	if (passImg)
549 		result += " %call_img";
550 
551 	if (passSmp)
552 		result += " %call_smp";
553 
554 	return result;
555 }
556 
557 // Get OpFunctionParameters
getFunctionDstParamStr(ReadOp readOp,TestType testType)558 std::string getFunctionDstParamStr (ReadOp readOp, TestType testType)
559 {
560 	const bool passImg = ((testType == TESTTYPE_PASS_IMAGE_TO_FUNCTION)			|| (testType == TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION));
561 	const bool passSmp = ((testType == TESTTYPE_PASS_SAMPLER_TO_FUNCTION)		|| (testType == TESTTYPE_PASS_IMAGE_AND_SAMPLER_TO_FUNCTION));
562 
563 	string result = "";
564 
565 	switch (readOp)
566 	{
567 		case READOP_IMAGESAMPLE:
568 		case READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD:
569 		case READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD:
570 			if (passImg)
571 				result +=	"           %func_img = OpFunctionParameter %Image\n";
572 
573 			if (passSmp)
574 				result +=	"           %func_smp = OpFunctionParameter %Sampler\n";
575 			break;
576 
577 		default:
578 			if (passImg && !passSmp)
579 				result +=	"           %func_img = OpFunctionParameter %Image\n";
580 
581 			if (passSmp && !passImg)
582 				result +=	"           %func_smp = OpFunctionParameter %Sampler\n";
583 
584 			if (passImg && passSmp)
585 				result +=	"           %func_tmp = OpFunctionParameter %Image\n"
586 							"           %func_smp = OpFunctionParameter %Sampler\n";
587 			break;
588 	}
589 
590 	return result;
591 }
592 
593 // Get read operation
getImageReadOpStr(ReadOp readOp,bool useNontemporal=false)594 std::string getImageReadOpStr (ReadOp readOp, bool useNontemporal = false)
595 {
596 	std::string imageOperand = useNontemporal ? " Nontemporal" : "";
597 
598 	switch (readOp)
599 	{
600 		case READOP_IMAGEREAD:
601 			return std::string("OpImageRead %v4f32 %func_img %coord") + imageOperand;
602 
603 		case READOP_IMAGEFETCH:
604 			return std::string("OpImageFetch %v4f32 %func_img %coord") + imageOperand;
605 
606 		case READOP_IMAGESAMPLE:
607 			if (useNontemporal)
608 				return "OpImageSampleExplicitLod %v4f32 %func_smi %normalcoordf Lod|Nontemporal %c_f32_0";
609 			return "OpImageSampleExplicitLod %v4f32 %func_smi %normalcoordf Lod %c_f32_0";
610 
611 		case READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD:
612 			return "OpImageSampleDrefImplicitLod %f32 %func_smi %normalcoordf %c_f32_0_5 Bias %c_f32_0";
613 
614 		case READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD:
615 			return "OpImageSampleDrefExplicitLod %f32 %func_smi %normalcoordf %c_f32_0_5 Lod %c_f32_0";
616 
617 		default:
618 			DE_FATAL("Unknown readop");
619 			return "";
620 	}
621 }
622 
isImageSampleDrefReadOp(ReadOp readOp)623 bool isImageSampleDrefReadOp (ReadOp readOp)
624 {
625 	return (readOp == READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD) || (readOp == READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD);
626 }
627 
628 static const VkFormat optypeimageFormatMismatchVkFormat[] =
629 {
630 	VK_FORMAT_R8G8B8A8_UNORM,
631 	VK_FORMAT_R8G8B8A8_SNORM,
632 	VK_FORMAT_R8G8B8A8_UINT,
633 	VK_FORMAT_R8G8B8A8_SINT,
634 	VK_FORMAT_R16G16B16A16_UINT,
635 	VK_FORMAT_R16G16B16A16_SINT,
636 	VK_FORMAT_R16G16B16A16_SFLOAT,
637 	VK_FORMAT_R32_UINT,
638 	VK_FORMAT_R32_SINT,
639 	VK_FORMAT_R32G32B32A32_UINT,
640 	VK_FORMAT_R32G32B32A32_SINT,
641 	VK_FORMAT_R32G32B32A32_SFLOAT
642 };
643 
644 static const size_t optypeimageFormatMismatchFormatCount = sizeof(optypeimageFormatMismatchVkFormat) / sizeof(VkFormat);
645 
646 static const char *optypeimageFormatMismatchSpirvFormat[] =
647 {
648 	"Rgba8",
649 	"Rgba8Snorm",
650 	"Rgba8ui",
651 	"Rgba8i",
652 	"Rgba16ui",
653 	"Rgba16i",
654 	"Rgba16f",
655 	"R32ui",
656 	"R32i",
657 	"Rgba32ui",
658 	"Rgba32i",
659 	"Rgba32f"
660 };
661 
662 static const char *optypeimageFormatMismatchCase[] =
663 {
664 	"rgba8",
665 	"rgba8snorm",
666 	"rgba8ui",
667 	"rgba8i",
668 	"rgba16ui",
669 	"rgba16i",
670 	"rgba16f",
671 	"r32ui",
672 	"r32i",
673 	"rgba32ui",
674 	"rgba32i",
675 	"rgba32f"
676 };
677 
678 // Get types and pointers for input images and samplers
getImageSamplerTypeStr(DescriptorType descType,ReadOp readOp,deUint32 depthProperty,TestType testType,int formatIndex)679 std::string getImageSamplerTypeStr (DescriptorType descType, ReadOp readOp, deUint32 depthProperty, TestType testType, int formatIndex)
680 {
681 	const string imageFormat =	(testType == TESTTYPE_OPTYPEIMAGE_MISMATCH) ? optypeimageFormatMismatchSpirvFormat[formatIndex] :
682 								isImageSampleDrefReadOp(readOp) ? "R32f" : "Rgba32f";
683 
684 	switch (descType)
685 	{
686 		case DESCRIPTOR_TYPE_STORAGE_IMAGE:
687 			return	"              %Image = OpTypeImage %f32 2D " + de::toString(depthProperty) + " 0 0 2 " + imageFormat + "\n"
688 					"           %ImagePtr = OpTypePointer UniformConstant %Image\n"
689 					"          %InputData = OpVariable %ImagePtr UniformConstant\n";
690 
691 		case DESCRIPTOR_TYPE_SAMPLED_IMAGE:
692 			return	"              %Image = OpTypeImage %f32 2D " + de::toString(depthProperty) + " 0 0 1 " + imageFormat + "\n"
693 					"           %ImagePtr = OpTypePointer UniformConstant %Image\n"
694 					"          %InputData = OpVariable %ImagePtr UniformConstant\n"
695 
696 					"            %Sampler = OpTypeSampler\n"
697 					"         %SamplerPtr = OpTypePointer UniformConstant %Sampler\n"
698 					"        %SamplerData = OpVariable %SamplerPtr UniformConstant\n"
699 					"       %SampledImage = OpTypeSampledImage %Image\n";
700 
701 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
702 			return	"              %Image = OpTypeImage %f32 2D " + de::toString(depthProperty) + " 0 0 1 " + imageFormat + "\n"
703 					"       %SampledImage = OpTypeSampledImage %Image\n"
704 					"         %SamplerPtr = OpTypePointer UniformConstant %SampledImage\n"
705 					"          %InputData = OpVariable %SamplerPtr UniformConstant\n";
706 
707 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES:
708 			return	"              %Image = OpTypeImage %f32 2D " + de::toString(depthProperty) + " 0 0 1 " + imageFormat + "\n"
709 					"           %ImagePtr = OpTypePointer UniformConstant %Image\n"
710 					"          %InputData = OpVariable %ImagePtr UniformConstant\n"
711 
712 					"            %Sampler = OpTypeSampler\n"
713 					"         %SamplerPtr = OpTypePointer UniformConstant %Sampler\n"
714 					"        %SamplerData = OpVariable %SamplerPtr UniformConstant\n"
715 					"       %SampledImage = OpTypeSampledImage %Image\n";
716 
717 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS:
718 			return	"              %Image = OpTypeImage %f32 2D " + de::toString(depthProperty) + " 0 0 1 " + imageFormat + "\n"
719 					"           %ImagePtr = OpTypePointer UniformConstant %Image\n"
720 					"          %InputData = OpVariable %ImagePtr UniformConstant\n"
721 					"         %InputData2 = OpVariable %ImagePtr UniformConstant\n"
722 
723 					"            %Sampler = OpTypeSampler\n"
724 					"         %SamplerPtr = OpTypePointer UniformConstant %Sampler\n"
725 					"        %SamplerData = OpVariable %SamplerPtr UniformConstant\n"
726 					"       %SamplerData2 = OpVariable %SamplerPtr UniformConstant\n"
727 					"       %SampledImage = OpTypeSampledImage %Image\n";
728 
729 		default:
730 			DE_FATAL("Unknown descriptor type");
731 			return "";
732 	}
733 }
734 
getInterfaceList(DescriptorType descType)735 std::string getInterfaceList (DescriptorType descType)
736 {
737 	std::string list = " %InputData %OutputData";
738 	switch (descType)
739 	{
740 		case DESCRIPTOR_TYPE_SAMPLED_IMAGE:
741 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES:
742 			list += " %SamplerData";
743 			break;
744 
745 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS:
746 			list += " %SamplerData %InputData2 %SamplerData2";
747 			break;
748 
749 		default:
750 			break;
751 	}
752 
753 	return list;
754 }
755 
getSamplerDecoration(DescriptorType descType)756 std::string getSamplerDecoration (DescriptorType descType)
757 {
758 	switch (descType)
759 	{
760 		// Separate image and sampler
761 		case DESCRIPTOR_TYPE_SAMPLED_IMAGE:
762 			return	"                       OpDecorate %SamplerData DescriptorSet 0\n"
763 					"                       OpDecorate %SamplerData Binding 1\n";
764 
765 		// Combined image sampler with separate variables
766 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_VARIABLES:
767 			return	"                       OpDecorate %SamplerData DescriptorSet 0\n"
768 					"                       OpDecorate %SamplerData Binding 0\n";
769 
770 		// Two combined image samplers with separate variables
771 		case DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS:
772 			return	"                       OpDecorate %SamplerData DescriptorSet 0\n"
773 					"                       OpDecorate %SamplerData Binding 0\n"
774 					"                       OpDecorate %InputData2 DescriptorSet 0\n"
775 					"                       OpDecorate %InputData2 Binding 1\n"
776 					"                       OpDecorate %SamplerData2 DescriptorSet 0\n"
777 					"                       OpDecorate %SamplerData2 Binding 1\n";
778 
779 		default:
780 			return "";
781 	}
782 }
783 
784 // no-operation verify functon to ignore test results (optypeimage_mismatch)
nopVerifyFunction(const std::vector<Resource> &,const std::vector<AllocationSp> &,const std::vector<Resource> &,tcu::TestLog &)785 bool nopVerifyFunction (const std::vector<Resource>&,
786 						const std::vector<AllocationSp>&,
787 						const std::vector<Resource>&,
788 						tcu::TestLog&)
789 {
790 	return true;
791 }
792 
addComputeImageSamplerTest(tcu::TestCaseGroup * group)793 void addComputeImageSamplerTest (tcu::TestCaseGroup* group)
794 {
795 	tcu::TestContext& testCtx = group->getTestContext();
796 
797 	de::Random				rnd					(deStringHash(group->getName()));
798 	const deUint32			numDataPoints		= 64;
799 	RGBA					defaultColors[4];
800 	vector<tcu::Vec4>		inputData;
801 
802 	inputData.reserve(numDataPoints);
803 
804 	for (deUint32 numIdx = 0; numIdx < numDataPoints; ++numIdx)
805 		inputData.push_back(tcu::randomVec4(rnd));
806 
807 	struct SpirvData
808 	{
809 		SpirvVersion	version;
810 		std::string		postfix;
811 	};
812 	const std::vector<SpirvData> spirvDataVect
813 	{
814 		{ SPIRV_VERSION_1_0, "" },
815 		{ SPIRV_VERSION_1_6, "_nontemporal" },
816 	};
817 
818 	for (deUint32 opNdx = 0u; opNdx <= READOP_IMAGESAMPLE; opNdx++)
819 	{
820 		de::MovePtr<tcu::TestCaseGroup> readOpGroup	(new tcu::TestCaseGroup(testCtx, getReadOpName((ReadOp)opNdx), ""));
821 
822 		for (deUint32 descNdx = 0u; descNdx < DESCRIPTOR_TYPE_LAST; descNdx++)
823 		{
824 			de::MovePtr<tcu::TestCaseGroup> descGroup (new tcu::TestCaseGroup(testCtx, getDescriptorName((DescriptorType)descNdx), ""));
825 
826 			for (deUint32 testNdx = 0u; testNdx < TESTTYPE_LAST; testNdx++)
827 			{
828 				if (!isValidTestCase((TestType)testNdx, (DescriptorType)descNdx, (ReadOp)opNdx))
829 					continue;
830 
831 				deUint32 numFormats = 1;
832 				if (testNdx == TESTTYPE_OPTYPEIMAGE_MISMATCH)
833 					numFormats = optypeimageFormatMismatchFormatCount;
834 
835 				for (deUint32 formatIndex = 0; formatIndex < numFormats; formatIndex++)
836 				{
837 					const std::string	functionParamTypes = getFunctionParamTypeStr((TestType)testNdx);
838 
839 					const std::string	functionSrcVariables = getFunctionSrcVariableStr((ReadOp)opNdx, (DescriptorType)descNdx, (TestType)testNdx);
840 					const std::string	functionDstVariables = getFunctionDstVariableStr((ReadOp)opNdx, (DescriptorType)descNdx, (TestType)testNdx);
841 
842 					const std::string	functionSrcParams = getFunctionSrcParamStr((TestType)testNdx);
843 					const std::string	functionDstParams = getFunctionDstParamStr((ReadOp)opNdx, (TestType)testNdx);
844 
845 					getDefaultColors(defaultColors);
846 
847 					ComputeShaderSpec	spec;
848 
849 					spec.numWorkGroups = IVec3(numDataPoints, 1, 1);
850 
851 					spec.inputs.push_back(Resource(BufferSp(new Vec4Buffer(inputData)), getVkDescriptorType((DescriptorType)descNdx)));
852 
853 					// Separate sampler for sampled images
854 					if ((DescriptorType)descNdx == DESCRIPTOR_TYPE_SAMPLED_IMAGE)
855 					{
856 						vector<tcu::Vec4> unusedData;
857 						spec.inputs.push_back(Resource(BufferSp(new Vec4Buffer(unusedData))));
858 						spec.inputs[1].setDescriptorType(VK_DESCRIPTOR_TYPE_SAMPLER);
859 					}
860 
861 					// Second combined image sampler with different image data
862 					if ((DescriptorType)descNdx == DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS)
863 					{
864 						for (size_t i = 0; i < inputData.size(); i++)
865 							inputData[i] = tcu::Vec4(1.0f) - inputData[i];
866 
867 						spec.inputs.push_back(BufferSp(new Vec4Buffer(inputData)));
868 						spec.inputs[1].setDescriptorType(getVkDescriptorType((DescriptorType)descNdx));
869 					}
870 
871 					// Shader is expected to pass the input image data to the output buffer
872 					spec.outputs.push_back(BufferSp(new Vec4Buffer(inputData)));
873 
874 					const std::string	samplerDecoration	= getSamplerDecoration((DescriptorType)descNdx);
875 
876 					for (auto spirvData : spirvDataVect)
877 					{
878 						spec.spirvVersion = spirvData.version;
879 
880 						bool		useSpirV16			(spirvData.version == SPIRV_VERSION_1_6);
881 						std::string	interfaceList		("");
882 						std::string	outputDecoration	("BufferBlock");
883 						std::string	outputType			("Uniform");
884 						std::string	imageReadOp			(getImageReadOpStr((ReadOp)opNdx, useSpirV16));
885 
886 						// adjust shader code to spv16
887 						if (useSpirV16)
888 						{
889 							interfaceList		= getInterfaceList((DescriptorType)descNdx);
890 							outputDecoration	= "Block";
891 							outputType			= "StorageBuffer";
892 						}
893 
894 						string testname = getTestTypeName((TestType)testNdx);
895 
896 						if (testNdx == TESTTYPE_OPTYPEIMAGE_MISMATCH)
897 						{
898 							// If testing for mismatched optypeimage, ignore the
899 							// result (we're only interested to see if we crash)
900 							spec.verifyIO = nopVerifyFunction;
901 
902 							testname = testname + string("_") + string(optypeimageFormatMismatchCase[formatIndex]);
903 						}
904 						testname += spirvData.postfix;
905 						de::MovePtr<tcu::TestCaseGroup> typeGroup (new tcu::TestCaseGroup(testCtx, testname.c_str(), ""));
906 
907 						de::MovePtr<tcu::TestCaseGroup> depthGroup (new tcu::TestCaseGroup(testCtx, "depth_property", ""));
908 						for (deUint32 propertyNdx = 0u; propertyNdx < DEPTH_PROPERTY_LAST; propertyNdx++)
909 						{
910 							const std::string	imageSamplerTypes = getImageSamplerTypeStr((DescriptorType)descNdx, (ReadOp)opNdx, (DepthProperty)propertyNdx, (TestType)testNdx, formatIndex);
911 							const string shaderSource =
912 								"                       OpCapability Shader\n"
913 								"                  %1 = OpExtInstImport \"GLSL.std.450\"\n"
914 								"                       OpMemoryModel Logical GLSL450\n"
915 								"                       OpEntryPoint GLCompute %main \"main\" %id" + interfaceList + "\n"
916 								"                       OpExecutionMode %main LocalSize 1 1 1\n"
917 								"                       OpSource GLSL 430\n"
918 								"                       OpDecorate %id BuiltIn GlobalInvocationId\n"
919 								"                       OpDecorate %_arr_v4f_u32_64 ArrayStride 16\n"
920 								"                       OpMemberDecorate %Output 0 Offset 0\n"
921 								"                       OpDecorate %Output " + outputDecoration + "\n"
922 								"                       OpDecorate %InputData DescriptorSet 0\n"
923 								"                       OpDecorate %InputData Binding 0\n"
924 
925 								+ samplerDecoration +
926 
927 								"                       OpDecorate %OutputData DescriptorSet 0\n"
928 								"                       OpDecorate %OutputData Binding " + de::toString(spec.inputs.size()) + "\n"
929 
930 								"               %void = OpTypeVoid\n"
931 								"                  %3 = OpTypeFunction %void\n"
932 								"                %u32 = OpTypeInt 32 0\n"
933 								"                %i32 = OpTypeInt 32 1\n"
934 								"                %f32 = OpTypeFloat 32\n"
935 								" %_ptr_Function_uint = OpTypePointer Function %u32\n"
936 								"              %v3u32 = OpTypeVector %u32 3\n"
937 								"   %_ptr_Input_v3u32 = OpTypePointer Input %v3u32\n"
938 								"                 %id = OpVariable %_ptr_Input_v3u32 Input\n"
939 								"            %c_f32_0 = OpConstant %f32 0.0\n"
940 								"            %c_u32_0 = OpConstant %u32 0\n"
941 								"            %c_i32_0 = OpConstant %i32 0\n"
942 								"    %_ptr_Input_uint = OpTypePointer Input %u32\n"
943 								"              %v2u32 = OpTypeVector %u32 2\n"
944 								"              %v2f32 = OpTypeVector %f32 2\n"
945 								"              %v4f32 = OpTypeVector %f32 4\n"
946 								"           %uint_128 = OpConstant %u32 128\n"
947 								"           %c_u32_64 = OpConstant %u32 64\n"
948 								"            %c_u32_8 = OpConstant %u32 8\n"
949 								"            %c_f32_8 = OpConstant %f32 8.0\n"
950 								"        %c_v2f32_8_8 = OpConstantComposite %v2f32 %c_f32_8 %c_f32_8\n"
951 								"    %_arr_v4f_u32_64 = OpTypeArray %v4f32 %c_u32_64\n"
952 								"   %_ptr_Uniform_v4f = OpTypePointer " + outputType + " %v4f32\n"
953 								"             %Output = OpTypeStruct %_arr_v4f_u32_64\n"
954 								"%_ptr_Uniform_Output = OpTypePointer " + outputType + " %Output\n"
955 								"         %OutputData = OpVariable %_ptr_Uniform_Output " + outputType + "\n"
956 
957 								+ imageSamplerTypes +
958 
959 								"     %read_func_type = OpTypeFunction %void %u32" + functionParamTypes + "\n"
960 
961 								"          %read_func = OpFunction %void None %read_func_type\n"
962 								"           %func_ndx = OpFunctionParameter %u32\n"
963 
964 								+ functionDstParams +
965 
966 								"          %funcentry = OpLabel\n"
967 								"                %row = OpUMod %u32 %func_ndx %c_u32_8\n"
968 								"                %col = OpUDiv %u32 %func_ndx %c_u32_8\n"
969 								"              %coord = OpCompositeConstruct %v2u32 %row %col\n"
970 								"             %coordf = OpConvertUToF %v2f32 %coord\n"
971 								"       %normalcoordf = OpFDiv %v2f32 %coordf %c_v2f32_8_8\n"
972 
973 								+ functionDstVariables +
974 
975 								"              %color = " + imageReadOp + "\n"
976 								"                 %36 = OpAccessChain %_ptr_Uniform_v4f %OutputData %c_u32_0 %func_ndx\n"
977 								"                       OpStore %36 %color\n"
978 								"                       OpReturn\n"
979 								"                       OpFunctionEnd\n"
980 
981 								"               %main = OpFunction %void None %3\n"
982 								"                  %5 = OpLabel\n"
983 								"                  %i = OpVariable %_ptr_Function_uint Function\n"
984 								"                 %14 = OpAccessChain %_ptr_Input_uint %id %c_u32_0\n"
985 								"                 %15 = OpLoad %u32 %14\n"
986 								"                       OpStore %i %15\n"
987 								"              %index = OpLoad %u32 %14\n"
988 
989 								+ functionSrcVariables +
990 
991 								"                %res = OpFunctionCall %void %read_func %index" + functionSrcParams + "\n"
992 								"                       OpReturn\n"
993 								"                       OpFunctionEnd\n";
994 
995 							spec.assembly = shaderSource;
996 
997 							depthGroup->addChild(new SpvAsmComputeShaderCase(testCtx, getDepthPropertyName((DepthProperty)propertyNdx), "", spec));
998 						}
999 						typeGroup->addChild(depthGroup.release());
1000 						descGroup->addChild(typeGroup.release());
1001 					}
1002 				}
1003 			}
1004 			readOpGroup->addChild(descGroup.release());
1005 		}
1006 		group->addChild(readOpGroup.release());
1007 	}
1008 }
1009 
generateGraphicsImageSamplerSource(ReadOp readOp,DescriptorType descriptorType,TestType testType,DepthProperty depthProperty,deUint32 outputBinding,deUint32 formatIndex)1010 map<string, string> generateGraphicsImageSamplerSource (ReadOp readOp, DescriptorType descriptorType, TestType testType, DepthProperty depthProperty, deUint32 outputBinding, deUint32 formatIndex)
1011 {
1012 	map<string, string>	source;
1013 
1014 	const std::string	imageReadOp				= getImageReadOpStr(readOp);
1015 	const std::string	imageSamplerTypes		= getImageSamplerTypeStr(descriptorType, readOp, depthProperty, testType, formatIndex);
1016 	const std::string	functionParamTypes		= getFunctionParamTypeStr(testType);
1017 	const std::string	functionSrcVariables	= getFunctionSrcVariableStr(readOp, descriptorType, testType);
1018 	const std::string	functionDstVariables	= getFunctionDstVariableStr(readOp, descriptorType, testType);
1019 	const std::string	functionSrcParams		= getFunctionSrcParamStr(testType);
1020 	const std::string	functionDstParams		= getFunctionDstParamStr(readOp, testType);
1021 	const std::string	samplerDecoration		= getSamplerDecoration(descriptorType);
1022 	const std::string	outputUniformPtr		= isImageSampleDrefReadOp(readOp) ? "%_ptr_Uniform_f32" : "%_ptr_Uniform_v4f32";
1023 	const std::string	outputArrayStruct		= isImageSampleDrefReadOp(readOp) ? "%_arr_f32_u32_64" : "%_arr_v4f32_u32_64";
1024 
1025 	source["pre_main"]	=
1026 		"           %c_u32_64 = OpConstant %u32 64\n"
1027 		"           %c_i32_64 = OpConstant %i32 64\n"
1028 		"            %c_i32_8 = OpConstant %i32 8\n"
1029 		"        %c_v2f32_8_8 = OpConstantComposite %v2f32 %c_f32_8 %c_f32_8\n"
1030 
1031 		"    %_arr_f32_u32_64 = OpTypeArray %f32 %c_u32_64\n"
1032 		"  %_arr_v4f32_u32_64 = OpTypeArray %v4f32 %c_u32_64\n"
1033 		"   %_ptr_Uniform_f32 = OpTypePointer Uniform %f32\n"
1034 		" %_ptr_Uniform_v4f32 = OpTypePointer Uniform %v4f32\n"
1035 
1036 		"             %Output = OpTypeStruct " + outputArrayStruct + "\n"
1037 		"%_ptr_Uniform_Output = OpTypePointer Uniform %Output\n"
1038 		"         %OutputData = OpVariable %_ptr_Uniform_Output Uniform\n"
1039 
1040 		+ imageSamplerTypes +
1041 
1042 		"     %read_func_type = OpTypeFunction %void %i32" + functionParamTypes + "\n";
1043 
1044 	source["decoration"]	=
1045 		"                       OpDecorate %_arr_f32_u32_64 ArrayStride 4\n"
1046 		"                       OpDecorate %_arr_v4f32_u32_64 ArrayStride 16\n"
1047 		"                       OpMemberDecorate %Output 0 Offset 0\n"
1048 		"                       OpDecorate %Output BufferBlock\n"
1049 		"                       OpDecorate %InputData DescriptorSet 0\n"
1050 		"                       OpDecorate %InputData Binding 0\n"
1051 
1052 		+ samplerDecoration +
1053 
1054 		"OpDecorate %OutputData DescriptorSet 0\n"
1055 		"OpDecorate %OutputData Binding " + de::toString(outputBinding) + "\n";
1056 
1057 	source["testfun"]	=
1058 		"          %read_func = OpFunction %void None %read_func_type\n"
1059 		"           %func_ndx = OpFunctionParameter %i32\n"
1060 
1061 		+ functionDstParams +
1062 
1063 		"          %funcentry = OpLabel\n"
1064 
1065 		"                %row = OpSRem %i32 %func_ndx %c_i32_8\n"
1066 		"                %col = OpSDiv %i32 %func_ndx %c_i32_8\n"
1067 		"              %coord = OpCompositeConstruct %v2i32 %row %col\n"
1068 		"             %coordf = OpConvertSToF %v2f32 %coord\n"
1069 		"       %normalcoordf = OpFDiv %v2f32 %coordf %c_v2f32_8_8\n"
1070 
1071 		+ functionDstVariables +
1072 
1073 		"              %color = " + imageReadOp + "\n"
1074 		"                 %36 = OpAccessChain " + outputUniformPtr + " %OutputData %c_i32_0 %func_ndx\n"
1075 		"                       OpStore %36 %color\n"
1076 
1077 		"                       OpReturn\n"
1078 		"                       OpFunctionEnd\n"
1079 
1080 		"          %test_code = OpFunction %v4f32 None %v4f32_v4f32_function\n"
1081 		"              %param = OpFunctionParameter %v4f32\n"
1082 
1083 		"              %entry = OpLabel\n"
1084 
1085 		"                  %i = OpVariable %fp_i32 Function\n"
1086 		"                       OpStore %i %c_i32_0\n"
1087 		"                       OpBranch %loop\n"
1088 
1089 		"               %loop = OpLabel\n"
1090 		"                 %15 = OpLoad %i32 %i\n"
1091 		"                 %lt = OpSLessThan %bool %15 %c_i32_64\n"
1092 		"                       OpLoopMerge %merge %inc None\n"
1093 		"                       OpBranchConditional %lt %write %merge\n"
1094 
1095 		"              %write = OpLabel\n"
1096 		"              %index = OpLoad %i32 %i\n"
1097 
1098 		+ functionSrcVariables +
1099 
1100 		"                %res = OpFunctionCall %void %read_func %index" + functionSrcParams + "\n"
1101 		"                       OpBranch %inc\n"
1102 
1103 		"                %inc = OpLabel\n"
1104 
1105 		"                 %37 = OpLoad %i32 %i\n"
1106 		"                 %39 = OpIAdd %i32 %37 %c_i32_1\n"
1107 		"                       OpStore %i %39\n"
1108 		"                       OpBranch %loop\n"
1109 
1110 		"              %merge = OpLabel\n"
1111 		"                       OpReturnValue %param\n"
1112 		"                       OpFunctionEnd\n";
1113 
1114 	return source;
1115 }
1116 
verifyDepthCompareResult(const std::vector<Resource> & originalFloats,const std::vector<AllocationSp> & outputAllocs,const std::vector<Resource> & expectedOutputs,tcu::TestLog &)1117 bool verifyDepthCompareResult (const std::vector<Resource>&		originalFloats,
1118 							   const std::vector<AllocationSp>&	outputAllocs,
1119 							   const std::vector<Resource>&		expectedOutputs,
1120 							   tcu::TestLog&)
1121 {
1122 	DE_UNREF(originalFloats);
1123 
1124 	if (outputAllocs.size() != expectedOutputs.size())
1125 		return false;
1126 
1127 	vector<deUint8>	expectedBytes;
1128 	expectedOutputs[0].getBytes(expectedBytes);
1129 
1130 	const float*	returnedAsFloat	= static_cast<const float*>(outputAllocs[0]->getHostPtr());
1131 	const float*	expectedAsFloat	= reinterpret_cast<const float*>(&expectedBytes.front());
1132 
1133 	for (deUint32 elementNdx = 0; elementNdx < static_cast<deUint32>(expectedBytes.size() / sizeof(float)); ++elementNdx)
1134 	{
1135 		const float input	= expectedAsFloat[elementNdx];
1136 		const float result	= returnedAsFloat[elementNdx];
1137 
1138 		// VK_COMPARE_OP_LESS: D = 1.0 if D < Dref, otherwise D = 0.0
1139 		if ((input < 0.5f && result != 0.0f) || (input >= 0.5f && result != 1.0f))
1140 			return false;
1141 	}
1142 
1143 	return true;
1144 }
1145 
addGraphicsImageSamplerTest(tcu::TestCaseGroup * group)1146 void addGraphicsImageSamplerTest (tcu::TestCaseGroup* group)
1147 {
1148 	tcu::TestContext&			testCtx				= group->getTestContext();
1149 
1150 	de::Random					rnd					(deStringHash(group->getName()));
1151 	const deUint32				numDataPoints		= 64;
1152 	RGBA						defaultColors[4];
1153 
1154 	SpecConstants				noSpecConstants;
1155 	PushConstants				noPushConstants;
1156 	GraphicsInterfaces			noInterfaces;
1157 	std::vector<std::string>	noExtensions;
1158 	VulkanFeatures				vulkanFeatures		= VulkanFeatures();
1159 
1160 	vector<tcu::Vec4>			inputDataBase		(numDataPoints);
1161 	for (deUint32 numIdx = 0; numIdx < numDataPoints; ++numIdx)
1162 		inputDataBase[numIdx] = tcu::randomVec4(rnd);
1163 	// Depth only has 1 component
1164 	vector<tcu::Vec4>			inputDataBaseDepth	= inputDataBase;
1165 	inputDataBaseDepth.resize(numDataPoints / 4);
1166 
1167 	for (deUint32 opNdx = 0u; opNdx < READOP_LAST; opNdx++)
1168 	{
1169 		de::MovePtr<tcu::TestCaseGroup>	readOpGroup	(new tcu::TestCaseGroup(testCtx, getReadOpName((ReadOp)opNdx), ""));
1170 
1171 		const VkFormat					imageFormat			= getImageFormat((ReadOp)opNdx);
1172 		const bool						hasDepthComponent	= tcu::hasDepthComponent(vk::mapVkFormat(imageFormat).order);
1173 
1174 		for (deUint32 descNdx = 0u; descNdx < DESCRIPTOR_TYPE_LAST; descNdx++)
1175 		{
1176 			de::MovePtr<tcu::TestCaseGroup> descGroup (new tcu::TestCaseGroup(testCtx, getDescriptorName((DescriptorType)descNdx), ""));
1177 
1178 			for (deUint32 testNdx = 0u; testNdx < TESTTYPE_LAST; testNdx++)
1179 			{
1180 				if (!isValidTestCase((TestType)testNdx, (DescriptorType)descNdx, (ReadOp)opNdx))
1181 					continue;
1182 
1183 				deUint32 formatCount = 1;
1184 				if (testNdx == TESTTYPE_OPTYPEIMAGE_MISMATCH)
1185 					formatCount = optypeimageFormatMismatchFormatCount;
1186 
1187 				// this group is only used for optypeimage_mismatch case
1188 				de::MovePtr<tcu::TestCaseGroup> testtypeGroup(new tcu::TestCaseGroup(testCtx, getTestTypeName((TestType)testNdx), ""));
1189 
1190 				for (deUint32 formatIndex = 0; formatIndex < formatCount; formatIndex++)
1191 				{
1192 					// optypeimage_mismatch uses an additional level of test hierarchy
1193 					const char *groupname = testNdx == TESTTYPE_OPTYPEIMAGE_MISMATCH ? optypeimageFormatMismatchCase[formatIndex] : getTestTypeName((TestType)testNdx);
1194 					de::MovePtr<tcu::TestCaseGroup>	typeGroup(new tcu::TestCaseGroup(testCtx, groupname, ""));
1195 					vector<Vec4>&					inputData = hasDepthComponent && testNdx != TESTTYPE_OPTYPEIMAGE_MISMATCH ? inputDataBaseDepth : inputDataBase;
1196 					GraphicsResources				resources;
1197 
1198 					resources.inputs.push_back(Resource(BufferSp(new Vec4Buffer(inputData)), getVkDescriptorType((DescriptorType)descNdx)));
1199 
1200 					// Separate sampler for sampled images
1201 					if ((DescriptorType)descNdx == DESCRIPTOR_TYPE_SAMPLED_IMAGE)
1202 					{
1203 						vector<tcu::Vec4> unusedData;
1204 						resources.inputs.push_back(Resource(BufferSp(new Vec4Buffer(unusedData)), VK_DESCRIPTOR_TYPE_SAMPLER));
1205 					}
1206 
1207 					// Second combined image sampler with different image data
1208 					if ((DescriptorType)descNdx == DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER_SEPARATE_DESCRIPTORS)
1209 					{
1210 						for (size_t i = 0; i < inputData.size(); i++)
1211 							inputData[i] = tcu::Vec4(1.0f) - inputData[i];
1212 
1213 						resources.inputs.push_back(Resource(BufferSp(new Vec4Buffer(inputData)), getVkDescriptorType((DescriptorType)descNdx)));
1214 					}
1215 
1216 					// Shader is expected to pass the input image data to output buffer
1217 					resources.outputs.push_back(Resource(BufferSp(new Vec4Buffer(inputData)), VK_DESCRIPTOR_TYPE_STORAGE_BUFFER));
1218 
1219 					getDefaultColors(defaultColors);
1220 
1221 					// If testing for mismatched optypeimage, ignore the rendered
1222 					// result (we're only interested to see if we crash)
1223 					if (testNdx == TESTTYPE_OPTYPEIMAGE_MISMATCH)
1224 					{
1225 						resources.verifyIO		= nopVerifyFunction;
1226 						resources.inputFormat	= optypeimageFormatMismatchVkFormat[formatIndex];
1227 					}
1228 					else if (hasDepthComponent)
1229 					{
1230 						resources.verifyIO		= verifyDepthCompareResult;
1231 						resources.inputFormat	= getImageFormat((ReadOp)opNdx);
1232 					}
1233 
1234 					de::MovePtr<tcu::TestCaseGroup> depthGroup (new tcu::TestCaseGroup(testCtx, "depth_property", ""));
1235 					for (deUint32 propertyNdx = 0u; propertyNdx < DEPTH_PROPERTY_LAST; propertyNdx++)
1236 					{
1237 						de::MovePtr<tcu::TestCaseGroup> depthPropertyGroup (new tcu::TestCaseGroup(testCtx, getDepthPropertyName((DepthProperty)propertyNdx), ""));
1238 						const map<string, string>		fragments = generateGraphicsImageSamplerSource((ReadOp)opNdx, (DescriptorType)descNdx, (TestType)testNdx, (DepthProperty)propertyNdx, (deUint32)resources.inputs.size(), (deUint32)((formatIndex + 1) % optypeimageFormatMismatchFormatCount));
1239 
1240 						// READOP_IMAGESAMPLE_DREF_IMPLICIT_LOD and READOP_IMAGESAMPLE_DREF_EXPLICIT_LOD can only be present in fragment/compute
1241 						if (opNdx <= READOP_IMAGESAMPLE)
1242 						{
1243 							vulkanFeatures.coreFeatures.vertexPipelineStoresAndAtomics = DE_TRUE;
1244 							vulkanFeatures.coreFeatures.fragmentStoresAndAtomics = DE_FALSE;
1245 							createTestForStage(VK_SHADER_STAGE_VERTEX_BIT, "shader_vert", defaultColors, defaultColors, fragments, noSpecConstants,
1246 								noPushConstants, resources, noInterfaces, noExtensions, vulkanFeatures, depthPropertyGroup.get());
1247 
1248 							createTestForStage(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, "shader_tessc", defaultColors, defaultColors, fragments, noSpecConstants,
1249 								noPushConstants, resources, noInterfaces, noExtensions, vulkanFeatures, depthPropertyGroup.get());
1250 
1251 							createTestForStage(VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, "shader_tesse", defaultColors, defaultColors, fragments, noSpecConstants,
1252 								noPushConstants, resources, noInterfaces, noExtensions, vulkanFeatures, depthPropertyGroup.get());
1253 
1254 							createTestForStage(VK_SHADER_STAGE_GEOMETRY_BIT, "shader_geom", defaultColors, defaultColors, fragments, noSpecConstants,
1255 								noPushConstants, resources, noInterfaces, noExtensions, vulkanFeatures, depthPropertyGroup.get());
1256 						}
1257 
1258 						vulkanFeatures.coreFeatures.vertexPipelineStoresAndAtomics = DE_FALSE;
1259 						vulkanFeatures.coreFeatures.fragmentStoresAndAtomics = DE_TRUE;
1260 						createTestForStage(VK_SHADER_STAGE_FRAGMENT_BIT, "shader_frag", defaultColors, defaultColors, fragments, noSpecConstants,
1261 							noPushConstants, resources, noInterfaces, noExtensions, vulkanFeatures, depthPropertyGroup.get());
1262 
1263 						depthGroup->addChild(depthPropertyGroup.release());
1264 					}
1265 					typeGroup->addChild(depthGroup.release());
1266 
1267 					if (testNdx == TESTTYPE_OPTYPEIMAGE_MISMATCH)
1268 						testtypeGroup->addChild(typeGroup.release());
1269 					else
1270 						descGroup->addChild(typeGroup.release());
1271 				}
1272 				if (testNdx == TESTTYPE_OPTYPEIMAGE_MISMATCH)
1273 					descGroup->addChild(testtypeGroup.release());
1274 			}
1275 			readOpGroup->addChild(descGroup.release());
1276 		}
1277 		group->addChild(readOpGroup.release());
1278 	}
1279 }
1280 } // anonymous
1281 
createImageSamplerComputeGroup(tcu::TestContext & testCtx)1282 tcu::TestCaseGroup* createImageSamplerComputeGroup (tcu::TestContext& testCtx)
1283 {
1284 	de::MovePtr<tcu::TestCaseGroup> group (new tcu::TestCaseGroup(testCtx, "image_sampler", "Compute tests for combining images and samplers."));
1285 	addComputeImageSamplerTest(group.get());
1286 
1287 	return group.release();
1288 }
1289 
createImageSamplerGraphicsGroup(tcu::TestContext & testCtx)1290 tcu::TestCaseGroup* createImageSamplerGraphicsGroup (tcu::TestContext& testCtx)
1291 {
1292 	de::MovePtr<tcu::TestCaseGroup>	group	(new tcu::TestCaseGroup(testCtx, "image_sampler", "Graphics tests for combining images and samplers."));
1293 
1294 	addGraphicsImageSamplerTest(group.get());
1295 
1296 	return group.release();
1297 }
1298 
1299 } // SpirVAssembly
1300 } // vkt
1301