1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2019 The Khronos Group Inc.
6 * Copyright (c) 2018 The Khronos Group Inc.
7 * Copyright (c) 2018 Google Inc.
8 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
24 * \brief Protected memory stack tests
25 *//*--------------------------------------------------------------------*/
27 #include "vktProtectedMemStackTests.hpp"
29 #include "vktProtectedMemContext.hpp"
30 #include "vktProtectedMemUtils.hpp"
31 #include "vktProtectedMemImageValidator.hpp"
32 #include "vktTestCase.hpp"
33 #include "vktTestGroupUtil.hpp"
35 #include "vkPrograms.hpp"
36 #include "vkTypeUtil.hpp"
37 #include "vkBuilderUtil.hpp"
38 #include "vkImageUtil.hpp"
39 #include "vkCmdUtil.hpp"
40 #include "vkObjUtil.hpp"
42 #include "tcuTestLog.hpp"
43 #include "tcuVector.hpp"
44 #include "tcuTextureUtil.hpp"
45 #include "tcuStringTemplate.hpp"
47 #include "gluTextureTestUtil.hpp"
49 #include "deRandom.hpp"
53 namespace ProtectedMem
65 Params (deUint32 stackSize_)
66 : stackSize (stackSize_)
68 // Find suitable image dimensions based on stack memory size
71 bool increaseWidth = true;
72 while (imageWidth * imageHeight < stackSize)
79 increaseWidth = !increaseWidth;
84 deUint32 getSeedValue (const Params& params)
86 return deInt32Hash(params.stackSize);
89 class StackTestInstance : public ProtectedTestInstance
92 StackTestInstance (Context& ctx,
93 const ImageValidator& validator,
94 const Params& params);
95 virtual tcu::TestStatus iterate (void);
98 de::MovePtr<tcu::Texture2D> createTestTexture2D (void);
99 tcu::TestStatus validateResult (vk::VkImage image,
100 vk::VkImageLayout imageLayout,
101 const tcu::Texture2D& texture2D,
102 const tcu::Sampler& refSampler);
103 void calculateRef (tcu::Texture2D& texture2D);
105 const ImageValidator& m_validator;
106 const Params& m_params;
109 class StackTestCase : public TestCase
112 StackTestCase (tcu::TestContext& testCtx,
113 const std::string& name,
114 const std::string& description,
115 const Params& params)
116 : TestCase (testCtx, name, description)
117 , m_validator (vk::VK_FORMAT_R8G8B8A8_UNORM)
122 virtual ~StackTestCase (void) {}
123 virtual TestInstance* createInstance (Context& ctx) const
125 return new StackTestInstance(ctx, m_validator, m_params);
127 virtual void initPrograms (vk::SourceCollections& programCollection) const;
130 ImageValidator m_validator;
134 void StackTestCase::initPrograms (vk::SourceCollections& programCollection) const
136 m_validator.initPrograms(programCollection);
138 // Test validates handling of protected memory allocated on stack.
139 // The test copies protected memory content into temporary variable allocated inside function p.
140 // Thus test forces protected content to appear on stack.
141 // Function p() returns specified protected memory element from the variable allocated on stack.
142 // Function u() returns specified protected memory element from the global variable.
143 // Values returned by p() and u() should be same.
144 // Test is repeated several times (16) to avoid coincidental matches.
145 // In case of any mismatches it is signalized to inherited verifier function by setting 0 in result store image.
146 // Each invocation validates particular element (bytes) on stack.
147 // Number of invocations matches stack size specified in test parameters.
151 "layout(local_size_x = " + de::toString(m_params.imageWidth) + ", local_size_y = " + de::toString(m_params.imageHeight) + ", local_size_z = 1) in;\n"
152 "layout(set = 0, binding = 0, rgba8) writeonly uniform highp image2D u_resultImage;\n"
153 "layout(set = 0, binding = 1, rgba8) readonly uniform highp image2D u_srcImage;\n"
154 "vec4 protectedData[" + de::toString(m_params.stackSize) + "];\n"
158 " vec4 localData[" + de::toString(m_params.stackSize) + "];\n"
159 " for (int i = 0; i < " + de::toString(m_params.stackSize) + "; i++)\n"
160 " localData[i] = protectedData[i];\n"
161 " return localData[idx];\n"
166 " return protectedData[idx];\n"
170 " const int n = " + de::toString(m_params.stackSize) + ";\n"
172 " int w = " + de::toString(m_params.imageWidth) + ";\n"
173 " int gx = int(gl_GlobalInvocationID.x);\n"
174 " int gy = int(gl_GlobalInvocationID.y);\n"
175 " int checked_ndx = gy * w + gx;\n"
178 " for (int j = 0; j < 16; j++)\n"
180 " for (int i = 0; i < n; i++)\n"
182 " const int idx = (i + j) % n;\n"
183 " protectedData[i] = imageLoad(u_srcImage, ivec2(idx % w, idx / w));\n"
186 " vec4 vp = p(checked_ndx);\n"
187 " vec4 vu = u(checked_ndx);\n"
188 " if (any(notEqual(vp,vu)))\n"
193 " outColor = vec4(0.0f);\n"
195 " outColor = vec4(1.0f);\n"
196 " imageStore(u_resultImage, ivec2(gx, gy), outColor);\n"
199 programCollection.glslSources.add("comp") << glu::ComputeSource(comp);
202 StackTestInstance::StackTestInstance (Context& ctx,
203 const ImageValidator& validator,
204 const Params& params)
205 : ProtectedTestInstance (ctx)
206 , m_validator (validator)
211 de::MovePtr<tcu::Texture2D> StackTestInstance::createTestTexture2D (void)
213 const tcu::TextureFormat texFmt = mapVkFormat(vk::VK_FORMAT_R8G8B8A8_UNORM);
214 de::MovePtr<tcu::Texture2D> texture2D (new tcu::Texture2D(texFmt, m_params.imageWidth, m_params.imageHeight));
216 texture2D->allocLevel(0);
218 const tcu::PixelBufferAccess& level = texture2D->getLevel(0);
220 fillWithUniqueColors(level, getSeedValue(m_params));
225 tcu::TestStatus StackTestInstance::iterate (void)
227 ProtectedContext& ctx (m_protectedContext);
228 const vk::DeviceInterface& vk = ctx.getDeviceInterface();
229 const vk::VkDevice device = ctx.getDevice();
230 const vk::VkQueue queue = ctx.getQueue();
231 const deUint32 queueFamilyIndex = ctx.getQueueFamilyIndex();
232 const vk::VkPhysicalDeviceProperties properties = vk::getPhysicalDeviceProperties(ctx.getInstanceDriver(), ctx.getPhysicalDevice());
234 vk::Unique<vk::VkCommandPool> cmdPool (makeCommandPool(vk, device, PROTECTION_ENABLED, queueFamilyIndex));
236 de::MovePtr<tcu::Texture2D> texture2D = createTestTexture2D();
237 const tcu::Sampler refSampler = tcu::Sampler(tcu::Sampler::CLAMP_TO_EDGE, tcu::Sampler::CLAMP_TO_EDGE, tcu::Sampler::CLAMP_TO_EDGE,
238 tcu::Sampler::NEAREST, tcu::Sampler::NEAREST);
240 vk::Unique<vk::VkShaderModule> computeShader (vk::createShaderModule(vk, device, ctx.getBinaryCollection().get("comp"), 0));
242 de::MovePtr<vk::ImageWithMemory> imageSrc;
243 de::MovePtr<vk::ImageWithMemory> imageDst;
244 vk::Move<vk::VkSampler> sampler;
245 vk::Move<vk::VkImageView> imageViewSrc;
246 vk::Move<vk::VkImageView> imageViewDst;
248 vk::Move<vk::VkDescriptorSetLayout> descriptorSetLayout;
249 vk::Move<vk::VkDescriptorPool> descriptorPool;
250 vk::Move<vk::VkDescriptorSet> descriptorSet;
252 // Check the number of invocations supported
253 if (properties.limits.maxComputeWorkGroupInvocations < m_params.imageWidth * m_params.imageHeight)
254 throw tcu::NotSupportedError("Not enough compute workgroup invocations supported.");
256 // Create src and dst images
258 vk::VkImageUsageFlags imageUsageFlags = vk::VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
259 vk::VK_IMAGE_USAGE_TRANSFER_DST_BIT |
260 vk::VK_IMAGE_USAGE_SAMPLED_BIT |
261 vk::VK_IMAGE_USAGE_STORAGE_BIT;
263 imageSrc = createImage2D(ctx, PROTECTION_ENABLED, queueFamilyIndex,
264 m_params.imageWidth, m_params.imageHeight,
265 vk::VK_FORMAT_R8G8B8A8_UNORM,
268 imageDst = createImage2D(ctx, PROTECTION_ENABLED, queueFamilyIndex,
269 m_params.imageWidth, m_params.imageHeight,
270 vk::VK_FORMAT_R8G8B8A8_UNORM,
274 // Upload source image
276 de::MovePtr<vk::ImageWithMemory> unprotectedImage = createImage2D(ctx, PROTECTION_DISABLED, queueFamilyIndex,
277 m_params.imageWidth, m_params.imageHeight,
278 vk::VK_FORMAT_R8G8B8A8_UNORM,
279 vk::VK_IMAGE_USAGE_TRANSFER_SRC_BIT | vk::VK_IMAGE_USAGE_TRANSFER_DST_BIT);
281 // Upload data to an unprotected image
282 uploadImage(m_protectedContext, **unprotectedImage, *texture2D);
284 // Copy unprotected image to protected image
285 copyToProtectedImage(m_protectedContext, **unprotectedImage, **imageSrc, vk::VK_IMAGE_LAYOUT_GENERAL, m_params.imageWidth, m_params.imageHeight);
289 clearImage(m_protectedContext, **imageDst);
291 // Create descriptors
293 vk::DescriptorSetLayoutBuilder layoutBuilder;
294 vk::DescriptorPoolBuilder poolBuilder;
296 layoutBuilder.addSingleBinding(vk::VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, vk::VK_SHADER_STAGE_COMPUTE_BIT);
297 layoutBuilder.addSingleBinding(vk::VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, vk::VK_SHADER_STAGE_COMPUTE_BIT);
298 poolBuilder.addType(vk::VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 2u);
300 descriptorSetLayout = layoutBuilder.build(vk, device);
301 descriptorPool = poolBuilder.build(vk, device, vk::VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
302 descriptorSet = makeDescriptorSet(vk, device, *descriptorPool, *descriptorSetLayout);
305 // Create pipeline layout
306 vk::Unique<vk::VkPipelineLayout> pipelineLayout (makePipelineLayout(vk, device, *descriptorSetLayout));
308 // Create image views
310 imageViewSrc = createImageView(ctx, **imageSrc, vk::VK_FORMAT_R8G8B8A8_UNORM);
311 imageViewDst = createImageView(ctx, **imageDst, vk::VK_FORMAT_R8G8B8A8_UNORM);
314 // Update descriptor set information
316 vk::DescriptorSetUpdateBuilder updateBuilder;
318 vk::VkDescriptorImageInfo descStorageImgDst = makeDescriptorImageInfo((vk::VkSampler)0, *imageViewDst, vk::VK_IMAGE_LAYOUT_GENERAL);
319 vk::VkDescriptorImageInfo descStorageImgSrc = makeDescriptorImageInfo((vk::VkSampler)0, *imageViewSrc, vk::VK_IMAGE_LAYOUT_GENERAL);
321 updateBuilder.writeSingle(*descriptorSet, vk::DescriptorSetUpdateBuilder::Location::binding(0u), vk::VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descStorageImgDst);
322 updateBuilder.writeSingle(*descriptorSet, vk::DescriptorSetUpdateBuilder::Location::binding(1u), vk::VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descStorageImgSrc);
324 updateBuilder.update(vk, device);
327 // Create compute commands & submit
329 const vk::Unique<vk::VkFence> fence (vk::createFence(vk, device));
330 vk::Unique<vk::VkPipeline> pipeline (makeComputePipeline(vk, device, *pipelineLayout, *computeShader, DE_NULL));
331 vk::Unique<vk::VkCommandBuffer> cmdBuffer (vk::allocateCommandBuffer(vk, device, *cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY));
333 beginCommandBuffer(vk, *cmdBuffer);
335 vk.cmdBindPipeline(*cmdBuffer, vk::VK_PIPELINE_BIND_POINT_COMPUTE, *pipeline);
336 vk.cmdBindDescriptorSets(*cmdBuffer, vk::VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineLayout, 0u, 1u, &*descriptorSet, 0u, DE_NULL);
337 vk.cmdDispatch(*cmdBuffer, 1u, 1u, 1u);
338 endCommandBuffer(vk, *cmdBuffer);
340 VK_CHECK(queueSubmit(ctx, PROTECTION_ENABLED, queue, *cmdBuffer, *fence, ~0ull));
343 // Calculate reference image
344 calculateRef(*texture2D);
347 return validateResult(**imageDst, vk::VK_IMAGE_LAYOUT_GENERAL, *texture2D, refSampler);
350 void StackTestInstance::calculateRef (tcu::Texture2D& texture2D)
352 const tcu::PixelBufferAccess& reference = texture2D.getLevel(0);
353 const tcu::IVec4 zero;
355 for (int x = 0; x < reference.getWidth(); ++x)
356 for (int y = 0; y < reference.getHeight(); ++y)
357 reference.setPixel(zero, x, y);
360 tcu::TestStatus StackTestInstance::validateResult (vk::VkImage image, vk::VkImageLayout imageLayout, const tcu::Texture2D& texture2D, const tcu::Sampler& refSampler)
362 de::Random rnd (getSeedValue(m_params));
363 ValidationData refData;
365 for (int ndx = 0; ndx < 4; ++ndx)
367 const float lod = 0.0f;
368 const float cx = rnd.getFloat(0.0f, 1.0f);
369 const float cy = rnd.getFloat(0.0f, 1.0f);
371 refData.coords[ndx] = tcu::Vec4(cx, cy, 0.0f, 0.0f);
372 refData.values[ndx] = texture2D.sample(refSampler, cx, cy, lod);
375 if (!m_validator.validateImage(m_protectedContext, refData, image, vk::VK_FORMAT_R8G8B8A8_UNORM, imageLayout))
376 return tcu::TestStatus::fail("Result validation failed");
378 return tcu::TestStatus::pass("Pass");
383 tcu::TestCaseGroup* createStackTests (tcu::TestContext& testCtx)
385 de::MovePtr<tcu::TestCaseGroup> stackGroup (new tcu::TestCaseGroup(testCtx, "stack", "Protected memory stack tests"));
387 static const deUint32 stackMemSizes[] = { 32, 64, 128, 256, 512, 1024 };
389 for (int stackMemSizeIdx = 0; stackMemSizeIdx < DE_LENGTH_OF_ARRAY(stackMemSizes); ++stackMemSizeIdx)
391 std::string testName = std::string("stacksize_") + de::toString(stackMemSizes[stackMemSizeIdx]);
393 stackGroup->addChild(new StackTestCase(testCtx, testName, "", Params(stackMemSizes[stackMemSizeIdx])));
396 return stackGroup.release();