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 Indirect Test
23 *//*--------------------------------------------------------------------*/
25 #include "vktDrawIndirectTest.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"
37 #include "vkQueryUtil.hpp"
40 #include "vkCmdUtil.hpp"
73 struct DrawTypedTestSpec : public TestSpecBase
76 : testFirstInstanceNdx(false)
77 , testIndirectCountExt(false)
81 bool testFirstInstanceNdx;
82 bool testIndirectCountExt;
85 class IndirectDraw : public DrawTestsBaseClass
88 typedef DrawTypedTestSpec TestSpec;
90 IndirectDraw (Context &context, TestSpec testSpec);
91 virtual tcu::TestStatus iterate (void);
93 template<typename T> void addCommand (const T&);
96 void setVertexBuffer (void);
97 void setFirstInstanceVertexBuffer (void);
99 std::vector<char> m_indirectBufferContents;
100 de::SharedPtr<Buffer> m_indirectBuffer;
101 vk::VkDeviceSize m_offsetInBuffer;
102 deUint32 m_strideInBuffer;
104 const bool m_testIndirectCountExt;
105 de::SharedPtr<Buffer> m_indirectCountBuffer;
106 vk::VkDeviceSize m_offsetInCountBuffer;
107 const deUint32 m_indirectCountExtDrawPadding;
109 deUint32 m_drawCount;
112 const DrawType m_drawType;
113 const bool m_testFirstInstanceNdx;
114 deBool m_isMultiDrawEnabled;
115 deUint32 m_drawIndirectMaxCount;
117 de::SharedPtr<Buffer> m_indexBuffer;
120 struct FirstInstanceSupported
122 static deUint32 getFirstInstance (void) { return 2; }
123 static bool isTestSupported (const vk::VkPhysicalDeviceFeatures& features) { return features.drawIndirectFirstInstance == VK_TRUE; }
126 struct FirstInstanceNotSupported
128 static deUint32 getFirstInstance (void) { return 0; }
129 static bool isTestSupported (const vk::VkPhysicalDeviceFeatures&) { return true; }
132 template<class FirstInstanceSupport>
133 class IndirectDrawInstanced : public IndirectDraw
136 IndirectDrawInstanced (Context &context, TestSpec testSpec);
137 virtual tcu::TestStatus iterate (void);
140 void IndirectDraw::setVertexBuffer (void)
142 int refVertexIndex = 2;
144 if (m_drawType == DRAW_TYPE_INDEXED)
146 for (int unusedIdx = 0; unusedIdx < VERTEX_OFFSET; unusedIdx++)
148 m_data.push_back(VertexElementData(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), -1));
150 refVertexIndex += VERTEX_OFFSET;
153 m_data.push_back(VertexElementData(tcu::Vec4( 1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), -1));
154 m_data.push_back(VertexElementData(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), -1));
158 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
159 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
160 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
161 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
162 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
163 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
164 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
166 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
167 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, 0.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
168 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, 0.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
169 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
170 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
171 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
172 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
173 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, 0.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
174 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, 0.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refVertexIndex++));
177 DE_FATAL("Unknown topology");
181 m_data.push_back(VertexElementData(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), -1));
184 void IndirectDraw::setFirstInstanceVertexBuffer (void)
186 if (m_context.getDeviceFeatures().drawIndirectFirstInstance != VK_TRUE)
188 TCU_THROW(NotSupportedError, "Required 'drawIndirectFirstInstance' feature is not supported");
191 if (m_drawType == DRAW_TYPE_INDEXED)
193 for (int unusedIdx = 0; unusedIdx < VERTEX_OFFSET; unusedIdx++)
195 m_data.push_back(VertexElementData(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), -1));
199 m_data.push_back(VertexElementData(tcu::Vec4( 1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), -1));
200 m_data.push_back(VertexElementData(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), -1));
204 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
206 int refInstanceIndex = 1;
207 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
208 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
209 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
211 refInstanceIndex = 0;
212 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
213 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
214 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
217 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
219 int refInstanceIndex = 1;
220 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, 0.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
221 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, 0.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
222 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
223 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, -0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
225 refInstanceIndex = 0;
226 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
227 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, 0.3f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
228 m_data.push_back(VertexElementData(tcu::Vec4(-0.3f, 0.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
229 m_data.push_back(VertexElementData(tcu::Vec4( 0.3f, 0.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), refInstanceIndex));
233 DE_FATAL("Unknown topology");
237 m_data.push_back(VertexElementData(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::blue().toVec(), -1));
240 IndirectDraw::IndirectDraw (Context &context, TestSpec testSpec)
241 : DrawTestsBaseClass (context, testSpec.shaders[glu::SHADERTYPE_VERTEX], testSpec.shaders[glu::SHADERTYPE_FRAGMENT], testSpec.topology)
242 , m_testIndirectCountExt (testSpec.testIndirectCountExt)
243 , m_indirectCountExtDrawPadding (1u)
244 , m_drawType (testSpec.drawType)
245 , m_testFirstInstanceNdx (testSpec.testFirstInstanceNdx)
247 if (m_testFirstInstanceNdx)
248 setFirstInstanceVertexBuffer();
254 if (testSpec.drawType == DRAW_TYPE_INDEXED)
256 const size_t indexBufferLength = m_data.size() - VERTEX_OFFSET;
258 m_indexBuffer = Buffer::createAndAlloc(m_vk, m_context.getDevice(), BufferCreateInfo(sizeof(deUint32) * indexBufferLength, vk::VK_BUFFER_USAGE_INDEX_BUFFER_BIT), m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
259 deUint32* indices = reinterpret_cast<deUint32*>(m_indexBuffer->getBoundMemory().getHostPtr());
260 for (size_t i = 0; i < indexBufferLength; i++)
262 indices[i] = static_cast<deUint32>(i);
264 vk::flushAlloc(m_vk, m_context.getDevice(), m_indexBuffer->getBoundMemory());
267 // Check device for multidraw support:
268 if (!m_context.getDeviceFeatures().multiDrawIndirect || m_testFirstInstanceNdx)
269 m_isMultiDrawEnabled = false;
271 m_isMultiDrawEnabled = true;
273 m_drawIndirectMaxCount = m_context.getDeviceProperties().limits.maxDrawIndirectCount;
277 void IndirectDraw::addCommand<vk::VkDrawIndirectCommand> (const vk::VkDrawIndirectCommand& command)
279 DE_ASSERT(m_drawType == DRAW_TYPE_SEQUENTIAL);
281 const size_t currentSize = m_indirectBufferContents.size();
283 m_indirectBufferContents.resize(currentSize + sizeof(command));
285 deMemcpy(&m_indirectBufferContents[currentSize], &command, sizeof(command));
289 void IndirectDraw::addCommand<vk::VkDrawIndexedIndirectCommand> (const vk::VkDrawIndexedIndirectCommand& command)
291 DE_ASSERT(m_drawType == DRAW_TYPE_INDEXED);
293 const size_t currentSize = m_indirectBufferContents.size();
295 m_indirectBufferContents.resize(currentSize + sizeof(command));
297 deMemcpy(&m_indirectBufferContents[currentSize], &command, sizeof(command));
300 tcu::TestStatus IndirectDraw::iterate (void)
302 tcu::TestLog& log = m_context.getTestContext().getLog();
303 const vk::VkQueue queue = m_context.getUniversalQueue();
304 const vk::VkDevice device = m_context.getDevice();
306 if (m_drawType == DRAW_TYPE_SEQUENTIAL)
310 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
312 vk::VkDrawIndirectCommand drawCommands[] =
318 (m_testFirstInstanceNdx ? 1u : 0u) //firstInstance
320 { (deUint32)-4, (deUint32)-2, (deUint32)-11, (deUint32)-9 }, // junk (stride)
328 addCommand(drawCommands[0]);
329 addCommand(drawCommands[1]);
330 addCommand(drawCommands[2]);
331 if (m_testIndirectCountExt)
333 addCommand(drawCommands[1]);
334 addCommand(drawCommands[1]);
338 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
340 vk::VkDrawIndirectCommand drawCommands[] =
346 (m_testFirstInstanceNdx ? 1u : 0u) //firstInstance
348 { (deUint32)-4, (deUint32)-2, (deUint32)-11, (deUint32)-9 }, // junk (stride)
356 addCommand(drawCommands[0]);
357 addCommand(drawCommands[1]);
358 addCommand(drawCommands[2]);
359 if (m_testIndirectCountExt)
361 addCommand(drawCommands[1]);
362 addCommand(drawCommands[1]);
367 TCU_FAIL("impossible");
370 m_strideInBuffer = 2 * (deUint32)sizeof(vk::VkDrawIndirectCommand);
372 else if (m_drawType == DRAW_TYPE_INDEXED)
376 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
378 vk::VkDrawIndexedIndirectCommand drawCommands[] =
384 VERTEX_OFFSET, // vertexOffset
385 (m_testFirstInstanceNdx ? 1u : 0u), // firstInstance
387 { (deUint32)-4, (deUint32)-2, (deUint32)-11, (deInt32)9, (deUint32)-7 }, // junk (stride)
392 VERTEX_OFFSET, // vertexOffset
396 addCommand(drawCommands[0]);
397 addCommand(drawCommands[1]);
398 addCommand(drawCommands[2]);
399 if (m_testIndirectCountExt)
401 addCommand(drawCommands[1]);
402 addCommand(drawCommands[1]);
406 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
408 vk::VkDrawIndexedIndirectCommand drawCommands[] =
414 VERTEX_OFFSET, // vertexOffset
415 (m_testFirstInstanceNdx ? 1u : 0u), // firstInstance
417 { (deUint32)-4, (deUint32)-2, (deUint32)-11, (deInt32)9, (deUint32)-7 }, // junk (stride)
422 VERTEX_OFFSET, // vertexOffset
426 addCommand(drawCommands[0]);
427 addCommand(drawCommands[1]);
428 addCommand(drawCommands[2]);
429 if (m_testIndirectCountExt)
431 addCommand(drawCommands[1]);
432 addCommand(drawCommands[1]);
437 TCU_FAIL("impossible");
440 m_strideInBuffer = 2 * (deUint32)sizeof(vk::VkDrawIndexedIndirectCommand);
444 m_offsetInBuffer = sizeof(m_junkData);
448 const vk::VkDeviceSize vertexBufferOffset = 0;
449 const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
451 m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
453 const vk::VkDeviceSize dataSize = m_indirectBufferContents.size();
455 m_indirectBuffer = Buffer::createAndAlloc( m_vk,
456 m_context.getDevice(),
457 BufferCreateInfo(dataSize + m_offsetInBuffer,
458 vk::VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT),
459 m_context.getDefaultAllocator(),
460 vk::MemoryRequirement::HostVisible);
462 deUint8* ptr = reinterpret_cast<deUint8*>(m_indirectBuffer->getBoundMemory().getHostPtr());
464 deMemcpy(ptr, &m_junkData, static_cast<size_t>(m_offsetInBuffer));
465 deMemcpy(ptr + m_offsetInBuffer, &m_indirectBufferContents[0], static_cast<size_t>(dataSize));
467 vk::flushAlloc(m_vk, m_context.getDevice(), m_indirectBuffer->getBoundMemory());
469 if (m_testIndirectCountExt)
471 m_offsetInCountBuffer = sizeof(tcu::Vec3);
472 m_indirectCountBuffer = Buffer::createAndAlloc(m_vk,
473 m_context.getDevice(),
474 BufferCreateInfo(m_offsetInCountBuffer + sizeof(m_drawCount),
475 vk::VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT),
476 m_context.getDefaultAllocator(),
477 vk::MemoryRequirement::HostVisible);
479 deUint8* countBufferPtr = reinterpret_cast<deUint8*>(m_indirectCountBuffer->getBoundMemory().getHostPtr());
481 if (m_isMultiDrawEnabled && m_drawCount <= m_drawIndirectMaxCount)
482 *(deUint32*)(countBufferPtr + m_offsetInCountBuffer) = m_drawCount;
484 *(deUint32*)(countBufferPtr + m_offsetInCountBuffer) = 1u;
486 vk::flushAlloc(m_vk, m_context.getDevice(), m_indirectCountBuffer->getBoundMemory());
489 m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
491 if (m_drawType == DRAW_TYPE_INDEXED)
493 m_vk.cmdBindIndexBuffer(*m_cmdBuffer, m_indexBuffer->object(), DE_NULL, vk::VK_INDEX_TYPE_UINT32);
496 if (m_isMultiDrawEnabled && m_drawCount <= m_drawIndirectMaxCount)
500 case DRAW_TYPE_SEQUENTIAL:
502 if (m_testIndirectCountExt)
503 m_vk.cmdDrawIndirectCount(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer,
504 m_indirectCountBuffer->object(), m_offsetInCountBuffer, m_drawCount + m_indirectCountExtDrawPadding,
507 m_vk.cmdDrawIndirect(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer, m_drawCount, m_strideInBuffer);
510 case DRAW_TYPE_INDEXED:
512 if (m_testIndirectCountExt)
513 m_vk.cmdDrawIndexedIndirectCount(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer,
514 m_indirectCountBuffer->object(), m_offsetInCountBuffer, m_drawCount + m_indirectCountExtDrawPadding,
517 m_vk.cmdDrawIndexedIndirect(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer, m_drawCount, m_strideInBuffer);
521 TCU_FAIL("impossible");
526 for (deUint32 drawNdx = 0; drawNdx < m_drawCount; drawNdx++)
530 case DRAW_TYPE_SEQUENTIAL:
532 if (m_testIndirectCountExt)
533 m_vk.cmdDrawIndirectCount(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer + drawNdx*m_strideInBuffer,
534 m_indirectCountBuffer->object(), m_offsetInCountBuffer, m_drawCount + m_indirectCountExtDrawPadding,
537 m_vk.cmdDrawIndirect(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer + drawNdx*m_strideInBuffer, 1u, 0u);
540 case DRAW_TYPE_INDEXED:
542 if (m_testIndirectCountExt)
543 m_vk.cmdDrawIndexedIndirectCount(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer + drawNdx*m_strideInBuffer,
544 m_indirectCountBuffer->object(), m_offsetInCountBuffer, m_drawCount + m_indirectCountExtDrawPadding,
547 m_vk.cmdDrawIndexedIndirect(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer + drawNdx*m_strideInBuffer, 1u, 0u);
551 TCU_FAIL("impossible");
555 endRenderPass(m_vk, *m_cmdBuffer);
556 endCommandBuffer(m_vk, *m_cmdBuffer);
558 submitCommandsAndWait(m_vk, device, queue, m_cmdBuffer.get());
561 tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5f + static_cast<float>(WIDTH)), (int)(0.5f + static_cast<float>(HEIGHT)));
562 referenceFrame.allocLevel(0);
564 const deInt32 frameWidth = referenceFrame.getWidth();
565 const deInt32 frameHeight = referenceFrame.getHeight();
567 tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
569 ReferenceImageCoordinates refCoords;
571 for (int y = 0; y < frameHeight; y++)
573 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
575 for (int x = 0; x < frameWidth; x++)
577 const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
579 if ((yCoord >= refCoords.bottom &&
580 yCoord <= refCoords.top &&
581 xCoord >= refCoords.left &&
582 xCoord <= refCoords.right))
583 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
587 const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
588 const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
589 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
591 qpTestResult res = QP_TEST_RESULT_PASS;
593 if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
594 referenceFrame.getLevel(0), renderedFrame, 0.05f, tcu::COMPARE_LOG_RESULT))
596 res = QP_TEST_RESULT_FAIL;
599 return tcu::TestStatus(res, qpGetTestResultName(res));
602 template<class FirstInstanceSupport>
603 IndirectDrawInstanced<FirstInstanceSupport>::IndirectDrawInstanced (Context &context, TestSpec testSpec)
604 : IndirectDraw(context, testSpec)
606 if (!FirstInstanceSupport::isTestSupported(m_context.getDeviceFeatures()))
608 throw tcu::NotSupportedError("Required 'drawIndirectFirstInstance' feature is not supported");
612 template<class FirstInstanceSupport>
613 tcu::TestStatus IndirectDrawInstanced<FirstInstanceSupport>::iterate (void)
615 tcu::TestLog& log = m_context.getTestContext().getLog();
616 const vk::VkQueue queue = m_context.getUniversalQueue();
617 const vk::VkDevice device = m_context.getDevice();
619 if (m_drawType == DRAW_TYPE_SEQUENTIAL)
623 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
625 vk::VkDrawIndirectCommand drawCmd[] =
631 FirstInstanceSupport::getFirstInstance() //firstInstance
633 { (deUint32)-4, (deUint32)-2, (deUint32)-11, (deUint32)-9 }, // junk (stride)
638 FirstInstanceSupport::getFirstInstance() //firstInstance
641 addCommand(drawCmd[0]);
642 addCommand(drawCmd[1]);
643 addCommand(drawCmd[2]);
644 if (m_testIndirectCountExt)
646 addCommand(drawCmd[1]);
647 addCommand(drawCmd[1]);
651 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
653 vk::VkDrawIndirectCommand drawCmd[] =
659 FirstInstanceSupport::getFirstInstance() //firstInstance
661 { (deUint32)-4, (deUint32)-2, (deUint32)-11, (deUint32)-9 },
666 FirstInstanceSupport::getFirstInstance() //firstInstance
669 addCommand(drawCmd[0]);
670 addCommand(drawCmd[1]);
671 addCommand(drawCmd[2]);
672 if (m_testIndirectCountExt)
674 addCommand(drawCmd[1]);
675 addCommand(drawCmd[1]);
680 TCU_FAIL("impossible");
684 m_strideInBuffer = 2 * (deUint32)sizeof(vk::VkDrawIndirectCommand);
686 else if (m_drawType == DRAW_TYPE_INDEXED)
690 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
692 vk::VkDrawIndexedIndirectCommand drawCmd[] =
698 VERTEX_OFFSET, // vertexOffset
699 FirstInstanceSupport::getFirstInstance() // firstInstance
701 { (deUint32)-4, (deUint32)-2, (deUint32)-11, (deInt32)9, (deUint32)-7 }, // junk (stride)
706 VERTEX_OFFSET, // vertexOffset
707 FirstInstanceSupport::getFirstInstance() // firstInstance
710 addCommand(drawCmd[0]);
711 addCommand(drawCmd[1]);
712 addCommand(drawCmd[2]);
713 if (m_testIndirectCountExt)
715 addCommand(drawCmd[1]);
716 addCommand(drawCmd[1]);
720 case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
722 vk::VkDrawIndexedIndirectCommand drawCmd[] =
728 VERTEX_OFFSET, // vertexOffset
729 FirstInstanceSupport::getFirstInstance() // firstInstance
731 { (deUint32)-4, (deUint32)-2, (deUint32)-11, (deInt32)9, (deUint32)-7 }, // junk (stride)
736 VERTEX_OFFSET, // vertexOffset
737 FirstInstanceSupport::getFirstInstance() // firstInstance
740 addCommand(drawCmd[0]);
741 addCommand(drawCmd[1]);
742 addCommand(drawCmd[2]);
743 if (m_testIndirectCountExt)
745 addCommand(drawCmd[1]);
746 addCommand(drawCmd[1]);
751 TCU_FAIL("impossible");
755 m_strideInBuffer = 2 * (deUint32)sizeof(vk::VkDrawIndexedIndirectCommand);
759 m_offsetInBuffer = sizeof(m_junkData);
763 const vk::VkDeviceSize vertexBufferOffset = 0;
764 const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
766 m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
768 const vk::VkDeviceSize dataSize = m_indirectBufferContents.size();
770 m_indirectBuffer = Buffer::createAndAlloc( m_vk,
771 m_context.getDevice(),
772 BufferCreateInfo(dataSize + m_offsetInBuffer,
773 vk::VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT),
774 m_context.getDefaultAllocator(),
775 vk::MemoryRequirement::HostVisible);
777 deUint8* ptr = reinterpret_cast<deUint8*>(m_indirectBuffer->getBoundMemory().getHostPtr());
779 deMemcpy(ptr, &m_junkData, static_cast<size_t>(m_offsetInBuffer));
780 deMemcpy((ptr + m_offsetInBuffer), &m_indirectBufferContents[0], static_cast<size_t>(dataSize));
782 vk::flushAlloc(m_vk, m_context.getDevice(), m_indirectBuffer->getBoundMemory());
784 if (m_testIndirectCountExt)
786 m_offsetInCountBuffer = sizeof(tcu::Vec3);
787 m_indirectCountBuffer = Buffer::createAndAlloc(m_vk,
788 m_context.getDevice(),
789 BufferCreateInfo(m_offsetInCountBuffer + sizeof(m_drawCount),
790 vk::VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT),
791 m_context.getDefaultAllocator(),
792 vk::MemoryRequirement::HostVisible);
794 deUint8* countBufferPtr = reinterpret_cast<deUint8*>(m_indirectCountBuffer->getBoundMemory().getHostPtr());
796 if (m_isMultiDrawEnabled && m_drawCount <= m_drawIndirectMaxCount)
797 *(deUint32*)(countBufferPtr + m_offsetInCountBuffer) = m_drawCount;
799 *(deUint32*)(countBufferPtr + m_offsetInCountBuffer) = 1u;
801 vk::flushAlloc(m_vk, m_context.getDevice(), m_indirectCountBuffer->getBoundMemory());
804 m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
806 if (m_drawType == DRAW_TYPE_INDEXED)
808 m_vk.cmdBindIndexBuffer(*m_cmdBuffer, m_indexBuffer->object(), DE_NULL, vk::VK_INDEX_TYPE_UINT32);
811 if (m_isMultiDrawEnabled && m_drawCount <= m_drawIndirectMaxCount)
815 case DRAW_TYPE_SEQUENTIAL:
817 if (m_testIndirectCountExt)
818 m_vk.cmdDrawIndirectCount(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer,
819 m_indirectCountBuffer->object(), m_offsetInCountBuffer,
820 m_drawCount + m_indirectCountExtDrawPadding, m_strideInBuffer);
822 m_vk.cmdDrawIndirect(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer, m_drawCount, m_strideInBuffer);
825 case DRAW_TYPE_INDEXED:
827 if (m_testIndirectCountExt)
828 m_vk.cmdDrawIndexedIndirectCount(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer,
829 m_indirectCountBuffer->object(), m_offsetInCountBuffer,
830 m_drawCount + m_indirectCountExtDrawPadding, m_strideInBuffer);
832 m_vk.cmdDrawIndexedIndirect(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer, m_drawCount, m_strideInBuffer);
836 TCU_FAIL("impossible");
841 for (deUint32 drawNdx = 0; drawNdx < m_drawCount; drawNdx++)
845 case DRAW_TYPE_SEQUENTIAL:
847 if (m_testIndirectCountExt)
848 m_vk.cmdDrawIndirectCount(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer + drawNdx*m_strideInBuffer,
849 m_indirectCountBuffer->object(), m_offsetInCountBuffer, m_drawCount + m_indirectCountExtDrawPadding,
852 m_vk.cmdDrawIndirect(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer + drawNdx*m_strideInBuffer, 1u, 0u);
855 case DRAW_TYPE_INDEXED:
857 if (m_testIndirectCountExt)
858 m_vk.cmdDrawIndexedIndirectCount(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer + drawNdx*m_strideInBuffer,
859 m_indirectCountBuffer->object(), m_offsetInCountBuffer, m_drawCount + m_indirectCountExtDrawPadding,
862 m_vk.cmdDrawIndexedIndirect(*m_cmdBuffer, m_indirectBuffer->object(), m_offsetInBuffer + drawNdx*m_strideInBuffer, 1u, 0u);
866 TCU_FAIL("impossible");
870 endRenderPass(m_vk, *m_cmdBuffer);
871 endCommandBuffer(m_vk, *m_cmdBuffer);
873 submitCommandsAndWait(m_vk, device, queue, m_cmdBuffer.get());
876 VK_CHECK(m_vk.queueWaitIdle(queue));
878 tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5f + static_cast<float>(WIDTH)), (int)(0.5 + static_cast<float>(HEIGHT)));
880 referenceFrame.allocLevel(0);
882 const deInt32 frameWidth = referenceFrame.getWidth();
883 const deInt32 frameHeight = referenceFrame.getHeight();
885 tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
887 ReferenceImageInstancedCoordinates refInstancedCoords;
889 for (int y = 0; y < frameHeight; y++)
891 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
893 for (int x = 0; x < frameWidth; x++)
895 const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
897 if ((yCoord >= refInstancedCoords.bottom &&
898 yCoord <= refInstancedCoords.top &&
899 xCoord >= refInstancedCoords.left &&
900 xCoord <= refInstancedCoords.right))
901 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
905 const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
906 const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
907 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
909 qpTestResult res = QP_TEST_RESULT_PASS;
911 if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
912 referenceFrame.getLevel(0), renderedFrame, 0.05f, tcu::COMPARE_LOG_RESULT))
914 res = QP_TEST_RESULT_FAIL;
917 return tcu::TestStatus(res, qpGetTestResultName(res));
920 void checkIndirectCountExt (Context& context)
922 context.requireDeviceFunctionality("VK_KHR_draw_indirect_count");
927 IndirectDrawTests::IndirectDrawTests (tcu::TestContext& testCtx)
928 : TestCaseGroup(testCtx, "indirect_draw", "indirect drawing simple geometry")
930 /* Left blank on purpose */
933 IndirectDrawTests::~IndirectDrawTests (void) {}
936 void IndirectDrawTests::init (void)
938 for (int drawTypeIdx = 0; drawTypeIdx < DRAWTYPE_LAST; drawTypeIdx++)
940 std::string drawTypeStr;
943 case DRAW_TYPE_SEQUENTIAL:
944 drawTypeStr = "sequential";
946 case DRAW_TYPE_INDEXED:
947 drawTypeStr = "indexed";
950 TCU_FAIL("impossible");
953 tcu::TestCaseGroup* drawTypeGroup = new tcu::TestCaseGroup(m_testCtx, drawTypeStr.c_str(), ("Draws geometry using " + drawTypeStr + "draw call").c_str());
955 tcu::TestCaseGroup* indirectDrawGroup = new tcu::TestCaseGroup(m_testCtx, "indirect_draw", "Draws geometry");
956 tcu::TestCaseGroup* indirectDrawCountGroup = new tcu::TestCaseGroup(m_testCtx, "indirect_draw_count", "Draws geometry with VK_KHR_draw_indirect_count extension");
958 IndirectDraw::TestSpec testSpec;
959 testSpec.drawType = static_cast<DrawType>(drawTypeIdx);
960 testSpec.shaders[glu::SHADERTYPE_VERTEX] = "vulkan/draw/VertexFetch.vert";
961 testSpec.shaders[glu::SHADERTYPE_FRAGMENT] = "vulkan/draw/VertexFetch.frag";
962 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
963 indirectDrawGroup->addChild(new InstanceFactory<IndirectDraw>(m_testCtx, "triangle_list", "Draws triangle list", testSpec));
964 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
965 indirectDrawGroup->addChild(new InstanceFactory<IndirectDraw>(m_testCtx, "triangle_strip", "Draws triangle strip", testSpec));
967 testSpec.testIndirectCountExt = true;
968 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
969 indirectDrawCountGroup->addChild(new InstanceFactory<IndirectDraw, FunctionSupport0>(m_testCtx, "triangle_list", "Draws triangle list", testSpec, FunctionSupport0(checkIndirectCountExt)));
970 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
971 indirectDrawCountGroup->addChild(new InstanceFactory<IndirectDraw, FunctionSupport0>(m_testCtx, "triangle_strip", "Draws triangle strip", testSpec, FunctionSupport0(checkIndirectCountExt)));
973 drawTypeGroup->addChild(indirectDrawGroup);
974 drawTypeGroup->addChild(indirectDrawCountGroup);
977 tcu::TestCaseGroup* indirectDrawFirstInstanceGroup = new tcu::TestCaseGroup(m_testCtx, "indirect_draw_first_instance", "Draws geometry with different first instance in one commandbuffer");
978 tcu::TestCaseGroup* indirectDrawCountFirstInstanceGroup = new tcu::TestCaseGroup(m_testCtx, "indirect_draw_count_first_instance", "Draws geometry with VK_KHR_draw_indirect_count extension with different first instance in one commandbuffer using ");
980 IndirectDraw::TestSpec testSpec;
981 testSpec.testFirstInstanceNdx = true;
982 testSpec.drawType = static_cast<DrawType>(drawTypeIdx);
983 testSpec.shaders[glu::SHADERTYPE_VERTEX] = "vulkan/draw/VertexFetchInstanceIndex.vert";
984 testSpec.shaders[glu::SHADERTYPE_FRAGMENT] = "vulkan/draw/VertexFetch.frag";
985 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
986 indirectDrawFirstInstanceGroup->addChild(new InstanceFactory<IndirectDraw>(m_testCtx, "triangle_list", "Draws triangle list", testSpec));
987 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
988 indirectDrawFirstInstanceGroup->addChild(new InstanceFactory<IndirectDraw>(m_testCtx, "triangle_strip", "Draws triangle strip", testSpec));
990 testSpec.testIndirectCountExt = true;
991 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
992 indirectDrawCountFirstInstanceGroup->addChild(new InstanceFactory<IndirectDraw, FunctionSupport0>(m_testCtx, "triangle_list", "Draws triangle list", testSpec, FunctionSupport0(checkIndirectCountExt)));
993 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
994 indirectDrawCountFirstInstanceGroup->addChild(new InstanceFactory<IndirectDraw, FunctionSupport0>(m_testCtx, "triangle_strip", "Draws triangle strip", testSpec, FunctionSupport0(checkIndirectCountExt)));
996 drawTypeGroup->addChild(indirectDrawFirstInstanceGroup);
997 drawTypeGroup->addChild(indirectDrawCountFirstInstanceGroup);
1000 tcu::TestCaseGroup* indirectDrawInstancedGroup = new tcu::TestCaseGroup(m_testCtx, "indirect_draw_instanced", "Draws an instanced geometry");
1001 tcu::TestCaseGroup* indirectDrawCountInstancedGroup = new tcu::TestCaseGroup(m_testCtx, "indirect_draw_count_instanced", "Draws an instanced geometry with VK_KHR_draw_indirect_count extension");
1003 tcu::TestCaseGroup* indirectDrawNoFirstInstanceGroup = new tcu::TestCaseGroup(m_testCtx, "no_first_instance", "Use 0 as firstInstance");
1004 tcu::TestCaseGroup* indirectDrawCountNoFirstInstanceGroup = new tcu::TestCaseGroup(m_testCtx, "no_first_instance", "Use 0 as firstInstance");
1006 IndirectDrawInstanced<FirstInstanceNotSupported>::TestSpec testSpec;
1007 testSpec.drawType = static_cast<DrawType>(drawTypeIdx);
1009 testSpec.shaders[glu::SHADERTYPE_VERTEX] = "vulkan/draw/VertexFetchInstanced.vert";
1010 testSpec.shaders[glu::SHADERTYPE_FRAGMENT] = "vulkan/draw/VertexFetch.frag";
1012 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
1013 indirectDrawNoFirstInstanceGroup->addChild(new InstanceFactory<IndirectDrawInstanced<FirstInstanceNotSupported> >(m_testCtx, "triangle_list", "Draws an instanced triangle list", testSpec));
1014 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
1015 indirectDrawNoFirstInstanceGroup->addChild(new InstanceFactory<IndirectDrawInstanced<FirstInstanceNotSupported> >(m_testCtx, "triangle_strip", "Draws an instanced triangle strip", testSpec));
1016 testSpec.testIndirectCountExt = true;
1017 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
1018 indirectDrawCountNoFirstInstanceGroup->addChild(new InstanceFactory<IndirectDrawInstanced<FirstInstanceNotSupported>, FunctionSupport0>(m_testCtx, "triangle_list", "Draws an instanced triangle list", testSpec, FunctionSupport0(checkIndirectCountExt)));
1019 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
1020 indirectDrawCountNoFirstInstanceGroup->addChild(new InstanceFactory<IndirectDrawInstanced<FirstInstanceNotSupported>, FunctionSupport0>(m_testCtx, "triangle_strip", "Draws an instanced triangle strip", testSpec, FunctionSupport0(checkIndirectCountExt)));
1022 indirectDrawInstancedGroup->addChild(indirectDrawNoFirstInstanceGroup);
1023 indirectDrawCountInstancedGroup->addChild(indirectDrawCountNoFirstInstanceGroup);
1025 tcu::TestCaseGroup* indirectDrawFirstInstanceGroup = new tcu::TestCaseGroup(m_testCtx, "first_instance", "Use drawIndirectFirstInstance optional feature");
1026 tcu::TestCaseGroup* indirectDrawCountFirstInstanceGroup = new tcu::TestCaseGroup(m_testCtx, "first_instance", "Use drawIndirectFirstInstance optional feature");
1028 IndirectDrawInstanced<FirstInstanceSupported>::TestSpec testSpec;
1029 testSpec.drawType = static_cast<DrawType>(drawTypeIdx);
1031 testSpec.shaders[glu::SHADERTYPE_VERTEX] = "vulkan/draw/VertexFetchInstancedFirstInstance.vert";
1032 testSpec.shaders[glu::SHADERTYPE_FRAGMENT] = "vulkan/draw/VertexFetch.frag";
1034 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
1035 indirectDrawFirstInstanceGroup->addChild(new InstanceFactory<IndirectDrawInstanced<FirstInstanceSupported> >(m_testCtx, "triangle_list", "Draws an instanced triangle list", testSpec));
1036 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
1037 indirectDrawFirstInstanceGroup->addChild(new InstanceFactory<IndirectDrawInstanced<FirstInstanceSupported> >(m_testCtx, "triangle_strip", "Draws an instanced triangle strip", testSpec));
1039 testSpec.testIndirectCountExt = true;
1040 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
1041 indirectDrawCountFirstInstanceGroup->addChild(new InstanceFactory<IndirectDrawInstanced<FirstInstanceSupported>, FunctionSupport0>(m_testCtx, "triangle_list", "Draws an instanced triangle list", testSpec, FunctionSupport0(checkIndirectCountExt)));
1042 testSpec.topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
1043 indirectDrawCountFirstInstanceGroup->addChild(new InstanceFactory<IndirectDrawInstanced<FirstInstanceSupported>, FunctionSupport0>(m_testCtx, "triangle_strip", "Draws an instanced triangle strip", testSpec, FunctionSupport0(checkIndirectCountExt)));
1045 indirectDrawInstancedGroup->addChild(indirectDrawFirstInstanceGroup);
1046 indirectDrawCountInstancedGroup->addChild(indirectDrawCountFirstInstanceGroup);
1048 drawTypeGroup->addChild(indirectDrawInstancedGroup);
1049 drawTypeGroup->addChild(indirectDrawCountInstancedGroup);
1052 addChild(drawTypeGroup);