1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2020 The Khronos Group Inc.
6 * Copyright (c) 2016 The Android Open Source Project
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 Testing writing and reading for mismatched formats
23 *//*--------------------------------------------------------------------*/
25 #include "vktImageLoadStoreTests.hpp"
26 #include "vktTestCaseUtil.hpp"
27 #include "vktImageTestsUtil.hpp"
28 #include "vktImageLoadStoreUtil.hpp"
29 #include "vktImageTexture.hpp"
33 #include "vkRefUtil.hpp"
34 #include "vkPlatform.hpp"
35 #include "vkPrograms.hpp"
36 #include "vkMemUtil.hpp"
37 #include "vkBarrierUtil.hpp"
38 #include "vkBuilderUtil.hpp"
39 #include "vkQueryUtil.hpp"
40 #include "vkImageUtil.hpp"
41 #include "vkCmdUtil.hpp"
42 #include "vkObjUtil.hpp"
43 #include "vkImageWithMemory.hpp"
46 #include "deUniquePtr.hpp"
47 #include "deSharedPtr.hpp"
48 #include "deStringUtil.hpp"
50 #include "tcuImageCompare.hpp"
51 #include "tcuTexture.hpp"
52 #include "tcuTextureUtil.hpp"
53 #include "tcuFloat.hpp"
54 #include "tcuStringTemplate.hpp"
71 const char* GLSLFormat;
74 tcu::TextureChannelClass ChannelClass;
77 FormatInfo getFormatInfo (VkFormat format)
81 const tcu::TextureFormat texFormat = mapVkFormat(format);
83 result.VectorWidth = getNumUsedChannels(texFormat.order);
84 result.BytesPerPixel = getPixelSize(texFormat);
85 result.ChannelClass = tcu::getTextureChannelClass(texFormat.type);
90 std::string ChannelClassToImageType (tcu::TextureChannelClass channelClass)
94 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER: return "uimage2D";
95 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER: return "iimage2D";
96 default: return "image2D";
100 std::string ChannelClassToVecType (tcu::TextureChannelClass channelClass)
102 switch (channelClass)
104 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER: return "uvec4";
105 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER: return "ivec4";
106 default: return "vec4";
110 std::string ChannelClassToDefaultVecValue (tcu::TextureChannelClass channelClass)
112 switch (channelClass)
114 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER: return "uvec4(1, 10, 100, 1000)";
115 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER: return "ivec4(-1, 2, -1000, 2000)";
116 default: return "vec4(0.25, 0.5, 0.0, 1.0)";
120 const std::map<std::string, FormatInfo> SpirvFormats {
121 { "Rgba32f", { nullptr, 4, 16, tcu::TEXTURECHANNELCLASS_FLOATING_POINT } },
122 { "Rg32f", { nullptr, 2, 8, tcu::TEXTURECHANNELCLASS_FLOATING_POINT } },
123 { "R32f", { nullptr, 1, 4, tcu::TEXTURECHANNELCLASS_FLOATING_POINT } },
124 { "Rgba16f", { nullptr, 4, 8, tcu::TEXTURECHANNELCLASS_FLOATING_POINT } },
125 { "Rg16f", { nullptr, 2, 4, tcu::TEXTURECHANNELCLASS_FLOATING_POINT } },
126 { "R16f", { nullptr, 1, 2, tcu::TEXTURECHANNELCLASS_FLOATING_POINT } },
127 { "Rgba16", { nullptr, 4, 8, tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT } },
128 { "Rg16", { nullptr, 2, 4, tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT } },
129 { "R16", { nullptr, 1, 2, tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT } },
130 { "Rgba16Snorm", { "rgba16_snorm", 4, 8, tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT } },
131 { "Rg16Snorm", { "rg16_snorm", 2, 4, tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT } },
132 { "R16Snorm", { "r16_snorm", 1, 2, tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT } },
133 { "Rgb10A2", { "rgb10_a2", 4, 4, tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT } },
134 { "R11fG11fB10f", { "r11f_g11f_b10f", 3, 4, tcu::TEXTURECHANNELCLASS_FLOATING_POINT } },
135 { "Rgba8", { nullptr, 4, 4, tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT } },
136 { "Rg8", { nullptr, 2, 2, tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT } },
137 { "R8", { nullptr, 1, 1, tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT } },
138 { "Rgba8Snorm", { "rgba8_snorm", 4, 4, tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT } },
139 { "Rg8Snorm", { "rg8_snorm", 2, 2, tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT } },
140 { "R8Snorm", { "r8_snorm", 1, 1, tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT } },
141 { "Rgba32i", { nullptr, 4, 16, tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER } },
142 { "Rg32i", { nullptr, 2, 2, tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER } },
143 { "R32i", { nullptr, 1, 1, tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER } },
144 { "Rgba16i", { nullptr, 4, 8, tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER } },
145 { "Rg16i", { nullptr, 2, 4, tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER } },
146 { "R16i", { nullptr, 1, 2, tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER } },
147 { "Rgba8i", { nullptr, 4, 4, tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER } },
148 { "Rg8i", { nullptr, 2, 2, tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER } },
149 { "R8i", { nullptr, 1, 1, tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER } },
150 { "Rgba32ui", { nullptr, 4, 16, tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER } },
151 { "Rg32ui", { nullptr, 2, 8, tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER } },
152 { "R32ui", { nullptr, 1, 4, tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER } },
153 { "Rgba16ui", { nullptr, 4, 8, tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER } },
154 { "Rg16ui", { nullptr, 2, 4, tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER } },
155 { "R16ui", { nullptr, 1, 2, tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER } },
156 { "Rgb10a2ui", { "rgb10_a2ui", 4, 4, tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER } },
157 { "Rgba8ui", { nullptr, 4, 4, tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER } },
158 { "Rg8ui", { nullptr, 2, 2, tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER } },
159 { "R8ui", { nullptr, 1, 1, tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER } }
162 FormatInfo getFormatInfo (const std::string& spirvFormat)
164 auto it = SpirvFormats.find(spirvFormat);
165 if (it != SpirvFormats.end()) return it->second;
166 else return {"", 0, 0, tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT};
169 bool matching (VkFormat format, const std::string& spirvFormat)
173 FormatInfo baseFormat = getFormatInfo(format);
174 FormatInfo shaderFormat = getFormatInfo(spirvFormat);
176 return (baseFormat.VectorWidth == shaderFormat.VectorWidth &&
177 baseFormat.BytesPerPixel == shaderFormat.BytesPerPixel &&
178 baseFormat.ChannelClass == shaderFormat.ChannelClass);
180 catch (const tcu::InternalError&)
193 class MismatchedFormatTest : public TestCase
196 MismatchedFormatTest (tcu::TestContext& testCtx,
197 const std::string& name,
198 const std::string& description,
200 const VkFormat format,
201 const std::string& spirvFormat);
203 virtual void checkSupport (Context& context) const;
204 void initPrograms (SourceCollections& programCollection) const;
205 TestInstance* createInstance (Context& context) const;
208 const TestType m_type;
209 const VkFormat m_format;
210 const std::string m_spirvFormat;
213 MismatchedFormatTest::MismatchedFormatTest (tcu::TestContext& testCtx,
214 const std::string& name,
215 const std::string& description,
217 const VkFormat format,
218 const std::string& spirvFormat)
219 : TestCase (testCtx, name, description)
222 , m_spirvFormat (spirvFormat)
226 void MismatchedFormatTest::checkSupport (Context& context) const
228 if (m_type == TestType::SPARSE_READ)
230 context.requireDeviceCoreFeature(DEVICE_CORE_FEATURE_SPARSE_BINDING);
232 if (!getPhysicalDeviceFeatures(context.getInstanceInterface(), context.getPhysicalDevice()).sparseResidencyBuffer)
234 TCU_THROW(NotSupportedError, "Sparse partially resident buffers not supported");
237 if (!getPhysicalDeviceFeatures(context.getInstanceInterface(), context.getPhysicalDevice()).shaderResourceResidency)
239 TCU_THROW(NotSupportedError, "Shader resource residency not supported");
243 VkFormatProperties formatProperties = getPhysicalDeviceFormatProperties(context.getInstanceInterface(), context.getPhysicalDevice(), m_format);
245 if ((formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) == 0)
247 TCU_THROW(NotSupportedError, "Creating storage image with this format is not supported");
251 void MismatchedFormatTest::initPrograms (SourceCollections& programCollection) const
255 if (m_type == TestType::READ)
260 layout (${FORMAT}, binding=0) uniform ${IMAGE} inputImage;
264 ${VECT} value = imageLoad(inputImage, ivec2(gl_GlobalInvocationID.xy));
268 else if (m_type == TestType::WRITE)
273 layout (${FORMAT}, binding=0) uniform ${IMAGE} inputImage;
277 imageStore(inputImage, ivec2(gl_GlobalInvocationID.xy), ${VALUE});
281 else if (m_type == TestType::SPARSE_READ)
285 #extension GL_ARB_sparse_texture2 : require
287 layout (${FORMAT}, binding=0) uniform ${IMAGE} inputImage;
292 int r = sparseImageLoadARB(inputImage, ivec2(gl_GlobalInvocationID.xy), result);
297 const FormatInfo spirvFormatInfo = getFormatInfo(m_spirvFormat);
299 const std::string glslFormat = spirvFormatInfo.GLSLFormat ?
300 spirvFormatInfo.GLSLFormat : de::toLower(m_spirvFormat);
302 std::map<std::string, std::string> specializations;
304 specializations["FORMAT"] = glslFormat;
305 specializations["VECT"] = ChannelClassToVecType(spirvFormatInfo.ChannelClass);
306 specializations["IMAGE"] = ChannelClassToImageType(spirvFormatInfo.ChannelClass);
307 specializations["VALUE"] = ChannelClassToDefaultVecValue(spirvFormatInfo.ChannelClass);
309 programCollection.glslSources.add("comp") << glu::ComputeSource( tcu::StringTemplate{source}.specialize(specializations) );
312 class MismatchedFormatTestInstance : public TestInstance
315 MismatchedFormatTestInstance (Context& context,
317 const VkFormat format,
318 const std::string& spirvFormat);
320 tcu::TestStatus iterate (void);
323 const TestType m_type;
324 const VkFormat m_format;
325 const std::string m_spirvFormat;
329 MismatchedFormatTestInstance::MismatchedFormatTestInstance (Context& context, const TestType type, const VkFormat format, const std::string& spirvFormat)
330 : TestInstance (context)
333 , m_spirvFormat (spirvFormat)
337 tcu::TestStatus MismatchedFormatTestInstance::iterate (void)
339 const DeviceInterface& vk = m_context.getDeviceInterface();
340 const VkDevice device = m_context.getDevice();
341 const VkQueue queue = m_context.getUniversalQueue();
342 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
344 Move<VkShaderModule> shaderModule = createShaderModule(vk, device, m_context.getBinaryCollection().get("comp"), 0);
346 Move<VkDescriptorSetLayout> descriptorSetLayout = DescriptorSetLayoutBuilder()
347 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
349 Move<VkDescriptorPool> descriptorPool = DescriptorPoolBuilder()
350 .addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)
351 .build(vk, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
353 Move<VkDescriptorSet> descriptorSet = makeDescriptorSet(vk, device, *descriptorPool, *descriptorSetLayout);
354 Move<VkPipelineLayout> pipelineLayout = makePipelineLayout(vk, device, descriptorSetLayout.get());
356 Move<VkPipeline> pipeline = makeComputePipeline(vk, device, *pipelineLayout, *shaderModule);
358 VkImageCreateFlags imageFlag = m_type == TestType::SPARSE_READ ? (VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) : 0u;
360 const VkImageCreateInfo imageCreateInfo =
362 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType;
363 DE_NULL, // const void* pNext;
364 imageFlag, // VkImageCreateFlags flags;
365 VK_IMAGE_TYPE_2D, // VkImageType imageType;
366 m_format, // VkFormat format;
367 makeExtent3D(8, 8, 1), // VkExtent3D extent;
368 1u, // deUint32 mipLevels;
369 1u, // deUint32 arrayLayers;
370 VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples;
371 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling tiling;
372 VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, // VkImageUsageFlags usage;
373 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
374 0u, // deUint32 queueFamilyIndexCount;
375 DE_NULL, // const deUint32* pQueueFamilyIndices;
376 VK_IMAGE_LAYOUT_UNDEFINED // VkImageLayout initialLayout;
379 vk::Move<vk::VkImage> storageImage = createImage(vk, device, &imageCreateInfo);
381 de::MovePtr<vk::Allocation> storageAllocation;
382 vk::Move<vk::VkSemaphore> bindSemaphore;
383 std::vector<de::SharedPtr<Allocation> > allocations;
385 if (m_type == TestType::SPARSE_READ)
387 bindSemaphore = createSemaphore(m_context.getDeviceInterface(), m_context.getDevice());
389 allocateAndBindSparseImage( vk, device, m_context.getPhysicalDevice(), m_context.getInstanceInterface(),
390 imageCreateInfo, *bindSemaphore, m_context.getSparseQueue(),
391 m_context.getDefaultAllocator(), allocations, mapVkFormat(m_format), *storageImage );
395 storageAllocation = m_context.getDefaultAllocator().allocate(getImageMemoryRequirements(vk, device, *storageImage), MemoryRequirement::Any);
396 VK_CHECK(vk.bindImageMemory(device, *storageImage, storageAllocation->getMemory(), storageAllocation->getOffset()));
399 Move<VkImageView> storageImageView = makeImageView(vk, device, *storageImage, VK_IMAGE_VIEW_TYPE_2D, m_format, makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u));
400 VkDescriptorImageInfo storageImageInfo = makeDescriptorImageInfo(DE_NULL, *storageImageView, VK_IMAGE_LAYOUT_GENERAL);
402 DescriptorSetUpdateBuilder builder;
404 .writeSingle(*descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &storageImageInfo)
407 Move<VkCommandPool> cmdPool = createCommandPool(vk, device, VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, queueFamilyIndex);
408 Move<VkCommandBuffer> cmdBuffer = allocateCommandBuffer(vk, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
410 beginCommandBuffer(vk, *cmdBuffer);
411 vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipeline);
412 vk.cmdBindDescriptorSets(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineLayout, 0u, 1u, &descriptorSet.get(), 0u, DE_NULL);
413 vk.cmdDispatch(*cmdBuffer, 8, 8, 1);
414 endCommandBuffer(vk, *cmdBuffer);
416 submitCommandsAndWait(vk, device, queue, *cmdBuffer);
418 return tcu::TestStatus::pass("Passed");
421 TestInstance* MismatchedFormatTest::createInstance (Context& context) const
423 return new MismatchedFormatTestInstance(context, m_type, m_format, m_spirvFormat);
428 tcu::TestCaseGroup* createImageMismatchedFormatsTests (tcu::TestContext& testCtx)
430 de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "mismatched_formats", "Test image load/store operations on mismatched formats"));
431 de::MovePtr<tcu::TestCaseGroup> testGroupOpRead(new tcu::TestCaseGroup(testCtx, "image_read", "perform OpImageRead"));
432 de::MovePtr<tcu::TestCaseGroup> testGroupOpWrite(new tcu::TestCaseGroup(testCtx, "image_write", "perform OpImageWrite"));
433 de::MovePtr<tcu::TestCaseGroup> testGroupOpSparseRead(new tcu::TestCaseGroup(testCtx, "sparse_image_read", "perform OpSparseImageRead"));
435 for (VkFormat format = VK_FORMAT_R4G4_UNORM_PACK8; format < VK_CORE_FORMAT_LAST; format = static_cast<VkFormat>(format+1))
437 for (auto& pair : SpirvFormats)
439 const std::string& spirvFormat = pair.first;
441 if (matching(format, spirvFormat))
443 const std::string enumName = getFormatName(format);
444 const std::string testName = de::toLower( enumName.substr(10) + "_with_" + spirvFormat );
446 testGroupOpRead->addChild(new MismatchedFormatTest( testCtx, testName, "",
448 format, spirvFormat) );
450 testGroupOpWrite->addChild(new MismatchedFormatTest(testCtx, testName, "",
452 format, spirvFormat) );
454 testGroupOpSparseRead->addChild(new MismatchedFormatTest( testCtx, testName, "",
455 TestType::SPARSE_READ,
456 format, spirvFormat) );
461 testGroup->addChild(testGroupOpRead.release());
462 testGroup->addChild(testGroupOpWrite.release());
463 testGroup->addChild(testGroupOpSparseRead.release());
465 return testGroup.release();