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