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