dEQP-VK.renderpass: Set IMAGE_USAGE_TRANSFER_SRC_BIT when needed
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / pipeline / vktPipelineVertexInputTests.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Imagination Technologies Ltd.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and/or associated documentation files (the
10  * "Materials"), to deal in the Materials without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Materials, and to
13  * permit persons to whom the Materials are furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice(s) and this permission notice shall be included
17  * in all copies or substantial portions of the Materials.
18  *
19  * The Materials are Confidential Information as defined by the
20  * Khronos Membership Agreement until designated non-confidential by Khronos,
21  * at which point this condition clause shall be removed.
22  *
23  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
30  *
31  *//*!
32  * \file
33  * \brief Vertex Input Tests
34  *//*--------------------------------------------------------------------*/
35
36 #include "vktPipelineVertexInputTests.hpp"
37 #include "vktPipelineCombinationsIterator.hpp"
38 #include "vktPipelineClearUtil.hpp"
39 #include "vktPipelineImageUtil.hpp"
40 #include "vktPipelineVertexUtil.hpp"
41 #include "vktPipelineReferenceRenderer.hpp"
42 #include "vktTestCase.hpp"
43 #include "vktTestCaseUtil.hpp"
44 #include "vkImageUtil.hpp"
45 #include "vkMemUtil.hpp"
46 #include "vkPrograms.hpp"
47 #include "vkQueryUtil.hpp"
48 #include "vkRef.hpp"
49 #include "vkRefUtil.hpp"
50 #include "tcuFloat.hpp"
51 #include "tcuImageCompare.hpp"
52 #include "deFloat16.h"
53 #include "deMemory.h"
54 #include "deStringUtil.hpp"
55 #include "deUniquePtr.hpp"
56
57 #include <sstream>
58 #include <vector>
59
60 namespace vkt
61 {
62 namespace pipeline
63 {
64
65 using namespace vk;
66
67 namespace
68 {
69
70 bool isSupportedVertexFormat (const InstanceInterface& instanceInterface, VkPhysicalDevice device, VkFormat format)
71 {
72         VkFormatProperties  formatProps;
73         deMemset(&formatProps, 0, sizeof(VkFormatProperties));
74         instanceInterface.getPhysicalDeviceFormatProperties(device, format, &formatProps);
75
76         return (formatProps.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) != 0u;
77 }
78
79 float getRepresentableDifferenceUnorm (VkFormat format)
80 {
81         DE_ASSERT(isVertexFormatUnorm(format) || isVertexFormatSRGB(format));
82
83         return 1.0f / float((1 << (getVertexFormatComponentSize(format) * 8)) - 1);
84 }
85
86 float getRepresentableDifferenceSnorm (VkFormat format)
87 {
88         DE_ASSERT(isVertexFormatSnorm(format));
89
90         return 1.0f / float((1 << (getVertexFormatComponentSize(format) * 8 - 1)) - 1);
91 }
92
93 class VertexInputTest : public vkt::TestCase
94 {
95 public:
96         enum GlslType
97         {
98                 GLSL_TYPE_INT,
99                 GLSL_TYPE_IVEC2,
100                 GLSL_TYPE_IVEC3,
101                 GLSL_TYPE_IVEC4,
102
103                 GLSL_TYPE_UINT,
104                 GLSL_TYPE_UVEC2,
105                 GLSL_TYPE_UVEC3,
106                 GLSL_TYPE_UVEC4,
107
108                 GLSL_TYPE_FLOAT,
109                 GLSL_TYPE_VEC2,
110                 GLSL_TYPE_VEC3,
111                 GLSL_TYPE_VEC4,
112                 GLSL_TYPE_MAT2,
113                 GLSL_TYPE_MAT3,
114                 GLSL_TYPE_MAT4,
115
116                 GLSL_TYPE_DOUBLE,
117                 GLSL_TYPE_DVEC2,
118                 GLSL_TYPE_DVEC3,
119                 GLSL_TYPE_DVEC4,
120                 GLSL_TYPE_DMAT2,
121                 GLSL_TYPE_DMAT3,
122                 GLSL_TYPE_DMAT4,
123
124                 GLSL_TYPE_COUNT
125         };
126
127         enum GlslBasicType
128         {
129                 GLSL_BASIC_TYPE_INT,
130                 GLSL_BASIC_TYPE_UINT,
131                 GLSL_BASIC_TYPE_FLOAT,
132                 GLSL_BASIC_TYPE_DOUBLE
133         };
134
135         enum BindingMapping
136         {
137                 BINDING_MAPPING_ONE_TO_ONE,     // Vertex input bindings will not contain data for more than one attribute.
138                 BINDING_MAPPING_ONE_TO_MANY     // Vertex input bindings can contain data for more than one attribute.
139         };
140
141         struct AttributeInfo
142         {
143                 GlslType                                glslType;
144                 VkFormat                                vkType;
145                 VkVertexInputRate               inputRate;
146         };
147
148         struct GlslTypeDescription
149         {
150                 const char*             name;
151                 int                             vertexInputComponentCount;
152                 int                             vertexInputCount;
153                 GlslBasicType   basicType;
154         };
155
156         static const GlslTypeDescription                s_glslTypeDescriptions[GLSL_TYPE_COUNT];
157
158                                                                                         VertexInputTest                         (tcu::TestContext&                                      testContext,
159                                                                                                                                                  const std::string&                                     name,
160                                                                                                                                                  const std::string&                                     description,
161                                                                                                                                                  const std::vector<AttributeInfo>&      attributeInfos,
162                                                                                                                                                  BindingMapping                                         bindingMapping);
163
164         virtual                                                                 ~VertexInputTest                        (void) {}
165         virtual void                                                    initPrograms                            (SourceCollections& programCollection) const;
166         virtual TestInstance*                                   createInstance                          (Context& context) const;
167         static bool                                                             isCompatibleType                        (VkFormat format, GlslType glslType);
168
169 private:
170         std::string                                                             getGlslInputDeclarations        (void) const;
171         std::string                                                             getGlslVertexCheck                      (void) const;
172         std::string                                                             getGlslAttributeConditions      (const AttributeInfo& attributeInfo, deUint32 attributeIndex) const;
173         static tcu::Vec4                                                getFormatThreshold                      (VkFormat format);
174
175         const std::vector<AttributeInfo>                m_attributeInfos;
176         const BindingMapping                                    m_bindingMapping;
177 };
178
179 class GlslTypeCombinationsIterator : public CombinationsIterator< std::vector<VertexInputTest::GlslType> >
180 {
181 public:
182                                                                                                         GlslTypeCombinationsIterator    (deUint32 numValues, deUint32 combinationSize);
183         virtual                                                                                 ~GlslTypeCombinationsIterator   (void) {}
184
185 protected:
186         virtual std::vector<VertexInputTest::GlslType>  getCombinationValue                             (const std::vector<deUint32>& combination);
187
188 private:
189         std::vector<VertexInputTest::GlslType>                  m_combinationValue;
190 };
191
192 class VertexInputInstance : public vkt::TestInstance
193 {
194 public:
195         struct VertexInputAttributeDescription
196         {
197                 VertexInputTest::GlslType                       glslType;
198                 int                                                                     vertexInputIndex;
199                 VkVertexInputAttributeDescription       vkDescription;
200         };
201
202         typedef std::vector<VertexInputAttributeDescription>    AttributeDescriptionList;
203
204                                                                                         VertexInputInstance                     (Context&                                                                                               context,
205                                                                                                                                                  const AttributeDescriptionList&                                                attributeDescriptions,
206                                                                                                                                                  const std::vector<VkVertexInputBindingDescription>&    bindingDescriptions,
207                                                                                                                                                  const std::vector<VkDeviceSize>&                                               bindingOffsets);
208
209         virtual                                                                 ~VertexInputInstance            (void);
210         virtual tcu::TestStatus                                 iterate                                         (void);
211
212
213         static void                                                             writeVertexInputData            (deUint8* destPtr, const VkVertexInputBindingDescription& bindingDescription, const VkDeviceSize bindingOffset, const AttributeDescriptionList& attributes);
214         static void                                                             writeVertexInputValue           (deUint8* destPtr, const VertexInputAttributeDescription& attributes, int indexId);
215
216 private:
217         tcu::TestStatus                                                 verifyImage                                     (void);
218
219 private:
220         std::vector<VkBuffer>                                   m_vertexBuffers;
221         std::vector<Allocation*>                                m_vertexBufferAllocs;
222
223         const tcu::IVec2                                                m_renderSize;
224         const VkFormat                                                  m_colorFormat;
225
226         Move<VkImage>                                                   m_colorImage;
227         de::MovePtr<Allocation>                                 m_colorImageAlloc;
228         Move<VkImage>                                                   m_depthImage;
229         Move<VkImageView>                                               m_colorAttachmentView;
230         Move<VkRenderPass>                                              m_renderPass;
231         Move<VkFramebuffer>                                             m_framebuffer;
232
233         Move<VkShaderModule>                                    m_vertexShaderModule;
234         Move<VkShaderModule>                                    m_fragmentShaderModule;
235
236         Move<VkPipelineLayout>                                  m_pipelineLayout;
237         Move<VkPipeline>                                                m_graphicsPipeline;
238
239         Move<VkCommandPool>                                             m_cmdPool;
240         Move<VkCommandBuffer>                                   m_cmdBuffer;
241
242         Move<VkFence>                                                   m_fence;
243 };
244
245 const VertexInputTest::GlslTypeDescription VertexInputTest::s_glslTypeDescriptions[GLSL_TYPE_COUNT] =
246 {
247         { "int",        1, 1, GLSL_BASIC_TYPE_INT },
248         { "ivec2",      2, 1, GLSL_BASIC_TYPE_INT },
249         { "ivec3",      3, 1, GLSL_BASIC_TYPE_INT },
250         { "ivec4",      4, 1, GLSL_BASIC_TYPE_INT },
251
252         { "uint",       1, 1, GLSL_BASIC_TYPE_UINT },
253         { "uvec2",      2, 1, GLSL_BASIC_TYPE_UINT },
254         { "uvec3",      3, 1, GLSL_BASIC_TYPE_UINT },
255         { "uvec4",      4, 1, GLSL_BASIC_TYPE_UINT },
256
257         { "float",      1, 1, GLSL_BASIC_TYPE_FLOAT },
258         { "vec2",       2, 1, GLSL_BASIC_TYPE_FLOAT },
259         { "vec3",       3, 1, GLSL_BASIC_TYPE_FLOAT },
260         { "vec4",       4, 1, GLSL_BASIC_TYPE_FLOAT },
261         { "mat2",       2, 2, GLSL_BASIC_TYPE_FLOAT },
262         { "mat3",       3, 3, GLSL_BASIC_TYPE_FLOAT },
263         { "mat4",       4, 4, GLSL_BASIC_TYPE_FLOAT },
264
265         { "double",     1, 1, GLSL_BASIC_TYPE_DOUBLE },
266         { "dvec2",      2, 1, GLSL_BASIC_TYPE_DOUBLE },
267         { "dvec3",      3, 1, GLSL_BASIC_TYPE_DOUBLE },
268         { "dvec4",      4, 1, GLSL_BASIC_TYPE_DOUBLE },
269         { "dmat2",      2, 2, GLSL_BASIC_TYPE_DOUBLE },
270         { "dmat3",      3, 3, GLSL_BASIC_TYPE_DOUBLE },
271         { "dmat4",      4, 4, GLSL_BASIC_TYPE_DOUBLE }
272 };
273
274
275 VertexInputTest::VertexInputTest (tcu::TestContext&                                             testContext,
276                                                                   const std::string&                                    name,
277                                                                   const std::string&                                    description,
278                                                                   const std::vector<AttributeInfo>&             attributeInfos,
279                                                                   BindingMapping                                                bindingMapping)
280
281         : vkt::TestCase                 (testContext, name, description)
282         , m_attributeInfos              (attributeInfos)
283         , m_bindingMapping              (bindingMapping)
284 {
285 }
286
287 TestInstance* VertexInputTest::createInstance (Context& context) const
288 {
289         // Create enough binding descriptions with random offsets
290         std::vector<VkVertexInputBindingDescription>    bindingDescriptions;
291         std::vector<VkDeviceSize>                                               bindingOffsets;
292
293         for (size_t bindingNdx = 0; bindingNdx < m_attributeInfos.size() * 2; bindingNdx++)
294         {
295                 // Use STEP_RATE_VERTEX in even bindings and STEP_RATE_INSTANCE in odd bindings
296                 const VkVertexInputRate                                         inputRate                       = (bindingNdx % 2 == 0) ? VK_VERTEX_INPUT_RATE_VERTEX : VK_VERTEX_INPUT_RATE_INSTANCE;
297
298                 // .strideInBytes will be updated when creating the attribute descriptions
299                 const VkVertexInputBindingDescription   bindingDescription      =
300                 {
301                         (deUint32)bindingNdx,   // deUint32                             binding;
302                         0,                                              // deUint32                             stride;
303                         inputRate                               // VkVertexInputRate    inputRate;
304                 };
305
306                 bindingDescriptions.push_back(bindingDescription);
307                 bindingOffsets.push_back(4 * bindingNdx);
308         }
309
310         // Create attribute descriptions, assign them to bindings and update .strideInBytes
311         std::vector<VertexInputInstance::VertexInputAttributeDescription>       attributeDescriptions;
312         deUint32                                                                                                                        attributeLocation               = 0;
313         std::vector<deUint32>                                                                                           attributeOffsets                        (bindingDescriptions.size(), 0);
314
315         for (size_t attributeNdx = 0; attributeNdx < m_attributeInfos.size(); attributeNdx++)
316         {
317                 const AttributeInfo&            attributeInfo                   = m_attributeInfos[attributeNdx];
318                 const GlslTypeDescription&      glslTypeDescription             = s_glslTypeDescriptions[attributeInfo.glslType];
319                 const deUint32                          inputSize                               = getVertexFormatSize(attributeInfo.vkType);
320                 deUint32                                        attributeBinding;
321
322                 if (m_bindingMapping == BINDING_MAPPING_ONE_TO_ONE)
323                 {
324                         if (attributeInfo.inputRate == VK_VERTEX_INPUT_RATE_VERTEX)
325                         {
326                                 attributeBinding = (deUint32)attributeNdx * 2; // Odd binding number
327                         }
328                         else // attributeInfo.inputRate == VK_VERTEX_INPUT_STEP_RATE_INSTANCE
329                         {
330                                 attributeBinding = (deUint32)attributeNdx * 2 + 1; // Even binding number
331                         }
332                 }
333                 else // m_bindingMapping == BINDING_MAPPING_ONE_TO_MANY
334                 {
335                         if (attributeInfo.inputRate == VK_VERTEX_INPUT_RATE_VERTEX)
336                         {
337                                 attributeBinding = 0;
338                         }
339                         else // attributeInfo.inputRate == VK_VERTEX_INPUT_STEP_RATE_INSTANCE
340                         {
341                                 attributeBinding = 1;
342                         }
343                 }
344
345                 for (int descNdx = 0; descNdx < glslTypeDescription.vertexInputCount; descNdx++)
346                 {
347                         const VertexInputInstance::VertexInputAttributeDescription attributeDescription =
348                         {
349                                 attributeInfo.glslType,                                                 // GlslType     glslType;
350                                 descNdx,                                                                                // int          index;
351                                 {
352                                         attributeLocation,                                                      // deUint32     location;
353                                         attributeBinding,                                                       // deUint32     binding;
354                                         attributeInfo.vkType,                                           // VkFormat     format;
355                                         attributeOffsets[attributeBinding],                     // deUint32     offset;
356                                 },
357                         };
358
359                         bindingDescriptions[attributeBinding].stride += inputSize;
360                         attributeOffsets[attributeBinding] += inputSize;
361
362                         attributeLocation++;
363
364                         attributeDescriptions.push_back(attributeDescription);
365                 }
366         }
367
368         return new VertexInputInstance(context, attributeDescriptions, bindingDescriptions, bindingOffsets);
369 }
370
371 void VertexInputTest::initPrograms (SourceCollections& programCollection) const
372 {
373         std::ostringstream vertexSrc;
374
375         vertexSrc << "#version 440\n"
376                           << getGlslInputDeclarations()
377                           << "layout(location = 0) out highp vec4 vtxColor;\n"
378                           << "out gl_PerVertex {\n"
379                           << "  vec4 gl_Position;\n"
380                           << "};\n"
381                           << "double abs (double x) { if (x < 0.0LF) return -x; else return x; }\n" // NOTE: Currently undefined in glslang ??
382                           << "void main (void)\n"
383                           << "{\n"
384                           << getGlslVertexCheck()
385                           << "}\n";
386
387         programCollection.glslSources.add("attribute_test_vert") << glu::VertexSource(vertexSrc.str());
388
389         programCollection.glslSources.add("attribute_test_frag") << glu::FragmentSource(
390                 "#version 440\n"
391                 "layout(location = 0) in highp vec4 vtxColor;\n"
392                 "layout(location = 0) out highp vec4 fragColor;\n"
393                 "void main (void)\n"
394                 "{\n"
395                 "       fragColor = vtxColor;\n"
396                 "}\n");
397 }
398
399 std::string VertexInputTest::getGlslInputDeclarations (void) const
400 {
401         std::ostringstream      glslInputs;
402         deUint32                        location = 0;
403
404         for (size_t attributeNdx = 0; attributeNdx < m_attributeInfos.size(); attributeNdx++)
405         {
406                 const GlslTypeDescription& glslTypeDesc = s_glslTypeDescriptions[m_attributeInfos[attributeNdx].glslType];
407
408                 glslInputs << "layout(location = " << location << ") in highp " << glslTypeDesc.name << " attr" << attributeNdx << ";\n";
409                 location += glslTypeDesc.vertexInputCount;
410         }
411
412         return glslInputs.str();
413 }
414
415 std::string VertexInputTest::getGlslVertexCheck (void) const
416 {
417         std::ostringstream      glslCode;
418         int                                     totalInputComponentCount        = 0;
419
420
421         glslCode << "   int okCount = 0;\n";
422
423         for (size_t attributeNdx = 0; attributeNdx < m_attributeInfos.size(); attributeNdx++)
424         {
425                 glslCode << getGlslAttributeConditions(m_attributeInfos[attributeNdx], (deUint32)attributeNdx);
426
427                 const int vertexInputCount      = VertexInputTest::s_glslTypeDescriptions[m_attributeInfos[attributeNdx].glslType].vertexInputCount;
428                 totalInputComponentCount        += vertexInputCount * VertexInputTest::s_glslTypeDescriptions[m_attributeInfos[attributeNdx].glslType].vertexInputComponentCount;
429         }
430
431         glslCode <<
432                 "       if (okCount == " << totalInputComponentCount << ")\n"
433                 "       {\n"
434                 "               if (gl_InstanceID == 0)\n"
435                 "                       vtxColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
436                 "               else\n"
437                 "                       vtxColor = vec4(0.0, 0.0, 1.0, 1.0);\n"
438                 "       }\n"
439                 "       else\n"
440                 "       {\n"
441                 "               vtxColor = vec4(okCount / float(" << totalInputComponentCount << "), 0.0f, 0.0f, 1.0);\n" <<
442                 "       }\n\n"
443                 "       if (gl_InstanceID == 0)\n"
444                 "       {\n"
445                 "               if (gl_VertexID == 0) gl_Position = vec4(-1.0, -1.0, 0.0, 1.0);\n"
446                 "               else if (gl_VertexID == 1) gl_Position = vec4(0.0, -1.0, 0.0, 1.0);\n"
447                 "               else if (gl_VertexID == 2) gl_Position = vec4(-1.0, 1.0, 0.0, 1.0);\n"
448                 "               else if (gl_VertexID == 3) gl_Position = vec4(0.0, 1.0, 0.0, 1.0);\n"
449                 "               else gl_Position = vec4(0.0);\n"
450                 "       }\n"
451                 "       else\n"
452                 "       {\n"
453                 "               if (gl_VertexID == 0) gl_Position = vec4(0.0, -1.0, 0.0, 1.0);\n"
454                 "               else if (gl_VertexID == 1) gl_Position = vec4(1.0, -1.0, 0.0, 1.0);\n"
455                 "               else if (gl_VertexID == 2) gl_Position = vec4(0.0, 1.0, 0.0, 1.0);\n"
456                 "               else if (gl_VertexID == 3) gl_Position = vec4(1.0, 1.0, 0.0, 1.0);\n"
457                 "               else gl_Position = vec4(0.0);\n"
458                 "       }\n";
459
460         return glslCode.str();
461 }
462
463 std::string VertexInputTest::getGlslAttributeConditions (const AttributeInfo& attributeInfo, deUint32 attributeIndex) const
464 {
465         std::ostringstream      glslCode;
466         std::ostringstream      attributeVar;
467         const std::string       indexId                         = (attributeInfo.inputRate == VK_VERTEX_INPUT_RATE_VERTEX) ? "gl_VertexID" : "gl_InstanceID";
468         const int                       componentCount          = VertexInputTest::s_glslTypeDescriptions[attributeInfo.glslType].vertexInputComponentCount;
469         const int                       vertexInputCount        = VertexInputTest::s_glslTypeDescriptions[attributeInfo.glslType].vertexInputCount;
470         const deUint32          totalComponentCount     = componentCount * vertexInputCount;
471         const tcu::Vec4         threshold                       = getFormatThreshold(attributeInfo.vkType);
472         deUint32                        componentIndex          = 0;
473
474         attributeVar << "attr" << attributeIndex;
475
476         glslCode << std::fixed;
477
478         for (int columnNdx = 0; columnNdx< vertexInputCount; columnNdx++)
479         {
480                 for (int rowNdx = 0; rowNdx < componentCount; rowNdx++)
481                 {
482                         std::string accessStr;
483                         {
484                                 // Build string representing the access to the attribute component
485                                 std::ostringstream accessStream;
486                                 accessStream << attributeVar.str();
487
488                                 if (vertexInputCount == 1)
489                                 {
490                                         if (componentCount > 1)
491                                                 accessStream << "[" << rowNdx << "]";
492                                 }
493                                 else
494                                 {
495                                         accessStream << "[" << columnNdx << "][" << rowNdx << "]";
496                                 }
497
498                                 accessStr = accessStream.str();
499                         }
500
501                         if (isVertexFormatSint(attributeInfo.vkType))
502                         {
503                                 glslCode << "\tif (" << accessStr << " == -(" << totalComponentCount << " * " << indexId << " + " << componentIndex << "))\n";
504                         }
505                         else if (isVertexFormatUint(attributeInfo.vkType))
506                         {
507                                 glslCode << "\tif (" << accessStr << " == uint(" << totalComponentCount << " * " << indexId << " + " << componentIndex << "))\n";
508                         }
509                         else if (isVertexFormatSfloat(attributeInfo.vkType))
510                         {
511                                 if (VertexInputTest::s_glslTypeDescriptions[attributeInfo.glslType].basicType == VertexInputTest::GLSL_BASIC_TYPE_DOUBLE)
512                                 {
513                                         glslCode << "\tif (abs(" << accessStr << " + double(0.01 * (" << totalComponentCount << ".0 * float(" << indexId << ") + " << componentIndex << ".0))) < double(" << threshold[rowNdx] << "))\n";
514                                 }
515                                 else
516                                 {
517                                         glslCode << "\tif (abs(" << accessStr << " + (0.01 * (" << totalComponentCount << ".0 * float(" << indexId << ") + " << componentIndex << ".0))) < " << threshold[rowNdx] << ")\n";
518                                 }
519                         }
520                         else if (isVertexFormatSscaled(attributeInfo.vkType))
521                         {
522                                 glslCode << "\tif (abs(" << accessStr << " + (" << totalComponentCount << ".0 * float(" << indexId << ") + " << componentIndex << ".0)) < " << threshold[rowNdx] << ")\n";
523                         }
524                         else if (isVertexFormatUscaled(attributeInfo.vkType))
525                         {
526                                 glslCode << "\t if (abs(" << accessStr << " - (" << totalComponentCount << ".0 * float(" << indexId << ") + " << componentIndex << ".0)) < " << threshold[rowNdx] << ")\n";
527                         }
528                         else if (isVertexFormatSnorm(attributeInfo.vkType))
529                         {
530                                 const float representableDiff = getRepresentableDifferenceSnorm(attributeInfo.vkType);
531
532                                 glslCode << "\tif (abs(" << accessStr << " - (-1.0 + " << representableDiff << " * (" << totalComponentCount << ".0 * float(" << indexId << ") + " << componentIndex << ".0))) < " << threshold[rowNdx] << ")\n";
533                         }
534                         else if (isVertexFormatUnorm(attributeInfo.vkType) || isVertexFormatSRGB(attributeInfo.vkType))
535                         {
536                                 const float representableDiff = getRepresentableDifferenceUnorm(attributeInfo.vkType);
537
538                                 glslCode << "\tif (abs(" << accessStr << " - " << "(" << representableDiff << " * (" << totalComponentCount << ".0 * float(" << indexId << ") + " << componentIndex << ".0))) < " << threshold[rowNdx] << ")\n";
539                         }
540                         else
541                         {
542                                 DE_ASSERT(false);
543                         }
544
545                         glslCode << "\t\tokCount++;\n\n";
546
547                         componentIndex++;
548                 }
549         }
550         return glslCode.str();
551 }
552
553 tcu::Vec4 VertexInputTest::getFormatThreshold (VkFormat format)
554 {
555         using tcu::Vec4;
556
557         switch (format)
558         {
559                 case VK_FORMAT_R32_SFLOAT:
560                 case VK_FORMAT_R32G32_SFLOAT:
561                 case VK_FORMAT_R32G32B32_SFLOAT:
562                 case VK_FORMAT_R32G32B32A32_SFLOAT:
563                 case VK_FORMAT_R64_SFLOAT:
564                 case VK_FORMAT_R64G64_SFLOAT:
565                 case VK_FORMAT_R64G64B64_SFLOAT:
566                 case VK_FORMAT_R64G64B64A64_SFLOAT:
567                         return Vec4(0.00001f);
568
569                 default:
570                         break;
571         }
572
573         if (isVertexFormatSnorm(format))
574         {
575                 return Vec4(1.5f * getRepresentableDifferenceSnorm(format));
576         }
577         else if (isVertexFormatUnorm(format))
578         {
579                 return Vec4(1.5f * getRepresentableDifferenceUnorm(format));
580         }
581
582         return Vec4(0.001f);
583 }
584
585 GlslTypeCombinationsIterator::GlslTypeCombinationsIterator (deUint32 numValues, deUint32 combinationSize)
586         : CombinationsIterator< std::vector<VertexInputTest::GlslType> >        (numValues, combinationSize)
587         , m_combinationValue                                                                                            (std::vector<VertexInputTest::GlslType>(combinationSize))
588 {
589         DE_ASSERT(numValues <= VertexInputTest::GLSL_TYPE_COUNT);
590 }
591
592 std::vector<VertexInputTest::GlslType> GlslTypeCombinationsIterator::getCombinationValue (const std::vector<deUint32>& combination)
593 {
594         for (size_t combinationItemNdx = 0; combinationItemNdx < combination.size(); combinationItemNdx++)
595                 m_combinationValue[combinationItemNdx] = (VertexInputTest::GlslType)combination[combinationItemNdx];
596
597         return m_combinationValue;
598 }
599
600 VertexInputInstance::VertexInputInstance (Context&                                                                                              context,
601                                                                                   const AttributeDescriptionList&                                               attributeDescriptions,
602                                                                                   const std::vector<VkVertexInputBindingDescription>&   bindingDescriptions,
603                                                                                   const std::vector<VkDeviceSize>&                                              bindingOffsets)
604         : vkt::TestInstance                     (context)
605         , m_renderSize                          (16, 16)
606         , m_colorFormat                         (VK_FORMAT_R8G8B8A8_UNORM)
607 {
608         DE_ASSERT(bindingDescriptions.size() == bindingOffsets.size());
609
610         const DeviceInterface&          vk                                              = context.getDeviceInterface();
611         const VkDevice                          vkDevice                                = context.getDevice();
612         const deUint32                          queueFamilyIndex                = context.getUniversalQueueFamilyIndex();
613         SimpleAllocator                         memAlloc                                (vk, vkDevice, getPhysicalDeviceMemoryProperties(context.getInstanceInterface(), context.getPhysicalDevice()));
614         const VkComponentMapping        componentMappingRGBA    = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
615
616         // Create color image
617         {
618                 const VkImageCreateInfo colorImageParams =
619                 {
620                         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                                                                            // VkStructureType                      sType;
621                         DE_NULL,                                                                                                                                        // const void*                          pNext;
622                         0u,                                                                                                                                                     // VkImageCreateFlags           flags;
623                         VK_IMAGE_TYPE_2D,                                                                                                                       // VkImageType                          imageType;
624                         m_colorFormat,                                                                                                                          // VkFormat                                     format;
625                         { m_renderSize.x(), m_renderSize.y(), 1u },                                                                     // VkExtent3D                           extent;
626                         1u,                                                                                                                                                     // deUint32                                     mipLevels;
627                         1u,                                                                                                                                                     // deUint32                                     arrayLayers;
628                         VK_SAMPLE_COUNT_1_BIT,                                                                                                          // VkSampleCountFlagBits        samples;
629                         VK_IMAGE_TILING_OPTIMAL,                                                                                                        // VkImageTiling                        tiling;
630                         VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,          // VkImageUsageFlags            usage;
631                         VK_SHARING_MODE_EXCLUSIVE,                                                                                                      // VkSharingMode                        sharingMode;
632                         1u,                                                                                                                                                     // deUint32                                     queueFamilyIndexCount;
633                         &queueFamilyIndex,                                                                                                                      // const deUint32*                      pQueueFamilyIndices;
634                         VK_IMAGE_LAYOUT_UNDEFINED,                                                                                                      // VkImageLayout                        initialLayout;
635                 };
636
637                 m_colorImage                    = createImage(vk, vkDevice, &colorImageParams);
638
639                 // Allocate and bind color image memory
640                 m_colorImageAlloc               = memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *m_colorImage), MemoryRequirement::Any);
641                 VK_CHECK(vk.bindImageMemory(vkDevice, *m_colorImage, m_colorImageAlloc->getMemory(), m_colorImageAlloc->getOffset()));
642         }
643
644         // Create color attachment view
645         {
646                 const VkImageViewCreateInfo colorAttachmentViewParams =
647                 {
648                         VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               // VkStructureType                      sType;
649                         DE_NULL,                                                                                // const void*                          pNext;
650                         0u,                                                                                             // VkImageViewCreateFlags       flags;
651                         *m_colorImage,                                                                  // VkImage                                      image;
652                         VK_IMAGE_VIEW_TYPE_2D,                                                  // VkImageViewType                      viewType;
653                         m_colorFormat,                                                                  // VkFormat                                     format;
654                         componentMappingRGBA,                                                   // VkComponentMapping           components;
655                         { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u },  // VkImageSubresourceRange      subresourceRange;
656                 };
657
658                 m_colorAttachmentView = createImageView(vk, vkDevice, &colorAttachmentViewParams);
659         }
660
661         // Create render pass
662         {
663                 const VkAttachmentDescription colorAttachmentDescription =
664                 {
665                         0u,                                                                                                     // VkAttachmentDescriptionFlags         flags;
666                         m_colorFormat,                                                                          // VkFormat                                                     format;
667                         VK_SAMPLE_COUNT_1_BIT,                                                          // VkSampleCountFlagBits                        samples;
668                         VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
669                         VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          storeOp;
670                         VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        // VkAttachmentLoadOp                           stencilLoadOp;
671                         VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          stencilStoreOp;
672                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       // VkImageLayout                                        initialLayout;
673                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout                                        finalLayout;
674                 };
675
676                 const VkAttachmentReference colorAttachmentReference =
677                 {
678                         0u,                                                                                                     // deUint32                     attachment;
679                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout        layout;
680                 };
681
682                 const VkSubpassDescription subpassDescription =
683                 {
684                         0u,                                                                                                     // VkSubpassDescriptionFlags    flags;
685                         VK_PIPELINE_BIND_POINT_GRAPHICS,                                        // VkPipelineBindPoint                  pipelineBindPoint;
686                         0u,                                                                                                     // deUint32                                             inputAttachmentCount;
687                         DE_NULL,                                                                                        // const VkAttachmentReference* pInputAttachments;
688                         1u,                                                                                                     // deUint32                                             colorAttachmentCount;
689                         &colorAttachmentReference,                                                      // const VkAttachmentReference* pColorAttachments;
690                         DE_NULL,                                                                                        // const VkAttachmentReference* pResolveAttachments;
691                         DE_NULL,                                                                                        // const VkAttachmentReference* pDepthStencilAttachment;
692                         0u,                                                                                                     // deUint32                                             preserveAttachmentCount;
693                         DE_NULL                                                                                         // const VkAttachmentReference* pPreserveAttachments;
694                 };
695
696                 const VkRenderPassCreateInfo renderPassParams =
697                 {
698                         VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,                      // VkStructureType                                      sType;
699                         DE_NULL,                                                                                        // const void*                                          pNext;
700                         0u,                                                                                                     // VkRenderPassCreateFlags                      flags;
701                         1u,                                                                                                     // deUint32                                                     attachmentCount;
702                         &colorAttachmentDescription,                                            // const VkAttachmentDescription*       pAttachments;
703                         1u,                                                                                                     // deUint32                                                     subpassCount;
704                         &subpassDescription,                                                            // const VkSubpassDescription*          pSubpasses;
705                         0u,                                                                                                     // deUint32                                                     dependencyCount;
706                         DE_NULL                                                                                         // const VkSubpassDependency*           pDependencies;
707                 };
708
709                 m_renderPass = createRenderPass(vk, vkDevice, &renderPassParams);
710         }
711
712         // Create framebuffer
713         {
714                 const VkFramebufferCreateInfo framebufferParams =
715                 {
716                         VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,                      // VkStructureType                      sType;
717                         DE_NULL,                                                                                        // const void*                          pNext;
718                         0u,                                                                                                     // VkFramebufferCreateFlags     flags;
719                         *m_renderPass,                                                                          // VkRenderPass                         renderPass;
720                         1u,                                                                                                     // deUint32                                     attachmentCount;
721                         &m_colorAttachmentView.get(),                                           // const VkImageView*           pAttachments;
722                         (deUint32)m_renderSize.x(),                                                     // deUint32                                     width;
723                         (deUint32)m_renderSize.y(),                                                     // deUint32                                     height;
724                         1u                                                                                                      // deUint32                                     layers;
725                 };
726
727                 m_framebuffer = createFramebuffer(vk, vkDevice, &framebufferParams);
728         }
729
730         // Create pipeline layout
731         {
732                 const VkPipelineLayoutCreateInfo pipelineLayoutParams =
733                 {
734                         VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,          // VkStructureType                                      sType;
735                         DE_NULL,                                                                                        // const void*                                          pNext;
736                         0u,                                                                                                     // VkPipelineLayoutCreateFlags          flags;
737                         0u,                                                                                                     // deUint32                                                     setLayoutCount;
738                         DE_NULL,                                                                                        // const VkDescriptorSetLayout*         pSetLayouts;
739                         0u,                                                                                                     // deUint32                                                     pushConstantRangeCount;
740                         DE_NULL                                                                                         // const VkPushConstantRange*           pPushConstantRanges;
741                 };
742
743                 m_pipelineLayout = createPipelineLayout(vk, vkDevice, &pipelineLayoutParams);
744         }
745
746         m_vertexShaderModule    = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get("attribute_test_vert"), 0);
747         m_fragmentShaderModule  = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get("attribute_test_frag"), 0);
748
749
750         // Create pipeline
751         {
752                 const VkPipelineShaderStageCreateInfo shaderStageParams[2] =
753                 {
754                         {
755                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                              sType;
756                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
757                                 0u,                                                                                                                     // VkPipelineShaderStageCreateFlags             flags;
758                                 VK_SHADER_STAGE_VERTEX_BIT,                                                                     // VkShaderStageFlagBits                                stage;
759                                 *m_vertexShaderModule,                                                                          // VkShaderModule                                               module;
760                                 "main",                                                                                                         // const char*                                                  pName;
761                                 DE_NULL                                                                                                         // const VkSpecializationInfo*                  pSpecializationInfo;
762                         },
763                         {
764                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                              sType;
765                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
766                                 0u,                                                                                                                     // VkPipelineShaderStageCreateFlags             flags;
767                                 VK_SHADER_STAGE_FRAGMENT_BIT,                                                           // VkShaderStageFlagBits                                stage;
768                                 *m_fragmentShaderModule,                                                                        // VkShaderModule                                               module;
769                                 "main",                                                                                                         // const char*                                                  pName;
770                                 DE_NULL                                                                                                         // const VkSpecializationInfo*                  pSpecializationInfo;
771                         }
772                 };
773
774                 // Create vertex attribute array and check if their VK formats are supported
775                 std::vector<VkVertexInputAttributeDescription> vkAttributeDescriptions;
776                 for (size_t attributeNdx = 0; attributeNdx < attributeDescriptions.size(); attributeNdx++)
777                 {
778                         const VkVertexInputAttributeDescription& attributeDescription = attributeDescriptions[attributeNdx].vkDescription;
779
780                         if (!isSupportedVertexFormat(context.getInstanceInterface(), context.getPhysicalDevice(), attributeDescription.format))
781                                 throw tcu::NotSupportedError(std::string("Unsupported format for vertex input: ") + getFormatName(attributeDescription.format));
782
783                         vkAttributeDescriptions.push_back(attributeDescription);
784                 }
785
786                 const VkPipelineVertexInputStateCreateInfo      vertexInputStateParams  =
787                 {
788                         VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,              // VkStructureType                                                      sType;
789                         DE_NULL,                                                                                                                // const void*                                                          pNext;
790                         0u,                                                                                                                             // VkPipelineVertexInputStateCreateFlags        flags;
791                         (deUint32)bindingDescriptions.size(),                                                   // deUint32                                                                     vertexBindingDescriptionCount;
792                         bindingDescriptions.data(),                                                                             // const VkVertexInputBindingDescription*       pVertexBindingDescriptions;
793                         (deUint32)vkAttributeDescriptions.size(),                                               // deUint32                                                                     vertexAttributeDescriptionCount;
794                         vkAttributeDescriptions.data()                                                                  // const VkVertexInputAttributeDescription*     pVertexAttributeDescriptions;
795                 };
796
797                 const VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateParams =
798                 {
799                         VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,    // VkStructureType                                                      sType;
800                         DE_NULL,                                                                                                                // const void*                                                          pNext;
801                         0u,                                                                                                                             // VkPipelineInputAssemblyStateCreateFlags      flags;
802                         VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,                                                   // VkPrimitiveTopology                                          topology;
803                         false                                                                                                                   // VkBool32                                                                     primitiveRestartEnable;
804                 };
805
806                 const VkViewport viewport =
807                 {
808                         0.0f,                                           // float        x;
809                         0.0f,                                           // float        y;
810                         (float)m_renderSize.x(),        // float        width;
811                         (float)m_renderSize.y(),        // float        height;
812                         0.0f,                                           // float        minDepth;
813                         1.0f                                            // float        maxDepth;
814                 };
815
816                 const VkRect2D scissor = { { 0, 0 }, { m_renderSize.x(), m_renderSize.y() } };
817
818                 const VkPipelineViewportStateCreateInfo viewportStateParams =
819                 {
820                         VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,                  // VkStructureType                                              sType;
821                         DE_NULL,                                                                                                                // const void*                                                  pNext;
822                         0u,                                                                                                                             // VkPipelineViewportStateCreateFlags   flags;
823                         1u,                                                                                                                             // deUint32                                                             viewportCount;
824                         &viewport,                                                                                                              // const VkViewport*                                    pViewports;
825                         1u,                                                                                                                             // deUint32                                                             scissorCount;
826                         &scissor                                                                                                                // const VkRect2D*                                              pScissors;
827                 };
828
829                 const VkPipelineRasterizationStateCreateInfo rasterStateParams =
830                 {
831                         VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,             // VkStructureType                                                      sType;
832                         DE_NULL,                                                                                                                // const void*                                                          pNext;
833                         0u,                                                                                                                             // VkPipelineRasterizationStateCreateFlags      flags;
834                         false,                                                                                                                  // VkBool32                                                                     depthClampEnable;
835                         false,                                                                                                                  // VkBool32                                                                     rasterizerDiscardEnable;
836                         VK_POLYGON_MODE_FILL,                                                                                   // VkPolygonMode                                                        polygonMode;
837                         VK_CULL_MODE_NONE,                                                                                              // VkCullModeFlags                                                      cullMode;
838                         VK_FRONT_FACE_COUNTER_CLOCKWISE,                                                                // VkFrontFace                                                          frontFace;
839                         VK_FALSE,                                                                                                               // VkBool32                                                                     depthBiasEnable;
840                         0.0f,                                                                                                                   // float                                                                        depthBiasConstantFactor;
841                         0.0f,                                                                                                                   // float                                                                        depthBiasClamp;
842                         0.0f,                                                                                                                   // float                                                                        depthBiasSlopeFactor;
843                         1.0f,                                                                                                                   // float                                                                        lineWidth;
844                 };
845
846                 const VkPipelineColorBlendAttachmentState colorBlendAttachmentState =
847                 {
848                         false,                                                                                                                                          // VkBool32                                     blendEnable;
849                         VK_BLEND_FACTOR_ONE,                                                                                                            // VkBlendFactor                        srcColorBlendFactor;
850                         VK_BLEND_FACTOR_ZERO,                                                                                                           // VkBlendFactor                        dstColorBlendFactor;
851                         VK_BLEND_OP_ADD,                                                                                                                        // VkBlendOp                            colorBlendOp;
852                         VK_BLEND_FACTOR_ONE,                                                                                                            // VkBlendFactor                        srcAlphaBlendFactor;
853                         VK_BLEND_FACTOR_ZERO,                                                                                                           // VkBlendFactor                        dstAlphaBlendFactor;
854                         VK_BLEND_OP_ADD,                                                                                                                        // VkBlendOp                            alphaBlendOp;
855                         VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |                                           // VkColorComponentFlags        colorWriteMask;
856                                 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
857                 };
858
859                 const VkPipelineColorBlendStateCreateInfo colorBlendStateParams =
860                 {
861                         VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,       // VkStructureType                                                              sType;
862                         DE_NULL,                                                                                                        // const void*                                                                  pNext;
863                         0u,                                                                                                                     // VkPipelineColorBlendStateCreateFlags                 flags;
864                         false,                                                                                                          // VkBool32                                                                             logicOpEnable;
865                         VK_LOGIC_OP_COPY,                                                                                       // VkLogicOp                                                                    logicOp;
866                         1u,                                                                                                                     // deUint32                                                                             attachmentCount;
867                         &colorBlendAttachmentState,                                                                     // const VkPipelineColorBlendAttachmentState*   pAttachments;
868                         { 0.0f, 0.0f, 0.0f, 0.0f },                                                                     // float                                                                                blendConstants[4];
869                 };
870
871                 const VkPipelineMultisampleStateCreateInfo      multisampleStateParams  =
872                 {
873                         VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
874                         DE_NULL,                                                                                                        // const void*                                                          pNext;
875                         0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
876                         VK_SAMPLE_COUNT_1_BIT,                                                                          // VkSampleCountFlagBits                                        rasterizationSamples;
877                         false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
878                         0.0f,                                                                                                           // float                                                                        minSampleShading;
879                         DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
880                         false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
881                         false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
882                 };
883
884                 const VkPipelineDynamicStateCreateInfo  dynamicStateParams              =
885                 {
886                         VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,           // VkStructureType                                              sType;
887                         DE_NULL,                                                                                                        // const void*                                                  pNext;
888                         0u,                                                                                                                     // VkPipelineDynamicStateCreateFlags    flags;
889                         0u,                                                                                                                     // deUint32                                                             dynamicStateCount;
890                         DE_NULL                                                                                                         // const VkDynamicState*                                pDynamicStates;
891                 };
892
893                 VkPipelineDepthStencilStateCreateInfo depthStencilStateParams =
894                 {
895                         VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,     // VkStructureType                                                      sType;
896                         DE_NULL,                                                                                                        // const void*                                                          pNext;
897                         0u,                                                                                                                     // VkPipelineDepthStencilStateCreateFlags       flags;
898                         false,                                                                                                          // VkBool32                                                                     depthTestEnable;
899                         false,                                                                                                          // VkBool32                                                                     depthWriteEnable;
900                         VK_COMPARE_OP_LESS,                                                                                     // VkCompareOp                                                          depthCompareOp;
901                         false,                                                                                                          // VkBool32                                                                     depthBoundsTestEnable;
902                         false,                                                                                                          // VkBool32                                                                     stencilTestEnable;
903                         // VkStencilOpState     front;
904                         {
905                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  failOp;
906                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  passOp;
907                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  depthFailOp;
908                                 VK_COMPARE_OP_NEVER,    // VkCompareOp  compareOp;
909                                 0u,                                             // deUint32             compareMask;
910                                 0u,                                             // deUint32             writeMask;
911                                 0u,                                             // deUint32             reference;
912                         },
913                         // VkStencilOpState     back;
914                         {
915                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  failOp;
916                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  passOp;
917                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  depthFailOp;
918                                 VK_COMPARE_OP_NEVER,    // VkCompareOp  compareOp;
919                                 0u,                                             // deUint32             compareMask;
920                                 0u,                                             // deUint32             writeMask;
921                                 0u,                                             // deUint32             reference;
922                         },
923                         -1.0f,                                                                                                          // float                        minDepthBounds;
924                         +1.0f,                                                                                                          // float                        maxDepthBounds;
925                 };
926
927                 const VkGraphicsPipelineCreateInfo graphicsPipelineParams =
928                 {
929                         VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,        // VkStructureType                                                                      sType;
930                         DE_NULL,                                                                                        // const void*                                                                          pNext;
931                         0u,                                                                                                     // VkPipelineCreateFlags                                                        flags;
932                         2u,                                                                                                     // deUint32                                                                                     stageCount;
933                         shaderStageParams,                                                                      // const VkPipelineShaderStageCreateInfo*                       pStages;
934                         &vertexInputStateParams,                                                        // const VkPipelineVertexInputStateCreateInfo*          pVertexInputState;
935                         &inputAssemblyStateParams,                                                      // const VkPipelineInputAssemblyStateCreateInfo*        pInputAssemblyState;
936                         DE_NULL,                                                                                        // const VkPipelineTessellationStateCreateInfo*         pTessellationState;
937                         &viewportStateParams,                                                           // const VkPipelineViewportStateCreateInfo*                     pViewportState;
938                         &rasterStateParams,                                                                     // const VkPipelineRasterizationStateCreateInfo*        pRasterizationState;
939                         &multisampleStateParams,                                                        // const VkPipelineMultisampleStateCreateInfo*          pMultisampleState;
940                         &depthStencilStateParams,                                                       // const VkPipelineDepthStencilStateCreateInfo*         pDepthStencilState;
941                         &colorBlendStateParams,                                                         // const VkPipelineColorBlendStateCreateInfo*           pColorBlendState;
942                         &dynamicStateParams,                                                            // const VkPipelineDynamicStateCreateInfo*                      pDynamicState;
943                         *m_pipelineLayout,                                                                      // VkPipelineLayout                                                                     layout;
944                         *m_renderPass,                                                                          // VkRenderPass                                                                         renderPass;
945                         0u,                                                                                                     // deUint32                                                                                     subpass;
946                         0u,                                                                                                     // VkPipeline                                                                           basePipelineHandle;
947                         0u                                                                                                      // deInt32                                                                                      basePipelineIndex;
948                 };
949
950                 m_graphicsPipeline      = createGraphicsPipeline(vk, vkDevice, DE_NULL, &graphicsPipelineParams);
951         }
952
953         // Create vertex buffer
954         {
955                 const VkBufferCreateInfo vertexBufferParams =
956                 {
957                         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,           // VkStructureType              sType;
958                         DE_NULL,                                                                        // const void*                  pNext;
959                         0u,                                                                                     // VkBufferCreateFlags  flags;
960                         4096u,                                                                          // VkDeviceSize                 size;
961                         VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,                      // VkBufferUsageFlags   usage;
962                         VK_SHARING_MODE_EXCLUSIVE,                                      // VkSharingMode                sharingMode;
963                         1u,                                                                                     // deUint32                             queueFamilyIndexCount;
964                         &queueFamilyIndex                                                       // const deUint32*              pQueueFamilyIndices;
965                 };
966
967                 // Upload data for each vertex input binding
968                 for (deUint32 bindingNdx = 0; bindingNdx < bindingDescriptions.size(); bindingNdx++)
969                 {
970                         Move<VkBuffer>                  vertexBuffer            = createBuffer(vk, vkDevice, &vertexBufferParams);
971                         de::MovePtr<Allocation> vertexBufferAlloc       = memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *vertexBuffer), MemoryRequirement::HostVisible);
972
973                         VK_CHECK(vk.bindBufferMemory(vkDevice, *vertexBuffer, vertexBufferAlloc->getMemory(), vertexBufferAlloc->getOffset()));
974
975                         writeVertexInputData((deUint8*)vertexBufferAlloc->getHostPtr(), bindingDescriptions[bindingNdx], bindingOffsets[bindingNdx], attributeDescriptions);
976                         flushMappedMemoryRange(vk, vkDevice, vertexBufferAlloc->getMemory(), vertexBufferAlloc->getOffset(), vertexBufferParams.size);
977
978                         m_vertexBuffers.push_back(vertexBuffer.disown());
979                         m_vertexBufferAllocs.push_back(vertexBufferAlloc.release());
980                 }
981         }
982
983         // Create command pool
984         {
985                 const VkCommandPoolCreateInfo cmdPoolParams =
986                 {
987                         VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,     // VkStructureType                              sType;
988                         DE_NULL,                                                                        // const void*                                  pNext;
989                         VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,           // VkCommandPoolCreateFlags             flags;
990                         queueFamilyIndex,                                                       // deUint32                                             queueFamilyIndex;
991                 };
992
993                 m_cmdPool = createCommandPool(vk, vkDevice, &cmdPoolParams);
994         }
995
996         // Create command buffer
997         {
998                 const VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
999                 {
1000                         VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // VkStructureType                      sType;
1001                         DE_NULL,                                                                                // const void*                          pNext;
1002                         *m_cmdPool,                                                                             // VkCommandPool                        commandPool;
1003                         VK_COMMAND_BUFFER_LEVEL_PRIMARY,                                // VkCommandBufferLevel         level;
1004                         1u                                                                                              // deUint32                                     bufferCount;
1005                 };
1006
1007                 const VkCommandBufferBeginInfo cmdBufferBeginInfo =
1008                 {
1009                         VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,    // VkStructureType                                      sType;
1010                         DE_NULL,                                                                                // const void*                                          pNext;
1011                         0u,                                                                                             // VkCommandBufferUsageFlags            flags;
1012                         DE_NULL,                                                                                // VkRenderPass                                         renderPass;
1013                         0u,                                                                                             // deUint32                                                     subpass;
1014                         DE_NULL,                                                                                // VkFramebuffer                                        framebuffer;
1015                         false,                                                                                  // VkBool32                                                     occlusionQueryEnable;
1016                         0u,                                                                                             // VkQueryControlFlags                          queryFlags;
1017                         0u                                                                                              // VkQueryPipelineStatisticFlags        pipelineStatistics;
1018                 };
1019
1020                 const VkClearValue attachmentClearValue = defaultClearValue(m_colorFormat);
1021
1022                 const VkRenderPassBeginInfo renderPassBeginInfo =
1023                 {
1024                         VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,                               // VkStructureType              sType;
1025                         DE_NULL,                                                                                                // const void*                  pNext;
1026                         *m_renderPass,                                                                                  // VkRenderPass                 renderPass;
1027                         *m_framebuffer,                                                                                 // VkFramebuffer                framebuffer;
1028                         { { 0, 0 }, { m_renderSize.x(), m_renderSize.y() } },   // VkRect2D                             renderArea;
1029                         1u,                                                                                                             // deUint32                             clearValueCount;
1030                         &attachmentClearValue                                                                   // const VkClearValue*  pClearValues;
1031                 };
1032
1033                 m_cmdBuffer = allocateCommandBuffer(vk, vkDevice, &cmdBufferAllocateInfo);
1034
1035                 VK_CHECK(vk.beginCommandBuffer(*m_cmdBuffer, &cmdBufferBeginInfo));
1036                 vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
1037
1038                 vk.cmdBindPipeline(*m_cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_graphicsPipeline);
1039
1040                 std::vector<VkBuffer> vertexBuffers;
1041                 for (size_t bufferNdx = 0; bufferNdx < m_vertexBuffers.size(); bufferNdx++)
1042                         vertexBuffers.push_back(m_vertexBuffers[bufferNdx]);
1043
1044                 if (vertexBuffers.size() <= 1)
1045                 {
1046                         // One vertex buffer
1047                         vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, (deUint32)vertexBuffers.size(), vertexBuffers.data(), bindingOffsets.data());
1048                 }
1049                 else
1050                 {
1051                         // Smoke-test vkCmdBindVertexBuffers(..., startBinding, ... )
1052
1053                         const deUint32 firstHalfLength = (deUint32)vertexBuffers.size() / 2;
1054                         const deUint32 secondHalfLength = firstHalfLength + (deUint32)(vertexBuffers.size() % 2);
1055
1056                         // Bind first half of vertex buffers
1057                         vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, firstHalfLength, vertexBuffers.data(), bindingOffsets.data());
1058
1059                         // Bind second half of vertex buffers
1060                         vk.cmdBindVertexBuffers(*m_cmdBuffer, firstHalfLength, secondHalfLength,
1061                                                                         vertexBuffers.data() + firstHalfLength,
1062                                                                         bindingOffsets.data() + firstHalfLength);
1063                 }
1064
1065                 vk.cmdDraw(*m_cmdBuffer, 4, 2, 0, 0);
1066
1067                 vk.cmdEndRenderPass(*m_cmdBuffer);
1068                 VK_CHECK(vk.endCommandBuffer(*m_cmdBuffer));
1069         }
1070
1071         // Create fence
1072         {
1073                 const VkFenceCreateInfo fenceParams =
1074                 {
1075                         VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,    // VkStructureType              sType;
1076                         DE_NULL,                                                                // const void*                  pNext;
1077                         0u                                                                              // VkFenceCreateFlags   flags;
1078                 };
1079
1080                 m_fence = createFence(vk, vkDevice, &fenceParams);
1081         }
1082 }
1083
1084 VertexInputInstance::~VertexInputInstance (void)
1085 {
1086         const DeviceInterface&          vk                      = m_context.getDeviceInterface();
1087         const VkDevice                          vkDevice        = m_context.getDevice();
1088
1089         for (size_t bufferNdx = 0; bufferNdx < m_vertexBuffers.size(); bufferNdx++)
1090                 vk.destroyBuffer(vkDevice, m_vertexBuffers[bufferNdx], DE_NULL);
1091
1092         for (size_t allocNdx = 0; allocNdx < m_vertexBufferAllocs.size(); allocNdx++)
1093                 delete m_vertexBufferAllocs[allocNdx];
1094 }
1095
1096 void VertexInputInstance::writeVertexInputData(deUint8* destPtr, const VkVertexInputBindingDescription& bindingDescription, const VkDeviceSize bindingOffset, const AttributeDescriptionList& attributes)
1097 {
1098         const deUint32 vertexCount = (bindingDescription.inputRate == VK_VERTEX_INPUT_RATE_VERTEX) ? (4 * 2) : 2;
1099
1100         deUint8* destOffsetPtr = ((deUint8 *)destPtr) + bindingOffset;
1101         for (deUint32 vertexNdx = 0; vertexNdx < vertexCount; vertexNdx++)
1102         {
1103                 deUint32 vertexInputOffset = 0;
1104                 for (size_t attributeNdx = 0; attributeNdx < attributes.size(); attributeNdx++)
1105                 {
1106                         const VertexInputAttributeDescription& attribDesc = attributes[attributeNdx];
1107
1108                         // Only write vertex input data to bindings referenced by attribute descriptions
1109                         if (attribDesc.vkDescription.binding == bindingDescription.binding)
1110                         {
1111                                 writeVertexInputValue(destOffsetPtr + vertexInputOffset, attribDesc, vertexNdx);
1112                                 vertexInputOffset += getVertexFormatSize(attribDesc.vkDescription.format);
1113                         }
1114                 }
1115                 DE_ASSERT(vertexInputOffset <= bindingDescription.stride);
1116                 destOffsetPtr += bindingDescription.stride;
1117         }
1118 }
1119
1120 void writeVertexInputValueSint (deUint8* destPtr, VkFormat format, int componentNdx, deInt32 value)
1121 {
1122         const deUint32  componentSize   = getVertexFormatComponentSize(format);
1123         deUint8*                destFormatPtr   = ((deUint8*)destPtr) + componentSize * componentNdx;
1124
1125         switch (componentSize)
1126         {
1127                 case 1:
1128                         *((deInt8*)destFormatPtr) = (deInt8)value;
1129                         break;
1130
1131                 case 2:
1132                         *((deInt16*)destFormatPtr) = (deInt16)value;
1133                         break;
1134
1135                 case 4:
1136                         *((deInt32*)destFormatPtr) = (deInt32)value;
1137                         break;
1138
1139                 default:
1140                         DE_ASSERT(false);
1141         }
1142 }
1143
1144 void writeVertexInputValueUint (deUint8* destPtr, VkFormat format, int componentNdx, deUint32 value)
1145 {
1146         const deUint32  componentSize   = getVertexFormatComponentSize(format);
1147         deUint8*                destFormatPtr   = ((deUint8*)destPtr) + componentSize * componentNdx;
1148
1149         switch (componentSize)
1150         {
1151                 case 1:
1152                         *((deUint8 *)destFormatPtr) = (deUint8)value;
1153                         break;
1154
1155                 case 2:
1156                         *((deUint16 *)destFormatPtr) = (deUint16)value;
1157                         break;
1158
1159                 case 4:
1160                         *((deUint32 *)destFormatPtr) = (deUint32)value;
1161                         break;
1162
1163                 default:
1164                         DE_ASSERT(false);
1165         }
1166 }
1167
1168 void writeVertexInputValueSfloat (deUint8* destPtr, VkFormat format, int componentNdx, float value)
1169 {
1170         const deUint32  componentSize   = getVertexFormatComponentSize(format);
1171         deUint8*                destFormatPtr   = ((deUint8*)destPtr) + componentSize * componentNdx;
1172
1173         switch (componentSize)
1174         {
1175                 case 2:
1176                         *((deFloat16*)destFormatPtr) = deFloat32To16(value);
1177                         break;
1178
1179                 case 4:
1180                         *((float*)destFormatPtr) = value;
1181                         break;
1182
1183                 default:
1184                         DE_ASSERT(false);
1185         }
1186 }
1187
1188 void VertexInputInstance::writeVertexInputValue (deUint8* destPtr, const VertexInputAttributeDescription& attribute, int indexId)
1189 {
1190         const int               vertexInputCount        = VertexInputTest::s_glslTypeDescriptions[attribute.glslType].vertexInputCount;
1191         const int               componentCount          = VertexInputTest::s_glslTypeDescriptions[attribute.glslType].vertexInputComponentCount;
1192         const deUint32  totalComponentCount     = componentCount * vertexInputCount;
1193         const deUint32  vertexInputIndex        = indexId * totalComponentCount + attribute.vertexInputIndex * componentCount;
1194         const bool              hasBGROrder                     = isVertexFormatComponentOrderBGR(attribute.vkDescription.format);
1195         int                             swizzledNdx;
1196
1197         for (int componentNdx = 0; componentNdx < componentCount; componentNdx++)
1198         {
1199                 if (hasBGROrder)
1200                 {
1201                         if (componentNdx == 0)
1202                                 swizzledNdx = 2;
1203                         else if (componentNdx == 2)
1204                                 swizzledNdx = 0;
1205                         else
1206                                 swizzledNdx = componentNdx;
1207                 }
1208                 else
1209                         swizzledNdx = componentNdx;
1210
1211                 switch (attribute.glslType)
1212                 {
1213                         case VertexInputTest::GLSL_TYPE_INT:
1214                         case VertexInputTest::GLSL_TYPE_IVEC2:
1215                         case VertexInputTest::GLSL_TYPE_IVEC3:
1216                         case VertexInputTest::GLSL_TYPE_IVEC4:
1217                                 writeVertexInputValueSint(destPtr, attribute.vkDescription.format, componentNdx, -(deInt32)(vertexInputIndex + swizzledNdx));
1218                                 break;
1219
1220                         case VertexInputTest::GLSL_TYPE_UINT:
1221                         case VertexInputTest::GLSL_TYPE_UVEC2:
1222                         case VertexInputTest::GLSL_TYPE_UVEC3:
1223                         case VertexInputTest::GLSL_TYPE_UVEC4:
1224                                 writeVertexInputValueUint(destPtr, attribute.vkDescription.format, componentNdx, vertexInputIndex + swizzledNdx);
1225                                 break;
1226
1227                         case VertexInputTest::GLSL_TYPE_FLOAT:
1228                         case VertexInputTest::GLSL_TYPE_VEC2:
1229                         case VertexInputTest::GLSL_TYPE_VEC3:
1230                         case VertexInputTest::GLSL_TYPE_VEC4:
1231                         case VertexInputTest::GLSL_TYPE_MAT2:
1232                         case VertexInputTest::GLSL_TYPE_MAT3:
1233                         case VertexInputTest::GLSL_TYPE_MAT4:
1234                                 if (isVertexFormatSfloat(attribute.vkDescription.format))
1235                                 {
1236                                         writeVertexInputValueSfloat(destPtr, attribute.vkDescription.format, componentNdx, -(0.01f * (float)(vertexInputIndex + swizzledNdx)));
1237                                 }
1238                                 else if (isVertexFormatSscaled(attribute.vkDescription.format))
1239                                 {
1240                                         writeVertexInputValueSint(destPtr, attribute.vkDescription.format, componentNdx, -(deInt32)(vertexInputIndex + swizzledNdx));
1241                                 }
1242                                 else if (isVertexFormatUscaled(attribute.vkDescription.format) || isVertexFormatUnorm(attribute.vkDescription.format) || isVertexFormatSRGB(attribute.vkDescription.format))
1243                                 {
1244                                         writeVertexInputValueUint(destPtr, attribute.vkDescription.format, componentNdx, vertexInputIndex + swizzledNdx);
1245                                 }
1246                                 else if (isVertexFormatSnorm(attribute.vkDescription.format))
1247                                 {
1248                                         const deInt32 minIntValue = -((1 << (getVertexFormatComponentSize(attribute.vkDescription.format) * 8 - 1))) + 1;
1249                                         writeVertexInputValueSint(destPtr, attribute.vkDescription.format, componentNdx, minIntValue + (vertexInputIndex + swizzledNdx));
1250                                 }
1251                                 else
1252                                         DE_ASSERT(false);
1253                                 break;
1254
1255                         case VertexInputTest::GLSL_TYPE_DOUBLE:
1256                         case VertexInputTest::GLSL_TYPE_DVEC2:
1257                         case VertexInputTest::GLSL_TYPE_DVEC3:
1258                         case VertexInputTest::GLSL_TYPE_DVEC4:
1259                         case VertexInputTest::GLSL_TYPE_DMAT2:
1260                         case VertexInputTest::GLSL_TYPE_DMAT3:
1261                         case VertexInputTest::GLSL_TYPE_DMAT4:
1262                                 *(reinterpret_cast<double *>(destPtr) + componentNdx) = -0.01 * (vertexInputIndex + swizzledNdx);
1263
1264                                 break;
1265
1266                         default:
1267                                 DE_ASSERT(false);
1268                 }
1269         }
1270 }
1271
1272 tcu::TestStatus VertexInputInstance::iterate (void)
1273 {
1274         const DeviceInterface&          vk                      = m_context.getDeviceInterface();
1275         const VkDevice                          vkDevice        = m_context.getDevice();
1276         const VkQueue                           queue           = m_context.getUniversalQueue();
1277         const VkSubmitInfo                      submitInfo      =
1278         {
1279                 VK_STRUCTURE_TYPE_SUBMIT_INFO,  // VkStructureType                      sType;
1280                 DE_NULL,                                                // const void*                          pNext;
1281                 0u,                                                             // deUint32                                     waitSemaphoreCount;
1282                 DE_NULL,                                                // const VkSemaphore*           pWaitSemaphores;
1283                 1u,                                                             // deUint32                                     commandBufferCount;
1284                 &m_cmdBuffer.get(),                             // const VkCommandBuffer*       pCommandBuffers;
1285                 0u,                                                             // deUint32                                     signalSemaphoreCount;
1286                 DE_NULL                                                 // const VkSemaphore*           pSignalSemaphores;
1287         };
1288
1289         VK_CHECK(vk.resetFences(vkDevice, 1, &m_fence.get()));
1290         VK_CHECK(vk.queueSubmit(queue, 1, &submitInfo, *m_fence));
1291         VK_CHECK(vk.waitForFences(vkDevice, 1, &m_fence.get(), true, ~(0ull) /* infinity*/));
1292
1293         return verifyImage();
1294 }
1295
1296 bool VertexInputTest::isCompatibleType (VkFormat format, GlslType glslType)
1297 {
1298         const GlslTypeDescription glslTypeDesc = s_glslTypeDescriptions[glslType];
1299
1300         if ((deUint32)s_glslTypeDescriptions[glslType].vertexInputComponentCount == getVertexFormatComponentCount(format))
1301         {
1302                 switch (glslTypeDesc.basicType)
1303                 {
1304                         case GLSL_BASIC_TYPE_INT:
1305                                 return isVertexFormatSint(format);
1306
1307                         case GLSL_BASIC_TYPE_UINT:
1308                                 return isVertexFormatUint(format);
1309
1310                         case GLSL_BASIC_TYPE_FLOAT:
1311                                 return getVertexFormatComponentSize(format) <= 4 && (isVertexFormatSfloat(format) || isVertexFormatSnorm(format) || isVertexFormatUnorm(format) || isVertexFormatSscaled(format) || isVertexFormatUscaled(format) || isVertexFormatSRGB(format));
1312
1313                         case GLSL_BASIC_TYPE_DOUBLE:
1314                                 return isVertexFormatSfloat(format) && getVertexFormatComponentSize(format) == 8;
1315
1316                         default:
1317                                 DE_ASSERT(false);
1318                                 return false;
1319                 }
1320         }
1321         else
1322                 return false;
1323 }
1324
1325 tcu::TestStatus VertexInputInstance::verifyImage (void)
1326 {
1327         bool                                                    compareOk                       = false;
1328         const tcu::TextureFormat                tcuColorFormat          = mapVkFormat(m_colorFormat);
1329         tcu::TextureLevel                               reference                       (tcuColorFormat, m_renderSize.x(), m_renderSize.y());
1330         const tcu::PixelBufferAccess    refRedSubregion         (tcu::getSubregion(reference.getAccess(),
1331                                                                                                                                                    deRoundFloatToInt32((float)m_renderSize.x() * 0.0f),
1332                                                                                                                                                    deRoundFloatToInt32((float)m_renderSize.y() * 0.0f),
1333                                                                                                                                                    deRoundFloatToInt32((float)m_renderSize.x() * 0.5f),
1334                                                                                                                                                    deRoundFloatToInt32((float)m_renderSize.y() * 1.0f)));
1335         const tcu::PixelBufferAccess    refBlueSubregion        (tcu::getSubregion(reference.getAccess(),
1336                                                                                                                                                    deRoundFloatToInt32((float)m_renderSize.x() * 0.5f),
1337                                                                                                                                                    deRoundFloatToInt32((float)m_renderSize.y() * 0.0f),
1338                                                                                                                                                    deRoundFloatToInt32((float)m_renderSize.x() * 0.5f),
1339                                                                                                                                                    deRoundFloatToInt32((float)m_renderSize.y() * 1.0f)));
1340
1341         // Create reference image
1342         tcu::clear(reference.getAccess(), defaultClearColor(tcuColorFormat));
1343         tcu::clear(refRedSubregion, tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
1344         tcu::clear(refBlueSubregion, tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
1345
1346         // Compare result with reference image
1347         {
1348                 const DeviceInterface&                  vk                                      = m_context.getDeviceInterface();
1349                 const VkDevice                                  vkDevice                        = m_context.getDevice();
1350                 const VkQueue                                   queue                           = m_context.getUniversalQueue();
1351                 const deUint32                                  queueFamilyIndex        = m_context.getUniversalQueueFamilyIndex();
1352                 SimpleAllocator                                 allocator                       (vk, vkDevice, getPhysicalDeviceMemoryProperties(m_context.getInstanceInterface(), m_context.getPhysicalDevice()));
1353                 de::MovePtr<tcu::TextureLevel>  result                          = readColorAttachment(vk, vkDevice, queue, queueFamilyIndex, allocator, *m_colorImage, m_colorFormat, m_renderSize);
1354
1355                 compareOk = tcu::intThresholdPositionDeviationCompare(m_context.getTestContext().getLog(),
1356                                                                                                                           "IntImageCompare",
1357                                                                                                                           "Image comparison",
1358                                                                                                                           reference.getAccess(),
1359                                                                                                                           result->getAccess(),
1360                                                                                                                           tcu::UVec4(2, 2, 2, 2),
1361                                                                                                                           tcu::IVec3(1, 1, 0),
1362                                                                                                                           true,
1363                                                                                                                           tcu::COMPARE_LOG_RESULT);
1364         }
1365
1366         if (compareOk)
1367                 return tcu::TestStatus::pass("Result image matches reference");
1368         else
1369                 return tcu::TestStatus::fail("Image mismatch");
1370 }
1371
1372 std::string getAttributeInfoCaseName (const VertexInputTest::AttributeInfo& attributeInfo)
1373 {
1374         std::ostringstream      caseName;
1375         const std::string       formatName      = getFormatName(attributeInfo.vkType);
1376
1377         caseName << VertexInputTest::s_glslTypeDescriptions[attributeInfo.glslType].name << "_as_" << de::toLower(formatName.substr(10)) << "_rate_";
1378
1379         if (attributeInfo.inputRate == VK_VERTEX_INPUT_RATE_VERTEX)
1380                 caseName <<  "vertex";
1381         else
1382                 caseName <<  "instance";
1383
1384         return caseName.str();
1385 }
1386
1387 std::string getAttributeInfosCaseName (const std::vector<VertexInputTest::AttributeInfo>& attributeInfos)
1388 {
1389         std::ostringstream caseName;
1390
1391         for (size_t attributeNdx = 0; attributeNdx < attributeInfos.size(); attributeNdx++)
1392         {
1393                 caseName << getAttributeInfoCaseName(attributeInfos[attributeNdx]);
1394
1395                 if (attributeNdx < attributeInfos.size() - 1)
1396                         caseName << "-";
1397         }
1398
1399         return caseName.str();
1400 }
1401
1402 std::string getAttributeInfoDescription (const VertexInputTest::AttributeInfo& attributeInfo)
1403 {
1404         std::ostringstream caseDesc;
1405
1406         caseDesc << std::string(VertexInputTest::s_glslTypeDescriptions[attributeInfo.glslType].name) << " from type " << getFormatName(attributeInfo.vkType) <<  " with ";
1407
1408         if (attributeInfo.inputRate == VK_VERTEX_INPUT_RATE_VERTEX)
1409                 caseDesc <<  "vertex input rate ";
1410         else
1411                 caseDesc <<  "instance input rate ";
1412
1413         return caseDesc.str();
1414 }
1415
1416 std::string getAttributeInfosDescription (const std::vector<VertexInputTest::AttributeInfo>& attributeInfos)
1417 {
1418         std::ostringstream caseDesc;
1419
1420         caseDesc << "Uses vertex attributes:\n";
1421
1422         for (size_t attributeNdx = 0; attributeNdx < attributeInfos.size(); attributeNdx++)
1423                 caseDesc << "\t- " << getAttributeInfoDescription (attributeInfos[attributeNdx]) << "\n";
1424
1425         return caseDesc.str();
1426 }
1427
1428 struct CompatibleFormats
1429 {
1430         VertexInputTest::GlslType       glslType;
1431         std::vector<VkFormat>           compatibleVkFormats;
1432 };
1433
1434 de::MovePtr<tcu::TestCaseGroup> createSingleAttributeTests (tcu::TestContext& testCtx)
1435 {
1436         const VkFormat vertexFormats[] =
1437         {
1438                 // Required, unpacked
1439                 VK_FORMAT_R8_UNORM,
1440                 VK_FORMAT_R8_SNORM,
1441                 VK_FORMAT_R8_UINT,
1442                 VK_FORMAT_R8_SINT,
1443                 VK_FORMAT_R8G8_UNORM,
1444                 VK_FORMAT_R8G8_SNORM,
1445                 VK_FORMAT_R8G8_UINT,
1446                 VK_FORMAT_R8G8_SINT,
1447                 VK_FORMAT_R8G8B8A8_UNORM,
1448                 VK_FORMAT_R8G8B8A8_SNORM,
1449                 VK_FORMAT_R8G8B8A8_UINT,
1450                 VK_FORMAT_R8G8B8A8_SINT,
1451                 VK_FORMAT_B8G8R8A8_UNORM,
1452                 VK_FORMAT_R16_UNORM,
1453                 VK_FORMAT_R16_SNORM,
1454                 VK_FORMAT_R16_UINT,
1455                 VK_FORMAT_R16_SINT,
1456                 VK_FORMAT_R16_SFLOAT,
1457                 VK_FORMAT_R16G16_UNORM,
1458                 VK_FORMAT_R16G16_SNORM,
1459                 VK_FORMAT_R16G16_UINT,
1460                 VK_FORMAT_R16G16_SINT,
1461                 VK_FORMAT_R16G16_SFLOAT,
1462                 VK_FORMAT_R16G16B16A16_UNORM,
1463                 VK_FORMAT_R16G16B16A16_SNORM,
1464                 VK_FORMAT_R16G16B16A16_UINT,
1465                 VK_FORMAT_R16G16B16A16_SINT,
1466                 VK_FORMAT_R16G16B16A16_SFLOAT,
1467                 VK_FORMAT_R32_UINT,
1468                 VK_FORMAT_R32_SINT,
1469                 VK_FORMAT_R32_SFLOAT,
1470                 VK_FORMAT_R32G32_UINT,
1471                 VK_FORMAT_R32G32_SINT,
1472                 VK_FORMAT_R32G32_SFLOAT,
1473                 VK_FORMAT_R32G32B32_UINT,
1474                 VK_FORMAT_R32G32B32_SINT,
1475                 VK_FORMAT_R32G32B32_SFLOAT,
1476                 VK_FORMAT_R32G32B32A32_UINT,
1477                 VK_FORMAT_R32G32B32A32_SINT,
1478                 VK_FORMAT_R32G32B32A32_SFLOAT,
1479
1480                 // Scaled formats
1481                 VK_FORMAT_R8G8_USCALED,
1482                 VK_FORMAT_R8G8_SSCALED,
1483                 VK_FORMAT_R16_USCALED,
1484                 VK_FORMAT_R16_SSCALED,
1485                 VK_FORMAT_R8G8B8_USCALED,
1486                 VK_FORMAT_R8G8B8_SSCALED,
1487                 VK_FORMAT_B8G8R8_USCALED,
1488                 VK_FORMAT_B8G8R8_SSCALED,
1489                 VK_FORMAT_R8G8B8A8_USCALED,
1490                 VK_FORMAT_R8G8B8A8_SSCALED,
1491                 VK_FORMAT_B8G8R8A8_USCALED,
1492                 VK_FORMAT_B8G8R8A8_SSCALED,
1493                 VK_FORMAT_R16G16_USCALED,
1494                 VK_FORMAT_R16G16_SSCALED,
1495                 VK_FORMAT_R16G16B16_USCALED,
1496                 VK_FORMAT_R16G16B16_SSCALED,
1497                 VK_FORMAT_R16G16B16A16_USCALED,
1498                 VK_FORMAT_R16G16B16A16_SSCALED,
1499
1500                 // SRGB formats
1501                 VK_FORMAT_R8_SRGB,
1502                 VK_FORMAT_R8G8_SRGB,
1503                 VK_FORMAT_R8G8B8_SRGB,
1504                 VK_FORMAT_B8G8R8_SRGB,
1505                 VK_FORMAT_R8G8B8A8_SRGB,
1506                 VK_FORMAT_B8G8R8A8_SRGB,
1507
1508                 // Double formats
1509                 VK_FORMAT_R64_SFLOAT,
1510                 VK_FORMAT_R64G64_SFLOAT,
1511                 VK_FORMAT_R64G64B64_SFLOAT,
1512                 VK_FORMAT_R64G64B64A64_SFLOAT,
1513         };
1514
1515         de::MovePtr<tcu::TestCaseGroup> singleAttributeTests (new tcu::TestCaseGroup(testCtx, "single_attribute", "Uses one attribute"));
1516
1517         for (int formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(vertexFormats); formatNdx++)
1518         {
1519                 for (int glslTypeNdx = 0; glslTypeNdx < VertexInputTest::GLSL_TYPE_COUNT; glslTypeNdx++)
1520                 {
1521                         if (VertexInputTest::isCompatibleType(vertexFormats[formatNdx], (VertexInputTest::GlslType)glslTypeNdx))
1522                         {
1523                                 // Create test case for RATE_VERTEX
1524                                 VertexInputTest::AttributeInfo attributeInfo;
1525                                 attributeInfo.vkType    = vertexFormats[formatNdx];
1526                                 attributeInfo.glslType  = (VertexInputTest::GlslType)glslTypeNdx;
1527                                 attributeInfo.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
1528
1529                                 singleAttributeTests->addChild(new VertexInputTest(testCtx,
1530                                                                                                                                    getAttributeInfoCaseName(attributeInfo),
1531                                                                                                                                    getAttributeInfoDescription(attributeInfo),
1532                                                                                                                                    std::vector<VertexInputTest::AttributeInfo>(1, attributeInfo),
1533                                                                                                                                    VertexInputTest::BINDING_MAPPING_ONE_TO_ONE));
1534
1535                                 // Create test case for RATE_INSTANCE
1536                                 attributeInfo.inputRate = VK_VERTEX_INPUT_RATE_INSTANCE;
1537
1538                                 singleAttributeTests->addChild(new VertexInputTest(testCtx,
1539                                                                                                                                    getAttributeInfoCaseName(attributeInfo),
1540                                                                                                                                    getAttributeInfoDescription(attributeInfo),
1541                                                                                                                                    std::vector<VertexInputTest::AttributeInfo>(1, attributeInfo),
1542                                                                                                                                    VertexInputTest::BINDING_MAPPING_ONE_TO_ONE));
1543                         }
1544                 }
1545         }
1546
1547         return singleAttributeTests;
1548 }
1549
1550 de::MovePtr<tcu::TestCaseGroup> createMultipleAttributeTests (tcu::TestContext& testCtx)
1551 {
1552         // Required vertex formats, unpacked
1553         const VkFormat vertexFormats[] =
1554         {
1555                 VK_FORMAT_R8_UNORM,
1556                 VK_FORMAT_R8_SNORM,
1557                 VK_FORMAT_R8_UINT,
1558                 VK_FORMAT_R8_SINT,
1559                 VK_FORMAT_R8G8_UNORM,
1560                 VK_FORMAT_R8G8_SNORM,
1561                 VK_FORMAT_R8G8_UINT,
1562                 VK_FORMAT_R8G8_SINT,
1563                 VK_FORMAT_R8G8B8A8_UNORM,
1564                 VK_FORMAT_R8G8B8A8_SNORM,
1565                 VK_FORMAT_R8G8B8A8_UINT,
1566                 VK_FORMAT_R8G8B8A8_SINT,
1567                 VK_FORMAT_B8G8R8A8_UNORM,
1568                 VK_FORMAT_R16_UNORM,
1569                 VK_FORMAT_R16_SNORM,
1570                 VK_FORMAT_R16_UINT,
1571                 VK_FORMAT_R16_SINT,
1572                 VK_FORMAT_R16_SFLOAT,
1573                 VK_FORMAT_R16G16_UNORM,
1574                 VK_FORMAT_R16G16_SNORM,
1575                 VK_FORMAT_R16G16_UINT,
1576                 VK_FORMAT_R16G16_SINT,
1577                 VK_FORMAT_R16G16_SFLOAT,
1578                 VK_FORMAT_R16G16B16A16_UNORM,
1579                 VK_FORMAT_R16G16B16A16_SNORM,
1580                 VK_FORMAT_R16G16B16A16_UINT,
1581                 VK_FORMAT_R16G16B16A16_SINT,
1582                 VK_FORMAT_R16G16B16A16_SFLOAT,
1583                 VK_FORMAT_R32_UINT,
1584                 VK_FORMAT_R32_SINT,
1585                 VK_FORMAT_R32_SFLOAT,
1586                 VK_FORMAT_R32G32_UINT,
1587                 VK_FORMAT_R32G32_SINT,
1588                 VK_FORMAT_R32G32_SFLOAT,
1589                 VK_FORMAT_R32G32B32_UINT,
1590                 VK_FORMAT_R32G32B32_SINT,
1591                 VK_FORMAT_R32G32B32_SFLOAT,
1592                 VK_FORMAT_R32G32B32A32_UINT,
1593                 VK_FORMAT_R32G32B32A32_SINT,
1594                 VK_FORMAT_R32G32B32A32_SFLOAT
1595         };
1596
1597         de::MovePtr<tcu::TestCaseGroup> multipleAttributeTests (new tcu::TestCaseGroup(testCtx, "multiple_attributes", "Uses more than one attribute"));
1598
1599         // Find compatible VK formats for each GLSL vertex type
1600         CompatibleFormats compatibleFormats[VertexInputTest::GLSL_TYPE_COUNT];
1601         {
1602                 for (int glslTypeNdx = 0; glslTypeNdx < VertexInputTest::GLSL_TYPE_COUNT; glslTypeNdx++)
1603                 {
1604                         for (int formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(vertexFormats); formatNdx++)
1605                         {
1606                                 if (VertexInputTest::isCompatibleType(vertexFormats[formatNdx], (VertexInputTest::GlslType)glslTypeNdx))
1607                                         compatibleFormats[glslTypeNdx].compatibleVkFormats.push_back(vertexFormats[formatNdx]);
1608                         }
1609                 }
1610         }
1611
1612         de::Random                                              randomFunc                              (102030);
1613         GlslTypeCombinationsIterator    glslTypeCombinationsItr (VertexInputTest::GLSL_TYPE_DOUBLE, 3); // Exclude double values, which are not included in vertexFormats
1614         de::MovePtr<tcu::TestCaseGroup> oneToOneAttributeTests  (new tcu::TestCaseGroup(testCtx, "attributes", ""));
1615         de::MovePtr<tcu::TestCaseGroup> oneToManyAttributeTests (new tcu::TestCaseGroup(testCtx, "attributes", ""));
1616
1617         while (glslTypeCombinationsItr.hasNext())
1618         {
1619                 const std::vector<VertexInputTest::GlslType>    glslTypes               = glslTypeCombinationsItr.next();
1620                 std::vector<VertexInputTest::AttributeInfo>             attributeInfos  (glslTypes.size());
1621
1622                 for (size_t attributeNdx = 0; attributeNdx < attributeInfos.size(); attributeNdx++)
1623                 {
1624                         DE_ASSERT(!compatibleFormats[glslTypes[attributeNdx]].compatibleVkFormats.empty());
1625
1626                         // Select a random compatible format
1627                         const std::vector<VkFormat>& formats = compatibleFormats[glslTypes[attributeNdx]].compatibleVkFormats;
1628                         const VkFormat format = formats[randomFunc.getUint32() % formats.size()];
1629
1630                         attributeInfos[attributeNdx].glslType   = glslTypes[attributeNdx];
1631                         attributeInfos[attributeNdx].inputRate  = (attributeNdx % 2 == 0) ? VK_VERTEX_INPUT_RATE_VERTEX : VK_VERTEX_INPUT_RATE_INSTANCE;
1632                         attributeInfos[attributeNdx].vkType             = format;
1633                 }
1634
1635                 const std::string       caseName        = getAttributeInfosCaseName(attributeInfos);
1636                 const std::string       caseDesc        = getAttributeInfosDescription(attributeInfos);
1637
1638                 oneToOneAttributeTests->addChild(new VertexInputTest(testCtx, caseName, caseDesc, attributeInfos, VertexInputTest::BINDING_MAPPING_ONE_TO_ONE));
1639                 oneToManyAttributeTests->addChild(new VertexInputTest(testCtx, caseName, caseDesc, attributeInfos, VertexInputTest::BINDING_MAPPING_ONE_TO_MANY));
1640         }
1641
1642         de::MovePtr<tcu::TestCaseGroup> bindingOneToOneTests    (new tcu::TestCaseGroup(testCtx, "binding_one_to_one", "Each attribute uses a unique binding"));
1643         bindingOneToOneTests->addChild(oneToOneAttributeTests.release());
1644         multipleAttributeTests->addChild(bindingOneToOneTests.release());
1645
1646         de::MovePtr<tcu::TestCaseGroup> bindingOneToManyTests   (new tcu::TestCaseGroup(testCtx, "binding_one_to_many", "Attributes share the same binding"));
1647         bindingOneToManyTests->addChild(oneToManyAttributeTests.release());
1648         multipleAttributeTests->addChild(bindingOneToManyTests.release());
1649
1650         return multipleAttributeTests;
1651 }
1652
1653 } // anonymous
1654
1655 tcu::TestCaseGroup* createVertexInputTests (tcu::TestContext& testCtx)
1656 {
1657         de::MovePtr<tcu::TestCaseGroup> vertexInputTests (new tcu::TestCaseGroup(testCtx, "vertex_input", ""));
1658
1659         vertexInputTests->addChild(createSingleAttributeTests(testCtx).release());
1660         vertexInputTests->addChild(createMultipleAttributeTests(testCtx).release());
1661
1662         return vertexInputTests.release();
1663 }
1664
1665 } // pipeline
1666 } // vkt