1 #ifndef _VKIMAGEUTIL_HPP 2 #define _VKIMAGEUTIL_HPP 3 /*------------------------------------------------------------------------- 4 * Vulkan CTS Framework 5 * -------------------- 6 * 7 * Copyright (c) 2015 The Khronos Group Inc. 8 * Copyright (c) 2015 Imagination Technologies Ltd. 9 * Copyright (c) 2015 Google Inc. 10 * 11 * Licensed under the Apache License, Version 2.0 (the "License"); 12 * you may not use this file except in compliance with the License. 13 * You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the License for the specific language governing permissions and 21 * limitations under the License. 22 * 23 *//*! 24 * \file 25 * \brief Utilities for images. 26 *//*--------------------------------------------------------------------*/ 27 28 #include "vkDefs.hpp" 29 #include "vkMemUtil.hpp" 30 #include "tcuTexture.hpp" 31 #include "tcuCompressedTexture.hpp" 32 #include "deSharedPtr.hpp" 33 34 namespace vk 35 { 36 37 bool isFloatFormat (VkFormat format); 38 bool isUfloatFormat (VkFormat format); 39 bool isSfloatFormat (VkFormat format); 40 bool isUnormFormat (VkFormat format); 41 bool isSnormFormat (VkFormat format); 42 bool isIntFormat (VkFormat format); 43 bool isUintFormat (VkFormat format); 44 bool isDepthStencilFormat (VkFormat format); 45 bool isCompressedFormat (VkFormat format); 46 bool isSrgbFormat (VkFormat format); 47 48 bool is64BitIntegerFormat (VkFormat format); 49 50 bool isSupportedByFramework (VkFormat format); 51 void checkImageSupport (const InstanceInterface& vki, VkPhysicalDevice physicalDevice, const VkImageCreateInfo& imageCreateInfo); 52 53 tcu::TextureFormat mapVkFormat (VkFormat format); 54 tcu::CompressedTexFormat mapVkCompressedFormat (VkFormat format); 55 tcu::TextureFormat getDepthCopyFormat (VkFormat combinedFormat); 56 tcu::TextureFormat getStencilCopyFormat (VkFormat combinedFormat); 57 58 tcu::Sampler mapVkSampler (const VkSamplerCreateInfo& samplerCreateInfo); 59 tcu::Sampler::CompareMode mapVkSamplerCompareOp (VkCompareOp compareOp); 60 tcu::Sampler::WrapMode mapVkSamplerAddressMode (VkSamplerAddressMode addressMode); 61 tcu::Sampler::ReductionMode mapVkSamplerReductionMode (VkSamplerReductionMode reductionMode); 62 tcu::Sampler::FilterMode mapVkMinTexFilter (VkFilter filter, VkSamplerMipmapMode mipMode); 63 tcu::Sampler::FilterMode mapVkMagTexFilter (VkFilter filter); 64 65 VkFilter mapFilterMode (tcu::Sampler::FilterMode filterMode); 66 VkSamplerMipmapMode mapMipmapMode (tcu::Sampler::FilterMode filterMode); 67 VkSamplerAddressMode mapWrapMode (tcu::Sampler::WrapMode wrapMode); 68 VkCompareOp mapCompareMode (tcu::Sampler::CompareMode mode); 69 VkFormat mapTextureFormat (const tcu::TextureFormat& format); 70 VkFormat mapCompressedTextureFormat (const tcu::CompressedTexFormat format); 71 VkSamplerCreateInfo mapSampler (const tcu::Sampler& sampler, const tcu::TextureFormat& format, float minLod = 0.0f, float maxLod = 1000.0f, bool unnormal = false); 72 rr::GenericVec4 mapVkColor (const VkClearColorValue& color); 73 VkClearColorValue mapVkColor (const rr::GenericVec4& color); 74 75 void imageUtilSelfTest (void); 76 77 float getRepresentableDiffUnorm (const VkFormat format, const deUint32 componentNdx); 78 float getRepresentableDiffSnorm (const VkFormat format, const deUint32 componentNdx); 79 deUint32 getFormatComponentWidth (const VkFormat format, const deUint32 componentNdx); 80 deUint32 getBlockSizeInBytes (const VkFormat compressedFormat); 81 deUint32 getBlockWidth (const VkFormat compressedFormat); 82 deUint32 getBlockHeight (const VkFormat compressedFormat); 83 84 bool hasSpirvFormat (VkFormat fmt); 85 const std::string getSpirvFormat (VkFormat fmt); 86 87 const deUint32 BUFFER_IMAGE_COPY_OFFSET_GRANULARITY = 4u; 88 89 // \todo [2017-05-18 pyry] Consider moving this to tcu 90 struct PlanarFormatDescription 91 { 92 enum 93 { 94 MAX_CHANNELS = 4, 95 MAX_PLANES = 3 96 }; 97 98 enum ChannelFlags 99 { 100 CHANNEL_R = (1u<<0), // Has "R" (0) channel 101 CHANNEL_G = (1u<<1), // Has "G" (1) channel 102 CHANNEL_B = (1u<<2), // Has "B" (2) channel 103 CHANNEL_A = (1u<<3), // Has "A" (3) channel 104 }; 105 106 struct Plane 107 { 108 deUint8 elementSizeBytes; 109 deUint8 widthDivisor; 110 deUint8 heightDivisor; 111 VkFormat planeCompatibleFormat; 112 }; 113 114 struct Channel 115 { 116 deUint8 planeNdx; 117 deUint8 type; // tcu::TextureChannelClass value 118 deUint8 offsetBits; // Offset in element in bits 119 deUint8 sizeBits; // Value size in bits 120 deUint8 strideBytes; // Pixel stride (in bytes), usually plane elementSize 121 }; 122 123 deUint8 numPlanes; 124 deUint8 presentChannels; 125 deUint8 blockWidth; 126 deUint8 blockHeight; 127 Plane planes[MAX_PLANES]; 128 Channel channels[MAX_CHANNELS]; 129 hasChannelNdxvk::PlanarFormatDescription130 inline bool hasChannelNdx (deUint32 ndx) const 131 { 132 DE_ASSERT(de::inBounds(ndx, 0u, 4u)); 133 return (presentChannels & (1u<<ndx)) != 0; 134 } 135 }; 136 137 bool isYCbCrFormat (VkFormat format); 138 bool isYCbCrExtensionFormat (VkFormat format); 139 bool isYCbCrConversionFormat (VkFormat format); 140 PlanarFormatDescription getPlanarFormatDescription (VkFormat format); 141 int getPlaneCount (VkFormat format); 142 deUint32 getMipmapCount (VkFormat format, 143 const vk::PlanarFormatDescription& formatDescription, 144 const vk::VkImageFormatProperties& imageFormatProperties, 145 const vk::VkExtent3D& extent); 146 147 deUint32 getPlaneSizeInBytes (const PlanarFormatDescription& formatInfo, 148 const VkExtent3D& baseExtents, 149 const deUint32 planeNdx, 150 const deUint32 mipmapLevel, 151 const deUint32 mipmapMemoryAlignment); 152 deUint32 getPlaneSizeInBytes (const PlanarFormatDescription& formatInfo, 153 const tcu::UVec2& baseExtents, 154 const deUint32 planeNdx, 155 const deUint32 mipmapLevel, 156 const deUint32 mipmapMemoryAlignment); 157 VkExtent3D getPlaneExtent (const PlanarFormatDescription& formatInfo, 158 const VkExtent3D& baseExtents, 159 const deUint32 planeNdx, 160 const deUint32 mipmapLevel); 161 tcu::UVec2 getPlaneExtent (const PlanarFormatDescription& formatInfo, 162 const tcu::UVec2& baseExtents, 163 const deUint32 planeNdx, 164 const deUint32 mipmapLevel); 165 tcu::UVec3 getImageSizeAlignment (VkFormat format); 166 tcu::UVec3 getImageSizeAlignment (const PlanarFormatDescription& formatInfo); 167 tcu::UVec2 getBlockExtent (VkFormat format); 168 tcu::UVec2 getBlockExtent (const PlanarFormatDescription& formatInfo); 169 VkFormat getPlaneCompatibleFormat (VkFormat format, 170 deUint32 planeNdx); 171 VkFormat getPlaneCompatibleFormat (const PlanarFormatDescription& formatInfo, 172 deUint32 planeNdx); 173 174 VkImageAspectFlagBits getPlaneAspect (deUint32 planeNdx); 175 deUint32 getAspectPlaneNdx (VkImageAspectFlagBits planeAspect); 176 bool isChromaSubsampled (VkFormat format); 177 bool isYCbCr422Format (VkFormat format); 178 bool isYCbCr420Format (VkFormat format); 179 180 tcu::PixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo, 181 const tcu::UVec2& size, 182 const deUint32* planeRowPitches, 183 void* const* planePtrs, 184 deUint32 channelNdx); 185 tcu::ConstPixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo, 186 const tcu::UVec2& size, 187 const deUint32* planeRowPitches, 188 const void* const* planePtrs, 189 deUint32 channelNdx); 190 tcu::PixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo, 191 const tcu::UVec3& size, 192 const deUint32* planeRowPitches, 193 void* const* planePtrs, 194 deUint32 channelNdx); 195 tcu::ConstPixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo, 196 const tcu::UVec3& size, 197 const deUint32* planeRowPitches, 198 const void* const* planePtrs, 199 deUint32 channelNdx); 200 VkImageAspectFlags getImageAspectFlags (const tcu::TextureFormat textureFormat); 201 VkExtent3D mipLevelExtents (const VkExtent3D& baseExtents, 202 const deUint32 mipLevel); 203 tcu::UVec3 alignedDivide (const VkExtent3D& extent, 204 const VkExtent3D& divisor); 205 206 /*--------------------------------------------------------------------*//*! 207 * Copies buffer data into an image. The buffer is expected to be 208 * in a state after host write. 209 *//*--------------------------------------------------------------------*/ 210 void copyBufferToImage (const DeviceInterface& vk, 211 vk::VkDevice device, 212 vk::VkQueue queue, 213 deUint32 queueFamilyIndex, 214 const vk::VkBuffer& buffer, 215 vk::VkDeviceSize bufferSize, 216 const std::vector<vk::VkBufferImageCopy>& copyRegions, 217 const vk::VkSemaphore* waitSemaphore, 218 vk::VkImageAspectFlags imageAspectFlags, 219 deUint32 mipLevels, 220 deUint32 arrayLayers, 221 vk::VkImage destImage, 222 VkImageLayout destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 223 VkPipelineStageFlags destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); 224 225 void copyBufferToImage (const DeviceInterface& vk, 226 const VkCommandBuffer& cmdBuffer, 227 const VkBuffer& buffer, 228 vk::VkDeviceSize bufferSize, 229 const std::vector<VkBufferImageCopy>& copyRegions, 230 VkImageAspectFlags imageAspectFlags, 231 deUint32 mipLevels, 232 deUint32 arrayLayers, 233 VkImage destImage, 234 VkImageLayout destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 235 VkPipelineStageFlags destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); 236 237 /*--------------------------------------------------------------------*//*! 238 * Copies image data into a buffer. The buffer is expected to be 239 * read by the host. 240 *//*--------------------------------------------------------------------*/ 241 void copyImageToBuffer (const DeviceInterface& vk, 242 vk::VkCommandBuffer cmdBuffer, 243 vk::VkImage image, 244 vk::VkBuffer buffer, 245 tcu::IVec2 size, 246 vk::VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, 247 vk::VkImageLayout oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 248 deUint32 numLayers = 1u, 249 VkImageAspectFlags barrierAspect = VK_IMAGE_ASPECT_COLOR_BIT, 250 VkImageAspectFlags copyAspect = VK_IMAGE_ASPECT_COLOR_BIT); 251 252 void copyImageToBuffer (const DeviceInterface& vk, 253 vk::VkCommandBuffer cmdBuffer, 254 vk::VkImage image, 255 vk::VkBuffer buffer, 256 vk::VkFormat format, 257 tcu::IVec2 size, 258 deUint32 mipLevel = 0u, 259 vk::VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, 260 vk::VkImageLayout oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 261 deUint32 numLayers = 1u, 262 VkImageAspectFlags barrierAspect = VK_IMAGE_ASPECT_COLOR_BIT, 263 VkImageAspectFlags copyAspect = VK_IMAGE_ASPECT_COLOR_BIT); 264 265 /*--------------------------------------------------------------------*//*! 266 * Clear a color image 267 *//*--------------------------------------------------------------------*/ 268 void clearColorImage (const DeviceInterface& vk, 269 const vk::VkDevice device, 270 const vk::VkQueue queue, 271 deUint32 queueFamilyIndex, 272 vk::VkImage image, 273 tcu::Vec4 clearColor, 274 vk::VkImageLayout oldLayout, 275 vk::VkImageLayout newLayout, 276 vk::VkPipelineStageFlags dstStageFlags, 277 deUint32 baseArrayLayer = 0u, 278 deUint32 layerCount = 1u); 279 280 void clearColorImage (const DeviceInterface& vk, 281 const vk::VkDevice device, 282 const vk::VkQueue queue, 283 deUint32 queueFamilyIndex, 284 vk::VkImage image, 285 vk::VkClearColorValue clearColor, 286 vk::VkImageLayout oldLayout, 287 vk::VkImageLayout newLayout, 288 vk::VkAccessFlags dstAccessFlags, 289 vk::VkPipelineStageFlags dstStageFlags, 290 deUint32 baseArrayLayer = 0u, 291 deUint32 layerCount = 1u); 292 293 /*--------------------------------------------------------------------*//*! 294 * Initialize color image with a chessboard pattern 295 *//*--------------------------------------------------------------------*/ 296 void initColorImageChessboardPattern (const DeviceInterface& vk, 297 const vk::VkDevice device, 298 const vk::VkQueue queue, 299 deUint32 queueFamilyIndex, 300 Allocator& allocator, 301 vk::VkImage image, 302 vk::VkFormat format, 303 tcu::Vec4 colorValue0, 304 tcu::Vec4 colorValue1, 305 deUint32 imageWidth, 306 deUint32 imageHeight, 307 deUint32 tileSize, 308 vk::VkImageLayout oldLayout, 309 vk::VkImageLayout newLayout, 310 vk::VkPipelineStageFlags dstStageFlags); 311 312 /*--------------------------------------------------------------------*//*! 313 * Copies depth/stencil image data into two separate buffers. 314 * The buffers are expected to be read by the host. 315 *//*--------------------------------------------------------------------*/ 316 void copyDepthStencilImageToBuffers (const DeviceInterface& vk, 317 vk::VkCommandBuffer cmdBuffer, 318 vk::VkImage image, 319 vk::VkBuffer depthBuffer, 320 vk::VkBuffer stencilBuffer, 321 tcu::IVec2 size, 322 vk::VkAccessFlags srcAccessMask, 323 vk::VkImageLayout oldLayout, 324 deUint32 numLayers = 1u); 325 326 /*--------------------------------------------------------------------*//*! 327 * Clear a depth/stencil image 328 *//*--------------------------------------------------------------------*/ 329 void clearDepthStencilImage (const DeviceInterface& vk, 330 const vk::VkDevice device, 331 const vk::VkQueue queue, 332 deUint32 queueFamilyIndex, 333 vk::VkImage image, 334 vk::VkFormat format, 335 float depthValue, 336 deUint32 stencilValue, 337 vk::VkImageLayout oldLayout, 338 vk::VkImageLayout newLayout, 339 vk::VkAccessFlags dstAccessFlags, 340 vk::VkPipelineStageFlags dstStageFlags); 341 342 /*--------------------------------------------------------------------*//*! 343 * Initialize depth and stencil channels with a chessboard pattern 344 *//*--------------------------------------------------------------------*/ 345 void initDepthStencilImageChessboardPattern (const DeviceInterface& vk, 346 const vk::VkDevice device, 347 const vk::VkQueue queue, 348 deUint32 queueFamilyIndex, 349 Allocator& allocator, 350 vk::VkImage image, 351 vk::VkFormat format, 352 float depthValue0, 353 float depthValue1, 354 deUint32 stencilValue0, 355 deUint32 stencilValue1, 356 deUint32 imageWidth, 357 deUint32 imageHeight, 358 deUint32 tileSize, 359 vk::VkImageLayout oldLayout, 360 vk::VkImageLayout newLayout, 361 vk::VkPipelineStageFlags dstStageFlags); 362 363 /*--------------------------------------------------------------------*//*! 364 * Checks if the physical device supports creation of the specified 365 * image format. 366 *//*--------------------------------------------------------------------*/ 367 bool checkSparseImageFormatSupport (const VkPhysicalDevice physicalDevice, 368 const InstanceInterface& instance, 369 const VkFormat format, 370 const VkImageType imageType, 371 const VkSampleCountFlagBits sampleCount, 372 const VkImageUsageFlags usageFlags, 373 const VkImageTiling imageTiling); 374 375 bool checkSparseImageFormatSupport (const vk::VkPhysicalDevice physicalDevice, 376 const vk::InstanceInterface& instance, 377 const vk::VkImageCreateInfo& imageCreateInfo); 378 379 /*--------------------------------------------------------------------*//*! 380 * Allocates memory for a sparse image and handles the memory binding. 381 *//*--------------------------------------------------------------------*/ 382 void allocateAndBindSparseImage (const vk::DeviceInterface& vk, 383 vk::VkDevice device, 384 const vk::VkPhysicalDevice physicalDevice, 385 const vk::InstanceInterface& instance, 386 const vk::VkImageCreateInfo& imageCreateInfo, 387 const vk::VkSemaphore& signalSemaphore, 388 vk::VkQueue queue, 389 vk::Allocator& allocator, 390 std::vector<de::SharedPtr<vk::Allocation> >& allocations, 391 tcu::TextureFormat format, 392 vk::VkImage destImage); 393 394 } // vk 395 396 #endif // _VKIMAGEUTIL_HPP 397