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 PlanarFormatDescription getPlanarFormatDescription (VkFormat format); 140 int getPlaneCount (VkFormat format); 141 deUint32 getMipmapCount (VkFormat format, 142 const vk::PlanarFormatDescription& formatDescription, 143 const vk::VkImageFormatProperties& imageFormatProperties, 144 const vk::VkExtent3D& extent); 145 146 deUint32 getPlaneSizeInBytes (const PlanarFormatDescription& formatInfo, 147 const VkExtent3D& baseExtents, 148 const deUint32 planeNdx, 149 const deUint32 mipmapLevel, 150 const deUint32 mipmapMemoryAlignment); 151 deUint32 getPlaneSizeInBytes (const PlanarFormatDescription& formatInfo, 152 const tcu::UVec2& baseExtents, 153 const deUint32 planeNdx, 154 const deUint32 mipmapLevel, 155 const deUint32 mipmapMemoryAlignment); 156 VkExtent3D getPlaneExtent (const PlanarFormatDescription& formatInfo, 157 const VkExtent3D& baseExtents, 158 const deUint32 planeNdx, 159 const deUint32 mipmapLevel); 160 tcu::UVec2 getPlaneExtent (const PlanarFormatDescription& formatInfo, 161 const tcu::UVec2& baseExtents, 162 const deUint32 planeNdx, 163 const deUint32 mipmapLevel); 164 tcu::UVec3 getImageSizeAlignment (VkFormat format); 165 tcu::UVec3 getImageSizeAlignment (const PlanarFormatDescription& formatInfo); 166 tcu::UVec2 getBlockExtent (VkFormat format); 167 tcu::UVec2 getBlockExtent (const PlanarFormatDescription& formatInfo); 168 VkFormat getPlaneCompatibleFormat (VkFormat format, 169 deUint32 planeNdx); 170 VkFormat getPlaneCompatibleFormat (const PlanarFormatDescription& formatInfo, 171 deUint32 planeNdx); 172 173 VkImageAspectFlagBits getPlaneAspect (deUint32 planeNdx); 174 deUint32 getAspectPlaneNdx (VkImageAspectFlagBits planeAspect); 175 bool isChromaSubsampled (VkFormat format); 176 bool isYCbCr422Format (VkFormat format); 177 bool isYCbCr420Format (VkFormat format); 178 179 tcu::PixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo, 180 const tcu::UVec2& size, 181 const deUint32* planeRowPitches, 182 void* const* planePtrs, 183 deUint32 channelNdx); 184 tcu::ConstPixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo, 185 const tcu::UVec2& size, 186 const deUint32* planeRowPitches, 187 const void* const* planePtrs, 188 deUint32 channelNdx); 189 tcu::PixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo, 190 const tcu::UVec3& size, 191 const deUint32* planeRowPitches, 192 void* const* planePtrs, 193 deUint32 channelNdx); 194 tcu::ConstPixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo, 195 const tcu::UVec3& size, 196 const deUint32* planeRowPitches, 197 const void* const* planePtrs, 198 deUint32 channelNdx); 199 VkImageAspectFlags getImageAspectFlags (const tcu::TextureFormat textureFormat); 200 VkExtent3D mipLevelExtents (const VkExtent3D& baseExtents, 201 const deUint32 mipLevel); 202 tcu::UVec3 alignedDivide (const VkExtent3D& extent, 203 const VkExtent3D& divisor); 204 205 /*--------------------------------------------------------------------*//*! 206 * Copies buffer data into an image. The buffer is expected to be 207 * in a state after host write. 208 *//*--------------------------------------------------------------------*/ 209 void copyBufferToImage (const DeviceInterface& vk, 210 vk::VkDevice device, 211 vk::VkQueue queue, 212 deUint32 queueFamilyIndex, 213 const vk::VkBuffer& buffer, 214 vk::VkDeviceSize bufferSize, 215 const std::vector<vk::VkBufferImageCopy>& copyRegions, 216 const vk::VkSemaphore* waitSemaphore, 217 vk::VkImageAspectFlags imageAspectFlags, 218 deUint32 mipLevels, 219 deUint32 arrayLayers, 220 vk::VkImage destImage, 221 VkImageLayout destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 222 VkPipelineStageFlags destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); 223 224 void copyBufferToImage (const DeviceInterface& vk, 225 const VkCommandBuffer& cmdBuffer, 226 const VkBuffer& buffer, 227 vk::VkDeviceSize bufferSize, 228 const std::vector<VkBufferImageCopy>& copyRegions, 229 VkImageAspectFlags imageAspectFlags, 230 deUint32 mipLevels, 231 deUint32 arrayLayers, 232 VkImage destImage, 233 VkImageLayout destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 234 VkPipelineStageFlags destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); 235 236 /*--------------------------------------------------------------------*//*! 237 * Copies image data into a buffer. The buffer is expected to be 238 * read by the host. 239 *//*--------------------------------------------------------------------*/ 240 void copyImageToBuffer (const DeviceInterface& vk, 241 vk::VkCommandBuffer cmdBuffer, 242 vk::VkImage image, 243 vk::VkBuffer buffer, 244 tcu::IVec2 size, 245 vk::VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, 246 vk::VkImageLayout oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 247 deUint32 numLayers = 1u, 248 VkImageAspectFlags barrierAspect = VK_IMAGE_ASPECT_COLOR_BIT, 249 VkImageAspectFlags copyAspect = VK_IMAGE_ASPECT_COLOR_BIT); 250 251 void copyImageToBuffer (const DeviceInterface& vk, 252 vk::VkCommandBuffer cmdBuffer, 253 vk::VkImage image, 254 vk::VkBuffer buffer, 255 vk::VkFormat format, 256 tcu::IVec2 size, 257 deUint32 mipLevel = 0u, 258 vk::VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, 259 vk::VkImageLayout oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 260 deUint32 numLayers = 1u, 261 VkImageAspectFlags barrierAspect = VK_IMAGE_ASPECT_COLOR_BIT, 262 VkImageAspectFlags copyAspect = VK_IMAGE_ASPECT_COLOR_BIT); 263 264 /*--------------------------------------------------------------------*//*! 265 * Clear a color image 266 *//*--------------------------------------------------------------------*/ 267 void clearColorImage (const DeviceInterface& vk, 268 const vk::VkDevice device, 269 const vk::VkQueue queue, 270 deUint32 queueFamilyIndex, 271 vk::VkImage image, 272 tcu::Vec4 clearColor, 273 vk::VkImageLayout oldLayout, 274 vk::VkImageLayout newLayout, 275 vk::VkPipelineStageFlags dstStageFlags, 276 deUint32 baseArrayLayer = 0u, 277 deUint32 layerCount = 1u); 278 279 void clearColorImage (const DeviceInterface& vk, 280 const vk::VkDevice device, 281 const vk::VkQueue queue, 282 deUint32 queueFamilyIndex, 283 vk::VkImage image, 284 vk::VkClearColorValue clearColor, 285 vk::VkImageLayout oldLayout, 286 vk::VkImageLayout newLayout, 287 vk::VkPipelineStageFlags dstStageFlags, 288 deUint32 baseArrayLayer = 0u, 289 deUint32 layerCount = 1u); 290 291 /*--------------------------------------------------------------------*//*! 292 * Initialize color image with a chessboard pattern 293 *//*--------------------------------------------------------------------*/ 294 void initColorImageChessboardPattern (const DeviceInterface& vk, 295 const vk::VkDevice device, 296 const vk::VkQueue queue, 297 deUint32 queueFamilyIndex, 298 Allocator& allocator, 299 vk::VkImage image, 300 vk::VkFormat format, 301 tcu::Vec4 colorValue0, 302 tcu::Vec4 colorValue1, 303 deUint32 imageWidth, 304 deUint32 imageHeight, 305 deUint32 tileSize, 306 vk::VkImageLayout oldLayout, 307 vk::VkImageLayout newLayout, 308 vk::VkPipelineStageFlags dstStageFlags); 309 310 /*--------------------------------------------------------------------*//*! 311 * Copies depth/stencil image data into two separate buffers. 312 * The buffers are expected to be read by the host. 313 *//*--------------------------------------------------------------------*/ 314 void copyDepthStencilImageToBuffers (const DeviceInterface& vk, 315 vk::VkCommandBuffer cmdBuffer, 316 vk::VkImage image, 317 vk::VkBuffer depthBuffer, 318 vk::VkBuffer stencilBuffer, 319 tcu::IVec2 size, 320 vk::VkAccessFlags srcAccessMask, 321 vk::VkImageLayout oldLayout, 322 deUint32 numLayers = 1u); 323 324 /*--------------------------------------------------------------------*//*! 325 * Clear a depth/stencil image 326 *//*--------------------------------------------------------------------*/ 327 void clearDepthStencilImage (const DeviceInterface& vk, 328 const vk::VkDevice device, 329 const vk::VkQueue queue, 330 deUint32 queueFamilyIndex, 331 vk::VkImage image, 332 float depthValue, 333 deUint32 stencilValue, 334 vk::VkImageLayout oldLayout, 335 vk::VkImageLayout newLayout, 336 vk::VkPipelineStageFlags dstStageFlags); 337 338 /*--------------------------------------------------------------------*//*! 339 * Initialize depth and stencil channels with a chessboard pattern 340 *//*--------------------------------------------------------------------*/ 341 void initDepthStencilImageChessboardPattern (const DeviceInterface& vk, 342 const vk::VkDevice device, 343 const vk::VkQueue queue, 344 deUint32 queueFamilyIndex, 345 Allocator& allocator, 346 vk::VkImage image, 347 vk::VkFormat format, 348 float depthValue0, 349 float depthValue1, 350 deUint32 stencilValue0, 351 deUint32 stencilValue1, 352 deUint32 imageWidth, 353 deUint32 imageHeight, 354 deUint32 tileSize, 355 vk::VkImageLayout oldLayout, 356 vk::VkImageLayout newLayout, 357 vk::VkPipelineStageFlags dstStageFlags); 358 359 /*--------------------------------------------------------------------*//*! 360 * Checks if the physical device supports creation of the specified 361 * image format. 362 *//*--------------------------------------------------------------------*/ 363 bool checkSparseImageFormatSupport (const VkPhysicalDevice physicalDevice, 364 const InstanceInterface& instance, 365 const VkFormat format, 366 const VkImageType imageType, 367 const VkSampleCountFlagBits sampleCount, 368 const VkImageUsageFlags usageFlags, 369 const VkImageTiling imageTiling); 370 371 bool checkSparseImageFormatSupport (const vk::VkPhysicalDevice physicalDevice, 372 const vk::InstanceInterface& instance, 373 const vk::VkImageCreateInfo& imageCreateInfo); 374 375 /*--------------------------------------------------------------------*//*! 376 * Allocates memory for a sparse image and handles the memory binding. 377 *//*--------------------------------------------------------------------*/ 378 void allocateAndBindSparseImage (const vk::DeviceInterface& vk, 379 vk::VkDevice device, 380 const vk::VkPhysicalDevice physicalDevice, 381 const vk::InstanceInterface& instance, 382 const vk::VkImageCreateInfo& imageCreateInfo, 383 const vk::VkSemaphore& signalSemaphore, 384 vk::VkQueue queue, 385 vk::Allocator& allocator, 386 std::vector<de::SharedPtr<vk::Allocation> >& allocations, 387 tcu::TextureFormat format, 388 vk::VkImage destImage); 389 390 } // vk 391 392 #endif // _VKIMAGEUTIL_HPP 393