Merge branch 'jekstrand_renderpass_transfer_bit_fix' into 'master'
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / draw / vktDrawIndexedTest.cpp
1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2015 The Khronos Group Inc.
6 * Copyright (c) 2015 Intel Corporation
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 Draw Indexed Tests
34 *//*--------------------------------------------------------------------*/
35
36 #include "vktDrawIndexedTest.hpp"
37
38 #include "vktTestCaseUtil.hpp"
39 #include "vktDrawTestCaseUtil.hpp"
40
41 #include "vktDrawBaseClass.hpp"
42
43 #include "tcuTestLog.hpp"
44 #include "tcuResource.hpp"
45 #include "tcuImageCompare.hpp"
46 #include "tcuTextureUtil.hpp"
47 #include "tcuRGBA.hpp"
48
49 #include "vkDefs.hpp"
50
51 namespace vkt
52 {
53 namespace Draw
54 {
55 namespace
56 {
57 class DrawIndexed : public DrawTestsBaseClass
58 {
59 public:
60                                                                 DrawIndexed                             (Context &context, ShaderMap shaders, vk::VkPrimitiveTopology topology);
61         virtual         tcu::TestStatus iterate                                 (void);
62 };
63
64 class DrawInstancedIndexed : public DrawIndexed
65 {
66 public:
67                                                                 DrawInstancedIndexed    (Context &context, ShaderMap shaders, vk::VkPrimitiveTopology topology);
68         virtual         tcu::TestStatus iterate                                 (void);
69 };
70
71 DrawIndexed::DrawIndexed (Context &context, ShaderMap shaders, vk::VkPrimitiveTopology topology)
72                 : DrawTestsBaseClass(context, shaders[glu::SHADERTYPE_VERTEX], shaders[glu::SHADERTYPE_FRAGMENT])
73 {
74         m_topology = topology;
75
76         /*0*/ m_data.push_back(PositionColorVertex(tcu::Vec4(   -0.3f,   0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
77         /*1*/ m_data.push_back(PositionColorVertex(tcu::Vec4(   -1.0f,   1.0f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
78         /*2*/ m_data.push_back(PositionColorVertex(tcu::Vec4(   -0.3f,  -0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
79         /*3*/ m_data.push_back(PositionColorVertex(tcu::Vec4(    1.0f,  -1.0f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
80         /*4*/ m_data.push_back(PositionColorVertex(tcu::Vec4(   -0.3f,  -0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
81         /*5*/ m_data.push_back(PositionColorVertex(tcu::Vec4(    0.3f,   0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
82         /*6*/ m_data.push_back(PositionColorVertex(tcu::Vec4(    0.3f,  -0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
83         /*7*/ m_data.push_back(PositionColorVertex(tcu::Vec4(    0.3f,   0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
84         /*8*/ m_data.push_back(PositionColorVertex(tcu::Vec4(   -1.0f,   1.0f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
85
86         switch (m_topology)
87         {
88                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
89                         m_indexes.push_back(0);
90                         m_indexes.push_back(0);
91                         m_indexes.push_back(2);
92                         m_indexes.push_back(0);
93                         m_indexes.push_back(6);
94                         m_indexes.push_back(6);
95                         m_indexes.push_back(0);
96                         m_indexes.push_back(7);
97                         break;
98                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
99                         m_indexes.push_back(0);
100                         m_indexes.push_back(0);
101                         m_indexes.push_back(2);
102                         m_indexes.push_back(0);
103                         m_indexes.push_back(6);
104                         m_indexes.push_back(5);
105                         m_indexes.push_back(0);
106                         m_indexes.push_back(7);
107                         break;
108
109                 case vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
110                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
111                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
112                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
113                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
114                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
115                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
116                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
117                 case vk::VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
118                 case vk::VK_PRIMITIVE_TOPOLOGY_LAST:
119                         DE_FATAL("Topology not implemented");
120                         break;
121                 default:
122                         DE_FATAL("Unknown topology");
123                         break;
124         }
125         initialize();
126 };
127
128 tcu::TestStatus DrawIndexed::iterate (void)
129 {
130         tcu::TestLog &log                       = m_context.getTestContext().getLog();
131         const vk::VkQueue queue         = m_context.getUniversalQueue();
132
133         beginRenderPass();
134
135         const vk::VkDeviceSize dataSize = m_indexes.size() * sizeof(deUint32);
136         m_indexBuffer = Buffer::createAndAlloc( m_vk, m_context.getDevice(),
137                                                                                         BufferCreateInfo(dataSize,
138                                                                                                                          vk::VK_BUFFER_USAGE_INDEX_BUFFER_BIT),
139                                                                                         m_context.getDefaultAllocator(),
140                                                                                         vk::MemoryRequirement::HostVisible);
141
142         deUint8* ptr = reinterpret_cast<deUint8*>(m_indexBuffer->getBoundMemory().getHostPtr());
143
144         deMemcpy(ptr, &m_indexes[0], static_cast<size_t>(dataSize));
145
146         vk::flushMappedMemoryRange(m_vk, m_context.getDevice(),
147                                                            m_vertexBuffer->getBoundMemory().getMemory(),
148                                                            m_vertexBuffer->getBoundMemory().getOffset(),
149                                                            dataSize);
150
151         const vk::VkDeviceSize vertexBufferOffset = 0;
152         const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
153         const vk::VkBuffer indexBuffer = m_indexBuffer->object();
154
155         m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
156         m_vk.cmdBindIndexBuffer(*m_cmdBuffer, indexBuffer, 0, vk::VK_INDEX_TYPE_UINT32);
157
158         m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
159
160         m_vk.cmdDrawIndexed(*m_cmdBuffer, 6, 1, 2, 0, 0);
161
162         m_vk.cmdEndRenderPass(*m_cmdBuffer);
163         m_vk.endCommandBuffer(*m_cmdBuffer);
164
165         vk::VkSubmitInfo submitInfo =
166         {
167                 vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,      // VkStructureType                      sType;
168                 DE_NULL,                                                        // const void*                          pNext;
169                 0,                                                                              // deUint32                                     waitSemaphoreCount;
170                 DE_NULL,                                                                // const VkSemaphore*           pWaitSemaphores;
171                 (const vk::VkPipelineStageFlags*)DE_NULL,
172                 1,                                                                              // deUint32                                     commandBufferCount;
173                 &m_cmdBuffer.get(),                                     // const VkCommandBuffer*       pCommandBuffers;
174                 0,                                                                              // deUint32                                     signalSemaphoreCount;
175                 DE_NULL                                                         // const VkSemaphore*           pSignalSemaphores;
176         };
177
178         VK_CHECK(m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL));
179
180         VK_CHECK(m_vk.queueWaitIdle(queue));
181
182         // Validation
183         tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
184                                                                   referenceFrame.allocLevel(0);
185
186         const deInt32 frameWidth        = referenceFrame.getWidth();
187         const deInt32 frameHeight       = referenceFrame.getHeight();
188
189         tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
190
191         ReferenceImageCoordinates refCoords;
192
193         for (int y = 0; y < frameHeight; y++)
194         {
195                 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
196
197                 for (int x = 0; x < frameWidth; x++)
198                 {
199                         const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
200
201                         if ((yCoord >= refCoords.bottom &&
202                                  yCoord <= refCoords.top        &&
203                                  xCoord >= refCoords.left       &&
204                                  xCoord <= refCoords.right))
205                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
206                 }
207         }
208
209         const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
210         const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
211                 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
212
213         qpTestResult res = QP_TEST_RESULT_PASS;
214
215         if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
216                 referenceFrame.getLevel(0), renderedFrame, 0.05f,
217                 tcu::COMPARE_LOG_RESULT)) {
218                 res = QP_TEST_RESULT_FAIL;
219         }
220
221         return tcu::TestStatus(res, qpGetTestResultName(res));
222 };
223
224 DrawInstancedIndexed::DrawInstancedIndexed (Context &context, ShaderMap shaders, vk::VkPrimitiveTopology topology)
225         : DrawIndexed   (context, shaders, topology)
226 {
227 }
228
229 tcu::TestStatus DrawInstancedIndexed::iterate (void)
230 {
231         tcu::TestLog &log               = m_context.getTestContext().getLog();
232         const vk::VkQueue queue = m_context.getUniversalQueue();
233
234         beginRenderPass();
235
236         const vk::VkDeviceSize dataSize = m_indexes.size() * sizeof(deUint32);
237         m_indexBuffer = Buffer::createAndAlloc( m_vk, m_context.getDevice(),
238                                                                                         BufferCreateInfo(dataSize,
239                                                                                                                          vk::VK_BUFFER_USAGE_INDEX_BUFFER_BIT),
240                                                                                         m_context.getDefaultAllocator(),
241                                                                                         vk::MemoryRequirement::HostVisible);
242
243         deUint8* ptr = reinterpret_cast<deUint8*>(m_indexBuffer->getBoundMemory().getHostPtr());
244
245         deMemcpy(ptr, &m_indexes[0], static_cast<size_t>(dataSize));
246         vk::flushMappedMemoryRange(m_vk, m_context.getDevice(),
247                                                            m_vertexBuffer->getBoundMemory().getMemory(),
248                                                            m_vertexBuffer->getBoundMemory().getOffset(),
249                                                            dataSize);
250
251         const vk::VkDeviceSize vertexBufferOffset = 0;
252         const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
253         const vk::VkBuffer indexBuffer = m_indexBuffer->object();
254
255         m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
256         m_vk.cmdBindIndexBuffer(*m_cmdBuffer, indexBuffer, 0, vk::VK_INDEX_TYPE_UINT32);
257         m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
258
259         switch (m_topology)
260         {
261                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
262                         m_vk.cmdDrawIndexed(*m_cmdBuffer, 6, 4, 2, 0, 2);
263                         break;
264                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
265                         m_vk.cmdDrawIndexed(*m_cmdBuffer, 4, 4, 2, 0, 2);
266                         break;
267                 case vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
268                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
269                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
270                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
271                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
272                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
273                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
274                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
275                 case vk::VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
276                 case vk::VK_PRIMITIVE_TOPOLOGY_LAST:
277                         DE_FATAL("Topology not implemented");
278                         break;
279                 default:
280                         DE_FATAL("Unknown topology");
281                         break;
282         }
283
284         m_vk.cmdEndRenderPass(*m_cmdBuffer);
285         m_vk.endCommandBuffer(*m_cmdBuffer);
286
287         vk::VkSubmitInfo submitInfo =
288         {
289                 vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,      // VkStructureType                      sType;
290                 DE_NULL,                                                        // const void*                          pNext;
291                 0,                                                                              // deUint32                                     waitSemaphoreCount;
292                 DE_NULL,                                                                // const VkSemaphore*           pWaitSemaphores;
293                 (const vk::VkPipelineStageFlags*)DE_NULL,
294                 1,                                                                              // deUint32                                     commandBufferCount;
295                 &m_cmdBuffer.get(),                                     // const VkCommandBuffer*       pCommandBuffers;
296                 0,                                                                              // deUint32                                     signalSemaphoreCount;
297                 DE_NULL                                                         // const VkSemaphore*           pSignalSemaphores;
298         };
299         VK_CHECK(m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL));
300
301         VK_CHECK(m_vk.queueWaitIdle(queue));
302
303         // Validation
304         VK_CHECK(m_vk.queueWaitIdle(queue));
305
306         tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
307         referenceFrame.allocLevel(0);
308
309         const deInt32 frameWidth = referenceFrame.getWidth();
310         const deInt32 frameHeight = referenceFrame.getHeight();
311
312         tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
313
314         ReferenceImageInstancedCoordinates refInstancedCoords;
315
316         for (int y = 0; y < frameHeight; y++)
317         {
318                 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
319
320                 for (int x = 0; x < frameWidth; x++)
321                 {
322                         const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
323
324                         if ((yCoord >= refInstancedCoords.bottom        &&
325                                  yCoord <= refInstancedCoords.top               &&
326                                  xCoord >= refInstancedCoords.left              &&
327                                  xCoord <= refInstancedCoords.right))
328                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
329                 }
330         }
331
332         const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
333         const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
334                 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
335
336         qpTestResult res = QP_TEST_RESULT_PASS;
337
338         if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
339                 referenceFrame.getLevel(0), renderedFrame, 0.05f,
340                 tcu::COMPARE_LOG_RESULT)) {
341                 res = QP_TEST_RESULT_FAIL;
342         }
343
344         return tcu::TestStatus(res, qpGetTestResultName(res));
345
346 }
347
348 }       // anonymous
349
350 DrawIndexedTests::DrawIndexedTests (tcu::TestContext &testCtx)
351         : TestCaseGroup (testCtx, "indexed_draw", "drawing indexed geometry")
352 {
353         /* Left blank on purpose */
354 }
355
356 DrawIndexedTests::~DrawIndexedTests (void) {}
357
358 void DrawIndexedTests::init (void)
359 {
360         ShaderMap shaderPaths;
361         shaderPaths[glu::SHADERTYPE_VERTEX] = "vulkan/draw/VertexFetch.vert";
362         shaderPaths[glu::SHADERTYPE_FRAGMENT] = "vulkan/draw/VertexFetch.frag";
363
364         addChild(new InstanceFactory<DrawIndexed>(m_testCtx, "draw_indexed_triangle_list", "Draws indexed triangle list", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST));
365         addChild(new InstanceFactory<DrawIndexed>(m_testCtx, "draw_indexed_triangle_strip", "Draws indexed triangle strip", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP));
366
367         shaderPaths[glu::SHADERTYPE_VERTEX] = "vulkan/draw/VertexFetchWithInstance.vert";
368         shaderPaths[glu::SHADERTYPE_FRAGMENT] = "vulkan/draw/VertexFetch.frag";
369
370         addChild(new InstanceFactory<DrawInstancedIndexed>(m_testCtx, "draw_instanced_indexed_triangle_list", "Draws indexed triangle list", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST));
371         addChild(new InstanceFactory<DrawInstancedIndexed>(m_testCtx, "draw_instanced_indexed_triangle_strip", "Draws indexed triangle strip", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP));
372 }
373
374 }       // DrawTests
375 }       // vkt