1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2015 The Khronos Group Inc.
6 * Copyright (c) 2015 Intel Corporation
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 * \brief Draw Indexed Tests
23 *//*--------------------------------------------------------------------*/
25 #include "vktDrawIndexedTest.hpp"
27 #include "vktTestCaseUtil.hpp"
28 #include "vktDrawTestCaseUtil.hpp"
30 #include "vktDrawBaseClass.hpp"
32 #include "tcuTestLog.hpp"
33 #include "tcuResource.hpp"
34 #include "tcuImageCompare.hpp"
35 #include "tcuTextureUtil.hpp"
36 #include "tcuRGBA.hpp"
51 class DrawIndexed : public DrawTestsBaseClass
54 typedef TestSpecBase TestSpec;
56 DrawIndexed (Context &context, TestSpec testSpec);
57 virtual tcu::TestStatus iterate (void);
59 std::vector<deUint32> m_indexes;
60 de::SharedPtr<Buffer> m_indexBuffer;
63 class DrawInstancedIndexed : public DrawIndexed
66 DrawInstancedIndexed (Context &context, TestSpec testSpec);
67 virtual tcu::TestStatus iterate (void);
70 DrawIndexed::DrawIndexed (Context &context, TestSpec testSpec)
71 : DrawTestsBaseClass(context, testSpec.shaders[glu::SHADERTYPE_VERTEX], testSpec.shaders[glu::SHADERTYPE_FRAGMENT], testSpec.topology)
75 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
76 m_indexes.push_back(0);
77 m_indexes.push_back(0);
78 m_indexes.push_back(2);
79 m_indexes.push_back(0);
80 m_indexes.push_back(6);
81 m_indexes.push_back(6);
82 m_indexes.push_back(0);
83 m_indexes.push_back(7);
85 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
86 m_indexes.push_back(0);
87 m_indexes.push_back(0);
88 m_indexes.push_back(2);
89 m_indexes.push_back(0);
90 m_indexes.push_back(6);
91 m_indexes.push_back(5);
92 m_indexes.push_back(0);
93 m_indexes.push_back(7);
96 case vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
97 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
98 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
99 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
100 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
101 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
102 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
103 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
104 case vk::VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
105 case vk::VK_PRIMITIVE_TOPOLOGY_LAST:
106 DE_FATAL("Topology not implemented");
109 DE_FATAL("Unknown topology");
113 for (int unusedIdx = 0; unusedIdx < VERTEX_OFFSET; unusedIdx++)
115 m_data.push_back(VertexElementData(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), -1));
118 int vertexIndex = VERTEX_OFFSET;
120 m_data.push_back(VertexElementData(tcu::Vec4( -0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), vertexIndex++));
121 m_data.push_back(VertexElementData(tcu::Vec4( -1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), vertexIndex++));
122 m_data.push_back(VertexElementData(tcu::Vec4( -0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), vertexIndex++));
123 m_data.push_back(VertexElementData(tcu::Vec4( 1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), vertexIndex++));
124 m_data.push_back(VertexElementData(tcu::Vec4( -0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), vertexIndex++));
125 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), vertexIndex++));
126 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), vertexIndex++));
127 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), vertexIndex++));
129 m_data.push_back(VertexElementData(tcu::Vec4( -1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), -1));
134 tcu::TestStatus DrawIndexed::iterate (void)
136 tcu::TestLog &log = m_context.getTestContext().getLog();
137 const vk::VkQueue queue = m_context.getUniversalQueue();
141 const vk::VkDeviceSize dataSize = m_indexes.size() * sizeof(deUint32);
142 m_indexBuffer = Buffer::createAndAlloc( m_vk, m_context.getDevice(),
143 BufferCreateInfo(dataSize,
144 vk::VK_BUFFER_USAGE_INDEX_BUFFER_BIT),
145 m_context.getDefaultAllocator(),
146 vk::MemoryRequirement::HostVisible);
148 deUint8* ptr = reinterpret_cast<deUint8*>(m_indexBuffer->getBoundMemory().getHostPtr());
150 deMemcpy(ptr, &m_indexes[0], static_cast<size_t>(dataSize));
152 vk::flushMappedMemoryRange(m_vk, m_context.getDevice(),
153 m_indexBuffer->getBoundMemory().getMemory(),
154 m_indexBuffer->getBoundMemory().getOffset(),
157 const vk::VkDeviceSize vertexBufferOffset = 0;
158 const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
159 const vk::VkBuffer indexBuffer = m_indexBuffer->object();
161 m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
162 m_vk.cmdBindIndexBuffer(*m_cmdBuffer, indexBuffer, 0, vk::VK_INDEX_TYPE_UINT32);
164 m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
166 m_vk.cmdDrawIndexed(*m_cmdBuffer, 6, 1, 2, VERTEX_OFFSET, 0);
168 m_vk.cmdEndRenderPass(*m_cmdBuffer);
169 m_vk.endCommandBuffer(*m_cmdBuffer);
171 vk::VkSubmitInfo submitInfo =
173 vk::VK_STRUCTURE_TYPE_SUBMIT_INFO, // VkStructureType sType;
174 DE_NULL, // const void* pNext;
175 0, // deUint32 waitSemaphoreCount;
176 DE_NULL, // const VkSemaphore* pWaitSemaphores;
177 (const vk::VkPipelineStageFlags*)DE_NULL,
178 1, // deUint32 commandBufferCount;
179 &m_cmdBuffer.get(), // const VkCommandBuffer* pCommandBuffers;
180 0, // deUint32 signalSemaphoreCount;
181 DE_NULL // const VkSemaphore* pSignalSemaphores;
184 VK_CHECK(m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL));
186 VK_CHECK(m_vk.queueWaitIdle(queue));
189 tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
190 referenceFrame.allocLevel(0);
192 const deInt32 frameWidth = referenceFrame.getWidth();
193 const deInt32 frameHeight = referenceFrame.getHeight();
195 tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
197 ReferenceImageCoordinates refCoords;
199 for (int y = 0; y < frameHeight; y++)
201 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
203 for (int x = 0; x < frameWidth; x++)
205 const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
207 if ((yCoord >= refCoords.bottom &&
208 yCoord <= refCoords.top &&
209 xCoord >= refCoords.left &&
210 xCoord <= refCoords.right))
211 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
215 const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
216 const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
217 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
219 qpTestResult res = QP_TEST_RESULT_PASS;
221 if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
222 referenceFrame.getLevel(0), renderedFrame, 0.05f,
223 tcu::COMPARE_LOG_RESULT)) {
224 res = QP_TEST_RESULT_FAIL;
227 return tcu::TestStatus(res, qpGetTestResultName(res));
230 DrawInstancedIndexed::DrawInstancedIndexed (Context &context, TestSpec testSpec)
231 : DrawIndexed (context, testSpec)
235 tcu::TestStatus DrawInstancedIndexed::iterate (void)
237 tcu::TestLog &log = m_context.getTestContext().getLog();
238 const vk::VkQueue queue = m_context.getUniversalQueue();
242 const vk::VkDeviceSize dataSize = m_indexes.size() * sizeof(deUint32);
243 m_indexBuffer = Buffer::createAndAlloc( m_vk, m_context.getDevice(),
244 BufferCreateInfo(dataSize,
245 vk::VK_BUFFER_USAGE_INDEX_BUFFER_BIT),
246 m_context.getDefaultAllocator(),
247 vk::MemoryRequirement::HostVisible);
249 deUint8* ptr = reinterpret_cast<deUint8*>(m_indexBuffer->getBoundMemory().getHostPtr());
251 deMemcpy(ptr, &m_indexes[0], static_cast<size_t>(dataSize));
252 vk::flushMappedMemoryRange(m_vk, m_context.getDevice(),
253 m_indexBuffer->getBoundMemory().getMemory(),
254 m_indexBuffer->getBoundMemory().getOffset(),
257 const vk::VkDeviceSize vertexBufferOffset = 0;
258 const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
259 const vk::VkBuffer indexBuffer = m_indexBuffer->object();
261 m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
262 m_vk.cmdBindIndexBuffer(*m_cmdBuffer, indexBuffer, 0, vk::VK_INDEX_TYPE_UINT32);
263 m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
267 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
268 m_vk.cmdDrawIndexed(*m_cmdBuffer, 6, 4, 2, VERTEX_OFFSET, 2);
270 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
271 m_vk.cmdDrawIndexed(*m_cmdBuffer, 4, 4, 2, VERTEX_OFFSET, 2);
273 case vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
274 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
275 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
276 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
277 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
278 case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
279 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
280 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
281 case vk::VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
282 case vk::VK_PRIMITIVE_TOPOLOGY_LAST:
283 DE_FATAL("Topology not implemented");
286 DE_FATAL("Unknown topology");
290 m_vk.cmdEndRenderPass(*m_cmdBuffer);
291 m_vk.endCommandBuffer(*m_cmdBuffer);
293 vk::VkSubmitInfo submitInfo =
295 vk::VK_STRUCTURE_TYPE_SUBMIT_INFO, // VkStructureType sType;
296 DE_NULL, // const void* pNext;
297 0, // deUint32 waitSemaphoreCount;
298 DE_NULL, // const VkSemaphore* pWaitSemaphores;
299 (const vk::VkPipelineStageFlags*)DE_NULL,
300 1, // deUint32 commandBufferCount;
301 &m_cmdBuffer.get(), // const VkCommandBuffer* pCommandBuffers;
302 0, // deUint32 signalSemaphoreCount;
303 DE_NULL // const VkSemaphore* pSignalSemaphores;
305 VK_CHECK(m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL));
307 VK_CHECK(m_vk.queueWaitIdle(queue));
310 VK_CHECK(m_vk.queueWaitIdle(queue));
312 tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
313 referenceFrame.allocLevel(0);
315 const deInt32 frameWidth = referenceFrame.getWidth();
316 const deInt32 frameHeight = referenceFrame.getHeight();
318 tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
320 ReferenceImageInstancedCoordinates refInstancedCoords;
322 for (int y = 0; y < frameHeight; y++)
324 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
326 for (int x = 0; x < frameWidth; x++)
328 const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
330 if ((yCoord >= refInstancedCoords.bottom &&
331 yCoord <= refInstancedCoords.top &&
332 xCoord >= refInstancedCoords.left &&
333 xCoord <= refInstancedCoords.right))
334 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
338 const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
339 const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
340 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
342 qpTestResult res = QP_TEST_RESULT_PASS;
344 if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
345 referenceFrame.getLevel(0), renderedFrame, 0.05f,
346 tcu::COMPARE_LOG_RESULT)) {
347 res = QP_TEST_RESULT_FAIL;
350 return tcu::TestStatus(res, qpGetTestResultName(res));
356 DrawIndexedTests::DrawIndexedTests (tcu::TestContext &testCtx)
357 : TestCaseGroup (testCtx, "indexed_draw", "drawing indexed geometry")
359 /* Left blank on purpose */
362 DrawIndexedTests::~DrawIndexedTests (void) {}
364 void DrawIndexedTests::init (void)
367 DrawIndexed::TestSpec testSpec;
368 testSpec.shaders[glu::SHADERTYPE_VERTEX] = "vulkan/draw/VertexFetch.vert";
369 testSpec.shaders[glu::SHADERTYPE_FRAGMENT] = "vulkan/draw/VertexFetch.frag";
371 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
372 addChild(new InstanceFactory<DrawIndexed>(m_testCtx, "draw_indexed_triangle_list", "Draws indexed triangle list", testSpec));
373 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
374 addChild(new InstanceFactory<DrawIndexed>(m_testCtx, "draw_indexed_triangle_strip", "Draws indexed triangle strip", testSpec));
377 DrawInstancedIndexed::TestSpec testSpec;
378 testSpec.shaders[glu::SHADERTYPE_VERTEX] = "vulkan/draw/VertexFetchInstancedFirstInstance.vert";
379 testSpec.shaders[glu::SHADERTYPE_FRAGMENT] = "vulkan/draw/VertexFetch.frag";
381 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
382 addChild(new InstanceFactory<DrawInstancedIndexed>(m_testCtx, "draw_instanced_indexed_triangle_list", "Draws indexed triangle list", testSpec));
383 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
384 addChild(new InstanceFactory<DrawInstancedIndexed>(m_testCtx, "draw_instanced_indexed_triangle_strip", "Draws indexed triangle strip", testSpec));