1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2016 The Khronos Group Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Image Tests Utility Classes
22 *//*--------------------------------------------------------------------*/
24 #include "vktImageTestsUtil.hpp"
25 #include "vkQueryUtil.hpp"
26 #include "vkTypeUtil.hpp"
27 #include "tcuTextureUtil.hpp"
36 Buffer::Buffer (const DeviceInterface& vk,
37 const VkDevice device,
39 const VkBufferCreateInfo& bufferCreateInfo,
40 const MemoryRequirement memoryRequirement)
42 m_buffer = createBuffer(vk, device, &bufferCreateInfo);
43 m_allocation = allocator.allocate(getBufferMemoryRequirements(vk, device, *m_buffer), memoryRequirement);
44 VK_CHECK(vk.bindBufferMemory(device, *m_buffer, m_allocation->getMemory(), m_allocation->getOffset()));
47 Image::Image (const DeviceInterface& vk,
48 const VkDevice device,
50 const VkImageCreateInfo& imageCreateInfo,
51 const MemoryRequirement memoryRequirement)
53 m_image = createImage(vk, device, &imageCreateInfo);
54 m_allocation = allocator.allocate(getImageMemoryRequirements(vk, device, *m_image), memoryRequirement);
55 VK_CHECK(vk.bindImageMemory(device, *m_image, m_allocation->getMemory(), m_allocation->getOffset()));
58 tcu::UVec3 getShaderGridSize (const ImageType imageType, const tcu::UVec3& imageSize)
63 case IMAGE_TYPE_BUFFER:
64 return tcu::UVec3(imageSize.x(), 1u, 1u);
66 case IMAGE_TYPE_1D_ARRAY:
67 return tcu::UVec3(imageSize.x(), imageSize.z(), 1u);
70 return tcu::UVec3(imageSize.x(), imageSize.y(), 1u);
72 case IMAGE_TYPE_2D_ARRAY:
74 return tcu::UVec3(imageSize.x(), imageSize.y(), imageSize.z());
77 return tcu::UVec3(imageSize.x(), imageSize.y(), 6u);
79 case IMAGE_TYPE_CUBE_ARRAY:
80 return tcu::UVec3(imageSize.x(), imageSize.y(), 6u * imageSize.z());
83 DE_FATAL("Unknown image type");
84 return tcu::UVec3(1u, 1u, 1u);
88 tcu::UVec3 getLayerSize (const ImageType imageType, const tcu::UVec3& imageSize)
93 case IMAGE_TYPE_1D_ARRAY:
94 case IMAGE_TYPE_BUFFER:
95 return tcu::UVec3(imageSize.x(), 1u, 1u);
98 case IMAGE_TYPE_2D_ARRAY:
100 case IMAGE_TYPE_CUBE_ARRAY:
101 return tcu::UVec3(imageSize.x(), imageSize.y(), 1u);
104 return tcu::UVec3(imageSize.x(), imageSize.y(), imageSize.z());
107 DE_FATAL("Unknown image type");
108 return tcu::UVec3(1u, 1u, 1u);
112 deUint32 getNumLayers (const ImageType imageType, const tcu::UVec3& imageSize)
119 case IMAGE_TYPE_BUFFER:
122 case IMAGE_TYPE_1D_ARRAY:
123 case IMAGE_TYPE_2D_ARRAY:
124 return imageSize.z();
126 case IMAGE_TYPE_CUBE:
129 case IMAGE_TYPE_CUBE_ARRAY:
130 return imageSize.z() * 6u;
133 DE_FATAL("Unknown image type");
138 deUint32 getNumPixels (const ImageType imageType, const tcu::UVec3& imageSize)
140 const tcu::UVec3 gridSize = getShaderGridSize(imageType, imageSize);
142 return gridSize.x() * gridSize.y() * gridSize.z();
145 deUint32 getDimensions (const ImageType imageType)
150 case IMAGE_TYPE_BUFFER:
153 case IMAGE_TYPE_1D_ARRAY:
157 case IMAGE_TYPE_2D_ARRAY:
158 case IMAGE_TYPE_CUBE:
159 case IMAGE_TYPE_CUBE_ARRAY:
164 DE_FATAL("Unknown image type");
169 deUint32 getLayerDimensions (const ImageType imageType)
174 case IMAGE_TYPE_BUFFER:
175 case IMAGE_TYPE_1D_ARRAY:
179 case IMAGE_TYPE_2D_ARRAY:
180 case IMAGE_TYPE_CUBE:
181 case IMAGE_TYPE_CUBE_ARRAY:
188 DE_FATAL("Unknown image type");
193 VkBufferCreateInfo makeBufferCreateInfo (const VkDeviceSize bufferSize,
194 const VkBufferUsageFlags usage)
196 const VkBufferCreateInfo bufferCreateInfo =
198 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
199 DE_NULL, // const void* pNext;
200 0u, // VkBufferCreateFlags flags;
201 bufferSize, // VkDeviceSize size;
202 usage, // VkBufferUsageFlags usage;
203 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
204 0u, // deUint32 queueFamilyIndexCount;
205 DE_NULL, // const deUint32* pQueueFamilyIndices;
207 return bufferCreateInfo;
210 VkBufferImageCopy makeBufferImageCopy (const VkExtent3D extent,
211 const deUint32 arraySize)
213 const VkBufferImageCopy copyParams =
215 0ull, // VkDeviceSize bufferOffset;
216 0u, // deUint32 bufferRowLength;
217 0u, // deUint32 bufferImageHeight;
218 makeImageSubresourceLayers(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 0u, arraySize), // VkImageSubresourceLayers imageSubresource;
219 makeOffset3D(0, 0, 0), // VkOffset3D imageOffset;
220 extent, // VkExtent3D imageExtent;
225 Move<VkPipelineLayout> makePipelineLayout (const DeviceInterface& vk,
226 const VkDevice device,
227 const VkDescriptorSetLayout descriptorSetLayout)
229 const VkPipelineLayoutCreateInfo pipelineLayoutParams =
231 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, // VkStructureType sType;
232 DE_NULL, // const void* pNext;
233 0u, // VkPipelineLayoutCreateFlags flags;
234 1u, // deUint32 setLayoutCount;
235 &descriptorSetLayout, // const VkDescriptorSetLayout* pSetLayouts;
236 0u, // deUint32 pushConstantRangeCount;
237 DE_NULL, // const VkPushConstantRange* pPushConstantRanges;
239 return createPipelineLayout(vk, device, &pipelineLayoutParams);
242 Move<VkPipeline> makeComputePipeline (const DeviceInterface& vk,
243 const VkDevice device,
244 const VkPipelineLayout pipelineLayout,
245 const VkShaderModule shaderModule)
247 const VkPipelineShaderStageCreateInfo pipelineShaderStageParams =
249 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, // VkStructureType sType;
250 DE_NULL, // const void* pNext;
251 0u, // VkPipelineShaderStageCreateFlags flags;
252 VK_SHADER_STAGE_COMPUTE_BIT, // VkShaderStageFlagBits stage;
253 shaderModule, // VkShaderModule module;
254 "main", // const char* pName;
255 DE_NULL, // const VkSpecializationInfo* pSpecializationInfo;
257 const VkComputePipelineCreateInfo pipelineCreateInfo =
259 VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, // VkStructureType sType;
260 DE_NULL, // const void* pNext;
261 0u, // VkPipelineCreateFlags flags;
262 pipelineShaderStageParams, // VkPipelineShaderStageCreateInfo stage;
263 pipelineLayout, // VkPipelineLayout layout;
264 DE_NULL, // VkPipeline basePipelineHandle;
265 0, // deInt32 basePipelineIndex;
267 return createComputePipeline(vk, device, DE_NULL , &pipelineCreateInfo);
270 Move<VkBufferView> makeBufferView (const DeviceInterface& vk,
271 const VkDevice vkDevice,
272 const VkBuffer buffer,
273 const VkFormat format,
274 const VkDeviceSize offset,
275 const VkDeviceSize size)
277 const VkBufferViewCreateInfo bufferViewParams =
279 VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO, // VkStructureType sType;
280 DE_NULL, // const void* pNext;
281 0u, // VkBufferViewCreateFlags flags;
282 buffer, // VkBuffer buffer;
283 format, // VkFormat format;
284 offset, // VkDeviceSize offset;
285 size, // VkDeviceSize range;
287 return createBufferView(vk, vkDevice, &bufferViewParams);
290 Move<VkImageView> makeImageView (const DeviceInterface& vk,
291 const VkDevice vkDevice,
293 const VkImageViewType imageViewType,
294 const VkFormat format,
295 const VkImageSubresourceRange subresourceRange)
297 const VkImageViewCreateInfo imageViewParams =
299 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
300 DE_NULL, // const void* pNext;
301 0u, // VkImageViewCreateFlags flags;
302 image, // VkImage image;
303 imageViewType, // VkImageViewType viewType;
304 format, // VkFormat format;
305 makeComponentMappingRGBA(), // VkComponentMapping components;
306 subresourceRange, // VkImageSubresourceRange subresourceRange;
308 return createImageView(vk, vkDevice, &imageViewParams);
311 Move<VkDescriptorSet> makeDescriptorSet (const DeviceInterface& vk,
312 const VkDevice device,
313 const VkDescriptorPool descriptorPool,
314 const VkDescriptorSetLayout setLayout)
316 const VkDescriptorSetAllocateInfo allocateParams =
318 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, // VkStructureType sType;
319 DE_NULL, // const void* pNext;
320 descriptorPool, // VkDescriptorPool descriptorPool;
321 1u, // deUint32 setLayoutCount;
322 &setLayout, // const VkDescriptorSetLayout* pSetLayouts;
324 return allocateDescriptorSet(vk, device, &allocateParams);
327 VkBufferMemoryBarrier makeBufferMemoryBarrier (const VkAccessFlags srcAccessMask,
328 const VkAccessFlags dstAccessMask,
329 const VkBuffer buffer,
330 const VkDeviceSize offset,
331 const VkDeviceSize bufferSizeBytes)
333 const VkBufferMemoryBarrier barrier =
335 VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, // VkStructureType sType;
336 DE_NULL, // const void* pNext;
337 srcAccessMask, // VkAccessFlags srcAccessMask;
338 dstAccessMask, // VkAccessFlags dstAccessMask;
339 VK_QUEUE_FAMILY_IGNORED, // deUint32 srcQueueFamilyIndex;
340 VK_QUEUE_FAMILY_IGNORED, // deUint32 destQueueFamilyIndex;
341 buffer, // VkBuffer buffer;
342 offset, // VkDeviceSize offset;
343 bufferSizeBytes, // VkDeviceSize size;
348 VkImageMemoryBarrier makeImageMemoryBarrier (const VkAccessFlags srcAccessMask,
349 const VkAccessFlags dstAccessMask,
350 const VkImageLayout oldLayout,
351 const VkImageLayout newLayout,
353 const VkImageSubresourceRange subresourceRange)
355 const VkImageMemoryBarrier barrier =
357 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType;
358 DE_NULL, // const void* pNext;
359 srcAccessMask, // VkAccessFlags outputMask;
360 dstAccessMask, // VkAccessFlags inputMask;
361 oldLayout, // VkImageLayout oldLayout;
362 newLayout, // VkImageLayout newLayout;
363 VK_QUEUE_FAMILY_IGNORED, // deUint32 srcQueueFamilyIndex;
364 VK_QUEUE_FAMILY_IGNORED, // deUint32 destQueueFamilyIndex;
365 image, // VkImage image;
366 subresourceRange, // VkImageSubresourceRange subresourceRange;
371 void beginCommandBuffer (const DeviceInterface& vk, const VkCommandBuffer commandBuffer)
373 const VkCommandBufferBeginInfo commandBufBeginParams =
375 VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, // VkStructureType sType;
376 DE_NULL, // const void* pNext;
377 0u, // VkCommandBufferUsageFlags flags;
378 (const VkCommandBufferInheritanceInfo*)DE_NULL,
380 VK_CHECK(vk.beginCommandBuffer(commandBuffer, &commandBufBeginParams));
382 void endCommandBuffer (const DeviceInterface& vk, const VkCommandBuffer commandBuffer)
384 VK_CHECK(vk.endCommandBuffer(commandBuffer));
387 void submitCommandsAndWait (const DeviceInterface& vk,
388 const VkDevice device,
390 const VkCommandBuffer commandBuffer)
392 const Unique<VkFence> fence(createFence(vk, device));
394 const VkSubmitInfo submitInfo =
396 VK_STRUCTURE_TYPE_SUBMIT_INFO, // VkStructureType sType;
397 DE_NULL, // const void* pNext;
398 0u, // deUint32 waitSemaphoreCount;
399 DE_NULL, // const VkSemaphore* pWaitSemaphores;
400 (const VkPipelineStageFlags*)DE_NULL,
401 1u, // deUint32 commandBufferCount;
402 &commandBuffer, // const VkCommandBuffer* pCommandBuffers;
403 0u, // deUint32 signalSemaphoreCount;
404 DE_NULL, // const VkSemaphore* pSignalSemaphores;
407 VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
408 VK_CHECK(vk.waitForFences(device, 1u, &fence.get(), DE_TRUE, ~0ull));
411 VkImageType mapImageType (const ImageType imageType)
416 case IMAGE_TYPE_1D_ARRAY:
417 case IMAGE_TYPE_BUFFER:
418 return VK_IMAGE_TYPE_1D;
421 case IMAGE_TYPE_2D_ARRAY:
422 case IMAGE_TYPE_CUBE:
423 case IMAGE_TYPE_CUBE_ARRAY:
424 return VK_IMAGE_TYPE_2D;
427 return VK_IMAGE_TYPE_3D;
431 return VK_IMAGE_TYPE_LAST;
435 VkImageViewType mapImageViewType (const ImageType imageType)
439 case IMAGE_TYPE_1D: return VK_IMAGE_VIEW_TYPE_1D;
440 case IMAGE_TYPE_1D_ARRAY: return VK_IMAGE_VIEW_TYPE_1D_ARRAY;
441 case IMAGE_TYPE_2D: return VK_IMAGE_VIEW_TYPE_2D;
442 case IMAGE_TYPE_2D_ARRAY: return VK_IMAGE_VIEW_TYPE_2D_ARRAY;
443 case IMAGE_TYPE_3D: return VK_IMAGE_VIEW_TYPE_3D;
444 case IMAGE_TYPE_CUBE: return VK_IMAGE_VIEW_TYPE_CUBE;
445 case IMAGE_TYPE_CUBE_ARRAY: return VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
449 return VK_IMAGE_VIEW_TYPE_LAST;
453 std::string getImageTypeName (const ImageType imageType)
457 case IMAGE_TYPE_1D: return "1d";
458 case IMAGE_TYPE_1D_ARRAY: return "1d_array";
459 case IMAGE_TYPE_2D: return "2d";
460 case IMAGE_TYPE_2D_ARRAY: return "2d_array";
461 case IMAGE_TYPE_3D: return "3d";
462 case IMAGE_TYPE_CUBE: return "cube";
463 case IMAGE_TYPE_CUBE_ARRAY: return "cube_array";
464 case IMAGE_TYPE_BUFFER: return "buffer";
472 std::string getShaderImageType (const tcu::TextureFormat& format, const ImageType imageType, const bool multisample)
474 std::string formatPart = tcu::getTextureChannelClass(format.type) == tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER ? "u" :
475 tcu::getTextureChannelClass(format.type) == tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER ? "i" : "";
477 std::string imageTypePart;
482 case IMAGE_TYPE_2D: imageTypePart = "2DMS"; break;
483 case IMAGE_TYPE_2D_ARRAY: imageTypePart = "2DMSArray"; break;
493 case IMAGE_TYPE_1D: imageTypePart = "1D"; break;
494 case IMAGE_TYPE_1D_ARRAY: imageTypePart = "1DArray"; break;
495 case IMAGE_TYPE_2D: imageTypePart = "2D"; break;
496 case IMAGE_TYPE_2D_ARRAY: imageTypePart = "2DArray"; break;
497 case IMAGE_TYPE_3D: imageTypePart = "3D"; break;
498 case IMAGE_TYPE_CUBE: imageTypePart = "Cube"; break;
499 case IMAGE_TYPE_CUBE_ARRAY: imageTypePart = "CubeArray"; break;
500 case IMAGE_TYPE_BUFFER: imageTypePart = "Buffer"; break;
507 return formatPart + "image" + imageTypePart;
510 std::string getShaderImageFormatQualifier (const tcu::TextureFormat& format)
512 const char* orderPart;
513 const char* typePart;
515 switch (format.order)
517 case tcu::TextureFormat::R: orderPart = "r"; break;
518 case tcu::TextureFormat::RG: orderPart = "rg"; break;
519 case tcu::TextureFormat::RGB: orderPart = "rgb"; break;
520 case tcu::TextureFormat::RGBA: orderPart = "rgba"; break;
529 case tcu::TextureFormat::FLOAT: typePart = "32f"; break;
530 case tcu::TextureFormat::HALF_FLOAT: typePart = "16f"; break;
532 case tcu::TextureFormat::UNSIGNED_INT32: typePart = "32ui"; break;
533 case tcu::TextureFormat::UNSIGNED_INT16: typePart = "16ui"; break;
534 case tcu::TextureFormat::UNSIGNED_INT8: typePart = "8ui"; break;
536 case tcu::TextureFormat::SIGNED_INT32: typePart = "32i"; break;
537 case tcu::TextureFormat::SIGNED_INT16: typePart = "16i"; break;
538 case tcu::TextureFormat::SIGNED_INT8: typePart = "8i"; break;
540 case tcu::TextureFormat::UNORM_INT16: typePart = "16"; break;
541 case tcu::TextureFormat::UNORM_INT8: typePart = "8"; break;
543 case tcu::TextureFormat::SNORM_INT16: typePart = "16_snorm"; break;
544 case tcu::TextureFormat::SNORM_INT8: typePart = "8_snorm"; break;
551 return std::string() + orderPart + typePart;
554 std::string getFormatShortString (const VkFormat format)
556 const std::string fullName = getFormatName(format);
558 DE_ASSERT(de::beginsWith(fullName, "VK_FORMAT_"));
560 return de::toLower(fullName.substr(10));