Test separate and combined images and samplers in SPIR-V
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / spirv_assembly / vktSpvAsmInstructionTests.cpp
1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 Google Inc.
6  * Copyright (c) 2016 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 SPIR-V Assembly Tests for Instructions (special opcode/operand)
23  *//*--------------------------------------------------------------------*/
24
25 #include "vktSpvAsmInstructionTests.hpp"
26
27 #include "tcuCommandLine.hpp"
28 #include "tcuFormatUtil.hpp"
29 #include "tcuFloat.hpp"
30 #include "tcuRGBA.hpp"
31 #include "tcuStringTemplate.hpp"
32 #include "tcuTestLog.hpp"
33 #include "tcuVectorUtil.hpp"
34 #include "tcuInterval.hpp"
35
36 #include "vkDefs.hpp"
37 #include "vkDeviceUtil.hpp"
38 #include "vkMemUtil.hpp"
39 #include "vkPlatform.hpp"
40 #include "vkPrograms.hpp"
41 #include "vkQueryUtil.hpp"
42 #include "vkRef.hpp"
43 #include "vkRefUtil.hpp"
44 #include "vkStrUtil.hpp"
45 #include "vkTypeUtil.hpp"
46
47 #include "deStringUtil.hpp"
48 #include "deUniquePtr.hpp"
49 #include "deMath.h"
50 #include "tcuStringTemplate.hpp"
51
52 #include "vktSpvAsm16bitStorageTests.hpp"
53 #include "vktSpvAsmUboMatrixPaddingTests.hpp"
54 #include "vktSpvAsmConditionalBranchTests.hpp"
55 #include "vktSpvAsmIndexingTests.hpp"
56 #include "vktSpvAsmImageSamplerTests.hpp"
57 #include "vktSpvAsmComputeShaderCase.hpp"
58 #include "vktSpvAsmComputeShaderTestUtil.hpp"
59 #include "vktSpvAsmGraphicsShaderTestUtil.hpp"
60 #include "vktSpvAsmVariablePointersTests.hpp"
61 #include "vktTestCaseUtil.hpp"
62
63 #include <cmath>
64 #include <limits>
65 #include <map>
66 #include <string>
67 #include <sstream>
68 #include <utility>
69 #include <stack>
70
71 namespace vkt
72 {
73 namespace SpirVAssembly
74 {
75
76 namespace
77 {
78
79 using namespace vk;
80 using std::map;
81 using std::string;
82 using std::vector;
83 using tcu::IVec3;
84 using tcu::IVec4;
85 using tcu::RGBA;
86 using tcu::TestLog;
87 using tcu::TestStatus;
88 using tcu::Vec4;
89 using de::UniquePtr;
90 using tcu::StringTemplate;
91 using tcu::Vec4;
92
93 template<typename T>
94 static void fillRandomScalars (de::Random& rnd, T minValue, T maxValue, void* dst, int numValues, int offset = 0)
95 {
96         T* const typedPtr = (T*)dst;
97         for (int ndx = 0; ndx < numValues; ndx++)
98                 typedPtr[offset + ndx] = randomScalar<T>(rnd, minValue, maxValue);
99 }
100
101 // Filter is a function that returns true if a value should pass, false otherwise.
102 template<typename T, typename FilterT>
103 static void fillRandomScalars (de::Random& rnd, T minValue, T maxValue, void* dst, int numValues, FilterT filter, int offset = 0)
104 {
105         T* const typedPtr = (T*)dst;
106         T value;
107         for (int ndx = 0; ndx < numValues; ndx++)
108         {
109                 do
110                         value = randomScalar<T>(rnd, minValue, maxValue);
111                 while (!filter(value));
112
113                 typedPtr[offset + ndx] = value;
114         }
115 }
116
117 // Gets a 64-bit integer with a more logarithmic distribution
118 deInt64 randomInt64LogDistributed (de::Random& rnd)
119 {
120         deInt64 val = rnd.getUint64();
121         val &= (1ull << rnd.getInt(1, 63)) - 1;
122         if (rnd.getBool())
123                 val = -val;
124         return val;
125 }
126
127 static void fillRandomInt64sLogDistributed (de::Random& rnd, vector<deInt64>& dst, int numValues)
128 {
129         for (int ndx = 0; ndx < numValues; ndx++)
130                 dst[ndx] = randomInt64LogDistributed(rnd);
131 }
132
133 template<typename FilterT>
134 static void fillRandomInt64sLogDistributed (de::Random& rnd, vector<deInt64>& dst, int numValues, FilterT filter)
135 {
136         for (int ndx = 0; ndx < numValues; ndx++)
137         {
138                 deInt64 value;
139                 do {
140                         value = randomInt64LogDistributed(rnd);
141                 } while (!filter(value));
142                 dst[ndx] = value;
143         }
144 }
145
146 inline bool filterNonNegative (const deInt64 value)
147 {
148         return value >= 0;
149 }
150
151 inline bool filterPositive (const deInt64 value)
152 {
153         return value > 0;
154 }
155
156 inline bool filterNotZero (const deInt64 value)
157 {
158         return value != 0;
159 }
160
161 static void floorAll (vector<float>& values)
162 {
163         for (size_t i = 0; i < values.size(); i++)
164                 values[i] = deFloatFloor(values[i]);
165 }
166
167 static void floorAll (vector<Vec4>& values)
168 {
169         for (size_t i = 0; i < values.size(); i++)
170                 values[i] = floor(values[i]);
171 }
172
173 struct CaseParameter
174 {
175         const char*             name;
176         string                  param;
177
178         CaseParameter   (const char* case_, const string& param_) : name(case_), param(param_) {}
179 };
180
181 // Assembly code used for testing OpNop, OpConstant{Null|Composite}, Op[No]Line, OpSource[Continued], OpSourceExtension, OpUndef is based on GLSL source code:
182 //
183 // #version 430
184 //
185 // layout(std140, set = 0, binding = 0) readonly buffer Input {
186 //   float elements[];
187 // } input_data;
188 // layout(std140, set = 0, binding = 1) writeonly buffer Output {
189 //   float elements[];
190 // } output_data;
191 //
192 // layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
193 //
194 // void main() {
195 //   uint x = gl_GlobalInvocationID.x;
196 //   output_data.elements[x] = -input_data.elements[x];
197 // }
198
199
200 static string getAsmForOpNopTest(bool useLiteralLocalSize, bool useSpecConstantWorkgroupSize) {
201         std::ostringstream out;
202         out << getComputeAsmShaderPreambleWithoutLocalSize();
203         if (useLiteralLocalSize) {
204                 out << "OpExecutionMode %main LocalSize 1 1 1\n";
205         }
206
207         out << "OpSource GLSL 430\n"
208                 "OpName %main           \"main\"\n"
209                 "OpName %id             \"gl_GlobalInvocationID\"\n"
210                 "OpDecorate %id BuiltIn GlobalInvocationId\n";
211
212         if (useSpecConstantWorkgroupSize) {
213                 out << "OpDecorate %spec_0 SpecId 100\n"
214                             "OpDecorate %spec_0 SpecId 100\n"
215                             "OpDecorate %spec_1 SpecId 101\n"
216                             "OpDecorate %spec_2 SpecId 102\n"
217                             "OpDecorate %gl_WorkGroupSize BuiltIn WorkgroupSize\n";
218         }
219
220         out << getComputeAsmInputOutputBufferTraits()
221                 << getComputeAsmCommonTypes()
222                 << getComputeAsmInputOutputBuffer()
223                 << "%id        = OpVariable %uvec3ptr Input\n"
224                 << "%zero      = OpConstant %i32 0\n";
225
226         if (useSpecConstantWorkgroupSize) {
227                 out << "%spec_0   = OpSpecConstant %u32 1\n"
228                 "%spec_1   = OpSpecConstant %u32 1\n"
229                 "%spec_2   = OpSpecConstant %u32 1\n"
230                 "%gl_WorkGroupSize = OpSpecConstantComposite %uvec3 %spec_0 %spec_1 %spec_2\n";
231         }
232
233         out << "%main      = OpFunction %void None %voidf\n"
234                 "%label     = OpLabel\n"
235                 "%idval     = OpLoad %uvec3 %id\n"
236                 "%x         = OpCompositeExtract %u32 %idval 0\n"
237
238                 "             OpNop\n" // Inside a function body
239
240                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
241                 "%inval     = OpLoad %f32 %inloc\n"
242                 "%neg       = OpFNegate %f32 %inval\n"
243                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
244                 "             OpStore %outloc %neg\n"
245                 "             OpReturn\n"
246                 "             OpFunctionEnd\n";
247     return out.str();
248 }
249
250 tcu::TestCaseGroup* createOpNopGroup (tcu::TestContext& testCtx)
251 {
252         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opnop", "Test the OpNop instruction"));
253         ComputeShaderSpec                               spec;
254         de::Random                                              rnd                             (deStringHash(group->getName()));
255         const int                                               numElements             = 100;
256         vector<float>                                   positiveFloats  (numElements, 0);
257         vector<float>                                   negativeFloats  (numElements, 0);
258
259         fillRandomScalars(rnd, 1.f, 100.f, &positiveFloats[0], numElements);
260
261         for (size_t ndx = 0; ndx < numElements; ++ndx)
262                 negativeFloats[ndx] = -positiveFloats[ndx];
263
264         spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
265         spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
266         spec.numWorkGroups = IVec3(numElements, 1, 1);
267
268     spec.assembly = getAsmForOpNopTest(true, false);
269         group->addChild(new SpvAsmComputeShaderCase(testCtx, "literal_localsize", "OpNop appearing at different places", spec));
270
271     spec.assembly = getAsmForOpNopTest(true, true);
272         group->addChild(new SpvAsmComputeShaderCase(testCtx, "literal_and_specid_localsize", "OpNop appearing at different places", spec));
273
274     spec.assembly = getAsmForOpNopTest(false, true);
275         group->addChild(new SpvAsmComputeShaderCase(testCtx, "specid_localsize", "OpNop appearing at different places", spec));
276
277         return group.release();
278 }
279
280 bool compareFUnord (const std::vector<BufferSp>& inputs, const vector<AllocationSp>& outputAllocs, const std::vector<BufferSp>& expectedOutputs, TestLog& log)
281 {
282         if (outputAllocs.size() != 1)
283                 return false;
284
285         vector<deUint8> input1Bytes;
286         vector<deUint8> input2Bytes;
287         vector<deUint8> expectedBytes;
288
289         inputs[0]->getBytes(input1Bytes);
290         inputs[1]->getBytes(input2Bytes);
291         expectedOutputs[0]->getBytes(expectedBytes);
292
293         const deInt32* const    expectedOutputAsInt             = reinterpret_cast<const deInt32* const>(&expectedBytes.front());
294         const deInt32* const    outputAsInt                             = static_cast<const deInt32* const>(outputAllocs[0]->getHostPtr());
295         const float* const              input1AsFloat                   = reinterpret_cast<const float* const>(&input1Bytes.front());
296         const float* const              input2AsFloat                   = reinterpret_cast<const float* const>(&input2Bytes.front());
297         bool returnValue                                                                = true;
298
299         for (size_t idx = 0; idx < expectedBytes.size() / sizeof(deInt32); ++idx)
300         {
301                 if (outputAsInt[idx] != expectedOutputAsInt[idx])
302                 {
303                         log << TestLog::Message << "ERROR: Sub-case failed. inputs: " << input1AsFloat[idx] << "," << input2AsFloat[idx] << " output: " << outputAsInt[idx]<< " expected output: " << expectedOutputAsInt[idx] << TestLog::EndMessage;
304                         returnValue = false;
305                 }
306         }
307         return returnValue;
308 }
309
310 typedef VkBool32 (*compareFuncType) (float, float);
311
312 struct OpFUnordCase
313 {
314         const char*             name;
315         const char*             opCode;
316         compareFuncType compareFunc;
317
318                                         OpFUnordCase                    (const char* _name, const char* _opCode, compareFuncType _compareFunc)
319                                                 : name                          (_name)
320                                                 , opCode                        (_opCode)
321                                                 , compareFunc           (_compareFunc) {}
322 };
323
324 #define ADD_OPFUNORD_CASE(NAME, OPCODE, OPERATOR) \
325 do { \
326     struct compare_##NAME { static VkBool32 compare(float x, float y) { return (x OPERATOR y) ? VK_TRUE : VK_FALSE; } }; \
327     cases.push_back(OpFUnordCase(#NAME, OPCODE, compare_##NAME::compare)); \
328 } while (deGetFalse())
329
330 tcu::TestCaseGroup* createOpFUnordGroup (tcu::TestContext& testCtx)
331 {
332         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opfunord", "Test the OpFUnord* opcodes"));
333         de::Random                                              rnd                             (deStringHash(group->getName()));
334         const int                                               numElements             = 100;
335         vector<OpFUnordCase>                    cases;
336
337         const StringTemplate                    shaderTemplate  (
338
339                 string(getComputeAsmShaderPreamble()) +
340
341                 "OpSource GLSL 430\n"
342                 "OpName %main           \"main\"\n"
343                 "OpName %id             \"gl_GlobalInvocationID\"\n"
344
345                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
346
347                 "OpDecorate %buf BufferBlock\n"
348                 "OpDecorate %buf2 BufferBlock\n"
349                 "OpDecorate %indata1 DescriptorSet 0\n"
350                 "OpDecorate %indata1 Binding 0\n"
351                 "OpDecorate %indata2 DescriptorSet 0\n"
352                 "OpDecorate %indata2 Binding 1\n"
353                 "OpDecorate %outdata DescriptorSet 0\n"
354                 "OpDecorate %outdata Binding 2\n"
355                 "OpDecorate %f32arr ArrayStride 4\n"
356                 "OpDecorate %i32arr ArrayStride 4\n"
357                 "OpMemberDecorate %buf 0 Offset 0\n"
358                 "OpMemberDecorate %buf2 0 Offset 0\n"
359
360                 + string(getComputeAsmCommonTypes()) +
361
362                 "%buf        = OpTypeStruct %f32arr\n"
363                 "%bufptr     = OpTypePointer Uniform %buf\n"
364                 "%indata1    = OpVariable %bufptr Uniform\n"
365                 "%indata2    = OpVariable %bufptr Uniform\n"
366
367                 "%buf2       = OpTypeStruct %i32arr\n"
368                 "%buf2ptr    = OpTypePointer Uniform %buf2\n"
369                 "%outdata    = OpVariable %buf2ptr Uniform\n"
370
371                 "%id        = OpVariable %uvec3ptr Input\n"
372                 "%zero      = OpConstant %i32 0\n"
373                 "%consti1   = OpConstant %i32 1\n"
374                 "%constf1   = OpConstant %f32 1.0\n"
375
376                 "%main      = OpFunction %void None %voidf\n"
377                 "%label     = OpLabel\n"
378                 "%idval     = OpLoad %uvec3 %id\n"
379                 "%x         = OpCompositeExtract %u32 %idval 0\n"
380
381                 "%inloc1    = OpAccessChain %f32ptr %indata1 %zero %x\n"
382                 "%inval1    = OpLoad %f32 %inloc1\n"
383                 "%inloc2    = OpAccessChain %f32ptr %indata2 %zero %x\n"
384                 "%inval2    = OpLoad %f32 %inloc2\n"
385                 "%outloc    = OpAccessChain %i32ptr %outdata %zero %x\n"
386
387                 "%result    = ${OPCODE} %bool %inval1 %inval2\n"
388                 "%int_res   = OpSelect %i32 %result %consti1 %zero\n"
389                 "             OpStore %outloc %int_res\n"
390
391                 "             OpReturn\n"
392                 "             OpFunctionEnd\n");
393
394         ADD_OPFUNORD_CASE(equal, "OpFUnordEqual", ==);
395         ADD_OPFUNORD_CASE(less, "OpFUnordLessThan", <);
396         ADD_OPFUNORD_CASE(lessequal, "OpFUnordLessThanEqual", <=);
397         ADD_OPFUNORD_CASE(greater, "OpFUnordGreaterThan", >);
398         ADD_OPFUNORD_CASE(greaterequal, "OpFUnordGreaterThanEqual", >=);
399         ADD_OPFUNORD_CASE(notequal, "OpFUnordNotEqual", !=);
400
401         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
402         {
403                 map<string, string>                     specializations;
404                 ComputeShaderSpec                       spec;
405                 const float                                     NaN                             = std::numeric_limits<float>::quiet_NaN();
406                 vector<float>                           inputFloats1    (numElements, 0);
407                 vector<float>                           inputFloats2    (numElements, 0);
408                 vector<deInt32>                         expectedInts    (numElements, 0);
409
410                 specializations["OPCODE"]       = cases[caseNdx].opCode;
411                 spec.assembly                           = shaderTemplate.specialize(specializations);
412
413                 fillRandomScalars(rnd, 1.f, 100.f, &inputFloats1[0], numElements);
414                 for (size_t ndx = 0; ndx < numElements; ++ndx)
415                 {
416                         switch (ndx % 6)
417                         {
418                                 case 0:         inputFloats2[ndx] = inputFloats1[ndx] + 1.0f; break;
419                                 case 1:         inputFloats2[ndx] = inputFloats1[ndx] - 1.0f; break;
420                                 case 2:         inputFloats2[ndx] = inputFloats1[ndx]; break;
421                                 case 3:         inputFloats2[ndx] = NaN; break;
422                                 case 4:         inputFloats2[ndx] = inputFloats1[ndx];  inputFloats1[ndx] = NaN; break;
423                                 case 5:         inputFloats2[ndx] = NaN;                                inputFloats1[ndx] = NaN; break;
424                         }
425                         expectedInts[ndx] = tcu::Float32(inputFloats1[ndx]).isNaN() || tcu::Float32(inputFloats2[ndx]).isNaN() || cases[caseNdx].compareFunc(inputFloats1[ndx], inputFloats2[ndx]);
426                 }
427
428                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats1)));
429                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats2)));
430                 spec.outputs.push_back(BufferSp(new Int32Buffer(expectedInts)));
431                 spec.numWorkGroups = IVec3(numElements, 1, 1);
432                 spec.verifyIO = &compareFUnord;
433                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
434         }
435
436         return group.release();
437 }
438
439 struct OpAtomicCase
440 {
441         const char*             name;
442         const char*             assembly;
443         OpAtomicType    opAtomic;
444         deInt32                 numOutputElements;
445
446                                         OpAtomicCase                    (const char* _name, const char* _assembly, OpAtomicType _opAtomic, deInt32 _numOutputElements)
447                                                 : name                          (_name)
448                                                 , assembly                      (_assembly)
449                                                 , opAtomic                      (_opAtomic)
450                                                 , numOutputElements     (_numOutputElements) {}
451 };
452
453 tcu::TestCaseGroup* createOpAtomicGroup (tcu::TestContext& testCtx, bool useStorageBuffer)
454 {
455         de::MovePtr<tcu::TestCaseGroup> group                           (new tcu::TestCaseGroup(testCtx,
456                                                                                                                                                                 useStorageBuffer ? "opatomic_storage_buffer" : "opatomic",
457                                                                                                                                                                 "Test the OpAtomic* opcodes"));
458         const int                                               numElements                     = 65535;
459         vector<OpAtomicCase>                    cases;
460
461         const StringTemplate                    shaderTemplate  (
462
463                 string("OpCapability Shader\n") +
464                 (useStorageBuffer ? "OpExtension \"SPV_KHR_storage_buffer_storage_class\"\n" : "") +
465                 "OpMemoryModel Logical GLSL450\n"
466                 "OpEntryPoint GLCompute %main \"main\" %id\n"
467                 "OpExecutionMode %main LocalSize 1 1 1\n" +
468
469                 "OpSource GLSL 430\n"
470                 "OpName %main           \"main\"\n"
471                 "OpName %id             \"gl_GlobalInvocationID\"\n"
472
473                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
474
475                 "OpDecorate %buf ${BLOCK_DECORATION}\n"
476                 "OpDecorate %indata DescriptorSet 0\n"
477                 "OpDecorate %indata Binding 0\n"
478                 "OpDecorate %i32arr ArrayStride 4\n"
479                 "OpMemberDecorate %buf 0 Offset 0\n"
480
481                 "OpDecorate %sumbuf ${BLOCK_DECORATION}\n"
482                 "OpDecorate %sum DescriptorSet 0\n"
483                 "OpDecorate %sum Binding 1\n"
484                 "OpMemberDecorate %sumbuf 0 Coherent\n"
485                 "OpMemberDecorate %sumbuf 0 Offset 0\n"
486
487                 + getComputeAsmCommonTypes("${BLOCK_POINTER_TYPE}") +
488
489                 "%buf       = OpTypeStruct %i32arr\n"
490                 "%bufptr    = OpTypePointer ${BLOCK_POINTER_TYPE} %buf\n"
491                 "%indata    = OpVariable %bufptr ${BLOCK_POINTER_TYPE}\n"
492
493                 "%sumbuf    = OpTypeStruct %i32arr\n"
494                 "%sumbufptr = OpTypePointer ${BLOCK_POINTER_TYPE} %sumbuf\n"
495                 "%sum       = OpVariable %sumbufptr ${BLOCK_POINTER_TYPE}\n"
496
497                 "%id        = OpVariable %uvec3ptr Input\n"
498                 "%minusone  = OpConstant %i32 -1\n"
499                 "%zero      = OpConstant %i32 0\n"
500                 "%one       = OpConstant %u32 1\n"
501                 "%two       = OpConstant %i32 2\n"
502
503                 "%main      = OpFunction %void None %voidf\n"
504                 "%label     = OpLabel\n"
505                 "%idval     = OpLoad %uvec3 %id\n"
506                 "%x         = OpCompositeExtract %u32 %idval 0\n"
507
508                 "%inloc     = OpAccessChain %i32ptr %indata %zero %x\n"
509                 "%inval     = OpLoad %i32 %inloc\n"
510
511                 "%outloc    = OpAccessChain %i32ptr %sum %zero ${INDEX}\n"
512                 "${INSTRUCTION}"
513
514                 "             OpReturn\n"
515                 "             OpFunctionEnd\n");
516
517         #define ADD_OPATOMIC_CASE(NAME, ASSEMBLY, OPATOMIC, NUM_OUTPUT_ELEMENTS) \
518         do { \
519                 DE_STATIC_ASSERT((NUM_OUTPUT_ELEMENTS) == 1 || (NUM_OUTPUT_ELEMENTS) == numElements); \
520                 cases.push_back(OpAtomicCase(#NAME, ASSEMBLY, OPATOMIC, NUM_OUTPUT_ELEMENTS)); \
521         } while (deGetFalse())
522         #define ADD_OPATOMIC_CASE_1(NAME, ASSEMBLY, OPATOMIC) ADD_OPATOMIC_CASE(NAME, ASSEMBLY, OPATOMIC, 1)
523         #define ADD_OPATOMIC_CASE_N(NAME, ASSEMBLY, OPATOMIC) ADD_OPATOMIC_CASE(NAME, ASSEMBLY, OPATOMIC, numElements)
524
525         ADD_OPATOMIC_CASE_1(iadd,       "%unused    = OpAtomicIAdd %i32 %outloc %one %zero %inval\n", OPATOMIC_IADD );
526         ADD_OPATOMIC_CASE_1(isub,       "%unused    = OpAtomicISub %i32 %outloc %one %zero %inval\n", OPATOMIC_ISUB );
527         ADD_OPATOMIC_CASE_1(iinc,       "%unused    = OpAtomicIIncrement %i32 %outloc %one %zero\n",  OPATOMIC_IINC );
528         ADD_OPATOMIC_CASE_1(idec,       "%unused    = OpAtomicIDecrement %i32 %outloc %one %zero\n",  OPATOMIC_IDEC );
529         ADD_OPATOMIC_CASE_N(load,       "%inval2    = OpAtomicLoad %i32 %inloc %zero %zero\n"
530                                                                 "             OpStore %outloc %inval2\n",  OPATOMIC_LOAD );
531         ADD_OPATOMIC_CASE_N(store,      "             OpAtomicStore %outloc %zero %zero %inval\n",  OPATOMIC_STORE );
532         ADD_OPATOMIC_CASE_N(compex, "%even      = OpSMod %i32 %inval %two\n"
533                                                                 "             OpStore %outloc %even\n"
534                                                                 "%unused    = OpAtomicCompareExchange %i32 %outloc %one %zero %zero %minusone %zero\n",  OPATOMIC_COMPEX );
535
536         #undef ADD_OPATOMIC_CASE
537         #undef ADD_OPATOMIC_CASE_1
538         #undef ADD_OPATOMIC_CASE_N
539
540         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
541         {
542                 map<string, string>                     specializations;
543                 ComputeShaderSpec                       spec;
544                 vector<deInt32>                         inputInts               (numElements, 0);
545                 vector<deInt32>                         expected                (cases[caseNdx].numOutputElements, -1);
546
547                 specializations["INDEX"]                                = (cases[caseNdx].numOutputElements == 1) ? "%zero" : "%x";
548                 specializations["INSTRUCTION"]                  = cases[caseNdx].assembly;
549                 specializations["BLOCK_DECORATION"]             = useStorageBuffer ? "Block" : "BufferBlock";
550                 specializations["BLOCK_POINTER_TYPE"]   = useStorageBuffer ? "StorageBuffer" : "Uniform";
551                 spec.assembly                                                   = shaderTemplate.specialize(specializations);
552
553                 if (useStorageBuffer)
554                         spec.extensions.push_back("VK_KHR_storage_buffer_storage_class");
555
556                 spec.inputs.push_back(BufferSp(new OpAtomicBuffer(numElements, cases[caseNdx].numOutputElements, cases[caseNdx].opAtomic, BUFFERTYPE_INPUT)));
557                 spec.outputs.push_back(BufferSp(new OpAtomicBuffer(numElements, cases[caseNdx].numOutputElements, cases[caseNdx].opAtomic, BUFFERTYPE_EXPECTED)));
558                 spec.numWorkGroups = IVec3(numElements, 1, 1);
559                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
560         }
561
562         return group.release();
563 }
564
565 tcu::TestCaseGroup* createOpLineGroup (tcu::TestContext& testCtx)
566 {
567         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opline", "Test the OpLine instruction"));
568         ComputeShaderSpec                               spec;
569         de::Random                                              rnd                             (deStringHash(group->getName()));
570         const int                                               numElements             = 100;
571         vector<float>                                   positiveFloats  (numElements, 0);
572         vector<float>                                   negativeFloats  (numElements, 0);
573
574         fillRandomScalars(rnd, 1.f, 100.f, &positiveFloats[0], numElements);
575
576         for (size_t ndx = 0; ndx < numElements; ++ndx)
577                 negativeFloats[ndx] = -positiveFloats[ndx];
578
579         spec.assembly =
580                 string(getComputeAsmShaderPreamble()) +
581
582                 "%fname1 = OpString \"negateInputs.comp\"\n"
583                 "%fname2 = OpString \"negateInputs\"\n"
584
585                 "OpSource GLSL 430\n"
586                 "OpName %main           \"main\"\n"
587                 "OpName %id             \"gl_GlobalInvocationID\"\n"
588
589                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
590
591                 + string(getComputeAsmInputOutputBufferTraits()) +
592
593                 "OpLine %fname1 0 0\n" // At the earliest possible position
594
595                 + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
596
597                 "OpLine %fname1 0 1\n" // Multiple OpLines in sequence
598                 "OpLine %fname2 1 0\n" // Different filenames
599                 "OpLine %fname1 1000 100000\n"
600
601                 "%id        = OpVariable %uvec3ptr Input\n"
602                 "%zero      = OpConstant %i32 0\n"
603
604                 "OpLine %fname1 1 1\n" // Before a function
605
606                 "%main      = OpFunction %void None %voidf\n"
607                 "%label     = OpLabel\n"
608
609                 "OpLine %fname1 1 1\n" // In a function
610
611                 "%idval     = OpLoad %uvec3 %id\n"
612                 "%x         = OpCompositeExtract %u32 %idval 0\n"
613                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
614                 "%inval     = OpLoad %f32 %inloc\n"
615                 "%neg       = OpFNegate %f32 %inval\n"
616                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
617                 "             OpStore %outloc %neg\n"
618                 "             OpReturn\n"
619                 "             OpFunctionEnd\n";
620         spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
621         spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
622         spec.numWorkGroups = IVec3(numElements, 1, 1);
623
624         group->addChild(new SpvAsmComputeShaderCase(testCtx, "all", "OpLine appearing at different places", spec));
625
626         return group.release();
627 }
628
629 tcu::TestCaseGroup* createOpNoLineGroup (tcu::TestContext& testCtx)
630 {
631         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opnoline", "Test the OpNoLine instruction"));
632         ComputeShaderSpec                               spec;
633         de::Random                                              rnd                             (deStringHash(group->getName()));
634         const int                                               numElements             = 100;
635         vector<float>                                   positiveFloats  (numElements, 0);
636         vector<float>                                   negativeFloats  (numElements, 0);
637
638         fillRandomScalars(rnd, 1.f, 100.f, &positiveFloats[0], numElements);
639
640         for (size_t ndx = 0; ndx < numElements; ++ndx)
641                 negativeFloats[ndx] = -positiveFloats[ndx];
642
643         spec.assembly =
644                 string(getComputeAsmShaderPreamble()) +
645
646                 "%fname = OpString \"negateInputs.comp\"\n"
647
648                 "OpSource GLSL 430\n"
649                 "OpName %main           \"main\"\n"
650                 "OpName %id             \"gl_GlobalInvocationID\"\n"
651
652                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
653
654                 + string(getComputeAsmInputOutputBufferTraits()) +
655
656                 "OpNoLine\n" // At the earliest possible position, without preceding OpLine
657
658                 + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
659
660                 "OpLine %fname 0 1\n"
661                 "OpNoLine\n" // Immediately following a preceding OpLine
662
663                 "OpLine %fname 1000 1\n"
664
665                 "%id        = OpVariable %uvec3ptr Input\n"
666                 "%zero      = OpConstant %i32 0\n"
667
668                 "OpNoLine\n" // Contents after the previous OpLine
669
670                 "%main      = OpFunction %void None %voidf\n"
671                 "%label     = OpLabel\n"
672                 "%idval     = OpLoad %uvec3 %id\n"
673                 "%x         = OpCompositeExtract %u32 %idval 0\n"
674
675                 "OpNoLine\n" // Multiple OpNoLine
676                 "OpNoLine\n"
677                 "OpNoLine\n"
678
679                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
680                 "%inval     = OpLoad %f32 %inloc\n"
681                 "%neg       = OpFNegate %f32 %inval\n"
682                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
683                 "             OpStore %outloc %neg\n"
684                 "             OpReturn\n"
685                 "             OpFunctionEnd\n";
686         spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
687         spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
688         spec.numWorkGroups = IVec3(numElements, 1, 1);
689
690         group->addChild(new SpvAsmComputeShaderCase(testCtx, "all", "OpNoLine appearing at different places", spec));
691
692         return group.release();
693 }
694
695 // Compare instruction for the contraction compute case.
696 // Returns true if the output is what is expected from the test case.
697 bool compareNoContractCase(const std::vector<BufferSp>&, const vector<AllocationSp>& outputAllocs, const std::vector<BufferSp>& expectedOutputs, TestLog&)
698 {
699         if (outputAllocs.size() != 1)
700                 return false;
701
702         // Only size is needed because we are not comparing the exact values.
703         size_t byteSize = expectedOutputs[0]->getByteSize();
704
705         const float*    outputAsFloat   = static_cast<const float*>(outputAllocs[0]->getHostPtr());
706
707         for(size_t i = 0; i < byteSize / sizeof(float); ++i) {
708                 if (outputAsFloat[i] != 0.f &&
709                         outputAsFloat[i] != -ldexp(1, -24)) {
710                         return false;
711                 }
712         }
713
714         return true;
715 }
716
717 tcu::TestCaseGroup* createNoContractionGroup (tcu::TestContext& testCtx)
718 {
719         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "nocontraction", "Test the NoContraction decoration"));
720         vector<CaseParameter>                   cases;
721         const int                                               numElements             = 100;
722         vector<float>                                   inputFloats1    (numElements, 0);
723         vector<float>                                   inputFloats2    (numElements, 0);
724         vector<float>                                   outputFloats    (numElements, 0);
725         const StringTemplate                    shaderTemplate  (
726                 string(getComputeAsmShaderPreamble()) +
727
728                 "OpName %main           \"main\"\n"
729                 "OpName %id             \"gl_GlobalInvocationID\"\n"
730
731                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
732
733                 "${DECORATION}\n"
734
735                 "OpDecorate %buf BufferBlock\n"
736                 "OpDecorate %indata1 DescriptorSet 0\n"
737                 "OpDecorate %indata1 Binding 0\n"
738                 "OpDecorate %indata2 DescriptorSet 0\n"
739                 "OpDecorate %indata2 Binding 1\n"
740                 "OpDecorate %outdata DescriptorSet 0\n"
741                 "OpDecorate %outdata Binding 2\n"
742                 "OpDecorate %f32arr ArrayStride 4\n"
743                 "OpMemberDecorate %buf 0 Offset 0\n"
744
745                 + string(getComputeAsmCommonTypes()) +
746
747                 "%buf        = OpTypeStruct %f32arr\n"
748                 "%bufptr     = OpTypePointer Uniform %buf\n"
749                 "%indata1    = OpVariable %bufptr Uniform\n"
750                 "%indata2    = OpVariable %bufptr Uniform\n"
751                 "%outdata    = OpVariable %bufptr Uniform\n"
752
753                 "%id         = OpVariable %uvec3ptr Input\n"
754                 "%zero       = OpConstant %i32 0\n"
755                 "%c_f_m1     = OpConstant %f32 -1.\n"
756
757                 "%main       = OpFunction %void None %voidf\n"
758                 "%label      = OpLabel\n"
759                 "%idval      = OpLoad %uvec3 %id\n"
760                 "%x          = OpCompositeExtract %u32 %idval 0\n"
761                 "%inloc1     = OpAccessChain %f32ptr %indata1 %zero %x\n"
762                 "%inval1     = OpLoad %f32 %inloc1\n"
763                 "%inloc2     = OpAccessChain %f32ptr %indata2 %zero %x\n"
764                 "%inval2     = OpLoad %f32 %inloc2\n"
765                 "%mul        = OpFMul %f32 %inval1 %inval2\n"
766                 "%add        = OpFAdd %f32 %mul %c_f_m1\n"
767                 "%outloc     = OpAccessChain %f32ptr %outdata %zero %x\n"
768                 "              OpStore %outloc %add\n"
769                 "              OpReturn\n"
770                 "              OpFunctionEnd\n");
771
772         cases.push_back(CaseParameter("multiplication", "OpDecorate %mul NoContraction"));
773         cases.push_back(CaseParameter("addition",               "OpDecorate %add NoContraction"));
774         cases.push_back(CaseParameter("both",                   "OpDecorate %mul NoContraction\nOpDecorate %add NoContraction"));
775
776         for (size_t ndx = 0; ndx < numElements; ++ndx)
777         {
778                 inputFloats1[ndx]       = 1.f + std::ldexp(1.f, -23); // 1 + 2^-23.
779                 inputFloats2[ndx]       = 1.f - std::ldexp(1.f, -23); // 1 - 2^-23.
780                 // Result for (1 + 2^-23) * (1 - 2^-23) - 1. With NoContraction, the multiplication will be
781                 // conducted separately and the result is rounded to 1, or 0x1.fffffcp-1
782                 // So the final result will be 0.f or 0x1p-24.
783                 // If the operation is combined into a precise fused multiply-add, then the result would be
784                 // 2^-46 (0xa8800000).
785                 outputFloats[ndx]       = 0.f;
786         }
787
788         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
789         {
790                 map<string, string>             specializations;
791                 ComputeShaderSpec               spec;
792
793                 specializations["DECORATION"] = cases[caseNdx].param;
794                 spec.assembly = shaderTemplate.specialize(specializations);
795                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats1)));
796                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats2)));
797                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
798                 spec.numWorkGroups = IVec3(numElements, 1, 1);
799                 // Check against the two possible answers based on rounding mode.
800                 spec.verifyIO = &compareNoContractCase;
801
802                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
803         }
804         return group.release();
805 }
806
807 bool compareFRem(const std::vector<BufferSp>&, const vector<AllocationSp>& outputAllocs, const std::vector<BufferSp>& expectedOutputs, TestLog&)
808 {
809         if (outputAllocs.size() != 1)
810                 return false;
811
812         vector<deUint8> expectedBytes;
813         expectedOutputs[0]->getBytes(expectedBytes);
814
815         const float*    expectedOutputAsFloat   = reinterpret_cast<const float*>(&expectedBytes.front());
816         const float*    outputAsFloat                   = static_cast<const float*>(outputAllocs[0]->getHostPtr());
817
818         for (size_t idx = 0; idx < expectedBytes.size() / sizeof(float); ++idx)
819         {
820                 const float f0 = expectedOutputAsFloat[idx];
821                 const float f1 = outputAsFloat[idx];
822                 // \todo relative error needs to be fairly high because FRem may be implemented as
823                 // (roughly) frac(a/b)*b, so LSB errors can be magnified. But this should be fine for now.
824                 if (deFloatAbs((f1 - f0) / f0) > 0.02)
825                         return false;
826         }
827
828         return true;
829 }
830
831 tcu::TestCaseGroup* createOpFRemGroup (tcu::TestContext& testCtx)
832 {
833         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opfrem", "Test the OpFRem instruction"));
834         ComputeShaderSpec                               spec;
835         de::Random                                              rnd                             (deStringHash(group->getName()));
836         const int                                               numElements             = 200;
837         vector<float>                                   inputFloats1    (numElements, 0);
838         vector<float>                                   inputFloats2    (numElements, 0);
839         vector<float>                                   outputFloats    (numElements, 0);
840
841         fillRandomScalars(rnd, -10000.f, 10000.f, &inputFloats1[0], numElements);
842         fillRandomScalars(rnd, -100.f, 100.f, &inputFloats2[0], numElements);
843
844         for (size_t ndx = 0; ndx < numElements; ++ndx)
845         {
846                 // Guard against divisors near zero.
847                 if (std::fabs(inputFloats2[ndx]) < 1e-3)
848                         inputFloats2[ndx] = 8.f;
849
850                 // The return value of std::fmod() has the same sign as its first operand, which is how OpFRem spec'd.
851                 outputFloats[ndx] = std::fmod(inputFloats1[ndx], inputFloats2[ndx]);
852         }
853
854         spec.assembly =
855                 string(getComputeAsmShaderPreamble()) +
856
857                 "OpName %main           \"main\"\n"
858                 "OpName %id             \"gl_GlobalInvocationID\"\n"
859
860                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
861
862                 "OpDecorate %buf BufferBlock\n"
863                 "OpDecorate %indata1 DescriptorSet 0\n"
864                 "OpDecorate %indata1 Binding 0\n"
865                 "OpDecorate %indata2 DescriptorSet 0\n"
866                 "OpDecorate %indata2 Binding 1\n"
867                 "OpDecorate %outdata DescriptorSet 0\n"
868                 "OpDecorate %outdata Binding 2\n"
869                 "OpDecorate %f32arr ArrayStride 4\n"
870                 "OpMemberDecorate %buf 0 Offset 0\n"
871
872                 + string(getComputeAsmCommonTypes()) +
873
874                 "%buf        = OpTypeStruct %f32arr\n"
875                 "%bufptr     = OpTypePointer Uniform %buf\n"
876                 "%indata1    = OpVariable %bufptr Uniform\n"
877                 "%indata2    = OpVariable %bufptr Uniform\n"
878                 "%outdata    = OpVariable %bufptr Uniform\n"
879
880                 "%id        = OpVariable %uvec3ptr Input\n"
881                 "%zero      = OpConstant %i32 0\n"
882
883                 "%main      = OpFunction %void None %voidf\n"
884                 "%label     = OpLabel\n"
885                 "%idval     = OpLoad %uvec3 %id\n"
886                 "%x         = OpCompositeExtract %u32 %idval 0\n"
887                 "%inloc1    = OpAccessChain %f32ptr %indata1 %zero %x\n"
888                 "%inval1    = OpLoad %f32 %inloc1\n"
889                 "%inloc2    = OpAccessChain %f32ptr %indata2 %zero %x\n"
890                 "%inval2    = OpLoad %f32 %inloc2\n"
891                 "%rem       = OpFRem %f32 %inval1 %inval2\n"
892                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
893                 "             OpStore %outloc %rem\n"
894                 "             OpReturn\n"
895                 "             OpFunctionEnd\n";
896
897         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats1)));
898         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats2)));
899         spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
900         spec.numWorkGroups = IVec3(numElements, 1, 1);
901         spec.verifyIO = &compareFRem;
902
903         group->addChild(new SpvAsmComputeShaderCase(testCtx, "all", "", spec));
904
905         return group.release();
906 }
907
908 bool compareNMin (const std::vector<BufferSp>&, const vector<AllocationSp>& outputAllocs, const std::vector<BufferSp>& expectedOutputs, TestLog&)
909 {
910         if (outputAllocs.size() != 1)
911                 return false;
912
913         const BufferSp&                 expectedOutput                  (expectedOutputs[0]);
914         std::vector<deUint8>    data;
915         expectedOutput->getBytes(data);
916
917         const float* const              expectedOutputAsFloat   = reinterpret_cast<const float*>(&data.front());
918         const float* const              outputAsFloat                   = static_cast<const float*>(outputAllocs[0]->getHostPtr());
919
920         for (size_t idx = 0; idx < expectedOutput->getByteSize() / sizeof(float); ++idx)
921         {
922                 const float f0 = expectedOutputAsFloat[idx];
923                 const float f1 = outputAsFloat[idx];
924
925                 // For NMin, we accept NaN as output if both inputs were NaN.
926                 // Otherwise the NaN is the wrong choise, as on architectures that
927                 // do not handle NaN, those are huge values.
928                 if (!(tcu::Float32(f1).isNaN() && tcu::Float32(f0).isNaN()) && deFloatAbs(f1 - f0) > 0.00001f)
929                         return false;
930         }
931
932         return true;
933 }
934
935 tcu::TestCaseGroup* createOpNMinGroup (tcu::TestContext& testCtx)
936 {
937         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opnmin", "Test the OpNMin instruction"));
938         ComputeShaderSpec                               spec;
939         de::Random                                              rnd                             (deStringHash(group->getName()));
940         const int                                               numElements             = 200;
941         vector<float>                                   inputFloats1    (numElements, 0);
942         vector<float>                                   inputFloats2    (numElements, 0);
943         vector<float>                                   outputFloats    (numElements, 0);
944
945         fillRandomScalars(rnd, -10000.f, 10000.f, &inputFloats1[0], numElements);
946         fillRandomScalars(rnd, -10000.f, 10000.f, &inputFloats2[0], numElements);
947
948         // Make the first case a full-NAN case.
949         inputFloats1[0] = TCU_NAN;
950         inputFloats2[0] = TCU_NAN;
951
952         for (size_t ndx = 0; ndx < numElements; ++ndx)
953         {
954                 // By default, pick the smallest
955                 outputFloats[ndx] = std::min(inputFloats1[ndx], inputFloats2[ndx]);
956
957                 // Make half of the cases NaN cases
958                 if ((ndx & 1) == 0)
959                 {
960                         // Alternate between the NaN operand
961                         if ((ndx & 2) == 0)
962                         {
963                                 outputFloats[ndx] = inputFloats2[ndx];
964                                 inputFloats1[ndx] = TCU_NAN;
965                         }
966                         else
967                         {
968                                 outputFloats[ndx] = inputFloats1[ndx];
969                                 inputFloats2[ndx] = TCU_NAN;
970                         }
971                 }
972         }
973
974         spec.assembly =
975                 "OpCapability Shader\n"
976                 "%std450        = OpExtInstImport \"GLSL.std.450\"\n"
977                 "OpMemoryModel Logical GLSL450\n"
978                 "OpEntryPoint GLCompute %main \"main\" %id\n"
979                 "OpExecutionMode %main LocalSize 1 1 1\n"
980
981                 "OpName %main           \"main\"\n"
982                 "OpName %id             \"gl_GlobalInvocationID\"\n"
983
984                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
985
986                 "OpDecorate %buf BufferBlock\n"
987                 "OpDecorate %indata1 DescriptorSet 0\n"
988                 "OpDecorate %indata1 Binding 0\n"
989                 "OpDecorate %indata2 DescriptorSet 0\n"
990                 "OpDecorate %indata2 Binding 1\n"
991                 "OpDecorate %outdata DescriptorSet 0\n"
992                 "OpDecorate %outdata Binding 2\n"
993                 "OpDecorate %f32arr ArrayStride 4\n"
994                 "OpMemberDecorate %buf 0 Offset 0\n"
995
996                 + string(getComputeAsmCommonTypes()) +
997
998                 "%buf        = OpTypeStruct %f32arr\n"
999                 "%bufptr     = OpTypePointer Uniform %buf\n"
1000                 "%indata1    = OpVariable %bufptr Uniform\n"
1001                 "%indata2    = OpVariable %bufptr Uniform\n"
1002                 "%outdata    = OpVariable %bufptr Uniform\n"
1003
1004                 "%id        = OpVariable %uvec3ptr Input\n"
1005                 "%zero      = OpConstant %i32 0\n"
1006
1007                 "%main      = OpFunction %void None %voidf\n"
1008                 "%label     = OpLabel\n"
1009                 "%idval     = OpLoad %uvec3 %id\n"
1010                 "%x         = OpCompositeExtract %u32 %idval 0\n"
1011                 "%inloc1    = OpAccessChain %f32ptr %indata1 %zero %x\n"
1012                 "%inval1    = OpLoad %f32 %inloc1\n"
1013                 "%inloc2    = OpAccessChain %f32ptr %indata2 %zero %x\n"
1014                 "%inval2    = OpLoad %f32 %inloc2\n"
1015                 "%rem       = OpExtInst %f32 %std450 NMin %inval1 %inval2\n"
1016                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
1017                 "             OpStore %outloc %rem\n"
1018                 "             OpReturn\n"
1019                 "             OpFunctionEnd\n";
1020
1021         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats1)));
1022         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats2)));
1023         spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
1024         spec.numWorkGroups = IVec3(numElements, 1, 1);
1025         spec.verifyIO = &compareNMin;
1026
1027         group->addChild(new SpvAsmComputeShaderCase(testCtx, "all", "", spec));
1028
1029         return group.release();
1030 }
1031
1032 bool compareNMax (const std::vector<BufferSp>&, const vector<AllocationSp>& outputAllocs, const std::vector<BufferSp>& expectedOutputs, TestLog&)
1033 {
1034         if (outputAllocs.size() != 1)
1035                 return false;
1036
1037         const BufferSp&                 expectedOutput                  = expectedOutputs[0];
1038         std::vector<deUint8>    data;
1039         expectedOutput->getBytes(data);
1040
1041         const float* const              expectedOutputAsFloat   = reinterpret_cast<const float*>(&data.front());
1042         const float* const              outputAsFloat                   = static_cast<const float*>(outputAllocs[0]->getHostPtr());
1043
1044         for (size_t idx = 0; idx < expectedOutput->getByteSize() / sizeof(float); ++idx)
1045         {
1046                 const float f0 = expectedOutputAsFloat[idx];
1047                 const float f1 = outputAsFloat[idx];
1048
1049                 // For NMax, NaN is considered acceptable result, since in
1050                 // architectures that do not handle NaNs, those are huge values.
1051                 if (!tcu::Float32(f1).isNaN() && deFloatAbs(f1 - f0) > 0.00001f)
1052                         return false;
1053         }
1054
1055         return true;
1056 }
1057
1058 tcu::TestCaseGroup* createOpNMaxGroup (tcu::TestContext& testCtx)
1059 {
1060         de::MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(testCtx, "opnmax", "Test the OpNMax instruction"));
1061         ComputeShaderSpec                               spec;
1062         de::Random                                              rnd                             (deStringHash(group->getName()));
1063         const int                                               numElements             = 200;
1064         vector<float>                                   inputFloats1    (numElements, 0);
1065         vector<float>                                   inputFloats2    (numElements, 0);
1066         vector<float>                                   outputFloats    (numElements, 0);
1067
1068         fillRandomScalars(rnd, -10000.f, 10000.f, &inputFloats1[0], numElements);
1069         fillRandomScalars(rnd, -10000.f, 10000.f, &inputFloats2[0], numElements);
1070
1071         // Make the first case a full-NAN case.
1072         inputFloats1[0] = TCU_NAN;
1073         inputFloats2[0] = TCU_NAN;
1074
1075         for (size_t ndx = 0; ndx < numElements; ++ndx)
1076         {
1077                 // By default, pick the biggest
1078                 outputFloats[ndx] = std::max(inputFloats1[ndx], inputFloats2[ndx]);
1079
1080                 // Make half of the cases NaN cases
1081                 if ((ndx & 1) == 0)
1082                 {
1083                         // Alternate between the NaN operand
1084                         if ((ndx & 2) == 0)
1085                         {
1086                                 outputFloats[ndx] = inputFloats2[ndx];
1087                                 inputFloats1[ndx] = TCU_NAN;
1088                         }
1089                         else
1090                         {
1091                                 outputFloats[ndx] = inputFloats1[ndx];
1092                                 inputFloats2[ndx] = TCU_NAN;
1093                         }
1094                 }
1095         }
1096
1097         spec.assembly =
1098                 "OpCapability Shader\n"
1099                 "%std450        = OpExtInstImport \"GLSL.std.450\"\n"
1100                 "OpMemoryModel Logical GLSL450\n"
1101                 "OpEntryPoint GLCompute %main \"main\" %id\n"
1102                 "OpExecutionMode %main LocalSize 1 1 1\n"
1103
1104                 "OpName %main           \"main\"\n"
1105                 "OpName %id             \"gl_GlobalInvocationID\"\n"
1106
1107                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
1108
1109                 "OpDecorate %buf BufferBlock\n"
1110                 "OpDecorate %indata1 DescriptorSet 0\n"
1111                 "OpDecorate %indata1 Binding 0\n"
1112                 "OpDecorate %indata2 DescriptorSet 0\n"
1113                 "OpDecorate %indata2 Binding 1\n"
1114                 "OpDecorate %outdata DescriptorSet 0\n"
1115                 "OpDecorate %outdata Binding 2\n"
1116                 "OpDecorate %f32arr ArrayStride 4\n"
1117                 "OpMemberDecorate %buf 0 Offset 0\n"
1118
1119                 + string(getComputeAsmCommonTypes()) +
1120
1121                 "%buf        = OpTypeStruct %f32arr\n"
1122                 "%bufptr     = OpTypePointer Uniform %buf\n"
1123                 "%indata1    = OpVariable %bufptr Uniform\n"
1124                 "%indata2    = OpVariable %bufptr Uniform\n"
1125                 "%outdata    = OpVariable %bufptr Uniform\n"
1126
1127                 "%id        = OpVariable %uvec3ptr Input\n"
1128                 "%zero      = OpConstant %i32 0\n"
1129
1130                 "%main      = OpFunction %void None %voidf\n"
1131                 "%label     = OpLabel\n"
1132                 "%idval     = OpLoad %uvec3 %id\n"
1133                 "%x         = OpCompositeExtract %u32 %idval 0\n"
1134                 "%inloc1    = OpAccessChain %f32ptr %indata1 %zero %x\n"
1135                 "%inval1    = OpLoad %f32 %inloc1\n"
1136                 "%inloc2    = OpAccessChain %f32ptr %indata2 %zero %x\n"
1137                 "%inval2    = OpLoad %f32 %inloc2\n"
1138                 "%rem       = OpExtInst %f32 %std450 NMax %inval1 %inval2\n"
1139                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
1140                 "             OpStore %outloc %rem\n"
1141                 "             OpReturn\n"
1142                 "             OpFunctionEnd\n";
1143
1144         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats1)));
1145         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats2)));
1146         spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
1147         spec.numWorkGroups = IVec3(numElements, 1, 1);
1148         spec.verifyIO = &compareNMax;
1149
1150         group->addChild(new SpvAsmComputeShaderCase(testCtx, "all", "", spec));
1151
1152         return group.release();
1153 }
1154
1155 bool compareNClamp (const std::vector<BufferSp>&, const vector<AllocationSp>& outputAllocs, const std::vector<BufferSp>& expectedOutputs, TestLog&)
1156 {
1157         if (outputAllocs.size() != 1)
1158                 return false;
1159
1160         const BufferSp&                 expectedOutput                  = expectedOutputs[0];
1161         std::vector<deUint8>    data;
1162         expectedOutput->getBytes(data);
1163
1164         const float* const              expectedOutputAsFloat   = reinterpret_cast<const float*>(&data.front());
1165         const float* const              outputAsFloat                   = static_cast<const float*>(outputAllocs[0]->getHostPtr());
1166
1167         for (size_t idx = 0; idx < expectedOutput->getByteSize() / sizeof(float) / 2; ++idx)
1168         {
1169                 const float e0 = expectedOutputAsFloat[idx * 2];
1170                 const float e1 = expectedOutputAsFloat[idx * 2 + 1];
1171                 const float res = outputAsFloat[idx];
1172
1173                 // For NClamp, we have two possible outcomes based on
1174                 // whether NaNs are handled or not.
1175                 // If either min or max value is NaN, the result is undefined,
1176                 // so this test doesn't stress those. If the clamped value is
1177                 // NaN, and NaNs are handled, the result is min; if NaNs are not
1178                 // handled, they are big values that result in max.
1179                 // If all three parameters are NaN, the result should be NaN.
1180                 if (!((tcu::Float32(e0).isNaN() && tcu::Float32(res).isNaN()) ||
1181                          (deFloatAbs(e0 - res) < 0.00001f) ||
1182                          (deFloatAbs(e1 - res) < 0.00001f)))
1183                         return false;
1184         }
1185
1186         return true;
1187 }
1188
1189 tcu::TestCaseGroup* createOpNClampGroup (tcu::TestContext& testCtx)
1190 {
1191         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opnclamp", "Test the OpNClamp instruction"));
1192         ComputeShaderSpec                               spec;
1193         de::Random                                              rnd                             (deStringHash(group->getName()));
1194         const int                                               numElements             = 200;
1195         vector<float>                                   inputFloats1    (numElements, 0);
1196         vector<float>                                   inputFloats2    (numElements, 0);
1197         vector<float>                                   inputFloats3    (numElements, 0);
1198         vector<float>                                   outputFloats    (numElements * 2, 0);
1199
1200         fillRandomScalars(rnd, -10000.f, 10000.f, &inputFloats1[0], numElements);
1201         fillRandomScalars(rnd, -10000.f, 10000.f, &inputFloats2[0], numElements);
1202         fillRandomScalars(rnd, -10000.f, 10000.f, &inputFloats3[0], numElements);
1203
1204         for (size_t ndx = 0; ndx < numElements; ++ndx)
1205         {
1206                 // Results are only defined if max value is bigger than min value.
1207                 if (inputFloats2[ndx] > inputFloats3[ndx])
1208                 {
1209                         float t = inputFloats2[ndx];
1210                         inputFloats2[ndx] = inputFloats3[ndx];
1211                         inputFloats3[ndx] = t;
1212                 }
1213
1214                 // By default, do the clamp, setting both possible answers
1215                 float defaultRes = std::min(std::max(inputFloats1[ndx], inputFloats2[ndx]), inputFloats3[ndx]);
1216
1217                 float maxResA = std::max(inputFloats1[ndx], inputFloats2[ndx]);
1218                 float maxResB = maxResA;
1219
1220                 // Alternate between the NaN cases
1221                 if (ndx & 1)
1222                 {
1223                         inputFloats1[ndx] = TCU_NAN;
1224                         // If NaN is handled, the result should be same as the clamp minimum.
1225                         // If NaN is not handled, the result should clamp to the clamp maximum.
1226                         maxResA = inputFloats2[ndx];
1227                         maxResB = inputFloats3[ndx];
1228                 }
1229                 else
1230                 {
1231                         // Not a NaN case - only one legal result.
1232                         maxResA = defaultRes;
1233                         maxResB = defaultRes;
1234                 }
1235
1236                 outputFloats[ndx * 2] = maxResA;
1237                 outputFloats[ndx * 2 + 1] = maxResB;
1238         }
1239
1240         // Make the first case a full-NAN case.
1241         inputFloats1[0] = TCU_NAN;
1242         inputFloats2[0] = TCU_NAN;
1243         inputFloats3[0] = TCU_NAN;
1244         outputFloats[0] = TCU_NAN;
1245         outputFloats[1] = TCU_NAN;
1246
1247         spec.assembly =
1248                 "OpCapability Shader\n"
1249                 "%std450        = OpExtInstImport \"GLSL.std.450\"\n"
1250                 "OpMemoryModel Logical GLSL450\n"
1251                 "OpEntryPoint GLCompute %main \"main\" %id\n"
1252                 "OpExecutionMode %main LocalSize 1 1 1\n"
1253
1254                 "OpName %main           \"main\"\n"
1255                 "OpName %id             \"gl_GlobalInvocationID\"\n"
1256
1257                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
1258
1259                 "OpDecorate %buf BufferBlock\n"
1260                 "OpDecorate %indata1 DescriptorSet 0\n"
1261                 "OpDecorate %indata1 Binding 0\n"
1262                 "OpDecorate %indata2 DescriptorSet 0\n"
1263                 "OpDecorate %indata2 Binding 1\n"
1264                 "OpDecorate %indata3 DescriptorSet 0\n"
1265                 "OpDecorate %indata3 Binding 2\n"
1266                 "OpDecorate %outdata DescriptorSet 0\n"
1267                 "OpDecorate %outdata Binding 3\n"
1268                 "OpDecorate %f32arr ArrayStride 4\n"
1269                 "OpMemberDecorate %buf 0 Offset 0\n"
1270
1271                 + string(getComputeAsmCommonTypes()) +
1272
1273                 "%buf        = OpTypeStruct %f32arr\n"
1274                 "%bufptr     = OpTypePointer Uniform %buf\n"
1275                 "%indata1    = OpVariable %bufptr Uniform\n"
1276                 "%indata2    = OpVariable %bufptr Uniform\n"
1277                 "%indata3    = OpVariable %bufptr Uniform\n"
1278                 "%outdata    = OpVariable %bufptr Uniform\n"
1279
1280                 "%id        = OpVariable %uvec3ptr Input\n"
1281                 "%zero      = OpConstant %i32 0\n"
1282
1283                 "%main      = OpFunction %void None %voidf\n"
1284                 "%label     = OpLabel\n"
1285                 "%idval     = OpLoad %uvec3 %id\n"
1286                 "%x         = OpCompositeExtract %u32 %idval 0\n"
1287                 "%inloc1    = OpAccessChain %f32ptr %indata1 %zero %x\n"
1288                 "%inval1    = OpLoad %f32 %inloc1\n"
1289                 "%inloc2    = OpAccessChain %f32ptr %indata2 %zero %x\n"
1290                 "%inval2    = OpLoad %f32 %inloc2\n"
1291                 "%inloc3    = OpAccessChain %f32ptr %indata3 %zero %x\n"
1292                 "%inval3    = OpLoad %f32 %inloc3\n"
1293                 "%rem       = OpExtInst %f32 %std450 NClamp %inval1 %inval2 %inval3\n"
1294                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
1295                 "             OpStore %outloc %rem\n"
1296                 "             OpReturn\n"
1297                 "             OpFunctionEnd\n";
1298
1299         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats1)));
1300         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats2)));
1301         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats3)));
1302         spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
1303         spec.numWorkGroups = IVec3(numElements, 1, 1);
1304         spec.verifyIO = &compareNClamp;
1305
1306         group->addChild(new SpvAsmComputeShaderCase(testCtx, "all", "", spec));
1307
1308         return group.release();
1309 }
1310
1311 tcu::TestCaseGroup* createOpSRemComputeGroup (tcu::TestContext& testCtx, qpTestResult negFailResult)
1312 {
1313         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opsrem", "Test the OpSRem instruction"));
1314         de::Random                                              rnd                             (deStringHash(group->getName()));
1315         const int                                               numElements             = 200;
1316
1317         const struct CaseParams
1318         {
1319                 const char*             name;
1320                 const char*             failMessage;            // customized status message
1321                 qpTestResult    failResult;                     // override status on failure
1322                 int                             op1Min, op1Max;         // operand ranges
1323                 int                             op2Min, op2Max;
1324         } cases[] =
1325         {
1326                 { "positive",   "Output doesn't match with expected",                           QP_TEST_RESULT_FAIL,    0,              65536,  0,              100 },
1327                 { "all",                "Inconsistent results, but within specification",       negFailResult,                  -65536, 65536,  -100,   100 },  // see below
1328         };
1329         // If either operand is negative the result is undefined. Some implementations may still return correct values.
1330
1331         for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
1332         {
1333                 const CaseParams&       params          = cases[caseNdx];
1334                 ComputeShaderSpec       spec;
1335                 vector<deInt32>         inputInts1      (numElements, 0);
1336                 vector<deInt32>         inputInts2      (numElements, 0);
1337                 vector<deInt32>         outputInts      (numElements, 0);
1338
1339                 fillRandomScalars(rnd, params.op1Min, params.op1Max, &inputInts1[0], numElements);
1340                 fillRandomScalars(rnd, params.op2Min, params.op2Max, &inputInts2[0], numElements, filterNotZero);
1341
1342                 for (int ndx = 0; ndx < numElements; ++ndx)
1343                 {
1344                         // The return value of std::fmod() has the same sign as its first operand, which is how OpFRem spec'd.
1345                         outputInts[ndx] = inputInts1[ndx] % inputInts2[ndx];
1346                 }
1347
1348                 spec.assembly =
1349                         string(getComputeAsmShaderPreamble()) +
1350
1351                         "OpName %main           \"main\"\n"
1352                         "OpName %id             \"gl_GlobalInvocationID\"\n"
1353
1354                         "OpDecorate %id BuiltIn GlobalInvocationId\n"
1355
1356                         "OpDecorate %buf BufferBlock\n"
1357                         "OpDecorate %indata1 DescriptorSet 0\n"
1358                         "OpDecorate %indata1 Binding 0\n"
1359                         "OpDecorate %indata2 DescriptorSet 0\n"
1360                         "OpDecorate %indata2 Binding 1\n"
1361                         "OpDecorate %outdata DescriptorSet 0\n"
1362                         "OpDecorate %outdata Binding 2\n"
1363                         "OpDecorate %i32arr ArrayStride 4\n"
1364                         "OpMemberDecorate %buf 0 Offset 0\n"
1365
1366                         + string(getComputeAsmCommonTypes()) +
1367
1368                         "%buf        = OpTypeStruct %i32arr\n"
1369                         "%bufptr     = OpTypePointer Uniform %buf\n"
1370                         "%indata1    = OpVariable %bufptr Uniform\n"
1371                         "%indata2    = OpVariable %bufptr Uniform\n"
1372                         "%outdata    = OpVariable %bufptr Uniform\n"
1373
1374                         "%id        = OpVariable %uvec3ptr Input\n"
1375                         "%zero      = OpConstant %i32 0\n"
1376
1377                         "%main      = OpFunction %void None %voidf\n"
1378                         "%label     = OpLabel\n"
1379                         "%idval     = OpLoad %uvec3 %id\n"
1380                         "%x         = OpCompositeExtract %u32 %idval 0\n"
1381                         "%inloc1    = OpAccessChain %i32ptr %indata1 %zero %x\n"
1382                         "%inval1    = OpLoad %i32 %inloc1\n"
1383                         "%inloc2    = OpAccessChain %i32ptr %indata2 %zero %x\n"
1384                         "%inval2    = OpLoad %i32 %inloc2\n"
1385                         "%rem       = OpSRem %i32 %inval1 %inval2\n"
1386                         "%outloc    = OpAccessChain %i32ptr %outdata %zero %x\n"
1387                         "             OpStore %outloc %rem\n"
1388                         "             OpReturn\n"
1389                         "             OpFunctionEnd\n";
1390
1391                 spec.inputs.push_back   (BufferSp(new Int32Buffer(inputInts1)));
1392                 spec.inputs.push_back   (BufferSp(new Int32Buffer(inputInts2)));
1393                 spec.outputs.push_back  (BufferSp(new Int32Buffer(outputInts)));
1394                 spec.numWorkGroups              = IVec3(numElements, 1, 1);
1395                 spec.failResult                 = params.failResult;
1396                 spec.failMessage                = params.failMessage;
1397
1398                 group->addChild(new SpvAsmComputeShaderCase(testCtx, params.name, "", spec));
1399         }
1400
1401         return group.release();
1402 }
1403
1404 tcu::TestCaseGroup* createOpSRemComputeGroup64 (tcu::TestContext& testCtx, qpTestResult negFailResult)
1405 {
1406         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opsrem64", "Test the 64-bit OpSRem instruction"));
1407         de::Random                                              rnd                             (deStringHash(group->getName()));
1408         const int                                               numElements             = 200;
1409
1410         const struct CaseParams
1411         {
1412                 const char*             name;
1413                 const char*             failMessage;            // customized status message
1414                 qpTestResult    failResult;                     // override status on failure
1415                 bool                    positive;
1416         } cases[] =
1417         {
1418                 { "positive",   "Output doesn't match with expected",                           QP_TEST_RESULT_FAIL,    true },
1419                 { "all",                "Inconsistent results, but within specification",       negFailResult,                  false },        // see below
1420         };
1421         // If either operand is negative the result is undefined. Some implementations may still return correct values.
1422
1423         for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
1424         {
1425                 const CaseParams&       params          = cases[caseNdx];
1426                 ComputeShaderSpec       spec;
1427                 vector<deInt64>         inputInts1      (numElements, 0);
1428                 vector<deInt64>         inputInts2      (numElements, 0);
1429                 vector<deInt64>         outputInts      (numElements, 0);
1430
1431                 if (params.positive)
1432                 {
1433                         fillRandomInt64sLogDistributed(rnd, inputInts1, numElements, filterNonNegative);
1434                         fillRandomInt64sLogDistributed(rnd, inputInts2, numElements, filterPositive);
1435                 }
1436                 else
1437                 {
1438                         fillRandomInt64sLogDistributed(rnd, inputInts1, numElements);
1439                         fillRandomInt64sLogDistributed(rnd, inputInts2, numElements, filterNotZero);
1440                 }
1441
1442                 for (int ndx = 0; ndx < numElements; ++ndx)
1443                 {
1444                         // The return value of std::fmod() has the same sign as its first operand, which is how OpFRem spec'd.
1445                         outputInts[ndx] = inputInts1[ndx] % inputInts2[ndx];
1446                 }
1447
1448                 spec.assembly =
1449                         "OpCapability Int64\n"
1450
1451                         + string(getComputeAsmShaderPreamble()) +
1452
1453                         "OpName %main           \"main\"\n"
1454                         "OpName %id             \"gl_GlobalInvocationID\"\n"
1455
1456                         "OpDecorate %id BuiltIn GlobalInvocationId\n"
1457
1458                         "OpDecorate %buf BufferBlock\n"
1459                         "OpDecorate %indata1 DescriptorSet 0\n"
1460                         "OpDecorate %indata1 Binding 0\n"
1461                         "OpDecorate %indata2 DescriptorSet 0\n"
1462                         "OpDecorate %indata2 Binding 1\n"
1463                         "OpDecorate %outdata DescriptorSet 0\n"
1464                         "OpDecorate %outdata Binding 2\n"
1465                         "OpDecorate %i64arr ArrayStride 8\n"
1466                         "OpMemberDecorate %buf 0 Offset 0\n"
1467
1468                         + string(getComputeAsmCommonTypes())
1469                         + string(getComputeAsmCommonInt64Types()) +
1470
1471                         "%buf        = OpTypeStruct %i64arr\n"
1472                         "%bufptr     = OpTypePointer Uniform %buf\n"
1473                         "%indata1    = OpVariable %bufptr Uniform\n"
1474                         "%indata2    = OpVariable %bufptr Uniform\n"
1475                         "%outdata    = OpVariable %bufptr Uniform\n"
1476
1477                         "%id        = OpVariable %uvec3ptr Input\n"
1478                         "%zero      = OpConstant %i64 0\n"
1479
1480                         "%main      = OpFunction %void None %voidf\n"
1481                         "%label     = OpLabel\n"
1482                         "%idval     = OpLoad %uvec3 %id\n"
1483                         "%x         = OpCompositeExtract %u32 %idval 0\n"
1484                         "%inloc1    = OpAccessChain %i64ptr %indata1 %zero %x\n"
1485                         "%inval1    = OpLoad %i64 %inloc1\n"
1486                         "%inloc2    = OpAccessChain %i64ptr %indata2 %zero %x\n"
1487                         "%inval2    = OpLoad %i64 %inloc2\n"
1488                         "%rem       = OpSRem %i64 %inval1 %inval2\n"
1489                         "%outloc    = OpAccessChain %i64ptr %outdata %zero %x\n"
1490                         "             OpStore %outloc %rem\n"
1491                         "             OpReturn\n"
1492                         "             OpFunctionEnd\n";
1493
1494                 spec.inputs.push_back   (BufferSp(new Int64Buffer(inputInts1)));
1495                 spec.inputs.push_back   (BufferSp(new Int64Buffer(inputInts2)));
1496                 spec.outputs.push_back  (BufferSp(new Int64Buffer(outputInts)));
1497                 spec.numWorkGroups              = IVec3(numElements, 1, 1);
1498                 spec.failResult                 = params.failResult;
1499                 spec.failMessage                = params.failMessage;
1500
1501                 group->addChild(new SpvAsmComputeShaderCase(testCtx, params.name, "", spec, COMPUTE_TEST_USES_INT64));
1502         }
1503
1504         return group.release();
1505 }
1506
1507 tcu::TestCaseGroup* createOpSModComputeGroup (tcu::TestContext& testCtx, qpTestResult negFailResult)
1508 {
1509         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opsmod", "Test the OpSMod instruction"));
1510         de::Random                                              rnd                             (deStringHash(group->getName()));
1511         const int                                               numElements             = 200;
1512
1513         const struct CaseParams
1514         {
1515                 const char*             name;
1516                 const char*             failMessage;            // customized status message
1517                 qpTestResult    failResult;                     // override status on failure
1518                 int                             op1Min, op1Max;         // operand ranges
1519                 int                             op2Min, op2Max;
1520         } cases[] =
1521         {
1522                 { "positive",   "Output doesn't match with expected",                           QP_TEST_RESULT_FAIL,    0,              65536,  0,              100 },
1523                 { "all",                "Inconsistent results, but within specification",       negFailResult,                  -65536, 65536,  -100,   100 },  // see below
1524         };
1525         // If either operand is negative the result is undefined. Some implementations may still return correct values.
1526
1527         for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
1528         {
1529                 const CaseParams&       params          = cases[caseNdx];
1530
1531                 ComputeShaderSpec       spec;
1532                 vector<deInt32>         inputInts1      (numElements, 0);
1533                 vector<deInt32>         inputInts2      (numElements, 0);
1534                 vector<deInt32>         outputInts      (numElements, 0);
1535
1536                 fillRandomScalars(rnd, params.op1Min, params.op1Max, &inputInts1[0], numElements);
1537                 fillRandomScalars(rnd, params.op2Min, params.op2Max, &inputInts2[0], numElements, filterNotZero);
1538
1539                 for (int ndx = 0; ndx < numElements; ++ndx)
1540                 {
1541                         deInt32 rem = inputInts1[ndx] % inputInts2[ndx];
1542                         if (rem == 0)
1543                         {
1544                                 outputInts[ndx] = 0;
1545                         }
1546                         else if ((inputInts1[ndx] >= 0) == (inputInts2[ndx] >= 0))
1547                         {
1548                                 // They have the same sign
1549                                 outputInts[ndx] = rem;
1550                         }
1551                         else
1552                         {
1553                                 // They have opposite sign.  The remainder operation takes the
1554                                 // sign inputInts1[ndx] but OpSMod is supposed to take ths sign
1555                                 // of inputInts2[ndx].  Adding inputInts2[ndx] will ensure that
1556                                 // the result has the correct sign and that it is still
1557                                 // congruent to inputInts1[ndx] modulo inputInts2[ndx]
1558                                 //
1559                                 // See also http://mathforum.org/library/drmath/view/52343.html
1560                                 outputInts[ndx] = rem + inputInts2[ndx];
1561                         }
1562                 }
1563
1564                 spec.assembly =
1565                         string(getComputeAsmShaderPreamble()) +
1566
1567                         "OpName %main           \"main\"\n"
1568                         "OpName %id             \"gl_GlobalInvocationID\"\n"
1569
1570                         "OpDecorate %id BuiltIn GlobalInvocationId\n"
1571
1572                         "OpDecorate %buf BufferBlock\n"
1573                         "OpDecorate %indata1 DescriptorSet 0\n"
1574                         "OpDecorate %indata1 Binding 0\n"
1575                         "OpDecorate %indata2 DescriptorSet 0\n"
1576                         "OpDecorate %indata2 Binding 1\n"
1577                         "OpDecorate %outdata DescriptorSet 0\n"
1578                         "OpDecorate %outdata Binding 2\n"
1579                         "OpDecorate %i32arr ArrayStride 4\n"
1580                         "OpMemberDecorate %buf 0 Offset 0\n"
1581
1582                         + string(getComputeAsmCommonTypes()) +
1583
1584                         "%buf        = OpTypeStruct %i32arr\n"
1585                         "%bufptr     = OpTypePointer Uniform %buf\n"
1586                         "%indata1    = OpVariable %bufptr Uniform\n"
1587                         "%indata2    = OpVariable %bufptr Uniform\n"
1588                         "%outdata    = OpVariable %bufptr Uniform\n"
1589
1590                         "%id        = OpVariable %uvec3ptr Input\n"
1591                         "%zero      = OpConstant %i32 0\n"
1592
1593                         "%main      = OpFunction %void None %voidf\n"
1594                         "%label     = OpLabel\n"
1595                         "%idval     = OpLoad %uvec3 %id\n"
1596                         "%x         = OpCompositeExtract %u32 %idval 0\n"
1597                         "%inloc1    = OpAccessChain %i32ptr %indata1 %zero %x\n"
1598                         "%inval1    = OpLoad %i32 %inloc1\n"
1599                         "%inloc2    = OpAccessChain %i32ptr %indata2 %zero %x\n"
1600                         "%inval2    = OpLoad %i32 %inloc2\n"
1601                         "%rem       = OpSMod %i32 %inval1 %inval2\n"
1602                         "%outloc    = OpAccessChain %i32ptr %outdata %zero %x\n"
1603                         "             OpStore %outloc %rem\n"
1604                         "             OpReturn\n"
1605                         "             OpFunctionEnd\n";
1606
1607                 spec.inputs.push_back   (BufferSp(new Int32Buffer(inputInts1)));
1608                 spec.inputs.push_back   (BufferSp(new Int32Buffer(inputInts2)));
1609                 spec.outputs.push_back  (BufferSp(new Int32Buffer(outputInts)));
1610                 spec.numWorkGroups              = IVec3(numElements, 1, 1);
1611                 spec.failResult                 = params.failResult;
1612                 spec.failMessage                = params.failMessage;
1613
1614                 group->addChild(new SpvAsmComputeShaderCase(testCtx, params.name, "", spec));
1615         }
1616
1617         return group.release();
1618 }
1619
1620 tcu::TestCaseGroup* createOpSModComputeGroup64 (tcu::TestContext& testCtx, qpTestResult negFailResult)
1621 {
1622         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opsmod64", "Test the OpSMod instruction"));
1623         de::Random                                              rnd                             (deStringHash(group->getName()));
1624         const int                                               numElements             = 200;
1625
1626         const struct CaseParams
1627         {
1628                 const char*             name;
1629                 const char*             failMessage;            // customized status message
1630                 qpTestResult    failResult;                     // override status on failure
1631                 bool                    positive;
1632         } cases[] =
1633         {
1634                 { "positive",   "Output doesn't match with expected",                           QP_TEST_RESULT_FAIL,    true },
1635                 { "all",                "Inconsistent results, but within specification",       negFailResult,                  false },        // see below
1636         };
1637         // If either operand is negative the result is undefined. Some implementations may still return correct values.
1638
1639         for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
1640         {
1641                 const CaseParams&       params          = cases[caseNdx];
1642
1643                 ComputeShaderSpec       spec;
1644                 vector<deInt64>         inputInts1      (numElements, 0);
1645                 vector<deInt64>         inputInts2      (numElements, 0);
1646                 vector<deInt64>         outputInts      (numElements, 0);
1647
1648
1649                 if (params.positive)
1650                 {
1651                         fillRandomInt64sLogDistributed(rnd, inputInts1, numElements, filterNonNegative);
1652                         fillRandomInt64sLogDistributed(rnd, inputInts2, numElements, filterPositive);
1653                 }
1654                 else
1655                 {
1656                         fillRandomInt64sLogDistributed(rnd, inputInts1, numElements);
1657                         fillRandomInt64sLogDistributed(rnd, inputInts2, numElements, filterNotZero);
1658                 }
1659
1660                 for (int ndx = 0; ndx < numElements; ++ndx)
1661                 {
1662                         deInt64 rem = inputInts1[ndx] % inputInts2[ndx];
1663                         if (rem == 0)
1664                         {
1665                                 outputInts[ndx] = 0;
1666                         }
1667                         else if ((inputInts1[ndx] >= 0) == (inputInts2[ndx] >= 0))
1668                         {
1669                                 // They have the same sign
1670                                 outputInts[ndx] = rem;
1671                         }
1672                         else
1673                         {
1674                                 // They have opposite sign.  The remainder operation takes the
1675                                 // sign inputInts1[ndx] but OpSMod is supposed to take ths sign
1676                                 // of inputInts2[ndx].  Adding inputInts2[ndx] will ensure that
1677                                 // the result has the correct sign and that it is still
1678                                 // congruent to inputInts1[ndx] modulo inputInts2[ndx]
1679                                 //
1680                                 // See also http://mathforum.org/library/drmath/view/52343.html
1681                                 outputInts[ndx] = rem + inputInts2[ndx];
1682                         }
1683                 }
1684
1685                 spec.assembly =
1686                         "OpCapability Int64\n"
1687
1688                         + string(getComputeAsmShaderPreamble()) +
1689
1690                         "OpName %main           \"main\"\n"
1691                         "OpName %id             \"gl_GlobalInvocationID\"\n"
1692
1693                         "OpDecorate %id BuiltIn GlobalInvocationId\n"
1694
1695                         "OpDecorate %buf BufferBlock\n"
1696                         "OpDecorate %indata1 DescriptorSet 0\n"
1697                         "OpDecorate %indata1 Binding 0\n"
1698                         "OpDecorate %indata2 DescriptorSet 0\n"
1699                         "OpDecorate %indata2 Binding 1\n"
1700                         "OpDecorate %outdata DescriptorSet 0\n"
1701                         "OpDecorate %outdata Binding 2\n"
1702                         "OpDecorate %i64arr ArrayStride 8\n"
1703                         "OpMemberDecorate %buf 0 Offset 0\n"
1704
1705                         + string(getComputeAsmCommonTypes())
1706                         + string(getComputeAsmCommonInt64Types()) +
1707
1708                         "%buf        = OpTypeStruct %i64arr\n"
1709                         "%bufptr     = OpTypePointer Uniform %buf\n"
1710                         "%indata1    = OpVariable %bufptr Uniform\n"
1711                         "%indata2    = OpVariable %bufptr Uniform\n"
1712                         "%outdata    = OpVariable %bufptr Uniform\n"
1713
1714                         "%id        = OpVariable %uvec3ptr Input\n"
1715                         "%zero      = OpConstant %i64 0\n"
1716
1717                         "%main      = OpFunction %void None %voidf\n"
1718                         "%label     = OpLabel\n"
1719                         "%idval     = OpLoad %uvec3 %id\n"
1720                         "%x         = OpCompositeExtract %u32 %idval 0\n"
1721                         "%inloc1    = OpAccessChain %i64ptr %indata1 %zero %x\n"
1722                         "%inval1    = OpLoad %i64 %inloc1\n"
1723                         "%inloc2    = OpAccessChain %i64ptr %indata2 %zero %x\n"
1724                         "%inval2    = OpLoad %i64 %inloc2\n"
1725                         "%rem       = OpSMod %i64 %inval1 %inval2\n"
1726                         "%outloc    = OpAccessChain %i64ptr %outdata %zero %x\n"
1727                         "             OpStore %outloc %rem\n"
1728                         "             OpReturn\n"
1729                         "             OpFunctionEnd\n";
1730
1731                 spec.inputs.push_back   (BufferSp(new Int64Buffer(inputInts1)));
1732                 spec.inputs.push_back   (BufferSp(new Int64Buffer(inputInts2)));
1733                 spec.outputs.push_back  (BufferSp(new Int64Buffer(outputInts)));
1734                 spec.numWorkGroups              = IVec3(numElements, 1, 1);
1735                 spec.failResult                 = params.failResult;
1736                 spec.failMessage                = params.failMessage;
1737
1738                 group->addChild(new SpvAsmComputeShaderCase(testCtx, params.name, "", spec, COMPUTE_TEST_USES_INT64));
1739         }
1740
1741         return group.release();
1742 }
1743
1744 // Copy contents in the input buffer to the output buffer.
1745 tcu::TestCaseGroup* createOpCopyMemoryGroup (tcu::TestContext& testCtx)
1746 {
1747         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opcopymemory", "Test the OpCopyMemory instruction"));
1748         de::Random                                              rnd                             (deStringHash(group->getName()));
1749         const int                                               numElements             = 100;
1750
1751         // The following case adds vec4(0., 0.5, 1.5, 2.5) to each of the elements in the input buffer and writes output to the output buffer.
1752         ComputeShaderSpec                               spec1;
1753         vector<Vec4>                                    inputFloats1    (numElements);
1754         vector<Vec4>                                    outputFloats1   (numElements);
1755
1756         fillRandomScalars(rnd, -200.f, 200.f, &inputFloats1[0], numElements * 4);
1757
1758         // CPU might not use the same rounding mode as the GPU. Use whole numbers to avoid rounding differences.
1759         floorAll(inputFloats1);
1760
1761         for (size_t ndx = 0; ndx < numElements; ++ndx)
1762                 outputFloats1[ndx] = inputFloats1[ndx] + Vec4(0.f, 0.5f, 1.5f, 2.5f);
1763
1764         spec1.assembly =
1765                 string(getComputeAsmShaderPreamble()) +
1766
1767                 "OpName %main           \"main\"\n"
1768                 "OpName %id             \"gl_GlobalInvocationID\"\n"
1769
1770                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
1771                 "OpDecorate %vec4arr ArrayStride 16\n"
1772
1773                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
1774
1775                 "%vec4       = OpTypeVector %f32 4\n"
1776                 "%vec4ptr_u  = OpTypePointer Uniform %vec4\n"
1777                 "%vec4ptr_f  = OpTypePointer Function %vec4\n"
1778                 "%vec4arr    = OpTypeRuntimeArray %vec4\n"
1779                 "%buf        = OpTypeStruct %vec4arr\n"
1780                 "%bufptr     = OpTypePointer Uniform %buf\n"
1781                 "%indata     = OpVariable %bufptr Uniform\n"
1782                 "%outdata    = OpVariable %bufptr Uniform\n"
1783
1784                 "%id         = OpVariable %uvec3ptr Input\n"
1785                 "%zero       = OpConstant %i32 0\n"
1786                 "%c_f_0      = OpConstant %f32 0.\n"
1787                 "%c_f_0_5    = OpConstant %f32 0.5\n"
1788                 "%c_f_1_5    = OpConstant %f32 1.5\n"
1789                 "%c_f_2_5    = OpConstant %f32 2.5\n"
1790                 "%c_vec4     = OpConstantComposite %vec4 %c_f_0 %c_f_0_5 %c_f_1_5 %c_f_2_5\n"
1791
1792                 "%main       = OpFunction %void None %voidf\n"
1793                 "%label      = OpLabel\n"
1794                 "%v_vec4     = OpVariable %vec4ptr_f Function\n"
1795                 "%idval      = OpLoad %uvec3 %id\n"
1796                 "%x          = OpCompositeExtract %u32 %idval 0\n"
1797                 "%inloc      = OpAccessChain %vec4ptr_u %indata %zero %x\n"
1798                 "%outloc     = OpAccessChain %vec4ptr_u %outdata %zero %x\n"
1799                 "              OpCopyMemory %v_vec4 %inloc\n"
1800                 "%v_vec4_val = OpLoad %vec4 %v_vec4\n"
1801                 "%add        = OpFAdd %vec4 %v_vec4_val %c_vec4\n"
1802                 "              OpStore %outloc %add\n"
1803                 "              OpReturn\n"
1804                 "              OpFunctionEnd\n";
1805
1806         spec1.inputs.push_back(BufferSp(new Vec4Buffer(inputFloats1)));
1807         spec1.outputs.push_back(BufferSp(new Vec4Buffer(outputFloats1)));
1808         spec1.numWorkGroups = IVec3(numElements, 1, 1);
1809
1810         group->addChild(new SpvAsmComputeShaderCase(testCtx, "vector", "OpCopyMemory elements of vector type", spec1));
1811
1812         // The following case copies a float[100] variable from the input buffer to the output buffer.
1813         ComputeShaderSpec                               spec2;
1814         vector<float>                                   inputFloats2    (numElements);
1815         vector<float>                                   outputFloats2   (numElements);
1816
1817         fillRandomScalars(rnd, -200.f, 200.f, &inputFloats2[0], numElements);
1818
1819         for (size_t ndx = 0; ndx < numElements; ++ndx)
1820                 outputFloats2[ndx] = inputFloats2[ndx];
1821
1822         spec2.assembly =
1823                 string(getComputeAsmShaderPreamble()) +
1824
1825                 "OpName %main           \"main\"\n"
1826                 "OpName %id             \"gl_GlobalInvocationID\"\n"
1827
1828                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
1829                 "OpDecorate %f32arr100 ArrayStride 4\n"
1830
1831                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
1832
1833                 "%hundred        = OpConstant %u32 100\n"
1834                 "%f32arr100      = OpTypeArray %f32 %hundred\n"
1835                 "%f32arr100ptr_f = OpTypePointer Function %f32arr100\n"
1836                 "%f32arr100ptr_u = OpTypePointer Uniform %f32arr100\n"
1837                 "%buf            = OpTypeStruct %f32arr100\n"
1838                 "%bufptr         = OpTypePointer Uniform %buf\n"
1839                 "%indata         = OpVariable %bufptr Uniform\n"
1840                 "%outdata        = OpVariable %bufptr Uniform\n"
1841
1842                 "%id             = OpVariable %uvec3ptr Input\n"
1843                 "%zero           = OpConstant %i32 0\n"
1844
1845                 "%main           = OpFunction %void None %voidf\n"
1846                 "%label          = OpLabel\n"
1847                 "%var            = OpVariable %f32arr100ptr_f Function\n"
1848                 "%inarr          = OpAccessChain %f32arr100ptr_u %indata %zero\n"
1849                 "%outarr         = OpAccessChain %f32arr100ptr_u %outdata %zero\n"
1850                 "                  OpCopyMemory %var %inarr\n"
1851                 "                  OpCopyMemory %outarr %var\n"
1852                 "                  OpReturn\n"
1853                 "                  OpFunctionEnd\n";
1854
1855         spec2.inputs.push_back(BufferSp(new Float32Buffer(inputFloats2)));
1856         spec2.outputs.push_back(BufferSp(new Float32Buffer(outputFloats2)));
1857         spec2.numWorkGroups = IVec3(1, 1, 1);
1858
1859         group->addChild(new SpvAsmComputeShaderCase(testCtx, "array", "OpCopyMemory elements of array type", spec2));
1860
1861         // The following case copies a struct{vec4, vec4, vec4, vec4} variable from the input buffer to the output buffer.
1862         ComputeShaderSpec                               spec3;
1863         vector<float>                                   inputFloats3    (16);
1864         vector<float>                                   outputFloats3   (16);
1865
1866         fillRandomScalars(rnd, -200.f, 200.f, &inputFloats3[0], 16);
1867
1868         for (size_t ndx = 0; ndx < 16; ++ndx)
1869                 outputFloats3[ndx] = inputFloats3[ndx];
1870
1871         spec3.assembly =
1872                 string(getComputeAsmShaderPreamble()) +
1873
1874                 "OpName %main           \"main\"\n"
1875                 "OpName %id             \"gl_GlobalInvocationID\"\n"
1876
1877                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
1878                 "OpMemberDecorate %buf 0 Offset 0\n"
1879                 "OpMemberDecorate %buf 1 Offset 16\n"
1880                 "OpMemberDecorate %buf 2 Offset 32\n"
1881                 "OpMemberDecorate %buf 3 Offset 48\n"
1882
1883                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
1884
1885                 "%vec4      = OpTypeVector %f32 4\n"
1886                 "%buf       = OpTypeStruct %vec4 %vec4 %vec4 %vec4\n"
1887                 "%bufptr    = OpTypePointer Uniform %buf\n"
1888                 "%indata    = OpVariable %bufptr Uniform\n"
1889                 "%outdata   = OpVariable %bufptr Uniform\n"
1890                 "%vec4stptr = OpTypePointer Function %buf\n"
1891
1892                 "%id        = OpVariable %uvec3ptr Input\n"
1893                 "%zero      = OpConstant %i32 0\n"
1894
1895                 "%main      = OpFunction %void None %voidf\n"
1896                 "%label     = OpLabel\n"
1897                 "%var       = OpVariable %vec4stptr Function\n"
1898                 "             OpCopyMemory %var %indata\n"
1899                 "             OpCopyMemory %outdata %var\n"
1900                 "             OpReturn\n"
1901                 "             OpFunctionEnd\n";
1902
1903         spec3.inputs.push_back(BufferSp(new Float32Buffer(inputFloats3)));
1904         spec3.outputs.push_back(BufferSp(new Float32Buffer(outputFloats3)));
1905         spec3.numWorkGroups = IVec3(1, 1, 1);
1906
1907         group->addChild(new SpvAsmComputeShaderCase(testCtx, "struct", "OpCopyMemory elements of struct type", spec3));
1908
1909         // The following case negates multiple float variables from the input buffer and stores the results to the output buffer.
1910         ComputeShaderSpec                               spec4;
1911         vector<float>                                   inputFloats4    (numElements);
1912         vector<float>                                   outputFloats4   (numElements);
1913
1914         fillRandomScalars(rnd, -200.f, 200.f, &inputFloats4[0], numElements);
1915
1916         for (size_t ndx = 0; ndx < numElements; ++ndx)
1917                 outputFloats4[ndx] = -inputFloats4[ndx];
1918
1919         spec4.assembly =
1920                 string(getComputeAsmShaderPreamble()) +
1921
1922                 "OpName %main           \"main\"\n"
1923                 "OpName %id             \"gl_GlobalInvocationID\"\n"
1924
1925                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
1926
1927                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
1928
1929                 "%f32ptr_f  = OpTypePointer Function %f32\n"
1930                 "%id        = OpVariable %uvec3ptr Input\n"
1931                 "%zero      = OpConstant %i32 0\n"
1932
1933                 "%main      = OpFunction %void None %voidf\n"
1934                 "%label     = OpLabel\n"
1935                 "%var       = OpVariable %f32ptr_f Function\n"
1936                 "%idval     = OpLoad %uvec3 %id\n"
1937                 "%x         = OpCompositeExtract %u32 %idval 0\n"
1938                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
1939                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
1940                 "             OpCopyMemory %var %inloc\n"
1941                 "%val       = OpLoad %f32 %var\n"
1942                 "%neg       = OpFNegate %f32 %val\n"
1943                 "             OpStore %outloc %neg\n"
1944                 "             OpReturn\n"
1945                 "             OpFunctionEnd\n";
1946
1947         spec4.inputs.push_back(BufferSp(new Float32Buffer(inputFloats4)));
1948         spec4.outputs.push_back(BufferSp(new Float32Buffer(outputFloats4)));
1949         spec4.numWorkGroups = IVec3(numElements, 1, 1);
1950
1951         group->addChild(new SpvAsmComputeShaderCase(testCtx, "float", "OpCopyMemory elements of float type", spec4));
1952
1953         return group.release();
1954 }
1955
1956 tcu::TestCaseGroup* createOpCopyObjectGroup (tcu::TestContext& testCtx)
1957 {
1958         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opcopyobject", "Test the OpCopyObject instruction"));
1959         ComputeShaderSpec                               spec;
1960         de::Random                                              rnd                             (deStringHash(group->getName()));
1961         const int                                               numElements             = 100;
1962         vector<float>                                   inputFloats             (numElements, 0);
1963         vector<float>                                   outputFloats    (numElements, 0);
1964
1965         fillRandomScalars(rnd, -200.f, 200.f, &inputFloats[0], numElements);
1966
1967         // CPU might not use the same rounding mode as the GPU. Use whole numbers to avoid rounding differences.
1968         floorAll(inputFloats);
1969
1970         for (size_t ndx = 0; ndx < numElements; ++ndx)
1971                 outputFloats[ndx] = inputFloats[ndx] + 7.5f;
1972
1973         spec.assembly =
1974                 string(getComputeAsmShaderPreamble()) +
1975
1976                 "OpName %main           \"main\"\n"
1977                 "OpName %id             \"gl_GlobalInvocationID\"\n"
1978
1979                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
1980
1981                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
1982
1983                 "%fmat     = OpTypeMatrix %fvec3 3\n"
1984                 "%three    = OpConstant %u32 3\n"
1985                 "%farr     = OpTypeArray %f32 %three\n"
1986                 "%fst      = OpTypeStruct %f32 %f32\n"
1987
1988                 + string(getComputeAsmInputOutputBuffer()) +
1989
1990                 "%id            = OpVariable %uvec3ptr Input\n"
1991                 "%zero          = OpConstant %i32 0\n"
1992                 "%c_f           = OpConstant %f32 1.5\n"
1993                 "%c_fvec3       = OpConstantComposite %fvec3 %c_f %c_f %c_f\n"
1994                 "%c_fmat        = OpConstantComposite %fmat %c_fvec3 %c_fvec3 %c_fvec3\n"
1995                 "%c_farr        = OpConstantComposite %farr %c_f %c_f %c_f\n"
1996                 "%c_fst         = OpConstantComposite %fst %c_f %c_f\n"
1997
1998                 "%main          = OpFunction %void None %voidf\n"
1999                 "%label         = OpLabel\n"
2000                 "%c_f_copy      = OpCopyObject %f32   %c_f\n"
2001                 "%c_fvec3_copy  = OpCopyObject %fvec3 %c_fvec3\n"
2002                 "%c_fmat_copy   = OpCopyObject %fmat  %c_fmat\n"
2003                 "%c_farr_copy   = OpCopyObject %farr  %c_farr\n"
2004                 "%c_fst_copy    = OpCopyObject %fst   %c_fst\n"
2005                 "%fvec3_elem    = OpCompositeExtract %f32 %c_fvec3_copy 0\n"
2006                 "%fmat_elem     = OpCompositeExtract %f32 %c_fmat_copy 1 2\n"
2007                 "%farr_elem     = OpCompositeExtract %f32 %c_farr_copy 2\n"
2008                 "%fst_elem      = OpCompositeExtract %f32 %c_fst_copy 1\n"
2009                 // Add up. 1.5 * 5 = 7.5.
2010                 "%add1          = OpFAdd %f32 %c_f_copy %fvec3_elem\n"
2011                 "%add2          = OpFAdd %f32 %add1     %fmat_elem\n"
2012                 "%add3          = OpFAdd %f32 %add2     %farr_elem\n"
2013                 "%add4          = OpFAdd %f32 %add3     %fst_elem\n"
2014
2015                 "%idval         = OpLoad %uvec3 %id\n"
2016                 "%x             = OpCompositeExtract %u32 %idval 0\n"
2017                 "%inloc         = OpAccessChain %f32ptr %indata %zero %x\n"
2018                 "%outloc        = OpAccessChain %f32ptr %outdata %zero %x\n"
2019                 "%inval         = OpLoad %f32 %inloc\n"
2020                 "%add           = OpFAdd %f32 %add4 %inval\n"
2021                 "                 OpStore %outloc %add\n"
2022                 "                 OpReturn\n"
2023                 "                 OpFunctionEnd\n";
2024         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
2025         spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
2026         spec.numWorkGroups = IVec3(numElements, 1, 1);
2027
2028         group->addChild(new SpvAsmComputeShaderCase(testCtx, "spotcheck", "OpCopyObject on different types", spec));
2029
2030         return group.release();
2031 }
2032 // Assembly code used for testing OpUnreachable is based on GLSL source code:
2033 //
2034 // #version 430
2035 //
2036 // layout(std140, set = 0, binding = 0) readonly buffer Input {
2037 //   float elements[];
2038 // } input_data;
2039 // layout(std140, set = 0, binding = 1) writeonly buffer Output {
2040 //   float elements[];
2041 // } output_data;
2042 //
2043 // void not_called_func() {
2044 //   // place OpUnreachable here
2045 // }
2046 //
2047 // uint modulo4(uint val) {
2048 //   switch (val % uint(4)) {
2049 //     case 0:  return 3;
2050 //     case 1:  return 2;
2051 //     case 2:  return 1;
2052 //     case 3:  return 0;
2053 //     default: return 100; // place OpUnreachable here
2054 //   }
2055 // }
2056 //
2057 // uint const5() {
2058 //   return 5;
2059 //   // place OpUnreachable here
2060 // }
2061 //
2062 // void main() {
2063 //   uint x = gl_GlobalInvocationID.x;
2064 //   if (const5() > modulo4(1000)) {
2065 //     output_data.elements[x] = -input_data.elements[x];
2066 //   } else {
2067 //     // place OpUnreachable here
2068 //     output_data.elements[x] = input_data.elements[x];
2069 //   }
2070 // }
2071
2072 tcu::TestCaseGroup* createOpUnreachableGroup (tcu::TestContext& testCtx)
2073 {
2074         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opunreachable", "Test the OpUnreachable instruction"));
2075         ComputeShaderSpec                               spec;
2076         de::Random                                              rnd                             (deStringHash(group->getName()));
2077         const int                                               numElements             = 100;
2078         vector<float>                                   positiveFloats  (numElements, 0);
2079         vector<float>                                   negativeFloats  (numElements, 0);
2080
2081         fillRandomScalars(rnd, 1.f, 100.f, &positiveFloats[0], numElements);
2082
2083         for (size_t ndx = 0; ndx < numElements; ++ndx)
2084                 negativeFloats[ndx] = -positiveFloats[ndx];
2085
2086         spec.assembly =
2087                 string(getComputeAsmShaderPreamble()) +
2088
2089                 "OpSource GLSL 430\n"
2090                 "OpName %main            \"main\"\n"
2091                 "OpName %func_not_called_func \"not_called_func(\"\n"
2092                 "OpName %func_modulo4         \"modulo4(u1;\"\n"
2093                 "OpName %func_const5          \"const5(\"\n"
2094                 "OpName %id                   \"gl_GlobalInvocationID\"\n"
2095
2096                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
2097
2098                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
2099
2100                 "%u32ptr    = OpTypePointer Function %u32\n"
2101                 "%uintfuint = OpTypeFunction %u32 %u32ptr\n"
2102                 "%unitf     = OpTypeFunction %u32\n"
2103
2104                 "%id        = OpVariable %uvec3ptr Input\n"
2105                 "%zero      = OpConstant %u32 0\n"
2106                 "%one       = OpConstant %u32 1\n"
2107                 "%two       = OpConstant %u32 2\n"
2108                 "%three     = OpConstant %u32 3\n"
2109                 "%four      = OpConstant %u32 4\n"
2110                 "%five      = OpConstant %u32 5\n"
2111                 "%hundred   = OpConstant %u32 100\n"
2112                 "%thousand  = OpConstant %u32 1000\n"
2113
2114                 + string(getComputeAsmInputOutputBuffer()) +
2115
2116                 // Main()
2117                 "%main   = OpFunction %void None %voidf\n"
2118                 "%main_entry  = OpLabel\n"
2119                 "%v_thousand  = OpVariable %u32ptr Function %thousand\n"
2120                 "%idval       = OpLoad %uvec3 %id\n"
2121                 "%x           = OpCompositeExtract %u32 %idval 0\n"
2122                 "%inloc       = OpAccessChain %f32ptr %indata %zero %x\n"
2123                 "%inval       = OpLoad %f32 %inloc\n"
2124                 "%outloc      = OpAccessChain %f32ptr %outdata %zero %x\n"
2125                 "%ret_const5  = OpFunctionCall %u32 %func_const5\n"
2126                 "%ret_modulo4 = OpFunctionCall %u32 %func_modulo4 %v_thousand\n"
2127                 "%cmp_gt      = OpUGreaterThan %bool %ret_const5 %ret_modulo4\n"
2128                 "               OpSelectionMerge %if_end None\n"
2129                 "               OpBranchConditional %cmp_gt %if_true %if_false\n"
2130                 "%if_true     = OpLabel\n"
2131                 "%negate      = OpFNegate %f32 %inval\n"
2132                 "               OpStore %outloc %negate\n"
2133                 "               OpBranch %if_end\n"
2134                 "%if_false    = OpLabel\n"
2135                 "               OpUnreachable\n" // Unreachable else branch for if statement
2136                 "%if_end      = OpLabel\n"
2137                 "               OpReturn\n"
2138                 "               OpFunctionEnd\n"
2139
2140                 // not_called_function()
2141                 "%func_not_called_func  = OpFunction %void None %voidf\n"
2142                 "%not_called_func_entry = OpLabel\n"
2143                 "                         OpUnreachable\n" // Unreachable entry block in not called static function
2144                 "                         OpFunctionEnd\n"
2145
2146                 // modulo4()
2147                 "%func_modulo4  = OpFunction %u32 None %uintfuint\n"
2148                 "%valptr        = OpFunctionParameter %u32ptr\n"
2149                 "%modulo4_entry = OpLabel\n"
2150                 "%val           = OpLoad %u32 %valptr\n"
2151                 "%modulo        = OpUMod %u32 %val %four\n"
2152                 "                 OpSelectionMerge %switch_merge None\n"
2153                 "                 OpSwitch %modulo %default 0 %case0 1 %case1 2 %case2 3 %case3\n"
2154                 "%case0         = OpLabel\n"
2155                 "                 OpReturnValue %three\n"
2156                 "%case1         = OpLabel\n"
2157                 "                 OpReturnValue %two\n"
2158                 "%case2         = OpLabel\n"
2159                 "                 OpReturnValue %one\n"
2160                 "%case3         = OpLabel\n"
2161                 "                 OpReturnValue %zero\n"
2162                 "%default       = OpLabel\n"
2163                 "                 OpUnreachable\n" // Unreachable default case for switch statement
2164                 "%switch_merge  = OpLabel\n"
2165                 "                 OpUnreachable\n" // Unreachable merge block for switch statement
2166                 "                 OpFunctionEnd\n"
2167
2168                 // const5()
2169                 "%func_const5  = OpFunction %u32 None %unitf\n"
2170                 "%const5_entry = OpLabel\n"
2171                 "                OpReturnValue %five\n"
2172                 "%unreachable  = OpLabel\n"
2173                 "                OpUnreachable\n" // Unreachable block in function
2174                 "                OpFunctionEnd\n";
2175         spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
2176         spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
2177         spec.numWorkGroups = IVec3(numElements, 1, 1);
2178
2179         group->addChild(new SpvAsmComputeShaderCase(testCtx, "all", "OpUnreachable appearing at different places", spec));
2180
2181         return group.release();
2182 }
2183
2184 // Assembly code used for testing decoration group is based on GLSL source code:
2185 //
2186 // #version 430
2187 //
2188 // layout(std140, set = 0, binding = 0) readonly buffer Input0 {
2189 //   float elements[];
2190 // } input_data0;
2191 // layout(std140, set = 0, binding = 1) readonly buffer Input1 {
2192 //   float elements[];
2193 // } input_data1;
2194 // layout(std140, set = 0, binding = 2) readonly buffer Input2 {
2195 //   float elements[];
2196 // } input_data2;
2197 // layout(std140, set = 0, binding = 3) readonly buffer Input3 {
2198 //   float elements[];
2199 // } input_data3;
2200 // layout(std140, set = 0, binding = 4) readonly buffer Input4 {
2201 //   float elements[];
2202 // } input_data4;
2203 // layout(std140, set = 0, binding = 5) writeonly buffer Output {
2204 //   float elements[];
2205 // } output_data;
2206 //
2207 // void main() {
2208 //   uint x = gl_GlobalInvocationID.x;
2209 //   output_data.elements[x] = input_data0.elements[x] + input_data1.elements[x] + input_data2.elements[x] + input_data3.elements[x] + input_data4.elements[x];
2210 // }
2211 tcu::TestCaseGroup* createDecorationGroupGroup (tcu::TestContext& testCtx)
2212 {
2213         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "decoration_group", "Test the OpDecorationGroup & OpGroupDecorate instruction"));
2214         ComputeShaderSpec                               spec;
2215         de::Random                                              rnd                             (deStringHash(group->getName()));
2216         const int                                               numElements             = 100;
2217         vector<float>                                   inputFloats0    (numElements, 0);
2218         vector<float>                                   inputFloats1    (numElements, 0);
2219         vector<float>                                   inputFloats2    (numElements, 0);
2220         vector<float>                                   inputFloats3    (numElements, 0);
2221         vector<float>                                   inputFloats4    (numElements, 0);
2222         vector<float>                                   outputFloats    (numElements, 0);
2223
2224         fillRandomScalars(rnd, -300.f, 300.f, &inputFloats0[0], numElements);
2225         fillRandomScalars(rnd, -300.f, 300.f, &inputFloats1[0], numElements);
2226         fillRandomScalars(rnd, -300.f, 300.f, &inputFloats2[0], numElements);
2227         fillRandomScalars(rnd, -300.f, 300.f, &inputFloats3[0], numElements);
2228         fillRandomScalars(rnd, -300.f, 300.f, &inputFloats4[0], numElements);
2229
2230         // CPU might not use the same rounding mode as the GPU. Use whole numbers to avoid rounding differences.
2231         floorAll(inputFloats0);
2232         floorAll(inputFloats1);
2233         floorAll(inputFloats2);
2234         floorAll(inputFloats3);
2235         floorAll(inputFloats4);
2236
2237         for (size_t ndx = 0; ndx < numElements; ++ndx)
2238                 outputFloats[ndx] = inputFloats0[ndx] + inputFloats1[ndx] + inputFloats2[ndx] + inputFloats3[ndx] + inputFloats4[ndx];
2239
2240         spec.assembly =
2241                 string(getComputeAsmShaderPreamble()) +
2242
2243                 "OpSource GLSL 430\n"
2244                 "OpName %main \"main\"\n"
2245                 "OpName %id \"gl_GlobalInvocationID\"\n"
2246
2247                 // Not using group decoration on variable.
2248                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
2249                 // Not using group decoration on type.
2250                 "OpDecorate %f32arr ArrayStride 4\n"
2251
2252                 "OpDecorate %groups BufferBlock\n"
2253                 "OpDecorate %groupm Offset 0\n"
2254                 "%groups = OpDecorationGroup\n"
2255                 "%groupm = OpDecorationGroup\n"
2256
2257                 // Group decoration on multiple structs.
2258                 "OpGroupDecorate %groups %outbuf %inbuf0 %inbuf1 %inbuf2 %inbuf3 %inbuf4\n"
2259                 // Group decoration on multiple struct members.
2260                 "OpGroupMemberDecorate %groupm %outbuf 0 %inbuf0 0 %inbuf1 0 %inbuf2 0 %inbuf3 0 %inbuf4 0\n"
2261
2262                 "OpDecorate %group1 DescriptorSet 0\n"
2263                 "OpDecorate %group3 DescriptorSet 0\n"
2264                 "OpDecorate %group3 NonWritable\n"
2265                 "OpDecorate %group3 Restrict\n"
2266                 "%group0 = OpDecorationGroup\n"
2267                 "%group1 = OpDecorationGroup\n"
2268                 "%group3 = OpDecorationGroup\n"
2269
2270                 // Applying the same decoration group multiple times.
2271                 "OpGroupDecorate %group1 %outdata\n"
2272                 "OpGroupDecorate %group1 %outdata\n"
2273                 "OpGroupDecorate %group1 %outdata\n"
2274                 "OpDecorate %outdata DescriptorSet 0\n"
2275                 "OpDecorate %outdata Binding 5\n"
2276                 // Applying decoration group containing nothing.
2277                 "OpGroupDecorate %group0 %indata0\n"
2278                 "OpDecorate %indata0 DescriptorSet 0\n"
2279                 "OpDecorate %indata0 Binding 0\n"
2280                 // Applying decoration group containing one decoration.
2281                 "OpGroupDecorate %group1 %indata1\n"
2282                 "OpDecorate %indata1 Binding 1\n"
2283                 // Applying decoration group containing multiple decorations.
2284                 "OpGroupDecorate %group3 %indata2 %indata3\n"
2285                 "OpDecorate %indata2 Binding 2\n"
2286                 "OpDecorate %indata3 Binding 3\n"
2287                 // Applying multiple decoration groups (with overlapping).
2288                 "OpGroupDecorate %group0 %indata4\n"
2289                 "OpGroupDecorate %group1 %indata4\n"
2290                 "OpGroupDecorate %group3 %indata4\n"
2291                 "OpDecorate %indata4 Binding 4\n"
2292
2293                 + string(getComputeAsmCommonTypes()) +
2294
2295                 "%id   = OpVariable %uvec3ptr Input\n"
2296                 "%zero = OpConstant %i32 0\n"
2297
2298                 "%outbuf    = OpTypeStruct %f32arr\n"
2299                 "%outbufptr = OpTypePointer Uniform %outbuf\n"
2300                 "%outdata   = OpVariable %outbufptr Uniform\n"
2301                 "%inbuf0    = OpTypeStruct %f32arr\n"
2302                 "%inbuf0ptr = OpTypePointer Uniform %inbuf0\n"
2303                 "%indata0   = OpVariable %inbuf0ptr Uniform\n"
2304                 "%inbuf1    = OpTypeStruct %f32arr\n"
2305                 "%inbuf1ptr = OpTypePointer Uniform %inbuf1\n"
2306                 "%indata1   = OpVariable %inbuf1ptr Uniform\n"
2307                 "%inbuf2    = OpTypeStruct %f32arr\n"
2308                 "%inbuf2ptr = OpTypePointer Uniform %inbuf2\n"
2309                 "%indata2   = OpVariable %inbuf2ptr Uniform\n"
2310                 "%inbuf3    = OpTypeStruct %f32arr\n"
2311                 "%inbuf3ptr = OpTypePointer Uniform %inbuf3\n"
2312                 "%indata3   = OpVariable %inbuf3ptr Uniform\n"
2313                 "%inbuf4    = OpTypeStruct %f32arr\n"
2314                 "%inbufptr  = OpTypePointer Uniform %inbuf4\n"
2315                 "%indata4   = OpVariable %inbufptr Uniform\n"
2316
2317                 "%main   = OpFunction %void None %voidf\n"
2318                 "%label  = OpLabel\n"
2319                 "%idval  = OpLoad %uvec3 %id\n"
2320                 "%x      = OpCompositeExtract %u32 %idval 0\n"
2321                 "%inloc0 = OpAccessChain %f32ptr %indata0 %zero %x\n"
2322                 "%inloc1 = OpAccessChain %f32ptr %indata1 %zero %x\n"
2323                 "%inloc2 = OpAccessChain %f32ptr %indata2 %zero %x\n"
2324                 "%inloc3 = OpAccessChain %f32ptr %indata3 %zero %x\n"
2325                 "%inloc4 = OpAccessChain %f32ptr %indata4 %zero %x\n"
2326                 "%outloc = OpAccessChain %f32ptr %outdata %zero %x\n"
2327                 "%inval0 = OpLoad %f32 %inloc0\n"
2328                 "%inval1 = OpLoad %f32 %inloc1\n"
2329                 "%inval2 = OpLoad %f32 %inloc2\n"
2330                 "%inval3 = OpLoad %f32 %inloc3\n"
2331                 "%inval4 = OpLoad %f32 %inloc4\n"
2332                 "%add0   = OpFAdd %f32 %inval0 %inval1\n"
2333                 "%add1   = OpFAdd %f32 %add0 %inval2\n"
2334                 "%add2   = OpFAdd %f32 %add1 %inval3\n"
2335                 "%add    = OpFAdd %f32 %add2 %inval4\n"
2336                 "          OpStore %outloc %add\n"
2337                 "          OpReturn\n"
2338                 "          OpFunctionEnd\n";
2339         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats0)));
2340         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats1)));
2341         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats2)));
2342         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats3)));
2343         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats4)));
2344         spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
2345         spec.numWorkGroups = IVec3(numElements, 1, 1);
2346
2347         group->addChild(new SpvAsmComputeShaderCase(testCtx, "all", "decoration group cases", spec));
2348
2349         return group.release();
2350 }
2351
2352 struct SpecConstantTwoIntCase
2353 {
2354         const char*             caseName;
2355         const char*             scDefinition0;
2356         const char*             scDefinition1;
2357         const char*             scResultType;
2358         const char*             scOperation;
2359         deInt32                 scActualValue0;
2360         deInt32                 scActualValue1;
2361         const char*             resultOperation;
2362         vector<deInt32> expectedOutput;
2363
2364                                         SpecConstantTwoIntCase (const char* name,
2365                                                                                         const char* definition0,
2366                                                                                         const char* definition1,
2367                                                                                         const char* resultType,
2368                                                                                         const char* operation,
2369                                                                                         deInt32 value0,
2370                                                                                         deInt32 value1,
2371                                                                                         const char* resultOp,
2372                                                                                         const vector<deInt32>& output)
2373                                                 : caseName                      (name)
2374                                                 , scDefinition0         (definition0)
2375                                                 , scDefinition1         (definition1)
2376                                                 , scResultType          (resultType)
2377                                                 , scOperation           (operation)
2378                                                 , scActualValue0        (value0)
2379                                                 , scActualValue1        (value1)
2380                                                 , resultOperation       (resultOp)
2381                                                 , expectedOutput        (output) {}
2382 };
2383
2384 tcu::TestCaseGroup* createSpecConstantGroup (tcu::TestContext& testCtx)
2385 {
2386         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opspecconstantop", "Test the OpSpecConstantOp instruction"));
2387         vector<SpecConstantTwoIntCase>  cases;
2388         de::Random                                              rnd                             (deStringHash(group->getName()));
2389         const int                                               numElements             = 100;
2390         vector<deInt32>                                 inputInts               (numElements, 0);
2391         vector<deInt32>                                 outputInts1             (numElements, 0);
2392         vector<deInt32>                                 outputInts2             (numElements, 0);
2393         vector<deInt32>                                 outputInts3             (numElements, 0);
2394         vector<deInt32>                                 outputInts4             (numElements, 0);
2395         const StringTemplate                    shaderTemplate  (
2396                 string(getComputeAsmShaderPreamble()) +
2397
2398                 "OpName %main           \"main\"\n"
2399                 "OpName %id             \"gl_GlobalInvocationID\"\n"
2400
2401                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
2402                 "OpDecorate %sc_0  SpecId 0\n"
2403                 "OpDecorate %sc_1  SpecId 1\n"
2404                 "OpDecorate %i32arr ArrayStride 4\n"
2405
2406                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
2407
2408                 "%buf     = OpTypeStruct %i32arr\n"
2409                 "%bufptr  = OpTypePointer Uniform %buf\n"
2410                 "%indata    = OpVariable %bufptr Uniform\n"
2411                 "%outdata   = OpVariable %bufptr Uniform\n"
2412
2413                 "%id        = OpVariable %uvec3ptr Input\n"
2414                 "%zero      = OpConstant %i32 0\n"
2415
2416                 "%sc_0      = OpSpecConstant${SC_DEF0}\n"
2417                 "%sc_1      = OpSpecConstant${SC_DEF1}\n"
2418                 "%sc_final  = OpSpecConstantOp ${SC_RESULT_TYPE} ${SC_OP}\n"
2419
2420                 "%main      = OpFunction %void None %voidf\n"
2421                 "%label     = OpLabel\n"
2422                 "%idval     = OpLoad %uvec3 %id\n"
2423                 "%x         = OpCompositeExtract %u32 %idval 0\n"
2424                 "%inloc     = OpAccessChain %i32ptr %indata %zero %x\n"
2425                 "%inval     = OpLoad %i32 %inloc\n"
2426                 "%final     = ${GEN_RESULT}\n"
2427                 "%outloc    = OpAccessChain %i32ptr %outdata %zero %x\n"
2428                 "             OpStore %outloc %final\n"
2429                 "             OpReturn\n"
2430                 "             OpFunctionEnd\n");
2431
2432         fillRandomScalars(rnd, -65536, 65536, &inputInts[0], numElements);
2433
2434         for (size_t ndx = 0; ndx < numElements; ++ndx)
2435         {
2436                 outputInts1[ndx] = inputInts[ndx] + 42;
2437                 outputInts2[ndx] = inputInts[ndx];
2438                 outputInts3[ndx] = inputInts[ndx] - 11200;
2439                 outputInts4[ndx] = inputInts[ndx] + 1;
2440         }
2441
2442         const char addScToInput[]               = "OpIAdd %i32 %inval %sc_final";
2443         const char selectTrueUsingSc[]  = "OpSelect %i32 %sc_final %inval %zero";
2444         const char selectFalseUsingSc[] = "OpSelect %i32 %sc_final %zero %inval";
2445
2446         cases.push_back(SpecConstantTwoIntCase("iadd",                                  " %i32 0",              " %i32 0",              "%i32",         "IAdd                 %sc_0 %sc_1",                     62,             -20,    addScToInput,           outputInts1));
2447         cases.push_back(SpecConstantTwoIntCase("isub",                                  " %i32 0",              " %i32 0",              "%i32",         "ISub                 %sc_0 %sc_1",                     100,    58,             addScToInput,           outputInts1));
2448         cases.push_back(SpecConstantTwoIntCase("imul",                                  " %i32 0",              " %i32 0",              "%i32",         "IMul                 %sc_0 %sc_1",                     -2,             -21,    addScToInput,           outputInts1));
2449         cases.push_back(SpecConstantTwoIntCase("sdiv",                                  " %i32 0",              " %i32 0",              "%i32",         "SDiv                 %sc_0 %sc_1",                     -126,   -3,             addScToInput,           outputInts1));
2450         cases.push_back(SpecConstantTwoIntCase("udiv",                                  " %i32 0",              " %i32 0",              "%i32",         "UDiv                 %sc_0 %sc_1",                     126,    3,              addScToInput,           outputInts1));
2451         cases.push_back(SpecConstantTwoIntCase("srem",                                  " %i32 0",              " %i32 0",              "%i32",         "SRem                 %sc_0 %sc_1",                     7,              3,              addScToInput,           outputInts4));
2452         cases.push_back(SpecConstantTwoIntCase("smod",                                  " %i32 0",              " %i32 0",              "%i32",         "SMod                 %sc_0 %sc_1",                     7,              3,              addScToInput,           outputInts4));
2453         cases.push_back(SpecConstantTwoIntCase("umod",                                  " %i32 0",              " %i32 0",              "%i32",         "UMod                 %sc_0 %sc_1",                     342,    50,             addScToInput,           outputInts1));
2454         cases.push_back(SpecConstantTwoIntCase("bitwiseand",                    " %i32 0",              " %i32 0",              "%i32",         "BitwiseAnd           %sc_0 %sc_1",                     42,             63,             addScToInput,           outputInts1));
2455         cases.push_back(SpecConstantTwoIntCase("bitwiseor",                             " %i32 0",              " %i32 0",              "%i32",         "BitwiseOr            %sc_0 %sc_1",                     34,             8,              addScToInput,           outputInts1));
2456         cases.push_back(SpecConstantTwoIntCase("bitwisexor",                    " %i32 0",              " %i32 0",              "%i32",         "BitwiseXor           %sc_0 %sc_1",                     18,             56,             addScToInput,           outputInts1));
2457         cases.push_back(SpecConstantTwoIntCase("shiftrightlogical",             " %i32 0",              " %i32 0",              "%i32",         "ShiftRightLogical    %sc_0 %sc_1",                     168,    2,              addScToInput,           outputInts1));
2458         cases.push_back(SpecConstantTwoIntCase("shiftrightarithmetic",  " %i32 0",              " %i32 0",              "%i32",         "ShiftRightArithmetic %sc_0 %sc_1",                     168,    2,              addScToInput,           outputInts1));
2459         cases.push_back(SpecConstantTwoIntCase("shiftleftlogical",              " %i32 0",              " %i32 0",              "%i32",         "ShiftLeftLogical     %sc_0 %sc_1",                     21,             1,              addScToInput,           outputInts1));
2460         cases.push_back(SpecConstantTwoIntCase("slessthan",                             " %i32 0",              " %i32 0",              "%bool",        "SLessThan            %sc_0 %sc_1",                     -20,    -10,    selectTrueUsingSc,      outputInts2));
2461         cases.push_back(SpecConstantTwoIntCase("ulessthan",                             " %i32 0",              " %i32 0",              "%bool",        "ULessThan            %sc_0 %sc_1",                     10,             20,             selectTrueUsingSc,      outputInts2));
2462         cases.push_back(SpecConstantTwoIntCase("sgreaterthan",                  " %i32 0",              " %i32 0",              "%bool",        "SGreaterThan         %sc_0 %sc_1",                     -1000,  50,             selectFalseUsingSc,     outputInts2));
2463         cases.push_back(SpecConstantTwoIntCase("ugreaterthan",                  " %i32 0",              " %i32 0",              "%bool",        "UGreaterThan         %sc_0 %sc_1",                     10,             5,              selectTrueUsingSc,      outputInts2));
2464         cases.push_back(SpecConstantTwoIntCase("slessthanequal",                " %i32 0",              " %i32 0",              "%bool",        "SLessThanEqual       %sc_0 %sc_1",                     -10,    -10,    selectTrueUsingSc,      outputInts2));
2465         cases.push_back(SpecConstantTwoIntCase("ulessthanequal",                " %i32 0",              " %i32 0",              "%bool",        "ULessThanEqual       %sc_0 %sc_1",                     50,             100,    selectTrueUsingSc,      outputInts2));
2466         cases.push_back(SpecConstantTwoIntCase("sgreaterthanequal",             " %i32 0",              " %i32 0",              "%bool",        "SGreaterThanEqual    %sc_0 %sc_1",                     -1000,  50,             selectFalseUsingSc,     outputInts2));
2467         cases.push_back(SpecConstantTwoIntCase("ugreaterthanequal",             " %i32 0",              " %i32 0",              "%bool",        "UGreaterThanEqual    %sc_0 %sc_1",                     10,             10,             selectTrueUsingSc,      outputInts2));
2468         cases.push_back(SpecConstantTwoIntCase("iequal",                                " %i32 0",              " %i32 0",              "%bool",        "IEqual               %sc_0 %sc_1",                     42,             24,             selectFalseUsingSc,     outputInts2));
2469         cases.push_back(SpecConstantTwoIntCase("logicaland",                    "True %bool",   "True %bool",   "%bool",        "LogicalAnd           %sc_0 %sc_1",                     0,              1,              selectFalseUsingSc,     outputInts2));
2470         cases.push_back(SpecConstantTwoIntCase("logicalor",                             "False %bool",  "False %bool",  "%bool",        "LogicalOr            %sc_0 %sc_1",                     1,              0,              selectTrueUsingSc,      outputInts2));
2471         cases.push_back(SpecConstantTwoIntCase("logicalequal",                  "True %bool",   "True %bool",   "%bool",        "LogicalEqual         %sc_0 %sc_1",                     0,              1,              selectFalseUsingSc,     outputInts2));
2472         cases.push_back(SpecConstantTwoIntCase("logicalnotequal",               "False %bool",  "False %bool",  "%bool",        "LogicalNotEqual      %sc_0 %sc_1",                     1,              0,              selectTrueUsingSc,      outputInts2));
2473         cases.push_back(SpecConstantTwoIntCase("snegate",                               " %i32 0",              " %i32 0",              "%i32",         "SNegate              %sc_0",                           -42,    0,              addScToInput,           outputInts1));
2474         cases.push_back(SpecConstantTwoIntCase("not",                                   " %i32 0",              " %i32 0",              "%i32",         "Not                  %sc_0",                           -43,    0,              addScToInput,           outputInts1));
2475         cases.push_back(SpecConstantTwoIntCase("logicalnot",                    "False %bool",  "False %bool",  "%bool",        "LogicalNot           %sc_0",                           1,              0,              selectFalseUsingSc,     outputInts2));
2476         cases.push_back(SpecConstantTwoIntCase("select",                                "False %bool",  " %i32 0",              "%i32",         "Select               %sc_0 %sc_1 %zero",       1,              42,             addScToInput,           outputInts1));
2477         // OpSConvert, OpFConvert: these two instructions involve ints/floats of different bitwidths.
2478
2479         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
2480         {
2481                 map<string, string>             specializations;
2482                 ComputeShaderSpec               spec;
2483
2484                 specializations["SC_DEF0"]                      = cases[caseNdx].scDefinition0;
2485                 specializations["SC_DEF1"]                      = cases[caseNdx].scDefinition1;
2486                 specializations["SC_RESULT_TYPE"]       = cases[caseNdx].scResultType;
2487                 specializations["SC_OP"]                        = cases[caseNdx].scOperation;
2488                 specializations["GEN_RESULT"]           = cases[caseNdx].resultOperation;
2489
2490                 spec.assembly = shaderTemplate.specialize(specializations);
2491                 spec.inputs.push_back(BufferSp(new Int32Buffer(inputInts)));
2492                 spec.outputs.push_back(BufferSp(new Int32Buffer(cases[caseNdx].expectedOutput)));
2493                 spec.numWorkGroups = IVec3(numElements, 1, 1);
2494                 spec.specConstants.push_back(cases[caseNdx].scActualValue0);
2495                 spec.specConstants.push_back(cases[caseNdx].scActualValue1);
2496
2497                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].caseName, cases[caseNdx].caseName, spec));
2498         }
2499
2500         ComputeShaderSpec                               spec;
2501
2502         spec.assembly =
2503                 string(getComputeAsmShaderPreamble()) +
2504
2505                 "OpName %main           \"main\"\n"
2506                 "OpName %id             \"gl_GlobalInvocationID\"\n"
2507
2508                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
2509                 "OpDecorate %sc_0  SpecId 0\n"
2510                 "OpDecorate %sc_1  SpecId 1\n"
2511                 "OpDecorate %sc_2  SpecId 2\n"
2512                 "OpDecorate %i32arr ArrayStride 4\n"
2513
2514                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
2515
2516                 "%ivec3       = OpTypeVector %i32 3\n"
2517                 "%buf         = OpTypeStruct %i32arr\n"
2518                 "%bufptr      = OpTypePointer Uniform %buf\n"
2519                 "%indata      = OpVariable %bufptr Uniform\n"
2520                 "%outdata     = OpVariable %bufptr Uniform\n"
2521
2522                 "%id          = OpVariable %uvec3ptr Input\n"
2523                 "%zero        = OpConstant %i32 0\n"
2524                 "%ivec3_0     = OpConstantComposite %ivec3 %zero %zero %zero\n"
2525                 "%vec3_undef  = OpUndef %ivec3\n"
2526
2527                 "%sc_0        = OpSpecConstant %i32 0\n"
2528                 "%sc_1        = OpSpecConstant %i32 0\n"
2529                 "%sc_2        = OpSpecConstant %i32 0\n"
2530                 "%sc_vec3_0   = OpSpecConstantOp %ivec3 CompositeInsert  %sc_0        %ivec3_0     0\n"                                                 // (sc_0, 0, 0)
2531                 "%sc_vec3_1   = OpSpecConstantOp %ivec3 CompositeInsert  %sc_1        %ivec3_0     1\n"                                                 // (0, sc_1, 0)
2532                 "%sc_vec3_2   = OpSpecConstantOp %ivec3 CompositeInsert  %sc_2        %ivec3_0     2\n"                                                 // (0, 0, sc_2)
2533                 "%sc_vec3_0_s = OpSpecConstantOp %ivec3 VectorShuffle    %sc_vec3_0   %vec3_undef  0          0xFFFFFFFF 2\n"   // (sc_0, ???,  0)
2534                 "%sc_vec3_1_s = OpSpecConstantOp %ivec3 VectorShuffle    %sc_vec3_1   %vec3_undef  0xFFFFFFFF 1          0\n"   // (???,  sc_1, 0)
2535                 "%sc_vec3_2_s = OpSpecConstantOp %ivec3 VectorShuffle    %vec3_undef  %sc_vec3_2   5          0xFFFFFFFF 5\n"   // (sc_2, ???,  sc_2)
2536                 "%sc_vec3_01  = OpSpecConstantOp %ivec3 VectorShuffle    %sc_vec3_0_s %sc_vec3_1_s 1 0 4\n"                                             // (0,    sc_0, sc_1)
2537                 "%sc_vec3_012 = OpSpecConstantOp %ivec3 VectorShuffle    %sc_vec3_01  %sc_vec3_2_s 5 1 2\n"                                             // (sc_2, sc_0, sc_1)
2538                 "%sc_ext_0    = OpSpecConstantOp %i32   CompositeExtract %sc_vec3_012              0\n"                                                 // sc_2
2539                 "%sc_ext_1    = OpSpecConstantOp %i32   CompositeExtract %sc_vec3_012              1\n"                                                 // sc_0
2540                 "%sc_ext_2    = OpSpecConstantOp %i32   CompositeExtract %sc_vec3_012              2\n"                                                 // sc_1
2541                 "%sc_sub      = OpSpecConstantOp %i32   ISub             %sc_ext_0    %sc_ext_1\n"                                                              // (sc_2 - sc_0)
2542                 "%sc_final    = OpSpecConstantOp %i32   IMul             %sc_sub      %sc_ext_2\n"                                                              // (sc_2 - sc_0) * sc_1
2543
2544                 "%main      = OpFunction %void None %voidf\n"
2545                 "%label     = OpLabel\n"
2546                 "%idval     = OpLoad %uvec3 %id\n"
2547                 "%x         = OpCompositeExtract %u32 %idval 0\n"
2548                 "%inloc     = OpAccessChain %i32ptr %indata %zero %x\n"
2549                 "%inval     = OpLoad %i32 %inloc\n"
2550                 "%final     = OpIAdd %i32 %inval %sc_final\n"
2551                 "%outloc    = OpAccessChain %i32ptr %outdata %zero %x\n"
2552                 "             OpStore %outloc %final\n"
2553                 "             OpReturn\n"
2554                 "             OpFunctionEnd\n";
2555         spec.inputs.push_back(BufferSp(new Int32Buffer(inputInts)));
2556         spec.outputs.push_back(BufferSp(new Int32Buffer(outputInts3)));
2557         spec.numWorkGroups = IVec3(numElements, 1, 1);
2558         spec.specConstants.push_back(123);
2559         spec.specConstants.push_back(56);
2560         spec.specConstants.push_back(-77);
2561
2562         group->addChild(new SpvAsmComputeShaderCase(testCtx, "vector_related", "VectorShuffle, CompositeExtract, & CompositeInsert", spec));
2563
2564         return group.release();
2565 }
2566
2567 void createOpPhiVartypeTests (de::MovePtr<tcu::TestCaseGroup>& group, tcu::TestContext& testCtx)
2568 {
2569         ComputeShaderSpec       specInt;
2570         ComputeShaderSpec       specFloat;
2571         ComputeShaderSpec       specVec3;
2572         ComputeShaderSpec       specMat4;
2573         ComputeShaderSpec       specArray;
2574         ComputeShaderSpec       specStruct;
2575         de::Random                      rnd                             (deStringHash(group->getName()));
2576         const int                       numElements             = 100;
2577         vector<float>           inputFloats             (numElements, 0);
2578         vector<float>           outputFloats    (numElements, 0);
2579
2580         fillRandomScalars(rnd, -300.f, 300.f, &inputFloats[0], numElements);
2581
2582         // CPU might not use the same rounding mode as the GPU. Use whole numbers to avoid rounding differences.
2583         floorAll(inputFloats);
2584
2585         for (size_t ndx = 0; ndx < numElements; ++ndx)
2586         {
2587                 // Just check if the value is positive or not
2588                 outputFloats[ndx] = (inputFloats[ndx] > 0) ? 1.0f : -1.0f;
2589         }
2590
2591         // All of the tests are of the form:
2592         //
2593         // testtype r
2594         //
2595         // if (inputdata > 0)
2596         //   r = 1
2597         // else
2598         //   r = -1
2599         //
2600         // return (float)r
2601
2602         specFloat.assembly =
2603                 string(getComputeAsmShaderPreamble()) +
2604
2605                 "OpSource GLSL 430\n"
2606                 "OpName %main \"main\"\n"
2607                 "OpName %id \"gl_GlobalInvocationID\"\n"
2608
2609                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
2610
2611                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
2612
2613                 "%id = OpVariable %uvec3ptr Input\n"
2614                 "%zero       = OpConstant %i32 0\n"
2615                 "%float_0    = OpConstant %f32 0.0\n"
2616                 "%float_1    = OpConstant %f32 1.0\n"
2617                 "%float_n1   = OpConstant %f32 -1.0\n"
2618
2619                 "%main     = OpFunction %void None %voidf\n"
2620                 "%entry    = OpLabel\n"
2621                 "%idval    = OpLoad %uvec3 %id\n"
2622                 "%x        = OpCompositeExtract %u32 %idval 0\n"
2623                 "%inloc    = OpAccessChain %f32ptr %indata %zero %x\n"
2624                 "%inval    = OpLoad %f32 %inloc\n"
2625
2626                 "%comp     = OpFOrdGreaterThan %bool %inval %float_0\n"
2627                 "            OpSelectionMerge %cm None\n"
2628                 "            OpBranchConditional %comp %tb %fb\n"
2629                 "%tb       = OpLabel\n"
2630                 "            OpBranch %cm\n"
2631                 "%fb       = OpLabel\n"
2632                 "            OpBranch %cm\n"
2633                 "%cm       = OpLabel\n"
2634                 "%res      = OpPhi %f32 %float_1 %tb %float_n1 %fb\n"
2635
2636                 "%outloc   = OpAccessChain %f32ptr %outdata %zero %x\n"
2637                 "            OpStore %outloc %res\n"
2638                 "            OpReturn\n"
2639
2640                 "            OpFunctionEnd\n";
2641         specFloat.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
2642         specFloat.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
2643         specFloat.numWorkGroups = IVec3(numElements, 1, 1);
2644
2645         specMat4.assembly =
2646                 string(getComputeAsmShaderPreamble()) +
2647
2648                 "OpSource GLSL 430\n"
2649                 "OpName %main \"main\"\n"
2650                 "OpName %id \"gl_GlobalInvocationID\"\n"
2651
2652                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
2653
2654                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
2655
2656                 "%id = OpVariable %uvec3ptr Input\n"
2657                 "%v4f32      = OpTypeVector %f32 4\n"
2658                 "%mat4v4f32  = OpTypeMatrix %v4f32 4\n"
2659                 "%zero       = OpConstant %i32 0\n"
2660                 "%float_0    = OpConstant %f32 0.0\n"
2661                 "%float_1    = OpConstant %f32 1.0\n"
2662                 "%float_n1   = OpConstant %f32 -1.0\n"
2663                 "%m11        = OpConstantComposite %v4f32 %float_1 %float_0 %float_0 %float_0\n"
2664                 "%m12        = OpConstantComposite %v4f32 %float_0 %float_1 %float_0 %float_0\n"
2665                 "%m13        = OpConstantComposite %v4f32 %float_0 %float_0 %float_1 %float_0\n"
2666                 "%m14        = OpConstantComposite %v4f32 %float_0 %float_0 %float_0 %float_1\n"
2667                 "%m1         = OpConstantComposite %mat4v4f32 %m11 %m12 %m13 %m14\n"
2668                 "%m21        = OpConstantComposite %v4f32 %float_n1 %float_0 %float_0 %float_0\n"
2669                 "%m22        = OpConstantComposite %v4f32 %float_0 %float_n1 %float_0 %float_0\n"
2670                 "%m23        = OpConstantComposite %v4f32 %float_0 %float_0 %float_n1 %float_0\n"
2671                 "%m24        = OpConstantComposite %v4f32 %float_0 %float_0 %float_0 %float_n1\n"
2672                 "%m2         = OpConstantComposite %mat4v4f32 %m21 %m22 %m23 %m24\n"
2673
2674                 "%main     = OpFunction %void None %voidf\n"
2675                 "%entry    = OpLabel\n"
2676                 "%idval    = OpLoad %uvec3 %id\n"
2677                 "%x        = OpCompositeExtract %u32 %idval 0\n"
2678                 "%inloc    = OpAccessChain %f32ptr %indata %zero %x\n"
2679                 "%inval    = OpLoad %f32 %inloc\n"
2680
2681                 "%comp     = OpFOrdGreaterThan %bool %inval %float_0\n"
2682                 "            OpSelectionMerge %cm None\n"
2683                 "            OpBranchConditional %comp %tb %fb\n"
2684                 "%tb       = OpLabel\n"
2685                 "            OpBranch %cm\n"
2686                 "%fb       = OpLabel\n"
2687                 "            OpBranch %cm\n"
2688                 "%cm       = OpLabel\n"
2689                 "%mres     = OpPhi %mat4v4f32 %m1 %tb %m2 %fb\n"
2690                 "%res      = OpCompositeExtract %f32 %mres 2 2\n"
2691
2692                 "%outloc   = OpAccessChain %f32ptr %outdata %zero %x\n"
2693                 "            OpStore %outloc %res\n"
2694                 "            OpReturn\n"
2695
2696                 "            OpFunctionEnd\n";
2697         specMat4.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
2698         specMat4.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
2699         specMat4.numWorkGroups = IVec3(numElements, 1, 1);
2700
2701         specVec3.assembly =
2702                 string(getComputeAsmShaderPreamble()) +
2703
2704                 "OpSource GLSL 430\n"
2705                 "OpName %main \"main\"\n"
2706                 "OpName %id \"gl_GlobalInvocationID\"\n"
2707
2708                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
2709
2710                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
2711
2712                 "%id = OpVariable %uvec3ptr Input\n"
2713                 "%v3f32      = OpTypeVector %f32 3\n"
2714                 "%zero       = OpConstant %i32 0\n"
2715                 "%float_0    = OpConstant %f32 0.0\n"
2716                 "%float_1    = OpConstant %f32 1.0\n"
2717                 "%float_n1   = OpConstant %f32 -1.0\n"
2718                 "%v1         = OpConstantComposite %v3f32 %float_1 %float_1 %float_1\n"
2719                 "%v2         = OpConstantComposite %v3f32 %float_n1 %float_n1 %float_n1\n"
2720
2721                 "%main     = OpFunction %void None %voidf\n"
2722                 "%entry    = OpLabel\n"
2723                 "%idval    = OpLoad %uvec3 %id\n"
2724                 "%x        = OpCompositeExtract %u32 %idval 0\n"
2725                 "%inloc    = OpAccessChain %f32ptr %indata %zero %x\n"
2726                 "%inval    = OpLoad %f32 %inloc\n"
2727
2728                 "%comp     = OpFOrdGreaterThan %bool %inval %float_0\n"
2729                 "            OpSelectionMerge %cm None\n"
2730                 "            OpBranchConditional %comp %tb %fb\n"
2731                 "%tb       = OpLabel\n"
2732                 "            OpBranch %cm\n"
2733                 "%fb       = OpLabel\n"
2734                 "            OpBranch %cm\n"
2735                 "%cm       = OpLabel\n"
2736                 "%vres     = OpPhi %v3f32 %v1 %tb %v2 %fb\n"
2737                 "%res      = OpCompositeExtract %f32 %vres 2\n"
2738
2739                 "%outloc   = OpAccessChain %f32ptr %outdata %zero %x\n"
2740                 "            OpStore %outloc %res\n"
2741                 "            OpReturn\n"
2742
2743                 "            OpFunctionEnd\n";
2744         specVec3.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
2745         specVec3.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
2746         specVec3.numWorkGroups = IVec3(numElements, 1, 1);
2747
2748         specInt.assembly =
2749                 string(getComputeAsmShaderPreamble()) +
2750
2751                 "OpSource GLSL 430\n"
2752                 "OpName %main \"main\"\n"
2753                 "OpName %id \"gl_GlobalInvocationID\"\n"
2754
2755                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
2756
2757                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
2758
2759                 "%id = OpVariable %uvec3ptr Input\n"
2760                 "%zero       = OpConstant %i32 0\n"
2761                 "%float_0    = OpConstant %f32 0.0\n"
2762                 "%i1         = OpConstant %i32 1\n"
2763                 "%i2         = OpConstant %i32 -1\n"
2764
2765                 "%main     = OpFunction %void None %voidf\n"
2766                 "%entry    = OpLabel\n"
2767                 "%idval    = OpLoad %uvec3 %id\n"
2768                 "%x        = OpCompositeExtract %u32 %idval 0\n"
2769                 "%inloc    = OpAccessChain %f32ptr %indata %zero %x\n"
2770                 "%inval    = OpLoad %f32 %inloc\n"
2771
2772                 "%comp     = OpFOrdGreaterThan %bool %inval %float_0\n"
2773                 "            OpSelectionMerge %cm None\n"
2774                 "            OpBranchConditional %comp %tb %fb\n"
2775                 "%tb       = OpLabel\n"
2776                 "            OpBranch %cm\n"
2777                 "%fb       = OpLabel\n"
2778                 "            OpBranch %cm\n"
2779                 "%cm       = OpLabel\n"
2780                 "%ires     = OpPhi %i32 %i1 %tb %i2 %fb\n"
2781                 "%res      = OpConvertSToF %f32 %ires\n"
2782
2783                 "%outloc   = OpAccessChain %f32ptr %outdata %zero %x\n"
2784                 "            OpStore %outloc %res\n"
2785                 "            OpReturn\n"
2786
2787                 "            OpFunctionEnd\n";
2788         specInt.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
2789         specInt.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
2790         specInt.numWorkGroups = IVec3(numElements, 1, 1);
2791
2792         specArray.assembly =
2793                 string(getComputeAsmShaderPreamble()) +
2794
2795                 "OpSource GLSL 430\n"
2796                 "OpName %main \"main\"\n"
2797                 "OpName %id \"gl_GlobalInvocationID\"\n"
2798
2799                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
2800
2801                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
2802
2803                 "%id = OpVariable %uvec3ptr Input\n"
2804                 "%zero       = OpConstant %i32 0\n"
2805                 "%u7         = OpConstant %u32 7\n"
2806                 "%float_0    = OpConstant %f32 0.0\n"
2807                 "%float_1    = OpConstant %f32 1.0\n"
2808                 "%float_n1   = OpConstant %f32 -1.0\n"
2809                 "%f32a7      = OpTypeArray %f32 %u7\n"
2810                 "%a1         = OpConstantComposite %f32a7 %float_1 %float_1 %float_1 %float_1 %float_1 %float_1 %float_1\n"
2811                 "%a2         = OpConstantComposite %f32a7 %float_n1 %float_n1 %float_n1 %float_n1 %float_n1 %float_n1 %float_n1\n"
2812                 "%main     = OpFunction %void None %voidf\n"
2813                 "%entry    = OpLabel\n"
2814                 "%idval    = OpLoad %uvec3 %id\n"
2815                 "%x        = OpCompositeExtract %u32 %idval 0\n"
2816                 "%inloc    = OpAccessChain %f32ptr %indata %zero %x\n"
2817                 "%inval    = OpLoad %f32 %inloc\n"
2818
2819                 "%comp     = OpFOrdGreaterThan %bool %inval %float_0\n"
2820                 "            OpSelectionMerge %cm None\n"
2821                 "            OpBranchConditional %comp %tb %fb\n"
2822                 "%tb       = OpLabel\n"
2823                 "            OpBranch %cm\n"
2824                 "%fb       = OpLabel\n"
2825                 "            OpBranch %cm\n"
2826                 "%cm       = OpLabel\n"
2827                 "%ares     = OpPhi %f32a7 %a1 %tb %a2 %fb\n"
2828                 "%res      = OpCompositeExtract %f32 %ares 5\n"
2829
2830                 "%outloc   = OpAccessChain %f32ptr %outdata %zero %x\n"
2831                 "            OpStore %outloc %res\n"
2832                 "            OpReturn\n"
2833
2834                 "            OpFunctionEnd\n";
2835         specArray.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
2836         specArray.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
2837         specArray.numWorkGroups = IVec3(numElements, 1, 1);
2838
2839         specStruct.assembly =
2840                 string(getComputeAsmShaderPreamble()) +
2841
2842                 "OpSource GLSL 430\n"
2843                 "OpName %main \"main\"\n"
2844                 "OpName %id \"gl_GlobalInvocationID\"\n"
2845
2846                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
2847
2848                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
2849
2850                 "%id = OpVariable %uvec3ptr Input\n"
2851                 "%v3f32      = OpTypeVector %f32 3\n"
2852                 "%zero       = OpConstant %i32 0\n"
2853                 "%float_0    = OpConstant %f32 0.0\n"
2854                 "%float_1    = OpConstant %f32 1.0\n"
2855                 "%float_n1   = OpConstant %f32 -1.0\n"
2856
2857                 "%v2f32      = OpTypeVector %f32 2\n"
2858                 "%Data2      = OpTypeStruct %f32 %v2f32\n"
2859                 "%Data       = OpTypeStruct %Data2 %f32\n"
2860
2861                 "%in1a       = OpConstantComposite %v2f32 %float_1 %float_1\n"
2862                 "%in1b       = OpConstantComposite %Data2 %float_1 %in1a\n"
2863                 "%s1         = OpConstantComposite %Data %in1b %float_1\n"
2864                 "%in2a       = OpConstantComposite %v2f32 %float_n1 %float_n1\n"
2865                 "%in2b       = OpConstantComposite %Data2 %float_n1 %in2a\n"
2866                 "%s2         = OpConstantComposite %Data %in2b %float_n1\n"
2867
2868                 "%main     = OpFunction %void None %voidf\n"
2869                 "%entry    = OpLabel\n"
2870                 "%idval    = OpLoad %uvec3 %id\n"
2871                 "%x        = OpCompositeExtract %u32 %idval 0\n"
2872                 "%inloc    = OpAccessChain %f32ptr %indata %zero %x\n"
2873                 "%inval    = OpLoad %f32 %inloc\n"
2874
2875                 "%comp     = OpFOrdGreaterThan %bool %inval %float_0\n"
2876                 "            OpSelectionMerge %cm None\n"
2877                 "            OpBranchConditional %comp %tb %fb\n"
2878                 "%tb       = OpLabel\n"
2879                 "            OpBranch %cm\n"
2880                 "%fb       = OpLabel\n"
2881                 "            OpBranch %cm\n"
2882                 "%cm       = OpLabel\n"
2883                 "%sres     = OpPhi %Data %s1 %tb %s2 %fb\n"
2884                 "%res      = OpCompositeExtract %f32 %sres 0 0\n"
2885
2886                 "%outloc   = OpAccessChain %f32ptr %outdata %zero %x\n"
2887                 "            OpStore %outloc %res\n"
2888                 "            OpReturn\n"
2889
2890                 "            OpFunctionEnd\n";
2891         specStruct.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
2892         specStruct.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
2893         specStruct.numWorkGroups = IVec3(numElements, 1, 1);
2894
2895         group->addChild(new SpvAsmComputeShaderCase(testCtx, "vartype_int", "OpPhi with int variables", specInt));
2896         group->addChild(new SpvAsmComputeShaderCase(testCtx, "vartype_float", "OpPhi with float variables", specFloat));
2897         group->addChild(new SpvAsmComputeShaderCase(testCtx, "vartype_vec3", "OpPhi with vec3 variables", specVec3));
2898         group->addChild(new SpvAsmComputeShaderCase(testCtx, "vartype_mat4", "OpPhi with mat4 variables", specMat4));
2899         group->addChild(new SpvAsmComputeShaderCase(testCtx, "vartype_array", "OpPhi with array variables", specArray));
2900         group->addChild(new SpvAsmComputeShaderCase(testCtx, "vartype_struct", "OpPhi with struct variables", specStruct));
2901 }
2902
2903 string generateConstantDefinitions (int count)
2904 {
2905         std::ostringstream      r;
2906         for (int i = 0; i < count; i++)
2907                 r << "%cf" << (i * 10 + 5) << " = OpConstant %f32 " <<(i * 10 + 5) << ".0\n";
2908         r << "\n";
2909         return r.str();
2910 }
2911
2912 string generateSwitchCases (int count)
2913 {
2914         std::ostringstream      r;
2915         for (int i = 0; i < count; i++)
2916                 r << " " << i << " %case" << i;
2917         r << "\n";
2918         return r.str();
2919 }
2920
2921 string generateSwitchTargets (int count)
2922 {
2923         std::ostringstream      r;
2924         for (int i = 0; i < count; i++)
2925                 r << "%case" << i << " = OpLabel\n            OpBranch %phi\n";
2926         r << "\n";
2927         return r.str();
2928 }
2929
2930 string generateOpPhiParams (int count)
2931 {
2932         std::ostringstream      r;
2933         for (int i = 0; i < count; i++)
2934                 r << " %cf" << (i * 10 + 5) << " %case" << i;
2935         r << "\n";
2936         return r.str();
2937 }
2938
2939 string generateIntWidth (int value)
2940 {
2941         std::ostringstream      r;
2942         r << value;
2943         return r.str();
2944 }
2945
2946 // Expand input string by injecting "ABC" between the input
2947 // string characters. The acc/add/treshold parameters are used
2948 // to skip some of the injections to make the result less
2949 // uniform (and a lot shorter).
2950 string expandOpPhiCase5 (const string& s, int &acc, int add, int treshold)
2951 {
2952         std::ostringstream      res;
2953         const char*                     p = s.c_str();
2954
2955         while (*p)
2956         {
2957                 res << *p;
2958                 acc += add;
2959                 if (acc > treshold)
2960                 {
2961                         acc -= treshold;
2962                         res << "ABC";
2963                 }
2964                 p++;
2965         }
2966         return res.str();
2967 }
2968
2969 // Calculate expected result based on the code string
2970 float calcOpPhiCase5 (float val, const string& s)
2971 {
2972         const char*             p               = s.c_str();
2973         float                   x[8];
2974         bool                    b[8];
2975         const float             tv[8]   = { 0.5f, 1.5f, 3.5f, 7.5f, 15.5f, 31.5f, 63.5f, 127.5f };
2976         const float             v               = deFloatAbs(val);
2977         float                   res             = 0;
2978         int                             depth   = -1;
2979         int                             skip    = 0;
2980
2981         for (int i = 7; i >= 0; --i)
2982                 x[i] = std::fmod((float)v, (float)(2 << i));
2983         for (int i = 7; i >= 0; --i)
2984                 b[i] = x[i] > tv[i];
2985
2986         while (*p)
2987         {
2988                 if (*p == 'A')
2989                 {
2990                         depth++;
2991                         if (skip == 0 && b[depth])
2992                         {
2993                                 res++;
2994                         }
2995                         else
2996                                 skip++;
2997                 }
2998                 if (*p == 'B')
2999                 {
3000                         if (skip)
3001                                 skip--;
3002                         if (b[depth] || skip)
3003                                 skip++;
3004                 }
3005                 if (*p == 'C')
3006                 {
3007                         depth--;
3008                         if (skip)
3009                                 skip--;
3010                 }
3011                 p++;
3012         }
3013         return res;
3014 }
3015
3016 // In the code string, the letters represent the following:
3017 //
3018 // A:
3019 //     if (certain bit is set)
3020 //     {
3021 //       result++;
3022 //
3023 // B:
3024 //     } else {
3025 //
3026 // C:
3027 //     }
3028 //
3029 // examples:
3030 // AABCBC leads to if(){r++;if(){r++;}else{}}else{}
3031 // ABABCC leads to if(){r++;}else{if(){r++;}else{}}
3032 // ABCABC leads to if(){r++;}else{}if(){r++;}else{}
3033 //
3034 // Code generation gets a bit complicated due to the else-branches,
3035 // which do not generate new values. Thus, the generator needs to
3036 // keep track of the previous variable change seen by the else
3037 // branch.
3038 string generateOpPhiCase5 (const string& s)
3039 {
3040         std::stack<int>                         idStack;
3041         std::stack<std::string>         value;
3042         std::stack<std::string>         valueLabel;
3043         std::stack<std::string>         mergeLeft;
3044         std::stack<std::string>         mergeRight;
3045         std::ostringstream                      res;
3046         const char*                                     p                       = s.c_str();
3047         int                                                     depth           = -1;
3048         int                                                     currId          = 0;
3049         int                                                     iter            = 0;
3050
3051         idStack.push(-1);
3052         value.push("%f32_0");
3053         valueLabel.push("%f32_0 %entry");
3054
3055         while (*p)
3056         {
3057                 if (*p == 'A')
3058                 {
3059                         depth++;
3060                         currId = iter;
3061                         idStack.push(currId);
3062                         res << "\tOpSelectionMerge %m" << currId << " None\n";
3063                         res << "\tOpBranchConditional %b" << depth << " %t" << currId << " %f" << currId << "\n";
3064                         res << "%t" << currId << " = OpLabel\n";
3065                         res << "%rt" << currId << " = OpFAdd %f32 " << value.top() << " %f32_1\n";
3066                         std::ostringstream tag;
3067                         tag << "%rt" << currId;
3068                         value.push(tag.str());
3069                         tag << " %t" << currId;
3070                         valueLabel.push(tag.str());
3071                 }
3072
3073                 if (*p == 'B')
3074                 {
3075                         mergeLeft.push(valueLabel.top());
3076                         value.pop();
3077                         valueLabel.pop();
3078                         res << "\tOpBranch %m" << currId << "\n";
3079                         res << "%f" << currId << " = OpLabel\n";
3080                         std::ostringstream tag;
3081                         tag << value.top() << " %f" << currId;
3082                         valueLabel.pop();
3083                         valueLabel.push(tag.str());
3084                 }
3085
3086                 if (*p == 'C')
3087                 {
3088                         mergeRight.push(valueLabel.top());
3089                         res << "\tOpBranch %m" << currId << "\n";
3090                         res << "%m" << currId << " = OpLabel\n";
3091                         if (*(p + 1) == 0)
3092                                 res << "%res"; // last result goes to %res
3093                         else
3094                                 res << "%rm" << currId;
3095                         res << " = OpPhi %f32  " << mergeLeft.top() << "  " << mergeRight.top() << "\n";
3096                         std::ostringstream tag;
3097                         tag << "%rm" << currId;
3098                         value.pop();
3099                         value.push(tag.str());
3100                         tag << " %m" << currId;
3101                         valueLabel.pop();
3102                         valueLabel.push(tag.str());
3103                         mergeLeft.pop();
3104                         mergeRight.pop();
3105                         depth--;
3106                         idStack.pop();
3107                         currId = idStack.top();
3108                 }
3109                 p++;
3110                 iter++;
3111         }
3112         return res.str();
3113 }
3114
3115 tcu::TestCaseGroup* createOpPhiGroup (tcu::TestContext& testCtx)
3116 {
3117         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opphi", "Test the OpPhi instruction"));
3118         ComputeShaderSpec                               spec1;
3119         ComputeShaderSpec                               spec2;
3120         ComputeShaderSpec                               spec3;
3121         ComputeShaderSpec                               spec4;
3122         ComputeShaderSpec                               spec5;
3123         de::Random                                              rnd                             (deStringHash(group->getName()));
3124         const int                                               numElements             = 100;
3125         vector<float>                                   inputFloats             (numElements, 0);
3126         vector<float>                                   outputFloats1   (numElements, 0);
3127         vector<float>                                   outputFloats2   (numElements, 0);
3128         vector<float>                                   outputFloats3   (numElements, 0);
3129         vector<float>                                   outputFloats4   (numElements, 0);
3130         vector<float>                                   outputFloats5   (numElements, 0);
3131         std::string                                             codestring              = "ABC";
3132         const int                                               test4Width              = 1024;
3133
3134         // Build case 5 code string. Each iteration makes the hierarchy more complicated.
3135         // 9 iterations with (7, 24) parameters makes the hierarchy 8 deep with about 1500 lines of
3136         // shader code.
3137         for (int i = 0, acc = 0; i < 9; i++)
3138                 codestring = expandOpPhiCase5(codestring, acc, 7, 24);
3139
3140         fillRandomScalars(rnd, -300.f, 300.f, &inputFloats[0], numElements);
3141
3142         // CPU might not use the same rounding mode as the GPU. Use whole numbers to avoid rounding differences.
3143         floorAll(inputFloats);
3144
3145         for (size_t ndx = 0; ndx < numElements; ++ndx)
3146         {
3147                 switch (ndx % 3)
3148                 {
3149                         case 0:         outputFloats1[ndx] = inputFloats[ndx] + 5.5f;   break;
3150                         case 1:         outputFloats1[ndx] = inputFloats[ndx] + 20.5f;  break;
3151                         case 2:         outputFloats1[ndx] = inputFloats[ndx] + 1.75f;  break;
3152                         default:        break;
3153                 }
3154                 outputFloats2[ndx] = inputFloats[ndx] + 6.5f * 3;
3155                 outputFloats3[ndx] = 8.5f - inputFloats[ndx];
3156
3157                 int index4 = (int)deFloor(deAbs((float)ndx * inputFloats[ndx]));
3158                 outputFloats4[ndx] = (float)(index4 % test4Width) * 10.0f + 5.0f;
3159
3160                 outputFloats5[ndx] = calcOpPhiCase5(inputFloats[ndx], codestring);
3161         }
3162
3163         spec1.assembly =
3164                 string(getComputeAsmShaderPreamble()) +
3165
3166                 "OpSource GLSL 430\n"
3167                 "OpName %main \"main\"\n"
3168                 "OpName %id \"gl_GlobalInvocationID\"\n"
3169
3170                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
3171
3172                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
3173
3174                 "%id = OpVariable %uvec3ptr Input\n"
3175                 "%zero       = OpConstant %i32 0\n"
3176                 "%three      = OpConstant %u32 3\n"
3177                 "%constf5p5  = OpConstant %f32 5.5\n"
3178                 "%constf20p5 = OpConstant %f32 20.5\n"
3179                 "%constf1p75 = OpConstant %f32 1.75\n"
3180                 "%constf8p5  = OpConstant %f32 8.5\n"
3181                 "%constf6p5  = OpConstant %f32 6.5\n"
3182
3183                 "%main     = OpFunction %void None %voidf\n"
3184                 "%entry    = OpLabel\n"
3185                 "%idval    = OpLoad %uvec3 %id\n"
3186                 "%x        = OpCompositeExtract %u32 %idval 0\n"
3187                 "%selector = OpUMod %u32 %x %three\n"
3188                 "            OpSelectionMerge %phi None\n"
3189                 "            OpSwitch %selector %default 0 %case0 1 %case1 2 %case2\n"
3190
3191                 // Case 1 before OpPhi.
3192                 "%case1    = OpLabel\n"
3193                 "            OpBranch %phi\n"
3194
3195                 "%default  = OpLabel\n"
3196                 "            OpUnreachable\n"
3197
3198                 "%phi      = OpLabel\n"
3199                 "%operand  = OpPhi %f32   %constf1p75 %case2   %constf20p5 %case1   %constf5p5 %case0\n" // not in the order of blocks
3200                 "%inloc    = OpAccessChain %f32ptr %indata %zero %x\n"
3201                 "%inval    = OpLoad %f32 %inloc\n"
3202                 "%add      = OpFAdd %f32 %inval %operand\n"
3203                 "%outloc   = OpAccessChain %f32ptr %outdata %zero %x\n"
3204                 "            OpStore %outloc %add\n"
3205                 "            OpReturn\n"
3206
3207                 // Case 0 after OpPhi.
3208                 "%case0    = OpLabel\n"
3209                 "            OpBranch %phi\n"
3210
3211
3212                 // Case 2 after OpPhi.
3213                 "%case2    = OpLabel\n"
3214                 "            OpBranch %phi\n"
3215
3216                 "            OpFunctionEnd\n";
3217         spec1.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
3218         spec1.outputs.push_back(BufferSp(new Float32Buffer(outputFloats1)));
3219         spec1.numWorkGroups = IVec3(numElements, 1, 1);
3220
3221         group->addChild(new SpvAsmComputeShaderCase(testCtx, "block", "out-of-order and unreachable blocks for OpPhi", spec1));
3222
3223         spec2.assembly =
3224                 string(getComputeAsmShaderPreamble()) +
3225
3226                 "OpName %main \"main\"\n"
3227                 "OpName %id \"gl_GlobalInvocationID\"\n"
3228
3229                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
3230
3231                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
3232
3233                 "%id         = OpVariable %uvec3ptr Input\n"
3234                 "%zero       = OpConstant %i32 0\n"
3235                 "%one        = OpConstant %i32 1\n"
3236                 "%three      = OpConstant %i32 3\n"
3237                 "%constf6p5  = OpConstant %f32 6.5\n"
3238
3239                 "%main       = OpFunction %void None %voidf\n"
3240                 "%entry      = OpLabel\n"
3241                 "%idval      = OpLoad %uvec3 %id\n"
3242                 "%x          = OpCompositeExtract %u32 %idval 0\n"
3243                 "%inloc      = OpAccessChain %f32ptr %indata %zero %x\n"
3244                 "%outloc     = OpAccessChain %f32ptr %outdata %zero %x\n"
3245                 "%inval      = OpLoad %f32 %inloc\n"
3246                 "              OpBranch %phi\n"
3247
3248                 "%phi        = OpLabel\n"
3249                 "%step       = OpPhi %i32 %zero  %entry %step_next  %phi\n"
3250                 "%accum      = OpPhi %f32 %inval %entry %accum_next %phi\n"
3251                 "%step_next  = OpIAdd %i32 %step %one\n"
3252                 "%accum_next = OpFAdd %f32 %accum %constf6p5\n"
3253                 "%still_loop = OpSLessThan %bool %step %three\n"
3254                 "              OpLoopMerge %exit %phi None\n"
3255                 "              OpBranchConditional %still_loop %phi %exit\n"
3256
3257                 "%exit       = OpLabel\n"
3258                 "              OpStore %outloc %accum\n"
3259                 "              OpReturn\n"
3260                 "              OpFunctionEnd\n";
3261         spec2.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
3262         spec2.outputs.push_back(BufferSp(new Float32Buffer(outputFloats2)));
3263         spec2.numWorkGroups = IVec3(numElements, 1, 1);
3264
3265         group->addChild(new SpvAsmComputeShaderCase(testCtx, "induction", "The usual way induction variables are handled in LLVM IR", spec2));
3266
3267         spec3.assembly =
3268                 string(getComputeAsmShaderPreamble()) +
3269
3270                 "OpName %main \"main\"\n"
3271                 "OpName %id \"gl_GlobalInvocationID\"\n"
3272
3273                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
3274
3275                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
3276
3277                 "%f32ptr_f   = OpTypePointer Function %f32\n"
3278                 "%id         = OpVariable %uvec3ptr Input\n"
3279                 "%true       = OpConstantTrue %bool\n"
3280                 "%false      = OpConstantFalse %bool\n"
3281                 "%zero       = OpConstant %i32 0\n"
3282                 "%constf8p5  = OpConstant %f32 8.5\n"
3283
3284                 "%main       = OpFunction %void None %voidf\n"
3285                 "%entry      = OpLabel\n"
3286                 "%b          = OpVariable %f32ptr_f Function %constf8p5\n"
3287                 "%idval      = OpLoad %uvec3 %id\n"
3288                 "%x          = OpCompositeExtract %u32 %idval 0\n"
3289                 "%inloc      = OpAccessChain %f32ptr %indata %zero %x\n"
3290                 "%outloc     = OpAccessChain %f32ptr %outdata %zero %x\n"
3291                 "%a_init     = OpLoad %f32 %inloc\n"
3292                 "%b_init     = OpLoad %f32 %b\n"
3293                 "              OpBranch %phi\n"
3294
3295                 "%phi        = OpLabel\n"
3296                 "%still_loop = OpPhi %bool %true   %entry %false  %phi\n"
3297                 "%a_next     = OpPhi %f32  %a_init %entry %b_next %phi\n"
3298                 "%b_next     = OpPhi %f32  %b_init %entry %a_next %phi\n"
3299                 "              OpLoopMerge %exit %phi None\n"
3300                 "              OpBranchConditional %still_loop %phi %exit\n"
3301
3302                 "%exit       = OpLabel\n"
3303                 "%sub        = OpFSub %f32 %a_next %b_next\n"
3304                 "              OpStore %outloc %sub\n"
3305                 "              OpReturn\n"
3306                 "              OpFunctionEnd\n";
3307         spec3.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
3308         spec3.outputs.push_back(BufferSp(new Float32Buffer(outputFloats3)));
3309         spec3.numWorkGroups = IVec3(numElements, 1, 1);
3310
3311         group->addChild(new SpvAsmComputeShaderCase(testCtx, "swap", "Swap the values of two variables using OpPhi", spec3));
3312
3313         spec4.assembly =
3314                 "OpCapability Shader\n"
3315                 "%ext = OpExtInstImport \"GLSL.std.450\"\n"
3316                 "OpMemoryModel Logical GLSL450\n"
3317                 "OpEntryPoint GLCompute %main \"main\" %id\n"
3318                 "OpExecutionMode %main LocalSize 1 1 1\n"
3319
3320                 "OpSource GLSL 430\n"
3321                 "OpName %main \"main\"\n"
3322                 "OpName %id \"gl_GlobalInvocationID\"\n"
3323
3324                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
3325
3326                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
3327
3328                 "%id       = OpVariable %uvec3ptr Input\n"
3329                 "%zero     = OpConstant %i32 0\n"
3330                 "%cimod    = OpConstant %u32 " + generateIntWidth(test4Width) + "\n"
3331
3332                 + generateConstantDefinitions(test4Width) +
3333
3334                 "%main     = OpFunction %void None %voidf\n"
3335                 "%entry    = OpLabel\n"
3336                 "%idval    = OpLoad %uvec3 %id\n"
3337                 "%x        = OpCompositeExtract %u32 %idval 0\n"
3338                 "%inloc    = OpAccessChain %f32ptr %indata %zero %x\n"
3339                 "%inval    = OpLoad %f32 %inloc\n"
3340                 "%xf       = OpConvertUToF %f32 %x\n"
3341                 "%xm       = OpFMul %f32 %xf %inval\n"
3342                 "%xa       = OpExtInst %f32 %ext FAbs %xm\n"
3343                 "%xi       = OpConvertFToU %u32 %xa\n"
3344                 "%selector = OpUMod %u32 %xi %cimod\n"
3345                 "            OpSelectionMerge %phi None\n"
3346                 "            OpSwitch %selector %default "
3347
3348                 + generateSwitchCases(test4Width) +
3349
3350                 "%default  = OpLabel\n"
3351                 "            OpUnreachable\n"
3352
3353                 + generateSwitchTargets(test4Width) +
3354
3355                 "%phi      = OpLabel\n"
3356                 "%result   = OpPhi %f32"
3357
3358                 + generateOpPhiParams(test4Width) +
3359
3360                 "%outloc   = OpAccessChain %f32ptr %outdata %zero %x\n"
3361                 "            OpStore %outloc %result\n"
3362                 "            OpReturn\n"
3363
3364                 "            OpFunctionEnd\n";
3365         spec4.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
3366         spec4.outputs.push_back(BufferSp(new Float32Buffer(outputFloats4)));
3367         spec4.numWorkGroups = IVec3(numElements, 1, 1);
3368
3369         group->addChild(new SpvAsmComputeShaderCase(testCtx, "wide", "OpPhi with a lot of parameters", spec4));
3370
3371         spec5.assembly =
3372                 "OpCapability Shader\n"
3373                 "%ext      = OpExtInstImport \"GLSL.std.450\"\n"
3374                 "OpMemoryModel Logical GLSL450\n"
3375                 "OpEntryPoint GLCompute %main \"main\" %id\n"
3376                 "OpExecutionMode %main LocalSize 1 1 1\n"
3377                 "%code     = OpString \"" + codestring + "\"\n"
3378
3379                 "OpSource GLSL 430\n"
3380                 "OpName %main \"main\"\n"
3381                 "OpName %id \"gl_GlobalInvocationID\"\n"
3382
3383                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
3384
3385                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
3386
3387                 "%id       = OpVariable %uvec3ptr Input\n"
3388                 "%zero     = OpConstant %i32 0\n"
3389                 "%f32_0    = OpConstant %f32 0.0\n"
3390                 "%f32_0_5  = OpConstant %f32 0.5\n"
3391                 "%f32_1    = OpConstant %f32 1.0\n"
3392                 "%f32_1_5  = OpConstant %f32 1.5\n"
3393                 "%f32_2    = OpConstant %f32 2.0\n"
3394                 "%f32_3_5  = OpConstant %f32 3.5\n"
3395                 "%f32_4    = OpConstant %f32 4.0\n"
3396                 "%f32_7_5  = OpConstant %f32 7.5\n"
3397                 "%f32_8    = OpConstant %f32 8.0\n"
3398                 "%f32_15_5 = OpConstant %f32 15.5\n"
3399                 "%f32_16   = OpConstant %f32 16.0\n"
3400                 "%f32_31_5 = OpConstant %f32 31.5\n"
3401                 "%f32_32   = OpConstant %f32 32.0\n"
3402                 "%f32_63_5 = OpConstant %f32 63.5\n"
3403                 "%f32_64   = OpConstant %f32 64.0\n"
3404                 "%f32_127_5 = OpConstant %f32 127.5\n"
3405                 "%f32_128  = OpConstant %f32 128.0\n"
3406                 "%f32_256  = OpConstant %f32 256.0\n"
3407
3408                 "%main     = OpFunction %void None %voidf\n"
3409                 "%entry    = OpLabel\n"
3410                 "%idval    = OpLoad %uvec3 %id\n"
3411                 "%x        = OpCompositeExtract %u32 %idval 0\n"
3412                 "%inloc    = OpAccessChain %f32ptr %indata %zero %x\n"
3413                 "%inval    = OpLoad %f32 %inloc\n"
3414
3415                 "%xabs     = OpExtInst %f32 %ext FAbs %inval\n"
3416                 "%x8       = OpFMod %f32 %xabs %f32_256\n"
3417                 "%x7       = OpFMod %f32 %xabs %f32_128\n"
3418                 "%x6       = OpFMod %f32 %xabs %f32_64\n"
3419                 "%x5       = OpFMod %f32 %xabs %f32_32\n"
3420                 "%x4       = OpFMod %f32 %xabs %f32_16\n"
3421                 "%x3       = OpFMod %f32 %xabs %f32_8\n"
3422                 "%x2       = OpFMod %f32 %xabs %f32_4\n"
3423                 "%x1       = OpFMod %f32 %xabs %f32_2\n"
3424
3425                 "%b7       = OpFOrdGreaterThanEqual %bool %x8 %f32_127_5\n"
3426                 "%b6       = OpFOrdGreaterThanEqual %bool %x7 %f32_63_5\n"
3427                 "%b5       = OpFOrdGreaterThanEqual %bool %x6 %f32_31_5\n"
3428                 "%b4       = OpFOrdGreaterThanEqual %bool %x5 %f32_15_5\n"
3429                 "%b3       = OpFOrdGreaterThanEqual %bool %x4 %f32_7_5\n"
3430                 "%b2       = OpFOrdGreaterThanEqual %bool %x3 %f32_3_5\n"
3431                 "%b1       = OpFOrdGreaterThanEqual %bool %x2 %f32_1_5\n"
3432                 "%b0       = OpFOrdGreaterThanEqual %bool %x1 %f32_0_5\n"
3433
3434                 + generateOpPhiCase5(codestring) +
3435
3436                 "%outloc   = OpAccessChain %f32ptr %outdata %zero %x\n"
3437                 "            OpStore %outloc %res\n"
3438                 "            OpReturn\n"
3439
3440                 "            OpFunctionEnd\n";
3441         spec5.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
3442         spec5.outputs.push_back(BufferSp(new Float32Buffer(outputFloats5)));
3443         spec5.numWorkGroups = IVec3(numElements, 1, 1);
3444
3445         group->addChild(new SpvAsmComputeShaderCase(testCtx, "nested", "Stress OpPhi with a lot of nesting", spec5));
3446
3447         createOpPhiVartypeTests(group, testCtx);
3448
3449         return group.release();
3450 }
3451
3452 // Assembly code used for testing block order is based on GLSL source code:
3453 //
3454 // #version 430
3455 //
3456 // layout(std140, set = 0, binding = 0) readonly buffer Input {
3457 //   float elements[];
3458 // } input_data;
3459 // layout(std140, set = 0, binding = 1) writeonly buffer Output {
3460 //   float elements[];
3461 // } output_data;
3462 //
3463 // void main() {
3464 //   uint x = gl_GlobalInvocationID.x;
3465 //   output_data.elements[x] = input_data.elements[x];
3466 //   if (x > uint(50)) {
3467 //     switch (x % uint(3)) {
3468 //       case 0: output_data.elements[x] += 1.5f; break;
3469 //       case 1: output_data.elements[x] += 42.f; break;
3470 //       case 2: output_data.elements[x] -= 27.f; break;
3471 //       default: break;
3472 //     }
3473 //   } else {
3474 //     output_data.elements[x] = -input_data.elements[x];
3475 //   }
3476 // }
3477 tcu::TestCaseGroup* createBlockOrderGroup (tcu::TestContext& testCtx)
3478 {
3479         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "block_order", "Test block orders"));
3480         ComputeShaderSpec                               spec;
3481         de::Random                                              rnd                             (deStringHash(group->getName()));
3482         const int                                               numElements             = 100;
3483         vector<float>                                   inputFloats             (numElements, 0);
3484         vector<float>                                   outputFloats    (numElements, 0);
3485
3486         fillRandomScalars(rnd, -100.f, 100.f, &inputFloats[0], numElements);
3487
3488         // CPU might not use the same rounding mode as the GPU. Use whole numbers to avoid rounding differences.
3489         floorAll(inputFloats);
3490
3491         for (size_t ndx = 0; ndx <= 50; ++ndx)
3492                 outputFloats[ndx] = -inputFloats[ndx];
3493
3494         for (size_t ndx = 51; ndx < numElements; ++ndx)
3495         {
3496                 switch (ndx % 3)
3497                 {
3498                         case 0:         outputFloats[ndx] = inputFloats[ndx] + 1.5f; break;
3499                         case 1:         outputFloats[ndx] = inputFloats[ndx] + 42.f; break;
3500                         case 2:         outputFloats[ndx] = inputFloats[ndx] - 27.f; break;
3501                         default:        break;
3502                 }
3503         }
3504
3505         spec.assembly =
3506                 string(getComputeAsmShaderPreamble()) +
3507
3508                 "OpSource GLSL 430\n"
3509                 "OpName %main \"main\"\n"
3510                 "OpName %id \"gl_GlobalInvocationID\"\n"
3511
3512                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
3513
3514                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
3515
3516                 "%u32ptr       = OpTypePointer Function %u32\n"
3517                 "%u32ptr_input = OpTypePointer Input %u32\n"
3518
3519                 + string(getComputeAsmInputOutputBuffer()) +
3520
3521                 "%id        = OpVariable %uvec3ptr Input\n"
3522                 "%zero      = OpConstant %i32 0\n"
3523                 "%const3    = OpConstant %u32 3\n"
3524                 "%const50   = OpConstant %u32 50\n"
3525                 "%constf1p5 = OpConstant %f32 1.5\n"
3526                 "%constf27  = OpConstant %f32 27.0\n"
3527                 "%constf42  = OpConstant %f32 42.0\n"
3528
3529                 "%main = OpFunction %void None %voidf\n"
3530
3531                 // entry block.
3532                 "%entry    = OpLabel\n"
3533
3534                 // Create a temporary variable to hold the value of gl_GlobalInvocationID.x.
3535                 "%xvar     = OpVariable %u32ptr Function\n"
3536                 "%xptr     = OpAccessChain %u32ptr_input %id %zero\n"
3537                 "%x        = OpLoad %u32 %xptr\n"
3538                 "            OpStore %xvar %x\n"
3539
3540                 "%cmp      = OpUGreaterThan %bool %x %const50\n"
3541                 "            OpSelectionMerge %if_merge None\n"
3542                 "            OpBranchConditional %cmp %if_true %if_false\n"
3543
3544                 // False branch for if-statement: placed in the middle of switch cases and before true branch.
3545                 "%if_false = OpLabel\n"
3546                 "%x_f      = OpLoad %u32 %xvar\n"
3547                 "%inloc_f  = OpAccessChain %f32ptr %indata %zero %x_f\n"
3548                 "%inval_f  = OpLoad %f32 %inloc_f\n"
3549                 "%negate   = OpFNegate %f32 %inval_f\n"
3550                 "%outloc_f = OpAccessChain %f32ptr %outdata %zero %x_f\n"
3551                 "            OpStore %outloc_f %negate\n"
3552                 "            OpBranch %if_merge\n"
3553
3554                 // Merge block for if-statement: placed in the middle of true and false branch.
3555                 "%if_merge = OpLabel\n"
3556                 "            OpReturn\n"
3557
3558                 // True branch for if-statement: placed in the middle of swtich cases and after the false branch.
3559                 "%if_true  = OpLabel\n"
3560                 "%xval_t   = OpLoad %u32 %xvar\n"
3561                 "%mod      = OpUMod %u32 %xval_t %const3\n"
3562                 "            OpSelectionMerge %switch_merge None\n"
3563                 "            OpSwitch %mod %default 0 %case0 1 %case1 2 %case2\n"
3564
3565                 // Merge block for switch-statement: placed before the case
3566                 // bodies.  But it must follow OpSwitch which dominates it.
3567                 "%switch_merge = OpLabel\n"
3568                 "                OpBranch %if_merge\n"
3569
3570                 // Case 1 for switch-statement: placed before case 0.
3571                 // It must follow the OpSwitch that dominates it.
3572                 "%case1    = OpLabel\n"
3573                 "%x_1      = OpLoad %u32 %xvar\n"
3574                 "%inloc_1  = OpAccessChain %f32ptr %indata %zero %x_1\n"
3575                 "%inval_1  = OpLoad %f32 %inloc_1\n"
3576                 "%addf42   = OpFAdd %f32 %inval_1 %constf42\n"
3577                 "%outloc_1 = OpAccessChain %f32ptr %outdata %zero %x_1\n"
3578                 "            OpStore %outloc_1 %addf42\n"
3579                 "            OpBranch %switch_merge\n"
3580
3581                 // Case 2 for switch-statement.
3582                 "%case2    = OpLabel\n"
3583                 "%x_2      = OpLoad %u32 %xvar\n"
3584                 "%inloc_2  = OpAccessChain %f32ptr %indata %zero %x_2\n"
3585                 "%inval_2  = OpLoad %f32 %inloc_2\n"
3586                 "%subf27   = OpFSub %f32 %inval_2 %constf27\n"
3587                 "%outloc_2 = OpAccessChain %f32ptr %outdata %zero %x_2\n"
3588                 "            OpStore %outloc_2 %subf27\n"
3589                 "            OpBranch %switch_merge\n"
3590
3591                 // Default case for switch-statement: placed in the middle of normal cases.
3592                 "%default = OpLabel\n"
3593                 "           OpBranch %switch_merge\n"
3594
3595                 // Case 0 for switch-statement: out of order.
3596                 "%case0    = OpLabel\n"
3597                 "%x_0      = OpLoad %u32 %xvar\n"
3598                 "%inloc_0  = OpAccessChain %f32ptr %indata %zero %x_0\n"
3599                 "%inval_0  = OpLoad %f32 %inloc_0\n"
3600                 "%addf1p5  = OpFAdd %f32 %inval_0 %constf1p5\n"
3601                 "%outloc_0 = OpAccessChain %f32ptr %outdata %zero %x_0\n"
3602                 "            OpStore %outloc_0 %addf1p5\n"
3603                 "            OpBranch %switch_merge\n"
3604
3605                 "            OpFunctionEnd\n";
3606         spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
3607         spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
3608         spec.numWorkGroups = IVec3(numElements, 1, 1);
3609
3610         group->addChild(new SpvAsmComputeShaderCase(testCtx, "all", "various out-of-order blocks", spec));
3611
3612         return group.release();
3613 }
3614
3615 tcu::TestCaseGroup* createMultipleShaderGroup (tcu::TestContext& testCtx)
3616 {
3617         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "multiple_shaders", "Test multiple shaders in the same module"));
3618         ComputeShaderSpec                               spec1;
3619         ComputeShaderSpec                               spec2;
3620         de::Random                                              rnd                             (deStringHash(group->getName()));
3621         const int                                               numElements             = 100;
3622         vector<float>                                   inputFloats             (numElements, 0);
3623         vector<float>                                   outputFloats1   (numElements, 0);
3624         vector<float>                                   outputFloats2   (numElements, 0);
3625         fillRandomScalars(rnd, -500.f, 500.f, &inputFloats[0], numElements);
3626
3627         for (size_t ndx = 0; ndx < numElements; ++ndx)
3628         {
3629                 outputFloats1[ndx] = inputFloats[ndx] + inputFloats[ndx];
3630                 outputFloats2[ndx] = -inputFloats[ndx];
3631         }
3632
3633         const string assembly(
3634                 "OpCapability Shader\n"
3635                 "OpCapability ClipDistance\n"
3636                 "OpMemoryModel Logical GLSL450\n"
3637                 "OpEntryPoint GLCompute %comp_main1 \"entrypoint1\" %id\n"
3638                 "OpEntryPoint GLCompute %comp_main2 \"entrypoint2\" %id\n"
3639                 // A module cannot have two OpEntryPoint instructions with the same Execution Model and the same Name string.
3640                 "OpEntryPoint Vertex    %vert_main  \"entrypoint2\" %vert_builtins %vertexIndex %instanceIndex\n"
3641                 "OpExecutionMode %comp_main1 LocalSize 1 1 1\n"
3642                 "OpExecutionMode %comp_main2 LocalSize 1 1 1\n"
3643
3644                 "OpName %comp_main1              \"entrypoint1\"\n"
3645                 "OpName %comp_main2              \"entrypoint2\"\n"
3646                 "OpName %vert_main               \"entrypoint2\"\n"
3647                 "OpName %id                      \"gl_GlobalInvocationID\"\n"
3648                 "OpName %vert_builtin_st         \"gl_PerVertex\"\n"
3649                 "OpName %vertexIndex             \"gl_VertexIndex\"\n"
3650                 "OpName %instanceIndex           \"gl_InstanceIndex\"\n"
3651                 "OpMemberName %vert_builtin_st 0 \"gl_Position\"\n"
3652                 "OpMemberName %vert_builtin_st 1 \"gl_PointSize\"\n"
3653                 "OpMemberName %vert_builtin_st 2 \"gl_ClipDistance\"\n"
3654
3655                 "OpDecorate %id                      BuiltIn GlobalInvocationId\n"
3656                 "OpDecorate %vertexIndex             BuiltIn VertexIndex\n"
3657                 "OpDecorate %instanceIndex           BuiltIn InstanceIndex\n"
3658                 "OpDecorate %vert_builtin_st         Block\n"
3659                 "OpMemberDecorate %vert_builtin_st 0 BuiltIn Position\n"
3660                 "OpMemberDecorate %vert_builtin_st 1 BuiltIn PointSize\n"
3661                 "OpMemberDecorate %vert_builtin_st 2 BuiltIn ClipDistance\n"
3662
3663                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
3664
3665                 "%zero       = OpConstant %i32 0\n"
3666                 "%one        = OpConstant %u32 1\n"
3667                 "%c_f32_1    = OpConstant %f32 1\n"
3668
3669                 "%i32inputptr         = OpTypePointer Input %i32\n"
3670                 "%vec4                = OpTypeVector %f32 4\n"
3671                 "%vec4ptr             = OpTypePointer Output %vec4\n"
3672                 "%f32arr1             = OpTypeArray %f32 %one\n"
3673                 "%vert_builtin_st     = OpTypeStruct %vec4 %f32 %f32arr1\n"
3674                 "%vert_builtin_st_ptr = OpTypePointer Output %vert_builtin_st\n"
3675                 "%vert_builtins       = OpVariable %vert_builtin_st_ptr Output\n"
3676
3677                 "%id         = OpVariable %uvec3ptr Input\n"
3678                 "%vertexIndex = OpVariable %i32inputptr Input\n"
3679                 "%instanceIndex = OpVariable %i32inputptr Input\n"
3680                 "%c_vec4_1   = OpConstantComposite %vec4 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_1\n"
3681
3682                 // gl_Position = vec4(1.);
3683                 "%vert_main  = OpFunction %void None %voidf\n"
3684                 "%vert_entry = OpLabel\n"
3685                 "%position   = OpAccessChain %vec4ptr %vert_builtins %zero\n"
3686                 "              OpStore %position %c_vec4_1\n"
3687                 "              OpReturn\n"
3688                 "              OpFunctionEnd\n"
3689
3690                 // Double inputs.
3691                 "%comp_main1  = OpFunction %void None %voidf\n"
3692                 "%comp1_entry = OpLabel\n"
3693                 "%idval1      = OpLoad %uvec3 %id\n"
3694                 "%x1          = OpCompositeExtract %u32 %idval1 0\n"
3695                 "%inloc1      = OpAccessChain %f32ptr %indata %zero %x1\n"
3696                 "%inval1      = OpLoad %f32 %inloc1\n"
3697                 "%add         = OpFAdd %f32 %inval1 %inval1\n"
3698                 "%outloc1     = OpAccessChain %f32ptr %outdata %zero %x1\n"
3699                 "               OpStore %outloc1 %add\n"
3700                 "               OpReturn\n"
3701                 "               OpFunctionEnd\n"
3702
3703                 // Negate inputs.
3704                 "%comp_main2  = OpFunction %void None %voidf\n"
3705                 "%comp2_entry = OpLabel\n"
3706                 "%idval2      = OpLoad %uvec3 %id\n"
3707                 "%x2          = OpCompositeExtract %u32 %idval2 0\n"
3708                 "%inloc2      = OpAccessChain %f32ptr %indata %zero %x2\n"
3709                 "%inval2      = OpLoad %f32 %inloc2\n"
3710                 "%neg         = OpFNegate %f32 %inval2\n"
3711                 "%outloc2     = OpAccessChain %f32ptr %outdata %zero %x2\n"
3712                 "               OpStore %outloc2 %neg\n"
3713                 "               OpReturn\n"
3714                 "               OpFunctionEnd\n");
3715
3716         spec1.assembly = assembly;
3717         spec1.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
3718         spec1.outputs.push_back(BufferSp(new Float32Buffer(outputFloats1)));
3719         spec1.numWorkGroups = IVec3(numElements, 1, 1);
3720         spec1.entryPoint = "entrypoint1";
3721
3722         spec2.assembly = assembly;
3723         spec2.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
3724         spec2.outputs.push_back(BufferSp(new Float32Buffer(outputFloats2)));
3725         spec2.numWorkGroups = IVec3(numElements, 1, 1);
3726         spec2.entryPoint = "entrypoint2";
3727
3728         group->addChild(new SpvAsmComputeShaderCase(testCtx, "shader1", "multiple shaders in the same module", spec1));
3729         group->addChild(new SpvAsmComputeShaderCase(testCtx, "shader2", "multiple shaders in the same module", spec2));
3730
3731         return group.release();
3732 }
3733
3734 inline std::string makeLongUTF8String (size_t num4ByteChars)
3735 {
3736         // An example of a longest valid UTF-8 character.  Be explicit about the
3737         // character type because Microsoft compilers can otherwise interpret the
3738         // character string as being over wide (16-bit) characters. Ideally, we
3739         // would just use a C++11 UTF-8 string literal, but we want to support older
3740         // Microsoft compilers.
3741         const std::basic_string<char> earthAfrica("\xF0\x9F\x8C\x8D");
3742         std::string longString;
3743         longString.reserve(num4ByteChars * 4);
3744         for (size_t count = 0; count < num4ByteChars; count++)
3745         {
3746                 longString += earthAfrica;
3747         }
3748         return longString;
3749 }
3750
3751 tcu::TestCaseGroup* createOpSourceGroup (tcu::TestContext& testCtx)
3752 {
3753         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opsource", "Tests the OpSource & OpSourceContinued instruction"));
3754         vector<CaseParameter>                   cases;
3755         de::Random                                              rnd                             (deStringHash(group->getName()));
3756         const int                                               numElements             = 100;
3757         vector<float>                                   positiveFloats  (numElements, 0);
3758         vector<float>                                   negativeFloats  (numElements, 0);
3759         const StringTemplate                    shaderTemplate  (
3760                 "OpCapability Shader\n"
3761                 "OpMemoryModel Logical GLSL450\n"
3762
3763                 "OpEntryPoint GLCompute %main \"main\" %id\n"
3764                 "OpExecutionMode %main LocalSize 1 1 1\n"
3765
3766                 "${SOURCE}\n"
3767
3768                 "OpName %main           \"main\"\n"
3769                 "OpName %id             \"gl_GlobalInvocationID\"\n"
3770
3771                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
3772
3773                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
3774
3775                 "%id        = OpVariable %uvec3ptr Input\n"
3776                 "%zero      = OpConstant %i32 0\n"
3777
3778                 "%main      = OpFunction %void None %voidf\n"
3779                 "%label     = OpLabel\n"
3780                 "%idval     = OpLoad %uvec3 %id\n"
3781                 "%x         = OpCompositeExtract %u32 %idval 0\n"
3782                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
3783                 "%inval     = OpLoad %f32 %inloc\n"
3784                 "%neg       = OpFNegate %f32 %inval\n"
3785                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
3786                 "             OpStore %outloc %neg\n"
3787                 "             OpReturn\n"
3788                 "             OpFunctionEnd\n");
3789
3790         cases.push_back(CaseParameter("unknown_source",                                                 "OpSource Unknown 0"));
3791         cases.push_back(CaseParameter("wrong_source",                                                   "OpSource OpenCL_C 210"));
3792         cases.push_back(CaseParameter("normal_filename",                                                "%fname = OpString \"filename\"\n"
3793                                                                                                                                                         "OpSource GLSL 430 %fname"));
3794         cases.push_back(CaseParameter("empty_filename",                                                 "%fname = OpString \"\"\n"
3795                                                                                                                                                         "OpSource GLSL 430 %fname"));
3796         cases.push_back(CaseParameter("normal_source_code",                                             "%fname = OpString \"filename\"\n"
3797                                                                                                                                                         "OpSource GLSL 430 %fname \"#version 430\nvoid main() {}\""));
3798         cases.push_back(CaseParameter("empty_source_code",                                              "%fname = OpString \"filename\"\n"
3799                                                                                                                                                         "OpSource GLSL 430 %fname \"\""));
3800         cases.push_back(CaseParameter("long_source_code",                                               "%fname = OpString \"filename\"\n"
3801                                                                                                                                                         "OpSource GLSL 430 %fname \"" + makeLongUTF8String(65530) + "ccc\"")); // word count: 65535
3802         cases.push_back(CaseParameter("utf8_source_code",                                               "%fname = OpString \"filename\"\n"
3803                                                                                                                                                         "OpSource GLSL 430 %fname \"\xE2\x98\x82\xE2\x98\x85\"")); // umbrella & black star symbol
3804         cases.push_back(CaseParameter("normal_sourcecontinued",                                 "%fname = OpString \"filename\"\n"
3805                                                                                                                                                         "OpSource GLSL 430 %fname \"#version 430\nvo\"\n"
3806                                                                                                                                                         "OpSourceContinued \"id main() {}\""));
3807         cases.push_back(CaseParameter("empty_sourcecontinued",                                  "%fname = OpString \"filename\"\n"
3808                                                                                                                                                         "OpSource GLSL 430 %fname \"#version 430\nvoid main() {}\"\n"
3809                                                                                                                                                         "OpSourceContinued \"\""));
3810         cases.push_back(CaseParameter("long_sourcecontinued",                                   "%fname = OpString \"filename\"\n"
3811                                                                                                                                                         "OpSource GLSL 430 %fname \"#version 430\nvoid main() {}\"\n"
3812                                                                                                                                                         "OpSourceContinued \"" + makeLongUTF8String(65533) + "ccc\"")); // word count: 65535
3813         cases.push_back(CaseParameter("utf8_sourcecontinued",                                   "%fname = OpString \"filename\"\n"
3814                                                                                                                                                         "OpSource GLSL 430 %fname \"#version 430\nvoid main() {}\"\n"
3815                                                                                                                                                         "OpSourceContinued \"\xE2\x98\x8E\xE2\x9A\x91\"")); // white telephone & black flag symbol
3816         cases.push_back(CaseParameter("multi_sourcecontinued",                                  "%fname = OpString \"filename\"\n"
3817                                                                                                                                                         "OpSource GLSL 430 %fname \"#version 430\n\"\n"
3818                                                                                                                                                         "OpSourceContinued \"void\"\n"
3819                                                                                                                                                         "OpSourceContinued \"main()\"\n"
3820                                                                                                                                                         "OpSourceContinued \"{}\""));
3821         cases.push_back(CaseParameter("empty_source_before_sourcecontinued",    "%fname = OpString \"filename\"\n"
3822                                                                                                                                                         "OpSource GLSL 430 %fname \"\"\n"
3823                                                                                                                                                         "OpSourceContinued \"#version 430\nvoid main() {}\""));
3824
3825         fillRandomScalars(rnd, 1.f, 100.f, &positiveFloats[0], numElements);
3826
3827         for (size_t ndx = 0; ndx < numElements; ++ndx)
3828                 negativeFloats[ndx] = -positiveFloats[ndx];
3829
3830         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
3831         {
3832                 map<string, string>             specializations;
3833                 ComputeShaderSpec               spec;
3834
3835                 specializations["SOURCE"] = cases[caseNdx].param;
3836                 spec.assembly = shaderTemplate.specialize(specializations);
3837                 spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
3838                 spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
3839                 spec.numWorkGroups = IVec3(numElements, 1, 1);
3840
3841                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
3842         }
3843
3844         return group.release();
3845 }
3846
3847 tcu::TestCaseGroup* createOpSourceExtensionGroup (tcu::TestContext& testCtx)
3848 {
3849         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opsourceextension", "Tests the OpSource instruction"));
3850         vector<CaseParameter>                   cases;
3851         de::Random                                              rnd                             (deStringHash(group->getName()));
3852         const int                                               numElements             = 100;
3853         vector<float>                                   inputFloats             (numElements, 0);
3854         vector<float>                                   outputFloats    (numElements, 0);
3855         const StringTemplate                    shaderTemplate  (
3856                 string(getComputeAsmShaderPreamble()) +
3857
3858                 "OpSourceExtension \"${EXTENSION}\"\n"
3859
3860                 "OpName %main           \"main\"\n"
3861                 "OpName %id             \"gl_GlobalInvocationID\"\n"
3862
3863                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
3864
3865                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
3866
3867                 "%id        = OpVariable %uvec3ptr Input\n"
3868                 "%zero      = OpConstant %i32 0\n"
3869
3870                 "%main      = OpFunction %void None %voidf\n"
3871                 "%label     = OpLabel\n"
3872                 "%idval     = OpLoad %uvec3 %id\n"
3873                 "%x         = OpCompositeExtract %u32 %idval 0\n"
3874                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
3875                 "%inval     = OpLoad %f32 %inloc\n"
3876                 "%neg       = OpFNegate %f32 %inval\n"
3877                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
3878                 "             OpStore %outloc %neg\n"
3879                 "             OpReturn\n"
3880                 "             OpFunctionEnd\n");
3881
3882         cases.push_back(CaseParameter("empty_extension",        ""));
3883         cases.push_back(CaseParameter("real_extension",         "GL_ARB_texture_rectangle"));
3884         cases.push_back(CaseParameter("fake_extension",         "GL_ARB_im_the_ultimate_extension"));
3885         cases.push_back(CaseParameter("utf8_extension",         "GL_ARB_\xE2\x98\x82\xE2\x98\x85"));
3886         cases.push_back(CaseParameter("long_extension",         makeLongUTF8String(65533) + "ccc")); // word count: 65535
3887
3888         fillRandomScalars(rnd, -200.f, 200.f, &inputFloats[0], numElements);
3889
3890         for (size_t ndx = 0; ndx < numElements; ++ndx)
3891                 outputFloats[ndx] = -inputFloats[ndx];
3892
3893         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
3894         {
3895                 map<string, string>             specializations;
3896                 ComputeShaderSpec               spec;
3897
3898                 specializations["EXTENSION"] = cases[caseNdx].param;
3899                 spec.assembly = shaderTemplate.specialize(specializations);
3900                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
3901                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
3902                 spec.numWorkGroups = IVec3(numElements, 1, 1);
3903
3904                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
3905         }
3906
3907         return group.release();
3908 }
3909
3910 // Checks that a compute shader can generate a constant null value of various types, without exercising a computation on it.
3911 tcu::TestCaseGroup* createOpConstantNullGroup (tcu::TestContext& testCtx)
3912 {
3913         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opconstantnull", "Tests the OpConstantNull instruction"));
3914         vector<CaseParameter>                   cases;
3915         de::Random                                              rnd                             (deStringHash(group->getName()));
3916         const int                                               numElements             = 100;
3917         vector<float>                                   positiveFloats  (numElements, 0);
3918         vector<float>                                   negativeFloats  (numElements, 0);
3919         const StringTemplate                    shaderTemplate  (
3920                 string(getComputeAsmShaderPreamble()) +
3921
3922                 "OpSource GLSL 430\n"
3923                 "OpName %main           \"main\"\n"
3924                 "OpName %id             \"gl_GlobalInvocationID\"\n"
3925
3926                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
3927
3928                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
3929                 "%uvec2     = OpTypeVector %u32 2\n"
3930                 "%bvec3     = OpTypeVector %bool 3\n"
3931                 "%fvec4     = OpTypeVector %f32 4\n"
3932                 "%fmat33    = OpTypeMatrix %fvec3 3\n"
3933                 "%const100  = OpConstant %u32 100\n"
3934                 "%uarr100   = OpTypeArray %i32 %const100\n"
3935                 "%struct    = OpTypeStruct %f32 %i32 %u32\n"
3936                 "%pointer   = OpTypePointer Function %i32\n"
3937                 + string(getComputeAsmInputOutputBuffer()) +
3938
3939                 "%null      = OpConstantNull ${TYPE}\n"
3940
3941                 "%id        = OpVariable %uvec3ptr Input\n"
3942                 "%zero      = OpConstant %i32 0\n"
3943
3944                 "%main      = OpFunction %void None %voidf\n"
3945                 "%label     = OpLabel\n"
3946                 "%idval     = OpLoad %uvec3 %id\n"
3947                 "%x         = OpCompositeExtract %u32 %idval 0\n"
3948                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
3949                 "%inval     = OpLoad %f32 %inloc\n"
3950                 "%neg       = OpFNegate %f32 %inval\n"
3951                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
3952                 "             OpStore %outloc %neg\n"
3953                 "             OpReturn\n"
3954                 "             OpFunctionEnd\n");
3955
3956         cases.push_back(CaseParameter("bool",                   "%bool"));
3957         cases.push_back(CaseParameter("sint32",                 "%i32"));
3958         cases.push_back(CaseParameter("uint32",                 "%u32"));
3959         cases.push_back(CaseParameter("float32",                "%f32"));
3960         cases.push_back(CaseParameter("vec4float32",    "%fvec4"));
3961         cases.push_back(CaseParameter("vec3bool",               "%bvec3"));
3962         cases.push_back(CaseParameter("vec2uint32",             "%uvec2"));
3963         cases.push_back(CaseParameter("matrix",                 "%fmat33"));
3964         cases.push_back(CaseParameter("array",                  "%uarr100"));
3965         cases.push_back(CaseParameter("struct",                 "%struct"));
3966         cases.push_back(CaseParameter("pointer",                "%pointer"));
3967
3968         fillRandomScalars(rnd, 1.f, 100.f, &positiveFloats[0], numElements);
3969
3970         for (size_t ndx = 0; ndx < numElements; ++ndx)
3971                 negativeFloats[ndx] = -positiveFloats[ndx];
3972
3973         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
3974         {
3975                 map<string, string>             specializations;
3976                 ComputeShaderSpec               spec;
3977
3978                 specializations["TYPE"] = cases[caseNdx].param;
3979                 spec.assembly = shaderTemplate.specialize(specializations);
3980                 spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
3981                 spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
3982                 spec.numWorkGroups = IVec3(numElements, 1, 1);
3983
3984                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
3985         }
3986
3987         return group.release();
3988 }
3989
3990 // Checks that a compute shader can generate a constant composite value of various types, without exercising a computation on it.
3991 tcu::TestCaseGroup* createOpConstantCompositeGroup (tcu::TestContext& testCtx)
3992 {
3993         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opconstantcomposite", "Tests the OpConstantComposite instruction"));
3994         vector<CaseParameter>                   cases;
3995         de::Random                                              rnd                             (deStringHash(group->getName()));
3996         const int                                               numElements             = 100;
3997         vector<float>                                   positiveFloats  (numElements, 0);
3998         vector<float>                                   negativeFloats  (numElements, 0);
3999         const StringTemplate                    shaderTemplate  (
4000                 string(getComputeAsmShaderPreamble()) +
4001
4002                 "OpSource GLSL 430\n"
4003                 "OpName %main           \"main\"\n"
4004                 "OpName %id             \"gl_GlobalInvocationID\"\n"
4005
4006                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
4007
4008                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
4009
4010                 "%id        = OpVariable %uvec3ptr Input\n"
4011                 "%zero      = OpConstant %i32 0\n"
4012
4013                 "${CONSTANT}\n"
4014
4015                 "%main      = OpFunction %void None %voidf\n"
4016                 "%label     = OpLabel\n"
4017                 "%idval     = OpLoad %uvec3 %id\n"
4018                 "%x         = OpCompositeExtract %u32 %idval 0\n"
4019                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
4020                 "%inval     = OpLoad %f32 %inloc\n"
4021                 "%neg       = OpFNegate %f32 %inval\n"
4022                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
4023                 "             OpStore %outloc %neg\n"
4024                 "             OpReturn\n"
4025                 "             OpFunctionEnd\n");
4026
4027         cases.push_back(CaseParameter("vector",                 "%five = OpConstant %u32 5\n"
4028                                                                                                         "%const = OpConstantComposite %uvec3 %five %zero %five"));
4029         cases.push_back(CaseParameter("matrix",                 "%m3fvec3 = OpTypeMatrix %fvec3 3\n"
4030                                                                                                         "%ten = OpConstant %f32 10.\n"
4031                                                                                                         "%fzero = OpConstant %f32 0.\n"
4032                                                                                                         "%vec = OpConstantComposite %fvec3 %ten %fzero %ten\n"
4033                                                                                                         "%mat = OpConstantComposite %m3fvec3 %vec %vec %vec"));
4034         cases.push_back(CaseParameter("struct",                 "%m2vec3 = OpTypeMatrix %fvec3 2\n"
4035                                                                                                         "%struct = OpTypeStruct %i32 %f32 %fvec3 %m2vec3\n"
4036                                                                                                         "%fzero = OpConstant %f32 0.\n"
4037                                                                                                         "%one = OpConstant %f32 1.\n"
4038                                                                                                         "%point5 = OpConstant %f32 0.5\n"
4039                                                                                                         "%vec = OpConstantComposite %fvec3 %one %one %fzero\n"
4040                                                                                                         "%mat = OpConstantComposite %m2vec3 %vec %vec\n"
4041                                                                                                         "%const = OpConstantComposite %struct %zero %point5 %vec %mat"));
4042         cases.push_back(CaseParameter("nested_struct",  "%st1 = OpTypeStruct %u32 %f32\n"
4043                                                                                                         "%st2 = OpTypeStruct %i32 %i32\n"
4044                                                                                                         "%struct = OpTypeStruct %st1 %st2\n"
4045                                                                                                         "%point5 = OpConstant %f32 0.5\n"
4046                                                                                                         "%one = OpConstant %u32 1\n"
4047                                                                                                         "%ten = OpConstant %i32 10\n"
4048                                                                                                         "%st1val = OpConstantComposite %st1 %one %point5\n"
4049                                                                                                         "%st2val = OpConstantComposite %st2 %ten %ten\n"
4050                                                                                                         "%const = OpConstantComposite %struct %st1val %st2val"));
4051
4052         fillRandomScalars(rnd, 1.f, 100.f, &positiveFloats[0], numElements);
4053
4054         for (size_t ndx = 0; ndx < numElements; ++ndx)
4055                 negativeFloats[ndx] = -positiveFloats[ndx];
4056
4057         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
4058         {
4059                 map<string, string>             specializations;
4060                 ComputeShaderSpec               spec;
4061
4062                 specializations["CONSTANT"] = cases[caseNdx].param;
4063                 spec.assembly = shaderTemplate.specialize(specializations);
4064                 spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
4065                 spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
4066                 spec.numWorkGroups = IVec3(numElements, 1, 1);
4067
4068                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
4069         }
4070
4071         return group.release();
4072 }
4073
4074 // Creates a floating point number with the given exponent, and significand
4075 // bits set. It can only create normalized numbers. Only the least significant
4076 // 24 bits of the significand will be examined. The final bit of the
4077 // significand will also be ignored. This allows alignment to be written
4078 // similarly to C99 hex-floats.
4079 // For example if you wanted to write 0x1.7f34p-12 you would call
4080 // constructNormalizedFloat(-12, 0x7f3400)
4081 float constructNormalizedFloat (deInt32 exponent, deUint32 significand)
4082 {
4083         float f = 1.0f;
4084
4085         for (deInt32 idx = 0; idx < 23; ++idx)
4086         {
4087                 f += ((significand & 0x800000) == 0) ? 0.f : std::ldexp(1.0f, -(idx + 1));
4088                 significand <<= 1;
4089         }
4090
4091         return std::ldexp(f, exponent);
4092 }
4093
4094 // Compare instruction for the OpQuantizeF16 compute exact case.
4095 // Returns true if the output is what is expected from the test case.
4096 bool compareOpQuantizeF16ComputeExactCase (const std::vector<BufferSp>&, const vector<AllocationSp>& outputAllocs, const std::vector<BufferSp>& expectedOutputs, TestLog&)
4097 {
4098         if (outputAllocs.size() != 1)
4099                 return false;
4100
4101         // Only size is needed because we cannot compare Nans.
4102         size_t byteSize = expectedOutputs[0]->getByteSize();
4103
4104         const float*    outputAsFloat   = static_cast<const float*>(outputAllocs[0]->getHostPtr());
4105
4106         if (byteSize != 4*sizeof(float)) {
4107                 return false;
4108         }
4109
4110         if (*outputAsFloat != constructNormalizedFloat(8, 0x304000) &&
4111                 *outputAsFloat != constructNormalizedFloat(8, 0x300000)) {
4112                 return false;
4113         }
4114         outputAsFloat++;
4115
4116         if (*outputAsFloat != -constructNormalizedFloat(-7, 0x600000) &&
4117                 *outputAsFloat != -constructNormalizedFloat(-7, 0x604000)) {
4118                 return false;
4119         }
4120         outputAsFloat++;
4121
4122         if (*outputAsFloat != constructNormalizedFloat(2, 0x01C000) &&
4123                 *outputAsFloat != constructNormalizedFloat(2, 0x020000)) {
4124                 return false;
4125         }
4126         outputAsFloat++;
4127
4128         if (*outputAsFloat != constructNormalizedFloat(1, 0xFFC000) &&
4129                 *outputAsFloat != constructNormalizedFloat(2, 0x000000)) {
4130                 return false;
4131         }
4132
4133         return true;
4134 }
4135
4136 // Checks that every output from a test-case is a float NaN.
4137 bool compareNan (const std::vector<BufferSp>&, const vector<AllocationSp>& outputAllocs, const std::vector<BufferSp>& expectedOutputs, TestLog&)
4138 {
4139         if (outputAllocs.size() != 1)
4140                 return false;
4141
4142         // Only size is needed because we cannot compare Nans.
4143         size_t byteSize = expectedOutputs[0]->getByteSize();
4144
4145         const float* const      output_as_float = static_cast<const float* const>(outputAllocs[0]->getHostPtr());
4146
4147         for (size_t idx = 0; idx < byteSize / sizeof(float); ++idx)
4148         {
4149                 if (!deFloatIsNaN(output_as_float[idx]))
4150                 {
4151                         return false;
4152                 }
4153         }
4154
4155         return true;
4156 }
4157
4158 // Checks that a compute shader can generate a constant composite value of various types, without exercising a computation on it.
4159 tcu::TestCaseGroup* createOpQuantizeToF16Group (tcu::TestContext& testCtx)
4160 {
4161         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opquantize", "Tests the OpQuantizeToF16 instruction"));
4162
4163         const std::string shader (
4164                 string(getComputeAsmShaderPreamble()) +
4165
4166                 "OpSource GLSL 430\n"
4167                 "OpName %main           \"main\"\n"
4168                 "OpName %id             \"gl_GlobalInvocationID\"\n"
4169
4170                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
4171
4172                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
4173
4174                 "%id        = OpVariable %uvec3ptr Input\n"
4175                 "%zero      = OpConstant %i32 0\n"
4176
4177                 "%main      = OpFunction %void None %voidf\n"
4178                 "%label     = OpLabel\n"
4179                 "%idval     = OpLoad %uvec3 %id\n"
4180                 "%x         = OpCompositeExtract %u32 %idval 0\n"
4181                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
4182                 "%inval     = OpLoad %f32 %inloc\n"
4183                 "%quant     = OpQuantizeToF16 %f32 %inval\n"
4184                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
4185                 "             OpStore %outloc %quant\n"
4186                 "             OpReturn\n"
4187                 "             OpFunctionEnd\n");
4188
4189         {
4190                 ComputeShaderSpec       spec;
4191                 const deUint32          numElements             = 100;
4192                 vector<float>           infinities;
4193                 vector<float>           results;
4194
4195                 infinities.reserve(numElements);
4196                 results.reserve(numElements);
4197
4198                 for (size_t idx = 0; idx < numElements; ++idx)
4199                 {
4200                         switch(idx % 4)
4201                         {
4202                                 case 0:
4203                                         infinities.push_back(std::numeric_limits<float>::infinity());
4204                                         results.push_back(std::numeric_limits<float>::infinity());
4205                                         break;
4206                                 case 1:
4207                                         infinities.push_back(-std::numeric_limits<float>::infinity());
4208                                         results.push_back(-std::numeric_limits<float>::infinity());
4209                                         break;
4210                                 case 2:
4211                                         infinities.push_back(std::ldexp(1.0f, 16));
4212                                         results.push_back(std::numeric_limits<float>::infinity());
4213                                         break;
4214                                 case 3:
4215                                         infinities.push_back(std::ldexp(-1.0f, 32));
4216                                         results.push_back(-std::numeric_limits<float>::infinity());
4217                                         break;
4218                         }
4219                 }
4220
4221                 spec.assembly = shader;
4222                 spec.inputs.push_back(BufferSp(new Float32Buffer(infinities)));
4223                 spec.outputs.push_back(BufferSp(new Float32Buffer(results)));
4224                 spec.numWorkGroups = IVec3(numElements, 1, 1);
4225
4226                 group->addChild(new SpvAsmComputeShaderCase(
4227                         testCtx, "infinities", "Check that infinities propagated and created", spec));
4228         }
4229
4230         {
4231                 ComputeShaderSpec       spec;
4232                 vector<float>           nans;
4233                 const deUint32          numElements             = 100;
4234
4235                 nans.reserve(numElements);
4236
4237                 for (size_t idx = 0; idx < numElements; ++idx)
4238                 {
4239                         if (idx % 2 == 0)
4240                         {
4241                                 nans.push_back(std::numeric_limits<float>::quiet_NaN());
4242                         }
4243                         else
4244                         {
4245                                 nans.push_back(-std::numeric_limits<float>::quiet_NaN());
4246                         }
4247                 }
4248
4249                 spec.assembly = shader;
4250                 spec.inputs.push_back(BufferSp(new Float32Buffer(nans)));
4251                 spec.outputs.push_back(BufferSp(new Float32Buffer(nans)));
4252                 spec.numWorkGroups = IVec3(numElements, 1, 1);
4253                 spec.verifyIO = &compareNan;
4254
4255                 group->addChild(new SpvAsmComputeShaderCase(
4256                         testCtx, "propagated_nans", "Check that nans are propagated", spec));
4257         }
4258
4259         {
4260                 ComputeShaderSpec       spec;
4261                 vector<float>           small;
4262                 vector<float>           zeros;
4263                 const deUint32          numElements             = 100;
4264
4265                 small.reserve(numElements);
4266                 zeros.reserve(numElements);
4267
4268                 for (size_t idx = 0; idx < numElements; ++idx)
4269                 {
4270                         switch(idx % 6)
4271                         {
4272                                 case 0:
4273                                         small.push_back(0.f);
4274                                         zeros.push_back(0.f);
4275                                         break;
4276                                 case 1:
4277                                         small.push_back(-0.f);
4278                                         zeros.push_back(-0.f);
4279                                         break;
4280                                 case 2:
4281                                         small.push_back(std::ldexp(1.0f, -16));
4282                                         zeros.push_back(0.f);
4283                                         break;
4284                                 case 3:
4285                                         small.push_back(std::ldexp(-1.0f, -32));
4286                                         zeros.push_back(-0.f);
4287                                         break;
4288                                 case 4:
4289                                         small.push_back(std::ldexp(1.0f, -127));
4290                                         zeros.push_back(0.f);
4291                                         break;
4292                                 case 5:
4293                                         small.push_back(-std::ldexp(1.0f, -128));
4294                                         zeros.push_back(-0.f);
4295                                         break;
4296                         }
4297                 }
4298
4299                 spec.assembly = shader;
4300                 spec.inputs.push_back(BufferSp(new Float32Buffer(small)));
4301                 spec.outputs.push_back(BufferSp(new Float32Buffer(zeros)));
4302                 spec.numWorkGroups = IVec3(numElements, 1, 1);
4303
4304                 group->addChild(new SpvAsmComputeShaderCase(
4305                         testCtx, "flush_to_zero", "Check that values are zeroed correctly", spec));
4306         }
4307
4308         {
4309                 ComputeShaderSpec       spec;
4310                 vector<float>           exact;
4311                 const deUint32          numElements             = 200;
4312
4313                 exact.reserve(numElements);
4314
4315                 for (size_t idx = 0; idx < numElements; ++idx)
4316                         exact.push_back(static_cast<float>(static_cast<int>(idx) - 100));
4317
4318                 spec.assembly = shader;
4319                 spec.inputs.push_back(BufferSp(new Float32Buffer(exact)));
4320                 spec.outputs.push_back(BufferSp(new Float32Buffer(exact)));
4321                 spec.numWorkGroups = IVec3(numElements, 1, 1);
4322
4323                 group->addChild(new SpvAsmComputeShaderCase(
4324                         testCtx, "exact", "Check that values exactly preserved where appropriate", spec));
4325         }
4326
4327         {
4328                 ComputeShaderSpec       spec;
4329                 vector<float>           inputs;
4330                 const deUint32          numElements             = 4;
4331
4332                 inputs.push_back(constructNormalizedFloat(8,    0x300300));
4333                 inputs.push_back(-constructNormalizedFloat(-7,  0x600800));
4334                 inputs.push_back(constructNormalizedFloat(2,    0x01E000));
4335                 inputs.push_back(constructNormalizedFloat(1,    0xFFE000));
4336
4337                 spec.assembly = shader;
4338                 spec.verifyIO = &compareOpQuantizeF16ComputeExactCase;
4339                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputs)));
4340                 spec.outputs.push_back(BufferSp(new Float32Buffer(inputs)));
4341                 spec.numWorkGroups = IVec3(numElements, 1, 1);
4342
4343                 group->addChild(new SpvAsmComputeShaderCase(
4344                         testCtx, "rounded", "Check that are rounded when needed", spec));
4345         }
4346
4347         return group.release();
4348 }
4349
4350 tcu::TestCaseGroup* createSpecConstantOpQuantizeToF16Group (tcu::TestContext& testCtx)
4351 {
4352         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opspecconstantop_opquantize", "Tests the OpQuantizeToF16 opcode for the OpSpecConstantOp instruction"));
4353
4354         const std::string shader (
4355                 string(getComputeAsmShaderPreamble()) +
4356
4357                 "OpName %main           \"main\"\n"
4358                 "OpName %id             \"gl_GlobalInvocationID\"\n"
4359
4360                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
4361
4362                 "OpDecorate %sc_0  SpecId 0\n"
4363                 "OpDecorate %sc_1  SpecId 1\n"
4364                 "OpDecorate %sc_2  SpecId 2\n"
4365                 "OpDecorate %sc_3  SpecId 3\n"
4366                 "OpDecorate %sc_4  SpecId 4\n"
4367                 "OpDecorate %sc_5  SpecId 5\n"
4368
4369                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
4370
4371                 "%id        = OpVariable %uvec3ptr Input\n"
4372                 "%zero      = OpConstant %i32 0\n"
4373                 "%c_u32_6   = OpConstant %u32 6\n"
4374
4375                 "%sc_0      = OpSpecConstant %f32 0.\n"
4376                 "%sc_1      = OpSpecConstant %f32 0.\n"
4377                 "%sc_2      = OpSpecConstant %f32 0.\n"
4378                 "%sc_3      = OpSpecConstant %f32 0.\n"
4379                 "%sc_4      = OpSpecConstant %f32 0.\n"
4380                 "%sc_5      = OpSpecConstant %f32 0.\n"
4381
4382                 "%sc_0_quant = OpSpecConstantOp %f32 QuantizeToF16 %sc_0\n"
4383                 "%sc_1_quant = OpSpecConstantOp %f32 QuantizeToF16 %sc_1\n"
4384                 "%sc_2_quant = OpSpecConstantOp %f32 QuantizeToF16 %sc_2\n"
4385                 "%sc_3_quant = OpSpecConstantOp %f32 QuantizeToF16 %sc_3\n"
4386                 "%sc_4_quant = OpSpecConstantOp %f32 QuantizeToF16 %sc_4\n"
4387                 "%sc_5_quant = OpSpecConstantOp %f32 QuantizeToF16 %sc_5\n"
4388
4389                 "%main      = OpFunction %void None %voidf\n"
4390                 "%label     = OpLabel\n"
4391                 "%idval     = OpLoad %uvec3 %id\n"
4392                 "%x         = OpCompositeExtract %u32 %idval 0\n"
4393                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
4394                 "%selector  = OpUMod %u32 %x %c_u32_6\n"
4395                 "            OpSelectionMerge %exit None\n"
4396                 "            OpSwitch %selector %exit 0 %case0 1 %case1 2 %case2 3 %case3 4 %case4 5 %case5\n"
4397
4398                 "%case0     = OpLabel\n"
4399                 "             OpStore %outloc %sc_0_quant\n"
4400                 "             OpBranch %exit\n"
4401
4402                 "%case1     = OpLabel\n"
4403                 "             OpStore %outloc %sc_1_quant\n"
4404                 "             OpBranch %exit\n"
4405
4406                 "%case2     = OpLabel\n"
4407                 "             OpStore %outloc %sc_2_quant\n"
4408                 "             OpBranch %exit\n"
4409
4410                 "%case3     = OpLabel\n"
4411                 "             OpStore %outloc %sc_3_quant\n"
4412                 "             OpBranch %exit\n"
4413
4414                 "%case4     = OpLabel\n"
4415                 "             OpStore %outloc %sc_4_quant\n"
4416                 "             OpBranch %exit\n"
4417
4418                 "%case5     = OpLabel\n"
4419                 "             OpStore %outloc %sc_5_quant\n"
4420                 "             OpBranch %exit\n"
4421
4422                 "%exit      = OpLabel\n"
4423                 "             OpReturn\n"
4424
4425                 "             OpFunctionEnd\n");
4426
4427         {
4428                 ComputeShaderSpec       spec;
4429                 const deUint8           numCases        = 4;
4430                 vector<float>           inputs          (numCases, 0.f);
4431                 vector<float>           outputs;
4432
4433                 spec.assembly           = shader;
4434                 spec.numWorkGroups      = IVec3(numCases, 1, 1);
4435
4436                 spec.specConstants.push_back(bitwiseCast<deUint32>(std::numeric_limits<float>::infinity()));
4437                 spec.specConstants.push_back(bitwiseCast<deUint32>(-std::numeric_limits<float>::infinity()));
4438                 spec.specConstants.push_back(bitwiseCast<deUint32>(std::ldexp(1.0f, 16)));
4439                 spec.specConstants.push_back(bitwiseCast<deUint32>(std::ldexp(-1.0f, 32)));
4440
4441                 outputs.push_back(std::numeric_limits<float>::infinity());
4442                 outputs.push_back(-std::numeric_limits<float>::infinity());
4443                 outputs.push_back(std::numeric_limits<float>::infinity());
4444                 outputs.push_back(-std::numeric_limits<float>::infinity());
4445
4446                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputs)));
4447                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputs)));
4448
4449                 group->addChild(new SpvAsmComputeShaderCase(
4450                         testCtx, "infinities", "Check that infinities propagated and created", spec));
4451         }
4452
4453         {
4454                 ComputeShaderSpec       spec;
4455                 const deUint8           numCases        = 2;
4456                 vector<float>           inputs          (numCases, 0.f);
4457                 vector<float>           outputs;
4458
4459                 spec.assembly           = shader;
4460                 spec.numWorkGroups      = IVec3(numCases, 1, 1);
4461                 spec.verifyIO           = &compareNan;
4462
4463                 outputs.push_back(std::numeric_limits<float>::quiet_NaN());
4464                 outputs.push_back(-std::numeric_limits<float>::quiet_NaN());
4465
4466                 for (deUint8 idx = 0; idx < numCases; ++idx)
4467                         spec.specConstants.push_back(bitwiseCast<deUint32>(outputs[idx]));
4468
4469                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputs)));
4470                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputs)));
4471
4472                 group->addChild(new SpvAsmComputeShaderCase(
4473                         testCtx, "propagated_nans", "Check that nans are propagated", spec));
4474         }
4475
4476         {
4477                 ComputeShaderSpec       spec;
4478                 const deUint8           numCases        = 6;
4479                 vector<float>           inputs          (numCases, 0.f);
4480                 vector<float>           outputs;
4481
4482                 spec.assembly           = shader;
4483                 spec.numWorkGroups      = IVec3(numCases, 1, 1);
4484
4485                 spec.specConstants.push_back(bitwiseCast<deUint32>(0.f));
4486                 spec.specConstants.push_back(bitwiseCast<deUint32>(-0.f));
4487                 spec.specConstants.push_back(bitwiseCast<deUint32>(std::ldexp(1.0f, -16)));
4488                 spec.specConstants.push_back(bitwiseCast<deUint32>(std::ldexp(-1.0f, -32)));
4489                 spec.specConstants.push_back(bitwiseCast<deUint32>(std::ldexp(1.0f, -127)));
4490                 spec.specConstants.push_back(bitwiseCast<deUint32>(-std::ldexp(1.0f, -128)));
4491
4492                 outputs.push_back(0.f);
4493                 outputs.push_back(-0.f);
4494                 outputs.push_back(0.f);
4495                 outputs.push_back(-0.f);
4496                 outputs.push_back(0.f);
4497                 outputs.push_back(-0.f);
4498
4499                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputs)));
4500                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputs)));
4501
4502                 group->addChild(new SpvAsmComputeShaderCase(
4503                         testCtx, "flush_to_zero", "Check that values are zeroed correctly", spec));
4504         }
4505
4506         {
4507                 ComputeShaderSpec       spec;
4508                 const deUint8           numCases        = 6;
4509                 vector<float>           inputs          (numCases, 0.f);
4510                 vector<float>           outputs;
4511
4512                 spec.assembly           = shader;
4513                 spec.numWorkGroups      = IVec3(numCases, 1, 1);
4514
4515                 for (deUint8 idx = 0; idx < 6; ++idx)
4516                 {
4517                         const float f = static_cast<float>(idx * 10 - 30) / 4.f;
4518                         spec.specConstants.push_back(bitwiseCast<deUint32>(f));
4519                         outputs.push_back(f);
4520                 }
4521
4522                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputs)));
4523                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputs)));
4524
4525                 group->addChild(new SpvAsmComputeShaderCase(
4526                         testCtx, "exact", "Check that values exactly preserved where appropriate", spec));
4527         }
4528
4529         {
4530                 ComputeShaderSpec       spec;
4531                 const deUint8           numCases        = 4;
4532                 vector<float>           inputs          (numCases, 0.f);
4533                 vector<float>           outputs;
4534
4535                 spec.assembly           = shader;
4536                 spec.numWorkGroups      = IVec3(numCases, 1, 1);
4537                 spec.verifyIO           = &compareOpQuantizeF16ComputeExactCase;
4538
4539                 outputs.push_back(constructNormalizedFloat(8, 0x300300));
4540                 outputs.push_back(-constructNormalizedFloat(-7, 0x600800));
4541                 outputs.push_back(constructNormalizedFloat(2, 0x01E000));
4542                 outputs.push_back(constructNormalizedFloat(1, 0xFFE000));
4543
4544                 for (deUint8 idx = 0; idx < numCases; ++idx)
4545                         spec.specConstants.push_back(bitwiseCast<deUint32>(outputs[idx]));
4546
4547                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputs)));
4548                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputs)));
4549
4550                 group->addChild(new SpvAsmComputeShaderCase(
4551                         testCtx, "rounded", "Check that are rounded when needed", spec));
4552         }
4553
4554         return group.release();
4555 }
4556
4557 // Checks that constant null/composite values can be used in computation.
4558 tcu::TestCaseGroup* createOpConstantUsageGroup (tcu::TestContext& testCtx)
4559 {
4560         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opconstantnullcomposite", "Spotcheck the OpConstantNull & OpConstantComposite instruction"));
4561         ComputeShaderSpec                               spec;
4562         de::Random                                              rnd                             (deStringHash(group->getName()));
4563         const int                                               numElements             = 100;
4564         vector<float>                                   positiveFloats  (numElements, 0);
4565         vector<float>                                   negativeFloats  (numElements, 0);
4566
4567         fillRandomScalars(rnd, 1.f, 100.f, &positiveFloats[0], numElements);
4568
4569         for (size_t ndx = 0; ndx < numElements; ++ndx)
4570                 negativeFloats[ndx] = -positiveFloats[ndx];
4571
4572         spec.assembly =
4573                 "OpCapability Shader\n"
4574                 "%std450 = OpExtInstImport \"GLSL.std.450\"\n"
4575                 "OpMemoryModel Logical GLSL450\n"
4576                 "OpEntryPoint GLCompute %main \"main\" %id\n"
4577                 "OpExecutionMode %main LocalSize 1 1 1\n"
4578
4579                 "OpSource GLSL 430\n"
4580                 "OpName %main           \"main\"\n"
4581                 "OpName %id             \"gl_GlobalInvocationID\"\n"
4582
4583                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
4584
4585                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
4586
4587                 "%fmat      = OpTypeMatrix %fvec3 3\n"
4588                 "%ten       = OpConstant %u32 10\n"
4589                 "%f32arr10  = OpTypeArray %f32 %ten\n"
4590                 "%fst       = OpTypeStruct %f32 %f32\n"
4591
4592                 + string(getComputeAsmInputOutputBuffer()) +
4593
4594                 "%id        = OpVariable %uvec3ptr Input\n"
4595                 "%zero      = OpConstant %i32 0\n"
4596
4597                 // Create a bunch of null values
4598                 "%unull     = OpConstantNull %u32\n"
4599                 "%fnull     = OpConstantNull %f32\n"
4600                 "%vnull     = OpConstantNull %fvec3\n"
4601                 "%mnull     = OpConstantNull %fmat\n"
4602                 "%anull     = OpConstantNull %f32arr10\n"
4603                 "%snull     = OpConstantComposite %fst %fnull %fnull\n"
4604
4605                 "%main      = OpFunction %void None %voidf\n"
4606                 "%label     = OpLabel\n"
4607                 "%idval     = OpLoad %uvec3 %id\n"
4608                 "%x         = OpCompositeExtract %u32 %idval 0\n"
4609                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
4610                 "%inval     = OpLoad %f32 %inloc\n"
4611                 "%neg       = OpFNegate %f32 %inval\n"
4612
4613                 // Get the abs() of (a certain element of) those null values
4614                 "%unull_cov = OpConvertUToF %f32 %unull\n"
4615                 "%unull_abs = OpExtInst %f32 %std450 FAbs %unull_cov\n"
4616                 "%fnull_abs = OpExtInst %f32 %std450 FAbs %fnull\n"
4617                 "%vnull_0   = OpCompositeExtract %f32 %vnull 0\n"
4618                 "%vnull_abs = OpExtInst %f32 %std450 FAbs %vnull_0\n"
4619                 "%mnull_12  = OpCompositeExtract %f32 %mnull 1 2\n"
4620                 "%mnull_abs = OpExtInst %f32 %std450 FAbs %mnull_12\n"
4621                 "%anull_3   = OpCompositeExtract %f32 %anull 3\n"
4622                 "%anull_abs = OpExtInst %f32 %std450 FAbs %anull_3\n"
4623                 "%snull_1   = OpCompositeExtract %f32 %snull 1\n"
4624                 "%snull_abs = OpExtInst %f32 %std450 FAbs %snull_1\n"
4625
4626                 // Add them all
4627                 "%add1      = OpFAdd %f32 %neg  %unull_abs\n"
4628                 "%add2      = OpFAdd %f32 %add1 %fnull_abs\n"
4629                 "%add3      = OpFAdd %f32 %add2 %vnull_abs\n"
4630                 "%add4      = OpFAdd %f32 %add3 %mnull_abs\n"
4631                 "%add5      = OpFAdd %f32 %add4 %anull_abs\n"
4632                 "%final     = OpFAdd %f32 %add5 %snull_abs\n"
4633
4634                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
4635                 "             OpStore %outloc %final\n" // write to output
4636                 "             OpReturn\n"
4637                 "             OpFunctionEnd\n";
4638         spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
4639         spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
4640         spec.numWorkGroups = IVec3(numElements, 1, 1);
4641
4642         group->addChild(new SpvAsmComputeShaderCase(testCtx, "spotcheck", "Check that values constructed via OpConstantNull & OpConstantComposite can be used", spec));
4643
4644         return group.release();
4645 }
4646
4647 // Assembly code used for testing loop control is based on GLSL source code:
4648 // #version 430
4649 //
4650 // layout(std140, set = 0, binding = 0) readonly buffer Input {
4651 //   float elements[];
4652 // } input_data;
4653 // layout(std140, set = 0, binding = 1) writeonly buffer Output {
4654 //   float elements[];
4655 // } output_data;
4656 //
4657 // void main() {
4658 //   uint x = gl_GlobalInvocationID.x;
4659 //   output_data.elements[x] = input_data.elements[x];
4660 //   for (uint i = 0; i < 4; ++i)
4661 //     output_data.elements[x] += 1.f;
4662 // }
4663 tcu::TestCaseGroup* createLoopControlGroup (tcu::TestContext& testCtx)
4664 {
4665         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "loop_control", "Tests loop control cases"));
4666         vector<CaseParameter>                   cases;
4667         de::Random                                              rnd                             (deStringHash(group->getName()));
4668         const int                                               numElements             = 100;
4669         vector<float>                                   inputFloats             (numElements, 0);
4670         vector<float>                                   outputFloats    (numElements, 0);
4671         const StringTemplate                    shaderTemplate  (
4672                 string(getComputeAsmShaderPreamble()) +
4673
4674                 "OpSource GLSL 430\n"
4675                 "OpName %main \"main\"\n"
4676                 "OpName %id \"gl_GlobalInvocationID\"\n"
4677
4678                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
4679
4680                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
4681
4682                 "%u32ptr      = OpTypePointer Function %u32\n"
4683
4684                 "%id          = OpVariable %uvec3ptr Input\n"
4685                 "%zero        = OpConstant %i32 0\n"
4686                 "%uzero       = OpConstant %u32 0\n"
4687                 "%one         = OpConstant %i32 1\n"
4688                 "%constf1     = OpConstant %f32 1.0\n"
4689                 "%four        = OpConstant %u32 4\n"
4690
4691                 "%main        = OpFunction %void None %voidf\n"
4692                 "%entry       = OpLabel\n"
4693                 "%i           = OpVariable %u32ptr Function\n"
4694                 "               OpStore %i %uzero\n"
4695
4696                 "%idval       = OpLoad %uvec3 %id\n"
4697                 "%x           = OpCompositeExtract %u32 %idval 0\n"
4698                 "%inloc       = OpAccessChain %f32ptr %indata %zero %x\n"
4699                 "%inval       = OpLoad %f32 %inloc\n"
4700                 "%outloc      = OpAccessChain %f32ptr %outdata %zero %x\n"
4701                 "               OpStore %outloc %inval\n"
4702                 "               OpBranch %loop_entry\n"
4703
4704                 "%loop_entry  = OpLabel\n"
4705                 "%i_val       = OpLoad %u32 %i\n"
4706                 "%cmp_lt      = OpULessThan %bool %i_val %four\n"
4707                 "               OpLoopMerge %loop_merge %loop_body ${CONTROL}\n"
4708                 "               OpBranchConditional %cmp_lt %loop_body %loop_merge\n"
4709                 "%loop_body   = OpLabel\n"
4710                 "%outval      = OpLoad %f32 %outloc\n"
4711                 "%addf1       = OpFAdd %f32 %outval %constf1\n"
4712                 "               OpStore %outloc %addf1\n"
4713                 "%new_i       = OpIAdd %u32 %i_val %one\n"
4714                 "               OpStore %i %new_i\n"
4715                 "               OpBranch %loop_entry\n"
4716                 "%loop_merge  = OpLabel\n"
4717                 "               OpReturn\n"
4718                 "               OpFunctionEnd\n");
4719
4720         cases.push_back(CaseParameter("none",                           "None"));
4721         cases.push_back(CaseParameter("unroll",                         "Unroll"));
4722         cases.push_back(CaseParameter("dont_unroll",            "DontUnroll"));
4723         cases.push_back(CaseParameter("unroll_dont_unroll",     "Unroll|DontUnroll"));
4724
4725         fillRandomScalars(rnd, -100.f, 100.f, &inputFloats[0], numElements);
4726
4727         for (size_t ndx = 0; ndx < numElements; ++ndx)
4728                 outputFloats[ndx] = inputFloats[ndx] + 4.f;
4729
4730         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
4731         {
4732                 map<string, string>             specializations;
4733                 ComputeShaderSpec               spec;
4734
4735                 specializations["CONTROL"] = cases[caseNdx].param;
4736                 spec.assembly = shaderTemplate.specialize(specializations);
4737                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
4738                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
4739                 spec.numWorkGroups = IVec3(numElements, 1, 1);
4740
4741                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
4742         }
4743
4744         return group.release();
4745 }
4746
4747 // Assembly code used for testing selection control is based on GLSL source code:
4748 // #version 430
4749 //
4750 // layout(std140, set = 0, binding = 0) readonly buffer Input {
4751 //   float elements[];
4752 // } input_data;
4753 // layout(std140, set = 0, binding = 1) writeonly buffer Output {
4754 //   float elements[];
4755 // } output_data;
4756 //
4757 // void main() {
4758 //   uint x = gl_GlobalInvocationID.x;
4759 //   float val = input_data.elements[x];
4760 //   if (val > 10.f)
4761 //     output_data.elements[x] = val + 1.f;
4762 //   else
4763 //     output_data.elements[x] = val - 1.f;
4764 // }
4765 tcu::TestCaseGroup* createSelectionControlGroup (tcu::TestContext& testCtx)
4766 {
4767         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "selection_control", "Tests selection control cases"));
4768         vector<CaseParameter>                   cases;
4769         de::Random                                              rnd                             (deStringHash(group->getName()));
4770         const int                                               numElements             = 100;
4771         vector<float>                                   inputFloats             (numElements, 0);
4772         vector<float>                                   outputFloats    (numElements, 0);
4773         const StringTemplate                    shaderTemplate  (
4774                 string(getComputeAsmShaderPreamble()) +
4775
4776                 "OpSource GLSL 430\n"
4777                 "OpName %main \"main\"\n"
4778                 "OpName %id \"gl_GlobalInvocationID\"\n"
4779
4780                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
4781
4782                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
4783
4784                 "%id       = OpVariable %uvec3ptr Input\n"
4785                 "%zero     = OpConstant %i32 0\n"
4786                 "%constf1  = OpConstant %f32 1.0\n"
4787                 "%constf10 = OpConstant %f32 10.0\n"
4788
4789                 "%main     = OpFunction %void None %voidf\n"
4790                 "%entry    = OpLabel\n"
4791                 "%idval    = OpLoad %uvec3 %id\n"
4792                 "%x        = OpCompositeExtract %u32 %idval 0\n"
4793                 "%inloc    = OpAccessChain %f32ptr %indata %zero %x\n"
4794                 "%inval    = OpLoad %f32 %inloc\n"
4795                 "%outloc   = OpAccessChain %f32ptr %outdata %zero %x\n"
4796                 "%cmp_gt   = OpFOrdGreaterThan %bool %inval %constf10\n"
4797
4798                 "            OpSelectionMerge %if_end ${CONTROL}\n"
4799                 "            OpBranchConditional %cmp_gt %if_true %if_false\n"
4800                 "%if_true  = OpLabel\n"
4801                 "%addf1    = OpFAdd %f32 %inval %constf1\n"
4802                 "            OpStore %outloc %addf1\n"
4803                 "            OpBranch %if_end\n"
4804                 "%if_false = OpLabel\n"
4805                 "%subf1    = OpFSub %f32 %inval %constf1\n"
4806                 "            OpStore %outloc %subf1\n"
4807                 "            OpBranch %if_end\n"
4808                 "%if_end   = OpLabel\n"
4809                 "            OpReturn\n"
4810                 "            OpFunctionEnd\n");
4811
4812         cases.push_back(CaseParameter("none",                                   "None"));
4813         cases.push_back(CaseParameter("flatten",                                "Flatten"));
4814         cases.push_back(CaseParameter("dont_flatten",                   "DontFlatten"));
4815         cases.push_back(CaseParameter("flatten_dont_flatten",   "DontFlatten|Flatten"));
4816
4817         fillRandomScalars(rnd, -100.f, 100.f, &inputFloats[0], numElements);
4818
4819         // CPU might not use the same rounding mode as the GPU. Use whole numbers to avoid rounding differences.
4820         floorAll(inputFloats);
4821
4822         for (size_t ndx = 0; ndx < numElements; ++ndx)
4823                 outputFloats[ndx] = inputFloats[ndx] + (inputFloats[ndx] > 10.f ? 1.f : -1.f);
4824
4825         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
4826         {
4827                 map<string, string>             specializations;
4828                 ComputeShaderSpec               spec;
4829
4830                 specializations["CONTROL"] = cases[caseNdx].param;
4831                 spec.assembly = shaderTemplate.specialize(specializations);
4832                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
4833                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
4834                 spec.numWorkGroups = IVec3(numElements, 1, 1);
4835
4836                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
4837         }
4838
4839         return group.release();
4840 }
4841
4842 // Assembly code used for testing function control is based on GLSL source code:
4843 //
4844 // #version 430
4845 //
4846 // layout(std140, set = 0, binding = 0) readonly buffer Input {
4847 //   float elements[];
4848 // } input_data;
4849 // layout(std140, set = 0, binding = 1) writeonly buffer Output {
4850 //   float elements[];
4851 // } output_data;
4852 //
4853 // float const10() { return 10.f; }
4854 //
4855 // void main() {
4856 //   uint x = gl_GlobalInvocationID.x;
4857 //   output_data.elements[x] = input_data.elements[x] + const10();
4858 // }
4859 tcu::TestCaseGroup* createFunctionControlGroup (tcu::TestContext& testCtx)
4860 {
4861         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "function_control", "Tests function control cases"));
4862         vector<CaseParameter>                   cases;
4863         de::Random                                              rnd                             (deStringHash(group->getName()));
4864         const int                                               numElements             = 100;
4865         vector<float>                                   inputFloats             (numElements, 0);
4866         vector<float>                                   outputFloats    (numElements, 0);
4867         const StringTemplate                    shaderTemplate  (
4868                 string(getComputeAsmShaderPreamble()) +
4869
4870                 "OpSource GLSL 430\n"
4871                 "OpName %main \"main\"\n"
4872                 "OpName %func_const10 \"const10(\"\n"
4873                 "OpName %id \"gl_GlobalInvocationID\"\n"
4874
4875                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
4876
4877                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
4878
4879                 "%f32f = OpTypeFunction %f32\n"
4880                 "%id = OpVariable %uvec3ptr Input\n"
4881                 "%zero = OpConstant %i32 0\n"
4882                 "%constf10 = OpConstant %f32 10.0\n"
4883
4884                 "%main         = OpFunction %void None %voidf\n"
4885                 "%entry        = OpLabel\n"
4886                 "%idval        = OpLoad %uvec3 %id\n"
4887                 "%x            = OpCompositeExtract %u32 %idval 0\n"
4888                 "%inloc        = OpAccessChain %f32ptr %indata %zero %x\n"
4889                 "%inval        = OpLoad %f32 %inloc\n"
4890                 "%ret_10       = OpFunctionCall %f32 %func_const10\n"
4891                 "%fadd         = OpFAdd %f32 %inval %ret_10\n"
4892                 "%outloc       = OpAccessChain %f32ptr %outdata %zero %x\n"
4893                 "                OpStore %outloc %fadd\n"
4894                 "                OpReturn\n"
4895                 "                OpFunctionEnd\n"
4896
4897                 "%func_const10 = OpFunction %f32 ${CONTROL} %f32f\n"
4898                 "%label        = OpLabel\n"
4899                 "                OpReturnValue %constf10\n"
4900                 "                OpFunctionEnd\n");
4901
4902         cases.push_back(CaseParameter("none",                                           "None"));
4903         cases.push_back(CaseParameter("inline",                                         "Inline"));
4904         cases.push_back(CaseParameter("dont_inline",                            "DontInline"));
4905         cases.push_back(CaseParameter("pure",                                           "Pure"));
4906         cases.push_back(CaseParameter("const",                                          "Const"));
4907         cases.push_back(CaseParameter("inline_pure",                            "Inline|Pure"));
4908         cases.push_back(CaseParameter("const_dont_inline",                      "Const|DontInline"));
4909         cases.push_back(CaseParameter("inline_dont_inline",                     "Inline|DontInline"));
4910         cases.push_back(CaseParameter("pure_inline_dont_inline",        "Pure|Inline|DontInline"));
4911
4912         fillRandomScalars(rnd, -100.f, 100.f, &inputFloats[0], numElements);
4913
4914         // CPU might not use the same rounding mode as the GPU. Use whole numbers to avoid rounding differences.
4915         floorAll(inputFloats);
4916
4917         for (size_t ndx = 0; ndx < numElements; ++ndx)
4918                 outputFloats[ndx] = inputFloats[ndx] + 10.f;
4919
4920         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
4921         {
4922                 map<string, string>             specializations;
4923                 ComputeShaderSpec               spec;
4924
4925                 specializations["CONTROL"] = cases[caseNdx].param;
4926                 spec.assembly = shaderTemplate.specialize(specializations);
4927                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
4928                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
4929                 spec.numWorkGroups = IVec3(numElements, 1, 1);
4930
4931                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
4932         }
4933
4934         return group.release();
4935 }
4936
4937 tcu::TestCaseGroup* createMemoryAccessGroup (tcu::TestContext& testCtx)
4938 {
4939         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "memory_access", "Tests memory access cases"));
4940         vector<CaseParameter>                   cases;
4941         de::Random                                              rnd                             (deStringHash(group->getName()));
4942         const int                                               numElements             = 100;
4943         vector<float>                                   inputFloats             (numElements, 0);
4944         vector<float>                                   outputFloats    (numElements, 0);
4945         const StringTemplate                    shaderTemplate  (
4946                 string(getComputeAsmShaderPreamble()) +
4947
4948                 "OpSource GLSL 430\n"
4949                 "OpName %main           \"main\"\n"
4950                 "OpName %id             \"gl_GlobalInvocationID\"\n"
4951
4952                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
4953
4954                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) + string(getComputeAsmInputOutputBuffer()) +
4955
4956                 "%f32ptr_f  = OpTypePointer Function %f32\n"
4957
4958                 "%id        = OpVariable %uvec3ptr Input\n"
4959                 "%zero      = OpConstant %i32 0\n"
4960                 "%four      = OpConstant %i32 4\n"
4961
4962                 "%main      = OpFunction %void None %voidf\n"
4963                 "%label     = OpLabel\n"
4964                 "%copy      = OpVariable %f32ptr_f Function\n"
4965                 "%idval     = OpLoad %uvec3 %id ${ACCESS}\n"
4966                 "%x         = OpCompositeExtract %u32 %idval 0\n"
4967                 "%inloc     = OpAccessChain %f32ptr %indata  %zero %x\n"
4968                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
4969                 "             OpCopyMemory %copy %inloc ${ACCESS}\n"
4970                 "%val1      = OpLoad %f32 %copy\n"
4971                 "%val2      = OpLoad %f32 %inloc\n"
4972                 "%add       = OpFAdd %f32 %val1 %val2\n"
4973                 "             OpStore %outloc %add ${ACCESS}\n"
4974                 "             OpReturn\n"
4975                 "             OpFunctionEnd\n");
4976
4977         cases.push_back(CaseParameter("null",                                   ""));
4978         cases.push_back(CaseParameter("none",                                   "None"));
4979         cases.push_back(CaseParameter("volatile",                               "Volatile"));
4980         cases.push_back(CaseParameter("aligned",                                "Aligned 4"));
4981         cases.push_back(CaseParameter("nontemporal",                    "Nontemporal"));
4982         cases.push_back(CaseParameter("aligned_nontemporal",    "Aligned|Nontemporal 4"));
4983         cases.push_back(CaseParameter("aligned_volatile",               "Volatile|Aligned 4"));
4984
4985         fillRandomScalars(rnd, -100.f, 100.f, &inputFloats[0], numElements);
4986
4987         for (size_t ndx = 0; ndx < numElements; ++ndx)
4988                 outputFloats[ndx] = inputFloats[ndx] + inputFloats[ndx];
4989
4990         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
4991         {
4992                 map<string, string>             specializations;
4993                 ComputeShaderSpec               spec;
4994
4995                 specializations["ACCESS"] = cases[caseNdx].param;
4996                 spec.assembly = shaderTemplate.specialize(specializations);
4997                 spec.inputs.push_back(BufferSp(new Float32Buffer(inputFloats)));
4998                 spec.outputs.push_back(BufferSp(new Float32Buffer(outputFloats)));
4999                 spec.numWorkGroups = IVec3(numElements, 1, 1);
5000
5001                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
5002         }
5003
5004         return group.release();
5005 }
5006
5007 // Checks that we can get undefined values for various types, without exercising a computation with it.
5008 tcu::TestCaseGroup* createOpUndefGroup (tcu::TestContext& testCtx)
5009 {
5010         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opundef", "Tests the OpUndef instruction"));
5011         vector<CaseParameter>                   cases;
5012         de::Random                                              rnd                             (deStringHash(group->getName()));
5013         const int                                               numElements             = 100;
5014         vector<float>                                   positiveFloats  (numElements, 0);
5015         vector<float>                                   negativeFloats  (numElements, 0);
5016         const StringTemplate                    shaderTemplate  (
5017                 string(getComputeAsmShaderPreamble()) +
5018
5019                 "OpSource GLSL 430\n"
5020                 "OpName %main           \"main\"\n"
5021                 "OpName %id             \"gl_GlobalInvocationID\"\n"
5022
5023                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
5024
5025                 + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes()) +
5026                 "%uvec2     = OpTypeVector %u32 2\n"
5027                 "%fvec4     = OpTypeVector %f32 4\n"
5028                 "%fmat33    = OpTypeMatrix %fvec3 3\n"
5029                 "%image     = OpTypeImage %f32 2D 0 0 0 1 Unknown\n"
5030                 "%sampler   = OpTypeSampler\n"
5031                 "%simage    = OpTypeSampledImage %image\n"
5032                 "%const100  = OpConstant %u32 100\n"
5033                 "%uarr100   = OpTypeArray %i32 %const100\n"
5034                 "%struct    = OpTypeStruct %f32 %i32 %u32\n"
5035                 "%pointer   = OpTypePointer Function %i32\n"
5036                 + string(getComputeAsmInputOutputBuffer()) +
5037
5038                 "%id        = OpVariable %uvec3ptr Input\n"
5039                 "%zero      = OpConstant %i32 0\n"
5040
5041                 "%main      = OpFunction %void None %voidf\n"
5042                 "%label     = OpLabel\n"
5043
5044                 "%undef     = OpUndef ${TYPE}\n"
5045
5046                 "%idval     = OpLoad %uvec3 %id\n"
5047                 "%x         = OpCompositeExtract %u32 %idval 0\n"
5048
5049                 "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
5050                 "%inval     = OpLoad %f32 %inloc\n"
5051                 "%neg       = OpFNegate %f32 %inval\n"
5052                 "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
5053                 "             OpStore %outloc %neg\n"
5054                 "             OpReturn\n"
5055                 "             OpFunctionEnd\n");
5056
5057         cases.push_back(CaseParameter("bool",                   "%bool"));
5058         cases.push_back(CaseParameter("sint32",                 "%i32"));
5059         cases.push_back(CaseParameter("uint32",                 "%u32"));
5060         cases.push_back(CaseParameter("float32",                "%f32"));
5061         cases.push_back(CaseParameter("vec4float32",    "%fvec4"));
5062         cases.push_back(CaseParameter("vec2uint32",             "%uvec2"));
5063         cases.push_back(CaseParameter("matrix",                 "%fmat33"));
5064         cases.push_back(CaseParameter("image",                  "%image"));
5065         cases.push_back(CaseParameter("sampler",                "%sampler"));
5066         cases.push_back(CaseParameter("sampledimage",   "%simage"));
5067         cases.push_back(CaseParameter("array",                  "%uarr100"));
5068         cases.push_back(CaseParameter("runtimearray",   "%f32arr"));
5069         cases.push_back(CaseParameter("struct",                 "%struct"));
5070         cases.push_back(CaseParameter("pointer",                "%pointer"));
5071
5072         fillRandomScalars(rnd, 1.f, 100.f, &positiveFloats[0], numElements);
5073
5074         for (size_t ndx = 0; ndx < numElements; ++ndx)
5075                 negativeFloats[ndx] = -positiveFloats[ndx];
5076
5077         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
5078         {
5079                 map<string, string>             specializations;
5080                 ComputeShaderSpec               spec;
5081
5082                 specializations["TYPE"] = cases[caseNdx].param;
5083                 spec.assembly = shaderTemplate.specialize(specializations);
5084                 spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
5085                 spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
5086                 spec.numWorkGroups = IVec3(numElements, 1, 1);
5087
5088                 group->addChild(new SpvAsmComputeShaderCase(testCtx, cases[caseNdx].name, cases[caseNdx].name, spec));
5089         }
5090
5091                 return group.release();
5092 }
5093
5094 } // anonymous
5095
5096 tcu::TestCaseGroup* createOpSourceTests (tcu::TestContext& testCtx)
5097 {
5098         struct NameCodePair { string name, code; };
5099         RGBA                                                    defaultColors[4];
5100         de::MovePtr<tcu::TestCaseGroup> opSourceTests                   (new tcu::TestCaseGroup(testCtx, "opsource", "OpSource instruction"));
5101         const std::string                               opsourceGLSLWithFile    = "%opsrcfile = OpString \"foo.vert\"\nOpSource GLSL 450 %opsrcfile ";
5102         map<string, string>                             fragments                               = passthruFragments();
5103         const NameCodePair                              tests[]                                 =
5104         {
5105                 {"unknown", "OpSource Unknown 321"},
5106                 {"essl", "OpSource ESSL 310"},
5107                 {"glsl", "OpSource GLSL 450"},
5108                 {"opencl_cpp", "OpSource OpenCL_CPP 120"},
5109                 {"opencl_c", "OpSource OpenCL_C 120"},
5110                 {"multiple", "OpSource GLSL 450\nOpSource GLSL 450"},
5111                 {"file", opsourceGLSLWithFile},
5112                 {"source", opsourceGLSLWithFile + "\"void main(){}\""},
5113                 // Longest possible source string: SPIR-V limits instructions to 65535
5114                 // words, of which the first 4 are opsourceGLSLWithFile; the rest will
5115                 // contain 65530 UTF8 characters (one word each) plus one last word
5116                 // containing 3 ASCII characters and \0.
5117                 {"longsource", opsourceGLSLWithFile + '"' + makeLongUTF8String(65530) + "ccc" + '"'}
5118         };
5119
5120         getDefaultColors(defaultColors);
5121         for (size_t testNdx = 0; testNdx < sizeof(tests) / sizeof(NameCodePair); ++testNdx)
5122         {
5123                 fragments["debug"] = tests[testNdx].code;
5124                 createTestsForAllStages(tests[testNdx].name, defaultColors, defaultColors, fragments, opSourceTests.get());
5125         }
5126
5127         return opSourceTests.release();
5128 }
5129
5130 tcu::TestCaseGroup* createOpSourceContinuedTests (tcu::TestContext& testCtx)
5131 {
5132         struct NameCodePair { string name, code; };
5133         RGBA                                                            defaultColors[4];
5134         de::MovePtr<tcu::TestCaseGroup>         opSourceTests           (new tcu::TestCaseGroup(testCtx, "opsourcecontinued", "OpSourceContinued instruction"));
5135         map<string, string>                                     fragments                       = passthruFragments();
5136         const std::string                                       opsource                        = "%opsrcfile = OpString \"foo.vert\"\nOpSource GLSL 450 %opsrcfile \"void main(){}\"\n";
5137         const NameCodePair                                      tests[]                         =
5138         {
5139                 {"empty", opsource + "OpSourceContinued \"\""},
5140                 {"short", opsource + "OpSourceContinued \"abcde\""},
5141                 {"multiple", opsource + "OpSourceContinued \"abcde\"\nOpSourceContinued \"fghij\""},
5142                 // Longest possible source string: SPIR-V limits instructions to 65535
5143                 // words, of which the first one is OpSourceContinued/length; the rest
5144                 // will contain 65533 UTF8 characters (one word each) plus one last word
5145                 // containing 3 ASCII characters and \0.
5146                 {"long", opsource + "OpSourceContinued \"" + makeLongUTF8String(65533) + "ccc\""}
5147         };
5148
5149         getDefaultColors(defaultColors);
5150         for (size_t testNdx = 0; testNdx < sizeof(tests) / sizeof(NameCodePair); ++testNdx)
5151         {
5152                 fragments["debug"] = tests[testNdx].code;
5153                 createTestsForAllStages(tests[testNdx].name, defaultColors, defaultColors, fragments, opSourceTests.get());
5154         }
5155
5156         return opSourceTests.release();
5157 }
5158
5159 tcu::TestCaseGroup* createOpNoLineTests(tcu::TestContext& testCtx)
5160 {
5161         RGBA                                                             defaultColors[4];
5162         de::MovePtr<tcu::TestCaseGroup>          opLineTests             (new tcu::TestCaseGroup(testCtx, "opnoline", "OpNoLine instruction"));
5163         map<string, string>                                      fragments;
5164         getDefaultColors(defaultColors);
5165         fragments["debug"]                      =
5166                 "%name = OpString \"name\"\n";
5167
5168         fragments["pre_main"]   =
5169                 "OpNoLine\n"
5170                 "OpNoLine\n"
5171                 "OpLine %name 1 1\n"
5172                 "OpNoLine\n"
5173                 "OpLine %name 1 1\n"
5174                 "OpLine %name 1 1\n"
5175                 "%second_function = OpFunction %v4f32 None %v4f32_function\n"
5176                 "OpNoLine\n"
5177                 "OpLine %name 1 1\n"
5178                 "OpNoLine\n"
5179                 "OpLine %name 1 1\n"
5180                 "OpLine %name 1 1\n"
5181                 "%second_param1 = OpFunctionParameter %v4f32\n"
5182                 "OpNoLine\n"
5183                 "OpNoLine\n"
5184                 "%label_secondfunction = OpLabel\n"
5185                 "OpNoLine\n"
5186                 "OpReturnValue %second_param1\n"
5187                 "OpFunctionEnd\n"
5188                 "OpNoLine\n"
5189                 "OpNoLine\n";
5190
5191         fragments["testfun"]            =
5192                 // A %test_code function that returns its argument unchanged.
5193                 "OpNoLine\n"
5194                 "OpNoLine\n"
5195                 "OpLine %name 1 1\n"
5196                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
5197                 "OpNoLine\n"
5198                 "%param1 = OpFunctionParameter %v4f32\n"
5199                 "OpNoLine\n"
5200                 "OpNoLine\n"
5201                 "%label_testfun = OpLabel\n"
5202                 "OpNoLine\n"
5203                 "%val1 = OpFunctionCall %v4f32 %second_function %param1\n"
5204                 "OpReturnValue %val1\n"
5205                 "OpFunctionEnd\n"
5206                 "OpLine %name 1 1\n"
5207                 "OpNoLine\n";
5208
5209         createTestsForAllStages("opnoline", defaultColors, defaultColors, fragments, opLineTests.get());
5210
5211         return opLineTests.release();
5212 }
5213
5214
5215 tcu::TestCaseGroup* createOpLineTests(tcu::TestContext& testCtx)
5216 {
5217         RGBA                                                                                                    defaultColors[4];
5218         de::MovePtr<tcu::TestCaseGroup>                                                 opLineTests                     (new tcu::TestCaseGroup(testCtx, "opline", "OpLine instruction"));
5219         map<string, string>                                                                             fragments;
5220         std::vector<std::pair<std::string, std::string> >               problemStrings;
5221
5222         problemStrings.push_back(std::make_pair<std::string, std::string>("empty_name", ""));
5223         problemStrings.push_back(std::make_pair<std::string, std::string>("short_name", "short_name"));
5224         problemStrings.push_back(std::make_pair<std::string, std::string>("long_name", makeLongUTF8String(65530) + "ccc"));
5225         getDefaultColors(defaultColors);
5226
5227         fragments["debug"]                      =
5228                 "%other_name = OpString \"other_name\"\n";
5229
5230         fragments["pre_main"]   =
5231                 "OpLine %file_name 32 0\n"
5232                 "OpLine %file_name 32 32\n"
5233                 "OpLine %file_name 32 40\n"
5234                 "OpLine %other_name 32 40\n"
5235                 "OpLine %other_name 0 100\n"
5236                 "OpLine %other_name 0 4294967295\n"
5237                 "OpLine %other_name 4294967295 0\n"
5238                 "OpLine %other_name 32 40\n"
5239                 "OpLine %file_name 0 0\n"
5240                 "%second_function = OpFunction %v4f32 None %v4f32_function\n"
5241                 "OpLine %file_name 1 0\n"
5242                 "%second_param1 = OpFunctionParameter %v4f32\n"
5243                 "OpLine %file_name 1 3\n"
5244                 "OpLine %file_name 1 2\n"
5245                 "%label_secondfunction = OpLabel\n"
5246                 "OpLine %file_name 0 2\n"
5247                 "OpReturnValue %second_param1\n"
5248                 "OpFunctionEnd\n"
5249                 "OpLine %file_name 0 2\n"
5250                 "OpLine %file_name 0 2\n";
5251
5252         fragments["testfun"]            =
5253                 // A %test_code function that returns its argument unchanged.
5254                 "OpLine %file_name 1 0\n"
5255                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
5256                 "OpLine %file_name 16 330\n"
5257                 "%param1 = OpFunctionParameter %v4f32\n"
5258                 "OpLine %file_name 14 442\n"
5259                 "%label_testfun = OpLabel\n"
5260                 "OpLine %file_name 11 1024\n"
5261                 "%val1 = OpFunctionCall %v4f32 %second_function %param1\n"
5262                 "OpLine %file_name 2 97\n"
5263                 "OpReturnValue %val1\n"
5264                 "OpFunctionEnd\n"
5265                 "OpLine %file_name 5 32\n";
5266
5267         for (size_t i = 0; i < problemStrings.size(); ++i)
5268         {
5269                 map<string, string> testFragments = fragments;
5270                 testFragments["debug"] += "%file_name = OpString \"" + problemStrings[i].second + "\"\n";
5271                 createTestsForAllStages(string("opline") + "_" + problemStrings[i].first, defaultColors, defaultColors, testFragments, opLineTests.get());
5272         }
5273
5274         return opLineTests.release();
5275 }
5276
5277 tcu::TestCaseGroup* createOpConstantNullTests(tcu::TestContext& testCtx)
5278 {
5279         de::MovePtr<tcu::TestCaseGroup> opConstantNullTests             (new tcu::TestCaseGroup(testCtx, "opconstantnull", "OpConstantNull instruction"));
5280         RGBA                                                    colors[4];
5281
5282
5283         const char                                              functionStart[] =
5284                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
5285                 "%param1 = OpFunctionParameter %v4f32\n"
5286                 "%lbl    = OpLabel\n";
5287
5288         const char                                              functionEnd[]   =
5289                 "OpReturnValue %transformed_param\n"
5290                 "OpFunctionEnd\n";
5291
5292         struct NameConstantsCode
5293         {
5294                 string name;
5295                 string constants;
5296                 string code;
5297         };
5298
5299         NameConstantsCode tests[] =
5300         {
5301                 {
5302                         "vec4",
5303                         "%cnull = OpConstantNull %v4f32\n",
5304                         "%transformed_param = OpFAdd %v4f32 %param1 %cnull\n"
5305                 },
5306                 {
5307                         "float",
5308                         "%cnull = OpConstantNull %f32\n",
5309                         "%vp = OpVariable %fp_v4f32 Function\n"
5310                         "%v  = OpLoad %v4f32 %vp\n"
5311                         "%v0 = OpVectorInsertDynamic %v4f32 %v %cnull %c_i32_0\n"
5312                         "%v1 = OpVectorInsertDynamic %v4f32 %v0 %cnull %c_i32_1\n"
5313                         "%v2 = OpVectorInsertDynamic %v4f32 %v1 %cnull %c_i32_2\n"
5314                         "%v3 = OpVectorInsertDynamic %v4f32 %v2 %cnull %c_i32_3\n"
5315                         "%transformed_param = OpFAdd %v4f32 %param1 %v3\n"
5316                 },
5317                 {
5318                         "bool",
5319                         "%cnull             = OpConstantNull %bool\n",
5320                         "%v                 = OpVariable %fp_v4f32 Function\n"
5321                         "                     OpStore %v %param1\n"
5322                         "                     OpSelectionMerge %false_label None\n"
5323                         "                     OpBranchConditional %cnull %true_label %false_label\n"
5324                         "%true_label        = OpLabel\n"
5325                         "                     OpStore %v %c_v4f32_0_5_0_5_0_5_0_5\n"
5326                         "                     OpBranch %false_label\n"
5327                         "%false_label       = OpLabel\n"
5328                         "%transformed_param = OpLoad %v4f32 %v\n"
5329                 },
5330                 {
5331                         "i32",
5332                         "%cnull             = OpConstantNull %i32\n",
5333                         "%v                 = OpVariable %fp_v4f32 Function %c_v4f32_0_5_0_5_0_5_0_5\n"
5334                         "%b                 = OpIEqual %bool %cnull %c_i32_0\n"
5335                         "                     OpSelectionMerge %false_label None\n"
5336                         "                     OpBranchConditional %b %true_label %false_label\n"
5337                         "%true_label        = OpLabel\n"
5338                         "                     OpStore %v %param1\n"
5339                         "                     OpBranch %false_label\n"
5340                         "%false_label       = OpLabel\n"
5341                         "%transformed_param = OpLoad %v4f32 %v\n"
5342                 },
5343                 {
5344                         "struct",
5345                         "%stype             = OpTypeStruct %f32 %v4f32\n"
5346                         "%fp_stype          = OpTypePointer Function %stype\n"
5347                         "%cnull             = OpConstantNull %stype\n",
5348                         "%v                 = OpVariable %fp_stype Function %cnull\n"
5349                         "%f                 = OpAccessChain %fp_v4f32 %v %c_i32_1\n"
5350                         "%f_val             = OpLoad %v4f32 %f\n"
5351                         "%transformed_param = OpFAdd %v4f32 %param1 %f_val\n"
5352                 },
5353                 {
5354                         "array",
5355                         "%a4_v4f32          = OpTypeArray %v4f32 %c_u32_4\n"
5356                         "%fp_a4_v4f32       = OpTypePointer Function %a4_v4f32\n"
5357                         "%cnull             = OpConstantNull %a4_v4f32\n",
5358                         "%v                 = OpVariable %fp_a4_v4f32 Function %cnull\n"
5359                         "%f                 = OpAccessChain %fp_v4f32 %v %c_u32_0\n"
5360                         "%f1                = OpAccessChain %fp_v4f32 %v %c_u32_1\n"
5361                         "%f2                = OpAccessChain %fp_v4f32 %v %c_u32_2\n"
5362                         "%f3                = OpAccessChain %fp_v4f32 %v %c_u32_3\n"
5363                         "%f_val             = OpLoad %v4f32 %f\n"
5364                         "%f1_val            = OpLoad %v4f32 %f1\n"
5365                         "%f2_val            = OpLoad %v4f32 %f2\n"
5366                         "%f3_val            = OpLoad %v4f32 %f3\n"
5367                         "%t0                = OpFAdd %v4f32 %param1 %f_val\n"
5368                         "%t1                = OpFAdd %v4f32 %t0 %f1_val\n"
5369                         "%t2                = OpFAdd %v4f32 %t1 %f2_val\n"
5370                         "%transformed_param = OpFAdd %v4f32 %t2 %f3_val\n"
5371                 },
5372                 {
5373                         "matrix",
5374                         "%mat4x4_f32        = OpTypeMatrix %v4f32 4\n"
5375                         "%cnull             = OpConstantNull %mat4x4_f32\n",
5376                         // Our null matrix * any vector should result in a zero vector.
5377                         "%v                 = OpVectorTimesMatrix %v4f32 %param1 %cnull\n"
5378                         "%transformed_param = OpFAdd %v4f32 %param1 %v\n"
5379                 }
5380         };
5381
5382         getHalfColorsFullAlpha(colors);
5383
5384         for (size_t testNdx = 0; testNdx < sizeof(tests) / sizeof(NameConstantsCode); ++testNdx)
5385         {
5386                 map<string, string> fragments;
5387                 fragments["pre_main"] = tests[testNdx].constants;
5388                 fragments["testfun"] = string(functionStart) + tests[testNdx].code + functionEnd;
5389                 createTestsForAllStages(tests[testNdx].name, colors, colors, fragments, opConstantNullTests.get());
5390         }
5391         return opConstantNullTests.release();
5392 }
5393 tcu::TestCaseGroup* createOpConstantCompositeTests(tcu::TestContext& testCtx)
5394 {
5395         de::MovePtr<tcu::TestCaseGroup> opConstantCompositeTests                (new tcu::TestCaseGroup(testCtx, "opconstantcomposite", "OpConstantComposite instruction"));
5396         RGBA                                                    inputColors[4];
5397         RGBA                                                    outputColors[4];
5398
5399
5400         const char                                              functionStart[]  =
5401                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
5402                 "%param1 = OpFunctionParameter %v4f32\n"
5403                 "%lbl    = OpLabel\n";
5404
5405         const char                                              functionEnd[]           =
5406                 "OpReturnValue %transformed_param\n"
5407                 "OpFunctionEnd\n";
5408
5409         struct NameConstantsCode
5410         {
5411                 string name;
5412                 string constants;
5413                 string code;
5414         };
5415
5416         NameConstantsCode tests[] =
5417         {
5418                 {
5419                         "vec4",
5420
5421                         "%cval              = OpConstantComposite %v4f32 %c_f32_0_5 %c_f32_0_5 %c_f32_0_5 %c_f32_0\n",
5422                         "%transformed_param = OpFAdd %v4f32 %param1 %cval\n"
5423                 },
5424                 {
5425                         "struct",
5426
5427                         "%stype             = OpTypeStruct %v4f32 %f32\n"
5428                         "%fp_stype          = OpTypePointer Function %stype\n"
5429                         "%f32_n_1           = OpConstant %f32 -1.0\n"
5430                         "%f32_1_5           = OpConstant %f32 !0x3fc00000\n" // +1.5
5431                         "%cvec              = OpConstantComposite %v4f32 %f32_1_5 %f32_1_5 %f32_1_5 %c_f32_1\n"
5432                         "%cval              = OpConstantComposite %stype %cvec %f32_n_1\n",
5433
5434                         "%v                 = OpVariable %fp_stype Function %cval\n"
5435                         "%vec_ptr           = OpAccessChain %fp_v4f32 %v %c_u32_0\n"
5436                         "%f32_ptr           = OpAccessChain %fp_f32 %v %c_u32_1\n"
5437                         "%vec_val           = OpLoad %v4f32 %vec_ptr\n"
5438                         "%f32_val           = OpLoad %f32 %f32_ptr\n"
5439                         "%tmp1              = OpVectorTimesScalar %v4f32 %c_v4f32_1_1_1_1 %f32_val\n" // vec4(-1)
5440                         "%tmp2              = OpFAdd %v4f32 %tmp1 %param1\n" // param1 + vec4(-1)
5441                         "%transformed_param = OpFAdd %v4f32 %tmp2 %vec_val\n" // param1 + vec4(-1) + vec4(1.5, 1.5, 1.5, 1.0)
5442                 },
5443                 {
5444                         // [1|0|0|0.5] [x] = x + 0.5
5445                         // [0|1|0|0.5] [y] = y + 0.5
5446                         // [0|0|1|0.5] [z] = z + 0.5
5447                         // [0|0|0|1  ] [1] = 1
5448                         "matrix",
5449
5450                         "%mat4x4_f32          = OpTypeMatrix %v4f32 4\n"
5451                     "%v4f32_1_0_0_0       = OpConstantComposite %v4f32 %c_f32_1 %c_f32_0 %c_f32_0 %c_f32_0\n"
5452                     "%v4f32_0_1_0_0       = OpConstantComposite %v4f32 %c_f32_0 %c_f32_1 %c_f32_0 %c_f32_0\n"
5453                     "%v4f32_0_0_1_0       = OpConstantComposite %v4f32 %c_f32_0 %c_f32_0 %c_f32_1 %c_f32_0\n"
5454                     "%v4f32_0_5_0_5_0_5_1 = OpConstantComposite %v4f32 %c_f32_0_5 %c_f32_0_5 %c_f32_0_5 %c_f32_1\n"
5455                         "%cval                = OpConstantComposite %mat4x4_f32 %v4f32_1_0_0_0 %v4f32_0_1_0_0 %v4f32_0_0_1_0 %v4f32_0_5_0_5_0_5_1\n",
5456
5457                         "%transformed_param   = OpMatrixTimesVector %v4f32 %cval %param1\n"
5458                 },
5459                 {
5460                         "array",
5461
5462                         "%c_v4f32_1_1_1_0     = OpConstantComposite %v4f32 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_0\n"
5463                         "%fp_a4f32            = OpTypePointer Function %a4f32\n"
5464                         "%f32_n_1             = OpConstant %f32 -1.0\n"
5465                         "%f32_1_5             = OpConstant %f32 !0x3fc00000\n" // +1.5
5466                         "%carr                = OpConstantComposite %a4f32 %c_f32_0 %f32_n_1 %f32_1_5 %c_f32_0\n",
5467
5468                         "%v                   = OpVariable %fp_a4f32 Function %carr\n"
5469                         "%f                   = OpAccessChain %fp_f32 %v %c_u32_0\n"
5470                         "%f1                  = OpAccessChain %fp_f32 %v %c_u32_1\n"
5471                         "%f2                  = OpAccessChain %fp_f32 %v %c_u32_2\n"
5472                         "%f3                  = OpAccessChain %fp_f32 %v %c_u32_3\n"
5473                         "%f_val               = OpLoad %f32 %f\n"
5474                         "%f1_val              = OpLoad %f32 %f1\n"
5475                         "%f2_val              = OpLoad %f32 %f2\n"
5476                         "%f3_val              = OpLoad %f32 %f3\n"
5477                         "%ftot1               = OpFAdd %f32 %f_val %f1_val\n"
5478                         "%ftot2               = OpFAdd %f32 %ftot1 %f2_val\n"
5479                         "%ftot3               = OpFAdd %f32 %ftot2 %f3_val\n"  // 0 - 1 + 1.5 + 0
5480                         "%add_vec             = OpVectorTimesScalar %v4f32 %c_v4f32_1_1_1_0 %ftot3\n"
5481                         "%transformed_param   = OpFAdd %v4f32 %param1 %add_vec\n"
5482                 },
5483                 {
5484                         //
5485                         // [
5486                         //   {
5487                         //      0.0,
5488                         //      [ 1.0, 1.0, 1.0, 1.0]
5489                         //   },
5490                         //   {
5491                         //      1.0,
5492                         //      [ 0.0, 0.5, 0.0, 0.0]
5493                         //   }, //     ^^^
5494                         //   {
5495                         //      0.0,
5496                         //      [ 1.0, 1.0, 1.0, 1.0]
5497                         //   }
5498                         // ]
5499                         "array_of_struct_of_array",
5500
5501                         "%c_v4f32_1_1_1_0     = OpConstantComposite %v4f32 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_0\n"
5502                         "%fp_a4f32            = OpTypePointer Function %a4f32\n"
5503                         "%stype               = OpTypeStruct %f32 %a4f32\n"
5504                         "%a3stype             = OpTypeArray %stype %c_u32_3\n"
5505                         "%fp_a3stype          = OpTypePointer Function %a3stype\n"
5506                         "%ca4f32_0            = OpConstantComposite %a4f32 %c_f32_0 %c_f32_0_5 %c_f32_0 %c_f32_0\n"
5507                         "%ca4f32_1            = OpConstantComposite %a4f32 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_1\n"
5508                         "%cstype1             = OpConstantComposite %stype %c_f32_0 %ca4f32_1\n"
5509                         "%cstype2             = OpConstantComposite %stype %c_f32_1 %ca4f32_0\n"
5510                         "%carr                = OpConstantComposite %a3stype %cstype1 %cstype2 %cstype1",
5511
5512                         "%v                   = OpVariable %fp_a3stype Function %carr\n"
5513                         "%f                   = OpAccessChain %fp_f32 %v %c_u32_1 %c_u32_1 %c_u32_1\n"
5514                         "%f_l                 = OpLoad %f32 %f\n"
5515                         "%add_vec             = OpVectorTimesScalar %v4f32 %c_v4f32_1_1_1_0 %f_l\n"
5516                         "%transformed_param   = OpFAdd %v4f32 %param1 %add_vec\n"
5517                 }
5518         };
5519
5520         getHalfColorsFullAlpha(inputColors);
5521         outputColors[0] = RGBA(255, 255, 255, 255);
5522         outputColors[1] = RGBA(255, 127, 127, 255);
5523         outputColors[2] = RGBA(127, 255, 127, 255);
5524         outputColors[3] = RGBA(127, 127, 255, 255);
5525
5526         for (size_t testNdx = 0; testNdx < sizeof(tests) / sizeof(NameConstantsCode); ++testNdx)
5527         {
5528                 map<string, string> fragments;
5529                 fragments["pre_main"] = tests[testNdx].constants;
5530                 fragments["testfun"] = string(functionStart) + tests[testNdx].code + functionEnd;
5531                 createTestsForAllStages(tests[testNdx].name, inputColors, outputColors, fragments, opConstantCompositeTests.get());
5532         }
5533         return opConstantCompositeTests.release();
5534 }
5535
5536 tcu::TestCaseGroup* createSelectionBlockOrderTests(tcu::TestContext& testCtx)
5537 {
5538         de::MovePtr<tcu::TestCaseGroup> group                           (new tcu::TestCaseGroup(testCtx, "selection_block_order", "Out-of-order blocks for selection"));
5539         RGBA                                                    inputColors[4];
5540         RGBA                                                    outputColors[4];
5541         map<string, string>                             fragments;
5542
5543         // vec4 test_code(vec4 param) {
5544         //   vec4 result = param;
5545         //   for (int i = 0; i < 4; ++i) {
5546         //     if (i == 0) result[i] = 0.;
5547         //     else        result[i] = 1. - result[i];
5548         //   }
5549         //   return result;
5550         // }
5551         const char                                              function[]                      =
5552                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
5553                 "%param1    = OpFunctionParameter %v4f32\n"
5554                 "%lbl       = OpLabel\n"
5555                 "%iptr      = OpVariable %fp_i32 Function\n"
5556                 "%result    = OpVariable %fp_v4f32 Function\n"
5557                 "             OpStore %iptr %c_i32_0\n"
5558                 "             OpStore %result %param1\n"
5559                 "             OpBranch %loop\n"
5560
5561                 // Loop entry block.
5562                 "%loop      = OpLabel\n"
5563                 "%ival      = OpLoad %i32 %iptr\n"
5564                 "%lt_4      = OpSLessThan %bool %ival %c_i32_4\n"
5565                 "             OpLoopMerge %exit %if_entry None\n"
5566                 "             OpBranchConditional %lt_4 %if_entry %exit\n"
5567
5568                 // Merge block for loop.
5569                 "%exit      = OpLabel\n"
5570                 "%ret       = OpLoad %v4f32 %result\n"
5571                 "             OpReturnValue %ret\n"
5572
5573                 // If-statement entry block.
5574                 "%if_entry  = OpLabel\n"
5575                 "%loc       = OpAccessChain %fp_f32 %result %ival\n"
5576                 "%eq_0      = OpIEqual %bool %ival %c_i32_0\n"
5577                 "             OpSelectionMerge %if_exit None\n"
5578                 "             OpBranchConditional %eq_0 %if_true %if_false\n"
5579
5580                 // False branch for if-statement.
5581                 "%if_false  = OpLabel\n"
5582                 "%val       = OpLoad %f32 %loc\n"
5583                 "%sub       = OpFSub %f32 %c_f32_1 %val\n"
5584                 "             OpStore %loc %sub\n"
5585                 "             OpBranch %if_exit\n"
5586
5587                 // Merge block for if-statement.
5588                 "%if_exit   = OpLabel\n"
5589                 "%ival_next = OpIAdd %i32 %ival %c_i32_1\n"
5590                 "             OpStore %iptr %ival_next\n"
5591                 "             OpBranch %loop\n"
5592
5593                 // True branch for if-statement.
5594                 "%if_true   = OpLabel\n"
5595                 "             OpStore %loc %c_f32_0\n"
5596                 "             OpBranch %if_exit\n"
5597
5598                 "             OpFunctionEnd\n";
5599
5600         fragments["testfun"]    = function;
5601
5602         inputColors[0]                  = RGBA(127, 127, 127, 0);
5603         inputColors[1]                  = RGBA(127, 0,   0,   0);
5604         inputColors[2]                  = RGBA(0,   127, 0,   0);
5605         inputColors[3]                  = RGBA(0,   0,   127, 0);
5606
5607         outputColors[0]                 = RGBA(0, 128, 128, 255);
5608         outputColors[1]                 = RGBA(0, 255, 255, 255);
5609         outputColors[2]                 = RGBA(0, 128, 255, 255);
5610         outputColors[3]                 = RGBA(0, 255, 128, 255);
5611
5612         createTestsForAllStages("out_of_order", inputColors, outputColors, fragments, group.get());
5613
5614         return group.release();
5615 }
5616
5617 tcu::TestCaseGroup* createSwitchBlockOrderTests(tcu::TestContext& testCtx)
5618 {
5619         de::MovePtr<tcu::TestCaseGroup> group                           (new tcu::TestCaseGroup(testCtx, "switch_block_order", "Out-of-order blocks for switch"));
5620         RGBA                                                    inputColors[4];
5621         RGBA                                                    outputColors[4];
5622         map<string, string>                             fragments;
5623
5624         const char                                              typesAndConstants[]     =
5625                 "%c_f32_p2  = OpConstant %f32 0.2\n"
5626                 "%c_f32_p4  = OpConstant %f32 0.4\n"
5627                 "%c_f32_p6  = OpConstant %f32 0.6\n"
5628                 "%c_f32_p8  = OpConstant %f32 0.8\n";
5629
5630         // vec4 test_code(vec4 param) {
5631         //   vec4 result = param;
5632         //   for (int i = 0; i < 4; ++i) {
5633         //     switch (i) {
5634         //       case 0: result[i] += .2; break;
5635         //       case 1: result[i] += .6; break;
5636         //       case 2: result[i] += .4; break;
5637         //       case 3: result[i] += .8; break;
5638         //       default: break; // unreachable
5639         //     }
5640         //   }
5641         //   return result;
5642         // }
5643         const char                                              function[]                      =
5644                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
5645                 "%param1    = OpFunctionParameter %v4f32\n"
5646                 "%lbl       = OpLabel\n"
5647                 "%iptr      = OpVariable %fp_i32 Function\n"
5648                 "%result    = OpVariable %fp_v4f32 Function\n"
5649                 "             OpStore %iptr %c_i32_0\n"
5650                 "             OpStore %result %param1\n"
5651                 "             OpBranch %loop\n"
5652
5653                 // Loop entry block.
5654                 "%loop      = OpLabel\n"
5655                 "%ival      = OpLoad %i32 %iptr\n"
5656                 "%lt_4      = OpSLessThan %bool %ival %c_i32_4\n"
5657                 "             OpLoopMerge %exit %switch_exit None\n"
5658                 "             OpBranchConditional %lt_4 %switch_entry %exit\n"
5659
5660                 // Merge block for loop.
5661                 "%exit      = OpLabel\n"
5662                 "%ret       = OpLoad %v4f32 %result\n"
5663                 "             OpReturnValue %ret\n"
5664
5665                 // Switch-statement entry block.
5666                 "%switch_entry   = OpLabel\n"
5667                 "%loc            = OpAccessChain %fp_f32 %result %ival\n"
5668                 "%val            = OpLoad %f32 %loc\n"
5669                 "                  OpSelectionMerge %switch_exit None\n"
5670                 "                  OpSwitch %ival %switch_default 0 %case0 1 %case1 2 %case2 3 %case3\n"
5671
5672                 "%case2          = OpLabel\n"
5673                 "%addp4          = OpFAdd %f32 %val %c_f32_p4\n"
5674                 "                  OpStore %loc %addp4\n"
5675                 "                  OpBranch %switch_exit\n"
5676
5677                 "%switch_default = OpLabel\n"
5678                 "                  OpUnreachable\n"
5679
5680                 "%case3          = OpLabel\n"
5681                 "%addp8          = OpFAdd %f32 %val %c_f32_p8\n"
5682                 "                  OpStore %loc %addp8\n"
5683                 "                  OpBranch %switch_exit\n"
5684
5685                 "%case0          = OpLabel\n"
5686                 "%addp2          = OpFAdd %f32 %val %c_f32_p2\n"
5687                 "                  OpStore %loc %addp2\n"
5688                 "                  OpBranch %switch_exit\n"
5689
5690                 // Merge block for switch-statement.
5691                 "%switch_exit    = OpLabel\n"
5692                 "%ival_next      = OpIAdd %i32 %ival %c_i32_1\n"
5693                 "                  OpStore %iptr %ival_next\n"
5694                 "                  OpBranch %loop\n"
5695
5696                 "%case1          = OpLabel\n"
5697                 "%addp6          = OpFAdd %f32 %val %c_f32_p6\n"
5698                 "                  OpStore %loc %addp6\n"
5699                 "                  OpBranch %switch_exit\n"
5700
5701                 "                  OpFunctionEnd\n";
5702
5703         fragments["pre_main"]   = typesAndConstants;
5704         fragments["testfun"]    = function;
5705
5706         inputColors[0]                  = RGBA(127, 27,  127, 51);
5707         inputColors[1]                  = RGBA(127, 0,   0,   51);
5708         inputColors[2]                  = RGBA(0,   27,  0,   51);
5709         inputColors[3]                  = RGBA(0,   0,   127, 51);
5710
5711         outputColors[0]                 = RGBA(178, 180, 229, 255);
5712         outputColors[1]                 = RGBA(178, 153, 102, 255);
5713         outputColors[2]                 = RGBA(51,  180, 102, 255);
5714         outputColors[3]                 = RGBA(51,  153, 229, 255);
5715
5716         createTestsForAllStages("out_of_order", inputColors, outputColors, fragments, group.get());
5717
5718         return group.release();
5719 }
5720
5721 tcu::TestCaseGroup* createDecorationGroupTests(tcu::TestContext& testCtx)
5722 {
5723         de::MovePtr<tcu::TestCaseGroup> group                           (new tcu::TestCaseGroup(testCtx, "decoration_group", "Decoration group tests"));
5724         RGBA                                                    inputColors[4];
5725         RGBA                                                    outputColors[4];
5726         map<string, string>                             fragments;
5727
5728         const char                                              decorations[]           =
5729                 "OpDecorate %array_group         ArrayStride 4\n"
5730                 "OpDecorate %struct_member_group Offset 0\n"
5731                 "%array_group         = OpDecorationGroup\n"
5732                 "%struct_member_group = OpDecorationGroup\n"
5733
5734                 "OpDecorate %group1 RelaxedPrecision\n"
5735                 "OpDecorate %group3 RelaxedPrecision\n"
5736                 "OpDecorate %group3 Invariant\n"
5737                 "OpDecorate %group3 Restrict\n"
5738                 "%group0 = OpDecorationGroup\n"
5739                 "%group1 = OpDecorationGroup\n"
5740                 "%group3 = OpDecorationGroup\n";
5741
5742         const char                                              typesAndConstants[]     =
5743                 "%a3f32     = OpTypeArray %f32 %c_u32_3\n"
5744                 "%struct1   = OpTypeStruct %a3f32\n"
5745                 "%struct2   = OpTypeStruct %a3f32\n"
5746                 "%fp_struct1 = OpTypePointer Function %struct1\n"
5747                 "%fp_struct2 = OpTypePointer Function %struct2\n"
5748                 "%c_f32_2    = OpConstant %f32 2.\n"
5749                 "%c_f32_n2   = OpConstant %f32 -2.\n"
5750
5751                 "%c_a3f32_1 = OpConstantComposite %a3f32 %c_f32_1 %c_f32_2 %c_f32_1\n"
5752                 "%c_a3f32_2 = OpConstantComposite %a3f32 %c_f32_n1 %c_f32_n2 %c_f32_n1\n"
5753                 "%c_struct1 = OpConstantComposite %struct1 %c_a3f32_1\n"
5754                 "%c_struct2 = OpConstantComposite %struct2 %c_a3f32_2\n";
5755
5756         const char                                              function[]                      =
5757                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
5758                 "%param     = OpFunctionParameter %v4f32\n"
5759                 "%entry     = OpLabel\n"
5760                 "%result    = OpVariable %fp_v4f32 Function\n"
5761                 "%v_struct1 = OpVariable %fp_struct1 Function\n"
5762                 "%v_struct2 = OpVariable %fp_struct2 Function\n"
5763                 "             OpStore %result %param\n"
5764                 "             OpStore %v_struct1 %c_struct1\n"
5765                 "             OpStore %v_struct2 %c_struct2\n"
5766                 "%ptr1      = OpAccessChain %fp_f32 %v_struct1 %c_i32_0 %c_i32_2\n"
5767                 "%val1      = OpLoad %f32 %ptr1\n"
5768                 "%ptr2      = OpAccessChain %fp_f32 %v_struct2 %c_i32_0 %c_i32_2\n"
5769                 "%val2      = OpLoad %f32 %ptr2\n"
5770                 "%addvalues = OpFAdd %f32 %val1 %val2\n"
5771                 "%ptr       = OpAccessChain %fp_f32 %result %c_i32_1\n"
5772                 "%val       = OpLoad %f32 %ptr\n"
5773                 "%addresult = OpFAdd %f32 %addvalues %val\n"
5774                 "             OpStore %ptr %addresult\n"
5775                 "%ret       = OpLoad %v4f32 %result\n"
5776                 "             OpReturnValue %ret\n"
5777                 "             OpFunctionEnd\n";
5778
5779         struct CaseNameDecoration
5780         {
5781                 string name;
5782                 string decoration;
5783         };
5784
5785         CaseNameDecoration tests[] =
5786         {
5787                 {
5788                         "same_decoration_group_on_multiple_types",
5789                         "OpGroupMemberDecorate %struct_member_group %struct1 0 %struct2 0\n"
5790                 },
5791                 {
5792                         "empty_decoration_group",
5793                         "OpGroupDecorate %group0      %a3f32\n"
5794                         "OpGroupDecorate %group0      %result\n"
5795                 },
5796                 {
5797                         "one_element_decoration_group",
5798                         "OpGroupDecorate %array_group %a3f32\n"
5799                 },
5800                 {
5801                         "multiple_elements_decoration_group",
5802                         "OpGroupDecorate %group3      %v_struct1\n"
5803                 },
5804                 {
5805                         "multiple_decoration_groups_on_same_variable",
5806                         "OpGroupDecorate %group0      %v_struct2\n"
5807                         "OpGroupDecorate %group1      %v_struct2\n"
5808                         "OpGroupDecorate %group3      %v_struct2\n"
5809                 },
5810                 {
5811                         "same_decoration_group_multiple_times",
5812                         "OpGroupDecorate %group1      %addvalues\n"
5813                         "OpGroupDecorate %group1      %addvalues\n"
5814                         "OpGroupDecorate %group1      %addvalues\n"
5815                 },
5816
5817         };
5818
5819         getHalfColorsFullAlpha(inputColors);
5820         getHalfColorsFullAlpha(outputColors);
5821
5822         for (size_t idx = 0; idx < (sizeof(tests) / sizeof(tests[0])); ++idx)
5823         {
5824                 fragments["decoration"] = decorations + tests[idx].decoration;
5825                 fragments["pre_main"]   = typesAndConstants;
5826                 fragments["testfun"]    = function;
5827
5828                 createTestsForAllStages(tests[idx].name, inputColors, outputColors, fragments, group.get());
5829         }
5830
5831         return group.release();
5832 }
5833
5834 struct SpecConstantTwoIntGraphicsCase
5835 {
5836         const char*             caseName;
5837         const char*             scDefinition0;
5838         const char*             scDefinition1;
5839         const char*             scResultType;
5840         const char*             scOperation;
5841         deInt32                 scActualValue0;
5842         deInt32                 scActualValue1;
5843         const char*             resultOperation;
5844         RGBA                    expectedColors[4];
5845
5846                                         SpecConstantTwoIntGraphicsCase (const char* name,
5847                                                                                         const char* definition0,
5848                                                                                         const char* definition1,
5849                                                                                         const char* resultType,
5850                                                                                         const char* operation,
5851                                                                                         deInt32         value0,
5852                                                                                         deInt32         value1,
5853                                                                                         const char* resultOp,
5854                                                                                         const RGBA      (&output)[4])
5855                                                 : caseName                      (name)
5856                                                 , scDefinition0         (definition0)
5857                                                 , scDefinition1         (definition1)
5858                                                 , scResultType          (resultType)
5859                                                 , scOperation           (operation)
5860                                                 , scActualValue0        (value0)
5861                                                 , scActualValue1        (value1)
5862                                                 , resultOperation       (resultOp)
5863         {
5864                 expectedColors[0] = output[0];
5865                 expectedColors[1] = output[1];
5866                 expectedColors[2] = output[2];
5867                 expectedColors[3] = output[3];
5868         }
5869 };
5870
5871 tcu::TestCaseGroup* createSpecConstantTests (tcu::TestContext& testCtx)
5872 {
5873         de::MovePtr<tcu::TestCaseGroup> group                           (new tcu::TestCaseGroup(testCtx, "opspecconstantop", "Test the OpSpecConstantOp instruction"));
5874         vector<SpecConstantTwoIntGraphicsCase>  cases;
5875         RGBA                                                    inputColors[4];
5876         RGBA                                                    outputColors0[4];
5877         RGBA                                                    outputColors1[4];
5878         RGBA                                                    outputColors2[4];
5879
5880         const char      decorations1[]                  =
5881                 "OpDecorate %sc_0  SpecId 0\n"
5882                 "OpDecorate %sc_1  SpecId 1\n";
5883
5884         const char      typesAndConstants1[]    =
5885                 "%sc_0      = OpSpecConstant${SC_DEF0}\n"
5886                 "%sc_1      = OpSpecConstant${SC_DEF1}\n"
5887                 "%sc_op     = OpSpecConstantOp ${SC_RESULT_TYPE} ${SC_OP}\n";
5888
5889         const char      function1[]                             =
5890                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
5891                 "%param     = OpFunctionParameter %v4f32\n"
5892                 "%label     = OpLabel\n"
5893                 "%result    = OpVariable %fp_v4f32 Function\n"
5894                 "             OpStore %result %param\n"
5895                 "%gen       = ${GEN_RESULT}\n"
5896                 "%index     = OpIAdd %i32 %gen %c_i32_1\n"
5897                 "%loc       = OpAccessChain %fp_f32 %result %index\n"
5898                 "%val       = OpLoad %f32 %loc\n"
5899                 "%add       = OpFAdd %f32 %val %c_f32_0_5\n"
5900                 "             OpStore %loc %add\n"
5901                 "%ret       = OpLoad %v4f32 %result\n"
5902                 "             OpReturnValue %ret\n"
5903                 "             OpFunctionEnd\n";
5904
5905         inputColors[0] = RGBA(127, 127, 127, 255);
5906         inputColors[1] = RGBA(127, 0,   0,   255);
5907         inputColors[2] = RGBA(0,   127, 0,   255);
5908         inputColors[3] = RGBA(0,   0,   127, 255);
5909
5910         // Derived from inputColors[x] by adding 128 to inputColors[x][0].
5911         outputColors0[0] = RGBA(255, 127, 127, 255);
5912         outputColors0[1] = RGBA(255, 0,   0,   255);
5913         outputColors0[2] = RGBA(128, 127, 0,   255);
5914         outputColors0[3] = RGBA(128, 0,   127, 255);
5915
5916         // Derived from inputColors[x] by adding 128 to inputColors[x][1].
5917         outputColors1[0] = RGBA(127, 255, 127, 255);
5918         outputColors1[1] = RGBA(127, 128, 0,   255);
5919         outputColors1[2] = RGBA(0,   255, 0,   255);
5920         outputColors1[3] = RGBA(0,   128, 127, 255);
5921
5922         // Derived from inputColors[x] by adding 128 to inputColors[x][2].
5923         outputColors2[0] = RGBA(127, 127, 255, 255);
5924         outputColors2[1] = RGBA(127, 0,   128, 255);
5925         outputColors2[2] = RGBA(0,   127, 128, 255);
5926         outputColors2[3] = RGBA(0,   0,   255, 255);
5927
5928         const char addZeroToSc[]                = "OpIAdd %i32 %c_i32_0 %sc_op";
5929         const char selectTrueUsingSc[]  = "OpSelect %i32 %sc_op %c_i32_1 %c_i32_0";
5930         const char selectFalseUsingSc[] = "OpSelect %i32 %sc_op %c_i32_0 %c_i32_1";
5931
5932         cases.push_back(SpecConstantTwoIntGraphicsCase("iadd",                                  " %i32 0",              " %i32 0",              "%i32",         "IAdd                 %sc_0 %sc_1",                             19,             -20,    addZeroToSc,            outputColors0));
5933         cases.push_back(SpecConstantTwoIntGraphicsCase("isub",                                  " %i32 0",              " %i32 0",              "%i32",         "ISub                 %sc_0 %sc_1",                             19,             20,             addZeroToSc,            outputColors0));
5934         cases.push_back(SpecConstantTwoIntGraphicsCase("imul",                                  " %i32 0",              " %i32 0",              "%i32",         "IMul                 %sc_0 %sc_1",                             -1,             -1,             addZeroToSc,            outputColors2));
5935         cases.push_back(SpecConstantTwoIntGraphicsCase("sdiv",                                  " %i32 0",              " %i32 0",              "%i32",         "SDiv                 %sc_0 %sc_1",                             -126,   126,    addZeroToSc,            outputColors0));
5936         cases.push_back(SpecConstantTwoIntGraphicsCase("udiv",                                  " %i32 0",              " %i32 0",              "%i32",         "UDiv                 %sc_0 %sc_1",                             126,    126,    addZeroToSc,            outputColors2));
5937         cases.push_back(SpecConstantTwoIntGraphicsCase("srem",                                  " %i32 0",              " %i32 0",              "%i32",         "SRem                 %sc_0 %sc_1",                             3,              2,              addZeroToSc,            outputColors2));
5938         cases.push_back(SpecConstantTwoIntGraphicsCase("smod",                                  " %i32 0",              " %i32 0",              "%i32",         "SMod                 %sc_0 %sc_1",                             3,              2,              addZeroToSc,            outputColors2));
5939         cases.push_back(SpecConstantTwoIntGraphicsCase("umod",                                  " %i32 0",              " %i32 0",              "%i32",         "UMod                 %sc_0 %sc_1",                             1001,   500,    addZeroToSc,            outputColors2));
5940         cases.push_back(SpecConstantTwoIntGraphicsCase("bitwiseand",                    " %i32 0",              " %i32 0",              "%i32",         "BitwiseAnd           %sc_0 %sc_1",                             0x33,   0x0d,   addZeroToSc,            outputColors2));
5941         cases.push_back(SpecConstantTwoIntGraphicsCase("bitwiseor",                             " %i32 0",              " %i32 0",              "%i32",         "BitwiseOr            %sc_0 %sc_1",                             0,              1,              addZeroToSc,            outputColors2));
5942         cases.push_back(SpecConstantTwoIntGraphicsCase("bitwisexor",                    " %i32 0",              " %i32 0",              "%i32",         "BitwiseXor           %sc_0 %sc_1",                             0x2e,   0x2f,   addZeroToSc,            outputColors2));
5943         cases.push_back(SpecConstantTwoIntGraphicsCase("shiftrightlogical",             " %i32 0",              " %i32 0",              "%i32",         "ShiftRightLogical    %sc_0 %sc_1",                             2,              1,              addZeroToSc,            outputColors2));
5944         cases.push_back(SpecConstantTwoIntGraphicsCase("shiftrightarithmetic",  " %i32 0",              " %i32 0",              "%i32",         "ShiftRightArithmetic %sc_0 %sc_1",                             -4,             2,              addZeroToSc,            outputColors0));
5945         cases.push_back(SpecConstantTwoIntGraphicsCase("shiftleftlogical",              " %i32 0",              " %i32 0",              "%i32",         "ShiftLeftLogical     %sc_0 %sc_1",                             1,              0,              addZeroToSc,            outputColors2));
5946         cases.push_back(SpecConstantTwoIntGraphicsCase("slessthan",                             " %i32 0",              " %i32 0",              "%bool",        "SLessThan            %sc_0 %sc_1",                             -20,    -10,    selectTrueUsingSc,      outputColors2));
5947         cases.push_back(SpecConstantTwoIntGraphicsCase("ulessthan",                             " %i32 0",              " %i32 0",              "%bool",        "ULessThan            %sc_0 %sc_1",                             10,             20,             selectTrueUsingSc,      outputColors2));
5948         cases.push_back(SpecConstantTwoIntGraphicsCase("sgreaterthan",                  " %i32 0",              " %i32 0",              "%bool",        "SGreaterThan         %sc_0 %sc_1",                             -1000,  50,             selectFalseUsingSc,     outputColors2));
5949         cases.push_back(SpecConstantTwoIntGraphicsCase("ugreaterthan",                  " %i32 0",              " %i32 0",              "%bool",        "UGreaterThan         %sc_0 %sc_1",                             10,             5,              selectTrueUsingSc,      outputColors2));
5950         cases.push_back(SpecConstantTwoIntGraphicsCase("slessthanequal",                " %i32 0",              " %i32 0",              "%bool",        "SLessThanEqual       %sc_0 %sc_1",                             -10,    -10,    selectTrueUsingSc,      outputColors2));
5951         cases.push_back(SpecConstantTwoIntGraphicsCase("ulessthanequal",                " %i32 0",              " %i32 0",              "%bool",        "ULessThanEqual       %sc_0 %sc_1",                             50,             100,    selectTrueUsingSc,      outputColors2));
5952         cases.push_back(SpecConstantTwoIntGraphicsCase("sgreaterthanequal",             " %i32 0",              " %i32 0",              "%bool",        "SGreaterThanEqual    %sc_0 %sc_1",                             -1000,  50,             selectFalseUsingSc,     outputColors2));
5953         cases.push_back(SpecConstantTwoIntGraphicsCase("ugreaterthanequal",             " %i32 0",              " %i32 0",              "%bool",        "UGreaterThanEqual    %sc_0 %sc_1",                             10,             10,             selectTrueUsingSc,      outputColors2));
5954         cases.push_back(SpecConstantTwoIntGraphicsCase("iequal",                                " %i32 0",              " %i32 0",              "%bool",        "IEqual               %sc_0 %sc_1",                             42,             24,             selectFalseUsingSc,     outputColors2));
5955         cases.push_back(SpecConstantTwoIntGraphicsCase("logicaland",                    "True %bool",   "True %bool",   "%bool",        "LogicalAnd           %sc_0 %sc_1",                             0,              1,              selectFalseUsingSc,     outputColors2));
5956         cases.push_back(SpecConstantTwoIntGraphicsCase("logicalor",                             "False %bool",  "False %bool",  "%bool",        "LogicalOr            %sc_0 %sc_1",                             1,              0,              selectTrueUsingSc,      outputColors2));
5957         cases.push_back(SpecConstantTwoIntGraphicsCase("logicalequal",                  "True %bool",   "True %bool",   "%bool",        "LogicalEqual         %sc_0 %sc_1",                             0,              1,              selectFalseUsingSc,     outputColors2));
5958         cases.push_back(SpecConstantTwoIntGraphicsCase("logicalnotequal",               "False %bool",  "False %bool",  "%bool",        "LogicalNotEqual      %sc_0 %sc_1",                             1,              0,              selectTrueUsingSc,      outputColors2));
5959         cases.push_back(SpecConstantTwoIntGraphicsCase("snegate",                               " %i32 0",              " %i32 0",              "%i32",         "SNegate              %sc_0",                                   -1,             0,              addZeroToSc,            outputColors2));
5960         cases.push_back(SpecConstantTwoIntGraphicsCase("not",                                   " %i32 0",              " %i32 0",              "%i32",         "Not                  %sc_0",                                   -2,             0,              addZeroToSc,            outputColors2));
5961         cases.push_back(SpecConstantTwoIntGraphicsCase("logicalnot",                    "False %bool",  "False %bool",  "%bool",        "LogicalNot           %sc_0",                                   1,              0,              selectFalseUsingSc,     outputColors2));
5962         cases.push_back(SpecConstantTwoIntGraphicsCase("select",                                "False %bool",  " %i32 0",              "%i32",         "Select               %sc_0 %sc_1 %c_i32_0",    1,              1,              addZeroToSc,            outputColors2));
5963         // OpSConvert, OpFConvert: these two instructions involve ints/floats of different bitwidths.
5964         // \todo[2015-12-1 antiagainst] OpQuantizeToF16
5965
5966         for (size_t caseNdx = 0; caseNdx < cases.size(); ++caseNdx)
5967         {
5968                 map<string, string>     specializations;
5969                 map<string, string>     fragments;
5970                 vector<deInt32>         specConstants;
5971
5972                 specializations["SC_DEF0"]                      = cases[caseNdx].scDefinition0;
5973                 specializations["SC_DEF1"]                      = cases[caseNdx].scDefinition1;
5974                 specializations["SC_RESULT_TYPE"]       = cases[caseNdx].scResultType;
5975                 specializations["SC_OP"]                        = cases[caseNdx].scOperation;
5976                 specializations["GEN_RESULT"]           = cases[caseNdx].resultOperation;
5977
5978                 fragments["decoration"]                         = tcu::StringTemplate(decorations1).specialize(specializations);
5979                 fragments["pre_main"]                           = tcu::StringTemplate(typesAndConstants1).specialize(specializations);
5980                 fragments["testfun"]                            = tcu::StringTemplate(function1).specialize(specializations);
5981
5982                 specConstants.push_back(cases[caseNdx].scActualValue0);
5983                 specConstants.push_back(cases[caseNdx].scActualValue1);
5984
5985                 createTestsForAllStages(cases[caseNdx].caseName, inputColors, cases[caseNdx].expectedColors, fragments, specConstants, group.get());
5986         }
5987
5988         const char      decorations2[]                  =
5989                 "OpDecorate %sc_0  SpecId 0\n"
5990                 "OpDecorate %sc_1  SpecId 1\n"
5991                 "OpDecorate %sc_2  SpecId 2\n";
5992
5993         const char      typesAndConstants2[]    =
5994                 "%v3i32       = OpTypeVector %i32 3\n"
5995                 "%vec3_0      = OpConstantComposite %v3i32 %c_i32_0 %c_i32_0 %c_i32_0\n"
5996                 "%vec3_undef  = OpUndef %v3i32\n"
5997
5998                 "%sc_0        = OpSpecConstant %i32 0\n"
5999                 "%sc_1        = OpSpecConstant %i32 0\n"
6000                 "%sc_2        = OpSpecConstant %i32 0\n"
6001                 "%sc_vec3_0   = OpSpecConstantOp %v3i32 CompositeInsert  %sc_0        %vec3_0      0\n"                                                 // (sc_0, 0,    0)
6002                 "%sc_vec3_1   = OpSpecConstantOp %v3i32 CompositeInsert  %sc_1        %vec3_0      1\n"                                                 // (0,    sc_1, 0)
6003                 "%sc_vec3_2   = OpSpecConstantOp %v3i32 CompositeInsert  %sc_2        %vec3_0      2\n"                                                 // (0,    0,    sc_2)
6004                 "%sc_vec3_0_s = OpSpecConstantOp %v3i32 VectorShuffle    %sc_vec3_0   %vec3_undef  0          0xFFFFFFFF 2\n"   // (sc_0, ???,  0)
6005                 "%sc_vec3_1_s = OpSpecConstantOp %v3i32 VectorShuffle    %sc_vec3_1   %vec3_undef  0xFFFFFFFF 1          0\n"   // (???,  sc_1, 0)
6006                 "%sc_vec3_2_s = OpSpecConstantOp %v3i32 VectorShuffle    %vec3_undef  %sc_vec3_2   5          0xFFFFFFFF 5\n"   // (sc_2, ???,  sc_2)
6007                 "%sc_vec3_01  = OpSpecConstantOp %v3i32 VectorShuffle    %sc_vec3_0_s %sc_vec3_1_s 1 0 4\n"                                             // (0,    sc_0, sc_1)
6008                 "%sc_vec3_012 = OpSpecConstantOp %v3i32 VectorShuffle    %sc_vec3_01  %sc_vec3_2_s 5 1 2\n"                                             // (sc_2, sc_0, sc_1)
6009                 "%sc_ext_0    = OpSpecConstantOp %i32   CompositeExtract %sc_vec3_012              0\n"                                                 // sc_2
6010                 "%sc_ext_1    = OpSpecConstantOp %i32   CompositeExtract %sc_vec3_012              1\n"                                                 // sc_0
6011                 "%sc_ext_2    = OpSpecConstantOp %i32   CompositeExtract %sc_vec3_012              2\n"                                                 // sc_1
6012                 "%sc_sub      = OpSpecConstantOp %i32   ISub             %sc_ext_0    %sc_ext_1\n"                                                              // (sc_2 - sc_0)
6013                 "%sc_final    = OpSpecConstantOp %i32   IMul             %sc_sub      %sc_ext_2\n";                                                             // (sc_2 - sc_0) * sc_1
6014
6015         const char      function2[]                             =
6016                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6017                 "%param     = OpFunctionParameter %v4f32\n"
6018                 "%label     = OpLabel\n"
6019                 "%result    = OpVariable %fp_v4f32 Function\n"
6020                 "             OpStore %result %param\n"
6021                 "%loc       = OpAccessChain %fp_f32 %result %sc_final\n"
6022                 "%val       = OpLoad %f32 %loc\n"
6023                 "%add       = OpFAdd %f32 %val %c_f32_0_5\n"
6024                 "             OpStore %loc %add\n"
6025                 "%ret       = OpLoad %v4f32 %result\n"
6026                 "             OpReturnValue %ret\n"
6027                 "             OpFunctionEnd\n";
6028
6029         map<string, string>     fragments;
6030         vector<deInt32>         specConstants;
6031
6032         fragments["decoration"] = decorations2;
6033         fragments["pre_main"]   = typesAndConstants2;
6034         fragments["testfun"]    = function2;
6035
6036         specConstants.push_back(56789);
6037         specConstants.push_back(-2);
6038         specConstants.push_back(56788);
6039
6040         createTestsForAllStages("vector_related", inputColors, outputColors2, fragments, specConstants, group.get());
6041
6042         return group.release();
6043 }
6044
6045 tcu::TestCaseGroup* createOpPhiTests(tcu::TestContext& testCtx)
6046 {
6047         de::MovePtr<tcu::TestCaseGroup> group                           (new tcu::TestCaseGroup(testCtx, "opphi", "Test the OpPhi instruction"));
6048         RGBA                                                    inputColors[4];
6049         RGBA                                                    outputColors1[4];
6050         RGBA                                                    outputColors2[4];
6051         RGBA                                                    outputColors3[4];
6052         map<string, string>                             fragments1;
6053         map<string, string>                             fragments2;
6054         map<string, string>                             fragments3;
6055
6056         const char      typesAndConstants1[]    =
6057                 "%c_f32_p2  = OpConstant %f32 0.2\n"
6058                 "%c_f32_p4  = OpConstant %f32 0.4\n"
6059                 "%c_f32_p5  = OpConstant %f32 0.5\n"
6060                 "%c_f32_p8  = OpConstant %f32 0.8\n";
6061
6062         // vec4 test_code(vec4 param) {
6063         //   vec4 result = param;
6064         //   for (int i = 0; i < 4; ++i) {
6065         //     float operand;
6066         //     switch (i) {
6067         //       case 0: operand = .2; break;
6068         //       case 1: operand = .5; break;
6069         //       case 2: operand = .4; break;
6070         //       case 3: operand = .0; break;
6071         //       default: break; // unreachable
6072         //     }
6073         //     result[i] += operand;
6074         //   }
6075         //   return result;
6076         // }
6077         const char      function1[]                             =
6078                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6079                 "%param1    = OpFunctionParameter %v4f32\n"
6080                 "%lbl       = OpLabel\n"
6081                 "%iptr      = OpVariable %fp_i32 Function\n"
6082                 "%result    = OpVariable %fp_v4f32 Function\n"
6083                 "             OpStore %iptr %c_i32_0\n"
6084                 "             OpStore %result %param1\n"
6085                 "             OpBranch %loop\n"
6086
6087                 "%loop      = OpLabel\n"
6088                 "%ival      = OpLoad %i32 %iptr\n"
6089                 "%lt_4      = OpSLessThan %bool %ival %c_i32_4\n"
6090                 "             OpLoopMerge %exit %phi None\n"
6091                 "             OpBranchConditional %lt_4 %entry %exit\n"
6092
6093                 "%entry     = OpLabel\n"
6094                 "%loc       = OpAccessChain %fp_f32 %result %ival\n"
6095                 "%val       = OpLoad %f32 %loc\n"
6096                 "             OpSelectionMerge %phi None\n"
6097                 "             OpSwitch %ival %default 0 %case0 1 %case1 2 %case2 3 %case3\n"
6098
6099                 "%case0     = OpLabel\n"
6100                 "             OpBranch %phi\n"
6101                 "%case1     = OpLabel\n"
6102                 "             OpBranch %phi\n"
6103                 "%case2     = OpLabel\n"
6104                 "             OpBranch %phi\n"
6105                 "%case3     = OpLabel\n"
6106                 "             OpBranch %phi\n"
6107
6108                 "%default   = OpLabel\n"
6109                 "             OpUnreachable\n"
6110
6111                 "%phi       = OpLabel\n"
6112                 "%operand   = OpPhi %f32 %c_f32_p4 %case2 %c_f32_p5 %case1 %c_f32_p2 %case0 %c_f32_0 %case3\n" // not in the order of blocks
6113                 "%add       = OpFAdd %f32 %val %operand\n"
6114                 "             OpStore %loc %add\n"
6115                 "%ival_next = OpIAdd %i32 %ival %c_i32_1\n"
6116                 "             OpStore %iptr %ival_next\n"
6117                 "             OpBranch %loop\n"
6118
6119                 "%exit      = OpLabel\n"
6120                 "%ret       = OpLoad %v4f32 %result\n"
6121                 "             OpReturnValue %ret\n"
6122
6123                 "             OpFunctionEnd\n";
6124
6125         fragments1["pre_main"]  = typesAndConstants1;
6126         fragments1["testfun"]   = function1;
6127
6128         getHalfColorsFullAlpha(inputColors);
6129
6130         outputColors1[0]                = RGBA(178, 255, 229, 255);
6131         outputColors1[1]                = RGBA(178, 127, 102, 255);
6132         outputColors1[2]                = RGBA(51,  255, 102, 255);
6133         outputColors1[3]                = RGBA(51,  127, 229, 255);
6134
6135         createTestsForAllStages("out_of_order", inputColors, outputColors1, fragments1, group.get());
6136
6137         const char      typesAndConstants2[]    =
6138                 "%c_f32_p2  = OpConstant %f32 0.2\n";
6139
6140         // Add .4 to the second element of the given parameter.
6141         const char      function2[]                             =
6142                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6143                 "%param     = OpFunctionParameter %v4f32\n"
6144                 "%entry     = OpLabel\n"
6145                 "%result    = OpVariable %fp_v4f32 Function\n"
6146                 "             OpStore %result %param\n"
6147                 "%loc       = OpAccessChain %fp_f32 %result %c_i32_1\n"
6148                 "%val       = OpLoad %f32 %loc\n"
6149                 "             OpBranch %phi\n"
6150
6151                 "%phi        = OpLabel\n"
6152                 "%step       = OpPhi %i32 %c_i32_0  %entry %step_next  %phi\n"
6153                 "%accum      = OpPhi %f32 %val      %entry %accum_next %phi\n"
6154                 "%step_next  = OpIAdd %i32 %step  %c_i32_1\n"
6155                 "%accum_next = OpFAdd %f32 %accum %c_f32_p2\n"
6156                 "%still_loop = OpSLessThan %bool %step %c_i32_2\n"
6157                 "              OpLoopMerge %exit %phi None\n"
6158                 "              OpBranchConditional %still_loop %phi %exit\n"
6159
6160                 "%exit       = OpLabel\n"
6161                 "              OpStore %loc %accum\n"
6162                 "%ret        = OpLoad %v4f32 %result\n"
6163                 "              OpReturnValue %ret\n"
6164
6165                 "              OpFunctionEnd\n";
6166
6167         fragments2["pre_main"]  = typesAndConstants2;
6168         fragments2["testfun"]   = function2;
6169
6170         outputColors2[0]                        = RGBA(127, 229, 127, 255);
6171         outputColors2[1]                        = RGBA(127, 102, 0,   255);
6172         outputColors2[2]                        = RGBA(0,   229, 0,   255);
6173         outputColors2[3]                        = RGBA(0,   102, 127, 255);
6174
6175         createTestsForAllStages("induction", inputColors, outputColors2, fragments2, group.get());
6176
6177         const char      typesAndConstants3[]    =
6178                 "%true      = OpConstantTrue %bool\n"
6179                 "%false     = OpConstantFalse %bool\n"
6180                 "%c_f32_p2  = OpConstant %f32 0.2\n";
6181
6182         // Swap the second and the third element of the given parameter.
6183         const char      function3[]                             =
6184                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6185                 "%param     = OpFunctionParameter %v4f32\n"
6186                 "%entry     = OpLabel\n"
6187                 "%result    = OpVariable %fp_v4f32 Function\n"
6188                 "             OpStore %result %param\n"
6189                 "%a_loc     = OpAccessChain %fp_f32 %result %c_i32_1\n"
6190                 "%a_init    = OpLoad %f32 %a_loc\n"
6191                 "%b_loc     = OpAccessChain %fp_f32 %result %c_i32_2\n"
6192                 "%b_init    = OpLoad %f32 %b_loc\n"
6193                 "             OpBranch %phi\n"
6194
6195                 "%phi        = OpLabel\n"
6196                 "%still_loop = OpPhi %bool %true   %entry %false  %phi\n"
6197                 "%a_next     = OpPhi %f32  %a_init %entry %b_next %phi\n"
6198                 "%b_next     = OpPhi %f32  %b_init %entry %a_next %phi\n"
6199                 "              OpLoopMerge %exit %phi None\n"
6200                 "              OpBranchConditional %still_loop %phi %exit\n"
6201
6202                 "%exit       = OpLabel\n"
6203                 "              OpStore %a_loc %a_next\n"
6204                 "              OpStore %b_loc %b_next\n"
6205                 "%ret        = OpLoad %v4f32 %result\n"
6206                 "              OpReturnValue %ret\n"
6207
6208                 "              OpFunctionEnd\n";
6209
6210         fragments3["pre_main"]  = typesAndConstants3;
6211         fragments3["testfun"]   = function3;
6212
6213         outputColors3[0]                        = RGBA(127, 127, 127, 255);
6214         outputColors3[1]                        = RGBA(127, 0,   0,   255);
6215         outputColors3[2]                        = RGBA(0,   0,   127, 255);
6216         outputColors3[3]                        = RGBA(0,   127, 0,   255);
6217
6218         createTestsForAllStages("swap", inputColors, outputColors3, fragments3, group.get());
6219
6220         return group.release();
6221 }
6222
6223 tcu::TestCaseGroup* createNoContractionTests(tcu::TestContext& testCtx)
6224 {
6225         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "nocontraction", "Test the NoContraction decoration"));
6226         RGBA                                                    inputColors[4];
6227         RGBA                                                    outputColors[4];
6228
6229         // With NoContraction, (1 + 2^-23) * (1 - 2^-23) - 1 should be conducted as a multiplication and an addition separately.
6230         // For the multiplication, the result is 1 - 2^-46, which is out of the precision range for 32-bit float. (32-bit float
6231         // only have 23-bit fraction.) So it will be rounded to 1. Or 0x1.fffffc. Then the final result is 0 or -0x1p-24.
6232         // On the contrary, the result will be 2^-46, which is a normalized number perfectly representable as 32-bit float.
6233         const char                                              constantsAndTypes[]      =
6234                 "%c_vec4_0       = OpConstantComposite %v4f32 %c_f32_0 %c_f32_0 %c_f32_0 %c_f32_1\n"
6235                 "%c_vec4_1       = OpConstantComposite %v4f32 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_1\n"
6236                 "%c_f32_1pl2_23  = OpConstant %f32 0x1.000002p+0\n" // 1 + 2^-23
6237                 "%c_f32_1mi2_23  = OpConstant %f32 0x1.fffffcp-1\n" // 1 - 2^-23
6238                 "%c_f32_n1pn24   = OpConstant %f32 -0x1p-24\n";
6239
6240         const char                                              function[]       =
6241                 "%test_code      = OpFunction %v4f32 None %v4f32_function\n"
6242                 "%param          = OpFunctionParameter %v4f32\n"
6243                 "%label          = OpLabel\n"
6244                 "%var1           = OpVariable %fp_f32 Function %c_f32_1pl2_23\n"
6245                 "%var2           = OpVariable %fp_f32 Function\n"
6246                 "%red            = OpCompositeExtract %f32 %param 0\n"
6247                 "%plus_red       = OpFAdd %f32 %c_f32_1mi2_23 %red\n"
6248                 "                  OpStore %var2 %plus_red\n"
6249                 "%val1           = OpLoad %f32 %var1\n"
6250                 "%val2           = OpLoad %f32 %var2\n"
6251                 "%mul            = OpFMul %f32 %val1 %val2\n"
6252                 "%add            = OpFAdd %f32 %mul %c_f32_n1\n"
6253                 "%is0            = OpFOrdEqual %bool %add %c_f32_0\n"
6254                 "%isn1n24         = OpFOrdEqual %bool %add %c_f32_n1pn24\n"
6255                 "%success        = OpLogicalOr %bool %is0 %isn1n24\n"
6256                 "%v4success      = OpCompositeConstruct %v4bool %success %success %success %success\n"
6257                 "%ret            = OpSelect %v4f32 %v4success %c_vec4_0 %c_vec4_1\n"
6258                 "                  OpReturnValue %ret\n"
6259                 "                  OpFunctionEnd\n";
6260
6261         struct CaseNameDecoration
6262         {
6263                 string name;
6264                 string decoration;
6265         };
6266
6267
6268         CaseNameDecoration tests[] = {
6269                 {"multiplication",      "OpDecorate %mul NoContraction"},
6270                 {"addition",            "OpDecorate %add NoContraction"},
6271                 {"both",                        "OpDecorate %mul NoContraction\nOpDecorate %add NoContraction"},
6272         };
6273
6274         getHalfColorsFullAlpha(inputColors);
6275
6276         for (deUint8 idx = 0; idx < 4; ++idx)
6277         {
6278                 inputColors[idx].setRed(0);
6279                 outputColors[idx] = RGBA(0, 0, 0, 255);
6280         }
6281
6282         for (size_t testNdx = 0; testNdx < sizeof(tests) / sizeof(CaseNameDecoration); ++testNdx)
6283         {
6284                 map<string, string> fragments;
6285
6286                 fragments["decoration"] = tests[testNdx].decoration;
6287                 fragments["pre_main"] = constantsAndTypes;
6288                 fragments["testfun"] = function;
6289
6290                 createTestsForAllStages(tests[testNdx].name, inputColors, outputColors, fragments, group.get());
6291         }
6292
6293         return group.release();
6294 }
6295
6296 tcu::TestCaseGroup* createMemoryAccessTests(tcu::TestContext& testCtx)
6297 {
6298         de::MovePtr<tcu::TestCaseGroup> memoryAccessTests (new tcu::TestCaseGroup(testCtx, "opmemoryaccess", "Memory Semantics"));
6299         RGBA                                                    colors[4];
6300
6301         const char                                              constantsAndTypes[]      =
6302                 "%c_a2f32_1         = OpConstantComposite %a2f32 %c_f32_1 %c_f32_1\n"
6303                 "%fp_a2f32          = OpTypePointer Function %a2f32\n"
6304                 "%stype             = OpTypeStruct  %v4f32 %a2f32 %f32\n"
6305                 "%fp_stype          = OpTypePointer Function %stype\n";
6306
6307         const char                                              function[]       =
6308                 "%test_code         = OpFunction %v4f32 None %v4f32_function\n"
6309                 "%param1            = OpFunctionParameter %v4f32\n"
6310                 "%lbl               = OpLabel\n"
6311                 "%v1                = OpVariable %fp_v4f32 Function\n"
6312                 "%v2                = OpVariable %fp_a2f32 Function\n"
6313                 "%v3                = OpVariable %fp_f32 Function\n"
6314                 "%v                 = OpVariable %fp_stype Function\n"
6315                 "%vv                = OpVariable %fp_stype Function\n"
6316                 "%vvv               = OpVariable %fp_f32 Function\n"
6317
6318                 "                     OpStore %v1 %c_v4f32_1_1_1_1\n"
6319                 "                     OpStore %v2 %c_a2f32_1\n"
6320                 "                     OpStore %v3 %c_f32_1\n"
6321
6322                 "%p_v4f32          = OpAccessChain %fp_v4f32 %v %c_u32_0\n"
6323                 "%p_a2f32          = OpAccessChain %fp_a2f32 %v %c_u32_1\n"
6324                 "%p_f32            = OpAccessChain %fp_f32 %v %c_u32_2\n"
6325                 "%v1_v             = OpLoad %v4f32 %v1 ${access_type}\n"
6326                 "%v2_v             = OpLoad %a2f32 %v2 ${access_type}\n"
6327                 "%v3_v             = OpLoad %f32 %v3 ${access_type}\n"
6328
6329                 "                    OpStore %p_v4f32 %v1_v ${access_type}\n"
6330                 "                    OpStore %p_a2f32 %v2_v ${access_type}\n"
6331                 "                    OpStore %p_f32 %v3_v ${access_type}\n"
6332
6333                 "                    OpCopyMemory %vv %v ${access_type}\n"
6334                 "                    OpCopyMemory %vvv %p_f32 ${access_type}\n"
6335
6336                 "%p_f32_2          = OpAccessChain %fp_f32 %vv %c_u32_2\n"
6337                 "%v_f32_2          = OpLoad %f32 %p_f32_2\n"
6338                 "%v_f32_3          = OpLoad %f32 %vvv\n"
6339
6340                 "%ret1             = OpVectorTimesScalar %v4f32 %param1 %v_f32_2\n"
6341                 "%ret2             = OpVectorTimesScalar %v4f32 %ret1 %v_f32_3\n"
6342                 "                    OpReturnValue %ret2\n"
6343                 "                    OpFunctionEnd\n";
6344
6345         struct NameMemoryAccess
6346         {
6347                 string name;
6348                 string accessType;
6349         };
6350
6351
6352         NameMemoryAccess tests[] =
6353         {
6354                 { "none", "" },
6355                 { "volatile", "Volatile" },
6356                 { "aligned",  "Aligned 1" },
6357                 { "volatile_aligned",  "Volatile|Aligned 1" },
6358                 { "nontemporal_aligned",  "Nontemporal|Aligned 1" },
6359                 { "volatile_nontemporal",  "Volatile|Nontemporal" },
6360                 { "volatile_nontermporal_aligned",  "Volatile|Nontemporal|Aligned 1" },
6361         };
6362
6363         getHalfColorsFullAlpha(colors);
6364
6365         for (size_t testNdx = 0; testNdx < sizeof(tests) / sizeof(NameMemoryAccess); ++testNdx)
6366         {
6367                 map<string, string> fragments;
6368                 map<string, string> memoryAccess;
6369                 memoryAccess["access_type"] = tests[testNdx].accessType;
6370
6371                 fragments["pre_main"] = constantsAndTypes;
6372                 fragments["testfun"] = tcu::StringTemplate(function).specialize(memoryAccess);
6373                 createTestsForAllStages(tests[testNdx].name, colors, colors, fragments, memoryAccessTests.get());
6374         }
6375         return memoryAccessTests.release();
6376 }
6377 tcu::TestCaseGroup* createOpUndefTests(tcu::TestContext& testCtx)
6378 {
6379         de::MovePtr<tcu::TestCaseGroup>         opUndefTests             (new tcu::TestCaseGroup(testCtx, "opundef", "Test OpUndef"));
6380         RGBA                                                            defaultColors[4];
6381         map<string, string>                                     fragments;
6382         getDefaultColors(defaultColors);
6383
6384         // First, simple cases that don't do anything with the OpUndef result.
6385         struct NameCodePair { string name, decl, type; };
6386         const NameCodePair tests[] =
6387         {
6388                 {"bool", "", "%bool"},
6389                 {"vec2uint32", "", "%v2u32"},
6390                 {"image", "%type = OpTypeImage %f32 2D 0 0 0 1 Unknown", "%type"},
6391                 {"sampler", "%type = OpTypeSampler", "%type"},
6392                 {"sampledimage", "%img = OpTypeImage %f32 2D 0 0 0 1 Unknown\n" "%type = OpTypeSampledImage %img", "%type"},
6393                 {"pointer", "", "%fp_i32"},
6394                 {"runtimearray", "%type = OpTypeRuntimeArray %f32", "%type"},
6395                 {"array", "%c_u32_100 = OpConstant %u32 100\n" "%type = OpTypeArray %i32 %c_u32_100", "%type"},
6396                 {"struct", "%type = OpTypeStruct %f32 %i32 %u32", "%type"}};
6397         for (size_t testNdx = 0; testNdx < sizeof(tests) / sizeof(NameCodePair); ++testNdx)
6398         {
6399                 fragments["undef_type"] = tests[testNdx].type;
6400                 fragments["testfun"] = StringTemplate(
6401                         "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6402                         "%param1 = OpFunctionParameter %v4f32\n"
6403                         "%label_testfun = OpLabel\n"
6404                         "%undef = OpUndef ${undef_type}\n"
6405                         "OpReturnValue %param1\n"
6406                         "OpFunctionEnd\n").specialize(fragments);
6407                 fragments["pre_main"] = tests[testNdx].decl;
6408                 createTestsForAllStages(tests[testNdx].name, defaultColors, defaultColors, fragments, opUndefTests.get());
6409         }
6410         fragments.clear();
6411
6412         fragments["testfun"] =
6413                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6414                 "%param1 = OpFunctionParameter %v4f32\n"
6415                 "%label_testfun = OpLabel\n"
6416                 "%undef = OpUndef %f32\n"
6417                 "%zero = OpFMul %f32 %undef %c_f32_0\n"
6418                 "%is_nan = OpIsNan %bool %zero\n" //OpUndef may result in NaN which may turn %zero into Nan.
6419                 "%actually_zero = OpSelect %f32 %is_nan %c_f32_0 %zero\n"
6420                 "%a = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
6421                 "%b = OpFAdd %f32 %a %actually_zero\n"
6422                 "%ret = OpVectorInsertDynamic %v4f32 %param1 %b %c_i32_0\n"
6423                 "OpReturnValue %ret\n"
6424                 "OpFunctionEnd\n";
6425
6426         createTestsForAllStages("float32", defaultColors, defaultColors, fragments, opUndefTests.get());
6427
6428         fragments["testfun"] =
6429                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6430                 "%param1 = OpFunctionParameter %v4f32\n"
6431                 "%label_testfun = OpLabel\n"
6432                 "%undef = OpUndef %i32\n"
6433                 "%zero = OpIMul %i32 %undef %c_i32_0\n"
6434                 "%a = OpVectorExtractDynamic %f32 %param1 %zero\n"
6435                 "%ret = OpVectorInsertDynamic %v4f32 %param1 %a %c_i32_0\n"
6436                 "OpReturnValue %ret\n"
6437                 "OpFunctionEnd\n";
6438
6439         createTestsForAllStages("sint32", defaultColors, defaultColors, fragments, opUndefTests.get());
6440
6441         fragments["testfun"] =
6442                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6443                 "%param1 = OpFunctionParameter %v4f32\n"
6444                 "%label_testfun = OpLabel\n"
6445                 "%undef = OpUndef %u32\n"
6446                 "%zero = OpIMul %u32 %undef %c_i32_0\n"
6447                 "%a = OpVectorExtractDynamic %f32 %param1 %zero\n"
6448                 "%ret = OpVectorInsertDynamic %v4f32 %param1 %a %c_i32_0\n"
6449                 "OpReturnValue %ret\n"
6450                 "OpFunctionEnd\n";
6451
6452         createTestsForAllStages("uint32", defaultColors, defaultColors, fragments, opUndefTests.get());
6453
6454         fragments["testfun"] =
6455                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6456                 "%param1 = OpFunctionParameter %v4f32\n"
6457                 "%label_testfun = OpLabel\n"
6458                 "%undef = OpUndef %v4f32\n"
6459                 "%vzero = OpVectorTimesScalar %v4f32 %undef %c_f32_0\n"
6460                 "%zero_0 = OpVectorExtractDynamic %f32 %vzero %c_i32_0\n"
6461                 "%zero_1 = OpVectorExtractDynamic %f32 %vzero %c_i32_1\n"
6462                 "%zero_2 = OpVectorExtractDynamic %f32 %vzero %c_i32_2\n"
6463                 "%zero_3 = OpVectorExtractDynamic %f32 %vzero %c_i32_3\n"
6464                 "%is_nan_0 = OpIsNan %bool %zero_0\n"
6465                 "%is_nan_1 = OpIsNan %bool %zero_1\n"
6466                 "%is_nan_2 = OpIsNan %bool %zero_2\n"
6467                 "%is_nan_3 = OpIsNan %bool %zero_3\n"
6468                 "%actually_zero_0 = OpSelect %f32 %is_nan_0 %c_f32_0 %zero_0\n"
6469                 "%actually_zero_1 = OpSelect %f32 %is_nan_0 %c_f32_0 %zero_1\n"
6470                 "%actually_zero_2 = OpSelect %f32 %is_nan_0 %c_f32_0 %zero_2\n"
6471                 "%actually_zero_3 = OpSelect %f32 %is_nan_0 %c_f32_0 %zero_3\n"
6472                 "%param1_0 = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
6473                 "%param1_1 = OpVectorExtractDynamic %f32 %param1 %c_i32_1\n"
6474                 "%param1_2 = OpVectorExtractDynamic %f32 %param1 %c_i32_2\n"
6475                 "%param1_3 = OpVectorExtractDynamic %f32 %param1 %c_i32_3\n"
6476                 "%sum_0 = OpFAdd %f32 %param1_0 %actually_zero_0\n"
6477                 "%sum_1 = OpFAdd %f32 %param1_1 %actually_zero_1\n"
6478                 "%sum_2 = OpFAdd %f32 %param1_2 %actually_zero_2\n"
6479                 "%sum_3 = OpFAdd %f32 %param1_3 %actually_zero_3\n"
6480                 "%ret3 = OpVectorInsertDynamic %v4f32 %param1 %sum_3 %c_i32_3\n"
6481                 "%ret2 = OpVectorInsertDynamic %v4f32 %ret3 %sum_2 %c_i32_2\n"
6482                 "%ret1 = OpVectorInsertDynamic %v4f32 %ret2 %sum_1 %c_i32_1\n"
6483                 "%ret = OpVectorInsertDynamic %v4f32 %ret1 %sum_0 %c_i32_0\n"
6484                 "OpReturnValue %ret\n"
6485                 "OpFunctionEnd\n";
6486
6487         createTestsForAllStages("vec4float32", defaultColors, defaultColors, fragments, opUndefTests.get());
6488
6489         fragments["pre_main"] =
6490                 "%m2x2f32 = OpTypeMatrix %v2f32 2\n";
6491         fragments["testfun"] =
6492                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6493                 "%param1 = OpFunctionParameter %v4f32\n"
6494                 "%label_testfun = OpLabel\n"
6495                 "%undef = OpUndef %m2x2f32\n"
6496                 "%mzero = OpMatrixTimesScalar %m2x2f32 %undef %c_f32_0\n"
6497                 "%zero_0 = OpCompositeExtract %f32 %mzero 0 0\n"
6498                 "%zero_1 = OpCompositeExtract %f32 %mzero 0 1\n"
6499                 "%zero_2 = OpCompositeExtract %f32 %mzero 1 0\n"
6500                 "%zero_3 = OpCompositeExtract %f32 %mzero 1 1\n"
6501                 "%is_nan_0 = OpIsNan %bool %zero_0\n"
6502                 "%is_nan_1 = OpIsNan %bool %zero_1\n"
6503                 "%is_nan_2 = OpIsNan %bool %zero_2\n"
6504                 "%is_nan_3 = OpIsNan %bool %zero_3\n"
6505                 "%actually_zero_0 = OpSelect %f32 %is_nan_0 %c_f32_0 %zero_0\n"
6506                 "%actually_zero_1 = OpSelect %f32 %is_nan_0 %c_f32_0 %zero_1\n"
6507                 "%actually_zero_2 = OpSelect %f32 %is_nan_0 %c_f32_0 %zero_2\n"
6508                 "%actually_zero_3 = OpSelect %f32 %is_nan_0 %c_f32_0 %zero_3\n"
6509                 "%param1_0 = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
6510                 "%param1_1 = OpVectorExtractDynamic %f32 %param1 %c_i32_1\n"
6511                 "%param1_2 = OpVectorExtractDynamic %f32 %param1 %c_i32_2\n"
6512                 "%param1_3 = OpVectorExtractDynamic %f32 %param1 %c_i32_3\n"
6513                 "%sum_0 = OpFAdd %f32 %param1_0 %actually_zero_0\n"
6514                 "%sum_1 = OpFAdd %f32 %param1_1 %actually_zero_1\n"
6515                 "%sum_2 = OpFAdd %f32 %param1_2 %actually_zero_2\n"
6516                 "%sum_3 = OpFAdd %f32 %param1_3 %actually_zero_3\n"
6517                 "%ret3 = OpVectorInsertDynamic %v4f32 %param1 %sum_3 %c_i32_3\n"
6518                 "%ret2 = OpVectorInsertDynamic %v4f32 %ret3 %sum_2 %c_i32_2\n"
6519                 "%ret1 = OpVectorInsertDynamic %v4f32 %ret2 %sum_1 %c_i32_1\n"
6520                 "%ret = OpVectorInsertDynamic %v4f32 %ret1 %sum_0 %c_i32_0\n"
6521                 "OpReturnValue %ret\n"
6522                 "OpFunctionEnd\n";
6523
6524         createTestsForAllStages("matrix", defaultColors, defaultColors, fragments, opUndefTests.get());
6525
6526         return opUndefTests.release();
6527 }
6528
6529 void createOpQuantizeSingleOptionTests(tcu::TestCaseGroup* testCtx)
6530 {
6531         const RGBA              inputColors[4]          =
6532         {
6533                 RGBA(0,         0,              0,              255),
6534                 RGBA(0,         0,              255,    255),
6535                 RGBA(0,         255,    0,              255),
6536                 RGBA(0,         255,    255,    255)
6537         };
6538
6539         const RGBA              expectedColors[4]       =
6540         {
6541                 RGBA(255,        0,              0,              255),
6542                 RGBA(255,        0,              0,              255),
6543                 RGBA(255,        0,              0,              255),
6544                 RGBA(255,        0,              0,              255)
6545         };
6546
6547         const struct SingleFP16Possibility
6548         {
6549                 const char* name;
6550                 const char* constant;  // Value to assign to %test_constant.
6551                 float           valueAsFloat;
6552                 const char* condition; // Must assign to %cond an expression that evaluates to true after %c = OpQuantizeToF16(%test_constant + 0).
6553         }                               tests[]                         =
6554         {
6555                 {
6556                         "negative",
6557                         "-0x1.3p1\n",
6558                         -constructNormalizedFloat(1, 0x300000),
6559                         "%cond = OpFOrdEqual %bool %c %test_constant\n"
6560                 }, // -19
6561                 {
6562                         "positive",
6563                         "0x1.0p7\n",
6564                         constructNormalizedFloat(7, 0x000000),
6565                         "%cond = OpFOrdEqual %bool %c %test_constant\n"
6566                 },  // +128
6567                 // SPIR-V requires that OpQuantizeToF16 flushes
6568                 // any numbers that would end up denormalized in F16 to zero.
6569                 {
6570                         "denorm",
6571                         "0x0.0006p-126\n",
6572                         std::ldexp(1.5f, -140),
6573                         "%cond = OpFOrdEqual %bool %c %c_f32_0\n"
6574                 },  // denorm
6575                 {
6576                         "negative_denorm",
6577                         "-0x0.0006p-126\n",
6578                         -std::ldexp(1.5f, -140),
6579                         "%cond = OpFOrdEqual %bool %c %c_f32_0\n"
6580                 }, // -denorm
6581                 {
6582                         "too_small",
6583                         "0x1.0p-16\n",
6584                         std::ldexp(1.0f, -16),
6585                         "%cond = OpFOrdEqual %bool %c %c_f32_0\n"
6586                 },     // too small positive
6587                 {
6588                         "negative_too_small",
6589                         "-0x1.0p-32\n",
6590                         -std::ldexp(1.0f, -32),
6591                         "%cond = OpFOrdEqual %bool %c %c_f32_0\n"
6592                 },      // too small negative
6593                 {
6594                         "negative_inf",
6595                         "-0x1.0p128\n",
6596                         -std::ldexp(1.0f, 128),
6597
6598                         "%gz = OpFOrdLessThan %bool %c %c_f32_0\n"
6599                         "%inf = OpIsInf %bool %c\n"
6600                         "%cond = OpLogicalAnd %bool %gz %inf\n"
6601                 },     // -inf to -inf
6602                 {
6603                         "inf",
6604                         "0x1.0p128\n",
6605                         std::ldexp(1.0f, 128),
6606
6607                         "%gz = OpFOrdGreaterThan %bool %c %c_f32_0\n"
6608                         "%inf = OpIsInf %bool %c\n"
6609                         "%cond = OpLogicalAnd %bool %gz %inf\n"
6610                 },     // +inf to +inf
6611                 {
6612                         "round_to_negative_inf",
6613                         "-0x1.0p32\n",
6614                         -std::ldexp(1.0f, 32),
6615
6616                         "%gz = OpFOrdLessThan %bool %c %c_f32_0\n"
6617                         "%inf = OpIsInf %bool %c\n"
6618                         "%cond = OpLogicalAnd %bool %gz %inf\n"
6619                 },     // round to -inf
6620                 {
6621                         "round_to_inf",
6622                         "0x1.0p16\n",
6623                         std::ldexp(1.0f, 16),
6624
6625                         "%gz = OpFOrdGreaterThan %bool %c %c_f32_0\n"
6626                         "%inf = OpIsInf %bool %c\n"
6627                         "%cond = OpLogicalAnd %bool %gz %inf\n"
6628                 },     // round to +inf
6629                 {
6630                         "nan",
6631                         "0x1.1p128\n",
6632                         std::numeric_limits<float>::quiet_NaN(),
6633
6634                         // Test for any NaN value, as NaNs are not preserved
6635                         "%direct_quant = OpQuantizeToF16 %f32 %test_constant\n"
6636                         "%cond = OpIsNan %bool %direct_quant\n"
6637                 }, // nan
6638                 {
6639                         "negative_nan",
6640                         "-0x1.0001p128\n",
6641                         std::numeric_limits<float>::quiet_NaN(),
6642
6643                         // Test for any NaN value, as NaNs are not preserved
6644                         "%direct_quant = OpQuantizeToF16 %f32 %test_constant\n"
6645                         "%cond = OpIsNan %bool %direct_quant\n"
6646                 } // -nan
6647         };
6648         const char*             constants                       =
6649                 "%test_constant = OpConstant %f32 ";  // The value will be test.constant.
6650
6651         StringTemplate  function                        (
6652                 "%test_code     = OpFunction %v4f32 None %v4f32_function\n"
6653                 "%param1        = OpFunctionParameter %v4f32\n"
6654                 "%label_testfun = OpLabel\n"
6655                 "%a             = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
6656                 "%b             = OpFAdd %f32 %test_constant %a\n"
6657                 "%c             = OpQuantizeToF16 %f32 %b\n"
6658                 "${condition}\n"
6659                 "%v4cond        = OpCompositeConstruct %v4bool %cond %cond %cond %cond\n"
6660                 "%retval        = OpSelect %v4f32 %v4cond %c_v4f32_1_0_0_1 %param1\n"
6661                 "                 OpReturnValue %retval\n"
6662                 "OpFunctionEnd\n"
6663         );
6664
6665         const char*             specDecorations         = "OpDecorate %test_constant SpecId 0\n";
6666         const char*             specConstants           =
6667                         "%test_constant = OpSpecConstant %f32 0.\n"
6668                         "%c             = OpSpecConstantOp %f32 QuantizeToF16 %test_constant\n";
6669
6670         StringTemplate  specConstantFunction(
6671                 "%test_code     = OpFunction %v4f32 None %v4f32_function\n"
6672                 "%param1        = OpFunctionParameter %v4f32\n"
6673                 "%label_testfun = OpLabel\n"
6674                 "${condition}\n"
6675                 "%v4cond        = OpCompositeConstruct %v4bool %cond %cond %cond %cond\n"
6676                 "%retval        = OpSelect %v4f32 %v4cond %c_v4f32_1_0_0_1 %param1\n"
6677                 "                 OpReturnValue %retval\n"
6678                 "OpFunctionEnd\n"
6679         );
6680
6681         for (size_t idx = 0; idx < (sizeof(tests)/sizeof(tests[0])); ++idx)
6682         {
6683                 map<string, string>                                                             codeSpecialization;
6684                 map<string, string>                                                             fragments;
6685                 codeSpecialization["condition"]                                 = tests[idx].condition;
6686                 fragments["testfun"]                                                    = function.specialize(codeSpecialization);
6687                 fragments["pre_main"]                                                   = string(constants) + tests[idx].constant + "\n";
6688                 createTestsForAllStages(tests[idx].name, inputColors, expectedColors, fragments, testCtx);
6689         }
6690
6691         for (size_t idx = 0; idx < (sizeof(tests)/sizeof(tests[0])); ++idx)
6692         {
6693                 map<string, string>                                                             codeSpecialization;
6694                 map<string, string>                                                             fragments;
6695                 vector<deInt32>                                                                 passConstants;
6696                 deInt32                                                                                 specConstant;
6697
6698                 codeSpecialization["condition"]                                 = tests[idx].condition;
6699                 fragments["testfun"]                                                    = specConstantFunction.specialize(codeSpecialization);
6700                 fragments["decoration"]                                                 = specDecorations;
6701                 fragments["pre_main"]                                                   = specConstants;
6702
6703                 memcpy(&specConstant, &tests[idx].valueAsFloat, sizeof(float));
6704                 passConstants.push_back(specConstant);
6705
6706                 createTestsForAllStages(string("spec_const_") + tests[idx].name, inputColors, expectedColors, fragments, passConstants, testCtx);
6707         }
6708 }
6709
6710 void createOpQuantizeTwoPossibilityTests(tcu::TestCaseGroup* testCtx)
6711 {
6712         RGBA inputColors[4] =  {
6713                 RGBA(0,         0,              0,              255),
6714                 RGBA(0,         0,              255,    255),
6715                 RGBA(0,         255,    0,              255),
6716                 RGBA(0,         255,    255,    255)
6717         };
6718
6719         RGBA expectedColors[4] =
6720         {
6721                 RGBA(255,        0,              0,              255),
6722                 RGBA(255,        0,              0,              255),
6723                 RGBA(255,        0,              0,              255),
6724                 RGBA(255,        0,              0,              255)
6725         };
6726
6727         struct DualFP16Possibility
6728         {
6729                 const char* name;
6730                 const char* input;
6731                 float           inputAsFloat;
6732                 const char* possibleOutput1;
6733                 const char* possibleOutput2;
6734         } tests[] = {
6735                 {
6736                         "positive_round_up_or_round_down",
6737                         "0x1.3003p8",
6738                         constructNormalizedFloat(8, 0x300300),
6739                         "0x1.304p8",
6740                         "0x1.3p8"
6741                 },
6742                 {
6743                         "negative_round_up_or_round_down",
6744                         "-0x1.6008p-7",
6745                         -constructNormalizedFloat(-7, 0x600800),
6746                         "-0x1.6p-7",
6747                         "-0x1.604p-7"
6748                 },
6749                 {
6750                         "carry_bit",
6751                         "0x1.01ep2",
6752                         constructNormalizedFloat(2, 0x01e000),
6753                         "0x1.01cp2",
6754                         "0x1.02p2"
6755                 },
6756                 {
6757                         "carry_to_exponent",
6758                         "0x1.ffep1",
6759                         constructNormalizedFloat(1, 0xffe000),
6760                         "0x1.ffcp1",
6761                         "0x1.0p2"
6762                 },
6763         };
6764         StringTemplate constants (
6765                 "%input_const = OpConstant %f32 ${input}\n"
6766                 "%possible_solution1 = OpConstant %f32 ${output1}\n"
6767                 "%possible_solution2 = OpConstant %f32 ${output2}\n"
6768                 );
6769
6770         StringTemplate specConstants (
6771                 "%input_const = OpSpecConstant %f32 0.\n"
6772                 "%possible_solution1 = OpConstant %f32 ${output1}\n"
6773                 "%possible_solution2 = OpConstant %f32 ${output2}\n"
6774         );
6775
6776         const char* specDecorations = "OpDecorate %input_const  SpecId 0\n";
6777
6778         const char* function  =
6779                 "%test_code     = OpFunction %v4f32 None %v4f32_function\n"
6780                 "%param1        = OpFunctionParameter %v4f32\n"
6781                 "%label_testfun = OpLabel\n"
6782                 "%a             = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
6783                 // For the purposes of this test we assume that 0.f will always get
6784                 // faithfully passed through the pipeline stages.
6785                 "%b             = OpFAdd %f32 %input_const %a\n"
6786                 "%c             = OpQuantizeToF16 %f32 %b\n"
6787                 "%eq_1          = OpFOrdEqual %bool %c %possible_solution1\n"
6788                 "%eq_2          = OpFOrdEqual %bool %c %possible_solution2\n"
6789                 "%cond          = OpLogicalOr %bool %eq_1 %eq_2\n"
6790                 "%v4cond        = OpCompositeConstruct %v4bool %cond %cond %cond %cond\n"
6791                 "%retval        = OpSelect %v4f32 %v4cond %c_v4f32_1_0_0_1 %param1"
6792                 "                 OpReturnValue %retval\n"
6793                 "OpFunctionEnd\n";
6794
6795         for(size_t idx = 0; idx < (sizeof(tests)/sizeof(tests[0])); ++idx) {
6796                 map<string, string>                                                                     fragments;
6797                 map<string, string>                                                                     constantSpecialization;
6798
6799                 constantSpecialization["input"]                                         = tests[idx].input;
6800                 constantSpecialization["output1"]                                       = tests[idx].possibleOutput1;
6801                 constantSpecialization["output2"]                                       = tests[idx].possibleOutput2;
6802                 fragments["testfun"]                                                            = function;
6803                 fragments["pre_main"]                                                           = constants.specialize(constantSpecialization);
6804                 createTestsForAllStages(tests[idx].name, inputColors, expectedColors, fragments, testCtx);
6805         }
6806
6807         for(size_t idx = 0; idx < (sizeof(tests)/sizeof(tests[0])); ++idx) {
6808                 map<string, string>                                                                     fragments;
6809                 map<string, string>                                                                     constantSpecialization;
6810                 vector<deInt32>                                                                         passConstants;
6811                 deInt32                                                                                         specConstant;
6812
6813                 constantSpecialization["output1"]                                       = tests[idx].possibleOutput1;
6814                 constantSpecialization["output2"]                                       = tests[idx].possibleOutput2;
6815                 fragments["testfun"]                                                            = function;
6816                 fragments["decoration"]                                                         = specDecorations;
6817                 fragments["pre_main"]                                                           = specConstants.specialize(constantSpecialization);
6818
6819                 memcpy(&specConstant, &tests[idx].inputAsFloat, sizeof(float));
6820                 passConstants.push_back(specConstant);
6821
6822                 createTestsForAllStages(string("spec_const_") + tests[idx].name, inputColors, expectedColors, fragments, passConstants, testCtx);
6823         }
6824 }
6825
6826 tcu::TestCaseGroup* createOpQuantizeTests(tcu::TestContext& testCtx)
6827 {
6828         de::MovePtr<tcu::TestCaseGroup> opQuantizeTests (new tcu::TestCaseGroup(testCtx, "opquantize", "Test OpQuantizeToF16"));
6829         createOpQuantizeSingleOptionTests(opQuantizeTests.get());
6830         createOpQuantizeTwoPossibilityTests(opQuantizeTests.get());
6831         return opQuantizeTests.release();
6832 }
6833
6834 struct ShaderPermutation
6835 {
6836         deUint8 vertexPermutation;
6837         deUint8 geometryPermutation;
6838         deUint8 tesscPermutation;
6839         deUint8 tessePermutation;
6840         deUint8 fragmentPermutation;
6841 };
6842
6843 ShaderPermutation getShaderPermutation(deUint8 inputValue)
6844 {
6845         ShaderPermutation       permutation =
6846         {
6847                 static_cast<deUint8>(inputValue & 0x10? 1u: 0u),
6848                 static_cast<deUint8>(inputValue & 0x08? 1u: 0u),
6849                 static_cast<deUint8>(inputValue & 0x04? 1u: 0u),
6850                 static_cast<deUint8>(inputValue & 0x02? 1u: 0u),
6851                 static_cast<deUint8>(inputValue & 0x01? 1u: 0u)
6852         };
6853         return permutation;
6854 }
6855
6856 tcu::TestCaseGroup* createModuleTests(tcu::TestContext& testCtx)
6857 {
6858         RGBA                                                            defaultColors[4];
6859         RGBA                                                            invertedColors[4];
6860         de::MovePtr<tcu::TestCaseGroup>         moduleTests                     (new tcu::TestCaseGroup(testCtx, "module", "Multiple entry points into shaders"));
6861
6862         const ShaderElement                                     combinedPipeline[]      =
6863         {
6864                 ShaderElement("module", "main", VK_SHADER_STAGE_VERTEX_BIT),
6865                 ShaderElement("module", "main", VK_SHADER_STAGE_GEOMETRY_BIT),
6866                 ShaderElement("module", "main", VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT),
6867                 ShaderElement("module", "main", VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT),
6868                 ShaderElement("module", "main", VK_SHADER_STAGE_FRAGMENT_BIT)
6869         };
6870
6871         getDefaultColors(defaultColors);
6872         getInvertedDefaultColors(invertedColors);
6873         addFunctionCaseWithPrograms<InstanceContext>(
6874                         moduleTests.get(), "same_module", "", createCombinedModule, runAndVerifyDefaultPipeline,
6875                         createInstanceContext(combinedPipeline, map<string, string>()));
6876
6877         const char* numbers[] =
6878         {
6879                 "1", "2"
6880         };
6881
6882         for (deInt8 idx = 0; idx < 32; ++idx)
6883         {
6884                 ShaderPermutation                       permutation             = getShaderPermutation(idx);
6885                 string                                          name                    = string("vert") + numbers[permutation.vertexPermutation] + "_geom" + numbers[permutation.geometryPermutation] + "_tessc" + numbers[permutation.tesscPermutation] + "_tesse" + numbers[permutation.tessePermutation] + "_frag" + numbers[permutation.fragmentPermutation];
6886                 const ShaderElement                     pipeline[]              =
6887                 {
6888                         ShaderElement("vert",   string("vert") +        numbers[permutation.vertexPermutation],         VK_SHADER_STAGE_VERTEX_BIT),
6889                         ShaderElement("geom",   string("geom") +        numbers[permutation.geometryPermutation],       VK_SHADER_STAGE_GEOMETRY_BIT),
6890                         ShaderElement("tessc",  string("tessc") +       numbers[permutation.tesscPermutation],          VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT),
6891                         ShaderElement("tesse",  string("tesse") +       numbers[permutation.tessePermutation],          VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT),
6892                         ShaderElement("frag",   string("frag") +        numbers[permutation.fragmentPermutation],       VK_SHADER_STAGE_FRAGMENT_BIT)
6893                 };
6894
6895                 // If there are an even number of swaps, then it should be no-op.
6896                 // If there are an odd number, the color should be flipped.
6897                 if ((permutation.vertexPermutation + permutation.geometryPermutation + permutation.tesscPermutation + permutation.tessePermutation + permutation.fragmentPermutation) % 2 == 0)
6898                 {
6899                         addFunctionCaseWithPrograms<InstanceContext>(
6900                                         moduleTests.get(), name, "", createMultipleEntries, runAndVerifyDefaultPipeline,
6901                                         createInstanceContext(pipeline, defaultColors, defaultColors, map<string, string>()));
6902                 }
6903                 else
6904                 {
6905                         addFunctionCaseWithPrograms<InstanceContext>(
6906                                         moduleTests.get(), name, "", createMultipleEntries, runAndVerifyDefaultPipeline,
6907                                         createInstanceContext(pipeline, defaultColors, invertedColors, map<string, string>()));
6908                 }
6909         }
6910         return moduleTests.release();
6911 }
6912
6913 tcu::TestCaseGroup* createLoopTests(tcu::TestContext& testCtx)
6914 {
6915         de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "loop", "Looping control flow"));
6916         RGBA defaultColors[4];
6917         getDefaultColors(defaultColors);
6918         map<string, string> fragments;
6919         fragments["pre_main"] =
6920                 "%c_f32_5 = OpConstant %f32 5.\n";
6921
6922         // A loop with a single block. The Continue Target is the loop block
6923         // itself. In SPIR-V terms, the "loop construct" contains no blocks at all
6924         // -- the "continue construct" forms the entire loop.
6925         fragments["testfun"] =
6926                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6927                 "%param1 = OpFunctionParameter %v4f32\n"
6928
6929                 "%entry = OpLabel\n"
6930                 "%val0 = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
6931                 "OpBranch %loop\n"
6932
6933                 ";adds and subtracts 1.0 to %val in alternate iterations\n"
6934                 "%loop = OpLabel\n"
6935                 "%count = OpPhi %i32 %c_i32_4 %entry %count__ %loop\n"
6936                 "%delta = OpPhi %f32 %c_f32_1 %entry %minus_delta %loop\n"
6937                 "%val1 = OpPhi %f32 %val0 %entry %val %loop\n"
6938                 "%val = OpFAdd %f32 %val1 %delta\n"
6939                 "%minus_delta = OpFSub %f32 %c_f32_0 %delta\n"
6940                 "%count__ = OpISub %i32 %count %c_i32_1\n"
6941                 "%again = OpSGreaterThan %bool %count__ %c_i32_0\n"
6942                 "OpLoopMerge %exit %loop None\n"
6943                 "OpBranchConditional %again %loop %exit\n"
6944
6945                 "%exit = OpLabel\n"
6946                 "%result = OpVectorInsertDynamic %v4f32 %param1 %val %c_i32_0\n"
6947                 "OpReturnValue %result\n"
6948
6949                 "OpFunctionEnd\n";
6950
6951         createTestsForAllStages("single_block", defaultColors, defaultColors, fragments, testGroup.get());
6952
6953         // Body comprised of multiple basic blocks.
6954         const StringTemplate multiBlock(
6955                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
6956                 "%param1 = OpFunctionParameter %v4f32\n"
6957
6958                 "%entry = OpLabel\n"
6959                 "%val0 = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
6960                 "OpBranch %loop\n"
6961
6962                 ";adds and subtracts 1.0 to %val in alternate iterations\n"
6963                 "%loop = OpLabel\n"
6964                 "%count = OpPhi %i32 %c_i32_4 %entry %count__ %gather\n"
6965                 "%delta = OpPhi %f32 %c_f32_1 %entry %delta_next %gather\n"
6966                 "%val1 = OpPhi %f32 %val0 %entry %val %gather\n"
6967                 // There are several possibilities for the Continue Target below.  Each
6968                 // will be specialized into a separate test case.
6969                 "OpLoopMerge %exit ${continue_target} None\n"
6970                 "OpBranch %if\n"
6971
6972                 "%if = OpLabel\n"
6973                 ";delta_next = (delta > 0) ? -1 : 1;\n"
6974                 "%gt0 = OpFOrdGreaterThan %bool %delta %c_f32_0\n"
6975                 "OpSelectionMerge %gather DontFlatten\n"
6976                 "OpBranchConditional %gt0 %even %odd ;tells us if %count is even or odd\n"
6977
6978                 "%odd = OpLabel\n"
6979                 "OpBranch %gather\n"
6980
6981                 "%even = OpLabel\n"
6982                 "OpBranch %gather\n"
6983
6984                 "%gather = OpLabel\n"
6985                 "%delta_next = OpPhi %f32 %c_f32_n1 %even %c_f32_1 %odd\n"
6986                 "%val = OpFAdd %f32 %val1 %delta\n"
6987                 "%count__ = OpISub %i32 %count %c_i32_1\n"
6988                 "%again = OpSGreaterThan %bool %count__ %c_i32_0\n"
6989                 "OpBranchConditional %again %loop %exit\n"
6990
6991                 "%exit = OpLabel\n"
6992                 "%result = OpVectorInsertDynamic %v4f32 %param1 %val %c_i32_0\n"
6993                 "OpReturnValue %result\n"
6994
6995                 "OpFunctionEnd\n");
6996
6997         map<string, string> continue_target;
6998
6999         // The Continue Target is the loop block itself.
7000         continue_target["continue_target"] = "%loop";
7001         fragments["testfun"] = multiBlock.specialize(continue_target);
7002         createTestsForAllStages("multi_block_continue_construct", defaultColors, defaultColors, fragments, testGroup.get());
7003
7004         // The Continue Target is at the end of the loop.
7005         continue_target["continue_target"] = "%gather";
7006         fragments["testfun"] = multiBlock.specialize(continue_target);
7007         createTestsForAllStages("multi_block_loop_construct", defaultColors, defaultColors, fragments, testGroup.get());
7008
7009         // A loop with continue statement.
7010         fragments["testfun"] =
7011                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
7012                 "%param1 = OpFunctionParameter %v4f32\n"
7013
7014                 "%entry = OpLabel\n"
7015                 "%val0 = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
7016                 "OpBranch %loop\n"
7017
7018                 ";adds 4, 3, and 1 to %val0 (skips 2)\n"
7019                 "%loop = OpLabel\n"
7020                 "%count = OpPhi %i32 %c_i32_4 %entry %count__ %continue\n"
7021                 "%val1 = OpPhi %f32 %val0 %entry %val %continue\n"
7022                 "OpLoopMerge %exit %continue None\n"
7023                 "OpBranch %if\n"
7024
7025                 "%if = OpLabel\n"
7026                 ";skip if %count==2\n"
7027                 "%eq2 = OpIEqual %bool %count %c_i32_2\n"
7028                 "OpSelectionMerge %continue DontFlatten\n"
7029                 "OpBranchConditional %eq2 %continue %body\n"
7030
7031                 "%body = OpLabel\n"
7032                 "%fcount = OpConvertSToF %f32 %count\n"
7033                 "%val2 = OpFAdd %f32 %val1 %fcount\n"
7034                 "OpBranch %continue\n"
7035
7036                 "%continue = OpLabel\n"
7037                 "%val = OpPhi %f32 %val2 %body %val1 %if\n"
7038                 "%count__ = OpISub %i32 %count %c_i32_1\n"
7039                 "%again = OpSGreaterThan %bool %count__ %c_i32_0\n"
7040                 "OpBranchConditional %again %loop %exit\n"
7041
7042                 "%exit = OpLabel\n"
7043                 "%same = OpFSub %f32 %val %c_f32_8\n"
7044                 "%result = OpVectorInsertDynamic %v4f32 %param1 %same %c_i32_0\n"
7045                 "OpReturnValue %result\n"
7046                 "OpFunctionEnd\n";
7047         createTestsForAllStages("continue", defaultColors, defaultColors, fragments, testGroup.get());
7048
7049         // A loop with break.
7050         fragments["testfun"] =
7051                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
7052                 "%param1 = OpFunctionParameter %v4f32\n"
7053
7054                 "%entry = OpLabel\n"
7055                 ";param1 components are between 0 and 1, so dot product is 4 or less\n"
7056                 "%dot = OpDot %f32 %param1 %param1\n"
7057                 "%div = OpFDiv %f32 %dot %c_f32_5\n"
7058                 "%zero = OpConvertFToU %u32 %div\n"
7059                 "%two = OpIAdd %i32 %zero %c_i32_2\n"
7060                 "%val0 = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
7061                 "OpBranch %loop\n"
7062
7063                 ";adds 4 and 3 to %val0 (exits early)\n"
7064                 "%loop = OpLabel\n"
7065                 "%count = OpPhi %i32 %c_i32_4 %entry %count__ %continue\n"
7066                 "%val1 = OpPhi %f32 %val0 %entry %val2 %continue\n"
7067                 "OpLoopMerge %exit %continue None\n"
7068                 "OpBranch %if\n"
7069
7070                 "%if = OpLabel\n"
7071                 ";end loop if %count==%two\n"
7072                 "%above2 = OpSGreaterThan %bool %count %two\n"
7073                 "OpSelectionMerge %continue DontFlatten\n"
7074                 "OpBranchConditional %above2 %body %exit\n"
7075
7076                 "%body = OpLabel\n"
7077                 "%fcount = OpConvertSToF %f32 %count\n"
7078                 "%val2 = OpFAdd %f32 %val1 %fcount\n"
7079                 "OpBranch %continue\n"
7080
7081                 "%continue = OpLabel\n"
7082                 "%count__ = OpISub %i32 %count %c_i32_1\n"
7083                 "%again = OpSGreaterThan %bool %count__ %c_i32_0\n"
7084                 "OpBranchConditional %again %loop %exit\n"
7085
7086                 "%exit = OpLabel\n"
7087                 "%val_post = OpPhi %f32 %val2 %continue %val1 %if\n"
7088                 "%same = OpFSub %f32 %val_post %c_f32_7\n"
7089                 "%result = OpVectorInsertDynamic %v4f32 %param1 %same %c_i32_0\n"
7090                 "OpReturnValue %result\n"
7091                 "OpFunctionEnd\n";
7092         createTestsForAllStages("break", defaultColors, defaultColors, fragments, testGroup.get());
7093
7094         // A loop with return.
7095         fragments["testfun"] =
7096                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
7097                 "%param1 = OpFunctionParameter %v4f32\n"
7098
7099                 "%entry = OpLabel\n"
7100                 ";param1 components are between 0 and 1, so dot product is 4 or less\n"
7101                 "%dot = OpDot %f32 %param1 %param1\n"
7102                 "%div = OpFDiv %f32 %dot %c_f32_5\n"
7103                 "%zero = OpConvertFToU %u32 %div\n"
7104                 "%two = OpIAdd %i32 %zero %c_i32_2\n"
7105                 "%val0 = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
7106                 "OpBranch %loop\n"
7107
7108                 ";returns early without modifying %param1\n"
7109                 "%loop = OpLabel\n"
7110                 "%count = OpPhi %i32 %c_i32_4 %entry %count__ %continue\n"
7111                 "%val1 = OpPhi %f32 %val0 %entry %val2 %continue\n"
7112                 "OpLoopMerge %exit %continue None\n"
7113                 "OpBranch %if\n"
7114
7115                 "%if = OpLabel\n"
7116                 ";return if %count==%two\n"
7117                 "%above2 = OpSGreaterThan %bool %count %two\n"
7118                 "OpSelectionMerge %continue DontFlatten\n"
7119                 "OpBranchConditional %above2 %body %early_exit\n"
7120
7121                 "%early_exit = OpLabel\n"
7122                 "OpReturnValue %param1\n"
7123
7124                 "%body = OpLabel\n"
7125                 "%fcount = OpConvertSToF %f32 %count\n"
7126                 "%val2 = OpFAdd %f32 %val1 %fcount\n"
7127                 "OpBranch %continue\n"
7128
7129                 "%continue = OpLabel\n"
7130                 "%count__ = OpISub %i32 %count %c_i32_1\n"
7131                 "%again = OpSGreaterThan %bool %count__ %c_i32_0\n"
7132                 "OpBranchConditional %again %loop %exit\n"
7133
7134                 "%exit = OpLabel\n"
7135                 ";should never get here, so return an incorrect result\n"
7136                 "%result = OpVectorInsertDynamic %v4f32 %param1 %val2 %c_i32_0\n"
7137                 "OpReturnValue %result\n"
7138                 "OpFunctionEnd\n";
7139         createTestsForAllStages("return", defaultColors, defaultColors, fragments, testGroup.get());
7140
7141         return testGroup.release();
7142 }
7143
7144 // A collection of tests putting OpControlBarrier in places GLSL forbids but SPIR-V allows.
7145 tcu::TestCaseGroup* createBarrierTests(tcu::TestContext& testCtx)
7146 {
7147         de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "barrier", "OpControlBarrier"));
7148         map<string, string> fragments;
7149
7150         // A barrier inside a function body.
7151         fragments["pre_main"] =
7152                 "%Workgroup = OpConstant %i32 2\n"
7153                 "%SequentiallyConsistent = OpConstant %i32 0x10\n";
7154         fragments["testfun"] =
7155                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
7156                 "%param1 = OpFunctionParameter %v4f32\n"
7157                 "%label_testfun = OpLabel\n"
7158                 "OpControlBarrier %Workgroup %Workgroup %SequentiallyConsistent\n"
7159                 "OpReturnValue %param1\n"
7160                 "OpFunctionEnd\n";
7161         addTessCtrlTest(testGroup.get(), "in_function", fragments);
7162
7163         // Common setup code for the following tests.
7164         fragments["pre_main"] =
7165                 "%Workgroup = OpConstant %i32 2\n"
7166                 "%SequentiallyConsistent = OpConstant %i32 0x10\n"
7167                 "%c_f32_5 = OpConstant %f32 5.\n";
7168         const string setupPercentZero =  // Begins %test_code function with code that sets %zero to 0u but cannot be optimized away.
7169                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
7170                 "%param1 = OpFunctionParameter %v4f32\n"
7171                 "%entry = OpLabel\n"
7172                 ";param1 components are between 0 and 1, so dot product is 4 or less\n"
7173                 "%dot = OpDot %f32 %param1 %param1\n"
7174                 "%div = OpFDiv %f32 %dot %c_f32_5\n"
7175                 "%zero = OpConvertFToU %u32 %div\n";
7176
7177         // Barriers inside OpSwitch branches.
7178         fragments["testfun"] =
7179                 setupPercentZero +
7180                 "OpSelectionMerge %switch_exit None\n"
7181                 "OpSwitch %zero %switch_default 0 %case0 1 %case1 ;should always go to %case0\n"
7182
7183                 "%case1 = OpLabel\n"
7184                 ";This barrier should never be executed, but its presence makes test failure more likely when there's a bug.\n"
7185                 "OpControlBarrier %Workgroup %Workgroup %SequentiallyConsistent\n"
7186                 "%wrong_branch_alert1 = OpVectorInsertDynamic %v4f32 %param1 %c_f32_0_5 %c_i32_0\n"
7187                 "OpBranch %switch_exit\n"
7188
7189                 "%switch_default = OpLabel\n"
7190                 "%wrong_branch_alert2 = OpVectorInsertDynamic %v4f32 %param1 %c_f32_0_5 %c_i32_0\n"
7191                 ";This barrier should never be executed, but its presence makes test failure more likely when there's a bug.\n"
7192                 "OpControlBarrier %Workgroup %Workgroup %SequentiallyConsistent\n"
7193                 "OpBranch %switch_exit\n"
7194
7195                 "%case0 = OpLabel\n"
7196                 "OpControlBarrier %Workgroup %Workgroup %SequentiallyConsistent\n"
7197                 "OpBranch %switch_exit\n"
7198
7199                 "%switch_exit = OpLabel\n"
7200                 "%ret = OpPhi %v4f32 %param1 %case0 %wrong_branch_alert1 %case1 %wrong_branch_alert2 %switch_default\n"
7201                 "OpReturnValue %ret\n"
7202                 "OpFunctionEnd\n";
7203         addTessCtrlTest(testGroup.get(), "in_switch", fragments);
7204
7205         // Barriers inside if-then-else.
7206         fragments["testfun"] =
7207                 setupPercentZero +
7208                 "%eq0 = OpIEqual %bool %zero %c_u32_0\n"
7209                 "OpSelectionMerge %exit DontFlatten\n"
7210                 "OpBranchConditional %eq0 %then %else\n"
7211
7212                 "%else = OpLabel\n"
7213                 ";This barrier should never be executed, but its presence makes test failure more likely when there's a bug.\n"
7214                 "OpControlBarrier %Workgroup %Workgroup %SequentiallyConsistent\n"
7215                 "%wrong_branch_alert = OpVectorInsertDynamic %v4f32 %param1 %c_f32_0_5 %c_i32_0\n"
7216                 "OpBranch %exit\n"
7217
7218                 "%then = OpLabel\n"
7219                 "OpControlBarrier %Workgroup %Workgroup %SequentiallyConsistent\n"
7220                 "OpBranch %exit\n"
7221
7222                 "%exit = OpLabel\n"
7223                 "%ret = OpPhi %v4f32 %param1 %then %wrong_branch_alert %else\n"
7224                 "OpReturnValue %ret\n"
7225                 "OpFunctionEnd\n";
7226         addTessCtrlTest(testGroup.get(), "in_if", fragments);
7227
7228         // A barrier after control-flow reconvergence, tempting the compiler to attempt something like this:
7229         // http://lists.llvm.org/pipermail/llvm-dev/2009-October/026317.html.
7230         fragments["testfun"] =
7231                 setupPercentZero +
7232                 "%thread_id = OpLoad %i32 %BP_gl_InvocationID\n"
7233                 "%thread0 = OpIEqual %bool %thread_id %c_i32_0\n"
7234                 "OpSelectionMerge %exit DontFlatten\n"
7235                 "OpBranchConditional %thread0 %then %else\n"
7236
7237                 "%else = OpLabel\n"
7238                 "%val0 = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
7239                 "OpBranch %exit\n"
7240
7241                 "%then = OpLabel\n"
7242                 "%val1 = OpVectorExtractDynamic %f32 %param1 %zero\n"
7243                 "OpBranch %exit\n"
7244
7245                 "%exit = OpLabel\n"
7246                 "%val = OpPhi %f32 %val0 %else %val1 %then\n"
7247                 "OpControlBarrier %Workgroup %Workgroup %SequentiallyConsistent\n"
7248                 "%ret = OpVectorInsertDynamic %v4f32 %param1 %val %zero\n"
7249                 "OpReturnValue %ret\n"
7250                 "OpFunctionEnd\n";
7251         addTessCtrlTest(testGroup.get(), "after_divergent_if", fragments);
7252
7253         // A barrier inside a loop.
7254         fragments["pre_main"] =
7255                 "%Workgroup = OpConstant %i32 2\n"
7256                 "%SequentiallyConsistent = OpConstant %i32 0x10\n"
7257                 "%c_f32_10 = OpConstant %f32 10.\n";
7258         fragments["testfun"] =
7259                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
7260                 "%param1 = OpFunctionParameter %v4f32\n"
7261                 "%entry = OpLabel\n"
7262                 "%val0 = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
7263                 "OpBranch %loop\n"
7264
7265                 ";adds 4, 3, 2, and 1 to %val0\n"
7266                 "%loop = OpLabel\n"
7267                 "%count = OpPhi %i32 %c_i32_4 %entry %count__ %loop\n"
7268                 "%val1 = OpPhi %f32 %val0 %entry %val %loop\n"
7269                 "OpControlBarrier %Workgroup %Workgroup %SequentiallyConsistent\n"
7270                 "%fcount = OpConvertSToF %f32 %count\n"
7271                 "%val = OpFAdd %f32 %val1 %fcount\n"
7272                 "%count__ = OpISub %i32 %count %c_i32_1\n"
7273                 "%again = OpSGreaterThan %bool %count__ %c_i32_0\n"
7274                 "OpLoopMerge %exit %loop None\n"
7275                 "OpBranchConditional %again %loop %exit\n"
7276
7277                 "%exit = OpLabel\n"
7278                 "%same = OpFSub %f32 %val %c_f32_10\n"
7279                 "%ret = OpVectorInsertDynamic %v4f32 %param1 %same %c_i32_0\n"
7280                 "OpReturnValue %ret\n"
7281                 "OpFunctionEnd\n";
7282         addTessCtrlTest(testGroup.get(), "in_loop", fragments);
7283
7284         return testGroup.release();
7285 }
7286
7287 // Test for the OpFRem instruction.
7288 tcu::TestCaseGroup* createFRemTests(tcu::TestContext& testCtx)
7289 {
7290         de::MovePtr<tcu::TestCaseGroup>         testGroup(new tcu::TestCaseGroup(testCtx, "frem", "OpFRem"));
7291         map<string, string>                                     fragments;
7292         RGBA                                                            inputColors[4];
7293         RGBA                                                            outputColors[4];
7294
7295         fragments["pre_main"]                            =
7296                 "%c_f32_3 = OpConstant %f32 3.0\n"
7297                 "%c_f32_n3 = OpConstant %f32 -3.0\n"
7298                 "%c_f32_4 = OpConstant %f32 4.0\n"
7299                 "%c_f32_p75 = OpConstant %f32 0.75\n"
7300                 "%c_v4f32_p75_p75_p75_p75 = OpConstantComposite %v4f32 %c_f32_p75 %c_f32_p75 %c_f32_p75 %c_f32_p75 \n"
7301                 "%c_v4f32_4_4_4_4 = OpConstantComposite %v4f32 %c_f32_4 %c_f32_4 %c_f32_4 %c_f32_4\n"
7302                 "%c_v4f32_3_n3_3_n3 = OpConstantComposite %v4f32 %c_f32_3 %c_f32_n3 %c_f32_3 %c_f32_n3\n";
7303
7304         // The test does the following.
7305         // vec4 result = (param1 * 8.0) - 4.0;
7306         // return (frem(result.x,3) + 0.75, frem(result.y, -3) + 0.75, 0, 1)
7307         fragments["testfun"]                             =
7308                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
7309                 "%param1 = OpFunctionParameter %v4f32\n"
7310                 "%label_testfun = OpLabel\n"
7311                 "%v_times_8 = OpVectorTimesScalar %v4f32 %param1 %c_f32_8\n"
7312                 "%minus_4 = OpFSub %v4f32 %v_times_8 %c_v4f32_4_4_4_4\n"
7313                 "%frem = OpFRem %v4f32 %minus_4 %c_v4f32_3_n3_3_n3\n"
7314                 "%added = OpFAdd %v4f32 %frem %c_v4f32_p75_p75_p75_p75\n"
7315                 "%xyz_1 = OpVectorInsertDynamic %v4f32 %added %c_f32_1 %c_i32_3\n"
7316                 "%xy_0_1 = OpVectorInsertDynamic %v4f32 %xyz_1 %c_f32_0 %c_i32_2\n"
7317                 "OpReturnValue %xy_0_1\n"
7318                 "OpFunctionEnd\n";
7319
7320
7321         inputColors[0]          = RGBA(16,      16,             0, 255);
7322         inputColors[1]          = RGBA(232, 232,        0, 255);
7323         inputColors[2]          = RGBA(232, 16,         0, 255);
7324         inputColors[3]          = RGBA(16,      232,    0, 255);
7325
7326         outputColors[0]         = RGBA(64,      64,             0, 255);
7327         outputColors[1]         = RGBA(255, 255,        0, 255);
7328         outputColors[2]         = RGBA(255, 64,         0, 255);
7329         outputColors[3]         = RGBA(64,      255,    0, 255);
7330
7331         createTestsForAllStages("frem", inputColors, outputColors, fragments, testGroup.get());
7332         return testGroup.release();
7333 }
7334
7335 // Test for the OpSRem instruction.
7336 tcu::TestCaseGroup* createOpSRemGraphicsTests(tcu::TestContext& testCtx, qpTestResult negFailResult)
7337 {
7338         de::MovePtr<tcu::TestCaseGroup>         testGroup(new tcu::TestCaseGroup(testCtx, "srem", "OpSRem"));
7339         map<string, string>                                     fragments;
7340
7341         fragments["pre_main"]                            =
7342                 "%c_f32_255 = OpConstant %f32 255.0\n"
7343                 "%c_i32_128 = OpConstant %i32 128\n"
7344                 "%c_i32_255 = OpConstant %i32 255\n"
7345                 "%c_v4f32_255 = OpConstantComposite %v4f32 %c_f32_255 %c_f32_255 %c_f32_255 %c_f32_255 \n"
7346                 "%c_v4f32_0_5 = OpConstantComposite %v4f32 %c_f32_0_5 %c_f32_0_5 %c_f32_0_5 %c_f32_0_5 \n"
7347                 "%c_v4i32_128 = OpConstantComposite %v4i32 %c_i32_128 %c_i32_128 %c_i32_128 %c_i32_128 \n";
7348
7349         // The test does the following.
7350         // ivec4 ints = int(param1 * 255.0 + 0.5) - 128;
7351         // ivec4 result = ivec4(srem(ints.x, ints.y), srem(ints.y, ints.z), srem(ints.z, ints.x), 255);
7352         // return float(result + 128) / 255.0;
7353         fragments["testfun"]                             =
7354                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
7355                 "%param1 = OpFunctionParameter %v4f32\n"
7356                 "%label_testfun = OpLabel\n"
7357                 "%div255 = OpFMul %v4f32 %param1 %c_v4f32_255\n"
7358                 "%add0_5 = OpFAdd %v4f32 %div255 %c_v4f32_0_5\n"
7359                 "%uints_in = OpConvertFToS %v4i32 %add0_5\n"
7360                 "%ints_in = OpISub %v4i32 %uints_in %c_v4i32_128\n"
7361                 "%x_in = OpCompositeExtract %i32 %ints_in 0\n"
7362                 "%y_in = OpCompositeExtract %i32 %ints_in 1\n"
7363                 "%z_in = OpCompositeExtract %i32 %ints_in 2\n"
7364                 "%x_out = OpSRem %i32 %x_in %y_in\n"
7365                 "%y_out = OpSRem %i32 %y_in %z_in\n"
7366                 "%z_out = OpSRem %i32 %z_in %x_in\n"
7367                 "%ints_out = OpCompositeConstruct %v4i32 %x_out %y_out %z_out %c_i32_255\n"
7368                 "%ints_offset = OpIAdd %v4i32 %ints_out %c_v4i32_128\n"
7369                 "%f_ints_offset = OpConvertSToF %v4f32 %ints_offset\n"
7370                 "%float_out = OpFDiv %v4f32 %f_ints_offset %c_v4f32_255\n"
7371                 "OpReturnValue %float_out\n"
7372                 "OpFunctionEnd\n";
7373
7374         const struct CaseParams
7375         {
7376                 const char*             name;
7377                 const char*             failMessageTemplate;    // customized status message
7378                 qpTestResult    failResult;                             // override status on failure
7379                 int                             operands[4][3];                 // four (x, y, z) vectors of operands
7380                 int                             results[4][3];                  // four (x, y, z) vectors of results
7381         } cases[] =
7382         {
7383                 {
7384                         "positive",
7385                         "${reason}",
7386                         QP_TEST_RESULT_FAIL,
7387                         { { 5, 12, 17 }, { 5, 5, 7 }, { 75, 8, 81 }, { 25, 60, 100 } },                 // operands
7388                         { { 5, 12,  2 }, { 0, 5, 2 }, {  3, 8,  6 }, { 25, 60,   0 } },                 // results
7389                 },
7390                 {
7391                         "all",
7392                         "Inconsistent results, but within specification: ${reason}",
7393                         negFailResult,                                                                                                                  // negative operands, not required by the spec
7394                         { { 5, 12, -17 }, { -5, -5, 7 }, { 75, 8, -81 }, { 25, -60, 100 } },    // operands
7395                         { { 5, 12,  -2 }, {  0, -5, 2 }, {  3, 8,  -6 }, { 25, -60,   0 } },    // results
7396                 },
7397         };
7398         // If either operand is negative the result is undefined. Some implementations may still return correct values.
7399
7400         for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
7401         {
7402                 const CaseParams&       params                  = cases[caseNdx];
7403                 RGBA                            inputColors[4];
7404                 RGBA                            outputColors[4];
7405
7406                 for (int i = 0; i < 4; ++i)
7407                 {
7408                         inputColors [i] = RGBA(params.operands[i][0] + 128, params.operands[i][1] + 128, params.operands[i][2] + 128, 255);
7409                         outputColors[i] = RGBA(params.results [i][0] + 128, params.results [i][1] + 128, params.results [i][2] + 128, 255);
7410                 }
7411
7412                 createTestsForAllStages(params.name, inputColors, outputColors, fragments, testGroup.get(), params.failResult, params.failMessageTemplate);
7413         }
7414
7415         return testGroup.release();
7416 }
7417
7418 // Test for the OpSMod instruction.
7419 tcu::TestCaseGroup* createOpSModGraphicsTests(tcu::TestContext& testCtx, qpTestResult negFailResult)
7420 {
7421         de::MovePtr<tcu::TestCaseGroup>         testGroup(new tcu::TestCaseGroup(testCtx, "smod", "OpSMod"));
7422         map<string, string>                                     fragments;
7423
7424         fragments["pre_main"]                            =
7425                 "%c_f32_255 = OpConstant %f32 255.0\n"
7426                 "%c_i32_128 = OpConstant %i32 128\n"
7427                 "%c_i32_255 = OpConstant %i32 255\n"
7428                 "%c_v4f32_255 = OpConstantComposite %v4f32 %c_f32_255 %c_f32_255 %c_f32_255 %c_f32_255 \n"
7429                 "%c_v4f32_0_5 = OpConstantComposite %v4f32 %c_f32_0_5 %c_f32_0_5 %c_f32_0_5 %c_f32_0_5 \n"
7430                 "%c_v4i32_128 = OpConstantComposite %v4i32 %c_i32_128 %c_i32_128 %c_i32_128 %c_i32_128 \n";
7431
7432         // The test does the following.
7433         // ivec4 ints = int(param1 * 255.0 + 0.5) - 128;
7434         // ivec4 result = ivec4(smod(ints.x, ints.y), smod(ints.y, ints.z), smod(ints.z, ints.x), 255);
7435         // return float(result + 128) / 255.0;
7436         fragments["testfun"]                             =
7437                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
7438                 "%param1 = OpFunctionParameter %v4f32\n"
7439                 "%label_testfun = OpLabel\n"
7440                 "%div255 = OpFMul %v4f32 %param1 %c_v4f32_255\n"
7441                 "%add0_5 = OpFAdd %v4f32 %div255 %c_v4f32_0_5\n"
7442                 "%uints_in = OpConvertFToS %v4i32 %add0_5\n"
7443                 "%ints_in = OpISub %v4i32 %uints_in %c_v4i32_128\n"
7444                 "%x_in = OpCompositeExtract %i32 %ints_in 0\n"
7445                 "%y_in = OpCompositeExtract %i32 %ints_in 1\n"
7446                 "%z_in = OpCompositeExtract %i32 %ints_in 2\n"
7447                 "%x_out = OpSMod %i32 %x_in %y_in\n"
7448                 "%y_out = OpSMod %i32 %y_in %z_in\n"
7449                 "%z_out = OpSMod %i32 %z_in %x_in\n"
7450                 "%ints_out = OpCompositeConstruct %v4i32 %x_out %y_out %z_out %c_i32_255\n"
7451                 "%ints_offset = OpIAdd %v4i32 %ints_out %c_v4i32_128\n"
7452                 "%f_ints_offset = OpConvertSToF %v4f32 %ints_offset\n"
7453                 "%float_out = OpFDiv %v4f32 %f_ints_offset %c_v4f32_255\n"
7454                 "OpReturnValue %float_out\n"
7455                 "OpFunctionEnd\n";
7456
7457         const struct CaseParams
7458         {
7459                 const char*             name;
7460                 const char*             failMessageTemplate;    // customized status message
7461                 qpTestResult    failResult;                             // override status on failure
7462                 int                             operands[4][3];                 // four (x, y, z) vectors of operands
7463                 int                             results[4][3];                  // four (x, y, z) vectors of results
7464         } cases[] =
7465         {
7466                 {
7467                         "positive",
7468                         "${reason}",
7469                         QP_TEST_RESULT_FAIL,
7470                         { { 5, 12, 17 }, { 5, 5, 7 }, { 75, 8, 81 }, { 25, 60, 100 } },                         // operands
7471                         { { 5, 12,  2 }, { 0, 5, 2 }, {  3, 8,  6 }, { 25, 60,   0 } },                         // results
7472                 },
7473                 {
7474                         "all",
7475                         "Inconsistent results, but within specification: ${reason}",
7476                         negFailResult,                                                                                                                          // negative operands, not required by the spec
7477                         { { 5, 12, -17 }, { -5, -5,  7 }, { 75,   8, -81 }, {  25, -60, 100 } },        // operands
7478                         { { 5, -5,   3 }, {  0,  2, -3 }, {  3, -73,  69 }, { -35,  40,   0 } },        // results
7479                 },
7480         };
7481         // If either operand is negative the result is undefined. Some implementations may still return correct values.
7482
7483         for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
7484         {
7485                 const CaseParams&       params                  = cases[caseNdx];
7486                 RGBA                            inputColors[4];
7487                 RGBA                            outputColors[4];
7488
7489                 for (int i = 0; i < 4; ++i)
7490                 {
7491                         inputColors [i] = RGBA(params.operands[i][0] + 128, params.operands[i][1] + 128, params.operands[i][2] + 128, 255);
7492                         outputColors[i] = RGBA(params.results [i][0] + 128, params.results [i][1] + 128, params.results [i][2] + 128, 255);
7493                 }
7494
7495                 createTestsForAllStages(params.name, inputColors, outputColors, fragments, testGroup.get(), params.failResult, params.failMessageTemplate);
7496         }
7497
7498         return testGroup.release();
7499 }
7500
7501 enum IntegerType
7502 {
7503         INTEGER_TYPE_SIGNED_16,
7504         INTEGER_TYPE_SIGNED_32,
7505         INTEGER_TYPE_SIGNED_64,
7506
7507         INTEGER_TYPE_UNSIGNED_16,
7508         INTEGER_TYPE_UNSIGNED_32,
7509         INTEGER_TYPE_UNSIGNED_64,
7510 };
7511
7512 const string getBitWidthStr (IntegerType type)
7513 {
7514         switch (type)
7515         {
7516                 case INTEGER_TYPE_SIGNED_16:
7517                 case INTEGER_TYPE_UNSIGNED_16:  return "16";
7518
7519                 case INTEGER_TYPE_SIGNED_32:
7520                 case INTEGER_TYPE_UNSIGNED_32:  return "32";
7521
7522                 case INTEGER_TYPE_SIGNED_64:
7523                 case INTEGER_TYPE_UNSIGNED_64:  return "64";
7524
7525                 default:                                                DE_ASSERT(false);
7526                                                                                 return "";
7527         }
7528 }
7529
7530 const string getByteWidthStr (IntegerType type)
7531 {
7532         switch (type)
7533         {
7534                 case INTEGER_TYPE_SIGNED_16:
7535                 case INTEGER_TYPE_UNSIGNED_16:  return "2";
7536
7537                 case INTEGER_TYPE_SIGNED_32:
7538                 case INTEGER_TYPE_UNSIGNED_32:  return "4";
7539
7540                 case INTEGER_TYPE_SIGNED_64:
7541                 case INTEGER_TYPE_UNSIGNED_64:  return "8";
7542
7543                 default:                                                DE_ASSERT(false);
7544                                                                                 return "";
7545         }
7546 }
7547
7548 bool isSigned (IntegerType type)
7549 {
7550         return (type <= INTEGER_TYPE_SIGNED_64);
7551 }
7552
7553 const string getTypeName (IntegerType type)
7554 {
7555         string prefix = isSigned(type) ? "" : "u";
7556         return prefix + "int" + getBitWidthStr(type);
7557 }
7558
7559 const string getTestName (IntegerType from, IntegerType to)
7560 {
7561         return getTypeName(from) + "_to_" + getTypeName(to);
7562 }
7563
7564 const string getAsmTypeDeclaration (IntegerType type)
7565 {
7566         string sign = isSigned(type) ? " 1" : " 0";
7567         return "OpTypeInt " + getBitWidthStr(type) + sign;
7568 }
7569
7570 const string getAsmTypeName (IntegerType type)
7571 {
7572         const string prefix = isSigned(type) ? "%i" : "%u";
7573         return prefix + getBitWidthStr(type);
7574 }
7575
7576 template<typename T>
7577 BufferSp getSpecializedBuffer (deInt64 number)
7578 {
7579         return BufferSp(new Buffer<T>(vector<T>(1, (T)number)));
7580 }
7581
7582 BufferSp getBuffer (IntegerType type, deInt64 number)
7583 {
7584         switch (type)
7585         {
7586                 case INTEGER_TYPE_SIGNED_16:    return getSpecializedBuffer<deInt16>(number);
7587                 case INTEGER_TYPE_SIGNED_32:    return getSpecializedBuffer<deInt32>(number);
7588                 case INTEGER_TYPE_SIGNED_64:    return getSpecializedBuffer<deInt64>(number);
7589
7590                 case INTEGER_TYPE_UNSIGNED_16:  return getSpecializedBuffer<deUint16>(number);
7591                 case INTEGER_TYPE_UNSIGNED_32:  return getSpecializedBuffer<deUint32>(number);
7592                 case INTEGER_TYPE_UNSIGNED_64:  return getSpecializedBuffer<deUint64>(number);
7593
7594                 default:                                                DE_ASSERT(false);
7595                                                                                 return BufferSp(new Buffer<deInt32>(vector<deInt32>(1, 0)));
7596         }
7597 }
7598
7599 bool usesInt16 (IntegerType from, IntegerType to)
7600 {
7601         return (from == INTEGER_TYPE_SIGNED_16 || from == INTEGER_TYPE_UNSIGNED_16
7602                         || to == INTEGER_TYPE_SIGNED_16 || to == INTEGER_TYPE_UNSIGNED_16);
7603 }
7604
7605 bool usesInt64 (IntegerType from, IntegerType to)
7606 {
7607         return (from == INTEGER_TYPE_SIGNED_64 || from == INTEGER_TYPE_UNSIGNED_64
7608                         || to == INTEGER_TYPE_SIGNED_64 || to == INTEGER_TYPE_UNSIGNED_64);
7609 }
7610
7611 ComputeTestFeatures getConversionUsedFeatures (IntegerType from, IntegerType to)
7612 {
7613         if (usesInt16(from, to))
7614         {
7615                 if (usesInt64(from, to))
7616                 {
7617                         return COMPUTE_TEST_USES_INT16_INT64;
7618                 }
7619                 else
7620                 {
7621                         return COMPUTE_TEST_USES_INT16;
7622                 }
7623         }
7624         else
7625         {
7626                 return COMPUTE_TEST_USES_INT64;
7627         }
7628 }
7629
7630 struct ConvertCase
7631 {
7632         ConvertCase (IntegerType from, IntegerType to, deInt64 number)
7633         : m_fromType            (from)
7634         , m_toType                      (to)
7635         , m_features            (getConversionUsedFeatures(from, to))
7636         , m_name                        (getTestName(from, to))
7637         , m_inputBuffer         (getBuffer(from, number))
7638         , m_outputBuffer        (getBuffer(to, number))
7639         {
7640                 m_asmTypes["inputType"]         = getAsmTypeName(from);
7641                 m_asmTypes["outputType"]        = getAsmTypeName(to);
7642
7643                 if (m_features == COMPUTE_TEST_USES_INT16)
7644                 {
7645                         m_asmTypes["int_capabilities"] = "OpCapability Int16\n";
7646                         m_asmTypes["int_additional_decl"] = "%i16        = OpTypeInt 16 1\n%u16        = OpTypeInt 16 0\n";
7647                 }
7648                 else if (m_features == COMPUTE_TEST_USES_INT64)
7649                 {
7650                         m_asmTypes["int_capabilities"] = "OpCapability Int64\n";
7651                         m_asmTypes["int_additional_decl"] = "%i64        = OpTypeInt 64 1\n%u64        = OpTypeInt 64 0\n";
7652                 }
7653                 else if (m_features == COMPUTE_TEST_USES_INT16_INT64)
7654                 {
7655                         m_asmTypes["int_capabilities"] = string("OpCapability Int16\n") +
7656                                                                                                         "OpCapability Int64\n";
7657                         m_asmTypes["int_additional_decl"] =     "%i16        = OpTypeInt 16 1\n%u16        = OpTypeInt 16 0\n"
7658                                                                 "%i64        = OpTypeInt 64 1\n%u64        = OpTypeInt 64 0\n";
7659                 }
7660                 else
7661                 {
7662                         DE_ASSERT(false);
7663                 }
7664         }
7665
7666         IntegerType                             m_fromType;
7667         IntegerType                             m_toType;
7668         ComputeTestFeatures             m_features;
7669         string                                  m_name;
7670         map<string, string>             m_asmTypes;
7671         BufferSp                                m_inputBuffer;
7672         BufferSp                                m_outputBuffer;
7673 };
7674
7675 const string getConvertCaseShaderStr (const string& instruction, const ConvertCase& convertCase)
7676 {
7677         map<string, string> params = convertCase.m_asmTypes;
7678
7679         params["instruction"] = instruction;
7680
7681         params["inDecorator"] = getByteWidthStr(convertCase.m_fromType);
7682         params["outDecorator"] = getByteWidthStr(convertCase.m_toType);
7683
7684         const StringTemplate shader (
7685                 "OpCapability Shader\n"
7686                 "${int_capabilities}"
7687                 "OpMemoryModel Logical GLSL450\n"
7688                 "OpEntryPoint GLCompute %main \"main\" %id\n"
7689                 "OpExecutionMode %main LocalSize 1 1 1\n"
7690                 "OpSource GLSL 430\n"
7691                 "OpName %main           \"main\"\n"
7692                 "OpName %id             \"gl_GlobalInvocationID\"\n"
7693                 // Decorators
7694                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
7695                 "OpDecorate %indata DescriptorSet 0\n"
7696                 "OpDecorate %indata Binding 0\n"
7697                 "OpDecorate %outdata DescriptorSet 0\n"
7698                 "OpDecorate %outdata Binding 1\n"
7699                 "OpDecorate %in_arr ArrayStride ${inDecorator}\n"
7700                 "OpDecorate %out_arr ArrayStride ${outDecorator}\n"
7701                 "OpDecorate %in_buf BufferBlock\n"
7702                 "OpDecorate %out_buf BufferBlock\n"
7703                 "OpMemberDecorate %in_buf 0 Offset 0\n"
7704                 "OpMemberDecorate %out_buf 0 Offset 0\n"
7705                 // Base types
7706                 "%void       = OpTypeVoid\n"
7707                 "%voidf      = OpTypeFunction %void\n"
7708                 "%u32        = OpTypeInt 32 0\n"
7709                 "%i32        = OpTypeInt 32 1\n"
7710                 "${int_additional_decl}"
7711                 "%uvec3      = OpTypeVector %u32 3\n"
7712                 "%uvec3ptr   = OpTypePointer Input %uvec3\n"
7713                 // Derived types
7714                 "%in_ptr     = OpTypePointer Uniform ${inputType}\n"
7715                 "%out_ptr    = OpTypePointer Uniform ${outputType}\n"
7716                 "%in_arr     = OpTypeRuntimeArray ${inputType}\n"
7717                 "%out_arr    = OpTypeRuntimeArray ${outputType}\n"
7718                 "%in_buf     = OpTypeStruct %in_arr\n"
7719                 "%out_buf    = OpTypeStruct %out_arr\n"
7720                 "%in_bufptr  = OpTypePointer Uniform %in_buf\n"
7721                 "%out_bufptr = OpTypePointer Uniform %out_buf\n"
7722                 "%indata     = OpVariable %in_bufptr Uniform\n"
7723                 "%outdata    = OpVariable %out_bufptr Uniform\n"
7724                 "%inputptr   = OpTypePointer Input ${inputType}\n"
7725                 "%id         = OpVariable %uvec3ptr Input\n"
7726                 // Constants
7727                 "%zero       = OpConstant %i32 0\n"
7728                 // Main function
7729                 "%main       = OpFunction %void None %voidf\n"
7730                 "%label      = OpLabel\n"
7731                 "%idval      = OpLoad %uvec3 %id\n"
7732                 "%x          = OpCompositeExtract %u32 %idval 0\n"
7733                 "%inloc      = OpAccessChain %in_ptr %indata %zero %x\n"
7734                 "%outloc     = OpAccessChain %out_ptr %outdata %zero %x\n"
7735                 "%inval      = OpLoad ${inputType} %inloc\n"
7736                 "%conv       = ${instruction} ${outputType} %inval\n"
7737                 "              OpStore %outloc %conv\n"
7738                 "              OpReturn\n"
7739                 "              OpFunctionEnd\n"
7740         );
7741
7742         return shader.specialize(params);
7743 }
7744
7745 void createSConvertCases (vector<ConvertCase>& testCases)
7746 {
7747         // Convert int to int
7748         testCases.push_back(ConvertCase(INTEGER_TYPE_SIGNED_16, INTEGER_TYPE_SIGNED_32,         14669));
7749         testCases.push_back(ConvertCase(INTEGER_TYPE_SIGNED_16, INTEGER_TYPE_SIGNED_64,         3341));
7750
7751         testCases.push_back(ConvertCase(INTEGER_TYPE_SIGNED_32, INTEGER_TYPE_SIGNED_64,         973610259));
7752
7753         // Convert int to unsigned int
7754         testCases.push_back(ConvertCase(INTEGER_TYPE_SIGNED_16, INTEGER_TYPE_UNSIGNED_32,       9288));
7755         testCases.push_back(ConvertCase(INTEGER_TYPE_SIGNED_16, INTEGER_TYPE_UNSIGNED_64,       15460));
7756
7757         testCases.push_back(ConvertCase(INTEGER_TYPE_SIGNED_32, INTEGER_TYPE_UNSIGNED_64,       346213461));
7758 }
7759
7760 //  Test for the OpSConvert instruction.
7761 tcu::TestCaseGroup* createSConvertTests (tcu::TestContext& testCtx)
7762 {
7763         const string instruction                                ("OpSConvert");
7764         de::MovePtr<tcu::TestCaseGroup> group   (new tcu::TestCaseGroup(testCtx, "sconvert", "OpSConvert"));
7765         vector<ConvertCase>                             testCases;
7766         createSConvertCases(testCases);
7767
7768         for (vector<ConvertCase>::const_iterator test = testCases.begin(); test != testCases.end(); ++test)
7769         {
7770                 ComputeShaderSpec       spec;
7771
7772                 spec.assembly = getConvertCaseShaderStr(instruction, *test);
7773                 spec.inputs.push_back(test->m_inputBuffer);
7774                 spec.outputs.push_back(test->m_outputBuffer);
7775                 spec.numWorkGroups = IVec3(1, 1, 1);
7776
7777                 group->addChild(new SpvAsmComputeShaderCase(testCtx, test->m_name.c_str(), "Convert integers with OpSConvert.", spec, test->m_features));
7778         }
7779
7780         return group.release();
7781 }
7782
7783 void createUConvertCases (vector<ConvertCase>& testCases)
7784 {
7785         // Convert unsigned int to unsigned int
7786         testCases.push_back(ConvertCase(INTEGER_TYPE_UNSIGNED_16,       INTEGER_TYPE_UNSIGNED_32,       60653));
7787         testCases.push_back(ConvertCase(INTEGER_TYPE_UNSIGNED_16,       INTEGER_TYPE_UNSIGNED_64,       17991));
7788
7789         testCases.push_back(ConvertCase(INTEGER_TYPE_UNSIGNED_32,       INTEGER_TYPE_UNSIGNED_64,       904256275));
7790
7791         // Convert unsigned int to int
7792         testCases.push_back(ConvertCase(INTEGER_TYPE_UNSIGNED_16,       INTEGER_TYPE_SIGNED_32,         38002));
7793         testCases.push_back(ConvertCase(INTEGER_TYPE_UNSIGNED_16,       INTEGER_TYPE_SIGNED_64,         64921));
7794
7795         testCases.push_back(ConvertCase(INTEGER_TYPE_UNSIGNED_32,       INTEGER_TYPE_SIGNED_64,         4294956295ll));
7796 }
7797
7798 //  Test for the OpUConvert instruction.
7799 tcu::TestCaseGroup* createUConvertTests (tcu::TestContext& testCtx)
7800 {
7801         const string instruction                                ("OpUConvert");
7802         de::MovePtr<tcu::TestCaseGroup> group   (new tcu::TestCaseGroup(testCtx, "uconvert", "OpUConvert"));
7803         vector<ConvertCase>                             testCases;
7804         createUConvertCases(testCases);
7805
7806         for (vector<ConvertCase>::const_iterator test = testCases.begin(); test != testCases.end(); ++test)
7807         {
7808                 ComputeShaderSpec       spec;
7809
7810                 spec.assembly = getConvertCaseShaderStr(instruction, *test);
7811                 spec.inputs.push_back(test->m_inputBuffer);
7812                 spec.outputs.push_back(test->m_outputBuffer);
7813                 spec.numWorkGroups = IVec3(1, 1, 1);
7814
7815                 group->addChild(new SpvAsmComputeShaderCase(testCtx, test->m_name.c_str(), "Convert integers with OpUConvert.", spec, test->m_features));
7816         }
7817         return group.release();
7818 }
7819
7820 const string getNumberTypeName (const NumberType type)
7821 {
7822         if (type == NUMBERTYPE_INT32)
7823         {
7824                 return "int";
7825         }
7826         else if (type == NUMBERTYPE_UINT32)
7827         {
7828                 return "uint";
7829         }
7830         else if (type == NUMBERTYPE_FLOAT32)
7831         {
7832                 return "float";
7833         }
7834         else
7835         {
7836                 DE_ASSERT(false);
7837                 return "";
7838         }
7839 }
7840
7841 deInt32 getInt(de::Random& rnd)
7842 {
7843         return rnd.getInt(std::numeric_limits<int>::min(), std::numeric_limits<int>::max());
7844 }
7845
7846 const string repeatString (const string& str, int times)
7847 {
7848         string filler;
7849         for (int i = 0; i < times; ++i)
7850         {
7851                 filler += str;
7852         }
7853         return filler;
7854 }
7855
7856 const string getRandomConstantString (const NumberType type, de::Random& rnd)
7857 {
7858         if (type == NUMBERTYPE_INT32)
7859         {
7860                 return numberToString<deInt32>(getInt(rnd));
7861         }
7862         else if (type == NUMBERTYPE_UINT32)
7863         {
7864                 return numberToString<deUint32>(rnd.getUint32());
7865         }
7866         else if (type == NUMBERTYPE_FLOAT32)
7867         {
7868                 return numberToString<float>(rnd.getFloat());
7869         }
7870         else
7871         {
7872                 DE_ASSERT(false);
7873                 return "";
7874         }
7875 }
7876
7877 void createVectorCompositeCases (vector<map<string, string> >& testCases, de::Random& rnd, const NumberType type)
7878 {
7879         map<string, string> params;
7880
7881         // Vec2 to Vec4
7882         for (int width = 2; width <= 4; ++width)
7883         {
7884                 const string randomConst = numberToString(getInt(rnd));
7885                 const string widthStr = numberToString(width);
7886                 const string composite_type = "${customType}vec" + widthStr;
7887                 const int index = rnd.getInt(0, width-1);
7888
7889                 params["type"]                  = "vec";
7890                 params["name"]                  = params["type"] + "_" + widthStr;
7891                 params["compositeDecl"]         = composite_type + " = OpTypeVector ${customType} " + widthStr +"\n";
7892                 params["compositeType"]         = composite_type;
7893                 params["filler"]                = string("%filler    = OpConstant ${customType} ") + getRandomConstantString(type, rnd) + "\n";
7894                 params["compositeConstruct"]    = "%instance  = OpCompositeConstruct " + composite_type + repeatString(" %filler", width) + "\n";
7895                 params["indexes"]               = numberToString(index);
7896                 testCases.push_back(params);
7897         }
7898 }
7899
7900 void createArrayCompositeCases (vector<map<string, string> >& testCases, de::Random& rnd, const NumberType type)
7901 {
7902         const int limit = 10;
7903         map<string, string> params;
7904
7905         for (int width = 2; width <= limit; ++width)
7906         {
7907                 string randomConst = numberToString(getInt(rnd));
7908                 string widthStr = numberToString(width);
7909                 int index = rnd.getInt(0, width-1);
7910
7911                 params["type"]                  = "array";
7912                 params["name"]                  = params["type"] + "_" + widthStr;
7913                 params["compositeDecl"]         = string("%arraywidth = OpConstant %u32 " + widthStr + "\n")
7914                                                                                         +        "%composite = OpTypeArray ${customType} %arraywidth\n";
7915                 params["compositeType"]         = "%composite";
7916                 params["filler"]                = string("%filler    = OpConstant ${customType} ") + getRandomConstantString(type, rnd) + "\n";
7917                 params["compositeConstruct"]    = "%instance  = OpCompositeConstruct %composite" + repeatString(" %filler", width) + "\n";
7918                 params["indexes"]               = numberToString(index);
7919                 testCases.push_back(params);
7920         }
7921 }
7922
7923 void createStructCompositeCases (vector<map<string, string> >& testCases, de::Random& rnd, const NumberType type)
7924 {
7925         const int limit = 10;
7926         map<string, string> params;
7927
7928         for (int width = 2; width <= limit; ++width)
7929         {
7930                 string randomConst = numberToString(getInt(rnd));
7931                 int index = rnd.getInt(0, width-1);
7932
7933                 params["type"]                  = "struct";
7934                 params["name"]                  = params["type"] + "_" + numberToString(width);
7935                 params["compositeDecl"]         = "%composite = OpTypeStruct" + repeatString(" ${customType}", width) + "\n";
7936                 params["compositeType"]         = "%composite";
7937                 params["filler"]                = string("%filler    = OpConstant ${customType} ") + getRandomConstantString(type, rnd) + "\n";
7938                 params["compositeConstruct"]    = "%instance  = OpCompositeConstruct %composite" + repeatString(" %filler", width) + "\n";
7939                 params["indexes"]               = numberToString(index);
7940                 testCases.push_back(params);
7941         }
7942 }
7943
7944 void createMatrixCompositeCases (vector<map<string, string> >& testCases, de::Random& rnd, const NumberType type)
7945 {
7946         map<string, string> params;
7947
7948         // Vec2 to Vec4
7949         for (int width = 2; width <= 4; ++width)
7950         {
7951                 string widthStr = numberToString(width);
7952
7953                 for (int column = 2 ; column <= 4; ++column)
7954                 {
7955                         int index_0 = rnd.getInt(0, column-1);
7956                         int index_1 = rnd.getInt(0, width-1);
7957                         string columnStr = numberToString(column);
7958
7959                         params["type"]          = "matrix";
7960                         params["name"]          = params["type"] + "_" + widthStr + "x" + columnStr;
7961                         params["compositeDecl"] = string("%vectype   = OpTypeVector ${customType} " + widthStr + "\n")
7962                                                                                                 +        "%composite = OpTypeMatrix %vectype " + columnStr + "\n";
7963                         params["compositeType"] = "%composite";
7964
7965                         params["filler"]        = string("%filler    = OpConstant ${customType} ") + getRandomConstantString(type, rnd) + "\n"
7966                                                                                                 +        "%fillerVec = OpConstantComposite %vectype" + repeatString(" %filler", width) + "\n";
7967
7968                         params["compositeConstruct"]    = "%instance  = OpCompositeConstruct %composite" + repeatString(" %fillerVec", column) + "\n";
7969                         params["indexes"]       = numberToString(index_0) + " " + numberToString(index_1);
7970                         testCases.push_back(params);
7971                 }
7972         }
7973 }
7974
7975 void createCompositeCases (vector<map<string, string> >& testCases, de::Random& rnd, const NumberType type)
7976 {
7977         createVectorCompositeCases(testCases, rnd, type);
7978         createArrayCompositeCases(testCases, rnd, type);
7979         createStructCompositeCases(testCases, rnd, type);
7980         // Matrix only supports float types
7981         if (type == NUMBERTYPE_FLOAT32)
7982         {
7983                 createMatrixCompositeCases(testCases, rnd, type);
7984         }
7985 }
7986
7987 const string getAssemblyTypeDeclaration (const NumberType type)
7988 {
7989         switch (type)
7990         {
7991                 case NUMBERTYPE_INT32:          return "OpTypeInt 32 1";
7992                 case NUMBERTYPE_UINT32:         return "OpTypeInt 32 0";
7993                 case NUMBERTYPE_FLOAT32:        return "OpTypeFloat 32";
7994                 default:                        DE_ASSERT(false); return "";
7995         }
7996 }
7997
7998 const string getAssemblyTypeName (const NumberType type)
7999 {
8000         switch (type)
8001         {
8002                 case NUMBERTYPE_INT32:          return "%i32";
8003                 case NUMBERTYPE_UINT32:         return "%u32";
8004                 case NUMBERTYPE_FLOAT32:        return "%f32";
8005                 default:                        DE_ASSERT(false); return "";
8006         }
8007 }
8008
8009 const string specializeCompositeInsertShaderTemplate (const NumberType type, const map<string, string>& params)
8010 {
8011         map<string, string>     parameters(params);
8012
8013         const string customType = getAssemblyTypeName(type);
8014         map<string, string> substCustomType;
8015         substCustomType["customType"] = customType;
8016         parameters["compositeDecl"] = StringTemplate(parameters.at("compositeDecl")).specialize(substCustomType);
8017         parameters["compositeType"] = StringTemplate(parameters.at("compositeType")).specialize(substCustomType);
8018         parameters["compositeConstruct"] = StringTemplate(parameters.at("compositeConstruct")).specialize(substCustomType);
8019         parameters["filler"] = StringTemplate(parameters.at("filler")).specialize(substCustomType);
8020         parameters["customType"] = customType;
8021         parameters["compositeDecorator"] = (parameters["type"] == "array") ? "OpDecorate %composite ArrayStride 4\n" : "";
8022
8023         if (parameters.at("compositeType") != "%u32vec3")
8024         {
8025                 parameters["u32vec3Decl"] = "%u32vec3   = OpTypeVector %u32 3\n";
8026         }
8027
8028         return StringTemplate(
8029                 "OpCapability Shader\n"
8030                 "OpCapability Matrix\n"
8031                 "OpMemoryModel Logical GLSL450\n"
8032                 "OpEntryPoint GLCompute %main \"main\" %id\n"
8033                 "OpExecutionMode %main LocalSize 1 1 1\n"
8034
8035                 "OpSource GLSL 430\n"
8036                 "OpName %main           \"main\"\n"
8037                 "OpName %id             \"gl_GlobalInvocationID\"\n"
8038
8039                 // Decorators
8040                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
8041                 "OpDecorate %buf BufferBlock\n"
8042                 "OpDecorate %indata DescriptorSet 0\n"
8043                 "OpDecorate %indata Binding 0\n"
8044                 "OpDecorate %outdata DescriptorSet 0\n"
8045                 "OpDecorate %outdata Binding 1\n"
8046                 "OpDecorate %customarr ArrayStride 4\n"
8047                 "${compositeDecorator}"
8048                 "OpMemberDecorate %buf 0 Offset 0\n"
8049
8050                 // General types
8051                 "%void      = OpTypeVoid\n"
8052                 "%voidf     = OpTypeFunction %void\n"
8053                 "%u32       = OpTypeInt 32 0\n"
8054                 "%i32       = OpTypeInt 32 1\n"
8055                 "%f32       = OpTypeFloat 32\n"
8056
8057                 // Composite declaration
8058                 "${compositeDecl}"
8059
8060                 // Constants
8061                 "${filler}"
8062
8063                 "${u32vec3Decl:opt}"
8064                 "%uvec3ptr  = OpTypePointer Input %u32vec3\n"
8065
8066                 // Inherited from custom
8067                 "%customptr = OpTypePointer Uniform ${customType}\n"
8068                 "%customarr = OpTypeRuntimeArray ${customType}\n"
8069                 "%buf       = OpTypeStruct %customarr\n"
8070                 "%bufptr    = OpTypePointer Uniform %buf\n"
8071
8072                 "%indata    = OpVariable %bufptr Uniform\n"
8073                 "%outdata   = OpVariable %bufptr Uniform\n"
8074
8075                 "%id        = OpVariable %uvec3ptr Input\n"
8076                 "%zero      = OpConstant %i32 0\n"
8077
8078                 "%main      = OpFunction %void None %voidf\n"
8079                 "%label     = OpLabel\n"
8080                 "%idval     = OpLoad %u32vec3 %id\n"
8081                 "%x         = OpCompositeExtract %u32 %idval 0\n"
8082
8083                 "%inloc     = OpAccessChain %customptr %indata %zero %x\n"
8084                 "%outloc    = OpAccessChain %customptr %outdata %zero %x\n"
8085                 // Read the input value
8086                 "%inval     = OpLoad ${customType} %inloc\n"
8087                 // Create the composite and fill it
8088                 "${compositeConstruct}"
8089                 // Insert the input value to a place
8090                 "%instance2 = OpCompositeInsert ${compositeType} %inval %instance ${indexes}\n"
8091                 // Read back the value from the position
8092                 "%out_val   = OpCompositeExtract ${customType} %instance2 ${indexes}\n"
8093                 // Store it in the output position
8094                 "             OpStore %outloc %out_val\n"
8095                 "             OpReturn\n"
8096                 "             OpFunctionEnd\n"
8097         ).specialize(parameters);
8098 }
8099
8100 template<typename T>
8101 BufferSp createCompositeBuffer(T number)
8102 {
8103         return BufferSp(new Buffer<T>(vector<T>(1, number)));
8104 }
8105
8106 tcu::TestCaseGroup* createOpCompositeInsertGroup (tcu::TestContext& testCtx)
8107 {
8108         de::MovePtr<tcu::TestCaseGroup> group   (new tcu::TestCaseGroup(testCtx, "opcompositeinsert", "Test the OpCompositeInsert instruction"));
8109         de::Random                                              rnd             (deStringHash(group->getName()));
8110
8111         for (int type = NUMBERTYPE_INT32; type != NUMBERTYPE_END32; ++type)
8112         {
8113                 NumberType                                              numberType              = NumberType(type);
8114                 const string                                    typeName                = getNumberTypeName(numberType);
8115                 const string                                    description             = "Test the OpCompositeInsert instruction with " + typeName + "s";
8116                 de::MovePtr<tcu::TestCaseGroup> subGroup                (new tcu::TestCaseGroup(testCtx, typeName.c_str(), description.c_str()));
8117                 vector<map<string, string> >    testCases;
8118
8119                 createCompositeCases(testCases, rnd, numberType);
8120
8121                 for (vector<map<string, string> >::const_iterator test = testCases.begin(); test != testCases.end(); ++test)
8122                 {
8123                         ComputeShaderSpec       spec;
8124
8125                         spec.assembly = specializeCompositeInsertShaderTemplate(numberType, *test);
8126
8127                         switch (numberType)
8128                         {
8129                                 case NUMBERTYPE_INT32:
8130                                 {
8131                                         deInt32 number = getInt(rnd);
8132                                         spec.inputs.push_back(createCompositeBuffer<deInt32>(number));
8133                                         spec.outputs.push_back(createCompositeBuffer<deInt32>(number));
8134                                         break;
8135                                 }
8136                                 case NUMBERTYPE_UINT32:
8137                                 {
8138                                         deUint32 number = rnd.getUint32();
8139                                         spec.inputs.push_back(createCompositeBuffer<deUint32>(number));
8140                                         spec.outputs.push_back(createCompositeBuffer<deUint32>(number));
8141                                         break;
8142                                 }
8143                                 case NUMBERTYPE_FLOAT32:
8144                                 {
8145                                         float number = rnd.getFloat();
8146                                         spec.inputs.push_back(createCompositeBuffer<float>(number));
8147                                         spec.outputs.push_back(createCompositeBuffer<float>(number));
8148                                         break;
8149                                 }
8150                                 default:
8151                                         DE_ASSERT(false);
8152                         }
8153
8154                         spec.numWorkGroups = IVec3(1, 1, 1);
8155                         subGroup->addChild(new SpvAsmComputeShaderCase(testCtx, test->at("name").c_str(), "OpCompositeInsert test", spec));
8156                 }
8157                 group->addChild(subGroup.release());
8158         }
8159         return group.release();
8160 }
8161
8162 struct AssemblyStructInfo
8163 {
8164         AssemblyStructInfo (const deUint32 comp, const deUint32 idx)
8165         : components    (comp)
8166         , index                 (idx)
8167         {}
8168
8169         deUint32 components;
8170         deUint32 index;
8171 };
8172
8173 const string specializeInBoundsShaderTemplate (const NumberType type, const AssemblyStructInfo& structInfo, const map<string, string>& params)
8174 {
8175         // Create the full index string
8176         string                          fullIndex       = numberToString(structInfo.index) + " " + params.at("indexes");
8177         // Convert it to list of indexes
8178         vector<string>          indexes         = de::splitString(fullIndex, ' ');
8179
8180         map<string, string>     parameters      (params);
8181         parameters["structType"]        = repeatString(" ${compositeType}", structInfo.components);
8182         parameters["structConstruct"]   = repeatString(" %instance", structInfo.components);
8183         parameters["insertIndexes"]     = fullIndex;
8184
8185         // In matrix cases the last two index is the CompositeExtract indexes
8186         const deUint32 extractIndexes = (parameters["type"] == "matrix") ? 2 : 1;
8187
8188         // Construct the extractIndex
8189         for (vector<string>::const_iterator index = indexes.end() - extractIndexes; index != indexes.end(); ++index)
8190         {
8191                 parameters["extractIndexes"] += " " + *index;
8192         }
8193
8194         // Remove the last 1 or 2 element depends on matrix case or not
8195         indexes.erase(indexes.end() - extractIndexes, indexes.end());
8196
8197         deUint32 id = 0;
8198         // Generate AccessChain index expressions (except for the last one, because we use ptr to the composite)
8199         for (vector<string>::const_iterator index = indexes.begin(); index != indexes.end(); ++index)
8200         {
8201                 string indexId = "%index_" + numberToString(id++);
8202                 parameters["accessChainConstDeclaration"] += indexId + "   = OpConstant %u32 " + *index + "\n";
8203                 parameters["accessChainIndexes"] += " " + indexId;
8204         }
8205
8206         parameters["compositeDecorator"] = (parameters["type"] == "array") ? "OpDecorate %composite ArrayStride 4\n" : "";
8207
8208         const string customType = getAssemblyTypeName(type);
8209         map<string, string> substCustomType;
8210         substCustomType["customType"] = customType;
8211         parameters["compositeDecl"] = StringTemplate(parameters.at("compositeDecl")).specialize(substCustomType);
8212         parameters["compositeType"] = StringTemplate(parameters.at("compositeType")).specialize(substCustomType);
8213         parameters["compositeConstruct"] = StringTemplate(parameters.at("compositeConstruct")).specialize(substCustomType);
8214         parameters["filler"] = StringTemplate(parameters.at("filler")).specialize(substCustomType);
8215         parameters["customType"] = customType;
8216
8217         const string compositeType = parameters.at("compositeType");
8218         map<string, string> substCompositeType;
8219         substCompositeType["compositeType"] = compositeType;
8220         parameters["structType"] = StringTemplate(parameters.at("structType")).specialize(substCompositeType);
8221         if (compositeType != "%u32vec3")
8222         {
8223                 parameters["u32vec3Decl"] = "%u32vec3   = OpTypeVector %u32 3\n";
8224         }
8225
8226         return StringTemplate(
8227                 "OpCapability Shader\n"
8228                 "OpCapability Matrix\n"
8229                 "OpMemoryModel Logical GLSL450\n"
8230                 "OpEntryPoint GLCompute %main \"main\" %id\n"
8231                 "OpExecutionMode %main LocalSize 1 1 1\n"
8232
8233                 "OpSource GLSL 430\n"
8234                 "OpName %main           \"main\"\n"
8235                 "OpName %id             \"gl_GlobalInvocationID\"\n"
8236                 // Decorators
8237                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
8238                 "OpDecorate %buf BufferBlock\n"
8239                 "OpDecorate %indata DescriptorSet 0\n"
8240                 "OpDecorate %indata Binding 0\n"
8241                 "OpDecorate %outdata DescriptorSet 0\n"
8242                 "OpDecorate %outdata Binding 1\n"
8243                 "OpDecorate %customarr ArrayStride 4\n"
8244                 "${compositeDecorator}"
8245                 "OpMemberDecorate %buf 0 Offset 0\n"
8246                 // General types
8247                 "%void      = OpTypeVoid\n"
8248                 "%voidf     = OpTypeFunction %void\n"
8249                 "%i32       = OpTypeInt 32 1\n"
8250                 "%u32       = OpTypeInt 32 0\n"
8251                 "%f32       = OpTypeFloat 32\n"
8252                 // Custom types
8253                 "${compositeDecl}"
8254                 // %u32vec3 if not already declared in ${compositeDecl}
8255                 "${u32vec3Decl:opt}"
8256                 "%uvec3ptr  = OpTypePointer Input %u32vec3\n"
8257                 // Inherited from composite
8258                 "%composite_p = OpTypePointer Function ${compositeType}\n"
8259                 "%struct_t  = OpTypeStruct${structType}\n"
8260                 "%struct_p  = OpTypePointer Function %struct_t\n"
8261                 // Constants
8262                 "${filler}"
8263                 "${accessChainConstDeclaration}"
8264                 // Inherited from custom
8265                 "%customptr = OpTypePointer Uniform ${customType}\n"
8266                 "%customarr = OpTypeRuntimeArray ${customType}\n"
8267                 "%buf       = OpTypeStruct %customarr\n"
8268                 "%bufptr    = OpTypePointer Uniform %buf\n"
8269                 "%indata    = OpVariable %bufptr Uniform\n"
8270                 "%outdata   = OpVariable %bufptr Uniform\n"
8271
8272                 "%id        = OpVariable %uvec3ptr Input\n"
8273                 "%zero      = OpConstant %u32 0\n"
8274                 "%main      = OpFunction %void None %voidf\n"
8275                 "%label     = OpLabel\n"
8276                 "%struct_v  = OpVariable %struct_p Function\n"
8277                 "%idval     = OpLoad %u32vec3 %id\n"
8278                 "%x         = OpCompositeExtract %u32 %idval 0\n"
8279                 // Create the input/output type
8280                 "%inloc     = OpInBoundsAccessChain %customptr %indata %zero %x\n"
8281                 "%outloc    = OpInBoundsAccessChain %customptr %outdata %zero %x\n"
8282                 // Read the input value
8283                 "%inval     = OpLoad ${customType} %inloc\n"
8284                 // Create the composite and fill it
8285                 "${compositeConstruct}"
8286                 // Create the struct and fill it with the composite
8287                 "%struct    = OpCompositeConstruct %struct_t${structConstruct}\n"
8288                 // Insert the value
8289                 "%comp_obj  = OpCompositeInsert %struct_t %inval %struct ${insertIndexes}\n"
8290                 // Store the object
8291                 "             OpStore %struct_v %comp_obj\n"
8292                 // Get deepest possible composite pointer
8293                 "%inner_ptr = OpInBoundsAccessChain %composite_p %struct_v${accessChainIndexes}\n"
8294                 "%read_obj  = OpLoad ${compositeType} %inner_ptr\n"
8295                 // Read back the stored value
8296                 "%read_val  = OpCompositeExtract ${customType} %read_obj${extractIndexes}\n"
8297                 "             OpStore %outloc %read_val\n"
8298                 "             OpReturn\n"
8299                 "             OpFunctionEnd\n"
8300         ).specialize(parameters);
8301 }
8302
8303 tcu::TestCaseGroup* createOpInBoundsAccessChainGroup (tcu::TestContext& testCtx)
8304 {
8305         de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "opinboundsaccesschain", "Test the OpInBoundsAccessChain instruction"));
8306         de::Random                                              rnd                             (deStringHash(group->getName()));
8307
8308         for (int type = NUMBERTYPE_INT32; type != NUMBERTYPE_END32; ++type)
8309         {
8310                 NumberType                                              numberType      = NumberType(type);
8311                 const string                                    typeName        = getNumberTypeName(numberType);
8312                 const string                                    description     = "Test the OpInBoundsAccessChain instruction with " + typeName + "s";
8313                 de::MovePtr<tcu::TestCaseGroup> subGroup        (new tcu::TestCaseGroup(testCtx, typeName.c_str(), description.c_str()));
8314
8315                 vector<map<string, string> >    testCases;
8316                 createCompositeCases(testCases, rnd, numberType);
8317
8318                 for (vector<map<string, string> >::const_iterator test = testCases.begin(); test != testCases.end(); ++test)
8319                 {
8320                         ComputeShaderSpec       spec;
8321
8322                         // Number of components inside of a struct
8323                         deUint32 structComponents = rnd.getInt(2, 8);
8324                         // Component index value
8325                         deUint32 structIndex = rnd.getInt(0, structComponents - 1);
8326                         AssemblyStructInfo structInfo(structComponents, structIndex);
8327
8328                         spec.assembly = specializeInBoundsShaderTemplate(numberType, structInfo, *test);
8329
8330                         switch (numberType)
8331                         {
8332                                 case NUMBERTYPE_INT32:
8333                                 {
8334                                         deInt32 number = getInt(rnd);
8335                                         spec.inputs.push_back(createCompositeBuffer<deInt32>(number));
8336                                         spec.outputs.push_back(createCompositeBuffer<deInt32>(number));
8337                                         break;
8338                                 }
8339                                 case NUMBERTYPE_UINT32:
8340                                 {
8341                                         deUint32 number = rnd.getUint32();
8342                                         spec.inputs.push_back(createCompositeBuffer<deUint32>(number));
8343                                         spec.outputs.push_back(createCompositeBuffer<deUint32>(number));
8344                                         break;
8345                                 }
8346                                 case NUMBERTYPE_FLOAT32:
8347                                 {
8348                                         float number = rnd.getFloat();
8349                                         spec.inputs.push_back(createCompositeBuffer<float>(number));
8350                                         spec.outputs.push_back(createCompositeBuffer<float>(number));
8351                                         break;
8352                                 }
8353                                 default:
8354                                         DE_ASSERT(false);
8355                         }
8356                         spec.numWorkGroups = IVec3(1, 1, 1);
8357                         subGroup->addChild(new SpvAsmComputeShaderCase(testCtx, test->at("name").c_str(), "OpInBoundsAccessChain test", spec));
8358                 }
8359                 group->addChild(subGroup.release());
8360         }
8361         return group.release();
8362 }
8363
8364 // If the params missing, uninitialized case
8365 const string specializeDefaultOutputShaderTemplate (const NumberType type, const map<string, string>& params = map<string, string>())
8366 {
8367         map<string, string> parameters(params);
8368
8369         parameters["customType"]        = getAssemblyTypeName(type);
8370
8371         // Declare the const value, and use it in the initializer
8372         if (params.find("constValue") != params.end())
8373         {
8374                 parameters["variableInitializer"]       = " %const";
8375         }
8376         // Uninitialized case
8377         else
8378         {
8379                 parameters["commentDecl"]       = ";";
8380         }
8381
8382         return StringTemplate(
8383                 "OpCapability Shader\n"
8384                 "OpMemoryModel Logical GLSL450\n"
8385                 "OpEntryPoint GLCompute %main \"main\" %id\n"
8386                 "OpExecutionMode %main LocalSize 1 1 1\n"
8387                 "OpSource GLSL 430\n"
8388                 "OpName %main           \"main\"\n"
8389                 "OpName %id             \"gl_GlobalInvocationID\"\n"
8390                 // Decorators
8391                 "OpDecorate %id BuiltIn GlobalInvocationId\n"
8392                 "OpDecorate %indata DescriptorSet 0\n"
8393                 "OpDecorate %indata Binding 0\n"
8394                 "OpDecorate %outdata DescriptorSet 0\n"
8395                 "OpDecorate %outdata Binding 1\n"
8396                 "OpDecorate %in_arr ArrayStride 4\n"
8397                 "OpDecorate %in_buf BufferBlock\n"
8398                 "OpMemberDecorate %in_buf 0 Offset 0\n"
8399                 // Base types
8400                 "%void       = OpTypeVoid\n"
8401                 "%voidf      = OpTypeFunction %void\n"
8402                 "%u32        = OpTypeInt 32 0\n"
8403                 "%i32        = OpTypeInt 32 1\n"
8404                 "%f32        = OpTypeFloat 32\n"
8405                 "%uvec3      = OpTypeVector %u32 3\n"
8406                 "%uvec3ptr   = OpTypePointer Input %uvec3\n"
8407                 "${commentDecl:opt}%const      = OpConstant ${customType} ${constValue:opt}\n"
8408                 // Derived types
8409                 "%in_ptr     = OpTypePointer Uniform ${customType}\n"
8410                 "%in_arr     = OpTypeRuntimeArray ${customType}\n"
8411                 "%in_buf     = OpTypeStruct %in_arr\n"
8412                 "%in_bufptr  = OpTypePointer Uniform %in_buf\n"
8413                 "%indata     = OpVariable %in_bufptr Uniform\n"
8414                 "%outdata    = OpVariable %in_bufptr Uniform\n"
8415                 "%id         = OpVariable %uvec3ptr Input\n"
8416                 "%var_ptr    = OpTypePointer Function ${customType}\n"
8417                 // Constants
8418                 "%zero       = OpConstant %i32 0\n"
8419                 // Main function
8420                 "%main       = OpFunction %void None %voidf\n"
8421                 "%label      = OpLabel\n"
8422                 "%out_var    = OpVariable %var_ptr Function${variableInitializer:opt}\n"
8423                 "%idval      = OpLoad %uvec3 %id\n"
8424                 "%x          = OpCompositeExtract %u32 %idval 0\n"
8425                 "%inloc      = OpAccessChain %in_ptr %indata %zero %x\n"
8426                 "%outloc     = OpAccessChain %in_ptr %outdata %zero %x\n"
8427
8428                 "%outval     = OpLoad ${customType} %out_var\n"
8429                 "              OpStore %outloc %outval\n"
8430                 "              OpReturn\n"
8431                 "              OpFunctionEnd\n"
8432         ).specialize(parameters);
8433 }
8434
8435 bool compareFloats (const std::vector<BufferSp>&, const vector<AllocationSp>& outputAllocs, const std::vector<BufferSp>& expectedOutputs, TestLog& log)
8436 {
8437         DE_ASSERT(outputAllocs.size() != 0);
8438         DE_ASSERT(outputAllocs.size() == expectedOutputs.size());
8439
8440         // Use custom epsilon because of the float->string conversion
8441         const float     epsilon = 0.00001f;
8442
8443         for (size_t outputNdx = 0; outputNdx < outputAllocs.size(); ++outputNdx)
8444         {
8445                 vector<deUint8> expectedBytes;
8446                 float                   expected;
8447                 float                   actual;
8448
8449                 expectedOutputs[outputNdx]->getBytes(expectedBytes);
8450                 memcpy(&expected, &expectedBytes.front(), expectedBytes.size());
8451                 memcpy(&actual, outputAllocs[outputNdx]->getHostPtr(), expectedBytes.size());
8452
8453                 // Test with epsilon
8454                 if (fabs(expected - actual) > epsilon)
8455                 {
8456                         log << TestLog::Message << "Error: The actual and expected values not matching."
8457                                 << " Expected: " << expected << " Actual: " << actual << " Epsilon: " << epsilon << TestLog::EndMessage;
8458                         return false;
8459                 }
8460         }
8461         return true;
8462 }
8463
8464 // Checks if the driver crash with uninitialized cases
8465 bool passthruVerify (const std::vector<BufferSp>&, const vector<AllocationSp>& outputAllocs, const std::vector<BufferSp>& expectedOutputs, TestLog&)
8466 {
8467         DE_ASSERT(outputAllocs.size() != 0);
8468         DE_ASSERT(outputAllocs.size() == expectedOutputs.size());
8469
8470         // Copy and discard the result.
8471         for (size_t outputNdx = 0; outputNdx < outputAllocs.size(); ++outputNdx)
8472         {
8473                 vector<deUint8> expectedBytes;
8474                 expectedOutputs[outputNdx]->getBytes(expectedBytes);
8475
8476                 const size_t    width                   = expectedBytes.size();
8477                 vector<char>    data                    (width);
8478
8479                 memcpy(&data[0], outputAllocs[outputNdx]->getHostPtr(), width);
8480         }
8481         return true;
8482 }
8483
8484 tcu::TestCaseGroup* createShaderDefaultOutputGroup (tcu::TestContext& testCtx)
8485 {
8486         de::MovePtr<tcu::TestCaseGroup> group   (new tcu::TestCaseGroup(testCtx, "shader_default_output", "Test shader default output."));
8487         de::Random                                              rnd             (deStringHash(group->getName()));
8488
8489         for (int type = NUMBERTYPE_INT32; type != NUMBERTYPE_END32; ++type)
8490         {
8491                 NumberType                                              numberType      = NumberType(type);
8492                 const string                                    typeName        = getNumberTypeName(numberType);
8493                 const string                                    description     = "Test the OpVariable initializer with " + typeName + ".";
8494                 de::MovePtr<tcu::TestCaseGroup> subGroup        (new tcu::TestCaseGroup(testCtx, typeName.c_str(), description.c_str()));
8495
8496                 // 2 similar subcases (initialized and uninitialized)
8497                 for (int subCase = 0; subCase < 2; ++subCase)
8498                 {
8499                         ComputeShaderSpec spec;
8500                         spec.numWorkGroups = IVec3(1, 1, 1);
8501
8502                         map<string, string>                             params;
8503
8504                         switch (numberType)
8505                         {
8506                                 case NUMBERTYPE_INT32:
8507                                 {
8508                                         deInt32 number = getInt(rnd);
8509                                         spec.inputs.push_back(createCompositeBuffer<deInt32>(number));
8510                                         spec.outputs.push_back(createCompositeBuffer<deInt32>(number));
8511                                         params["constValue"] = numberToString(number);
8512                                         break;
8513                                 }
8514                                 case NUMBERTYPE_UINT32:
8515                                 {
8516                                         deUint32 number = rnd.getUint32();
8517                                         spec.inputs.push_back(createCompositeBuffer<deUint32>(number));
8518                                         spec.outputs.push_back(createCompositeBuffer<deUint32>(number));
8519                                         params["constValue"] = numberToString(number);
8520                                         break;
8521                                 }
8522                                 case NUMBERTYPE_FLOAT32:
8523                                 {
8524                                         float number = rnd.getFloat();
8525                                         spec.inputs.push_back(createCompositeBuffer<float>(number));
8526                                         spec.outputs.push_back(createCompositeBuffer<float>(number));
8527                                         spec.verifyIO = &compareFloats;
8528                                         params["constValue"] = numberToString(number);
8529                                         break;
8530                                 }
8531                                 default:
8532                                         DE_ASSERT(false);
8533                         }
8534
8535                         // Initialized subcase
8536                         if (!subCase)
8537                         {
8538                                 spec.assembly = specializeDefaultOutputShaderTemplate(numberType, params);
8539                                 subGroup->addChild(new SpvAsmComputeShaderCase(testCtx, "initialized", "OpVariable initializer tests.", spec));
8540                         }
8541                         // Uninitialized subcase
8542                         else
8543                         {
8544                                 spec.assembly = specializeDefaultOutputShaderTemplate(numberType);
8545                                 spec.verifyIO = &passthruVerify;
8546                                 subGroup->addChild(new SpvAsmComputeShaderCase(testCtx, "uninitialized", "OpVariable initializer tests.", spec));
8547                         }
8548                 }
8549                 group->addChild(subGroup.release());
8550         }
8551         return group.release();
8552 }
8553
8554 tcu::TestCaseGroup* createOpNopTests (tcu::TestContext& testCtx)
8555 {
8556         de::MovePtr<tcu::TestCaseGroup> testGroup (new tcu::TestCaseGroup(testCtx, "opnop", "Test OpNop"));
8557         RGBA                                                    defaultColors[4];
8558         map<string, string>                             opNopFragments;
8559
8560         getDefaultColors(defaultColors);
8561
8562         opNopFragments["testfun"]               =
8563                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
8564                 "%param1 = OpFunctionParameter %v4f32\n"
8565                 "%label_testfun = OpLabel\n"
8566                 "OpNop\n"
8567                 "OpNop\n"
8568                 "OpNop\n"
8569                 "OpNop\n"
8570                 "OpNop\n"
8571                 "OpNop\n"
8572                 "OpNop\n"
8573                 "OpNop\n"
8574                 "%a = OpVectorExtractDynamic %f32 %param1 %c_i32_0\n"
8575                 "%b = OpFAdd %f32 %a %a\n"
8576                 "OpNop\n"
8577                 "%c = OpFSub %f32 %b %a\n"
8578                 "%ret = OpVectorInsertDynamic %v4f32 %param1 %c %c_i32_0\n"
8579                 "OpNop\n"
8580                 "OpNop\n"
8581                 "OpReturnValue %ret\n"
8582                 "OpFunctionEnd\n";
8583
8584         createTestsForAllStages("opnop", defaultColors, defaultColors, opNopFragments, testGroup.get());
8585
8586         return testGroup.release();
8587 }
8588
8589 tcu::TestCaseGroup* createInstructionTests (tcu::TestContext& testCtx)
8590 {
8591         de::MovePtr<tcu::TestCaseGroup> instructionTests        (new tcu::TestCaseGroup(testCtx, "instruction", "Instructions with special opcodes/operands"));
8592         de::MovePtr<tcu::TestCaseGroup> computeTests            (new tcu::TestCaseGroup(testCtx, "compute", "Compute Instructions with special opcodes/operands"));
8593         de::MovePtr<tcu::TestCaseGroup> graphicsTests           (new tcu::TestCaseGroup(testCtx, "graphics", "Graphics Instructions with special opcodes/operands"));
8594
8595         computeTests->addChild(createOpNopGroup(testCtx));
8596         computeTests->addChild(createOpFUnordGroup(testCtx));
8597         computeTests->addChild(createOpAtomicGroup(testCtx, false));
8598         computeTests->addChild(createOpAtomicGroup(testCtx, true)); // Using new StorageBuffer decoration
8599         computeTests->addChild(createOpLineGroup(testCtx));
8600         computeTests->addChild(createOpNoLineGroup(testCtx));
8601         computeTests->addChild(createOpConstantNullGroup(testCtx));
8602         computeTests->addChild(createOpConstantCompositeGroup(testCtx));
8603         computeTests->addChild(createOpConstantUsageGroup(testCtx));
8604         computeTests->addChild(createSpecConstantGroup(testCtx));
8605         computeTests->addChild(createOpSourceGroup(testCtx));
8606         computeTests->addChild(createOpSourceExtensionGroup(testCtx));
8607         computeTests->addChild(createDecorationGroupGroup(testCtx));
8608         computeTests->addChild(createOpPhiGroup(testCtx));
8609         computeTests->addChild(createLoopControlGroup(testCtx));
8610         computeTests->addChild(createFunctionControlGroup(testCtx));
8611         computeTests->addChild(createSelectionControlGroup(testCtx));
8612         computeTests->addChild(createBlockOrderGroup(testCtx));
8613         computeTests->addChild(createMultipleShaderGroup(testCtx));
8614         computeTests->addChild(createMemoryAccessGroup(testCtx));
8615         computeTests->addChild(createOpCopyMemoryGroup(testCtx));
8616         computeTests->addChild(createOpCopyObjectGroup(testCtx));
8617         computeTests->addChild(createNoContractionGroup(testCtx));
8618         computeTests->addChild(createOpUndefGroup(testCtx));
8619         computeTests->addChild(createOpUnreachableGroup(testCtx));
8620         computeTests ->addChild(createOpQuantizeToF16Group(testCtx));
8621         computeTests ->addChild(createOpFRemGroup(testCtx));
8622         computeTests->addChild(createOpSRemComputeGroup(testCtx, QP_TEST_RESULT_PASS));
8623         computeTests->addChild(createOpSRemComputeGroup64(testCtx, QP_TEST_RESULT_PASS));
8624         computeTests->addChild(createOpSModComputeGroup(testCtx, QP_TEST_RESULT_PASS));
8625         computeTests->addChild(createOpSModComputeGroup64(testCtx, QP_TEST_RESULT_PASS));
8626         computeTests->addChild(createSConvertTests(testCtx));
8627         computeTests->addChild(createUConvertTests(testCtx));
8628         computeTests->addChild(createOpCompositeInsertGroup(testCtx));
8629         computeTests->addChild(createOpInBoundsAccessChainGroup(testCtx));
8630         computeTests->addChild(createShaderDefaultOutputGroup(testCtx));
8631         computeTests->addChild(createOpNMinGroup(testCtx));
8632         computeTests->addChild(createOpNMaxGroup(testCtx));
8633         computeTests->addChild(createOpNClampGroup(testCtx));
8634         {
8635                 de::MovePtr<tcu::TestCaseGroup> computeAndroidTests     (new tcu::TestCaseGroup(testCtx, "android", "Android CTS Tests"));
8636
8637                 computeAndroidTests->addChild(createOpSRemComputeGroup(testCtx, QP_TEST_RESULT_QUALITY_WARNING));
8638                 computeAndroidTests->addChild(createOpSModComputeGroup(testCtx, QP_TEST_RESULT_QUALITY_WARNING));
8639
8640                 computeTests->addChild(computeAndroidTests.release());
8641         }
8642
8643         computeTests->addChild(create16BitStorageComputeGroup(testCtx));
8644         computeTests->addChild(createUboMatrixPaddingComputeGroup(testCtx));
8645         computeTests->addChild(createConditionalBranchComputeGroup(testCtx));
8646         computeTests->addChild(createIndexingComputeGroup(testCtx));
8647         computeTests->addChild(createVariablePointersComputeGroup(testCtx));
8648         computeTests->addChild(createImageSamplerComputeGroup(testCtx));
8649         graphicsTests->addChild(createOpNopTests(testCtx));
8650         graphicsTests->addChild(createOpSourceTests(testCtx));
8651         graphicsTests->addChild(createOpSourceContinuedTests(testCtx));
8652         graphicsTests->addChild(createOpLineTests(testCtx));
8653         graphicsTests->addChild(createOpNoLineTests(testCtx));
8654         graphicsTests->addChild(createOpConstantNullTests(testCtx));
8655         graphicsTests->addChild(createOpConstantCompositeTests(testCtx));
8656         graphicsTests->addChild(createMemoryAccessTests(testCtx));
8657         graphicsTests->addChild(createOpUndefTests(testCtx));
8658         graphicsTests->addChild(createSelectionBlockOrderTests(testCtx));
8659         graphicsTests->addChild(createModuleTests(testCtx));
8660         graphicsTests->addChild(createSwitchBlockOrderTests(testCtx));
8661         graphicsTests->addChild(createOpPhiTests(testCtx));
8662         graphicsTests->addChild(createNoContractionTests(testCtx));
8663         graphicsTests->addChild(createOpQuantizeTests(testCtx));
8664         graphicsTests->addChild(createLoopTests(testCtx));
8665         graphicsTests->addChild(createSpecConstantTests(testCtx));
8666         graphicsTests->addChild(createSpecConstantOpQuantizeToF16Group(testCtx));
8667         graphicsTests->addChild(createBarrierTests(testCtx));
8668         graphicsTests->addChild(createDecorationGroupTests(testCtx));
8669         graphicsTests->addChild(createFRemTests(testCtx));
8670         graphicsTests->addChild(createOpSRemGraphicsTests(testCtx, QP_TEST_RESULT_PASS));
8671         graphicsTests->addChild(createOpSModGraphicsTests(testCtx, QP_TEST_RESULT_PASS));
8672
8673         {
8674                 de::MovePtr<tcu::TestCaseGroup> graphicsAndroidTests    (new tcu::TestCaseGroup(testCtx, "android", "Android CTS Tests"));
8675
8676                 graphicsAndroidTests->addChild(createOpSRemGraphicsTests(testCtx, QP_TEST_RESULT_QUALITY_WARNING));
8677                 graphicsAndroidTests->addChild(createOpSModGraphicsTests(testCtx, QP_TEST_RESULT_QUALITY_WARNING));
8678
8679                 graphicsTests->addChild(graphicsAndroidTests.release());
8680         }
8681
8682         graphicsTests->addChild(create16BitStorageGraphicsGroup(testCtx));
8683         graphicsTests->addChild(createUboMatrixPaddingGraphicsGroup(testCtx));
8684         graphicsTests->addChild(createConditionalBranchGraphicsGroup(testCtx));
8685         graphicsTests->addChild(createIndexingGraphicsGroup(testCtx));
8686         graphicsTests->addChild(createVariablePointersGraphicsGroup(testCtx));
8687         graphicsTests->addChild(createImageSamplerGraphicsGroup(testCtx));
8688
8689         instructionTests->addChild(computeTests.release());
8690         instructionTests->addChild(graphicsTests.release());
8691
8692         return instructionTests.release();
8693 }
8694
8695 } // SpirVAssembly
8696 } // vkt