dEQP-VK.renderpass: Set IMAGE_USAGE_TRANSFER_SRC_BIT when needed
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / draw / vktDrawSimpleTest.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 Simple Draw Tests
34 *//*--------------------------------------------------------------------*/
35
36 #include "vktDrawSimpleTest.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 SimpleDraw : public DrawTestsBaseClass
58 {
59 public:
60                                                         SimpleDraw                              (Context &context, ShaderMap shaders, vk::VkPrimitiveTopology topology);
61         virtual tcu::TestStatus iterate                                 (void);
62 };
63
64 class SimpleDrawInstanced : public SimpleDraw
65 {
66 public:
67                                                         SimpleDrawInstanced             (Context &context, ShaderMap shaders, vk::VkPrimitiveTopology topology);
68         tcu::TestStatus                 iterate                                 (void);
69 };
70
71 SimpleDraw::SimpleDraw (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         switch (m_topology)
77         {
78                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
79                         m_data.push_back(PositionColorVertex(tcu::Vec4(  1.0f,  -1.0f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
80                         m_data.push_back(PositionColorVertex(tcu::Vec4( -1.0f,   1.0f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
81                         m_data.push_back(PositionColorVertex(tcu::Vec4( -0.3f,  -0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
82                         m_data.push_back(PositionColorVertex(tcu::Vec4( -0.3f,   0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
83                         m_data.push_back(PositionColorVertex(tcu::Vec4(  0.3f,  -0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
84                         m_data.push_back(PositionColorVertex(tcu::Vec4(  0.3f,  -0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
85                         m_data.push_back(PositionColorVertex(tcu::Vec4(  0.3f,   0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
86                         m_data.push_back(PositionColorVertex(tcu::Vec4( -0.3f,   0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
87                         m_data.push_back(PositionColorVertex(tcu::Vec4( -1.0f,   1.0f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
88                         break;
89                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
90                         m_data.push_back(PositionColorVertex(tcu::Vec4(  1.0f,  -1.0f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
91                         m_data.push_back(PositionColorVertex(tcu::Vec4( -1.0f,   1.0f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
92                         m_data.push_back(PositionColorVertex(tcu::Vec4( -0.3f,  -0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
93                         m_data.push_back(PositionColorVertex(tcu::Vec4( -0.3f,   0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
94                         m_data.push_back(PositionColorVertex(tcu::Vec4(  0.3f,  -0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
95                         m_data.push_back(PositionColorVertex(tcu::Vec4(  0.3f,   0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
96                         m_data.push_back(PositionColorVertex(tcu::Vec4( -0.3f,   0.3f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
97                         m_data.push_back(PositionColorVertex(tcu::Vec4( -1.0f,   1.0f,  1.0f,   1.0f), tcu::RGBA::blue().toVec()));
98                         break;
99                 case vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
100                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
101                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
102                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
103                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
104                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
105                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
106                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
107                 case vk::VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
108                 case vk::VK_PRIMITIVE_TOPOLOGY_LAST:
109                         DE_FATAL("Topology not implemented");
110                         break;
111                 default:
112                         DE_FATAL("Unknown topology");
113                         break;
114         }
115         initialize();
116 }
117
118 tcu::TestStatus SimpleDraw::iterate (void)
119 {
120         tcu::TestLog &log                                                       = m_context.getTestContext().getLog();
121         const vk::VkQueue queue                                         = m_context.getUniversalQueue();
122
123         beginRenderPass();
124
125         const vk::VkDeviceSize vertexBufferOffset       = 0;
126         const vk::VkBuffer vertexBuffer                         = m_vertexBuffer->object();
127
128         m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
129         m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
130
131         switch (m_topology)
132         {
133                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
134                         m_vk.cmdDraw(*m_cmdBuffer, 6, 1, 2, 0);
135                         break;
136                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
137                         m_vk.cmdDraw(*m_cmdBuffer, 4, 1, 2, 0);
138                         break;
139                 case vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
140                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
141                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
142                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
143                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
144                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
145                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
146                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
147                 case vk::VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
148                 case vk::VK_PRIMITIVE_TOPOLOGY_LAST:
149                         DE_FATAL("Topology not implemented");
150                         break;
151                 default:
152                         DE_FATAL("Unknown topology");
153                         break;
154         }
155
156         m_vk.cmdEndRenderPass(*m_cmdBuffer);
157         m_vk.endCommandBuffer(*m_cmdBuffer);
158
159         vk::VkSubmitInfo submitInfo =
160         {
161                 vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,      // VkStructureType                      sType;
162                 DE_NULL,                                                        // const void*                          pNext;
163                 0,                                                                              // deUint32                                     waitSemaphoreCount;
164                 DE_NULL,                                                                // const VkSemaphore*           pWaitSemaphores;
165                 1,                                                                              // deUint32                                     commandBufferCount;
166                 &m_cmdBuffer.get(),                                     // const VkCommandBuffer*       pCommandBuffers;
167                 0,                                                                              // deUint32                                     signalSemaphoreCount;
168                 DE_NULL                                                         // const VkSemaphore*           pSignalSemaphores;
169         };
170         VK_CHECK(m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL));
171
172         VK_CHECK(m_vk.queueWaitIdle(queue));
173
174         // Validation
175         tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
176
177         referenceFrame.allocLevel(0);
178
179         const deInt32 frameWidth        = referenceFrame.getWidth();
180         const deInt32 frameHeight       = referenceFrame.getHeight();
181
182         tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
183
184         ReferenceImageCoordinates refCoords;
185
186         for (int y = 0; y < frameHeight; y++)
187         {
188                 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
189
190                 for (int x = 0; x < frameWidth; x++)
191                 {
192                         const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
193
194                         if ((yCoord >= refCoords.bottom &&
195                                  yCoord <= refCoords.top        &&
196                                  xCoord >= refCoords.left       &&
197                                  xCoord <= refCoords.right))
198                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
199                 }
200         }
201
202         const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
203         const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
204                 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
205
206         qpTestResult res = QP_TEST_RESULT_PASS;
207
208         if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
209                 referenceFrame.getLevel(0), renderedFrame, 0.05f,
210                 tcu::COMPARE_LOG_RESULT)) {
211                 res = QP_TEST_RESULT_FAIL;
212         }
213
214         return tcu::TestStatus(res, qpGetTestResultName(res));
215
216 }
217
218 SimpleDrawInstanced::SimpleDrawInstanced (Context &context, ShaderMap shaders, vk::VkPrimitiveTopology topology)
219         : SimpleDraw    (context, shaders, topology) {}
220
221 tcu::TestStatus SimpleDrawInstanced::iterate (void)
222 {
223         tcu::TestLog &log               = m_context.getTestContext().getLog();
224
225         const vk::VkQueue queue = m_context.getUniversalQueue();
226
227         beginRenderPass();
228
229         const vk::VkDeviceSize vertexBufferOffset = 0;
230         const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
231
232         m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
233
234         m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
235
236         switch (m_topology)
237         {
238                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
239                         m_vk.cmdDraw(*m_cmdBuffer, 6, 4, 2, 2);
240                         break;
241                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
242                         m_vk.cmdDraw(*m_cmdBuffer, 4, 4, 2, 2);
243                         break;
244                 case vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
245                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
246                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
247                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
248                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
249                 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
250                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
251                 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
252                 case vk::VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
253                 case vk::VK_PRIMITIVE_TOPOLOGY_LAST:
254                         DE_FATAL("Topology not implemented");
255                         break;
256                 default:
257                         DE_FATAL("Unknown topology");
258                         break;
259         }
260
261         m_vk.cmdEndRenderPass(*m_cmdBuffer);
262         m_vk.endCommandBuffer(*m_cmdBuffer);
263
264         vk::VkSubmitInfo submitInfo =
265         {
266                 vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,      // VkStructureType                      sType;
267                 DE_NULL,                                                        // const void*                          pNext;
268                 0,                                                                              // deUint32                                     waitSemaphoreCount;
269                 DE_NULL,                                                                // const VkSemaphore*           pWaitSemaphores;
270                 1,                                                                              // deUint32                                     commandBufferCount;
271                 &m_cmdBuffer.get(),                                     // const VkCommandBuffer*       pCommandBuffers;
272                 0,                                                                              // deUint32                                     signalSemaphoreCount;
273                 DE_NULL                                                         // const VkSemaphore*           pSignalSemaphores;
274         };
275         VK_CHECK(m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL));
276
277         VK_CHECK(m_vk.queueWaitIdle(queue));
278
279         // Validation
280         VK_CHECK(m_vk.queueWaitIdle(queue));
281
282         tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
283
284         referenceFrame.allocLevel(0);
285
286         const deInt32 frameWidth        = referenceFrame.getWidth();
287         const deInt32 frameHeight       = referenceFrame.getHeight();
288
289         tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
290
291         ReferenceImageInstancedCoordinates refInstancedCoords;
292
293         for (int y = 0; y < frameHeight; y++)
294         {
295                 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
296
297                 for (int x = 0; x < frameWidth; x++)
298                 {
299                         const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
300
301                         if ((yCoord >= refInstancedCoords.bottom        &&
302                                  yCoord <= refInstancedCoords.top               &&
303                                  xCoord >= refInstancedCoords.left              &&
304                                  xCoord <= refInstancedCoords.right))
305                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
306                 }
307         }
308
309         const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
310         const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
311                 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
312
313         qpTestResult res = QP_TEST_RESULT_PASS;
314
315         if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
316                 referenceFrame.getLevel(0), renderedFrame, 0.05f,
317                 tcu::COMPARE_LOG_RESULT)) {
318                 res = QP_TEST_RESULT_FAIL;
319         }
320
321         return tcu::TestStatus(res, qpGetTestResultName(res));
322 }
323
324 }       // anonymous
325
326 SimpleDrawTests::SimpleDrawTests (tcu::TestContext &testCtx)
327 : TestCaseGroup (testCtx, "simple_draw", "drawing simple geometry")
328 {
329         /* Left blank on purpose */
330 }
331
332 SimpleDrawTests::~SimpleDrawTests (void) {}
333
334
335 void SimpleDrawTests::init (void)
336 {
337         ShaderMap shaderPaths;
338         shaderPaths[glu::SHADERTYPE_VERTEX]             = "vulkan/draw/VertexFetch.vert";
339         shaderPaths[glu::SHADERTYPE_FRAGMENT]   = "vulkan/draw/VertexFetch.frag";
340
341         addChild(new InstanceFactory<SimpleDraw>(m_testCtx, "simple_draw_triangle_list", "Draws triangle list", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST));
342         addChild(new InstanceFactory<SimpleDraw>(m_testCtx, "simple_draw_triangle_strip", "Draws triangle strip", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP));
343
344         shaderPaths[glu::SHADERTYPE_VERTEX]             = "vulkan/draw/VertexFetchWithInstance.vert";
345         shaderPaths[glu::SHADERTYPE_FRAGMENT]   = "vulkan/draw/VertexFetch.frag";
346
347         addChild(new InstanceFactory<SimpleDrawInstanced>(m_testCtx, "simple_draw_instanced_triangle_list", "Draws an instanced triangle list", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST));
348         addChild(new InstanceFactory<SimpleDrawInstanced>(m_testCtx, "simple_draw_instanced_triangle_strip", "Draws an instanced triangle strip", shaderPaths, vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP));
349 }
350
351 }       // DrawTests
352 }       // vkt