1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2017 The Khronos Group Inc.
6 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 Protected content copy image tests
23 *//*--------------------------------------------------------------------*/
25 #include "vktProtectedMemCopyImageTests.hpp"
27 #include "deRandom.hpp"
28 #include "tcuTestLog.hpp"
29 #include "tcuVector.hpp"
31 #include "vkPrograms.hpp"
32 #include "vktTestCase.hpp"
33 #include "vktTestGroupUtil.hpp"
34 #include "vkTypeUtil.hpp"
35 #include "vkBuilderUtil.hpp"
36 #include "vkCmdUtil.hpp"
38 #include "vktProtectedMemContext.hpp"
39 #include "vktProtectedMemUtils.hpp"
40 #include "vktProtectedMemImageValidator.hpp"
44 namespace ProtectedMem
55 class CopyImageTestInstance : public ProtectedTestInstance
58 CopyImageTestInstance (Context& ctx,
59 const vk::VkClearColorValue& clearColorValue,
60 const ValidationData& refData,
61 const ImageValidator& validator,
62 const CmdBufferType cmdBufferType);
63 virtual tcu::TestStatus iterate (void);
66 const vk::VkFormat m_imageFormat;
67 const vk::VkClearColorValue& m_clearColorValue;
68 const ValidationData& m_refData;
69 const ImageValidator& m_validator;
70 const CmdBufferType m_cmdBufferType;
73 class CopyImageTestCase : public TestCase
76 CopyImageTestCase (tcu::TestContext& testCtx,
77 const std::string& name,
78 vk::VkClearColorValue clearColorValue,
80 CmdBufferType cmdBufferType)
81 : TestCase (testCtx, name, "Clear and copy image.")
82 , m_clearColorValue (clearColorValue)
84 , m_cmdBufferType (cmdBufferType)
88 virtual ~CopyImageTestCase (void) {}
89 virtual TestInstance* createInstance (Context& ctx) const
91 return new CopyImageTestInstance(ctx, m_clearColorValue, m_refData, m_validator, m_cmdBufferType);
93 virtual void initPrograms (vk::SourceCollections& programCollection) const
95 m_validator.initPrograms(programCollection);
98 vk::VkClearColorValue m_clearColorValue;
99 ValidationData m_refData;
100 ImageValidator m_validator;
101 CmdBufferType m_cmdBufferType;
104 CopyImageTestInstance::CopyImageTestInstance (Context& ctx,
105 const vk::VkClearColorValue& clearColorValue,
106 const ValidationData& refData,
107 const ImageValidator& validator,
108 const CmdBufferType cmdBufferType)
109 : ProtectedTestInstance (ctx)
110 , m_imageFormat (vk::VK_FORMAT_R8G8B8A8_UNORM)
111 , m_clearColorValue (clearColorValue)
112 , m_refData (refData)
113 , m_validator (validator)
114 , m_cmdBufferType (cmdBufferType)
118 tcu::TestStatus CopyImageTestInstance::iterate()
120 ProtectedContext& ctx (m_protectedContext);
121 const vk::DeviceInterface& vk = ctx.getDeviceInterface();
122 const vk::VkDevice device = ctx.getDevice();
123 const vk::VkQueue queue = ctx.getQueue();
124 const deUint32 queueFamilyIndex = ctx.getQueueFamilyIndex();
127 de::MovePtr<vk::ImageWithMemory> colorImage = createImage2D(ctx, PROTECTION_ENABLED, queueFamilyIndex,
128 RENDER_WIDTH, RENDER_HEIGHT,
130 vk::VK_IMAGE_USAGE_SAMPLED_BIT
131 | vk::VK_IMAGE_USAGE_TRANSFER_DST_BIT);
132 de::MovePtr<vk::ImageWithMemory> colorImageSrc = createImage2D(ctx, PROTECTION_ENABLED, queueFamilyIndex,
133 RENDER_WIDTH, RENDER_HEIGHT,
135 vk::VK_IMAGE_USAGE_SAMPLED_BIT
136 | vk::VK_IMAGE_USAGE_TRANSFER_DST_BIT
137 | vk::VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
139 vk::Unique<vk::VkPipelineLayout> pipelineLayout (createPipelineLayout(ctx, 0u, DE_NULL));
141 vk::Unique<vk::VkCommandPool> cmdPool (makeCommandPool(vk, device, PROTECTION_ENABLED, queueFamilyIndex));
142 vk::Unique<vk::VkCommandBuffer> cmdBuffer (vk::allocateCommandBuffer(vk, device, *cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY));
143 vk::Unique<vk::VkCommandBuffer> secondaryCmdBuffer (vk::allocateCommandBuffer(vk, device, *cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_SECONDARY));
144 vk::VkCommandBuffer targetCmdBuffer = (m_cmdBufferType == CMD_BUFFER_SECONDARY) ? *secondaryCmdBuffer : *cmdBuffer;
147 beginCommandBuffer(vk, *cmdBuffer);
149 if (m_cmdBufferType == CMD_BUFFER_SECONDARY)
151 // Begin secondary command buffer
152 const vk::VkCommandBufferInheritanceInfo secCmdBufInheritInfo =
154 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
156 (vk::VkRenderPass)0u, // renderPass
158 (vk::VkFramebuffer)0u, // framebuffer
159 VK_FALSE, // occlusionQueryEnable
160 (vk::VkQueryControlFlags)0u, // queryFlags
161 (vk::VkQueryPipelineStatisticFlags)0u, // pipelineStatistics
163 beginSecondaryCommandBuffer(vk, *secondaryCmdBuffer, secCmdBufInheritInfo);
166 // Start image barrier for source image.
168 const vk::VkImageMemoryBarrier startImgBarrier =
170 vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
173 vk::VK_ACCESS_TRANSFER_WRITE_BIT, // dstAccessMask
174 vk::VK_IMAGE_LAYOUT_UNDEFINED, // oldLayout
175 vk::VK_IMAGE_LAYOUT_GENERAL, // newLayout
176 queueFamilyIndex, // srcQueueFamilyIndex
177 queueFamilyIndex, // dstQueueFamilyIndex
178 **colorImageSrc, // image
180 vk::VK_IMAGE_ASPECT_COLOR_BIT, // aspectMask
183 0u, // baseArraySlice
184 1u, // subresourceRange
188 vk.cmdPipelineBarrier(targetCmdBuffer,
189 vk::VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
190 vk::VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
191 (vk::VkDependencyFlags)0,
192 0, (const vk::VkMemoryBarrier*)DE_NULL,
193 0, (const vk::VkBufferMemoryBarrier*)DE_NULL,
194 1, &startImgBarrier);
198 const vk::VkImageSubresourceRange subresourceRange =
200 vk::VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask
201 0u, // uint32_t baseMipLevel
202 1u, // uint32_t levelCount
203 0u, // uint32_t baseArrayLayer
204 1u, // uint32_t layerCount
206 vk.cmdClearColorImage(targetCmdBuffer, **colorImageSrc, vk::VK_IMAGE_LAYOUT_GENERAL, &m_clearColorValue, 1, &subresourceRange);
208 // Image barrier to change accessMask to transfer read bit for source image.
210 const vk::VkImageMemoryBarrier initializeBarrier =
212 vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
214 vk::VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
215 vk::VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
216 vk::VK_IMAGE_LAYOUT_GENERAL, // oldLayout
217 vk::VK_IMAGE_LAYOUT_GENERAL, // newLayout
218 queueFamilyIndex, // srcQueueFamilyIndex
219 queueFamilyIndex, // dstQueueFamilyIndex
220 **colorImageSrc, // image
222 vk::VK_IMAGE_ASPECT_COLOR_BIT, // aspectMask
225 0u, // baseArraySlice
226 1u, // subresourceRange
230 vk.cmdPipelineBarrier(targetCmdBuffer,
231 vk::VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
232 vk::VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
233 (vk::VkDependencyFlags)0,
234 0, (const vk::VkMemoryBarrier*)DE_NULL,
235 0, (const vk::VkBufferMemoryBarrier*)DE_NULL,
236 1, &initializeBarrier);
239 // Image barrier for destination image.
241 const vk::VkImageMemoryBarrier initializeBarrier =
243 vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
246 vk::VK_ACCESS_TRANSFER_WRITE_BIT, // dstAccessMask
247 vk::VK_IMAGE_LAYOUT_UNDEFINED, // oldLayout
248 vk::VK_IMAGE_LAYOUT_GENERAL, // newLayout
249 queueFamilyIndex, // srcQueueFamilyIndex
250 queueFamilyIndex, // dstQueueFamilyIndex
251 **colorImage, // image
253 vk::VK_IMAGE_ASPECT_COLOR_BIT, // aspectMask
256 0u, // baseArraySlice
257 1u, // subresourceRange
261 vk.cmdPipelineBarrier(targetCmdBuffer,
262 vk::VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
263 vk::VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
264 (vk::VkDependencyFlags)0,
265 0, (const vk::VkMemoryBarrier*)DE_NULL,
266 0, (const vk::VkBufferMemoryBarrier*)DE_NULL,
267 1, &initializeBarrier);
271 const vk::VkImageSubresourceLayers imgSubResCopy =
273 vk::VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask;
274 0u, // deUint32 mipLevel;
275 0u, // deUint32 baseArrayLayer;
276 1u, // deUint32 layerCount;
278 const vk::VkOffset3D nullOffset = {0u, 0u, 0u};
279 const vk::VkExtent3D imageExtent = {(deUint32)RENDER_WIDTH, (deUint32)RENDER_HEIGHT, 1u};
280 const vk::VkImageCopy copyImageRegion =
282 imgSubResCopy, // VkImageSubresourceCopy srcSubresource;
283 nullOffset, // VkOffset3D srcOffset;
284 imgSubResCopy, // VkImageSubresourceCopy destSubresource;
285 nullOffset, // VkOffset3D destOffset;
286 imageExtent, // VkExtent3D extent;
289 vk.cmdCopyImage(targetCmdBuffer, **colorImageSrc, vk::VK_IMAGE_LAYOUT_GENERAL,
290 **colorImage, vk::VK_IMAGE_LAYOUT_GENERAL, 1u, ©ImageRegion);
293 const vk::VkImageMemoryBarrier endImgBarrier =
295 vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
297 vk::VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
298 vk::VK_ACCESS_SHADER_READ_BIT, // dstAccessMask
299 vk::VK_IMAGE_LAYOUT_GENERAL, // oldLayout
300 vk::VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // newLayout
301 queueFamilyIndex, // srcQueueFamilyIndex
302 queueFamilyIndex, // dstQueueFamilyIndex
303 **colorImage, // image
305 vk::VK_IMAGE_ASPECT_COLOR_BIT, // aspectMask
308 0u, // baseArraySlice
309 1u, // subresourceRange
312 vk.cmdPipelineBarrier(targetCmdBuffer,
313 vk::VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
314 vk::VK_PIPELINE_STAGE_TRANSFER_BIT,
315 (vk::VkDependencyFlags)0,
316 0, (const vk::VkMemoryBarrier*)DE_NULL,
317 0, (const vk::VkBufferMemoryBarrier*)DE_NULL,
321 if (m_cmdBufferType == CMD_BUFFER_SECONDARY)
323 endCommandBuffer(vk, *secondaryCmdBuffer);
324 vk.cmdExecuteCommands(*cmdBuffer, 1u, &secondaryCmdBuffer.get());
327 endCommandBuffer(vk, *cmdBuffer);
329 // Submit command buffer
330 const vk::Unique<vk::VkFence> fence (vk::createFence(vk, device));
331 VK_CHECK(queueSubmit(ctx, PROTECTION_ENABLED, queue, *cmdBuffer, *fence, ~0ull));
334 ctx.getTestContext().getLog()
335 << tcu::TestLog::Message << "Color clear value: " << tcu::Vec4(m_clearColorValue.float32) << tcu::TestLog::EndMessage;
337 // Validate resulting image
338 if (m_validator.validateImage(ctx, m_refData, **colorImage, m_imageFormat))
339 return tcu::TestStatus::pass("Everything went OK");
341 return tcu::TestStatus::fail("Something went really wrong");
344 tcu::TestCaseGroup* createCopyImageTests (tcu::TestContext& testCtx, CmdBufferType cmdBufferType)
347 const vk::VkClearColorValue clearColorValue;
348 const ValidationData data;
350 { { { 1.0f, 0.0f, 0.0f, 1.0f } },
352 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
353 tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
354 { tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f),
355 tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), }
358 { { { 0.0f, 1.0f, 0.0f, 1.0f } },
360 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
361 tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
362 { tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f),
363 tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), }
366 { { { 0.0f, 0.0f, 1.0f, 1.0f } },
368 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
369 tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
370 { tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f),
371 tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), }
374 { { { 0.0f, 0.0f, 0.0f, 1.0f } },
376 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
377 tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
378 { tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f),
379 tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), }
382 { { { 1.0f, 0.0f, 0.0f, 1.0f } },
384 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
385 tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
386 { tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f),
387 tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), }
390 { { { 1.0f, 0.0f, 0.0f, 0.0f } },
392 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
393 tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
394 { tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f),
395 tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f), }
398 { { { 0.1f, 0.2f, 0.3f, 0.0f } },
400 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
401 tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
402 { tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f), tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f),
403 tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f), tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f), }
408 de::MovePtr<tcu::TestCaseGroup> copyStaticTests (new tcu::TestCaseGroup(testCtx, "static", "Copy Image Tests with static input"));
410 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(testData); ++ndx)
412 const std::string name = "copy_" + de::toString(ndx + 1);
413 copyStaticTests->addChild(new CopyImageTestCase(testCtx, name.c_str(), testData[ndx].clearColorValue, testData[ndx].data, cmdBufferType));
416 /* Add a few randomized tests */
417 de::MovePtr<tcu::TestCaseGroup> copyRandomTests (new tcu::TestCaseGroup(testCtx, "random", "Copy Image Tests with random input"));
418 const int testCount = 10;
419 de::Random rnd (testCtx.getCommandLine().getBaseSeed());
420 for (int ndx = 0; ndx < testCount; ++ndx)
422 const std::string name = "copy_" + de::toString(ndx + 1);
423 vk::VkClearValue clearValue = vk::makeClearValueColorF32(
424 rnd.getFloat(0.0, 1.0f),
425 rnd.getFloat(0.0, 1.0f),
426 rnd.getFloat(0.0, 1.0f),
427 rnd.getFloat(0.0, 1.0f));
429 tcu::Vec4 refValue (clearValue.color.float32[0], clearValue.color.float32[1], clearValue.color.float32[2], clearValue.color.float32[3]);
430 ValidationData data =
432 { tcu::Vec4(rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f)),
433 tcu::Vec4(rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f)),
434 tcu::Vec4(rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f)),
435 tcu::Vec4(rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f), rnd.getFloat(0.0f, 1.0f)) },
436 { refValue, refValue, refValue, refValue }
438 copyRandomTests->addChild(new CopyImageTestCase(testCtx, name.c_str(), clearValue.color, data, cmdBufferType));
441 std::string groupName = getCmdBufferTypeStr(cmdBufferType);
442 std::string groupDesc = "Copy Image Tests with " + groupName + " command buffer";
443 de::MovePtr<tcu::TestCaseGroup> copyTests (new tcu::TestCaseGroup(testCtx, groupName.c_str(), groupDesc.c_str()));
444 copyTests->addChild(copyStaticTests.release());
445 copyTests->addChild(copyRandomTests.release());
446 return copyTests.release();
451 tcu::TestCaseGroup* createCopyImageTests (tcu::TestContext& testCtx)
453 de::MovePtr<tcu::TestCaseGroup> copyTests (new tcu::TestCaseGroup(testCtx, "copy", "Copy Image Tests"));
455 copyTests->addChild(createCopyImageTests(testCtx, CMD_BUFFER_PRIMARY));
456 copyTests->addChild(createCopyImageTests(testCtx, CMD_BUFFER_SECONDARY));
458 return copyTests.release();