dEQP-VK.renderpass: Set IMAGE_USAGE_TRANSFER_SRC_BIT when needed
[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                 1,                                                                              // deUint32                                     commandBufferCount;
172                 &m_cmdBuffer.get(),                                     // const VkCommandBuffer*       pCommandBuffers;
173                 0,                                                                              // deUint32                                     signalSemaphoreCount;
174                 DE_NULL                                                         // const VkSemaphore*           pSignalSemaphores;
175         };
176
177         VK_CHECK(m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL));
178
179         VK_CHECK(m_vk.queueWaitIdle(queue));
180
181         // Validation
182         tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
183                                                                   referenceFrame.allocLevel(0);
184
185         const deInt32 frameWidth        = referenceFrame.getWidth();
186         const deInt32 frameHeight       = referenceFrame.getHeight();
187
188         tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
189
190         ReferenceImageCoordinates refCoords;
191
192         for (int y = 0; y < frameHeight; y++)
193         {
194                 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
195
196                 for (int x = 0; x < frameWidth; x++)
197                 {
198                         const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
199
200                         if ((yCoord >= refCoords.bottom &&
201                                  yCoord <= refCoords.top        &&
202                                  xCoord >= refCoords.left       &&
203                                  xCoord <= refCoords.right))
204                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
205                 }
206         }
207
208         const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
209         const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
210                 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
211
212         qpTestResult res = QP_TEST_RESULT_PASS;
213
214         if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
215                 referenceFrame.getLevel(0), renderedFrame, 0.05f,
216                 tcu::COMPARE_LOG_RESULT)) {
217                 res = QP_TEST_RESULT_FAIL;
218         }
219
220         return tcu::TestStatus(res, qpGetTestResultName(res));
221 };
222
223 DrawInstancedIndexed::DrawInstancedIndexed (Context &context, ShaderMap shaders, vk::VkPrimitiveTopology topology)
224         : DrawIndexed   (context, shaders, topology)
225 {
226 }
227
228 tcu::TestStatus DrawInstancedIndexed::iterate (void)
229 {
230         tcu::TestLog &log               = m_context.getTestContext().getLog();
231         const vk::VkQueue queue = m_context.getUniversalQueue();
232
233         beginRenderPass();
234
235         const vk::VkDeviceSize dataSize = m_indexes.size() * sizeof(deUint32);
236         m_indexBuffer = Buffer::createAndAlloc( m_vk, m_context.getDevice(),
237                                                                                         BufferCreateInfo(dataSize,
238                                                                                                                          vk::VK_BUFFER_USAGE_INDEX_BUFFER_BIT),
239                                                                                         m_context.getDefaultAllocator(),
240                                                                                         vk::MemoryRequirement::HostVisible);
241
242         deUint8* ptr = reinterpret_cast<deUint8*>(m_indexBuffer->getBoundMemory().getHostPtr());
243
244         deMemcpy(ptr, &m_indexes[0], static_cast<size_t>(dataSize));
245         vk::flushMappedMemoryRange(m_vk, m_context.getDevice(),
246                                                            m_vertexBuffer->getBoundMemory().getMemory(),
247                                                            m_vertexBuffer->getBoundMemory().getOffset(),
248                                                            dataSize);
249
250         const vk::VkDeviceSize vertexBufferOffset = 0;
251         const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
252         const vk::VkBuffer indexBuffer = m_indexBuffer->object();
253
254         m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
255         m_vk.cmdBindIndexBuffer(*m_cmdBuffer, indexBuffer, 0, vk::VK_INDEX_TYPE_UINT32);
256         m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
257
258         switch (m_topology)
259         {
260                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
261                         m_vk.cmdDrawIndexed(*m_cmdBuffer, 6, 4, 2, 0, 2);
262                         break;
263                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
264                         m_vk.cmdDrawIndexed(*m_cmdBuffer, 4, 4, 2, 0, 2);
265                         break;
266                 case vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
267                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
268                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
269                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
270                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
271                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
272                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
273                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
274                 case vk::VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
275                 case vk::VK_PRIMITIVE_TOPOLOGY_LAST:
276                         DE_FATAL("Topology not implemented");
277                         break;
278                 default:
279                         DE_FATAL("Unknown topology");
280                         break;
281         }
282
283         m_vk.cmdEndRenderPass(*m_cmdBuffer);
284         m_vk.endCommandBuffer(*m_cmdBuffer);
285
286         vk::VkSubmitInfo submitInfo =
287         {
288                 vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,      // VkStructureType                      sType;
289                 DE_NULL,                                                        // const void*                          pNext;
290                 0,                                                                              // deUint32                                     waitSemaphoreCount;
291                 DE_NULL,                                                                // const VkSemaphore*           pWaitSemaphores;
292                 1,                                                                              // deUint32                                     commandBufferCount;
293                 &m_cmdBuffer.get(),                                     // const VkCommandBuffer*       pCommandBuffers;
294                 0,                                                                              // deUint32                                     signalSemaphoreCount;
295                 DE_NULL                                                         // const VkSemaphore*           pSignalSemaphores;
296         };
297         VK_CHECK(m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL));
298
299         VK_CHECK(m_vk.queueWaitIdle(queue));
300
301         // Validation
302         VK_CHECK(m_vk.queueWaitIdle(queue));
303
304         tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
305         referenceFrame.allocLevel(0);
306
307         const deInt32 frameWidth = referenceFrame.getWidth();
308         const deInt32 frameHeight = referenceFrame.getHeight();
309
310         tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
311
312         ReferenceImageInstancedCoordinates refInstancedCoords;
313
314         for (int y = 0; y < frameHeight; y++)
315         {
316                 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
317
318                 for (int x = 0; x < frameWidth; x++)
319                 {
320                         const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
321
322                         if ((yCoord >= refInstancedCoords.bottom        &&
323                                  yCoord <= refInstancedCoords.top               &&
324                                  xCoord >= refInstancedCoords.left              &&
325                                  xCoord <= refInstancedCoords.right))
326                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
327                 }
328         }
329
330         const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
331         const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
332                 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
333
334         qpTestResult res = QP_TEST_RESULT_PASS;
335
336         if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
337                 referenceFrame.getLevel(0), renderedFrame, 0.05f,
338                 tcu::COMPARE_LOG_RESULT)) {
339                 res = QP_TEST_RESULT_FAIL;
340         }
341
342         return tcu::TestStatus(res, qpGetTestResultName(res));
343
344 }
345
346 }       // anonymous
347
348 DrawIndexedTests::DrawIndexedTests (tcu::TestContext &testCtx)
349         : TestCaseGroup (testCtx, "indexed_draw", "drawing indexed geometry")
350 {
351         /* Left blank on purpose */
352 }
353
354 DrawIndexedTests::~DrawIndexedTests (void) {}
355
356 void DrawIndexedTests::init (void)
357 {
358         ShaderMap shaderPaths;
359         shaderPaths[glu::SHADERTYPE_VERTEX] = "vulkan/draw/VertexFetch.vert";
360         shaderPaths[glu::SHADERTYPE_FRAGMENT] = "vulkan/draw/VertexFetch.frag";
361
362         addChild(new InstanceFactory<DrawIndexed>(m_testCtx, "draw_indexed_triangle_list", "Draws indexed triangle list", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST));
363         addChild(new InstanceFactory<DrawIndexed>(m_testCtx, "draw_indexed_triangle_strip", "Draws indexed triangle strip", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP));
364
365         shaderPaths[glu::SHADERTYPE_VERTEX] = "vulkan/draw/VertexFetchWithInstance.vert";
366         shaderPaths[glu::SHADERTYPE_FRAGMENT] = "vulkan/draw/VertexFetch.frag";
367
368         addChild(new InstanceFactory<DrawInstancedIndexed>(m_testCtx, "draw_instanced_indexed_triangle_list", "Draws indexed triangle list", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST));
369         addChild(new InstanceFactory<DrawInstancedIndexed>(m_testCtx, "draw_instanced_indexed_triangle_strip", "Draws indexed triangle strip", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP));
370 }
371
372 }       // DrawTests
373 }       // vkt