Add tests for SPV_KHR_variable_pointers extension
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / spirv_assembly / vktSpvAsmGraphicsShaderTestUtil.cpp
1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2017 Google Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Graphics pipeline for SPIR-V assembly tests
22  *//*--------------------------------------------------------------------*/
23
24 #include "vktSpvAsmGraphicsShaderTestUtil.hpp"
25
26 #include "tcuFloat.hpp"
27 #include "tcuStringTemplate.hpp"
28
29 #include "vkDefs.hpp"
30 #include "vkMemUtil.hpp"
31 #include "vkPlatform.hpp"
32 #include "vkQueryUtil.hpp"
33 #include "vkRefUtil.hpp"
34 #include "vkTypeUtil.hpp"
35
36 #include "deRandom.hpp"
37
38 namespace vkt
39 {
40 namespace SpirVAssembly
41 {
42
43 using namespace vk;
44 using std::map;
45 using std::string;
46 using std::vector;
47 using tcu::Float16;
48 using tcu::Float32;
49 using tcu::IVec3;
50 using tcu::IVec4;
51 using tcu::RGBA;
52 using tcu::TestLog;
53 using tcu::TestStatus;
54 using tcu::Vec4;
55 using de::UniquePtr;
56 using tcu::StringTemplate;
57 using tcu::Vec4;
58
59 deUint32 IFDataType::getElementNumBytes (void) const
60 {
61         if (elementType < NUMBERTYPE_END32)
62                 return 4;
63
64         return 2;
65 }
66
67 VkFormat IFDataType::getVkFormat (void) const
68 {
69         if (numElements == 1)
70         {
71                 switch (elementType)
72                 {
73                         case NUMBERTYPE_FLOAT32:        return VK_FORMAT_R32_SFLOAT;
74                         case NUMBERTYPE_INT32:          return VK_FORMAT_R32_SINT;
75                         case NUMBERTYPE_UINT32:         return VK_FORMAT_R32_UINT;
76                         case NUMBERTYPE_FLOAT16:        return VK_FORMAT_R16_SFLOAT;
77                         case NUMBERTYPE_INT16:          return VK_FORMAT_R16_SINT;
78                         case NUMBERTYPE_UINT16:         return VK_FORMAT_R16_UINT;
79                         default:                                        break;
80                 }
81         }
82         else if (numElements == 2)
83         {
84                 switch (elementType)
85                 {
86                         case NUMBERTYPE_FLOAT32:        return VK_FORMAT_R32G32_SFLOAT;
87                         case NUMBERTYPE_INT32:          return VK_FORMAT_R32G32_SINT;
88                         case NUMBERTYPE_UINT32:         return VK_FORMAT_R32G32_UINT;
89                         case NUMBERTYPE_FLOAT16:        return VK_FORMAT_R16G16_SFLOAT;
90                         case NUMBERTYPE_INT16:          return VK_FORMAT_R16G16_SINT;
91                         case NUMBERTYPE_UINT16:         return VK_FORMAT_R16G16_UINT;
92                         default:                                        break;
93                 }
94         }
95         else if (numElements == 3)
96         {
97                 switch (elementType)
98                 {
99                         case NUMBERTYPE_FLOAT32:        return VK_FORMAT_R32G32B32_SFLOAT;
100                         case NUMBERTYPE_INT32:          return VK_FORMAT_R32G32B32_SINT;
101                         case NUMBERTYPE_UINT32:         return VK_FORMAT_R32G32B32_UINT;
102                         case NUMBERTYPE_FLOAT16:        return VK_FORMAT_R16G16B16_SFLOAT;
103                         case NUMBERTYPE_INT16:          return VK_FORMAT_R16G16B16_SINT;
104                         case NUMBERTYPE_UINT16:         return VK_FORMAT_R16G16B16_UINT;
105                         default:                                        break;
106                 }
107         }
108         else if (numElements == 4)
109         {
110                 switch (elementType)
111                 {
112                         case NUMBERTYPE_FLOAT32:        return VK_FORMAT_R32G32B32A32_SFLOAT;
113                         case NUMBERTYPE_INT32:          return VK_FORMAT_R32G32B32A32_SINT;
114                         case NUMBERTYPE_UINT32:         return VK_FORMAT_R32G32B32A32_UINT;
115                         case NUMBERTYPE_FLOAT16:        return VK_FORMAT_R16G16B16A16_SFLOAT;
116                         case NUMBERTYPE_INT16:          return VK_FORMAT_R16G16B16A16_SINT;
117                         case NUMBERTYPE_UINT16:         return VK_FORMAT_R16G16B16A16_UINT;
118                         default:                                        break;
119                 }
120         }
121
122         DE_ASSERT(false);
123         return VK_FORMAT_UNDEFINED;
124 }
125
126 tcu::TextureFormat IFDataType::getTextureFormat (void) const
127 {
128         tcu::TextureFormat::ChannelType         ct      = tcu::TextureFormat::CHANNELTYPE_LAST;
129         tcu::TextureFormat::ChannelOrder        co      = tcu::TextureFormat::CHANNELORDER_LAST;
130
131         switch (elementType)
132         {
133                 case NUMBERTYPE_FLOAT32:        ct = tcu::TextureFormat::FLOAT;                         break;
134                 case NUMBERTYPE_INT32:          ct = tcu::TextureFormat::SIGNED_INT32;          break;
135                 case NUMBERTYPE_UINT32:         ct = tcu::TextureFormat::UNSIGNED_INT32;        break;
136                 case NUMBERTYPE_FLOAT16:        ct = tcu::TextureFormat::HALF_FLOAT;            break;
137                 case NUMBERTYPE_INT16:          ct = tcu::TextureFormat::SIGNED_INT16;          break;
138                 case NUMBERTYPE_UINT16:         ct = tcu::TextureFormat::UNSIGNED_INT16;        break;
139                 default:                                        DE_ASSERT(false);
140         }
141
142         switch (numElements)
143         {
144                 case 1:                         co = tcu::TextureFormat::R;                                     break;
145                 case 2:                         co = tcu::TextureFormat::RG;                            break;
146                 case 3:                         co = tcu::TextureFormat::RGB;                           break;
147                 case 4:                         co = tcu::TextureFormat::RGBA;                          break;
148                 default:                        DE_ASSERT(false);
149         }
150
151         return tcu::TextureFormat(co, ct);
152 }
153
154 string IFDataType::str (void) const
155 {
156         string  ret;
157
158         switch (elementType)
159         {
160                 case NUMBERTYPE_FLOAT32:        ret = "f32"; break;
161                 case NUMBERTYPE_INT32:          ret = "i32"; break;
162                 case NUMBERTYPE_UINT32:         ret = "u32"; break;
163                 case NUMBERTYPE_FLOAT16:        ret = "f16"; break;
164                 case NUMBERTYPE_INT16:          ret = "i16"; break;
165                 case NUMBERTYPE_UINT16:         ret = "u16"; break;
166                 default:                                        DE_ASSERT(false);
167         }
168
169         if (numElements == 1)
170                 return ret;
171
172         return string("v") + numberToString(numElements) + ret;
173 }
174
175 VkBufferUsageFlagBits getMatchingBufferUsageFlagBit(VkDescriptorType dType)
176 {
177         switch (dType)
178         {
179                 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: return VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
180                 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: return VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
181                 default:                                                                DE_ASSERT(0 && "not implemented");
182         }
183         return (VkBufferUsageFlagBits)0;
184 }
185
186 static void requireFormatUsageSupport(const InstanceInterface& vki, VkPhysicalDevice physicalDevice, VkFormat format, VkImageTiling imageTiling, VkImageUsageFlags requiredUsageFlags)
187 {
188         VkFormatProperties              properties;
189         VkFormatFeatureFlags    tilingFeatures  = 0;
190
191         vki.getPhysicalDeviceFormatProperties(physicalDevice, format, &properties);
192
193         switch (imageTiling)
194         {
195                 case VK_IMAGE_TILING_LINEAR:
196                         tilingFeatures = properties.linearTilingFeatures;
197                         break;
198
199                 case VK_IMAGE_TILING_OPTIMAL:
200                         tilingFeatures = properties.optimalTilingFeatures;
201                         break;
202
203                 default:
204                         DE_ASSERT(false);
205                         break;
206         }
207
208         if ((requiredUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) != 0)
209         {
210                 if ((tilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)
211                         TCU_THROW(NotSupportedError, "Image format cannot be used as color attachment");
212                 requiredUsageFlags ^= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
213         }
214
215
216         if ((requiredUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) != 0)
217         {
218                 if ((tilingFeatures & VK_FORMAT_FEATURE_TRANSFER_SRC_BIT_KHR) == 0)
219                         TCU_THROW(NotSupportedError, "Image format cannot be used as transfer source");
220                 requiredUsageFlags ^= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
221         }
222
223
224         DE_ASSERT(!requiredUsageFlags && "checking other image usage bits not supported yet");
225 }
226
227 InstanceContext::InstanceContext (const RGBA                                            (&inputs)[4],
228                                                                   const RGBA                                            (&outputs)[4],
229                                                                   const map<string, string>&            testCodeFragments_,
230                                                                   const StageToSpecConstantMap&         specConstants_,
231                                                                   const PushConstants&                          pushConsants_,
232                                                                   const GraphicsResources&                      resources_,
233                                                                   const GraphicsInterfaces&                     interfaces_,
234                                                                   const vector<string>&                         extensions_,
235                                                                   const vector<string>&                         features_,
236                                                                   VulkanFeatures                                        vulkanFeatures_,
237                                                                   VkShaderStageFlags                            customizedStages_)
238         : testCodeFragments                             (testCodeFragments_)
239         , specConstants                                 (specConstants_)
240         , hasTessellation                               (false)
241         , requiredStages                                (static_cast<VkShaderStageFlagBits>(0))
242         , requiredDeviceExtensions              (extensions_)
243         , requiredDeviceFeatures                (features_)
244         , requestedFeatures                             (vulkanFeatures_)
245         , pushConstants                                 (pushConsants_)
246         , customizedStages                              (customizedStages_)
247         , resources                                             (resources_)
248         , interfaces                                    (interfaces_)
249         , failResult                                    (QP_TEST_RESULT_FAIL)
250         , failMessageTemplate                   ("${reason}")
251 {
252         inputColors[0]          = inputs[0];
253         inputColors[1]          = inputs[1];
254         inputColors[2]          = inputs[2];
255         inputColors[3]          = inputs[3];
256
257         outputColors[0]         = outputs[0];
258         outputColors[1]         = outputs[1];
259         outputColors[2]         = outputs[2];
260         outputColors[3]         = outputs[3];
261 }
262
263 InstanceContext::InstanceContext (const InstanceContext& other)
264         : moduleMap                                             (other.moduleMap)
265         , testCodeFragments                             (other.testCodeFragments)
266         , specConstants                                 (other.specConstants)
267         , hasTessellation                               (other.hasTessellation)
268         , requiredStages                                (other.requiredStages)
269         , requiredDeviceExtensions              (other.requiredDeviceExtensions)
270         , requiredDeviceFeatures                (other.requiredDeviceFeatures)
271         , requestedFeatures                             (other.requestedFeatures)
272         , pushConstants                                 (other.pushConstants)
273         , customizedStages                              (other.customizedStages)
274         , resources                                             (other.resources)
275         , interfaces                                    (other.interfaces)
276         , failResult                                    (other.failResult)
277         , failMessageTemplate                   (other.failMessageTemplate)
278 {
279         inputColors[0]          = other.inputColors[0];
280         inputColors[1]          = other.inputColors[1];
281         inputColors[2]          = other.inputColors[2];
282         inputColors[3]          = other.inputColors[3];
283
284         outputColors[0]         = other.outputColors[0];
285         outputColors[1]         = other.outputColors[1];
286         outputColors[2]         = other.outputColors[2];
287         outputColors[3]         = other.outputColors[3];
288 }
289
290 string InstanceContext::getSpecializedFailMessage (const string& failureReason)
291 {
292         map<string, string>             parameters;
293         parameters["reason"]    = failureReason;
294         return StringTemplate(failMessageTemplate).specialize(parameters);
295 }
296
297 ShaderElement::ShaderElement (const string&                             moduleName_,
298                                                           const string&                         entryPoint_,
299                                                           VkShaderStageFlagBits         shaderStage_)
300                 : moduleName(moduleName_)
301                 , entryName(entryPoint_)
302                 , stage(shaderStage_)
303 {
304 }
305
306 void getDefaultColors (RGBA (&colors)[4])
307 {
308         colors[0] = RGBA::white();
309         colors[1] = RGBA::red();
310         colors[2] = RGBA::green();
311         colors[3] = RGBA::blue();
312 }
313
314 void getHalfColorsFullAlpha (RGBA (&colors)[4])
315 {
316         colors[0] = RGBA(127, 127, 127, 255);
317         colors[1] = RGBA(127, 0,   0,   255);
318         colors[2] = RGBA(0,       127, 0,       255);
319         colors[3] = RGBA(0,       0,   127, 255);
320 }
321
322 void getInvertedDefaultColors (RGBA (&colors)[4])
323 {
324         colors[0] = RGBA(0,             0,              0,              255);
325         colors[1] = RGBA(0,             255,    255,    255);
326         colors[2] = RGBA(255,   0,              255,    255);
327         colors[3] = RGBA(255,   255,    0,              255);
328 }
329
330 // For the current InstanceContext, constructs the required modules and shader stage create infos.
331 void createPipelineShaderStages (const DeviceInterface&                                         vk,
332                                                                  const VkDevice                                                         vkDevice,
333                                                                  InstanceContext&                                                       instance,
334                                                                  Context&                                                                       context,
335                                                                  vector<ModuleHandleSp>&                                        modules,
336                                                                  vector<VkPipelineShaderStageCreateInfo>&       createInfos)
337 {
338         for (ModuleMap::const_iterator moduleNdx = instance.moduleMap.begin(); moduleNdx != instance.moduleMap.end(); ++moduleNdx)
339         {
340                 const ModuleHandleSp mod(new Unique<VkShaderModule>(createShaderModule(vk, vkDevice, context.getBinaryCollection().get(moduleNdx->first), 0)));
341                 modules.push_back(ModuleHandleSp(mod));
342                 for (vector<EntryToStage>::const_iterator shaderNdx = moduleNdx->second.begin(); shaderNdx != moduleNdx->second.end(); ++shaderNdx)
343                 {
344                         const EntryToStage&                                             stage                   = *shaderNdx;
345                         const VkPipelineShaderStageCreateInfo   shaderParam             =
346                         {
347                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,    //      VkStructureType                 sType;
348                                 DE_NULL,                                                                                                //      const void*                             pNext;
349                                 (VkPipelineShaderStageCreateFlags)0,
350                                 stage.second,                                                                                   //      VkShaderStageFlagBits   stage;
351                                 **modules.back(),                                                                               //      VkShaderModule                  module;
352                                 stage.first.c_str(),                                                                    //      const char*                             pName;
353                                 (const VkSpecializationInfo*)DE_NULL,
354                         };
355                         createInfos.push_back(shaderParam);
356                 }
357         }
358 }
359
360 #define SPIRV_ASSEMBLY_TYPES                                                                                                                                    \
361         "%void = OpTypeVoid\n"                                                                                                                                          \
362         "%bool = OpTypeBool\n"                                                                                                                                          \
363                                                                                                                                                                                                 \
364         "%i32 = OpTypeInt 32 1\n"                                                                                                                                       \
365         "%u32 = OpTypeInt 32 0\n"                                                                                                                                       \
366                                                                                                                                                                                                 \
367         "%f32 = OpTypeFloat 32\n"                                                                                                                                       \
368         "%v2i32 = OpTypeVector %i32 2\n"                                                                                                                        \
369         "%v2u32 = OpTypeVector %u32 2\n"                                                                                                                        \
370         "%v2f32 = OpTypeVector %f32 2\n"                                                                                                                        \
371         "%v3f32 = OpTypeVector %f32 3\n"                                                                                                                        \
372         "%v4i32 = OpTypeVector %i32 4\n"                                                                                                                        \
373         "%v4u32 = OpTypeVector %u32 4\n"                                                                                                                        \
374         "%v4f32 = OpTypeVector %f32 4\n"                                                                                                                        \
375         "%v4bool = OpTypeVector %bool 4\n"                                                                                                                      \
376                                                                                                                                                                                                 \
377         "%v4f32_function = OpTypeFunction %v4f32 %v4f32\n"                                                                                      \
378         "%bool_function = OpTypeFunction %bool\n"                                                                                                       \
379         "%fun = OpTypeFunction %void\n"                                                                                                                         \
380                                                                                                                                                                                                 \
381         "%ip_f32 = OpTypePointer Input %f32\n"                                                                                                          \
382         "%ip_i32 = OpTypePointer Input %i32\n"                                                                                                          \
383         "%ip_u32 = OpTypePointer Input %u32\n"                                                                                                          \
384         "%ip_v3f32 = OpTypePointer Input %v3f32\n"                                                                                                      \
385         "%ip_v2f32 = OpTypePointer Input %v2f32\n"                                                                                                      \
386         "%ip_v2i32 = OpTypePointer Input %v2i32\n"                                                                                                      \
387         "%ip_v2u32 = OpTypePointer Input %v2u32\n"                                                                                                      \
388         "%ip_v4f32 = OpTypePointer Input %v4f32\n"                                                                                                      \
389         "%ip_v4i32 = OpTypePointer Input %v4i32\n"                                                                                                      \
390         "%ip_v4u32 = OpTypePointer Input %v4u32\n"                                                                                                      \
391                                                                                                                                                                                                 \
392         "%op_f32 = OpTypePointer Output %f32\n"                                                                                                         \
393         "%op_i32 = OpTypePointer Output %i32\n"                                                                                                         \
394         "%op_u32 = OpTypePointer Output %u32\n"                                                                                                         \
395         "%op_v2f32 = OpTypePointer Output %v2f32\n"                                                                                                     \
396         "%op_v2i32 = OpTypePointer Output %v2i32\n"                                                                                                     \
397         "%op_v2u32 = OpTypePointer Output %v2u32\n"                                                                                                     \
398         "%op_v4f32 = OpTypePointer Output %v4f32\n"                                                                                                     \
399         "%op_v4i32 = OpTypePointer Output %v4i32\n"                                                                                                     \
400         "%op_v4u32 = OpTypePointer Output %v4u32\n"                                                                                                     \
401                                                                                                                                                                                                 \
402         "%fp_f32   = OpTypePointer Function %f32\n"                                                                                                     \
403         "%fp_i32   = OpTypePointer Function %i32\n"                                                                                                     \
404         "%fp_v4f32 = OpTypePointer Function %v4f32\n"
405
406 #define SPIRV_ASSEMBLY_CONSTANTS                                                                                                                                \
407         "%c_f32_1 = OpConstant %f32 1.0\n"                                                                                                                      \
408         "%c_f32_0 = OpConstant %f32 0.0\n"                                                                                                                      \
409         "%c_f32_0_5 = OpConstant %f32 0.5\n"                                                                                                            \
410         "%c_f32_n1  = OpConstant %f32 -1.\n"                                                                                                            \
411         "%c_f32_7 = OpConstant %f32 7.0\n"                                                                                                                      \
412         "%c_f32_8 = OpConstant %f32 8.0\n"                                                                                                                      \
413         "%c_i32_0 = OpConstant %i32 0\n"                                                                                                                        \
414         "%c_i32_1 = OpConstant %i32 1\n"                                                                                                                        \
415         "%c_i32_2 = OpConstant %i32 2\n"                                                                                                                        \
416         "%c_i32_3 = OpConstant %i32 3\n"                                                                                                                        \
417         "%c_i32_4 = OpConstant %i32 4\n"                                                                                                                        \
418         "%c_u32_0 = OpConstant %u32 0\n"                                                                                                                        \
419         "%c_u32_1 = OpConstant %u32 1\n"                                                                                                                        \
420         "%c_u32_2 = OpConstant %u32 2\n"                                                                                                                        \
421         "%c_u32_3 = OpConstant %u32 3\n"                                                                                                                        \
422         "%c_u32_32 = OpConstant %u32 32\n"                                                                                                                      \
423         "%c_u32_4 = OpConstant %u32 4\n"                                                                                                                        \
424         "%c_u32_31_bits = OpConstant %u32 0x7FFFFFFF\n"                                                                                         \
425         "%c_v4f32_1_1_1_1 = OpConstantComposite %v4f32 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_1\n"           \
426         "%c_v4f32_1_0_0_1 = OpConstantComposite %v4f32 %c_f32_1 %c_f32_0 %c_f32_0 %c_f32_1\n"           \
427         "%c_v4f32_0_5_0_5_0_5_0_5 = OpConstantComposite %v4f32 %c_f32_0_5 %c_f32_0_5 %c_f32_0_5 %c_f32_0_5\n"
428
429 #define SPIRV_ASSEMBLY_ARRAYS                                                                                                                                   \
430         "%a1f32 = OpTypeArray %f32 %c_u32_1\n"                                                                                                          \
431         "%a2f32 = OpTypeArray %f32 %c_u32_2\n"                                                                                                          \
432         "%a3v4f32 = OpTypeArray %v4f32 %c_u32_3\n"                                                                                                      \
433         "%a4f32 = OpTypeArray %f32 %c_u32_4\n"                                                                                                          \
434         "%a32v4f32 = OpTypeArray %v4f32 %c_u32_32\n"                                                                                            \
435         "%ip_a3v4f32 = OpTypePointer Input %a3v4f32\n"                                                                                          \
436         "%ip_a32v4f32 = OpTypePointer Input %a32v4f32\n"                                                                                        \
437         "%op_a2f32 = OpTypePointer Output %a2f32\n"                                                                                                     \
438         "%op_a3v4f32 = OpTypePointer Output %a3v4f32\n"                                                                                         \
439         "%op_a4f32 = OpTypePointer Output %a4f32\n"
440
441 // Creates vertex-shader assembly by specializing a boilerplate StringTemplate
442 // on fragments, which must (at least) map "testfun" to an OpFunction definition
443 // for %test_code that takes and returns a %v4f32.  Boilerplate IDs are prefixed
444 // with "BP_" to avoid collisions with fragments.
445 //
446 // It corresponds roughly to this GLSL:
447 //;
448 // layout(location = 0) in vec4 position;
449 // layout(location = 1) in vec4 color;
450 // layout(location = 1) out highp vec4 vtxColor;
451 // void main (void) { gl_Position = position; vtxColor = test_func(color); }
452 string makeVertexShaderAssembly(const map<string, string>& fragments)
453 {
454 // \todo [2015-11-23 awoloszyn] Remove OpName once these have stabalized
455         static const char vertexShaderBoilerplate[] =
456                 "OpCapability Shader\n"
457                 "OpCapability ClipDistance\n"
458                 "OpCapability CullDistance\n"
459                 "${capability:opt}\n"
460                 "${extension:opt}\n"
461                 "OpMemoryModel Logical GLSL450\n"
462                 "OpEntryPoint Vertex %main \"main\" %BP_stream %BP_position %BP_vtx_color %BP_color %BP_gl_VertexIndex %BP_gl_InstanceIndex ${IF_entrypoint:opt} \n"
463                 "${debug:opt}\n"
464                 "OpName %main \"main\"\n"
465                 "OpName %BP_gl_PerVertex \"gl_PerVertex\"\n"
466                 "OpMemberName %BP_gl_PerVertex 0 \"gl_Position\"\n"
467                 "OpMemberName %BP_gl_PerVertex 1 \"gl_PointSize\"\n"
468                 "OpMemberName %BP_gl_PerVertex 2 \"gl_ClipDistance\"\n"
469                 "OpMemberName %BP_gl_PerVertex 3 \"gl_CullDistance\"\n"
470                 "OpName %test_code \"testfun(vf4;\"\n"
471                 "OpName %BP_stream \"\"\n"
472                 "OpName %BP_position \"position\"\n"
473                 "OpName %BP_vtx_color \"vtxColor\"\n"
474                 "OpName %BP_color \"color\"\n"
475                 "OpName %BP_gl_VertexIndex \"gl_VertexIndex\"\n"
476                 "OpName %BP_gl_InstanceIndex \"gl_InstanceIndex\"\n"
477                 "OpMemberDecorate %BP_gl_PerVertex 0 BuiltIn Position\n"
478                 "OpMemberDecorate %BP_gl_PerVertex 1 BuiltIn PointSize\n"
479                 "OpMemberDecorate %BP_gl_PerVertex 2 BuiltIn ClipDistance\n"
480                 "OpMemberDecorate %BP_gl_PerVertex 3 BuiltIn CullDistance\n"
481                 "OpDecorate %BP_gl_PerVertex Block\n"
482                 "OpDecorate %BP_position Location 0\n"
483                 "OpDecorate %BP_vtx_color Location 1\n"
484                 "OpDecorate %BP_color Location 1\n"
485                 "OpDecorate %BP_gl_VertexIndex BuiltIn VertexIndex\n"
486                 "OpDecorate %BP_gl_InstanceIndex BuiltIn InstanceIndex\n"
487                 "${IF_decoration:opt}\n"
488                 "${decoration:opt}\n"
489                 SPIRV_ASSEMBLY_TYPES
490                 SPIRV_ASSEMBLY_CONSTANTS
491                 SPIRV_ASSEMBLY_ARRAYS
492                 "%BP_gl_PerVertex = OpTypeStruct %v4f32 %f32 %a1f32 %a1f32\n"
493                 "%BP_op_gl_PerVertex = OpTypePointer Output %BP_gl_PerVertex\n"
494                 "%BP_stream = OpVariable %BP_op_gl_PerVertex Output\n"
495                 "%BP_position = OpVariable %ip_v4f32 Input\n"
496                 "%BP_vtx_color = OpVariable %op_v4f32 Output\n"
497                 "%BP_color = OpVariable %ip_v4f32 Input\n"
498                 "%BP_gl_VertexIndex = OpVariable %ip_i32 Input\n"
499                 "%BP_gl_InstanceIndex = OpVariable %ip_i32 Input\n"
500                 "${pre_main:opt}\n"
501                 "${IF_variable:opt}\n"
502                 "%main = OpFunction %void None %fun\n"
503                 "%BP_label = OpLabel\n"
504                 "${IF_carryforward:opt}\n"
505                 "%BP_pos = OpLoad %v4f32 %BP_position\n"
506                 "%BP_gl_pos = OpAccessChain %op_v4f32 %BP_stream %c_i32_0\n"
507                 "OpStore %BP_gl_pos %BP_pos\n"
508                 "%BP_col = OpLoad %v4f32 %BP_color\n"
509                 "%BP_col_transformed = OpFunctionCall %v4f32 %test_code %BP_col\n"
510                 "OpStore %BP_vtx_color %BP_col_transformed\n"
511                 "OpReturn\n"
512                 "OpFunctionEnd\n"
513                 "${interface_op_func:opt}\n"
514
515                 "%isUniqueIdZero = OpFunction %bool None %bool_function\n"
516                 "%getId_label = OpLabel\n"
517                 "%vert_id = OpLoad %i32 %BP_gl_VertexIndex\n"
518                 "%is_id_0 = OpIEqual %bool %vert_id %c_i32_0\n"
519                 "OpReturnValue %is_id_0\n"
520                 "OpFunctionEnd\n"
521
522                 "${testfun}\n";
523         return tcu::StringTemplate(vertexShaderBoilerplate).specialize(fragments);
524 }
525
526 // Creates tess-control-shader assembly by specializing a boilerplate
527 // StringTemplate on fragments, which must (at least) map "testfun" to an
528 // OpFunction definition for %test_code that takes and returns a %v4f32.
529 // Boilerplate IDs are prefixed with "BP_" to avoid collisions with fragments.
530 //
531 // It roughly corresponds to the following GLSL.
532 //
533 // #version 450
534 // layout(vertices = 3) out;
535 // layout(location = 1) in vec4 in_color[];
536 // layout(location = 1) out vec4 out_color[];
537 //
538 // void main() {
539 //   out_color[gl_InvocationID] = testfun(in_color[gl_InvocationID]);
540 //   gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
541 //   if (gl_InvocationID == 0) {
542 //     gl_TessLevelOuter[0] = 1.0;
543 //     gl_TessLevelOuter[1] = 1.0;
544 //     gl_TessLevelOuter[2] = 1.0;
545 //     gl_TessLevelInner[0] = 1.0;
546 //   }
547 // }
548 string makeTessControlShaderAssembly (const map<string, string>& fragments)
549 {
550         static const char tessControlShaderBoilerplate[] =
551                 "OpCapability Tessellation\n"
552                 "OpCapability ClipDistance\n"
553                 "OpCapability CullDistance\n"
554                 "${capability:opt}\n"
555                 "${extension:opt}\n"
556                 "OpMemoryModel Logical GLSL450\n"
557                 "OpEntryPoint TessellationControl %BP_main \"main\" %BP_out_color %BP_gl_InvocationID %BP_in_color %BP_gl_out %BP_gl_in %BP_gl_TessLevelOuter %BP_gl_TessLevelInner ${IF_entrypoint:opt} \n"
558                 "OpExecutionMode %BP_main OutputVertices 3\n"
559                 "${debug:opt}\n"
560                 "OpName %BP_main \"main\"\n"
561                 "OpName %test_code \"testfun(vf4;\"\n"
562                 "OpName %BP_out_color \"out_color\"\n"
563                 "OpName %BP_gl_InvocationID \"gl_InvocationID\"\n"
564                 "OpName %BP_in_color \"in_color\"\n"
565                 "OpName %BP_gl_PerVertex \"gl_PerVertex\"\n"
566                 "OpMemberName %BP_gl_PerVertex 0 \"gl_Position\"\n"
567                 "OpMemberName %BP_gl_PerVertex 1 \"gl_PointSize\"\n"
568                 "OpMemberName %BP_gl_PerVertex 2 \"gl_ClipDistance\"\n"
569                 "OpMemberName %BP_gl_PerVertex 3 \"gl_CullDistance\"\n"
570                 "OpName %BP_gl_out \"gl_out\"\n"
571                 "OpName %BP_gl_PVOut \"gl_PerVertex\"\n"
572                 "OpMemberName %BP_gl_PVOut 0 \"gl_Position\"\n"
573                 "OpMemberName %BP_gl_PVOut 1 \"gl_PointSize\"\n"
574                 "OpMemberName %BP_gl_PVOut 2 \"gl_ClipDistance\"\n"
575                 "OpMemberName %BP_gl_PVOut 3 \"gl_CullDistance\"\n"
576                 "OpName %BP_gl_in \"gl_in\"\n"
577                 "OpName %BP_gl_TessLevelOuter \"gl_TessLevelOuter\"\n"
578                 "OpName %BP_gl_TessLevelInner \"gl_TessLevelInner\"\n"
579                 "OpDecorate %BP_out_color Location 1\n"
580                 "OpDecorate %BP_gl_InvocationID BuiltIn InvocationId\n"
581                 "OpDecorate %BP_in_color Location 1\n"
582                 "OpMemberDecorate %BP_gl_PerVertex 0 BuiltIn Position\n"
583                 "OpMemberDecorate %BP_gl_PerVertex 1 BuiltIn PointSize\n"
584                 "OpMemberDecorate %BP_gl_PerVertex 2 BuiltIn ClipDistance\n"
585                 "OpMemberDecorate %BP_gl_PerVertex 3 BuiltIn CullDistance\n"
586                 "OpDecorate %BP_gl_PerVertex Block\n"
587                 "OpMemberDecorate %BP_gl_PVOut 0 BuiltIn Position\n"
588                 "OpMemberDecorate %BP_gl_PVOut 1 BuiltIn PointSize\n"
589                 "OpMemberDecorate %BP_gl_PVOut 2 BuiltIn ClipDistance\n"
590                 "OpMemberDecorate %BP_gl_PVOut 3 BuiltIn CullDistance\n"
591                 "OpDecorate %BP_gl_PVOut Block\n"
592                 "OpDecorate %BP_gl_TessLevelOuter Patch\n"
593                 "OpDecorate %BP_gl_TessLevelOuter BuiltIn TessLevelOuter\n"
594                 "OpDecorate %BP_gl_TessLevelInner Patch\n"
595                 "OpDecorate %BP_gl_TessLevelInner BuiltIn TessLevelInner\n"
596                 "${IF_decoration:opt}\n"
597                 "${decoration:opt}\n"
598                 SPIRV_ASSEMBLY_TYPES
599                 SPIRV_ASSEMBLY_CONSTANTS
600                 SPIRV_ASSEMBLY_ARRAYS
601                 "%BP_out_color = OpVariable %op_a3v4f32 Output\n"
602                 "%BP_gl_InvocationID = OpVariable %ip_i32 Input\n"
603                 "%BP_in_color = OpVariable %ip_a32v4f32 Input\n"
604                 "%BP_gl_PerVertex = OpTypeStruct %v4f32 %f32 %a1f32 %a1f32\n"
605                 "%BP_a3_gl_PerVertex = OpTypeArray %BP_gl_PerVertex %c_u32_3\n"
606                 "%BP_op_a3_gl_PerVertex = OpTypePointer Output %BP_a3_gl_PerVertex\n"
607                 "%BP_gl_out = OpVariable %BP_op_a3_gl_PerVertex Output\n"
608                 "%BP_gl_PVOut = OpTypeStruct %v4f32 %f32 %a1f32 %a1f32\n"
609                 "%BP_a32_gl_PVOut = OpTypeArray %BP_gl_PVOut %c_u32_32\n"
610                 "%BP_ip_a32_gl_PVOut = OpTypePointer Input %BP_a32_gl_PVOut\n"
611                 "%BP_gl_in = OpVariable %BP_ip_a32_gl_PVOut Input\n"
612                 "%BP_gl_TessLevelOuter = OpVariable %op_a4f32 Output\n"
613                 "%BP_gl_TessLevelInner = OpVariable %op_a2f32 Output\n"
614                 "${pre_main:opt}\n"
615                 "${IF_variable:opt}\n"
616
617                 "%BP_main = OpFunction %void None %fun\n"
618                 "%BP_label = OpLabel\n"
619                 "%BP_gl_Invoc = OpLoad %i32 %BP_gl_InvocationID\n"
620                 "${IF_carryforward:opt}\n"
621                 "%BP_in_col_loc = OpAccessChain %ip_v4f32 %BP_in_color %BP_gl_Invoc\n"
622                 "%BP_out_col_loc = OpAccessChain %op_v4f32 %BP_out_color %BP_gl_Invoc\n"
623                 "%BP_in_col_val = OpLoad %v4f32 %BP_in_col_loc\n"
624                 "%BP_clr_transformed = OpFunctionCall %v4f32 %test_code %BP_in_col_val\n"
625                 "OpStore %BP_out_col_loc %BP_clr_transformed\n"
626
627                 "%BP_in_pos_loc = OpAccessChain %ip_v4f32 %BP_gl_in %BP_gl_Invoc %c_i32_0\n"
628                 "%BP_out_pos_loc = OpAccessChain %op_v4f32 %BP_gl_out %BP_gl_Invoc %c_i32_0\n"
629                 "%BP_in_pos_val = OpLoad %v4f32 %BP_in_pos_loc\n"
630                 "OpStore %BP_out_pos_loc %BP_in_pos_val\n"
631
632                 "%BP_cmp = OpIEqual %bool %BP_gl_Invoc %c_i32_0\n"
633                 "OpSelectionMerge %BP_merge_label None\n"
634                 "OpBranchConditional %BP_cmp %BP_if_label %BP_merge_label\n"
635                 "%BP_if_label = OpLabel\n"
636                 "%BP_gl_TessLevelOuterPos_0 = OpAccessChain %op_f32 %BP_gl_TessLevelOuter %c_i32_0\n"
637                 "%BP_gl_TessLevelOuterPos_1 = OpAccessChain %op_f32 %BP_gl_TessLevelOuter %c_i32_1\n"
638                 "%BP_gl_TessLevelOuterPos_2 = OpAccessChain %op_f32 %BP_gl_TessLevelOuter %c_i32_2\n"
639                 "%BP_gl_TessLevelInnerPos_0 = OpAccessChain %op_f32 %BP_gl_TessLevelInner %c_i32_0\n"
640                 "OpStore %BP_gl_TessLevelOuterPos_0 %c_f32_1\n"
641                 "OpStore %BP_gl_TessLevelOuterPos_1 %c_f32_1\n"
642                 "OpStore %BP_gl_TessLevelOuterPos_2 %c_f32_1\n"
643                 "OpStore %BP_gl_TessLevelInnerPos_0 %c_f32_1\n"
644                 "OpBranch %BP_merge_label\n"
645                 "%BP_merge_label = OpLabel\n"
646                 "OpReturn\n"
647                 "OpFunctionEnd\n"
648                 "${interface_op_func:opt}\n"
649
650                 "%isUniqueIdZero = OpFunction %bool None %bool_function\n"
651                 "%getId_label = OpLabel\n"
652                 "%invocation_id = OpLoad %i32 %BP_gl_InvocationID\n"
653                 "%is_id_0 = OpIEqual %bool %invocation_id %c_i32_0\n"
654                 "OpReturnValue %is_id_0\n"
655                 "OpFunctionEnd\n"
656
657                 "${testfun}\n";
658         return tcu::StringTemplate(tessControlShaderBoilerplate).specialize(fragments);
659 }
660
661 // Creates tess-evaluation-shader assembly by specializing a boilerplate
662 // StringTemplate on fragments, which must (at least) map "testfun" to an
663 // OpFunction definition for %test_code that takes and returns a %v4f32.
664 // Boilerplate IDs are prefixed with "BP_" to avoid collisions with fragments.
665 //
666 // It roughly corresponds to the following glsl.
667 //
668 // #version 450
669 //
670 // layout(triangles, equal_spacing, ccw) in;
671 // layout(location = 1) in vec4 in_color[];
672 // layout(location = 1) out vec4 out_color;
673 //
674 // #define interpolate(val)
675 //   vec4(gl_TessCoord.x) * val[0] + vec4(gl_TessCoord.y) * val[1] +
676 //          vec4(gl_TessCoord.z) * val[2]
677 //
678 // void main() {
679 //   gl_Position = vec4(gl_TessCoord.x) * gl_in[0].gl_Position +
680 //                  vec4(gl_TessCoord.y) * gl_in[1].gl_Position +
681 //                  vec4(gl_TessCoord.z) * gl_in[2].gl_Position;
682 //   out_color = testfun(interpolate(in_color));
683 // }
684 string makeTessEvalShaderAssembly(const map<string, string>& fragments)
685 {
686         static const char tessEvalBoilerplate[] =
687                 "OpCapability Tessellation\n"
688                 "OpCapability ClipDistance\n"
689                 "OpCapability CullDistance\n"
690                 "${capability:opt}\n"
691                 "${extension:opt}\n"
692                 "OpMemoryModel Logical GLSL450\n"
693                 "OpEntryPoint TessellationEvaluation %BP_main \"main\" %BP_stream %BP_gl_TessCoord %BP_gl_PrimitiveID %BP_gl_in %BP_out_color %BP_in_color ${IF_entrypoint:opt} \n"
694                 "OpExecutionMode %BP_main Triangles\n"
695                 "OpExecutionMode %BP_main SpacingEqual\n"
696                 "OpExecutionMode %BP_main VertexOrderCcw\n"
697                 "${debug:opt}\n"
698                 "OpName %BP_main \"main\"\n"
699                 "OpName %test_code \"testfun(vf4;\"\n"
700                 "OpName %BP_gl_PerVertexOut \"gl_PerVertex\"\n"
701                 "OpMemberName %BP_gl_PerVertexOut 0 \"gl_Position\"\n"
702                 "OpMemberName %BP_gl_PerVertexOut 1 \"gl_PointSize\"\n"
703                 "OpMemberName %BP_gl_PerVertexOut 2 \"gl_ClipDistance\"\n"
704                 "OpMemberName %BP_gl_PerVertexOut 3 \"gl_CullDistance\"\n"
705                 "OpName %BP_stream \"\"\n"
706                 "OpName %BP_gl_TessCoord \"gl_TessCoord\"\n"
707                 "OpName %BP_gl_PerVertexIn \"gl_PerVertex\"\n"
708                 "OpName %BP_gl_PrimitiveID \"gl_PrimitiveID\"\n"
709                 "OpMemberName %BP_gl_PerVertexIn 0 \"gl_Position\"\n"
710                 "OpMemberName %BP_gl_PerVertexIn 1 \"gl_PointSize\"\n"
711                 "OpMemberName %BP_gl_PerVertexIn 2 \"gl_ClipDistance\"\n"
712                 "OpMemberName %BP_gl_PerVertexIn 3 \"gl_CullDistance\"\n"
713                 "OpName %BP_gl_in \"gl_in\"\n"
714                 "OpName %BP_out_color \"out_color\"\n"
715                 "OpName %BP_in_color \"in_color\"\n"
716                 "OpMemberDecorate %BP_gl_PerVertexOut 0 BuiltIn Position\n"
717                 "OpMemberDecorate %BP_gl_PerVertexOut 1 BuiltIn PointSize\n"
718                 "OpMemberDecorate %BP_gl_PerVertexOut 2 BuiltIn ClipDistance\n"
719                 "OpMemberDecorate %BP_gl_PerVertexOut 3 BuiltIn CullDistance\n"
720                 "OpDecorate %BP_gl_PerVertexOut Block\n"
721                 "OpDecorate %BP_gl_PrimitiveID BuiltIn PrimitiveId\n"
722                 "OpDecorate %BP_gl_TessCoord BuiltIn TessCoord\n"
723                 "OpMemberDecorate %BP_gl_PerVertexIn 0 BuiltIn Position\n"
724                 "OpMemberDecorate %BP_gl_PerVertexIn 1 BuiltIn PointSize\n"
725                 "OpMemberDecorate %BP_gl_PerVertexIn 2 BuiltIn ClipDistance\n"
726                 "OpMemberDecorate %BP_gl_PerVertexIn 3 BuiltIn CullDistance\n"
727                 "OpDecorate %BP_gl_PerVertexIn Block\n"
728                 "OpDecorate %BP_out_color Location 1\n"
729                 "OpDecorate %BP_in_color Location 1\n"
730                 "${IF_decoration:opt}\n"
731                 "${decoration:opt}\n"
732                 SPIRV_ASSEMBLY_TYPES
733                 SPIRV_ASSEMBLY_CONSTANTS
734                 SPIRV_ASSEMBLY_ARRAYS
735                 "%BP_gl_PerVertexOut = OpTypeStruct %v4f32 %f32 %a1f32 %a1f32\n"
736                 "%BP_op_gl_PerVertexOut = OpTypePointer Output %BP_gl_PerVertexOut\n"
737                 "%BP_stream = OpVariable %BP_op_gl_PerVertexOut Output\n"
738                 "%BP_gl_TessCoord = OpVariable %ip_v3f32 Input\n"
739                 "%BP_gl_PrimitiveID = OpVariable %op_i32 Input\n"
740                 "%BP_gl_PerVertexIn = OpTypeStruct %v4f32 %f32 %a1f32 %a1f32\n"
741                 "%BP_a32_gl_PerVertexIn = OpTypeArray %BP_gl_PerVertexIn %c_u32_32\n"
742                 "%BP_ip_a32_gl_PerVertexIn = OpTypePointer Input %BP_a32_gl_PerVertexIn\n"
743                 "%BP_gl_in = OpVariable %BP_ip_a32_gl_PerVertexIn Input\n"
744                 "%BP_out_color = OpVariable %op_v4f32 Output\n"
745                 "%BP_in_color = OpVariable %ip_a32v4f32 Input\n"
746                 "${pre_main:opt}\n"
747                 "${IF_variable:opt}\n"
748                 "%BP_main = OpFunction %void None %fun\n"
749                 "%BP_label = OpLabel\n"
750                 "${IF_carryforward:opt}\n"
751                 "%BP_gl_TC_0 = OpAccessChain %ip_f32 %BP_gl_TessCoord %c_u32_0\n"
752                 "%BP_gl_TC_1 = OpAccessChain %ip_f32 %BP_gl_TessCoord %c_u32_1\n"
753                 "%BP_gl_TC_2 = OpAccessChain %ip_f32 %BP_gl_TessCoord %c_u32_2\n"
754                 "%BP_gl_in_gl_Pos_0 = OpAccessChain %ip_v4f32 %BP_gl_in %c_i32_0 %c_i32_0\n"
755                 "%BP_gl_in_gl_Pos_1 = OpAccessChain %ip_v4f32 %BP_gl_in %c_i32_1 %c_i32_0\n"
756                 "%BP_gl_in_gl_Pos_2 = OpAccessChain %ip_v4f32 %BP_gl_in %c_i32_2 %c_i32_0\n"
757
758                 "%BP_gl_OPos = OpAccessChain %op_v4f32 %BP_stream %c_i32_0\n"
759                 "%BP_in_color_0 = OpAccessChain %ip_v4f32 %BP_in_color %c_i32_0\n"
760                 "%BP_in_color_1 = OpAccessChain %ip_v4f32 %BP_in_color %c_i32_1\n"
761                 "%BP_in_color_2 = OpAccessChain %ip_v4f32 %BP_in_color %c_i32_2\n"
762
763                 "%BP_TC_W_0 = OpLoad %f32 %BP_gl_TC_0\n"
764                 "%BP_TC_W_1 = OpLoad %f32 %BP_gl_TC_1\n"
765                 "%BP_TC_W_2 = OpLoad %f32 %BP_gl_TC_2\n"
766                 "%BP_v4f32_TC_0 = OpCompositeConstruct %v4f32 %BP_TC_W_0 %BP_TC_W_0 %BP_TC_W_0 %BP_TC_W_0\n"
767                 "%BP_v4f32_TC_1 = OpCompositeConstruct %v4f32 %BP_TC_W_1 %BP_TC_W_1 %BP_TC_W_1 %BP_TC_W_1\n"
768                 "%BP_v4f32_TC_2 = OpCompositeConstruct %v4f32 %BP_TC_W_2 %BP_TC_W_2 %BP_TC_W_2 %BP_TC_W_2\n"
769
770                 "%BP_gl_IP_0 = OpLoad %v4f32 %BP_gl_in_gl_Pos_0\n"
771                 "%BP_gl_IP_1 = OpLoad %v4f32 %BP_gl_in_gl_Pos_1\n"
772                 "%BP_gl_IP_2 = OpLoad %v4f32 %BP_gl_in_gl_Pos_2\n"
773
774                 "%BP_IP_W_0 = OpFMul %v4f32 %BP_v4f32_TC_0 %BP_gl_IP_0\n"
775                 "%BP_IP_W_1 = OpFMul %v4f32 %BP_v4f32_TC_1 %BP_gl_IP_1\n"
776                 "%BP_IP_W_2 = OpFMul %v4f32 %BP_v4f32_TC_2 %BP_gl_IP_2\n"
777
778                 "%BP_pos_sum_0 = OpFAdd %v4f32 %BP_IP_W_0 %BP_IP_W_1\n"
779                 "%BP_pos_sum_1 = OpFAdd %v4f32 %BP_pos_sum_0 %BP_IP_W_2\n"
780
781                 "OpStore %BP_gl_OPos %BP_pos_sum_1\n"
782
783                 "%BP_IC_0 = OpLoad %v4f32 %BP_in_color_0\n"
784                 "%BP_IC_1 = OpLoad %v4f32 %BP_in_color_1\n"
785                 "%BP_IC_2 = OpLoad %v4f32 %BP_in_color_2\n"
786
787                 "%BP_IC_W_0 = OpFMul %v4f32 %BP_v4f32_TC_0 %BP_IC_0\n"
788                 "%BP_IC_W_1 = OpFMul %v4f32 %BP_v4f32_TC_1 %BP_IC_1\n"
789                 "%BP_IC_W_2 = OpFMul %v4f32 %BP_v4f32_TC_2 %BP_IC_2\n"
790
791                 "%BP_col_sum_0 = OpFAdd %v4f32 %BP_IC_W_0 %BP_IC_W_1\n"
792                 "%BP_col_sum_1 = OpFAdd %v4f32 %BP_col_sum_0 %BP_IC_W_2\n"
793
794                 "%BP_clr_transformed = OpFunctionCall %v4f32 %test_code %BP_col_sum_1\n"
795
796                 "OpStore %BP_out_color %BP_clr_transformed\n"
797                 "OpReturn\n"
798                 "OpFunctionEnd\n"
799                 "${interface_op_func:opt}\n"
800
801                 "%isUniqueIdZero = OpFunction %bool None %bool_function\n"
802                 "%getId_label = OpLabel\n"
803                 "%primitive_id = OpLoad %i32 %BP_gl_PrimitiveID\n"
804                 "%is_primitive_0 = OpIEqual %bool %primitive_id %c_i32_0\n"
805                 "%TC_0_loc = OpAccessChain %ip_f32 %BP_gl_TessCoord %c_u32_0\n"
806                 "%TC_1_loc = OpAccessChain %ip_f32 %BP_gl_TessCoord %c_u32_1\n"
807                 "%TC_2_loc = OpAccessChain %ip_f32 %BP_gl_TessCoord %c_u32_2\n"
808                 "%TC_W_0 = OpLoad %f32 %TC_0_loc\n"
809                 "%TC_W_1 = OpLoad %f32 %TC_1_loc\n"
810                 "%TC_W_2 = OpLoad %f32 %TC_2_loc\n"
811                 "%is_W_0_1 = OpFOrdEqual %bool %TC_W_0 %c_f32_1\n"
812                 "%is_W_1_0 = OpFOrdEqual %bool %TC_W_1 %c_f32_0\n"
813                 "%is_W_2_0 = OpFOrdEqual %bool %TC_W_2 %c_f32_0\n"
814                 "%is_tessCoord_1_0 = OpLogicalAnd %bool %is_W_0_1 %is_W_1_0\n"
815                 "%is_tessCoord_1_0_0 = OpLogicalAnd %bool %is_tessCoord_1_0 %is_W_2_0\n"
816                 "%is_unique_id_0 = OpLogicalAnd %bool %is_tessCoord_1_0_0 %is_primitive_0\n"
817                 "OpReturnValue %is_unique_id_0\n"
818                 "OpFunctionEnd\n"
819
820                 "${testfun}\n";
821         return tcu::StringTemplate(tessEvalBoilerplate).specialize(fragments);
822 }
823
824 // Creates geometry-shader assembly by specializing a boilerplate StringTemplate
825 // on fragments, which must (at least) map "testfun" to an OpFunction definition
826 // for %test_code that takes and returns a %v4f32.  Boilerplate IDs are prefixed
827 // with "BP_" to avoid collisions with fragments.
828 //
829 // Derived from this GLSL:
830 //
831 // #version 450
832 // layout(triangles) in;
833 // layout(triangle_strip, max_vertices = 3) out;
834 //
835 // layout(location = 1) in vec4 in_color[];
836 // layout(location = 1) out vec4 out_color;
837 //
838 // void main() {
839 //   gl_Position = gl_in[0].gl_Position;
840 //   out_color = test_fun(in_color[0]);
841 //   EmitVertex();
842 //   gl_Position = gl_in[1].gl_Position;
843 //   out_color = test_fun(in_color[1]);
844 //   EmitVertex();
845 //   gl_Position = gl_in[2].gl_Position;
846 //   out_color = test_fun(in_color[2]);
847 //   EmitVertex();
848 //   EndPrimitive();
849 // }
850 string makeGeometryShaderAssembly(const map<string, string>& fragments)
851 {
852         static const char geometryShaderBoilerplate[] =
853                 "OpCapability Geometry\n"
854                 "OpCapability ClipDistance\n"
855                 "OpCapability CullDistance\n"
856                 "${capability:opt}\n"
857                 "${extension:opt}\n"
858                 "OpMemoryModel Logical GLSL450\n"
859                 "OpEntryPoint Geometry %BP_main \"main\" %BP_out_gl_position %BP_gl_PrimitiveID %BP_gl_in %BP_out_color %BP_in_color ${IF_entrypoint:opt} \n"
860                 "OpExecutionMode %BP_main Triangles\n"
861                 "OpExecutionMode %BP_main OutputTriangleStrip\n"
862                 "OpExecutionMode %BP_main OutputVertices 3\n"
863                 "${debug:opt}\n"
864                 "OpName %BP_main \"main\"\n"
865                 "OpName %BP_gl_PrimitiveID \"gl_PrimitiveID\"\n"
866                 "OpName %BP_per_vertex_in \"gl_PerVertex\"\n"
867                 "OpMemberName %BP_per_vertex_in 0 \"gl_Position\"\n"
868                 "OpMemberName %BP_per_vertex_in 1 \"gl_PointSize\"\n"
869                 "OpMemberName %BP_per_vertex_in 2 \"gl_ClipDistance\"\n"
870                 "OpMemberName %BP_per_vertex_in 3 \"gl_CullDistance\"\n"
871                 "OpName %BP_gl_in \"gl_in\"\n"
872                 "OpName %BP_out_color \"out_color\"\n"
873                 "OpName %BP_in_color \"in_color\"\n"
874                 "OpName %test_code \"testfun(vf4;\"\n"
875                 "OpDecorate %BP_gl_PrimitiveID BuiltIn PrimitiveId\n"
876                 "OpDecorate %BP_out_gl_position BuiltIn Position\n"
877                 "OpMemberDecorate %BP_per_vertex_in 0 BuiltIn Position\n"
878                 "OpMemberDecorate %BP_per_vertex_in 1 BuiltIn PointSize\n"
879                 "OpMemberDecorate %BP_per_vertex_in 2 BuiltIn ClipDistance\n"
880                 "OpMemberDecorate %BP_per_vertex_in 3 BuiltIn CullDistance\n"
881                 "OpDecorate %BP_per_vertex_in Block\n"
882                 "OpDecorate %BP_out_color Location 1\n"
883                 "OpDecorate %BP_in_color Location 1\n"
884                 "${IF_decoration:opt}\n"
885                 "${decoration:opt}\n"
886                 SPIRV_ASSEMBLY_TYPES
887                 SPIRV_ASSEMBLY_CONSTANTS
888                 SPIRV_ASSEMBLY_ARRAYS
889                 "%BP_per_vertex_in = OpTypeStruct %v4f32 %f32 %a1f32 %a1f32\n"
890                 "%BP_a3_per_vertex_in = OpTypeArray %BP_per_vertex_in %c_u32_3\n"
891                 "%BP_ip_a3_per_vertex_in = OpTypePointer Input %BP_a3_per_vertex_in\n"
892                 "%BP_pp_i32 = OpTypePointer Private %i32\n"
893                 "%BP_pp_v4i32 = OpTypePointer Private %v4i32\n"
894
895                 "%BP_gl_in = OpVariable %BP_ip_a3_per_vertex_in Input\n"
896                 "%BP_out_color = OpVariable %op_v4f32 Output\n"
897                 "%BP_in_color = OpVariable %ip_a3v4f32 Input\n"
898                 "%BP_gl_PrimitiveID = OpVariable %ip_i32 Input\n"
899                 "%BP_out_gl_position = OpVariable %op_v4f32 Output\n"
900                 "%BP_vertexIdInCurrentPatch = OpVariable %BP_pp_v4i32 Private\n"
901                 "${pre_main:opt}\n"
902                 "${IF_variable:opt}\n"
903
904                 "%BP_main = OpFunction %void None %fun\n"
905                 "%BP_label = OpLabel\n"
906
907                 "${IF_carryforward:opt}\n"
908
909                 "%BP_primitiveId = OpLoad %i32 %BP_gl_PrimitiveID\n"
910                 "%BP_addr_vertexIdInCurrentPatch = OpAccessChain %BP_pp_i32 %BP_vertexIdInCurrentPatch %BP_primitiveId\n"
911
912                 "%BP_gl_in_0_gl_position = OpAccessChain %ip_v4f32 %BP_gl_in %c_i32_0 %c_i32_0\n"
913                 "%BP_gl_in_1_gl_position = OpAccessChain %ip_v4f32 %BP_gl_in %c_i32_1 %c_i32_0\n"
914                 "%BP_gl_in_2_gl_position = OpAccessChain %ip_v4f32 %BP_gl_in %c_i32_2 %c_i32_0\n"
915
916                 "%BP_in_position_0 = OpLoad %v4f32 %BP_gl_in_0_gl_position\n"
917                 "%BP_in_position_1 = OpLoad %v4f32 %BP_gl_in_1_gl_position\n"
918                 "%BP_in_position_2 = OpLoad %v4f32 %BP_gl_in_2_gl_position \n"
919
920                 "%BP_in_color_0_ptr = OpAccessChain %ip_v4f32 %BP_in_color %c_i32_0\n"
921                 "%BP_in_color_1_ptr = OpAccessChain %ip_v4f32 %BP_in_color %c_i32_1\n"
922                 "%BP_in_color_2_ptr = OpAccessChain %ip_v4f32 %BP_in_color %c_i32_2\n"
923
924                 "%BP_in_color_0 = OpLoad %v4f32 %BP_in_color_0_ptr\n"
925                 "%BP_in_color_1 = OpLoad %v4f32 %BP_in_color_1_ptr\n"
926                 "%BP_in_color_2 = OpLoad %v4f32 %BP_in_color_2_ptr\n"
927
928                 "OpStore %BP_addr_vertexIdInCurrentPatch %c_i32_0\n"
929                 "%BP_transformed_in_color_0 = OpFunctionCall %v4f32 %test_code %BP_in_color_0\n"
930                 "OpStore %BP_addr_vertexIdInCurrentPatch %c_i32_1\n"
931                 "%BP_transformed_in_color_1 = OpFunctionCall %v4f32 %test_code %BP_in_color_1\n"
932                 "OpStore %BP_addr_vertexIdInCurrentPatch %c_i32_2\n"
933                 "%BP_transformed_in_color_2 = OpFunctionCall %v4f32 %test_code %BP_in_color_2\n"
934
935
936                 "OpStore %BP_out_gl_position %BP_in_position_0\n"
937                 "OpStore %BP_out_color %BP_transformed_in_color_0\n"
938                 "OpEmitVertex\n"
939
940                 "OpStore %BP_out_gl_position %BP_in_position_1\n"
941                 "OpStore %BP_out_color %BP_transformed_in_color_1\n"
942                 "OpEmitVertex\n"
943
944                 "OpStore %BP_out_gl_position %BP_in_position_2\n"
945                 "OpStore %BP_out_color %BP_transformed_in_color_2\n"
946                 "OpEmitVertex\n"
947
948                 "OpEndPrimitive\n"
949                 "OpReturn\n"
950                 "OpFunctionEnd\n"
951                 "${interface_op_func:opt}\n"
952
953                 "%isUniqueIdZero = OpFunction %bool None %bool_function\n"
954                 "%getId_label = OpLabel\n"
955                 "%primitive_id = OpLoad %i32 %BP_gl_PrimitiveID\n"
956                 "%addr_vertexIdInCurrentPatch = OpAccessChain %BP_pp_i32 %BP_vertexIdInCurrentPatch %primitive_id\n"
957                 "%vertexIdInCurrentPatch = OpLoad %i32 %addr_vertexIdInCurrentPatch\n"
958                 "%is_primitive_0 = OpIEqual %bool %primitive_id %c_i32_0\n"
959                 "%is_vertex_0 = OpIEqual %bool %vertexIdInCurrentPatch %c_i32_0\n"
960                 "%is_unique_id_0 = OpLogicalAnd %bool %is_primitive_0 %is_vertex_0\n"
961                 "OpReturnValue %is_unique_id_0\n"
962                 "OpFunctionEnd\n"
963
964                 "${testfun}\n";
965         return tcu::StringTemplate(geometryShaderBoilerplate).specialize(fragments);
966 }
967
968 // Creates fragment-shader assembly by specializing a boilerplate StringTemplate
969 // on fragments, which must (at least) map "testfun" to an OpFunction definition
970 // for %test_code that takes and returns a %v4f32.  Boilerplate IDs are prefixed
971 // with "BP_" to avoid collisions with fragments.
972 //
973 // Derived from this GLSL:
974 //
975 // layout(location = 1) in highp vec4 vtxColor;
976 // layout(location = 0) out highp vec4 fragColor;
977 // highp vec4 testfun(highp vec4 x) { return x; }
978 // void main(void) { fragColor = testfun(vtxColor); }
979 //
980 // with modifications including passing vtxColor by value and ripping out
981 // testfun() definition.
982 string makeFragmentShaderAssembly(const map<string, string>& fragments)
983 {
984         static const char fragmentShaderBoilerplate[] =
985                 "OpCapability Shader\n"
986                 "${capability:opt}\n"
987                 "${extension:opt}\n"
988                 "OpMemoryModel Logical GLSL450\n"
989                 "OpEntryPoint Fragment %BP_main \"main\" %BP_vtxColor %BP_fragColor %BP_gl_FragCoord ${IF_entrypoint:opt} \n"
990                 "OpExecutionMode %BP_main OriginUpperLeft\n"
991                 "${debug:opt}\n"
992                 "OpName %BP_main \"main\"\n"
993                 "OpName %BP_gl_FragCoord \"fragCoord\"\n"
994                 "OpName %BP_fragColor \"fragColor\"\n"
995                 "OpName %BP_vtxColor \"vtxColor\"\n"
996                 "OpName %test_code \"testfun(vf4;\"\n"
997                 "OpDecorate %BP_fragColor Location 0\n"
998                 "OpDecorate %BP_vtxColor Location 1\n"
999                 "OpDecorate %BP_gl_FragCoord BuiltIn FragCoord\n"
1000                 "${IF_decoration:opt}\n"
1001                 "${decoration:opt}\n"
1002                 SPIRV_ASSEMBLY_TYPES
1003                 SPIRV_ASSEMBLY_CONSTANTS
1004                 SPIRV_ASSEMBLY_ARRAYS
1005                 "%BP_gl_FragCoord = OpVariable %ip_v4f32 Input\n"
1006                 "%BP_fragColor = OpVariable %op_v4f32 Output\n"
1007                 "%BP_vtxColor = OpVariable %ip_v4f32 Input\n"
1008                 "${pre_main:opt}\n"
1009                 "${IF_variable:opt}\n"
1010                 "%BP_main = OpFunction %void None %fun\n"
1011                 "%BP_label_main = OpLabel\n"
1012                 "${IF_carryforward:opt}\n"
1013                 "%BP_tmp1 = OpLoad %v4f32 %BP_vtxColor\n"
1014                 "%BP_tmp2 = OpFunctionCall %v4f32 %test_code %BP_tmp1\n"
1015                 "OpStore %BP_fragColor %BP_tmp2\n"
1016                 "OpReturn\n"
1017                 "OpFunctionEnd\n"
1018                 "${interface_op_func:opt}\n"
1019
1020                 "%isUniqueIdZero = OpFunction %bool None %bool_function\n"
1021                 "%getId_label = OpLabel\n"
1022                 "%loc_x_coord = OpAccessChain %ip_f32 %BP_gl_FragCoord %c_i32_0\n"
1023                 "%loc_y_coord = OpAccessChain %ip_f32 %BP_gl_FragCoord %c_i32_1\n"
1024                 "%x_coord = OpLoad %f32 %loc_x_coord\n"
1025                 "%y_coord = OpLoad %f32 %loc_y_coord\n"
1026                 "%is_x_idx0 = OpFOrdEqual %bool %x_coord %c_f32_0_5\n"
1027                 "%is_y_idx0 = OpFOrdEqual %bool %y_coord %c_f32_0_5\n"
1028                 "%is_frag_0 = OpLogicalAnd %bool %is_x_idx0 %is_y_idx0\n"
1029                 "OpReturnValue %is_frag_0\n"
1030                 "OpFunctionEnd\n"
1031
1032                 "${testfun}\n";
1033         return tcu::StringTemplate(fragmentShaderBoilerplate).specialize(fragments);
1034 }
1035
1036 // Creates mappings from placeholders to pass-through shader code which copies
1037 // the input to the output faithfully.
1038 map<string, string> passthruInterface(const IFDataType& data_type)
1039 {
1040         const string            var_type        = data_type.str();
1041         map<string, string>     fragments       = passthruFragments();
1042         const string            functype        = string("%") + var_type + "_" + var_type + "_function";
1043
1044         fragments["interface_op_func"]  =
1045                 string("%interface_op_func = OpFunction %") + var_type + " None " + functype + "\n"
1046                 "               %io_param1 = OpFunctionParameter %" + var_type + "\n"
1047                 "                %IF_label = OpLabel\n"
1048                 "                            OpReturnValue %io_param1\n"
1049                 "                            OpFunctionEnd\n";
1050         fragments["input_type"]                 = var_type;
1051         fragments["output_type"]                = var_type;
1052         fragments["pre_main"]                   = "";
1053
1054         if (!data_type.elementIs32bit())
1055         {
1056                 if (data_type.elementType == NUMBERTYPE_FLOAT16)
1057                 {
1058                         fragments["pre_main"]   += "%f16 = OpTypeFloat 16\n";
1059                 }
1060                 else if (data_type.elementType == NUMBERTYPE_INT16)
1061                 {
1062                         fragments["pre_main"]   += "%i16 = OpTypeInt 16 1\n";
1063                 }
1064                 else
1065                 {
1066                         fragments["pre_main"]   += "%u16 = OpTypeInt 16 0\n";
1067                 }
1068
1069                 fragments["capability"]         = "OpCapability StorageInputOutput16\n";
1070                 fragments["extension"]          = "OpExtension \"SPV_KHR_16bit_storage\"\n";
1071
1072                 if (data_type.isVector())
1073                 {
1074                         fragments["pre_main"]   += "%" + var_type + " = OpTypeVector %" + IFDataType(1, data_type.elementType).str() + " " + numberToString(data_type.numElements) + "\n";
1075                 }
1076
1077                 fragments["pre_main"]           +=
1078                         "%ip_" + var_type + " = OpTypePointer Input %" + var_type + "\n"
1079                         "%op_" + var_type + " = OpTypePointer Output %" + var_type + "\n";
1080         }
1081
1082         fragments["pre_main"]                   +=
1083                 functype + " = OpTypeFunction %" + var_type + " %" + var_type + "\n"
1084                 "%a3" + var_type + " = OpTypeArray %" + var_type + " %c_i32_3\n"
1085                 "%ip_a3" + var_type + " = OpTypePointer Input %a3" + var_type + "\n"
1086                 "%op_a3" + var_type + " = OpTypePointer Output %a3" + var_type + "\n";
1087
1088         return fragments;
1089 }
1090
1091 // Returns mappings from interface placeholders to their concrete values.
1092 //
1093 // The concrete values should be specialized again to provide ${input_type}
1094 // and ${output_type}.
1095 //
1096 // %ip_${input_type} and %op_${output_type} should also be defined in the final code.
1097 map<string, string> fillInterfacePlaceholderVert (void)
1098 {
1099         map<string, string>     fragments       ;
1100
1101         fragments["IF_entrypoint"]              = "%IF_input %IF_output";
1102         fragments["IF_variable"]                =
1103                 " %IF_input = OpVariable %ip_${input_type} Input\n"
1104                 "%IF_output = OpVariable %op_${output_type} Output\n";
1105         fragments["IF_decoration"]              =
1106                 "OpDecorate  %IF_input Location 2\n"
1107                 "OpDecorate %IF_output Location 2\n";
1108         fragments["IF_carryforward"]    =
1109                 "%IF_input_val = OpLoad %${input_type} %IF_input\n"
1110                 "   %IF_result = OpFunctionCall %${output_type} %interface_op_func %IF_input_val\n"
1111                 "                OpStore %IF_output %IF_result\n";
1112
1113         // Make sure the rest still need to be instantialized.
1114         fragments["capability"]                 = "${capability:opt}";
1115         fragments["extension"]                  = "${extension:opt}";
1116         fragments["debug"]                              = "${debug:opt}";
1117         fragments["decoration"]                 = "${decoration:opt}";
1118         fragments["pre_main"]                   = "${pre_main:opt}";
1119         fragments["testfun"]                    = "${testfun}";
1120         fragments["interface_op_func"]  = "${interface_op_func}";
1121
1122         return fragments;
1123 }
1124
1125 // Returns mappings from interface placeholders to their concrete values.
1126 //
1127 // The concrete values should be specialized again to provide ${input_type}
1128 // and ${output_type}.
1129 //
1130 // %ip_${input_type} and %op_${output_type} should also be defined in the final code.
1131 map<string, string> fillInterfacePlaceholderFrag (void)
1132 {
1133         map<string, string>     fragments       ;
1134
1135         fragments["IF_entrypoint"]              = "%IF_input %IF_output";
1136         fragments["IF_variable"]                =
1137                 " %IF_input = OpVariable %ip_${input_type} Input\n"
1138                 "%IF_output = OpVariable %op_${output_type} Output\n";
1139         fragments["IF_decoration"]              =
1140                 "OpDecorate %IF_input Flat\n"
1141                 "OpDecorate %IF_input Location 2\n"
1142                 "OpDecorate %IF_output Location 1\n";  // Fragment shader should write to location #1.
1143         fragments["IF_carryforward"]    =
1144                 "%IF_input_val = OpLoad %${input_type} %IF_input\n"
1145                 "   %IF_result = OpFunctionCall %${output_type} %interface_op_func %IF_input_val\n"
1146                 "                OpStore %IF_output %IF_result\n";
1147
1148         // Make sure the rest still need to be instantialized.
1149         fragments["capability"]                 = "${capability:opt}";
1150         fragments["extension"]                  = "${extension:opt}";
1151         fragments["debug"]                              = "${debug:opt}";
1152         fragments["decoration"]                 = "${decoration:opt}";
1153         fragments["pre_main"]                   = "${pre_main:opt}";
1154         fragments["testfun"]                    = "${testfun}";
1155         fragments["interface_op_func"]  = "${interface_op_func}";
1156
1157         return fragments;
1158 }
1159
1160 // Returns mappings from interface placeholders to their concrete values.
1161 //
1162 // The concrete values should be specialized again to provide ${input_type}
1163 // and ${output_type}.
1164 //
1165 // %ip_${input_type}, %op_${output_type}, %ip_a3${input_type}, and $op_a3${output_type}
1166 // should also be defined in the final code.
1167 map<string, string> fillInterfacePlaceholderTessCtrl (void)
1168 {
1169         map<string, string>     fragments       ;
1170
1171         fragments["IF_entrypoint"]              = "%IF_input %IF_output";
1172         fragments["IF_variable"]                =
1173                 " %IF_input = OpVariable %ip_a3${input_type} Input\n"
1174                 "%IF_output = OpVariable %op_a3${output_type} Output\n";
1175         fragments["IF_decoration"]              =
1176                 "OpDecorate  %IF_input Location 2\n"
1177                 "OpDecorate %IF_output Location 2\n";
1178         fragments["IF_carryforward"]    =
1179                 " %IF_input_ptr0 = OpAccessChain %ip_${input_type} %IF_input %c_i32_0\n"
1180                 " %IF_input_ptr1 = OpAccessChain %ip_${input_type} %IF_input %c_i32_1\n"
1181                 " %IF_input_ptr2 = OpAccessChain %ip_${input_type} %IF_input %c_i32_2\n"
1182                 "%IF_output_ptr0 = OpAccessChain %op_${output_type} %IF_output %c_i32_0\n"
1183                 "%IF_output_ptr1 = OpAccessChain %op_${output_type} %IF_output %c_i32_1\n"
1184                 "%IF_output_ptr2 = OpAccessChain %op_${output_type} %IF_output %c_i32_2\n"
1185                 "%IF_input_val0 = OpLoad %${input_type} %IF_input_ptr0\n"
1186                 "%IF_input_val1 = OpLoad %${input_type} %IF_input_ptr1\n"
1187                 "%IF_input_val2 = OpLoad %${input_type} %IF_input_ptr2\n"
1188                 "%IF_input_res0 = OpFunctionCall %${output_type} %interface_op_func %IF_input_val0\n"
1189                 "%IF_input_res1 = OpFunctionCall %${output_type} %interface_op_func %IF_input_val1\n"
1190                 "%IF_input_res2 = OpFunctionCall %${output_type} %interface_op_func %IF_input_val2\n"
1191                 "OpStore %IF_output_ptr0 %IF_input_res0\n"
1192                 "OpStore %IF_output_ptr1 %IF_input_res1\n"
1193                 "OpStore %IF_output_ptr2 %IF_input_res2\n";
1194
1195         // Make sure the rest still need to be instantialized.
1196         fragments["capability"]                 = "${capability:opt}";
1197         fragments["extension"]                  = "${extension:opt}";
1198         fragments["debug"]                              = "${debug:opt}";
1199         fragments["decoration"]                 = "${decoration:opt}";
1200         fragments["pre_main"]                   = "${pre_main:opt}";
1201         fragments["testfun"]                    = "${testfun}";
1202         fragments["interface_op_func"]  = "${interface_op_func}";
1203
1204         return fragments;
1205 }
1206
1207 // Returns mappings from interface placeholders to their concrete values.
1208 //
1209 // The concrete values should be specialized again to provide ${input_type}
1210 // and ${output_type}.
1211 //
1212 // %ip_${input_type}, %op_${output_type}, %ip_a3${input_type}, and $op_a3${output_type}
1213 // should also be defined in the final code.
1214 map<string, string> fillInterfacePlaceholderTessEvalGeom (void)
1215 {
1216         map<string, string>     fragments       ;
1217
1218         fragments["IF_entrypoint"]              = "%IF_input %IF_output";
1219         fragments["IF_variable"]                =
1220                 " %IF_input = OpVariable %ip_a3${input_type} Input\n"
1221                 "%IF_output = OpVariable %op_${output_type} Output\n";
1222         fragments["IF_decoration"]              =
1223                 "OpDecorate  %IF_input Location 2\n"
1224                 "OpDecorate %IF_output Location 2\n";
1225         fragments["IF_carryforward"]    =
1226                 // Only get the first value since all three values are the same anyway.
1227                 " %IF_input_ptr0 = OpAccessChain %ip_${input_type} %IF_input %c_i32_0\n"
1228                 " %IF_input_val0 = OpLoad %${input_type} %IF_input_ptr0\n"
1229                 " %IF_input_res0 = OpFunctionCall %${output_type} %interface_op_func %IF_input_val0\n"
1230                 "OpStore %IF_output %IF_input_res0\n";
1231
1232         // Make sure the rest still need to be instantialized.
1233         fragments["capability"]                 = "${capability:opt}";
1234         fragments["extension"]                  = "${extension:opt}";
1235         fragments["debug"]                              = "${debug:opt}";
1236         fragments["decoration"]                 = "${decoration:opt}";
1237         fragments["pre_main"]                   = "${pre_main:opt}";
1238         fragments["testfun"]                    = "${testfun}";
1239         fragments["interface_op_func"]  = "${interface_op_func}";
1240
1241         return fragments;
1242 }
1243
1244 map<string, string> passthruFragments(void)
1245 {
1246         map<string, string> fragments;
1247         fragments["testfun"] =
1248                 // A %test_code function that returns its argument unchanged.
1249                 "%test_code = OpFunction %v4f32 None %v4f32_function\n"
1250                 "%param1 = OpFunctionParameter %v4f32\n"
1251                 "%label_testfun = OpLabel\n"
1252                 "OpReturnValue %param1\n"
1253                 "OpFunctionEnd\n";
1254         return fragments;
1255 }
1256
1257 // Adds shader assembly text to dst.spirvAsmSources for all shader kinds.
1258 // Vertex shader gets custom code from context, the rest are pass-through.
1259 void addShaderCodeCustomVertex(vk::SourceCollections& dst, InstanceContext context)
1260 {
1261         if (!context.interfaces.empty())
1262         {
1263                 // Inject boilerplate code to wire up additional input/output variables between stages.
1264                 // Just copy the contents in input variable to output variable in all stages except
1265                 // the customized stage.
1266                 dst.spirvAsmSources.add("vert") << StringTemplate(makeVertexShaderAssembly(fillInterfacePlaceholderVert())).specialize(context.testCodeFragments);
1267                 dst.spirvAsmSources.add("frag") << StringTemplate(makeFragmentShaderAssembly(fillInterfacePlaceholderFrag())).specialize(passthruInterface(context.interfaces.getOutputType()));
1268         } else {
1269                 map<string, string> passthru = passthruFragments();
1270
1271                 dst.spirvAsmSources.add("vert") << makeVertexShaderAssembly(context.testCodeFragments);
1272                 dst.spirvAsmSources.add("frag") << makeFragmentShaderAssembly(passthru);
1273         }
1274 }
1275
1276 // Adds shader assembly text to dst.spirvAsmSources for all shader kinds.
1277 // Tessellation control shader gets custom code from context, the rest are
1278 // pass-through.
1279 void addShaderCodeCustomTessControl(vk::SourceCollections& dst, InstanceContext context)
1280 {
1281         if (!context.interfaces.empty())
1282         {
1283                 // Inject boilerplate code to wire up additional input/output variables between stages.
1284                 // Just copy the contents in input variable to output variable in all stages except
1285                 // the customized stage.
1286                 dst.spirvAsmSources.add("vert") << StringTemplate(makeVertexShaderAssembly(fillInterfacePlaceholderVert())).specialize(passthruInterface(context.interfaces.getInputType()));
1287                 dst.spirvAsmSources.add("tessc") << StringTemplate(makeTessControlShaderAssembly(fillInterfacePlaceholderTessCtrl())).specialize(context.testCodeFragments);
1288                 dst.spirvAsmSources.add("tesse") << StringTemplate(makeTessEvalShaderAssembly(fillInterfacePlaceholderTessEvalGeom())).specialize(passthruInterface(context.interfaces.getOutputType()));
1289                 dst.spirvAsmSources.add("frag") << StringTemplate(makeFragmentShaderAssembly(fillInterfacePlaceholderFrag())).specialize(passthruInterface(context.interfaces.getOutputType()));
1290         }
1291         else
1292         {
1293                 map<string, string> passthru = passthruFragments();
1294
1295                 dst.spirvAsmSources.add("vert") << makeVertexShaderAssembly(passthru);
1296                 dst.spirvAsmSources.add("tessc") << makeTessControlShaderAssembly(context.testCodeFragments);
1297                 dst.spirvAsmSources.add("tesse") << makeTessEvalShaderAssembly(passthru);
1298                 dst.spirvAsmSources.add("frag") << makeFragmentShaderAssembly(passthru);
1299         }
1300 }
1301
1302 // Adds shader assembly text to dst.spirvAsmSources for all shader kinds.
1303 // Tessellation evaluation shader gets custom code from context, the rest are
1304 // pass-through.
1305 void addShaderCodeCustomTessEval(vk::SourceCollections& dst, InstanceContext context)
1306 {
1307         if (!context.interfaces.empty())
1308         {
1309                 // Inject boilerplate code to wire up additional input/output variables between stages.
1310                 // Just copy the contents in input variable to output variable in all stages except
1311                 // the customized stage.
1312                 dst.spirvAsmSources.add("vert") << StringTemplate(makeVertexShaderAssembly(fillInterfacePlaceholderVert())).specialize(passthruInterface(context.interfaces.getInputType()));
1313                 dst.spirvAsmSources.add("tessc") << StringTemplate(makeTessControlShaderAssembly(fillInterfacePlaceholderTessCtrl())).specialize(passthruInterface(context.interfaces.getInputType()));
1314                 dst.spirvAsmSources.add("tesse") << StringTemplate(makeTessEvalShaderAssembly(fillInterfacePlaceholderTessEvalGeom())).specialize(context.testCodeFragments);
1315                 dst.spirvAsmSources.add("frag") << StringTemplate(makeFragmentShaderAssembly(fillInterfacePlaceholderFrag())).specialize(passthruInterface(context.interfaces.getOutputType()));
1316         }
1317         else
1318         {
1319                 map<string, string> passthru = passthruFragments();
1320                 dst.spirvAsmSources.add("vert") << makeVertexShaderAssembly(passthru);
1321                 dst.spirvAsmSources.add("tessc") << makeTessControlShaderAssembly(passthru);
1322                 dst.spirvAsmSources.add("tesse") << makeTessEvalShaderAssembly(context.testCodeFragments);
1323                 dst.spirvAsmSources.add("frag") << makeFragmentShaderAssembly(passthru);
1324         }
1325 }
1326
1327 // Adds shader assembly text to dst.spirvAsmSources for all shader kinds.
1328 // Geometry shader gets custom code from context, the rest are pass-through.
1329 void addShaderCodeCustomGeometry(vk::SourceCollections& dst, InstanceContext context)
1330 {
1331         if (!context.interfaces.empty())
1332         {
1333                 // Inject boilerplate code to wire up additional input/output variables between stages.
1334                 // Just copy the contents in input variable to output variable in all stages except
1335                 // the customized stage.
1336                 dst.spirvAsmSources.add("vert") << StringTemplate(makeVertexShaderAssembly(fillInterfacePlaceholderVert())).specialize(passthruInterface(context.interfaces.getInputType()));
1337                 dst.spirvAsmSources.add("geom") << StringTemplate(makeGeometryShaderAssembly(fillInterfacePlaceholderTessEvalGeom())).specialize(context.testCodeFragments);
1338                 dst.spirvAsmSources.add("frag") << StringTemplate(makeFragmentShaderAssembly(fillInterfacePlaceholderFrag())).specialize(passthruInterface(context.interfaces.getOutputType()));
1339         }
1340         else
1341         {
1342                 map<string, string> passthru = passthruFragments();
1343                 dst.spirvAsmSources.add("vert") << makeVertexShaderAssembly(passthru);
1344                 dst.spirvAsmSources.add("geom") << makeGeometryShaderAssembly(context.testCodeFragments);
1345                 dst.spirvAsmSources.add("frag") << makeFragmentShaderAssembly(passthru);
1346         }
1347 }
1348
1349 // Adds shader assembly text to dst.spirvAsmSources for all shader kinds.
1350 // Fragment shader gets custom code from context, the rest are pass-through.
1351 void addShaderCodeCustomFragment(vk::SourceCollections& dst, InstanceContext context)
1352 {
1353         if (!context.interfaces.empty())
1354         {
1355                 // Inject boilerplate code to wire up additional input/output variables between stages.
1356                 // Just copy the contents in input variable to output variable in all stages except
1357                 // the customized stage.
1358                 dst.spirvAsmSources.add("vert") << StringTemplate(makeVertexShaderAssembly(fillInterfacePlaceholderVert())).specialize(passthruInterface(context.interfaces.getInputType()));
1359                 dst.spirvAsmSources.add("frag") << StringTemplate(makeFragmentShaderAssembly(fillInterfacePlaceholderFrag())).specialize(context.testCodeFragments);
1360         }
1361         else
1362         {
1363                 map<string, string> passthru = passthruFragments();
1364                 dst.spirvAsmSources.add("vert") << makeVertexShaderAssembly(passthru);
1365                 dst.spirvAsmSources.add("frag") << makeFragmentShaderAssembly(context.testCodeFragments);
1366         }
1367 }
1368
1369 void createCombinedModule(vk::SourceCollections& dst, InstanceContext)
1370 {
1371         // \todo [2015-12-07 awoloszyn] Make tessellation / geometry conditional
1372         // \todo [2015-12-07 awoloszyn] Remove OpName and OpMemberName at some point
1373         dst.spirvAsmSources.add("module") <<
1374                 "OpCapability Shader\n"
1375                 "OpCapability ClipDistance\n"
1376                 "OpCapability CullDistance\n"
1377                 "OpCapability Geometry\n"
1378                 "OpCapability Tessellation\n"
1379                 "OpMemoryModel Logical GLSL450\n"
1380
1381                 "OpEntryPoint Vertex %vert_main \"main\" %vert_Position %vert_vtxColor %vert_color %vert_vtxPosition %vert_vertex_id %vert_instance_id\n"
1382                 "OpEntryPoint Geometry %geom_main \"main\" %geom_out_gl_position %geom_gl_in %geom_out_color %geom_in_color\n"
1383                 "OpEntryPoint TessellationControl %tessc_main \"main\" %tessc_out_color %tessc_gl_InvocationID %tessc_in_color %tessc_out_position %tessc_in_position %tessc_gl_TessLevelOuter %tessc_gl_TessLevelInner\n"
1384                 "OpEntryPoint TessellationEvaluation %tesse_main \"main\" %tesse_stream %tesse_gl_tessCoord %tesse_in_position %tesse_out_color %tesse_in_color \n"
1385                 "OpEntryPoint Fragment %frag_main \"main\" %frag_vtxColor %frag_fragColor\n"
1386
1387                 "OpExecutionMode %geom_main Triangles\n"
1388                 "OpExecutionMode %geom_main OutputTriangleStrip\n"
1389                 "OpExecutionMode %geom_main OutputVertices 3\n"
1390
1391                 "OpExecutionMode %tessc_main OutputVertices 3\n"
1392
1393                 "OpExecutionMode %tesse_main Triangles\n"
1394                 "OpExecutionMode %tesse_main SpacingEqual\n"
1395                 "OpExecutionMode %tesse_main VertexOrderCcw\n"
1396
1397                 "OpExecutionMode %frag_main OriginUpperLeft\n"
1398
1399                 "OpName %vert_main \"main\"\n"
1400                 "OpName %vert_vtxPosition \"vtxPosition\"\n"
1401                 "OpName %vert_Position \"position\"\n"
1402                 "OpName %vert_vtxColor \"vtxColor\"\n"
1403                 "OpName %vert_color \"color\"\n"
1404                 "OpName %vert_vertex_id \"gl_VertexIndex\"\n"
1405                 "OpName %vert_instance_id \"gl_InstanceIndex\"\n"
1406                 "OpName %geom_main \"main\"\n"
1407                 "OpName %geom_per_vertex_in \"gl_PerVertex\"\n"
1408                 "OpMemberName %geom_per_vertex_in 0 \"gl_Position\"\n"
1409                 "OpMemberName %geom_per_vertex_in 1 \"gl_PointSize\"\n"
1410                 "OpMemberName %geom_per_vertex_in 2 \"gl_ClipDistance\"\n"
1411                 "OpMemberName %geom_per_vertex_in 3 \"gl_CullDistance\"\n"
1412                 "OpName %geom_gl_in \"gl_in\"\n"
1413                 "OpName %geom_out_color \"out_color\"\n"
1414                 "OpName %geom_in_color \"in_color\"\n"
1415                 "OpName %tessc_main \"main\"\n"
1416                 "OpName %tessc_out_color \"out_color\"\n"
1417                 "OpName %tessc_gl_InvocationID \"gl_InvocationID\"\n"
1418                 "OpName %tessc_in_color \"in_color\"\n"
1419                 "OpName %tessc_out_position \"out_position\"\n"
1420                 "OpName %tessc_in_position \"in_position\"\n"
1421                 "OpName %tessc_gl_TessLevelOuter \"gl_TessLevelOuter\"\n"
1422                 "OpName %tessc_gl_TessLevelInner \"gl_TessLevelInner\"\n"
1423                 "OpName %tesse_main \"main\"\n"
1424                 "OpName %tesse_per_vertex_out \"gl_PerVertex\"\n"
1425                 "OpMemberName %tesse_per_vertex_out 0 \"gl_Position\"\n"
1426                 "OpMemberName %tesse_per_vertex_out 1 \"gl_PointSize\"\n"
1427                 "OpMemberName %tesse_per_vertex_out 2 \"gl_ClipDistance\"\n"
1428                 "OpMemberName %tesse_per_vertex_out 3 \"gl_CullDistance\"\n"
1429                 "OpName %tesse_stream \"\"\n"
1430                 "OpName %tesse_gl_tessCoord \"gl_TessCoord\"\n"
1431                 "OpName %tesse_in_position \"in_position\"\n"
1432                 "OpName %tesse_out_color \"out_color\"\n"
1433                 "OpName %tesse_in_color \"in_color\"\n"
1434                 "OpName %frag_main \"main\"\n"
1435                 "OpName %frag_fragColor \"fragColor\"\n"
1436                 "OpName %frag_vtxColor \"vtxColor\"\n"
1437
1438                 "; Vertex decorations\n"
1439                 "OpDecorate %vert_vtxPosition Location 2\n"
1440                 "OpDecorate %vert_Position Location 0\n"
1441                 "OpDecorate %vert_vtxColor Location 1\n"
1442                 "OpDecorate %vert_color Location 1\n"
1443                 "OpDecorate %vert_vertex_id BuiltIn VertexIndex\n"
1444                 "OpDecorate %vert_instance_id BuiltIn InstanceIndex\n"
1445
1446                 "; Geometry decorations\n"
1447                 "OpDecorate %geom_out_gl_position BuiltIn Position\n"
1448                 "OpMemberDecorate %geom_per_vertex_in 0 BuiltIn Position\n"
1449                 "OpMemberDecorate %geom_per_vertex_in 1 BuiltIn PointSize\n"
1450                 "OpMemberDecorate %geom_per_vertex_in 2 BuiltIn ClipDistance\n"
1451                 "OpMemberDecorate %geom_per_vertex_in 3 BuiltIn CullDistance\n"
1452                 "OpDecorate %geom_per_vertex_in Block\n"
1453                 "OpDecorate %geom_out_color Location 1\n"
1454                 "OpDecorate %geom_in_color Location 1\n"
1455
1456                 "; Tessellation Control decorations\n"
1457                 "OpDecorate %tessc_out_color Location 1\n"
1458                 "OpDecorate %tessc_gl_InvocationID BuiltIn InvocationId\n"
1459                 "OpDecorate %tessc_in_color Location 1\n"
1460                 "OpDecorate %tessc_out_position Location 2\n"
1461                 "OpDecorate %tessc_in_position Location 2\n"
1462                 "OpDecorate %tessc_gl_TessLevelOuter Patch\n"
1463                 "OpDecorate %tessc_gl_TessLevelOuter BuiltIn TessLevelOuter\n"
1464                 "OpDecorate %tessc_gl_TessLevelInner Patch\n"
1465                 "OpDecorate %tessc_gl_TessLevelInner BuiltIn TessLevelInner\n"
1466
1467                 "; Tessellation Evaluation decorations\n"
1468                 "OpMemberDecorate %tesse_per_vertex_out 0 BuiltIn Position\n"
1469                 "OpMemberDecorate %tesse_per_vertex_out 1 BuiltIn PointSize\n"
1470                 "OpMemberDecorate %tesse_per_vertex_out 2 BuiltIn ClipDistance\n"
1471                 "OpMemberDecorate %tesse_per_vertex_out 3 BuiltIn CullDistance\n"
1472                 "OpDecorate %tesse_per_vertex_out Block\n"
1473                 "OpDecorate %tesse_gl_tessCoord BuiltIn TessCoord\n"
1474                 "OpDecorate %tesse_in_position Location 2\n"
1475                 "OpDecorate %tesse_out_color Location 1\n"
1476                 "OpDecorate %tesse_in_color Location 1\n"
1477
1478                 "; Fragment decorations\n"
1479                 "OpDecorate %frag_fragColor Location 0\n"
1480                 "OpDecorate %frag_vtxColor Location 1\n"
1481
1482                 SPIRV_ASSEMBLY_TYPES
1483                 SPIRV_ASSEMBLY_CONSTANTS
1484                 SPIRV_ASSEMBLY_ARRAYS
1485
1486                 "; Vertex Variables\n"
1487                 "%vert_vtxPosition = OpVariable %op_v4f32 Output\n"
1488                 "%vert_Position = OpVariable %ip_v4f32 Input\n"
1489                 "%vert_vtxColor = OpVariable %op_v4f32 Output\n"
1490                 "%vert_color = OpVariable %ip_v4f32 Input\n"
1491                 "%vert_vertex_id = OpVariable %ip_i32 Input\n"
1492                 "%vert_instance_id = OpVariable %ip_i32 Input\n"
1493
1494                 "; Geometry Variables\n"
1495                 "%geom_per_vertex_in = OpTypeStruct %v4f32 %f32 %a1f32 %a1f32\n"
1496                 "%geom_a3_per_vertex_in = OpTypeArray %geom_per_vertex_in %c_u32_3\n"
1497                 "%geom_ip_a3_per_vertex_in = OpTypePointer Input %geom_a3_per_vertex_in\n"
1498                 "%geom_gl_in = OpVariable %geom_ip_a3_per_vertex_in Input\n"
1499                 "%geom_out_color = OpVariable %op_v4f32 Output\n"
1500                 "%geom_in_color = OpVariable %ip_a3v4f32 Input\n"
1501                 "%geom_out_gl_position = OpVariable %op_v4f32 Output\n"
1502
1503                 "; Tessellation Control Variables\n"
1504                 "%tessc_out_color = OpVariable %op_a3v4f32 Output\n"
1505                 "%tessc_gl_InvocationID = OpVariable %ip_i32 Input\n"
1506                 "%tessc_in_color = OpVariable %ip_a32v4f32 Input\n"
1507                 "%tessc_out_position = OpVariable %op_a3v4f32 Output\n"
1508                 "%tessc_in_position = OpVariable %ip_a32v4f32 Input\n"
1509                 "%tessc_gl_TessLevelOuter = OpVariable %op_a4f32 Output\n"
1510                 "%tessc_gl_TessLevelInner = OpVariable %op_a2f32 Output\n"
1511
1512                 "; Tessellation Evaluation Decorations\n"
1513                 "%tesse_per_vertex_out = OpTypeStruct %v4f32 %f32 %a1f32 %a1f32\n"
1514                 "%tesse_op_per_vertex_out = OpTypePointer Output %tesse_per_vertex_out\n"
1515                 "%tesse_stream = OpVariable %tesse_op_per_vertex_out Output\n"
1516                 "%tesse_gl_tessCoord = OpVariable %ip_v3f32 Input\n"
1517                 "%tesse_in_position = OpVariable %ip_a32v4f32 Input\n"
1518                 "%tesse_out_color = OpVariable %op_v4f32 Output\n"
1519                 "%tesse_in_color = OpVariable %ip_a32v4f32 Input\n"
1520
1521                 "; Fragment Variables\n"
1522                 "%frag_fragColor = OpVariable %op_v4f32 Output\n"
1523                 "%frag_vtxColor = OpVariable %ip_v4f32 Input\n"
1524
1525                 "; Vertex Entry\n"
1526                 "%vert_main = OpFunction %void None %fun\n"
1527                 "%vert_label = OpLabel\n"
1528                 "%vert_tmp_position = OpLoad %v4f32 %vert_Position\n"
1529                 "OpStore %vert_vtxPosition %vert_tmp_position\n"
1530                 "%vert_tmp_color = OpLoad %v4f32 %vert_color\n"
1531                 "OpStore %vert_vtxColor %vert_tmp_color\n"
1532                 "OpReturn\n"
1533                 "OpFunctionEnd\n"
1534
1535                 "; Geometry Entry\n"
1536                 "%geom_main = OpFunction %void None %fun\n"
1537                 "%geom_label = OpLabel\n"
1538                 "%geom_gl_in_0_gl_position = OpAccessChain %ip_v4f32 %geom_gl_in %c_i32_0 %c_i32_0\n"
1539                 "%geom_gl_in_1_gl_position = OpAccessChain %ip_v4f32 %geom_gl_in %c_i32_1 %c_i32_0\n"
1540                 "%geom_gl_in_2_gl_position = OpAccessChain %ip_v4f32 %geom_gl_in %c_i32_2 %c_i32_0\n"
1541                 "%geom_in_position_0 = OpLoad %v4f32 %geom_gl_in_0_gl_position\n"
1542                 "%geom_in_position_1 = OpLoad %v4f32 %geom_gl_in_1_gl_position\n"
1543                 "%geom_in_position_2 = OpLoad %v4f32 %geom_gl_in_2_gl_position \n"
1544                 "%geom_in_color_0_ptr = OpAccessChain %ip_v4f32 %geom_in_color %c_i32_0\n"
1545                 "%geom_in_color_1_ptr = OpAccessChain %ip_v4f32 %geom_in_color %c_i32_1\n"
1546                 "%geom_in_color_2_ptr = OpAccessChain %ip_v4f32 %geom_in_color %c_i32_2\n"
1547                 "%geom_in_color_0 = OpLoad %v4f32 %geom_in_color_0_ptr\n"
1548                 "%geom_in_color_1 = OpLoad %v4f32 %geom_in_color_1_ptr\n"
1549                 "%geom_in_color_2 = OpLoad %v4f32 %geom_in_color_2_ptr\n"
1550                 "OpStore %geom_out_gl_position %geom_in_position_0\n"
1551                 "OpStore %geom_out_color %geom_in_color_0\n"
1552                 "OpEmitVertex\n"
1553                 "OpStore %geom_out_gl_position %geom_in_position_1\n"
1554                 "OpStore %geom_out_color %geom_in_color_1\n"
1555                 "OpEmitVertex\n"
1556                 "OpStore %geom_out_gl_position %geom_in_position_2\n"
1557                 "OpStore %geom_out_color %geom_in_color_2\n"
1558                 "OpEmitVertex\n"
1559                 "OpEndPrimitive\n"
1560                 "OpReturn\n"
1561                 "OpFunctionEnd\n"
1562
1563                 "; Tessellation Control Entry\n"
1564                 "%tessc_main = OpFunction %void None %fun\n"
1565                 "%tessc_label = OpLabel\n"
1566                 "%tessc_invocation_id = OpLoad %i32 %tessc_gl_InvocationID\n"
1567                 "%tessc_in_color_ptr = OpAccessChain %ip_v4f32 %tessc_in_color %tessc_invocation_id\n"
1568                 "%tessc_in_position_ptr = OpAccessChain %ip_v4f32 %tessc_in_position %tessc_invocation_id\n"
1569                 "%tessc_in_color_val = OpLoad %v4f32 %tessc_in_color_ptr\n"
1570                 "%tessc_in_position_val = OpLoad %v4f32 %tessc_in_position_ptr\n"
1571                 "%tessc_out_color_ptr = OpAccessChain %op_v4f32 %tessc_out_color %tessc_invocation_id\n"
1572                 "%tessc_out_position_ptr = OpAccessChain %op_v4f32 %tessc_out_position %tessc_invocation_id\n"
1573                 "OpStore %tessc_out_color_ptr %tessc_in_color_val\n"
1574                 "OpStore %tessc_out_position_ptr %tessc_in_position_val\n"
1575                 "%tessc_is_first_invocation = OpIEqual %bool %tessc_invocation_id %c_i32_0\n"
1576                 "OpSelectionMerge %tessc_merge_label None\n"
1577                 "OpBranchConditional %tessc_is_first_invocation %tessc_first_invocation %tessc_merge_label\n"
1578                 "%tessc_first_invocation = OpLabel\n"
1579                 "%tessc_tess_outer_0 = OpAccessChain %op_f32 %tessc_gl_TessLevelOuter %c_i32_0\n"
1580                 "%tessc_tess_outer_1 = OpAccessChain %op_f32 %tessc_gl_TessLevelOuter %c_i32_1\n"
1581                 "%tessc_tess_outer_2 = OpAccessChain %op_f32 %tessc_gl_TessLevelOuter %c_i32_2\n"
1582                 "%tessc_tess_inner = OpAccessChain %op_f32 %tessc_gl_TessLevelInner %c_i32_0\n"
1583                 "OpStore %tessc_tess_outer_0 %c_f32_1\n"
1584                 "OpStore %tessc_tess_outer_1 %c_f32_1\n"
1585                 "OpStore %tessc_tess_outer_2 %c_f32_1\n"
1586                 "OpStore %tessc_tess_inner %c_f32_1\n"
1587                 "OpBranch %tessc_merge_label\n"
1588                 "%tessc_merge_label = OpLabel\n"
1589                 "OpReturn\n"
1590                 "OpFunctionEnd\n"
1591
1592                 "; Tessellation Evaluation Entry\n"
1593                 "%tesse_main = OpFunction %void None %fun\n"
1594                 "%tesse_label = OpLabel\n"
1595                 "%tesse_tc_0_ptr = OpAccessChain %ip_f32 %tesse_gl_tessCoord %c_u32_0\n"
1596                 "%tesse_tc_1_ptr = OpAccessChain %ip_f32 %tesse_gl_tessCoord %c_u32_1\n"
1597                 "%tesse_tc_2_ptr = OpAccessChain %ip_f32 %tesse_gl_tessCoord %c_u32_2\n"
1598                 "%tesse_tc_0 = OpLoad %f32 %tesse_tc_0_ptr\n"
1599                 "%tesse_tc_1 = OpLoad %f32 %tesse_tc_1_ptr\n"
1600                 "%tesse_tc_2 = OpLoad %f32 %tesse_tc_2_ptr\n"
1601                 "%tesse_in_pos_0_ptr = OpAccessChain %ip_v4f32 %tesse_in_position %c_i32_0\n"
1602                 "%tesse_in_pos_1_ptr = OpAccessChain %ip_v4f32 %tesse_in_position %c_i32_1\n"
1603                 "%tesse_in_pos_2_ptr = OpAccessChain %ip_v4f32 %tesse_in_position %c_i32_2\n"
1604                 "%tesse_in_pos_0 = OpLoad %v4f32 %tesse_in_pos_0_ptr\n"
1605                 "%tesse_in_pos_1 = OpLoad %v4f32 %tesse_in_pos_1_ptr\n"
1606                 "%tesse_in_pos_2 = OpLoad %v4f32 %tesse_in_pos_2_ptr\n"
1607                 "%tesse_in_pos_0_weighted = OpVectorTimesScalar %v4f32 %tesse_in_pos_0 %tesse_tc_0\n"
1608                 "%tesse_in_pos_1_weighted = OpVectorTimesScalar %v4f32 %tesse_in_pos_1 %tesse_tc_1\n"
1609                 "%tesse_in_pos_2_weighted = OpVectorTimesScalar %v4f32 %tesse_in_pos_2 %tesse_tc_2\n"
1610                 "%tesse_out_pos_ptr = OpAccessChain %op_v4f32 %tesse_stream %c_i32_0\n"
1611                 "%tesse_in_pos_0_plus_pos_1 = OpFAdd %v4f32 %tesse_in_pos_0_weighted %tesse_in_pos_1_weighted\n"
1612                 "%tesse_computed_out = OpFAdd %v4f32 %tesse_in_pos_0_plus_pos_1 %tesse_in_pos_2_weighted\n"
1613                 "OpStore %tesse_out_pos_ptr %tesse_computed_out\n"
1614                 "%tesse_in_clr_0_ptr = OpAccessChain %ip_v4f32 %tesse_in_color %c_i32_0\n"
1615                 "%tesse_in_clr_1_ptr = OpAccessChain %ip_v4f32 %tesse_in_color %c_i32_1\n"
1616                 "%tesse_in_clr_2_ptr = OpAccessChain %ip_v4f32 %tesse_in_color %c_i32_2\n"
1617                 "%tesse_in_clr_0 = OpLoad %v4f32 %tesse_in_clr_0_ptr\n"
1618                 "%tesse_in_clr_1 = OpLoad %v4f32 %tesse_in_clr_1_ptr\n"
1619                 "%tesse_in_clr_2 = OpLoad %v4f32 %tesse_in_clr_2_ptr\n"
1620                 "%tesse_in_clr_0_weighted = OpVectorTimesScalar %v4f32 %tesse_in_clr_0 %tesse_tc_0\n"
1621                 "%tesse_in_clr_1_weighted = OpVectorTimesScalar %v4f32 %tesse_in_clr_1 %tesse_tc_1\n"
1622                 "%tesse_in_clr_2_weighted = OpVectorTimesScalar %v4f32 %tesse_in_clr_2 %tesse_tc_2\n"
1623                 "%tesse_in_clr_0_plus_col_1 = OpFAdd %v4f32 %tesse_in_clr_0_weighted %tesse_in_clr_1_weighted\n"
1624                 "%tesse_computed_clr = OpFAdd %v4f32 %tesse_in_clr_0_plus_col_1 %tesse_in_clr_2_weighted\n"
1625                 "OpStore %tesse_out_color %tesse_computed_clr\n"
1626                 "OpReturn\n"
1627                 "OpFunctionEnd\n"
1628
1629                 "; Fragment Entry\n"
1630                 "%frag_main = OpFunction %void None %fun\n"
1631                 "%frag_label_main = OpLabel\n"
1632                 "%frag_tmp1 = OpLoad %v4f32 %frag_vtxColor\n"
1633                 "OpStore %frag_fragColor %frag_tmp1\n"
1634                 "OpReturn\n"
1635                 "OpFunctionEnd\n";
1636 }
1637
1638 void createMultipleEntries(vk::SourceCollections& dst, InstanceContext)
1639 {
1640         dst.spirvAsmSources.add("vert") <<
1641         // This module contains 2 vertex shaders. One that is a passthrough
1642         // and a second that inverts the color of the output (1.0 - color).
1643                 "OpCapability Shader\n"
1644                 "OpMemoryModel Logical GLSL450\n"
1645                 "OpEntryPoint Vertex %main \"vert1\" %Position %vtxColor %color %vtxPosition %vertex_id %instance_id\n"
1646                 "OpEntryPoint Vertex %main2 \"vert2\" %Position %vtxColor %color %vtxPosition %vertex_id %instance_id\n"
1647
1648                 "OpName %main \"vert1\"\n"
1649                 "OpName %main2 \"vert2\"\n"
1650                 "OpName %vtxPosition \"vtxPosition\"\n"
1651                 "OpName %Position \"position\"\n"
1652                 "OpName %vtxColor \"vtxColor\"\n"
1653                 "OpName %color \"color\"\n"
1654                 "OpName %vertex_id \"gl_VertexIndex\"\n"
1655                 "OpName %instance_id \"gl_InstanceIndex\"\n"
1656
1657                 "OpDecorate %vtxPosition Location 2\n"
1658                 "OpDecorate %Position Location 0\n"
1659                 "OpDecorate %vtxColor Location 1\n"
1660                 "OpDecorate %color Location 1\n"
1661                 "OpDecorate %vertex_id BuiltIn VertexIndex\n"
1662                 "OpDecorate %instance_id BuiltIn InstanceIndex\n"
1663                 SPIRV_ASSEMBLY_TYPES
1664                 SPIRV_ASSEMBLY_CONSTANTS
1665                 SPIRV_ASSEMBLY_ARRAYS
1666                 "%cval = OpConstantComposite %v4f32 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_0\n"
1667                 "%vtxPosition = OpVariable %op_v4f32 Output\n"
1668                 "%Position = OpVariable %ip_v4f32 Input\n"
1669                 "%vtxColor = OpVariable %op_v4f32 Output\n"
1670                 "%color = OpVariable %ip_v4f32 Input\n"
1671                 "%vertex_id = OpVariable %ip_i32 Input\n"
1672                 "%instance_id = OpVariable %ip_i32 Input\n"
1673
1674                 "%main = OpFunction %void None %fun\n"
1675                 "%label = OpLabel\n"
1676                 "%tmp_position = OpLoad %v4f32 %Position\n"
1677                 "OpStore %vtxPosition %tmp_position\n"
1678                 "%tmp_color = OpLoad %v4f32 %color\n"
1679                 "OpStore %vtxColor %tmp_color\n"
1680                 "OpReturn\n"
1681                 "OpFunctionEnd\n"
1682
1683                 "%main2 = OpFunction %void None %fun\n"
1684                 "%label2 = OpLabel\n"
1685                 "%tmp_position2 = OpLoad %v4f32 %Position\n"
1686                 "OpStore %vtxPosition %tmp_position2\n"
1687                 "%tmp_color2 = OpLoad %v4f32 %color\n"
1688                 "%tmp_color3 = OpFSub %v4f32 %cval %tmp_color2\n"
1689                 "%tmp_color4 = OpVectorInsertDynamic %v4f32 %tmp_color3 %c_f32_1 %c_i32_3\n"
1690                 "OpStore %vtxColor %tmp_color4\n"
1691                 "OpReturn\n"
1692                 "OpFunctionEnd\n";
1693
1694         dst.spirvAsmSources.add("frag") <<
1695                 // This is a single module that contains 2 fragment shaders.
1696                 // One that passes color through and the other that inverts the output
1697                 // color (1.0 - color).
1698                 "OpCapability Shader\n"
1699                 "OpMemoryModel Logical GLSL450\n"
1700                 "OpEntryPoint Fragment %main \"frag1\" %vtxColor %fragColor\n"
1701                 "OpEntryPoint Fragment %main2 \"frag2\" %vtxColor %fragColor\n"
1702                 "OpExecutionMode %main OriginUpperLeft\n"
1703                 "OpExecutionMode %main2 OriginUpperLeft\n"
1704
1705                 "OpName %main \"frag1\"\n"
1706                 "OpName %main2 \"frag2\"\n"
1707                 "OpName %fragColor \"fragColor\"\n"
1708                 "OpName %vtxColor \"vtxColor\"\n"
1709                 "OpDecorate %fragColor Location 0\n"
1710                 "OpDecorate %vtxColor Location 1\n"
1711                 SPIRV_ASSEMBLY_TYPES
1712                 SPIRV_ASSEMBLY_CONSTANTS
1713                 SPIRV_ASSEMBLY_ARRAYS
1714                 "%cval = OpConstantComposite %v4f32 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_0\n"
1715                 "%fragColor = OpVariable %op_v4f32 Output\n"
1716                 "%vtxColor = OpVariable %ip_v4f32 Input\n"
1717
1718                 "%main = OpFunction %void None %fun\n"
1719                 "%label_main = OpLabel\n"
1720                 "%tmp1 = OpLoad %v4f32 %vtxColor\n"
1721                 "OpStore %fragColor %tmp1\n"
1722                 "OpReturn\n"
1723                 "OpFunctionEnd\n"
1724
1725                 "%main2 = OpFunction %void None %fun\n"
1726                 "%label_main2 = OpLabel\n"
1727                 "%tmp2 = OpLoad %v4f32 %vtxColor\n"
1728                 "%tmp3 = OpFSub %v4f32 %cval %tmp2\n"
1729                 "%tmp4 = OpVectorInsertDynamic %v4f32 %tmp3 %c_f32_1 %c_i32_3\n"
1730                 "OpStore %fragColor %tmp4\n"
1731                 "OpReturn\n"
1732                 "OpFunctionEnd\n";
1733
1734         dst.spirvAsmSources.add("geom") <<
1735                 "OpCapability Geometry\n"
1736                 "OpCapability ClipDistance\n"
1737                 "OpCapability CullDistance\n"
1738                 "OpMemoryModel Logical GLSL450\n"
1739                 "OpEntryPoint Geometry %geom1_main \"geom1\" %out_gl_position %gl_in %out_color %in_color\n"
1740                 "OpEntryPoint Geometry %geom2_main \"geom2\" %out_gl_position %gl_in %out_color %in_color\n"
1741                 "OpExecutionMode %geom1_main Triangles\n"
1742                 "OpExecutionMode %geom2_main Triangles\n"
1743                 "OpExecutionMode %geom1_main OutputTriangleStrip\n"
1744                 "OpExecutionMode %geom2_main OutputTriangleStrip\n"
1745                 "OpExecutionMode %geom1_main OutputVertices 3\n"
1746                 "OpExecutionMode %geom2_main OutputVertices 3\n"
1747                 "OpName %geom1_main \"geom1\"\n"
1748                 "OpName %geom2_main \"geom2\"\n"
1749                 "OpName %per_vertex_in \"gl_PerVertex\"\n"
1750                 "OpMemberName %per_vertex_in 0 \"gl_Position\"\n"
1751                 "OpMemberName %per_vertex_in 1 \"gl_PointSize\"\n"
1752                 "OpMemberName %per_vertex_in 2 \"gl_ClipDistance\"\n"
1753                 "OpMemberName %per_vertex_in 3 \"gl_CullDistance\"\n"
1754                 "OpName %gl_in \"gl_in\"\n"
1755                 "OpName %out_color \"out_color\"\n"
1756                 "OpName %in_color \"in_color\"\n"
1757                 "OpDecorate %out_gl_position BuiltIn Position\n"
1758                 "OpMemberDecorate %per_vertex_in 0 BuiltIn Position\n"
1759                 "OpMemberDecorate %per_vertex_in 1 BuiltIn PointSize\n"
1760                 "OpMemberDecorate %per_vertex_in 2 BuiltIn ClipDistance\n"
1761                 "OpMemberDecorate %per_vertex_in 3 BuiltIn CullDistance\n"
1762                 "OpDecorate %per_vertex_in Block\n"
1763                 "OpDecorate %out_color Location 1\n"
1764                 "OpDecorate %in_color Location 1\n"
1765                 SPIRV_ASSEMBLY_TYPES
1766                 SPIRV_ASSEMBLY_CONSTANTS
1767                 SPIRV_ASSEMBLY_ARRAYS
1768                 "%cval = OpConstantComposite %v4f32 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_0\n"
1769                 "%per_vertex_in = OpTypeStruct %v4f32 %f32 %a1f32 %a1f32\n"
1770                 "%a3_per_vertex_in = OpTypeArray %per_vertex_in %c_u32_3\n"
1771                 "%ip_a3_per_vertex_in = OpTypePointer Input %a3_per_vertex_in\n"
1772                 "%gl_in = OpVariable %ip_a3_per_vertex_in Input\n"
1773                 "%out_color = OpVariable %op_v4f32 Output\n"
1774                 "%in_color = OpVariable %ip_a3v4f32 Input\n"
1775                 "%out_gl_position = OpVariable %op_v4f32 Output\n"
1776
1777                 "%geom1_main = OpFunction %void None %fun\n"
1778                 "%geom1_label = OpLabel\n"
1779                 "%geom1_gl_in_0_gl_position = OpAccessChain %ip_v4f32 %gl_in %c_i32_0 %c_i32_0\n"
1780                 "%geom1_gl_in_1_gl_position = OpAccessChain %ip_v4f32 %gl_in %c_i32_1 %c_i32_0\n"
1781                 "%geom1_gl_in_2_gl_position = OpAccessChain %ip_v4f32 %gl_in %c_i32_2 %c_i32_0\n"
1782                 "%geom1_in_position_0 = OpLoad %v4f32 %geom1_gl_in_0_gl_position\n"
1783                 "%geom1_in_position_1 = OpLoad %v4f32 %geom1_gl_in_1_gl_position\n"
1784                 "%geom1_in_position_2 = OpLoad %v4f32 %geom1_gl_in_2_gl_position \n"
1785                 "%geom1_in_color_0_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_0\n"
1786                 "%geom1_in_color_1_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_1\n"
1787                 "%geom1_in_color_2_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_2\n"
1788                 "%geom1_in_color_0 = OpLoad %v4f32 %geom1_in_color_0_ptr\n"
1789                 "%geom1_in_color_1 = OpLoad %v4f32 %geom1_in_color_1_ptr\n"
1790                 "%geom1_in_color_2 = OpLoad %v4f32 %geom1_in_color_2_ptr\n"
1791                 "OpStore %out_gl_position %geom1_in_position_0\n"
1792                 "OpStore %out_color %geom1_in_color_0\n"
1793                 "OpEmitVertex\n"
1794                 "OpStore %out_gl_position %geom1_in_position_1\n"
1795                 "OpStore %out_color %geom1_in_color_1\n"
1796                 "OpEmitVertex\n"
1797                 "OpStore %out_gl_position %geom1_in_position_2\n"
1798                 "OpStore %out_color %geom1_in_color_2\n"
1799                 "OpEmitVertex\n"
1800                 "OpEndPrimitive\n"
1801                 "OpReturn\n"
1802                 "OpFunctionEnd\n"
1803
1804                 "%geom2_main = OpFunction %void None %fun\n"
1805                 "%geom2_label = OpLabel\n"
1806                 "%geom2_gl_in_0_gl_position = OpAccessChain %ip_v4f32 %gl_in %c_i32_0 %c_i32_0\n"
1807                 "%geom2_gl_in_1_gl_position = OpAccessChain %ip_v4f32 %gl_in %c_i32_1 %c_i32_0\n"
1808                 "%geom2_gl_in_2_gl_position = OpAccessChain %ip_v4f32 %gl_in %c_i32_2 %c_i32_0\n"
1809                 "%geom2_in_position_0 = OpLoad %v4f32 %geom2_gl_in_0_gl_position\n"
1810                 "%geom2_in_position_1 = OpLoad %v4f32 %geom2_gl_in_1_gl_position\n"
1811                 "%geom2_in_position_2 = OpLoad %v4f32 %geom2_gl_in_2_gl_position \n"
1812                 "%geom2_in_color_0_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_0\n"
1813                 "%geom2_in_color_1_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_1\n"
1814                 "%geom2_in_color_2_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_2\n"
1815                 "%geom2_in_color_0 = OpLoad %v4f32 %geom2_in_color_0_ptr\n"
1816                 "%geom2_in_color_1 = OpLoad %v4f32 %geom2_in_color_1_ptr\n"
1817                 "%geom2_in_color_2 = OpLoad %v4f32 %geom2_in_color_2_ptr\n"
1818                 "%geom2_transformed_in_color_0 = OpFSub %v4f32 %cval %geom2_in_color_0\n"
1819                 "%geom2_transformed_in_color_1 = OpFSub %v4f32 %cval %geom2_in_color_1\n"
1820                 "%geom2_transformed_in_color_2 = OpFSub %v4f32 %cval %geom2_in_color_2\n"
1821                 "%geom2_transformed_in_color_0_a = OpVectorInsertDynamic %v4f32 %geom2_transformed_in_color_0 %c_f32_1 %c_i32_3\n"
1822                 "%geom2_transformed_in_color_1_a = OpVectorInsertDynamic %v4f32 %geom2_transformed_in_color_1 %c_f32_1 %c_i32_3\n"
1823                 "%geom2_transformed_in_color_2_a = OpVectorInsertDynamic %v4f32 %geom2_transformed_in_color_2 %c_f32_1 %c_i32_3\n"
1824                 "OpStore %out_gl_position %geom2_in_position_0\n"
1825                 "OpStore %out_color %geom2_transformed_in_color_0_a\n"
1826                 "OpEmitVertex\n"
1827                 "OpStore %out_gl_position %geom2_in_position_1\n"
1828                 "OpStore %out_color %geom2_transformed_in_color_1_a\n"
1829                 "OpEmitVertex\n"
1830                 "OpStore %out_gl_position %geom2_in_position_2\n"
1831                 "OpStore %out_color %geom2_transformed_in_color_2_a\n"
1832                 "OpEmitVertex\n"
1833                 "OpEndPrimitive\n"
1834                 "OpReturn\n"
1835                 "OpFunctionEnd\n";
1836
1837         dst.spirvAsmSources.add("tessc") <<
1838                 "OpCapability Tessellation\n"
1839                 "OpMemoryModel Logical GLSL450\n"
1840                 "OpEntryPoint TessellationControl %tessc1_main \"tessc1\" %out_color %gl_InvocationID %in_color %out_position %in_position %gl_TessLevelOuter %gl_TessLevelInner\n"
1841                 "OpEntryPoint TessellationControl %tessc2_main \"tessc2\" %out_color %gl_InvocationID %in_color %out_position %in_position %gl_TessLevelOuter %gl_TessLevelInner\n"
1842                 "OpExecutionMode %tessc1_main OutputVertices 3\n"
1843                 "OpExecutionMode %tessc2_main OutputVertices 3\n"
1844                 "OpName %tessc1_main \"tessc1\"\n"
1845                 "OpName %tessc2_main \"tessc2\"\n"
1846                 "OpName %out_color \"out_color\"\n"
1847                 "OpName %gl_InvocationID \"gl_InvocationID\"\n"
1848                 "OpName %in_color \"in_color\"\n"
1849                 "OpName %out_position \"out_position\"\n"
1850                 "OpName %in_position \"in_position\"\n"
1851                 "OpName %gl_TessLevelOuter \"gl_TessLevelOuter\"\n"
1852                 "OpName %gl_TessLevelInner \"gl_TessLevelInner\"\n"
1853                 "OpDecorate %out_color Location 1\n"
1854                 "OpDecorate %gl_InvocationID BuiltIn InvocationId\n"
1855                 "OpDecorate %in_color Location 1\n"
1856                 "OpDecorate %out_position Location 2\n"
1857                 "OpDecorate %in_position Location 2\n"
1858                 "OpDecorate %gl_TessLevelOuter Patch\n"
1859                 "OpDecorate %gl_TessLevelOuter BuiltIn TessLevelOuter\n"
1860                 "OpDecorate %gl_TessLevelInner Patch\n"
1861                 "OpDecorate %gl_TessLevelInner BuiltIn TessLevelInner\n"
1862                 SPIRV_ASSEMBLY_TYPES
1863                 SPIRV_ASSEMBLY_CONSTANTS
1864                 SPIRV_ASSEMBLY_ARRAYS
1865                 "%cval = OpConstantComposite %v4f32 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_0\n"
1866                 "%out_color = OpVariable %op_a3v4f32 Output\n"
1867                 "%gl_InvocationID = OpVariable %ip_i32 Input\n"
1868                 "%in_color = OpVariable %ip_a32v4f32 Input\n"
1869                 "%out_position = OpVariable %op_a3v4f32 Output\n"
1870                 "%in_position = OpVariable %ip_a32v4f32 Input\n"
1871                 "%gl_TessLevelOuter = OpVariable %op_a4f32 Output\n"
1872                 "%gl_TessLevelInner = OpVariable %op_a2f32 Output\n"
1873
1874                 "%tessc1_main = OpFunction %void None %fun\n"
1875                 "%tessc1_label = OpLabel\n"
1876                 "%tessc1_invocation_id = OpLoad %i32 %gl_InvocationID\n"
1877                 "%tessc1_in_color_ptr = OpAccessChain %ip_v4f32 %in_color %tessc1_invocation_id\n"
1878                 "%tessc1_in_position_ptr = OpAccessChain %ip_v4f32 %in_position %tessc1_invocation_id\n"
1879                 "%tessc1_in_color_val = OpLoad %v4f32 %tessc1_in_color_ptr\n"
1880                 "%tessc1_in_position_val = OpLoad %v4f32 %tessc1_in_position_ptr\n"
1881                 "%tessc1_out_color_ptr = OpAccessChain %op_v4f32 %out_color %tessc1_invocation_id\n"
1882                 "%tessc1_out_position_ptr = OpAccessChain %op_v4f32 %out_position %tessc1_invocation_id\n"
1883                 "OpStore %tessc1_out_color_ptr %tessc1_in_color_val\n"
1884                 "OpStore %tessc1_out_position_ptr %tessc1_in_position_val\n"
1885                 "%tessc1_is_first_invocation = OpIEqual %bool %tessc1_invocation_id %c_i32_0\n"
1886                 "OpSelectionMerge %tessc1_merge_label None\n"
1887                 "OpBranchConditional %tessc1_is_first_invocation %tessc1_first_invocation %tessc1_merge_label\n"
1888                 "%tessc1_first_invocation = OpLabel\n"
1889                 "%tessc1_tess_outer_0 = OpAccessChain %op_f32 %gl_TessLevelOuter %c_i32_0\n"
1890                 "%tessc1_tess_outer_1 = OpAccessChain %op_f32 %gl_TessLevelOuter %c_i32_1\n"
1891                 "%tessc1_tess_outer_2 = OpAccessChain %op_f32 %gl_TessLevelOuter %c_i32_2\n"
1892                 "%tessc1_tess_inner = OpAccessChain %op_f32 %gl_TessLevelInner %c_i32_0\n"
1893                 "OpStore %tessc1_tess_outer_0 %c_f32_1\n"
1894                 "OpStore %tessc1_tess_outer_1 %c_f32_1\n"
1895                 "OpStore %tessc1_tess_outer_2 %c_f32_1\n"
1896                 "OpStore %tessc1_tess_inner %c_f32_1\n"
1897                 "OpBranch %tessc1_merge_label\n"
1898                 "%tessc1_merge_label = OpLabel\n"
1899                 "OpReturn\n"
1900                 "OpFunctionEnd\n"
1901
1902                 "%tessc2_main = OpFunction %void None %fun\n"
1903                 "%tessc2_label = OpLabel\n"
1904                 "%tessc2_invocation_id = OpLoad %i32 %gl_InvocationID\n"
1905                 "%tessc2_in_color_ptr = OpAccessChain %ip_v4f32 %in_color %tessc2_invocation_id\n"
1906                 "%tessc2_in_position_ptr = OpAccessChain %ip_v4f32 %in_position %tessc2_invocation_id\n"
1907                 "%tessc2_in_color_val = OpLoad %v4f32 %tessc2_in_color_ptr\n"
1908                 "%tessc2_in_position_val = OpLoad %v4f32 %tessc2_in_position_ptr\n"
1909                 "%tessc2_out_color_ptr = OpAccessChain %op_v4f32 %out_color %tessc2_invocation_id\n"
1910                 "%tessc2_out_position_ptr = OpAccessChain %op_v4f32 %out_position %tessc2_invocation_id\n"
1911                 "%tessc2_transformed_color = OpFSub %v4f32 %cval %tessc2_in_color_val\n"
1912                 "%tessc2_transformed_color_a = OpVectorInsertDynamic %v4f32 %tessc2_transformed_color %c_f32_1 %c_i32_3\n"
1913                 "OpStore %tessc2_out_color_ptr %tessc2_transformed_color_a\n"
1914                 "OpStore %tessc2_out_position_ptr %tessc2_in_position_val\n"
1915                 "%tessc2_is_first_invocation = OpIEqual %bool %tessc2_invocation_id %c_i32_0\n"
1916                 "OpSelectionMerge %tessc2_merge_label None\n"
1917                 "OpBranchConditional %tessc2_is_first_invocation %tessc2_first_invocation %tessc2_merge_label\n"
1918                 "%tessc2_first_invocation = OpLabel\n"
1919                 "%tessc2_tess_outer_0 = OpAccessChain %op_f32 %gl_TessLevelOuter %c_i32_0\n"
1920                 "%tessc2_tess_outer_1 = OpAccessChain %op_f32 %gl_TessLevelOuter %c_i32_1\n"
1921                 "%tessc2_tess_outer_2 = OpAccessChain %op_f32 %gl_TessLevelOuter %c_i32_2\n"
1922                 "%tessc2_tess_inner = OpAccessChain %op_f32 %gl_TessLevelInner %c_i32_0\n"
1923                 "OpStore %tessc2_tess_outer_0 %c_f32_1\n"
1924                 "OpStore %tessc2_tess_outer_1 %c_f32_1\n"
1925                 "OpStore %tessc2_tess_outer_2 %c_f32_1\n"
1926                 "OpStore %tessc2_tess_inner %c_f32_1\n"
1927                 "OpBranch %tessc2_merge_label\n"
1928                 "%tessc2_merge_label = OpLabel\n"
1929                 "OpReturn\n"
1930                 "OpFunctionEnd\n";
1931
1932         dst.spirvAsmSources.add("tesse") <<
1933                 "OpCapability Tessellation\n"
1934                 "OpCapability ClipDistance\n"
1935                 "OpCapability CullDistance\n"
1936                 "OpMemoryModel Logical GLSL450\n"
1937                 "OpEntryPoint TessellationEvaluation %tesse1_main \"tesse1\" %stream %gl_tessCoord %in_position %out_color %in_color \n"
1938                 "OpEntryPoint TessellationEvaluation %tesse2_main \"tesse2\" %stream %gl_tessCoord %in_position %out_color %in_color \n"
1939                 "OpExecutionMode %tesse1_main Triangles\n"
1940                 "OpExecutionMode %tesse1_main SpacingEqual\n"
1941                 "OpExecutionMode %tesse1_main VertexOrderCcw\n"
1942                 "OpExecutionMode %tesse2_main Triangles\n"
1943                 "OpExecutionMode %tesse2_main SpacingEqual\n"
1944                 "OpExecutionMode %tesse2_main VertexOrderCcw\n"
1945                 "OpName %tesse1_main \"tesse1\"\n"
1946                 "OpName %tesse2_main \"tesse2\"\n"
1947                 "OpName %per_vertex_out \"gl_PerVertex\"\n"
1948                 "OpMemberName %per_vertex_out 0 \"gl_Position\"\n"
1949                 "OpMemberName %per_vertex_out 1 \"gl_PointSize\"\n"
1950                 "OpMemberName %per_vertex_out 2 \"gl_ClipDistance\"\n"
1951                 "OpMemberName %per_vertex_out 3 \"gl_CullDistance\"\n"
1952                 "OpName %stream \"\"\n"
1953                 "OpName %gl_tessCoord \"gl_TessCoord\"\n"
1954                 "OpName %in_position \"in_position\"\n"
1955                 "OpName %out_color \"out_color\"\n"
1956                 "OpName %in_color \"in_color\"\n"
1957                 "OpMemberDecorate %per_vertex_out 0 BuiltIn Position\n"
1958                 "OpMemberDecorate %per_vertex_out 1 BuiltIn PointSize\n"
1959                 "OpMemberDecorate %per_vertex_out 2 BuiltIn ClipDistance\n"
1960                 "OpMemberDecorate %per_vertex_out 3 BuiltIn CullDistance\n"
1961                 "OpDecorate %per_vertex_out Block\n"
1962                 "OpDecorate %gl_tessCoord BuiltIn TessCoord\n"
1963                 "OpDecorate %in_position Location 2\n"
1964                 "OpDecorate %out_color Location 1\n"
1965                 "OpDecorate %in_color Location 1\n"
1966                 SPIRV_ASSEMBLY_TYPES
1967                 SPIRV_ASSEMBLY_CONSTANTS
1968                 SPIRV_ASSEMBLY_ARRAYS
1969                 "%cval = OpConstantComposite %v4f32 %c_f32_1 %c_f32_1 %c_f32_1 %c_f32_0\n"
1970                 "%per_vertex_out = OpTypeStruct %v4f32 %f32 %a1f32 %a1f32\n"
1971                 "%op_per_vertex_out = OpTypePointer Output %per_vertex_out\n"
1972                 "%stream = OpVariable %op_per_vertex_out Output\n"
1973                 "%gl_tessCoord = OpVariable %ip_v3f32 Input\n"
1974                 "%in_position = OpVariable %ip_a32v4f32 Input\n"
1975                 "%out_color = OpVariable %op_v4f32 Output\n"
1976                 "%in_color = OpVariable %ip_a32v4f32 Input\n"
1977
1978                 "%tesse1_main = OpFunction %void None %fun\n"
1979                 "%tesse1_label = OpLabel\n"
1980                 "%tesse1_tc_0_ptr = OpAccessChain %ip_f32 %gl_tessCoord %c_u32_0\n"
1981                 "%tesse1_tc_1_ptr = OpAccessChain %ip_f32 %gl_tessCoord %c_u32_1\n"
1982                 "%tesse1_tc_2_ptr = OpAccessChain %ip_f32 %gl_tessCoord %c_u32_2\n"
1983                 "%tesse1_tc_0 = OpLoad %f32 %tesse1_tc_0_ptr\n"
1984                 "%tesse1_tc_1 = OpLoad %f32 %tesse1_tc_1_ptr\n"
1985                 "%tesse1_tc_2 = OpLoad %f32 %tesse1_tc_2_ptr\n"
1986                 "%tesse1_in_pos_0_ptr = OpAccessChain %ip_v4f32 %in_position %c_i32_0\n"
1987                 "%tesse1_in_pos_1_ptr = OpAccessChain %ip_v4f32 %in_position %c_i32_1\n"
1988                 "%tesse1_in_pos_2_ptr = OpAccessChain %ip_v4f32 %in_position %c_i32_2\n"
1989                 "%tesse1_in_pos_0 = OpLoad %v4f32 %tesse1_in_pos_0_ptr\n"
1990                 "%tesse1_in_pos_1 = OpLoad %v4f32 %tesse1_in_pos_1_ptr\n"
1991                 "%tesse1_in_pos_2 = OpLoad %v4f32 %tesse1_in_pos_2_ptr\n"
1992                 "%tesse1_in_pos_0_weighted = OpVectorTimesScalar %v4f32 %tesse1_in_pos_0 %tesse1_tc_0\n"
1993                 "%tesse1_in_pos_1_weighted = OpVectorTimesScalar %v4f32 %tesse1_in_pos_1 %tesse1_tc_1\n"
1994                 "%tesse1_in_pos_2_weighted = OpVectorTimesScalar %v4f32 %tesse1_in_pos_2 %tesse1_tc_2\n"
1995                 "%tesse1_out_pos_ptr = OpAccessChain %op_v4f32 %stream %c_i32_0\n"
1996                 "%tesse1_in_pos_0_plus_pos_1 = OpFAdd %v4f32 %tesse1_in_pos_0_weighted %tesse1_in_pos_1_weighted\n"
1997                 "%tesse1_computed_out = OpFAdd %v4f32 %tesse1_in_pos_0_plus_pos_1 %tesse1_in_pos_2_weighted\n"
1998                 "OpStore %tesse1_out_pos_ptr %tesse1_computed_out\n"
1999                 "%tesse1_in_clr_0_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_0\n"
2000                 "%tesse1_in_clr_1_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_1\n"
2001                 "%tesse1_in_clr_2_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_2\n"
2002                 "%tesse1_in_clr_0 = OpLoad %v4f32 %tesse1_in_clr_0_ptr\n"
2003                 "%tesse1_in_clr_1 = OpLoad %v4f32 %tesse1_in_clr_1_ptr\n"
2004                 "%tesse1_in_clr_2 = OpLoad %v4f32 %tesse1_in_clr_2_ptr\n"
2005                 "%tesse1_in_clr_0_weighted = OpVectorTimesScalar %v4f32 %tesse1_in_clr_0 %tesse1_tc_0\n"
2006                 "%tesse1_in_clr_1_weighted = OpVectorTimesScalar %v4f32 %tesse1_in_clr_1 %tesse1_tc_1\n"
2007                 "%tesse1_in_clr_2_weighted = OpVectorTimesScalar %v4f32 %tesse1_in_clr_2 %tesse1_tc_2\n"
2008                 "%tesse1_in_clr_0_plus_col_1 = OpFAdd %v4f32 %tesse1_in_clr_0_weighted %tesse1_in_clr_1_weighted\n"
2009                 "%tesse1_computed_clr = OpFAdd %v4f32 %tesse1_in_clr_0_plus_col_1 %tesse1_in_clr_2_weighted\n"
2010                 "OpStore %out_color %tesse1_computed_clr\n"
2011                 "OpReturn\n"
2012                 "OpFunctionEnd\n"
2013
2014                 "%tesse2_main = OpFunction %void None %fun\n"
2015                 "%tesse2_label = OpLabel\n"
2016                 "%tesse2_tc_0_ptr = OpAccessChain %ip_f32 %gl_tessCoord %c_u32_0\n"
2017                 "%tesse2_tc_1_ptr = OpAccessChain %ip_f32 %gl_tessCoord %c_u32_1\n"
2018                 "%tesse2_tc_2_ptr = OpAccessChain %ip_f32 %gl_tessCoord %c_u32_2\n"
2019                 "%tesse2_tc_0 = OpLoad %f32 %tesse2_tc_0_ptr\n"
2020                 "%tesse2_tc_1 = OpLoad %f32 %tesse2_tc_1_ptr\n"
2021                 "%tesse2_tc_2 = OpLoad %f32 %tesse2_tc_2_ptr\n"
2022                 "%tesse2_in_pos_0_ptr = OpAccessChain %ip_v4f32 %in_position %c_i32_0\n"
2023                 "%tesse2_in_pos_1_ptr = OpAccessChain %ip_v4f32 %in_position %c_i32_1\n"
2024                 "%tesse2_in_pos_2_ptr = OpAccessChain %ip_v4f32 %in_position %c_i32_2\n"
2025                 "%tesse2_in_pos_0 = OpLoad %v4f32 %tesse2_in_pos_0_ptr\n"
2026                 "%tesse2_in_pos_1 = OpLoad %v4f32 %tesse2_in_pos_1_ptr\n"
2027                 "%tesse2_in_pos_2 = OpLoad %v4f32 %tesse2_in_pos_2_ptr\n"
2028                 "%tesse2_in_pos_0_weighted = OpVectorTimesScalar %v4f32 %tesse2_in_pos_0 %tesse2_tc_0\n"
2029                 "%tesse2_in_pos_1_weighted = OpVectorTimesScalar %v4f32 %tesse2_in_pos_1 %tesse2_tc_1\n"
2030                 "%tesse2_in_pos_2_weighted = OpVectorTimesScalar %v4f32 %tesse2_in_pos_2 %tesse2_tc_2\n"
2031                 "%tesse2_out_pos_ptr = OpAccessChain %op_v4f32 %stream %c_i32_0\n"
2032                 "%tesse2_in_pos_0_plus_pos_1 = OpFAdd %v4f32 %tesse2_in_pos_0_weighted %tesse2_in_pos_1_weighted\n"
2033                 "%tesse2_computed_out = OpFAdd %v4f32 %tesse2_in_pos_0_plus_pos_1 %tesse2_in_pos_2_weighted\n"
2034                 "OpStore %tesse2_out_pos_ptr %tesse2_computed_out\n"
2035                 "%tesse2_in_clr_0_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_0\n"
2036                 "%tesse2_in_clr_1_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_1\n"
2037                 "%tesse2_in_clr_2_ptr = OpAccessChain %ip_v4f32 %in_color %c_i32_2\n"
2038                 "%tesse2_in_clr_0 = OpLoad %v4f32 %tesse2_in_clr_0_ptr\n"
2039                 "%tesse2_in_clr_1 = OpLoad %v4f32 %tesse2_in_clr_1_ptr\n"
2040                 "%tesse2_in_clr_2 = OpLoad %v4f32 %tesse2_in_clr_2_ptr\n"
2041                 "%tesse2_in_clr_0_weighted = OpVectorTimesScalar %v4f32 %tesse2_in_clr_0 %tesse2_tc_0\n"
2042                 "%tesse2_in_clr_1_weighted = OpVectorTimesScalar %v4f32 %tesse2_in_clr_1 %tesse2_tc_1\n"
2043                 "%tesse2_in_clr_2_weighted = OpVectorTimesScalar %v4f32 %tesse2_in_clr_2 %tesse2_tc_2\n"
2044                 "%tesse2_in_clr_0_plus_col_1 = OpFAdd %v4f32 %tesse2_in_clr_0_weighted %tesse2_in_clr_1_weighted\n"
2045                 "%tesse2_computed_clr = OpFAdd %v4f32 %tesse2_in_clr_0_plus_col_1 %tesse2_in_clr_2_weighted\n"
2046                 "%tesse2_clr_transformed = OpFSub %v4f32 %cval %tesse2_computed_clr\n"
2047                 "%tesse2_clr_transformed_a = OpVectorInsertDynamic %v4f32 %tesse2_clr_transformed %c_f32_1 %c_i32_3\n"
2048                 "OpStore %out_color %tesse2_clr_transformed_a\n"
2049                 "OpReturn\n"
2050                 "OpFunctionEnd\n";
2051 }
2052
2053 bool compare16BitFloat (float original, deUint16 returned, RoundingModeFlags flags, tcu::TestLog& log)
2054 {
2055         // We only support RTE, RTZ, or both.
2056         DE_ASSERT(static_cast<int>(flags) > 0 && static_cast<int>(flags) < 4);
2057
2058         const Float32   originalFloat   (original);
2059         const Float16   returnedFloat   (returned);
2060
2061         // Zero are turned into zero under both RTE and RTZ.
2062         if (originalFloat.isZero())
2063         {
2064                 if (returnedFloat.isZero())
2065                         return true;
2066
2067                 log << TestLog::Message << "Error: expected zero but returned " << returned << TestLog::EndMessage;
2068                 return false;
2069         }
2070
2071         // Any denormalized value input into a shader may be flushed to 0.
2072         if (originalFloat.isDenorm() && returnedFloat.isZero())
2073                 return true;
2074
2075         // Inf are always turned into Inf with the same sign, too.
2076         if (originalFloat.isInf())
2077         {
2078                 if (returnedFloat.isInf() && originalFloat.signBit() == returnedFloat.signBit())
2079                         return true;
2080
2081                 log << TestLog::Message << "Error: expected Inf but returned " << returned << TestLog::EndMessage;
2082                 return false;
2083         }
2084
2085         // NaN are always turned into NaN, too.
2086         if (originalFloat.isNaN())
2087         {
2088                 if (returnedFloat.isNaN())
2089                         return true;
2090
2091                 log << TestLog::Message << "Error: expected NaN but returned " << returned << TestLog::EndMessage;
2092                 return false;
2093         }
2094
2095         // Check all rounding modes
2096         for (int bitNdx = 0; bitNdx < 2; ++bitNdx)
2097         {
2098                 if ((flags & (1u << bitNdx)) == 0)
2099                         continue;       // This rounding mode is not selected.
2100
2101                 const Float16   expectedFloat   (deFloat32To16Round(original, deRoundingMode(bitNdx)));
2102
2103                 // Any denormalized value potentially generated by any instruction in a shader may be flushed to 0.
2104                 if (expectedFloat.isDenorm() && returnedFloat.isZero())
2105                         return true;
2106
2107                 // If not matched in the above cases, they should have the same bit pattern.
2108                 if (expectedFloat.bits() == returnedFloat.bits())
2109                         return true;
2110         }
2111
2112         log << TestLog::Message << "Error: found unmatched 32-bit and 16-bit floats: " << originalFloat.bits() << " vs " << returned << TestLog::EndMessage;
2113         return false;
2114 }
2115
2116 bool compare32BitFloat (float expected, float returned, tcu::TestLog& log)
2117 {
2118         const Float32   expectedFloat   (expected);
2119         const Float32   returnedFloat   (returned);
2120
2121         // Any denormalized value potentially generated by any instruction in a shader may be flushed to 0.
2122         if (expectedFloat.isDenorm() && returnedFloat.isZero())
2123                 return true;
2124
2125         {
2126                 const Float16   originalFloat   (deFloat32To16(expected));
2127
2128                 // Any denormalized value input into a shader may be flushed to 0.
2129                 if (originalFloat.isDenorm() && returnedFloat.isZero())
2130                         return true;
2131         }
2132
2133         if (expectedFloat.isNaN())
2134         {
2135                 if (returnedFloat.isNaN())
2136                         return true;
2137
2138                 log << TestLog::Message << "Error: expected NaN but returned " << returned << TestLog::EndMessage;
2139                 return false;
2140         }
2141
2142         if (returned == expected)
2143                 return true;
2144
2145         log << TestLog::Message << "Error: found unmatched 32-bit float: expected " << expectedFloat.bits() << " vs. returned " << returnedFloat.bits() << TestLog::EndMessage;
2146         return false;
2147 }
2148
2149 Move<VkBuffer> createBufferForResource(const DeviceInterface& vk, const VkDevice vkDevice, const Resource& resource, deUint32 queueFamilyIndex)
2150 {
2151         const VkBufferCreateInfo        resourceBufferParams    =
2152         {
2153                 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,                                                           // sType
2154                 DE_NULL,                                                                                                                        // pNext
2155                 (VkBufferCreateFlags)0,                                                                                         // flags
2156                 (VkDeviceSize)resource.second->getNumBytes(),                                           // size
2157                 (VkBufferUsageFlags)getMatchingBufferUsageFlagBit(resource.first),      // usage
2158                 VK_SHARING_MODE_EXCLUSIVE,                                                                                      // sharingMode
2159                 1u,                                                                                                                                     // queueFamilyCount
2160                 &queueFamilyIndex,                                                                                                      // pQueueFamilyIndices
2161         };
2162
2163         return createBuffer(vk, vkDevice, &resourceBufferParams);
2164 }
2165
2166 TestStatus runAndVerifyDefaultPipeline (Context& context, InstanceContext instance)
2167 {
2168         const InstanceInterface&                                        vkInstance                              = context.getInstanceInterface();
2169         const VkPhysicalDevice                                          vkPhysicalDevice                = context.getPhysicalDevice();
2170         const deUint32                                                          queueFamilyIndex                = context.getUniversalQueueFamilyIndex();
2171         // Create a dedicated logic device with required extensions enabled for this test case.
2172         const tcu::UVec2                                                        renderSize                              (256, 256);
2173         const int                                                                       testSpecificSeed                = 31354125;
2174         const int                                                                       seed                                    = context.getTestContext().getCommandLine().getBaseSeed() ^ testSpecificSeed;
2175         bool                                                                            supportsGeometry                = false;
2176         bool                                                                            supportsTessellation    = false;
2177         bool                                                                            hasTessellation         = false;
2178         const bool                                                                      hasPushConstants                = !instance.pushConstants.empty();
2179         const deUint32                                                          numResources                    = static_cast<deUint32>(instance.resources.inputs.size() + instance.resources.outputs.size());
2180         const bool                                                                      needInterface                   = !instance.interfaces.empty();
2181         const VkPhysicalDeviceFeatures&                         features                                = context.getDeviceFeatures();
2182
2183
2184         supportsGeometry                = features.geometryShader == VK_TRUE;
2185         supportsTessellation    = features.tessellationShader == VK_TRUE;
2186         hasTessellation                 = (instance.requiredStages & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) ||
2187                                                                 (instance.requiredStages & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
2188
2189         if (hasTessellation && !supportsTessellation)
2190         {
2191                 throw tcu::NotSupportedError(std::string("Tessellation not supported"));
2192         }
2193
2194         if ((instance.requiredStages & VK_SHADER_STAGE_GEOMETRY_BIT) &&
2195                 !supportsGeometry)
2196         {
2197                 throw tcu::NotSupportedError(std::string("Geometry not supported"));
2198         }
2199
2200         {
2201                 for (deUint32 featureNdx = 0; featureNdx < instance.requiredDeviceFeatures.size(); ++featureNdx)
2202                 {
2203                         const string& feature = instance.requiredDeviceFeatures[featureNdx];
2204
2205                         if (feature == "shaderInt16")
2206                         {
2207                                 if (features.shaderInt16 != VK_TRUE)
2208                                         throw tcu::NotSupportedError(std::string("Device feature not supported: ") + feature);
2209                         }
2210                         else
2211                         {
2212                                 throw tcu::InternalError(std::string("Unimplemented physical device feature: ") + feature);
2213                         }
2214                 }
2215         }
2216
2217         // 16bit storage features
2218         {
2219                 if (!is16BitStorageFeaturesSupported(vkInstance, vkPhysicalDevice, context.getInstanceExtensions(), instance.requestedFeatures.ext16BitStorage))
2220                         TCU_THROW(NotSupportedError, "Requested 16bit storage features not supported");
2221         }
2222
2223         // Variable Pointers features
2224         {
2225                 if (!isVariablePointersFeaturesSupported(vkInstance, vkPhysicalDevice, context.getInstanceExtensions(), instance.requestedFeatures.extVariablePointers))
2226                         TCU_THROW(NotSupportedError, "Requested Variable Pointer features not supported");
2227
2228                 if (instance.requestedFeatures.extVariablePointers)
2229                 {
2230                         // The device doesn't have the vertexPipelineStoresAndAtomics feature, but the test requires the feature for
2231                         // vertex, tesselation, and geometry stages.
2232                         if (features.vertexPipelineStoresAndAtomics == DE_FALSE &&
2233                                 instance.requestedFeatures.coreFeatures.vertexPipelineStoresAndAtomics == DE_TRUE &&
2234                             (instance.customizedStages & vk::VK_SHADER_STAGE_VERTEX_BIT ||
2235                                  instance.customizedStages & vk::VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT ||
2236                                  instance.customizedStages & vk::VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
2237                                  instance.customizedStages & vk::VK_SHADER_STAGE_GEOMETRY_BIT))
2238                                 TCU_THROW(NotSupportedError, "This VK_KHR_variable_pointers extension test requires vertexPipelineStoresAndAtomics device feature.");
2239
2240                         // The device doesn't have the fragmentStoresAndAtomics feature, but the test requires this feature for the fragment stage.
2241                         if (features.fragmentStoresAndAtomics == DE_FALSE &&
2242                             instance.requestedFeatures.coreFeatures.fragmentStoresAndAtomics == DE_TRUE &&
2243                                 instance.customizedStages & vk::VK_SHADER_STAGE_FRAGMENT_BIT)
2244                                 TCU_THROW(NotSupportedError, "This VK_KHR_variable_pointers extension test requires fragmentStoresAndAtomics device feature.");
2245                 }
2246         }
2247
2248         // defer device and other resource creation until after feature checks
2249         const Unique<VkDevice>                                          vkDevice                                (createDeviceWithExtensions(context, queueFamilyIndex, context.getDeviceExtensions(), instance.requiredDeviceExtensions));
2250         const DeviceDriver                                                      vk                                              (vkInstance, *vkDevice);
2251         const VkQueue                                                           queue                                   = getDeviceQueue(vk, *vkDevice, queueFamilyIndex, 0);
2252         const de::UniquePtr<Allocator>                          allocatorUptr                   (createAllocator(vkInstance, vkPhysicalDevice, vk, *vkDevice));
2253         Allocator&                                                                      allocator                               = *allocatorUptr;
2254         vector<ModuleHandleSp>                                          modules;
2255         map<VkShaderStageFlagBits, VkShaderModule>      moduleByStage;
2256
2257
2258         de::Random(seed).shuffle(instance.inputColors, instance.inputColors+4);
2259         de::Random(seed).shuffle(instance.outputColors, instance.outputColors+4);
2260         const Vec4                                                              vertexData[]                    =
2261         {
2262                 // Upper left corner:
2263                 Vec4(-1.0f, -1.0f, 0.0f, 1.0f), instance.inputColors[0].toVec(),
2264                 Vec4(-0.5f, -1.0f, 0.0f, 1.0f), instance.inputColors[0].toVec(),
2265                 Vec4(-1.0f, -0.5f, 0.0f, 1.0f), instance.inputColors[0].toVec(),
2266
2267                 // Upper right corner:
2268                 Vec4(+0.5f, -1.0f, 0.0f, 1.0f), instance.inputColors[1].toVec(),
2269                 Vec4(+1.0f, -1.0f, 0.0f, 1.0f), instance.inputColors[1].toVec(),
2270                 Vec4(+1.0f, -0.5f, 0.0f, 1.0f), instance.inputColors[1].toVec(),
2271
2272                 // Lower left corner:
2273                 Vec4(-1.0f, +0.5f, 0.0f, 1.0f), instance.inputColors[2].toVec(),
2274                 Vec4(-0.5f, +1.0f, 0.0f, 1.0f), instance.inputColors[2].toVec(),
2275                 Vec4(-1.0f, +1.0f, 0.0f, 1.0f), instance.inputColors[2].toVec(),
2276
2277                 // Lower right corner:
2278                 Vec4(+1.0f, +0.5f, 0.0f, 1.0f), instance.inputColors[3].toVec(),
2279                 Vec4(+1.0f, +1.0f, 0.0f, 1.0f), instance.inputColors[3].toVec(),
2280                 Vec4(+0.5f, +1.0f, 0.0f, 1.0f), instance.inputColors[3].toVec()
2281         };
2282         const size_t                                                    singleVertexDataSize    = 2 * sizeof(Vec4);
2283         const size_t                                                    vertexCount                             = sizeof(vertexData) / singleVertexDataSize;
2284
2285         Move<VkBuffer>                                                  vertexInputBuffer               ;
2286         de::MovePtr<Allocation>                                 vertexInputMemory               ;
2287         Move<VkBuffer>                                                  fragOutputBuffer                ;
2288         de::MovePtr<Allocation>                                 fragOutputMemory                ;
2289         Move<VkImage>                                                   fragOutputImage                 ;
2290         de::MovePtr<Allocation>                                 fragOutputImageMemory   ;
2291         Move<VkImageView>                                               fragOutputImageView             ;
2292
2293         const VkBufferCreateInfo                                vertexBufferParams              =
2294         {
2295                 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,   //      VkStructureType         sType;
2296                 DE_NULL,                                                                //      const void*                     pNext;
2297                 0u,                                                                             //      VkBufferCreateFlags     flags;
2298                 (VkDeviceSize)sizeof(vertexData),               //      VkDeviceSize            size;
2299                 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,              //      VkBufferUsageFlags      usage;
2300                 VK_SHARING_MODE_EXCLUSIVE,                              //      VkSharingMode           sharingMode;
2301                 1u,                                                                             //      deUint32                        queueFamilyCount;
2302                 &queueFamilyIndex,                                              //      const deUint32*         pQueueFamilyIndices;
2303         };
2304         const Unique<VkBuffer>                                  vertexBuffer                    (createBuffer(vk, *vkDevice, &vertexBufferParams));
2305         const UniquePtr<Allocation>                             vertexBufferMemory              (allocator.allocate(getBufferMemoryRequirements(vk, *vkDevice, *vertexBuffer), MemoryRequirement::HostVisible));
2306
2307         VK_CHECK(vk.bindBufferMemory(*vkDevice, *vertexBuffer, vertexBufferMemory->getMemory(), vertexBufferMemory->getOffset()));
2308
2309         const VkDeviceSize                                              imageSizeBytes                  = (VkDeviceSize)(sizeof(deUint32)*renderSize.x()*renderSize.y());
2310         const VkBufferCreateInfo                                readImageBufferParams   =
2311         {
2312                 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,           //      VkStructureType         sType;
2313                 DE_NULL,                                                                        //      const void*                     pNext;
2314                 0u,                                                                                     //      VkBufferCreateFlags     flags;
2315                 imageSizeBytes,                                                         //      VkDeviceSize            size;
2316                 VK_BUFFER_USAGE_TRANSFER_DST_BIT,                       //      VkBufferUsageFlags      usage;
2317                 VK_SHARING_MODE_EXCLUSIVE,                                      //      VkSharingMode           sharingMode;
2318                 1u,                                                                                     //      deUint32                        queueFamilyCount;
2319                 &queueFamilyIndex,                                                      //      const deUint32*         pQueueFamilyIndices;
2320         };
2321         const Unique<VkBuffer>                                  readImageBuffer                 (createBuffer(vk, *vkDevice, &readImageBufferParams));
2322         const UniquePtr<Allocation>                             readImageBufferMemory   (allocator.allocate(getBufferMemoryRequirements(vk, *vkDevice, *readImageBuffer), MemoryRequirement::HostVisible));
2323
2324         VK_CHECK(vk.bindBufferMemory(*vkDevice, *readImageBuffer, readImageBufferMemory->getMemory(), readImageBufferMemory->getOffset()));
2325
2326         VkImageCreateInfo                                               imageParams                             =
2327         {
2328                 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                                                                    //      VkStructureType         sType;
2329                 DE_NULL,                                                                                                                                //      const void*                     pNext;
2330                 0u,                                                                                                                                             //      VkImageCreateFlags      flags;
2331                 VK_IMAGE_TYPE_2D,                                                                                                               //      VkImageType                     imageType;
2332                 VK_FORMAT_R8G8B8A8_UNORM,                                                                                               //      VkFormat                        format;
2333                 { renderSize.x(), renderSize.y(), 1 },                                                                  //      VkExtent3D                      extent;
2334                 1u,                                                                                                                                             //      deUint32                        mipLevels;
2335                 1u,                                                                                                                                             //      deUint32                        arraySize;
2336                 VK_SAMPLE_COUNT_1_BIT,                                                                                                  //      deUint32                        samples;
2337                 VK_IMAGE_TILING_OPTIMAL,                                                                                                //      VkImageTiling           tiling;
2338                 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT|VK_IMAGE_USAGE_TRANSFER_SRC_BIT,    //      VkImageUsageFlags       usage;
2339                 VK_SHARING_MODE_EXCLUSIVE,                                                                                              //      VkSharingMode           sharingMode;
2340                 1u,                                                                                                                                             //      deUint32                        queueFamilyCount;
2341                 &queueFamilyIndex,                                                                                                              //      const deUint32*         pQueueFamilyIndices;
2342                 VK_IMAGE_LAYOUT_UNDEFINED,                                                                                              //      VkImageLayout           initialLayout;
2343         };
2344
2345         const Unique<VkImage>                                   image                                   (createImage(vk, *vkDevice, &imageParams));
2346         const UniquePtr<Allocation>                             imageMemory                             (allocator.allocate(getImageMemoryRequirements(vk, *vkDevice, *image), MemoryRequirement::Any));
2347
2348         VK_CHECK(vk.bindImageMemory(*vkDevice, *image, imageMemory->getMemory(), imageMemory->getOffset()));
2349
2350         if (needInterface)
2351         {
2352                 // The pipeline renders four triangles, each with three vertexes.
2353                 // Test instantialization only provides four data points, each
2354                 // for one triangle. So we need allocate space of three times of
2355                 // input buffer's size.
2356                 const deUint32                                                  inputNumBytes                   = deUint32(instance.interfaces.getInputBuffer()->getNumBytes() * 3);
2357                 // Create an additional buffer and backing memory for one input variable.
2358                 const VkBufferCreateInfo                                vertexInputParams               =
2359                 {
2360                         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,           //      VkStructureType         sType;
2361                         DE_NULL,                                                                        //      const void*                     pNext;
2362                         0u,                                                                                     //      VkBufferCreateFlags     flags;
2363                         inputNumBytes,                                                          //      VkDeviceSize            size;
2364                         VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,                      //      VkBufferUsageFlags      usage;
2365                         VK_SHARING_MODE_EXCLUSIVE,                                      //      VkSharingMode           sharingMode;
2366                         1u,                                                                                     //      deUint32                        queueFamilyCount;
2367                         &queueFamilyIndex,                                                      //      const deUint32*         pQueueFamilyIndices;
2368                 };
2369
2370                 vertexInputBuffer = createBuffer(vk, *vkDevice, &vertexInputParams);
2371                 vertexInputMemory = allocator.allocate(getBufferMemoryRequirements(vk, *vkDevice, *vertexInputBuffer), MemoryRequirement::HostVisible);
2372                 VK_CHECK(vk.bindBufferMemory(*vkDevice, *vertexInputBuffer, vertexInputMemory->getMemory(), vertexInputMemory->getOffset()));
2373
2374                 // Create an additional buffer and backing memory for an output variable.
2375                 const VkDeviceSize                                              fragOutputImgSize               = (VkDeviceSize)(instance.interfaces.getOutputType().getNumBytes() * renderSize.x() * renderSize.y());
2376                 const VkBufferCreateInfo                                fragOutputParams                =
2377                 {
2378                         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,           //      VkStructureType         sType;
2379                         DE_NULL,                                                                        //      const void*                     pNext;
2380                         0u,                                                                                     //      VkBufferCreateFlags     flags;
2381                         fragOutputImgSize,                                                      //      VkDeviceSize            size;
2382                         VK_BUFFER_USAGE_TRANSFER_DST_BIT,                       //      VkBufferUsageFlags      usage;
2383                         VK_SHARING_MODE_EXCLUSIVE,                                      //      VkSharingMode           sharingMode;
2384                         1u,                                                                                     //      deUint32                        queueFamilyCount;
2385                         &queueFamilyIndex,                                                      //      const deUint32*         pQueueFamilyIndices;
2386                 };
2387                 fragOutputBuffer = createBuffer(vk, *vkDevice, &fragOutputParams);
2388                 fragOutputMemory = allocator.allocate(getBufferMemoryRequirements(vk, *vkDevice, *fragOutputBuffer), MemoryRequirement::HostVisible);
2389                 VK_CHECK(vk.bindBufferMemory(*vkDevice, *fragOutputBuffer, fragOutputMemory->getMemory(), fragOutputMemory->getOffset()));
2390
2391                 // Create an additional image and backing memory for attachment.
2392                 // Reuse the previous imageParams since we only need to change the image format.
2393                 imageParams.format              = instance.interfaces.getOutputType().getVkFormat();
2394
2395                 // Check the usage bits on the given image format are supported.
2396                 requireFormatUsageSupport(vkInstance, vkPhysicalDevice, imageParams.format, imageParams.tiling, imageParams.usage);
2397
2398                 fragOutputImage                 = createImage(vk, *vkDevice, &imageParams);
2399                 fragOutputImageMemory   = allocator.allocate(getImageMemoryRequirements(vk, *vkDevice, *fragOutputImage), MemoryRequirement::Any);
2400
2401                 VK_CHECK(vk.bindImageMemory(*vkDevice, *fragOutputImage, fragOutputImageMemory->getMemory(), fragOutputImageMemory->getOffset()));
2402         }
2403
2404         vector<VkAttachmentDescription>                 colorAttDescs                   ;
2405         vector<VkAttachmentReference>                   colorAttRefs                    ;
2406         {
2407                 const VkAttachmentDescription           attDesc                                 =
2408                 {
2409                         0u,                                                                                             //      VkAttachmentDescriptionFlags    flags;
2410                         VK_FORMAT_R8G8B8A8_UNORM,                                               //      VkFormat                                                format;
2411                         VK_SAMPLE_COUNT_1_BIT,                                                  //      deUint32                                                samples;
2412                         VK_ATTACHMENT_LOAD_OP_CLEAR,                                    //      VkAttachmentLoadOp                              loadOp;
2413                         VK_ATTACHMENT_STORE_OP_STORE,                                   //      VkAttachmentStoreOp                             storeOp;
2414                         VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                //      VkAttachmentLoadOp                              stencilLoadOp;
2415                         VK_ATTACHMENT_STORE_OP_DONT_CARE,                               //      VkAttachmentStoreOp                             stencilStoreOp;
2416                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,               //      VkImageLayout                                   initialLayout;
2417                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,               //      VkImageLayout                                   finalLayout;
2418                 };
2419                 colorAttDescs.push_back(attDesc);
2420
2421                 const VkAttachmentReference                     attRef                                  =
2422                 {
2423                         0u,                                                                                             //      deUint32                attachment;
2424                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,               //      VkImageLayout   layout;
2425                 };
2426                 colorAttRefs.push_back(attRef);
2427         }
2428
2429         if (needInterface)
2430         {
2431                 const VkAttachmentDescription           attDesc                                 =
2432                 {
2433                         0u,                                                                                                     //      VkAttachmentDescriptionFlags    flags;
2434                         instance.interfaces.getOutputType().getVkFormat(),      //      VkFormat                                                format;
2435                         VK_SAMPLE_COUNT_1_BIT,                                                          //      deUint32                                                samples;
2436                         VK_ATTACHMENT_LOAD_OP_CLEAR,                                            //      VkAttachmentLoadOp                              loadOp;
2437                         VK_ATTACHMENT_STORE_OP_STORE,                                           //      VkAttachmentStoreOp                             storeOp;
2438                         VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        //      VkAttachmentLoadOp                              stencilLoadOp;
2439                         VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       //      VkAttachmentStoreOp                             stencilStoreOp;
2440                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       //      VkImageLayout                                   initialLayout;
2441                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       //      VkImageLayout                                   finalLayout;
2442                 };
2443                 colorAttDescs.push_back(attDesc);
2444
2445                 const VkAttachmentReference                     attRef                                  =
2446                 {
2447                         1u,                                                                                             //      deUint32                attachment;
2448                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,               //      VkImageLayout   layout;
2449                 };
2450                 colorAttRefs.push_back(attRef);
2451         }
2452
2453         VkSubpassDescription                                    subpassDesc                             =
2454         {
2455                 0u,                                                                                             //      VkSubpassDescriptionFlags               flags;
2456                 VK_PIPELINE_BIND_POINT_GRAPHICS,                                //      VkPipelineBindPoint                             pipelineBindPoint;
2457                 0u,                                                                                             //      deUint32                                                inputCount;
2458                 DE_NULL,                                                                                //      const VkAttachmentReference*    pInputAttachments;
2459                 1u,                                                                                             //      deUint32                                                colorCount;
2460                 colorAttRefs.data(),                                                    //      const VkAttachmentReference*    pColorAttachments;
2461                 DE_NULL,                                                                                //      const VkAttachmentReference*    pResolveAttachments;
2462                 DE_NULL,                                                                                //      const VkAttachmentReference*    pDepthStencilAttachment;
2463                 0u,                                                                                             //      deUint32                                                preserveCount;
2464                 DE_NULL,                                                                                //      const VkAttachmentReference*    pPreserveAttachments;
2465
2466         };
2467         VkRenderPassCreateInfo                                  renderPassParams                =
2468         {
2469                 VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,              //      VkStructureType                                 sType;
2470                 DE_NULL,                                                                                //      const void*                                             pNext;
2471                 (VkRenderPassCreateFlags)0,
2472                 1u,                                                                                             //      deUint32                                                attachmentCount;
2473                 colorAttDescs.data(),                                                   //      const VkAttachmentDescription*  pAttachments;
2474                 1u,                                                                                             //      deUint32                                                subpassCount;
2475                 &subpassDesc,                                                                   //      const VkSubpassDescription*             pSubpasses;
2476                 0u,                                                                                             //      deUint32                                                dependencyCount;
2477                 DE_NULL,                                                                                //      const VkSubpassDependency*              pDependencies;
2478         };
2479
2480         if (needInterface)
2481         {
2482                 subpassDesc.colorAttachmentCount += 1;
2483                 renderPassParams.attachmentCount += 1;
2484         }
2485
2486         const Unique<VkRenderPass>                              renderPass                              (createRenderPass(vk, *vkDevice, &renderPassParams));
2487
2488         const VkImageViewCreateInfo                             colorAttViewParams              =
2489         {
2490                 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               //      VkStructureType                         sType;
2491                 DE_NULL,                                                                                //      const void*                                     pNext;
2492                 0u,                                                                                             //      VkImageViewCreateFlags          flags;
2493                 *image,                                                                                 //      VkImage                                         image;
2494                 VK_IMAGE_VIEW_TYPE_2D,                                                  //      VkImageViewType                         viewType;
2495                 VK_FORMAT_R8G8B8A8_UNORM,                                               //      VkFormat                                        format;
2496                 {
2497                         VK_COMPONENT_SWIZZLE_R,
2498                         VK_COMPONENT_SWIZZLE_G,
2499                         VK_COMPONENT_SWIZZLE_B,
2500                         VK_COMPONENT_SWIZZLE_A
2501                 },                                                                                              //      VkChannelMapping                        channels;
2502                 {
2503                         VK_IMAGE_ASPECT_COLOR_BIT,                                              //      VkImageAspectFlags      aspectMask;
2504                         0u,                                                                                             //      deUint32                        baseMipLevel;
2505                         1u,                                                                                             //      deUint32                        mipLevels;
2506                         0u,                                                                                             //      deUint32                        baseArrayLayer;
2507                         1u,                                                                                             //      deUint32                        arraySize;
2508                 },                                                                                              //      VkImageSubresourceRange         subresourceRange;
2509         };
2510         const Unique<VkImageView>                               colorAttView                    (createImageView(vk, *vkDevice, &colorAttViewParams));
2511
2512         vector<VkImageView>                                             attViews                                ;
2513         attViews.push_back(*colorAttView);
2514
2515         // Handle resources requested by the test instantiation.
2516         const deUint32                                                  numInResources                  = static_cast<deUint32>(instance.resources.inputs.size());
2517         const deUint32                                                  numOutResources                 = static_cast<deUint32>(instance.resources.outputs.size());
2518         // These variables should be placed out of the following if block to avoid deallocation after out of scope.
2519         vector<AllocationSp>                                    inResourceMemories              ;
2520         vector<AllocationSp>                                    outResourceMemories             ;
2521         vector<BufferHandleSp>                                  inResourceBuffers               ;
2522         vector<BufferHandleSp>                                  outResourceBuffers              ;
2523         Move<VkDescriptorPool>                                  descriptorPool                  ;
2524         Move<VkDescriptorSetLayout>                             setLayout                               ;
2525         VkDescriptorSetLayout                                   rawSetLayout                    = DE_NULL;
2526         VkDescriptorSet                                                 rawSet                                  = DE_NULL;
2527
2528         if (numResources != 0)
2529         {
2530                 vector<VkDescriptorSetLayoutBinding>    setLayoutBindings       ;
2531                 vector<VkDescriptorPoolSize>                    poolSizes                       ;
2532
2533                 setLayoutBindings.reserve(numResources);
2534                 poolSizes.reserve(numResources);
2535
2536                 // Process all input resources.
2537                 for (deUint32 inputNdx = 0; inputNdx < numInResources; ++inputNdx)
2538                 {
2539                         const Resource&                                 resource                                = instance.resources.inputs[inputNdx];
2540                         // Create buffer and allocate memory.
2541                         Move<VkBuffer>                                  resourceBuffer                  = createBufferForResource(vk, *vkDevice, resource, queueFamilyIndex);
2542                         de::MovePtr<Allocation>                 resourceMemory                  = allocator.allocate(getBufferMemoryRequirements(vk, *vkDevice, *resourceBuffer), MemoryRequirement::HostVisible);
2543
2544                         VK_CHECK(vk.bindBufferMemory(*vkDevice, *resourceBuffer, resourceMemory->getMemory(), resourceMemory->getOffset()));
2545
2546                         // Copy data to memory.
2547                         const VkMappedMemoryRange               range                                   =
2548                         {
2549                                 VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,                          //      VkStructureType sType;
2550                                 DE_NULL,                                                                                        //      const void*             pNext;
2551                                 resourceMemory->getMemory(),                                            //      VkDeviceMemory  mem;
2552                                 0,                                                                                                      //      VkDeviceSize    offset;
2553                                 VK_WHOLE_SIZE,                                                                          //      VkDeviceSize    size;
2554                         };
2555
2556                         deMemcpy(resourceMemory->getHostPtr(), resource.second->data(), resource.second->getNumBytes());
2557                         VK_CHECK(vk.flushMappedMemoryRanges(*vkDevice, 1u, &range));
2558
2559                         inResourceMemories.push_back(AllocationSp(resourceMemory.release()));
2560                         inResourceBuffers.push_back(BufferHandleSp(new BufferHandleUp(resourceBuffer)));
2561
2562                         // Prepare descriptor bindings and pool sizes for creating descriptor set layout and pool.
2563                         const VkDescriptorSetLayoutBinding      binding                         =
2564                         {
2565                                 inputNdx,                                                                                       // binding
2566                                 resource.first,                                                                         // descriptorType
2567                                 1u,                                                                                                     // descriptorCount
2568                                 VK_SHADER_STAGE_ALL_GRAPHICS,                                           // stageFlags
2569                                 DE_NULL,                                                                                        // pImmutableSamplers
2570                         };
2571                         setLayoutBindings.push_back(binding);
2572
2573                         // Note: the following code doesn't check and unify descriptors of the same type.
2574                         const VkDescriptorPoolSize              poolSize                                =
2575                         {
2576                                 resource.first,                                                                         // type
2577                                 1u,                                                                                                     // descriptorCount
2578                         };
2579                         poolSizes.push_back(poolSize);
2580                 }
2581
2582                 // Process all output resources.
2583                 for (deUint32 outputNdx = 0; outputNdx < numOutResources; ++outputNdx)
2584                 {
2585                         const Resource&                                 resource                                = instance.resources.outputs[outputNdx];
2586                         // Create buffer and allocate memory.
2587                         Move<VkBuffer>                                  resourceBuffer                  = createBufferForResource(vk, *vkDevice, resource, queueFamilyIndex);
2588                         de::MovePtr<Allocation>                 resourceMemory                  = allocator.allocate(getBufferMemoryRequirements(vk, *vkDevice, *resourceBuffer), MemoryRequirement::HostVisible);
2589
2590                         VK_CHECK(vk.bindBufferMemory(*vkDevice, *resourceBuffer, resourceMemory->getMemory(), resourceMemory->getOffset()));
2591
2592                         // Fill memory with all ones.
2593                         const VkMappedMemoryRange               range                                   =
2594                         {
2595                                 VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,                          //      VkStructureType sType;
2596                                 DE_NULL,                                                                                        //      const void*             pNext;
2597                                 resourceMemory->getMemory(),                                            //      VkDeviceMemory  mem;
2598                                 0,                                                                                                      //      VkDeviceSize    offset;
2599                                 VK_WHOLE_SIZE,                                                                          //      VkDeviceSize    size;
2600                         };
2601
2602                         deMemset((deUint8*)resourceMemory->getHostPtr(), 0xff, resource.second->getNumBytes());
2603                         VK_CHECK(vk.flushMappedMemoryRanges(*vkDevice, 1u, &range));
2604
2605                         outResourceMemories.push_back(AllocationSp(resourceMemory.release()));
2606                         outResourceBuffers.push_back(BufferHandleSp(new BufferHandleUp(resourceBuffer)));
2607
2608                         // Prepare descriptor bindings and pool sizes for creating descriptor set layout and pool.
2609                         const VkDescriptorSetLayoutBinding      binding                         =
2610                         {
2611                                 numInResources  + outputNdx,                                            // binding
2612                                 resource.first,                                                                         // descriptorType
2613                                 1u,                                                                                                     // descriptorCount
2614                                 VK_SHADER_STAGE_ALL_GRAPHICS,                                           // stageFlags
2615                                 DE_NULL,                                                                                        // pImmutableSamplers
2616                         };
2617                         setLayoutBindings.push_back(binding);
2618
2619                         // Note: the following code doesn't check and unify descriptors of the same type.
2620                         const VkDescriptorPoolSize              poolSize                                =
2621                         {
2622                                 resource.first,                                                                         // type
2623                                 1u,                                                                                                     // descriptorCount
2624                         };
2625                         poolSizes.push_back(poolSize);
2626                 }
2627
2628                 // Create descriptor set layout, descriptor pool, and allocate descriptor set.
2629                 const VkDescriptorSetLayoutCreateInfo   setLayoutParams         =
2630                 {
2631                         VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,    // sType
2632                         DE_NULL,                                                                                                // pNext
2633                         (VkDescriptorSetLayoutCreateFlags)0,                                    // flags
2634                         numResources,                                                                                   // bindingCount
2635                         setLayoutBindings.data(),                                                               // pBindings
2636                 };
2637                 setLayout                                                                                                       = createDescriptorSetLayout(vk, *vkDevice, &setLayoutParams);
2638                 rawSetLayout                                                                                            = *setLayout;
2639
2640                 const VkDescriptorPoolCreateInfo                poolParams                      =
2641                 {
2642                         VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,                  // sType
2643                         DE_NULL,                                                                                                // pNext
2644                         (VkDescriptorPoolCreateFlags)0,                                                 // flags
2645                         1u,                                                                                                             // maxSets
2646                         numResources,                                                                                   // poolSizeCount
2647                         poolSizes.data(),                                                                               // pPoolSizes
2648                 };
2649                 descriptorPool                                                                                          = createDescriptorPool(vk, *vkDevice, &poolParams);
2650
2651                 const VkDescriptorSetAllocateInfo               setAllocParams          =
2652                 {
2653                         VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,                 // sType
2654                         DE_NULL,                                                                                                // pNext
2655                         *descriptorPool,                                                                                // descriptorPool
2656                         1u,                                                                                                             // descriptorSetCount
2657                         &rawSetLayout,                                                                                  // pSetLayouts
2658                 };
2659                 VK_CHECK(vk.allocateDescriptorSets(*vkDevice, &setAllocParams, &rawSet));
2660
2661                 // Update descriptor set.
2662                 vector<VkWriteDescriptorSet>                    writeSpecs                      ;
2663                 vector<VkDescriptorBufferInfo>                  dBufferInfos            ;
2664
2665                 writeSpecs.reserve(numResources);
2666                 dBufferInfos.reserve(numResources);
2667
2668                 for (deUint32 inputNdx = 0; inputNdx < numInResources; ++inputNdx)
2669                 {
2670                         const VkDescriptorBufferInfo            bufInfo                         =
2671                         {
2672                                 **inResourceBuffers[inputNdx],                                          // buffer
2673                                 0,                                                                                                      // offset
2674                                 VK_WHOLE_SIZE,                                                                          // size
2675                         };
2676                         dBufferInfos.push_back(bufInfo);
2677
2678                         const VkWriteDescriptorSet                      writeSpec                       = {
2679                                 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,                         // sType
2680                                 DE_NULL,                                                                                        // pNext
2681                                 rawSet,                                                                                         // dstSet
2682                                 inputNdx,                                                                                       // binding
2683                                 0,                                                                                                      // dstArrayElement
2684                                 1u,                                                                                                     // descriptorCount
2685                                 instance.resources.inputs[inputNdx].first,                      // descriptorType
2686                                 DE_NULL,                                                                                        // pImageInfo
2687                                 &dBufferInfos.back(),                                                           // pBufferInfo
2688                                 DE_NULL,                                                                                        // pTexelBufferView
2689                         };
2690                         writeSpecs.push_back(writeSpec);
2691                 }
2692                 for (deUint32 outputNdx = 0; outputNdx < numOutResources; ++outputNdx)
2693                 {
2694                         const VkDescriptorBufferInfo            bufInfo                         =
2695                         {
2696                                 **outResourceBuffers[outputNdx],                                        // buffer
2697                                 0,                                                                                                      // offset
2698                                 VK_WHOLE_SIZE,                                                                          // size
2699                         };
2700                         dBufferInfos.push_back(bufInfo);
2701
2702                         const VkWriteDescriptorSet                      writeSpec                       = {
2703                                 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,                         // sType
2704                                 DE_NULL,                                                                                        // pNext
2705                                 rawSet,                                                                                         // dstSet
2706                                 numInResources + outputNdx,                                                     // binding
2707                                 0,                                                                                                      // dstArrayElement
2708                                 1u,                                                                                                     // descriptorCount
2709                                 instance.resources.outputs[outputNdx].first,            // descriptorType
2710                                 DE_NULL,                                                                                        // pImageInfo
2711                                 &dBufferInfos.back(),                                                           // pBufferInfo
2712                                 DE_NULL,                                                                                        // pTexelBufferView
2713                         };
2714                         writeSpecs.push_back(writeSpec);
2715                 }
2716                 vk.updateDescriptorSets(*vkDevice, numResources, writeSpecs.data(), 0, DE_NULL);
2717         }
2718
2719         // Pipeline layout
2720         VkPipelineLayoutCreateInfo                              pipelineLayoutParams    =
2721         {
2722                 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,                  //      VkStructureType                                 sType;
2723                 DE_NULL,                                                                                                //      const void*                                             pNext;
2724                 (VkPipelineLayoutCreateFlags)0,
2725                 0u,                                                                                                             //      deUint32                                                descriptorSetCount;
2726                 DE_NULL,                                                                                                //      const VkDescriptorSetLayout*    pSetLayouts;
2727                 0u,                                                                                                             //      deUint32                                                pushConstantRangeCount;
2728                 DE_NULL,                                                                                                //      const VkPushConstantRange*              pPushConstantRanges;
2729         };
2730
2731         VkPushConstantRange                                             pushConstantRange               =
2732         {
2733                 VK_SHADER_STAGE_ALL_GRAPHICS,                                                   // VkShaderStageFlags    stageFlags;
2734                 0,                                                                                                              // uint32_t              offset;
2735                 0,                                                                                                              // uint32_t              size;
2736         };
2737         if (hasPushConstants)
2738         {
2739                 pushConstantRange.size                                          = static_cast<deUint32>(instance.pushConstants.getBuffer()->getNumBytes());
2740                 pipelineLayoutParams.pushConstantRangeCount     = 1;
2741                 pipelineLayoutParams.pPushConstantRanges        = &pushConstantRange;
2742         }
2743         if (numResources != 0)
2744         {
2745                 // Update pipeline layout with the descriptor set layout.
2746                 pipelineLayoutParams.setLayoutCount                                                             = 1;
2747                 pipelineLayoutParams.pSetLayouts                                                                = &rawSetLayout;
2748         }
2749         const Unique<VkPipelineLayout>                  pipelineLayout                  (createPipelineLayout(vk, *vkDevice, &pipelineLayoutParams));
2750
2751         // Pipeline
2752         vector<VkPipelineShaderStageCreateInfo>         shaderStageParams;
2753         // We need these vectors to make sure that information about specialization constants for each stage can outlive createGraphicsPipeline().
2754         vector<vector<VkSpecializationMapEntry> >       specConstantEntries;
2755         vector<VkSpecializationInfo>                            specializationInfos;
2756         createPipelineShaderStages(vk, *vkDevice, instance, context, modules, shaderStageParams);
2757
2758         // And we don't want the reallocation of these vectors to invalidate pointers pointing to their contents.
2759         specConstantEntries.reserve(shaderStageParams.size());
2760         specializationInfos.reserve(shaderStageParams.size());
2761
2762         // Patch the specialization info field in PipelineShaderStageCreateInfos.
2763         for (vector<VkPipelineShaderStageCreateInfo>::iterator stageInfo = shaderStageParams.begin(); stageInfo != shaderStageParams.end(); ++stageInfo)
2764         {
2765                 const StageToSpecConstantMap::const_iterator stageIt = instance.specConstants.find(stageInfo->stage);
2766
2767                 if (stageIt != instance.specConstants.end())
2768                 {
2769                         const size_t                                            numSpecConstants        = stageIt->second.size();
2770                         vector<VkSpecializationMapEntry>        entries;
2771                         VkSpecializationInfo                            specInfo;
2772
2773                         entries.resize(numSpecConstants);
2774
2775                         // Only support 32-bit integers as spec constants now. And their constant IDs are numbered sequentially starting from 0.
2776                         for (size_t ndx = 0; ndx < numSpecConstants; ++ndx)
2777                         {
2778                                 entries[ndx].constantID = (deUint32)ndx;
2779                                 entries[ndx].offset             = deUint32(ndx * sizeof(deInt32));
2780                                 entries[ndx].size               = sizeof(deInt32);
2781                         }
2782
2783                         specConstantEntries.push_back(entries);
2784
2785                         specInfo.mapEntryCount  = (deUint32)numSpecConstants;
2786                         specInfo.pMapEntries    = specConstantEntries.back().data();
2787                         specInfo.dataSize               = numSpecConstants * sizeof(deInt32);
2788                         specInfo.pData                  = stageIt->second.data();
2789                         specializationInfos.push_back(specInfo);
2790
2791                         stageInfo->pSpecializationInfo = &specializationInfos.back();
2792                 }
2793         }
2794         const VkPipelineDepthStencilStateCreateInfo     depthStencilParams              =
2795         {
2796                 VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,     //      VkStructureType         sType;
2797                 DE_NULL,                                                                                                        //      const void*                     pNext;
2798                 (VkPipelineDepthStencilStateCreateFlags)0,
2799                 DE_FALSE,                                                                                                       //      deUint32                        depthTestEnable;
2800                 DE_FALSE,                                                                                                       //      deUint32                        depthWriteEnable;
2801                 VK_COMPARE_OP_ALWAYS,                                                                           //      VkCompareOp                     depthCompareOp;
2802                 DE_FALSE,                                                                                                       //      deUint32                        depthBoundsTestEnable;
2803                 DE_FALSE,                                                                                                       //      deUint32                        stencilTestEnable;
2804                 {
2805                         VK_STENCIL_OP_KEEP,                                                                                     //      VkStencilOp     stencilFailOp;
2806                         VK_STENCIL_OP_KEEP,                                                                                     //      VkStencilOp     stencilPassOp;
2807                         VK_STENCIL_OP_KEEP,                                                                                     //      VkStencilOp     stencilDepthFailOp;
2808                         VK_COMPARE_OP_ALWAYS,                                                                           //      VkCompareOp     stencilCompareOp;
2809                         0u,                                                                                                                     //      deUint32        stencilCompareMask;
2810                         0u,                                                                                                                     //      deUint32        stencilWriteMask;
2811                         0u,                                                                                                                     //      deUint32        stencilReference;
2812                 },                                                                                                                      //      VkStencilOpState        front;
2813                 {
2814                         VK_STENCIL_OP_KEEP,                                                                                     //      VkStencilOp     stencilFailOp;
2815                         VK_STENCIL_OP_KEEP,                                                                                     //      VkStencilOp     stencilPassOp;
2816                         VK_STENCIL_OP_KEEP,                                                                                     //      VkStencilOp     stencilDepthFailOp;
2817                         VK_COMPARE_OP_ALWAYS,                                                                           //      VkCompareOp     stencilCompareOp;
2818                         0u,                                                                                                                     //      deUint32        stencilCompareMask;
2819                         0u,                                                                                                                     //      deUint32        stencilWriteMask;
2820                         0u,                                                                                                                     //      deUint32        stencilReference;
2821                 },                                                                                                                      //      VkStencilOpState        back;
2822                 -1.0f,                                                                                                          //      float                           minDepthBounds;
2823                 +1.0f,                                                                                                          //      float                           maxDepthBounds;
2824         };
2825         const VkViewport                                                viewport0                               =
2826         {
2827                 0.0f,                                                                                                           //      float   originX;
2828                 0.0f,                                                                                                           //      float   originY;
2829                 (float)renderSize.x(),                                                                          //      float   width;
2830                 (float)renderSize.y(),                                                                          //      float   height;
2831                 0.0f,                                                                                                           //      float   minDepth;
2832                 1.0f,                                                                                                           //      float   maxDepth;
2833         };
2834         const VkRect2D                                                  scissor0                                =
2835         {
2836                 {
2837                         0u,                                                                                                                     //      deInt32 x;
2838                         0u,                                                                                                                     //      deInt32 y;
2839                 },                                                                                                                      //      VkOffset2D      offset;
2840                 {
2841                         renderSize.x(),                                                                                         //      deInt32 width;
2842                         renderSize.y(),                                                                                         //      deInt32 height;
2843                 },                                                                                                                      //      VkExtent2D      extent;
2844         };
2845         const VkPipelineViewportStateCreateInfo         viewportParams                  =
2846         {
2847                 VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,          //      VkStructureType         sType;
2848                 DE_NULL,                                                                                                        //      const void*                     pNext;
2849                 (VkPipelineViewportStateCreateFlags)0,
2850                 1u,                                                                                                                     //      deUint32                        viewportCount;
2851                 &viewport0,
2852                 1u,
2853                 &scissor0
2854         };
2855         const VkSampleMask                                                      sampleMask                              = ~0u;
2856         const VkPipelineMultisampleStateCreateInfo      multisampleParams               =
2857         {
2858                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       //      VkStructureType                 sType;
2859                 DE_NULL,                                                                                                        //      const void*                             pNext;
2860                 (VkPipelineMultisampleStateCreateFlags)0,
2861                 VK_SAMPLE_COUNT_1_BIT,                                                                          //      VkSampleCountFlagBits   rasterSamples;
2862                 DE_FALSE,                                                                                                       //      deUint32                                sampleShadingEnable;
2863                 0.0f,                                                                                                           //      float                                   minSampleShading;
2864                 &sampleMask,                                                                                            //      const VkSampleMask*             pSampleMask;
2865                 DE_FALSE,                                                                                                       //      VkBool32                                alphaToCoverageEnable;
2866                 DE_FALSE,                                                                                                       //      VkBool32                                alphaToOneEnable;
2867         };
2868         const VkPipelineRasterizationStateCreateInfo    rasterParams            =
2869         {
2870                 VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,     //      VkStructureType sType;
2871                 DE_NULL,                                                                                                        //      const void*             pNext;
2872                 (VkPipelineRasterizationStateCreateFlags)0,
2873                 DE_TRUE,                                                                                                        //      deUint32                depthClipEnable;
2874                 DE_FALSE,                                                                                                       //      deUint32                rasterizerDiscardEnable;
2875                 VK_POLYGON_MODE_FILL,                                                                           //      VkFillMode              fillMode;
2876                 VK_CULL_MODE_NONE,                                                                                      //      VkCullMode              cullMode;
2877                 VK_FRONT_FACE_COUNTER_CLOCKWISE,                                                        //      VkFrontFace             frontFace;
2878                 VK_FALSE,                                                                                                       //      VkBool32                depthBiasEnable;
2879                 0.0f,                                                                                                           //      float                   depthBias;
2880                 0.0f,                                                                                                           //      float                   depthBiasClamp;
2881                 0.0f,                                                                                                           //      float                   slopeScaledDepthBias;
2882                 1.0f,                                                                                                           //      float                   lineWidth;
2883         };
2884         const VkPrimitiveTopology topology = hasTessellation? VK_PRIMITIVE_TOPOLOGY_PATCH_LIST: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
2885         const VkPipelineInputAssemblyStateCreateInfo    inputAssemblyParams     =
2886         {
2887                 VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,    //      VkStructureType         sType;
2888                 DE_NULL,                                                                                                                //      const void*                     pNext;
2889                 (VkPipelineInputAssemblyStateCreateFlags)0,
2890                 topology,                                                                                                               //      VkPrimitiveTopology     topology;
2891                 DE_FALSE,                                                                                                               //      deUint32                        primitiveRestartEnable;
2892         };
2893
2894         vector<VkVertexInputBindingDescription>         vertexBindings;
2895         vector<VkVertexInputAttributeDescription>       vertexAttribs;
2896
2897         const VkVertexInputBindingDescription           vertexBinding0                  =
2898         {
2899                 0u,                                                                     // deUint32                                     binding;
2900                 deUint32(singleVertexDataSize),         // deUint32                                     strideInBytes;
2901                 VK_VERTEX_INPUT_RATE_VERTEX                     // VkVertexInputStepRate        stepRate;
2902         };
2903         vertexBindings.push_back(vertexBinding0);
2904
2905         {
2906                 VkVertexInputAttributeDescription               attr0                                   =
2907                 {
2908                         0u,                                                                     // deUint32     location;
2909                         0u,                                                                     // deUint32     binding;
2910                         VK_FORMAT_R32G32B32A32_SFLOAT,          // VkFormat     format;
2911                         0u                                                                      // deUint32     offsetInBytes;
2912                 };
2913                 vertexAttribs.push_back(attr0);
2914
2915                 VkVertexInputAttributeDescription               attr1                                   =
2916                 {
2917                         1u,                                                                     // deUint32     location;
2918                         0u,                                                                     // deUint32     binding;
2919                         VK_FORMAT_R32G32B32A32_SFLOAT,          // VkFormat     format;
2920                         sizeof(Vec4),                                           // deUint32     offsetInBytes;
2921                 };
2922                 vertexAttribs.push_back(attr1);
2923         };
2924
2925         // If the test instantiation has additional input/output interface variables, we need to create additional bindings.
2926         // Right now we only support one additional input varible for the vertex stage, and that will be bound to binding #1
2927         // with location #2.
2928         if (needInterface)
2929         {
2930                 const VkVertexInputBindingDescription   vertexBinding1                  =
2931                 {
2932                         1u,                                                                                                     // deUint32                                     binding;
2933                         instance.interfaces.getInputType().getNumBytes(),       // deUint32                                     strideInBytes;
2934                         VK_VERTEX_INPUT_RATE_VERTEX                                                     // VkVertexInputStepRate        stepRate;
2935                 };
2936                 vertexBindings.push_back(vertexBinding1);
2937
2938                 VkVertexInputAttributeDescription               attr                                    =
2939                 {
2940                         2u,                                                                                                     // deUint32     location;
2941                         1u,                                                                                                     // deUint32     binding;
2942                         instance.interfaces.getInputType().getVkFormat(),       // VkFormat     format;
2943                         0,                                                                                                      // deUint32     offsetInBytes;
2944                 };
2945                 vertexAttribs.push_back(attr);
2946         }
2947
2948         VkPipelineVertexInputStateCreateInfo            vertexInputStateParams  =
2949         {
2950                 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,      //      VkStructureType                                                         sType;
2951                 DE_NULL,                                                                                                        //      const void*                                                                     pNext;
2952                 (VkPipelineVertexInputStateCreateFlags)0,
2953                 1u,                                                                                                                     //      deUint32                                                                        bindingCount;
2954                 vertexBindings.data(),                                                                          //      const VkVertexInputBindingDescription*          pVertexBindingDescriptions;
2955                 2u,                                                                                                                     //      deUint32                                                                        attributeCount;
2956                 vertexAttribs.data(),                                                                           //      const VkVertexInputAttributeDescription*        pVertexAttributeDescriptions;
2957         };
2958
2959         if (needInterface)
2960         {
2961                 vertexInputStateParams.vertexBindingDescriptionCount += 1;
2962                 vertexInputStateParams.vertexAttributeDescriptionCount += 1;
2963         }
2964
2965         vector<VkPipelineColorBlendAttachmentState>     attBlendStates                  ;
2966         const VkPipelineColorBlendAttachmentState       attBlendState                   =
2967         {
2968                 DE_FALSE,                                                                                                       //      deUint32                blendEnable;
2969                 VK_BLEND_FACTOR_ONE,                                                                            //      VkBlend                 srcBlendColor;
2970                 VK_BLEND_FACTOR_ZERO,                                                                           //      VkBlend                 destBlendColor;
2971                 VK_BLEND_OP_ADD,                                                                                        //      VkBlendOp               blendOpColor;
2972                 VK_BLEND_FACTOR_ONE,                                                                            //      VkBlend                 srcBlendAlpha;
2973                 VK_BLEND_FACTOR_ZERO,                                                                           //      VkBlend                 destBlendAlpha;
2974                 VK_BLEND_OP_ADD,                                                                                        //      VkBlendOp               blendOpAlpha;
2975                 (VK_COLOR_COMPONENT_R_BIT|
2976                  VK_COLOR_COMPONENT_G_BIT|
2977                  VK_COLOR_COMPONENT_B_BIT|
2978                  VK_COLOR_COMPONENT_A_BIT),                                                                     //      VkChannelFlags  channelWriteMask;
2979         };
2980         attBlendStates.push_back(attBlendState);
2981
2982         if (needInterface)
2983                 attBlendStates.push_back(attBlendState);
2984
2985         VkPipelineColorBlendStateCreateInfo             blendParams                             =
2986         {
2987                 VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,       //      VkStructureType                                                         sType;
2988                 DE_NULL,                                                                                                        //      const void*                                                                     pNext;
2989                 (VkPipelineColorBlendStateCreateFlags)0,
2990                 DE_FALSE,                                                                                                       //      VkBool32                                                                        logicOpEnable;
2991                 VK_LOGIC_OP_COPY,                                                                                       //      VkLogicOp                                                                       logicOp;
2992                 1u,                                                                                                                     //      deUint32                                                                        attachmentCount;
2993                 attBlendStates.data(),                                                                          //      const VkPipelineColorBlendAttachmentState*      pAttachments;
2994                 { 0.0f, 0.0f, 0.0f, 0.0f },                                                                     //      float                                                                           blendConst[4];
2995         };
2996         if (needInterface)
2997         {
2998                 blendParams.attachmentCount += 1;
2999         }
3000         const VkPipelineTessellationStateCreateInfo     tessellationState       =
3001         {
3002                 VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO,
3003                 DE_NULL,
3004                 (VkPipelineTessellationStateCreateFlags)0,
3005                 3u
3006         };
3007
3008         const VkPipelineTessellationStateCreateInfo* tessellationInfo   =       hasTessellation ? &tessellationState: DE_NULL;
3009         const VkGraphicsPipelineCreateInfo              pipelineParams                  =
3010         {
3011                 VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,                //      VkStructureType                                                                 sType;
3012                 DE_NULL,                                                                                                //      const void*                                                                             pNext;
3013                 0u,                                                                                                             //      VkPipelineCreateFlags                                                   flags;
3014                 (deUint32)shaderStageParams.size(),                                             //      deUint32                                                                                stageCount;
3015                 &shaderStageParams[0],                                                                  //      const VkPipelineShaderStageCreateInfo*                  pStages;
3016                 &vertexInputStateParams,                                                                //      const VkPipelineVertexInputStateCreateInfo*             pVertexInputState;
3017                 &inputAssemblyParams,                                                                   //      const VkPipelineInputAssemblyStateCreateInfo*   pInputAssemblyState;
3018                 tessellationInfo,                                                                               //      const VkPipelineTessellationStateCreateInfo*    pTessellationState;
3019                 &viewportParams,                                                                                //      const VkPipelineViewportStateCreateInfo*                pViewportState;
3020                 &rasterParams,                                                                                  //      const VkPipelineRasterStateCreateInfo*                  pRasterState;
3021                 &multisampleParams,                                                                             //      const VkPipelineMultisampleStateCreateInfo*             pMultisampleState;
3022                 &depthStencilParams,                                                                    //      const VkPipelineDepthStencilStateCreateInfo*    pDepthStencilState;
3023                 &blendParams,                                                                                   //      const VkPipelineColorBlendStateCreateInfo*              pColorBlendState;
3024                 (const VkPipelineDynamicStateCreateInfo*)DE_NULL,               //      const VkPipelineDynamicStateCreateInfo*                 pDynamicState;
3025                 *pipelineLayout,                                                                                //      VkPipelineLayout                                                                layout;
3026                 *renderPass,                                                                                    //      VkRenderPass                                                                    renderPass;
3027                 0u,                                                                                                             //      deUint32                                                                                subpass;
3028                 DE_NULL,                                                                                                //      VkPipeline                                                                              basePipelineHandle;
3029                 0u,                                                                                                             //      deInt32                                                                                 basePipelineIndex;
3030         };
3031
3032         const Unique<VkPipeline>                                pipeline                                (createGraphicsPipeline(vk, *vkDevice, DE_NULL, &pipelineParams));
3033
3034         if (needInterface)
3035         {
3036                 const VkImageViewCreateInfo                     fragOutputViewParams    =
3037                 {
3038                         VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,                       //      VkStructureType                         sType;
3039                         DE_NULL,                                                                                        //      const void*                                     pNext;
3040                         0u,                                                                                                     //      VkImageViewCreateFlags          flags;
3041                         *fragOutputImage,                                                                       //      VkImage                                         image;
3042                         VK_IMAGE_VIEW_TYPE_2D,                                                          //      VkImageViewType                         viewType;
3043                         instance.interfaces.getOutputType().getVkFormat(),      //      VkFormat                                        format;
3044                         {
3045                                 VK_COMPONENT_SWIZZLE_R,
3046                                 VK_COMPONENT_SWIZZLE_G,
3047                                 VK_COMPONENT_SWIZZLE_B,
3048                                 VK_COMPONENT_SWIZZLE_A
3049                         },                                                                                                      //      VkChannelMapping                        channels;
3050                         {
3051                                 VK_IMAGE_ASPECT_COLOR_BIT,                                              //      VkImageAspectFlags      aspectMask;
3052                                 0u,                                                                                             //      deUint32                        baseMipLevel;
3053                                 1u,                                                                                             //      deUint32                        mipLevels;
3054                                 0u,                                                                                             //      deUint32                        baseArrayLayer;
3055                                 1u,                                                                                             //      deUint32                        arraySize;
3056                         },                                                                                                      //      VkImageSubresourceRange         subresourceRange;
3057                 };
3058                 fragOutputImageView = createImageView(vk, *vkDevice, &fragOutputViewParams);
3059                 attViews.push_back(*fragOutputImageView);
3060         }
3061
3062         // Framebuffer
3063         VkFramebufferCreateInfo                                 framebufferParams               =
3064         {
3065                 VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,                              //      VkStructureType         sType;
3066                 DE_NULL,                                                                                                //      const void*                     pNext;
3067                 (VkFramebufferCreateFlags)0,
3068                 *renderPass,                                                                                    //      VkRenderPass            renderPass;
3069                 1u,                                                                                                             //      deUint32                        attachmentCount;
3070                 attViews.data(),                                                                                //      const VkImageView*      pAttachments;
3071                 (deUint32)renderSize.x(),                                                               //      deUint32                        width;
3072                 (deUint32)renderSize.y(),                                                               //      deUint32                        height;
3073                 1u,                                                                                                             //      deUint32                        layers;
3074         };
3075
3076         if (needInterface)
3077                 framebufferParams.attachmentCount += 1;
3078
3079         const Unique<VkFramebuffer>                             framebuffer                             (createFramebuffer(vk, *vkDevice, &framebufferParams));
3080
3081         const Unique<VkCommandPool>                             cmdPool                                 (createCommandPool(vk, *vkDevice, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, queueFamilyIndex));
3082
3083         // Command buffer
3084         const Unique<VkCommandBuffer>                   cmdBuf                                  (allocateCommandBuffer(vk, *vkDevice, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
3085
3086         const VkCommandBufferBeginInfo                  cmdBufBeginParams               =
3087         {
3088                 VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,                    //      VkStructureType                         sType;
3089                 DE_NULL,                                                                                                //      const void*                                     pNext;
3090                 (VkCommandBufferUsageFlags)0,
3091                 (const VkCommandBufferInheritanceInfo*)DE_NULL,
3092         };
3093
3094         // Record commands
3095         VK_CHECK(vk.beginCommandBuffer(*cmdBuf, &cmdBufBeginParams));
3096
3097         {
3098                 const VkMemoryBarrier                   vertFlushBarrier        =
3099                 {
3100                         VK_STRUCTURE_TYPE_MEMORY_BARRIER,                       //      VkStructureType         sType;
3101                         DE_NULL,                                                                        //      const void*                     pNext;
3102                         VK_ACCESS_HOST_WRITE_BIT,                                       //      VkMemoryOutputFlags     outputMask;
3103                         VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT,            //      VkMemoryInputFlags      inputMask;
3104                 };
3105                 vector<VkImageMemoryBarrier>    colorAttBarriers        ;
3106
3107                 VkImageMemoryBarrier                    imgBarrier          =
3108                 {
3109                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,         //      VkStructureType                 sType;
3110                         DE_NULL,                                                                        //      const void*                             pNext;
3111                         0u,                                                                                     //      VkMemoryOutputFlags             outputMask;
3112                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,           //      VkMemoryInputFlags              inputMask;
3113                         VK_IMAGE_LAYOUT_UNDEFINED,                                      //      VkImageLayout                   oldLayout;
3114                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,       //      VkImageLayout                   newLayout;
3115                         queueFamilyIndex,                                                       //      deUint32                                srcQueueFamilyIndex;
3116                         queueFamilyIndex,                                                       //      deUint32                                destQueueFamilyIndex;
3117                         *image,                                                                         //      VkImage                                 image;
3118                         {
3119                                 VK_IMAGE_ASPECT_COLOR_BIT,                                      //      VkImageAspect   aspect;
3120                                 0u,                                                                                     //      deUint32                baseMipLevel;
3121                                 1u,                                                                                     //      deUint32                mipLevels;
3122                                 0u,                                                                                     //      deUint32                baseArraySlice;
3123                                 1u,                                                                                     //      deUint32                arraySize;
3124                         }                                                                                       //      VkImageSubresourceRange subresourceRange;
3125                 };
3126                 colorAttBarriers.push_back(imgBarrier);
3127                 if (needInterface)
3128                 {
3129                         imgBarrier.image = *fragOutputImage;
3130                         colorAttBarriers.push_back(imgBarrier);
3131                         vk.cmdPipelineBarrier(*cmdBuf, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, (VkDependencyFlags)0, 1, &vertFlushBarrier, 0, (const VkBufferMemoryBarrier*)DE_NULL, 2, colorAttBarriers.data());
3132                 }
3133                 else
3134                 {
3135                         vk.cmdPipelineBarrier(*cmdBuf, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, (VkDependencyFlags)0, 1, &vertFlushBarrier, 0, (const VkBufferMemoryBarrier*)DE_NULL, 1, colorAttBarriers.data());
3136                 }
3137         }
3138
3139         {
3140                 vector<VkClearValue>                    clearValue              ;
3141                 clearValue.push_back(makeClearValueColorF32(0.125f, 0.25f, 0.75f, 1.0f));
3142                 if (needInterface)
3143                 {
3144                         clearValue.push_back(makeClearValueColorU32(0, 0, 0, 0));
3145                 }
3146                 VkRenderPassBeginInfo                   passBeginParams =
3147                 {
3148                         VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,                       //      VkStructureType         sType;
3149                         DE_NULL,                                                                                        //      const void*                     pNext;
3150                         *renderPass,                                                                            //      VkRenderPass            renderPass;
3151                         *framebuffer,                                                                           //      VkFramebuffer           framebuffer;
3152                         { { 0, 0 }, { renderSize.x(), renderSize.y() } },       //      VkRect2D                        renderArea;
3153                         1u,                                                                                                     //      deUint32                        clearValueCount;
3154                         clearValue.data(),                                                                      //      const VkClearValue*     pClearValues;
3155                 };
3156                 if (needInterface)
3157                 {
3158                         passBeginParams.clearValueCount += 1;
3159                 }
3160                 vk.cmdBeginRenderPass(*cmdBuf, &passBeginParams, VK_SUBPASS_CONTENTS_INLINE);
3161         }
3162
3163         vk.cmdBindPipeline(*cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, *pipeline);
3164         {
3165                 const VkDeviceSize bindingOffset = 0;
3166                 vk.cmdBindVertexBuffers(*cmdBuf, 0u, 1u, &vertexBuffer.get(), &bindingOffset);
3167         }
3168         if (needInterface)
3169         {
3170                 const VkDeviceSize bindingOffset = 0;
3171                 vk.cmdBindVertexBuffers(*cmdBuf, 1u, 1u, &vertexInputBuffer.get(), &bindingOffset);
3172         }
3173         if (hasPushConstants)
3174         {
3175                 const deUint32  size    = static_cast<deUint32>(instance.pushConstants.getBuffer()->getNumBytes());
3176                 const void*             data    = instance.pushConstants.getBuffer()->data();
3177
3178                 vk.cmdPushConstants(*cmdBuf, *pipelineLayout, VK_SHADER_STAGE_ALL_GRAPHICS, 0, size, data);
3179         }
3180         if (numResources != 0)
3181         {
3182                 // Bind to set number 0.
3183                 vk.cmdBindDescriptorSets(*cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, *pipelineLayout, 0, 1, &rawSet, 0, DE_NULL);
3184         }
3185         vk.cmdDraw(*cmdBuf, deUint32(vertexCount), 1u /*run pipeline once*/, 0u /*first vertex*/, 0u /*first instanceIndex*/);
3186         vk.cmdEndRenderPass(*cmdBuf);
3187
3188         {
3189                 vector<VkImageMemoryBarrier>    renderFinishBarrier;
3190                 VkImageMemoryBarrier                    imgBarrier                              =
3191                 {
3192                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,         //      VkStructureType                 sType;
3193                         DE_NULL,                                                                        //      const void*                             pNext;
3194                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,           //      VkMemoryOutputFlags             outputMask;
3195                         VK_ACCESS_TRANSFER_READ_BIT,                            //      VkMemoryInputFlags              inputMask;
3196                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,       //      VkImageLayout                   oldLayout;
3197                         VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,           //      VkImageLayout                   newLayout;
3198                         queueFamilyIndex,                                                       //      deUint32                                srcQueueFamilyIndex;
3199                         queueFamilyIndex,                                                       //      deUint32                                destQueueFamilyIndex;
3200                         *image,                                                                         //      VkImage                                 image;
3201                         {
3202                                 VK_IMAGE_ASPECT_COLOR_BIT,                                      //      VkImageAspectFlags      aspectMask;
3203                                 0u,                                                                                     //      deUint32                        baseMipLevel;
3204                                 1u,                                                                                     //      deUint32                        mipLevels;
3205                                 0u,                                                                                     //      deUint32                        baseArraySlice;
3206                                 1u,                                                                                     //      deUint32                        arraySize;
3207                         }                                                                                       //      VkImageSubresourceRange subresourceRange;
3208                 };
3209                 renderFinishBarrier.push_back(imgBarrier);
3210
3211                 if (needInterface)
3212                 {
3213                         imgBarrier.image = *fragOutputImage;
3214                         renderFinishBarrier.push_back(imgBarrier);
3215                         vk.cmdPipelineBarrier(*cmdBuf, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 0, (const VkBufferMemoryBarrier*)DE_NULL, 2, renderFinishBarrier.data());
3216                 }
3217                 else
3218                 {
3219                         vk.cmdPipelineBarrier(*cmdBuf, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 0, (const VkBufferMemoryBarrier*)DE_NULL, 1, renderFinishBarrier.data());
3220                 }
3221         }
3222
3223         {
3224                 const VkBufferImageCopy copyParams      =
3225                 {
3226                         (VkDeviceSize)0u,                                               //      VkDeviceSize                    bufferOffset;
3227                         (deUint32)renderSize.x(),                               //      deUint32                                bufferRowLength;
3228                         (deUint32)renderSize.y(),                               //      deUint32                                bufferImageHeight;
3229                         {
3230                                 VK_IMAGE_ASPECT_COLOR_BIT,                              //      VkImageAspect           aspect;
3231                                 0u,                                                                             //      deUint32                        mipLevel;
3232                                 0u,                                                                             //      deUint32                        arrayLayer;
3233                                 1u,                                                                             //      deUint32                        arraySize;
3234                         },                                                                              //      VkImageSubresourceCopy  imageSubresource;
3235                         { 0u, 0u, 0u },                                                 //      VkOffset3D                              imageOffset;
3236                         { renderSize.x(), renderSize.y(), 1u }  //      VkExtent3D                              imageExtent;
3237                 };
3238                 vk.cmdCopyImageToBuffer(*cmdBuf, *image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *readImageBuffer, 1u, &copyParams);
3239
3240                 if (needInterface)
3241                 {
3242                         vk.cmdCopyImageToBuffer(*cmdBuf, *fragOutputImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *fragOutputBuffer, 1u, &copyParams);
3243                 }
3244         }
3245
3246         {
3247                 vector<VkBufferMemoryBarrier> cpFinishBarriers          ;
3248                 VkBufferMemoryBarrier                   copyFinishBarrier       =
3249                 {
3250                         VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,        //      VkStructureType         sType;
3251                         DE_NULL,                                                                        //      const void*                     pNext;
3252                         VK_ACCESS_TRANSFER_WRITE_BIT,                           //      VkMemoryOutputFlags     outputMask;
3253                         VK_ACCESS_HOST_READ_BIT,                                        //      VkMemoryInputFlags      inputMask;
3254                         queueFamilyIndex,                                                       //      deUint32                        srcQueueFamilyIndex;
3255                         queueFamilyIndex,                                                       //      deUint32                        destQueueFamilyIndex;
3256                         *readImageBuffer,                                                       //      VkBuffer                        buffer;
3257                         0u,                                                                                     //      VkDeviceSize            offset;
3258                         imageSizeBytes                                                          //      VkDeviceSize            size;
3259                 };
3260                 cpFinishBarriers.push_back(copyFinishBarrier);
3261
3262                 if (needInterface)
3263                 {
3264                         copyFinishBarrier.buffer        = *fragOutputBuffer;
3265                         copyFinishBarrier.size          = VK_WHOLE_SIZE;
3266                         cpFinishBarriers.push_back(copyFinishBarrier);
3267
3268                         vk.cmdPipelineBarrier(*cmdBuf, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 2, cpFinishBarriers.data(), 0, (const VkImageMemoryBarrier*)DE_NULL);
3269                 }
3270                 else
3271                 {
3272                         vk.cmdPipelineBarrier(*cmdBuf, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 1, cpFinishBarriers.data(), 0, (const VkImageMemoryBarrier*)DE_NULL);
3273                 }
3274         }
3275
3276         VK_CHECK(vk.endCommandBuffer(*cmdBuf));
3277
3278         // Upload vertex data
3279         {
3280                 const VkMappedMemoryRange       range                   =
3281                 {
3282                         VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,  //      VkStructureType sType;
3283                         DE_NULL,                                                                //      const void*             pNext;
3284                         vertexBufferMemory->getMemory(),                //      VkDeviceMemory  mem;
3285                         0,                                                                              //      VkDeviceSize    offset;
3286                         (VkDeviceSize)sizeof(vertexData),               //      VkDeviceSize    size;
3287                 };
3288                 void*                                           vertexBufPtr    = vertexBufferMemory->getHostPtr();
3289
3290                 deMemcpy(vertexBufPtr, &vertexData[0], sizeof(vertexData));
3291                 VK_CHECK(vk.flushMappedMemoryRanges(*vkDevice, 1u, &range));
3292         }
3293
3294         if (needInterface)
3295         {
3296                 const deUint32                          typNumBytes             = instance.interfaces.getInputType().getNumBytes();
3297                 const deUint32                          bufNumBytes             = static_cast<deUint32>(instance.interfaces.getInputBuffer()->getNumBytes());
3298
3299                 // Require that the test instantation provides four output values.
3300                 DE_ASSERT(bufNumBytes == 4 * typNumBytes);
3301
3302                 // We have four triangles. Because interpolation happens before executing the fragment shader,
3303                 // we need to provide the same vertex attribute for the same triangle. That means, duplicate each
3304                 // value three times for all four values.
3305
3306                 const deUint8*                          provided                = static_cast<const deUint8*>(instance.interfaces.getInputBuffer()->data());
3307                 vector<deUint8>                         data;
3308
3309                 data.reserve(3 * bufNumBytes);
3310
3311                 for (deUint32 offset = 0; offset < bufNumBytes; offset += typNumBytes)
3312                         for (deUint32 vertexNdx = 0; vertexNdx < 3; ++vertexNdx)
3313                                 for (deUint32 byteNdx = 0; byteNdx < typNumBytes; ++byteNdx)
3314                                         data.push_back(provided[offset + byteNdx]);
3315
3316                 deMemcpy(vertexInputMemory->getHostPtr(), data.data(), data.size());
3317
3318                 const VkMappedMemoryRange       range                   =
3319                 {
3320                         VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,  //      VkStructureType sType;
3321                         DE_NULL,                                                                //      const void*             pNext;
3322                         vertexInputMemory->getMemory(),                 //      VkDeviceMemory  mem;
3323                         0,                                                                              //      VkDeviceSize    offset;
3324                         VK_WHOLE_SIZE,                                                  //      VkDeviceSize    size;
3325                 };
3326
3327                 VK_CHECK(vk.flushMappedMemoryRanges(*vkDevice, 1u, &range));
3328         }
3329
3330         // Submit & wait for completion
3331         {
3332                 const VkFenceCreateInfo fenceParams     =
3333                 {
3334                         VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,    //      VkStructureType         sType;
3335                         DE_NULL,                                                                //      const void*                     pNext;
3336                         0u,                                                                             //      VkFenceCreateFlags      flags;
3337                 };
3338                 const Unique<VkFence>   fence           (createFence(vk, *vkDevice, &fenceParams));
3339                 const VkSubmitInfo              submitInfo      =
3340                 {
3341                         VK_STRUCTURE_TYPE_SUBMIT_INFO,
3342                         DE_NULL,
3343                         0u,
3344                         (const VkSemaphore*)DE_NULL,
3345                         (const VkPipelineStageFlags*)DE_NULL,
3346                         1u,
3347                         &cmdBuf.get(),
3348                         0u,
3349                         (const VkSemaphore*)DE_NULL,
3350                 };
3351
3352                 VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
3353                 VK_CHECK(vk.waitForFences(*vkDevice, 1u, &fence.get(), DE_TRUE, ~0ull));
3354         }
3355
3356         const void* imagePtr    = readImageBufferMemory->getHostPtr();
3357         const tcu::ConstPixelBufferAccess pixelBuffer(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8),
3358                                                                                                   renderSize.x(), renderSize.y(), 1, imagePtr);
3359         // Log image
3360         {
3361                 const VkMappedMemoryRange       range           =
3362                 {
3363                         VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,  //      VkStructureType sType;
3364                         DE_NULL,                                                                //      const void*             pNext;
3365                         readImageBufferMemory->getMemory(),             //      VkDeviceMemory  mem;
3366                         0,                                                                              //      VkDeviceSize    offset;
3367                         imageSizeBytes,                                                 //      VkDeviceSize    size;
3368                 };
3369
3370                 VK_CHECK(vk.invalidateMappedMemoryRanges(*vkDevice, 1u, &range));
3371                 context.getTestContext().getLog() << TestLog::Image("Result", "Result", pixelBuffer);
3372         }
3373
3374         if (needInterface)
3375         {
3376                 const VkDeviceSize                      fragOutputImgSize       = (VkDeviceSize)(instance.interfaces.getOutputType().getNumBytes() * renderSize.x() * renderSize.y());
3377                 const VkMappedMemoryRange       range                           =
3378                 {
3379                         VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,  //      VkStructureType sType;
3380                         DE_NULL,                                                                //      const void*             pNext;
3381                         fragOutputMemory->getMemory(),                  //      VkDeviceMemory  mem;
3382                         0,                                                                              //      VkDeviceSize    offset;
3383                         fragOutputImgSize,                                              //      VkDeviceSize    size;
3384                 };
3385
3386                 VK_CHECK(vk.invalidateMappedMemoryRanges(*vkDevice, 1u, &range));
3387         }
3388
3389         { // Make sure all output resources are ready.
3390                 for (deUint32 outputNdx = 0; outputNdx < numOutResources; ++outputNdx)
3391                 {
3392                         const VkMappedMemoryRange       range   =
3393                         {
3394                                 VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,                  //      VkStructureType sType;
3395                                 DE_NULL,                                                                                //      const void*             pNext;
3396                                 outResourceMemories[outputNdx]->getMemory(),    //      VkDeviceMemory  mem;
3397                                 0,                                                                                              //      VkDeviceSize    offset;
3398                                 VK_WHOLE_SIZE,                                                                  //      VkDeviceSize    size;
3399                         };
3400
3401                         VK_CHECK(vk.invalidateMappedMemoryRanges(*vkDevice, 1u, &range));
3402                 }
3403         }
3404
3405         const RGBA threshold(1, 1, 1, 1);
3406
3407         const RGBA upperLeft(pixelBuffer.getPixel(1, 1));
3408         if (!tcu::compareThreshold(upperLeft, instance.outputColors[0], threshold))
3409                 return TestStatus(instance.failResult, instance.getSpecializedFailMessage("Upper left corner mismatch"));
3410
3411         const RGBA upperRight(pixelBuffer.getPixel(pixelBuffer.getWidth() - 1, 1));
3412         if (!tcu::compareThreshold(upperRight, instance.outputColors[1], threshold))
3413                 return TestStatus(instance.failResult, instance.getSpecializedFailMessage("Upper right corner mismatch"));
3414
3415         const RGBA lowerLeft(pixelBuffer.getPixel(1, pixelBuffer.getHeight() - 1));
3416         if (!tcu::compareThreshold(lowerLeft, instance.outputColors[2], threshold))
3417                 return TestStatus(instance.failResult, instance.getSpecializedFailMessage("Lower left corner mismatch"));
3418
3419         const RGBA lowerRight(pixelBuffer.getPixel(pixelBuffer.getWidth() - 1, pixelBuffer.getHeight() - 1));
3420         if (!tcu::compareThreshold(lowerRight, instance.outputColors[3], threshold))
3421                 return TestStatus(instance.failResult, instance.getSpecializedFailMessage("Lower right corner mismatch"));
3422
3423         // Check that the contents in the ouput variable matches expected.
3424         if (needInterface)
3425         {
3426                 const IFDataType&                                       outputType                              = instance.interfaces.getOutputType();
3427                 const void*                                                     inputData                               = instance.interfaces.getInputBuffer()->data();
3428                 const void*                                                     outputData                              = instance.interfaces.getOutputBuffer()->data();
3429                 vector<std::pair<int, int> >            positions;
3430                 const tcu::ConstPixelBufferAccess       fragOutputBufferAccess  (outputType.getTextureFormat(), renderSize.x(), renderSize.y(), 1, fragOutputMemory->getHostPtr());
3431
3432                 positions.push_back(std::make_pair(1, 1));
3433                 positions.push_back(std::make_pair(fragOutputBufferAccess.getWidth() - 1, 1));
3434                 positions.push_back(std::make_pair(1, fragOutputBufferAccess.getHeight() - 1));
3435                 positions.push_back(std::make_pair(fragOutputBufferAccess.getWidth() - 1, fragOutputBufferAccess.getHeight() - 1));
3436
3437                 for (deUint32 posNdx = 0; posNdx < positions.size(); ++posNdx)
3438                 {
3439                         const int       x               = positions[posNdx].first;
3440                         const int       y               = positions[posNdx].second;
3441                         bool            equal   = true;
3442
3443                         if (outputType.elementType == NUMBERTYPE_FLOAT32)
3444                         {
3445                                 const float*            expected        = static_cast<const float*>(outputData) + posNdx * outputType.numElements;
3446                                 const float*            actual          = static_cast<const float*>(fragOutputBufferAccess.getPixelPtr(x, y));
3447
3448                                 for (deUint32 eleNdx = 0; eleNdx < outputType.numElements; ++eleNdx)
3449                                         if (!compare32BitFloat(expected[eleNdx], actual[eleNdx], context.getTestContext().getLog()))
3450                                                 equal = false;
3451                         }
3452                         else if (outputType.elementType == NUMBERTYPE_INT32)
3453                         {
3454                                 const deInt32*          expected        = static_cast<const deInt32*>(outputData) + posNdx * outputType.numElements;
3455                                 const deInt32*          actual          = static_cast<const deInt32*>(fragOutputBufferAccess.getPixelPtr(x, y));
3456
3457                                 for (deUint32 eleNdx = 0; eleNdx < outputType.numElements; ++eleNdx)
3458                                         if (expected[eleNdx] != actual[eleNdx])
3459                                                 equal = false;
3460                         }
3461                         else if (outputType.elementType == NUMBERTYPE_UINT32)
3462                         {
3463                                 const deUint32*         expected        = static_cast<const deUint32*>(outputData) + posNdx * outputType.numElements;
3464                                 const deUint32*         actual          = static_cast<const deUint32*>(fragOutputBufferAccess.getPixelPtr(x, y));
3465
3466                                 for (deUint32 eleNdx = 0; eleNdx < outputType.numElements; ++eleNdx)
3467                                         if (expected[eleNdx] != actual[eleNdx])
3468                                                 equal = false;
3469                         }
3470                         else if (outputType.elementType == NUMBERTYPE_FLOAT16)
3471                         {
3472                                 const float*            original        = static_cast<const float*>(inputData) + posNdx * outputType.numElements;
3473                                 const deFloat16*        actual          = static_cast<const deFloat16*>(fragOutputBufferAccess.getPixelPtr(x, y));
3474
3475                                 for (deUint32 eleNdx = 0; eleNdx < outputType.numElements; ++eleNdx)
3476                                         if (!compare16BitFloat(original[eleNdx], actual[eleNdx], instance.interfaces.getRoundingMode(), context.getTestContext().getLog()))
3477                                                 equal = false;
3478                         }
3479                         else if (outputType.elementType == NUMBERTYPE_INT16)
3480                         {
3481                                 const deInt16*          expected        = static_cast<const deInt16*>(outputData) + posNdx * outputType.numElements;
3482                                 const deInt16*          actual          = static_cast<const deInt16*>(fragOutputBufferAccess.getPixelPtr(x, y));
3483
3484                                 for (deUint32 eleNdx = 0; eleNdx < outputType.numElements; ++eleNdx)
3485                                         if (expected[eleNdx] != actual[eleNdx])
3486                                                 equal = false;
3487                         }
3488                         else if (outputType.elementType == NUMBERTYPE_UINT16)
3489                         {
3490                                 const deUint16*         expected        = static_cast<const deUint16*>(outputData) + posNdx * outputType.numElements;
3491                                 const deUint16*         actual          = static_cast<const deUint16*>(fragOutputBufferAccess.getPixelPtr(x, y));
3492
3493                                 for (deUint32 eleNdx = 0; eleNdx < outputType.numElements; ++eleNdx)
3494                                         if (expected[eleNdx] != actual[eleNdx])
3495                                                 equal = false;
3496                         }
3497                         else {
3498                                 DE_ASSERT(0 && "unhandled type");
3499                         }
3500
3501                         if (!equal)
3502                                 return TestStatus(instance.failResult, instance.getSpecializedFailMessage("fragment output dat point #" + numberToString(posNdx) + " mismatch"));
3503                 }
3504         }
3505
3506         // Check the contents in output resources match with expected.
3507         for (deUint32 outputNdx = 0; outputNdx < numOutResources; ++outputNdx)
3508         {
3509                 const BufferSp& expected = instance.resources.outputs[outputNdx].second;
3510
3511                 if (instance.resources.verifyIO != DE_NULL)
3512                 {
3513                         if (!(*instance.resources.verifyIO)(instance.resources.inputs, outResourceMemories, instance.resources.outputs, context.getTestContext().getLog()))
3514                                 return tcu::TestStatus::fail("Resource returned doesn't match with expected");
3515                 }
3516                 else
3517                 {
3518                         if (deMemCmp(expected->data(), outResourceMemories[outputNdx]->getHostPtr(), expected->getNumBytes()))
3519                                 return tcu::TestStatus::fail("Resource returned doesn't match bitwisely with expected");
3520                 }
3521         }
3522
3523         return TestStatus::pass("Rendered output matches input");
3524 }
3525
3526 void createTestsForAllStages (const std::string&                        name,
3527                                                           const RGBA                                    (&inputColors)[4],
3528                                                           const RGBA                                    (&outputColors)[4],
3529                                                           const map<string, string>&    testCodeFragments,
3530                                                           const vector<deInt32>&                specConstants,
3531                                                           const PushConstants&                  pushConstants,
3532                                                           const GraphicsResources&              resources,
3533                                                           const GraphicsInterfaces&             interfaces,
3534                                                           const vector<string>&                 extensions,
3535                                                           const vector<string>&                 features,
3536                                                           VulkanFeatures                                vulkanFeatures,
3537                                                           tcu::TestCaseGroup*                   tests,
3538                                                           const qpTestResult                    failResult,
3539                                                           const string&                                 failMessageTemplate)
3540 {
3541         const ShaderElement             vertFragPipelineStages[]                =
3542         {
3543                 ShaderElement("vert", "main", VK_SHADER_STAGE_VERTEX_BIT),
3544                 ShaderElement("frag", "main", VK_SHADER_STAGE_FRAGMENT_BIT),
3545         };
3546
3547         const ShaderElement             tessPipelineStages[]                    =
3548         {
3549                 ShaderElement("vert", "main", VK_SHADER_STAGE_VERTEX_BIT),
3550                 ShaderElement("tessc", "main", VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT),
3551                 ShaderElement("tesse", "main", VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT),
3552                 ShaderElement("frag", "main", VK_SHADER_STAGE_FRAGMENT_BIT),
3553         };
3554
3555         const ShaderElement             geomPipelineStages[]                            =
3556         {
3557                 ShaderElement("vert", "main", VK_SHADER_STAGE_VERTEX_BIT),
3558                 ShaderElement("geom", "main", VK_SHADER_STAGE_GEOMETRY_BIT),
3559                 ShaderElement("frag", "main", VK_SHADER_STAGE_FRAGMENT_BIT),
3560         };
3561
3562         StageToSpecConstantMap  specConstantMap;
3563
3564         specConstantMap[VK_SHADER_STAGE_VERTEX_BIT] = specConstants;
3565         addFunctionCaseWithPrograms<InstanceContext>(
3566                         tests, name + "_vert", "", addShaderCodeCustomVertex, runAndVerifyDefaultPipeline,
3567                         createInstanceContext(vertFragPipelineStages, inputColors, outputColors, testCodeFragments,
3568                                 specConstantMap, pushConstants, resources, interfaces, extensions, features, vulkanFeatures, vk::VK_SHADER_STAGE_VERTEX_BIT, failResult, failMessageTemplate));
3569
3570         specConstantMap.clear();
3571         specConstantMap[VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT] = specConstants;
3572         addFunctionCaseWithPrograms<InstanceContext>(
3573                         tests, name + "_tessc", "", addShaderCodeCustomTessControl, runAndVerifyDefaultPipeline,
3574                         createInstanceContext(tessPipelineStages, inputColors, outputColors, testCodeFragments,
3575                                 specConstantMap, pushConstants, resources, interfaces, extensions, features, vulkanFeatures, vk::VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, failResult, failMessageTemplate));
3576
3577         specConstantMap.clear();
3578         specConstantMap[VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT] = specConstants;
3579         addFunctionCaseWithPrograms<InstanceContext>(
3580                         tests, name + "_tesse", "", addShaderCodeCustomTessEval, runAndVerifyDefaultPipeline,
3581                         createInstanceContext(tessPipelineStages, inputColors, outputColors, testCodeFragments,
3582                                 specConstantMap, pushConstants, resources, interfaces, extensions, features, vulkanFeatures, vk::VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, failResult, failMessageTemplate));
3583
3584         specConstantMap.clear();
3585         specConstantMap[VK_SHADER_STAGE_GEOMETRY_BIT] = specConstants;
3586         addFunctionCaseWithPrograms<InstanceContext>(
3587                         tests, name + "_geom", "", addShaderCodeCustomGeometry, runAndVerifyDefaultPipeline,
3588                         createInstanceContext(geomPipelineStages, inputColors, outputColors, testCodeFragments,
3589                                 specConstantMap, pushConstants, resources, interfaces, extensions, features, vulkanFeatures, vk::VK_SHADER_STAGE_GEOMETRY_BIT, failResult, failMessageTemplate));
3590
3591         specConstantMap.clear();
3592         specConstantMap[VK_SHADER_STAGE_FRAGMENT_BIT] = specConstants;
3593         addFunctionCaseWithPrograms<InstanceContext>(
3594                         tests, name + "_frag", "", addShaderCodeCustomFragment, runAndVerifyDefaultPipeline,
3595                         createInstanceContext(vertFragPipelineStages, inputColors, outputColors, testCodeFragments,
3596                                 specConstantMap, pushConstants, resources, interfaces, extensions, features, vulkanFeatures, vk::VK_SHADER_STAGE_FRAGMENT_BIT, failResult, failMessageTemplate));
3597 }
3598
3599 void addTessCtrlTest(tcu::TestCaseGroup* group, const char* name, const map<string, string>& fragments)
3600 {
3601         RGBA defaultColors[4];
3602         getDefaultColors(defaultColors);
3603         const ShaderElement pipelineStages[] =
3604         {
3605                 ShaderElement("vert", "main", VK_SHADER_STAGE_VERTEX_BIT),
3606                 ShaderElement("tessc", "main", VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT),
3607                 ShaderElement("tesse", "main", VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT),
3608                 ShaderElement("frag", "main", VK_SHADER_STAGE_FRAGMENT_BIT),
3609         };
3610
3611         addFunctionCaseWithPrograms<InstanceContext>(
3612                         group, name, "", addShaderCodeCustomTessControl,
3613                         runAndVerifyDefaultPipeline, createInstanceContext(
3614                                 pipelineStages, defaultColors, defaultColors, fragments,
3615                                 StageToSpecConstantMap(), PushConstants(), GraphicsResources(),
3616                                 GraphicsInterfaces(), vector<string>(), vector<string>(),
3617                                 VulkanFeatures(), vk::VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT));
3618 }
3619
3620 } // SpirVAssembly
3621 } // vkt