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