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