1 #ifndef _VKIMAGEUTIL_HPP
2 #define _VKIMAGEUTIL_HPP
3 /*-------------------------------------------------------------------------
7 * Copyright (c) 2015 The Khronos Group Inc.
8 * Copyright (c) 2015 Imagination Technologies Ltd.
9 * Copyright (c) 2015 Google Inc.
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 * \brief Utilities for images.
26 *//*--------------------------------------------------------------------*/
29 #include "tcuTexture.hpp"
30 #include "tcuCompressedTexture.hpp"
31 #include "deSharedPtr.hpp"
32 #include "vkImageWithMemory.hpp"
33 #include "vkBufferWithMemory.hpp"
34 #include "vkMemUtil.hpp"
35 #include "vkTypeUtil.hpp"
41 bool isFloatFormat (VkFormat format);
42 bool isUfloatFormat (VkFormat format);
43 bool isSfloatFormat (VkFormat format);
44 bool isUnormFormat (VkFormat format);
45 bool isSnormFormat (VkFormat format);
46 bool isIntFormat (VkFormat format);
47 bool isUintFormat (VkFormat format);
48 bool isScaledFormat (VkFormat format);
49 bool isDepthStencilFormat (VkFormat format);
50 bool isCompressedFormat (VkFormat format);
51 bool isSrgbFormat (VkFormat format);
53 bool is64BitIntegerFormat (VkFormat format);
55 bool isSupportedByFramework (VkFormat format);
56 void checkImageSupport (const InstanceInterface& vki, VkPhysicalDevice physicalDevice, const VkImageCreateInfo& imageCreateInfo);
58 tcu::TextureFormat mapVkFormat (VkFormat format);
59 tcu::CompressedTexFormat mapVkCompressedFormat (VkFormat format);
60 tcu::TextureFormat getDepthCopyFormat (VkFormat combinedFormat);
61 tcu::TextureFormat getStencilCopyFormat (VkFormat combinedFormat);
63 tcu::Sampler mapVkSampler (const VkSamplerCreateInfo& samplerCreateInfo);
64 tcu::Sampler::CompareMode mapVkSamplerCompareOp (VkCompareOp compareOp);
65 tcu::Sampler::WrapMode mapVkSamplerAddressMode (VkSamplerAddressMode addressMode);
66 tcu::Sampler::ReductionMode mapVkSamplerReductionMode (VkSamplerReductionMode reductionMode);
67 tcu::Sampler::FilterMode mapVkMinTexFilter (VkFilter filter, VkSamplerMipmapMode mipMode);
68 tcu::Sampler::FilterMode mapVkMagTexFilter (VkFilter filter);
70 VkFilter mapFilterMode (tcu::Sampler::FilterMode filterMode);
71 VkSamplerMipmapMode mapMipmapMode (tcu::Sampler::FilterMode filterMode);
72 VkSamplerAddressMode mapWrapMode (tcu::Sampler::WrapMode wrapMode);
73 VkCompareOp mapCompareMode (tcu::Sampler::CompareMode mode);
74 VkFormat mapTextureFormat (const tcu::TextureFormat& format);
75 VkFormat mapCompressedTextureFormat (const tcu::CompressedTexFormat format);
76 VkSamplerCreateInfo mapSampler (const tcu::Sampler& sampler, const tcu::TextureFormat& format, float minLod = 0.0f, float maxLod = 1000.0f, bool unnormal = false);
77 rr::GenericVec4 mapVkColor (const VkClearColorValue& color);
78 VkClearColorValue mapVkColor (const rr::GenericVec4& color);
80 void imageUtilSelfTest (void);
82 float getRepresentableDiffUnorm (const VkFormat format, const deUint32 componentNdx);
83 float getRepresentableDiffSnorm (const VkFormat format, const deUint32 componentNdx);
84 deUint32 getFormatComponentWidth (const VkFormat format, const deUint32 componentNdx);
85 deUint32 getBlockSizeInBytes (const VkFormat compressedFormat);
86 deUint32 getBlockWidth (const VkFormat compressedFormat);
87 deUint32 getBlockHeight (const VkFormat compressedFormat);
89 bool hasSpirvFormat (VkFormat fmt);
90 const std::string getSpirvFormat (VkFormat fmt);
92 const deUint32 BUFFER_IMAGE_COPY_OFFSET_GRANULARITY = 4u;
94 // \todo [2017-05-18 pyry] Consider moving this to tcu
95 struct PlanarFormatDescription
105 CHANNEL_R = (1u<<0), // Has "R" (0) channel
106 CHANNEL_G = (1u<<1), // Has "G" (1) channel
107 CHANNEL_B = (1u<<2), // Has "B" (2) channel
108 CHANNEL_A = (1u<<3), // Has "A" (3) channel
113 deUint8 elementSizeBytes;
114 deUint8 widthDivisor;
115 deUint8 heightDivisor;
116 VkFormat planeCompatibleFormat;
122 deUint8 type; // tcu::TextureChannelClass value
123 deUint8 offsetBits; // Offset in element in bits
124 deUint8 sizeBits; // Value size in bits
125 deUint8 strideBytes; // Pixel stride (in bytes), usually plane elementSize
129 deUint8 presentChannels;
132 Plane planes[MAX_PLANES];
133 Channel channels[MAX_CHANNELS];
135 inline bool hasChannelNdx (deUint32 ndx) const
137 DE_ASSERT(de::inBounds(ndx, 0u, 4u));
138 return (presentChannels & (1u<<ndx)) != 0;
142 class ImageWithBuffer {
143 std::unique_ptr<ImageWithMemory> image;
144 Move<vk::VkImageView> imageView;
145 std::unique_ptr<BufferWithMemory> buffer;
150 const DeviceInterface& vkd,
151 const VkDevice device,
153 vk::VkExtent3D extent,
154 vk::VkFormat imageFormat,
155 vk::VkImageUsageFlags usage,
156 vk::VkImageType imageType,
157 vk::VkImageSubresourceRange ssr = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u),
158 uint32_t arrayLayers = 1,
159 vk::VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT,
160 vk::VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL,
161 uint32_t mipLevels = 1,
162 vk::VkSharingMode sharingMode = VK_SHARING_MODE_EXCLUSIVE);
165 VkImageView getImageView();
166 VkBuffer getBuffer();
167 VkDeviceSize getBufferSize();
168 Allocation& getImageAllocation();
169 Allocation& getBufferAllocation();
172 bool isYCbCrFormat (VkFormat format);
173 bool isYCbCrExtensionFormat (VkFormat format);
174 bool isYCbCrConversionFormat (VkFormat format);
175 PlanarFormatDescription getPlanarFormatDescription (VkFormat format);
176 int getPlaneCount (VkFormat format);
177 deUint32 getMipmapCount (VkFormat format,
178 const vk::PlanarFormatDescription& formatDescription,
179 const vk::VkImageFormatProperties& imageFormatProperties,
180 const vk::VkExtent3D& extent);
182 deUint32 getPlaneSizeInBytes (const PlanarFormatDescription& formatInfo,
183 const VkExtent3D& baseExtents,
184 const deUint32 planeNdx,
185 const deUint32 mipmapLevel,
186 const deUint32 mipmapMemoryAlignment);
187 deUint32 getPlaneSizeInBytes (const PlanarFormatDescription& formatInfo,
188 const tcu::UVec2& baseExtents,
189 const deUint32 planeNdx,
190 const deUint32 mipmapLevel,
191 const deUint32 mipmapMemoryAlignment);
192 VkExtent3D getPlaneExtent (const PlanarFormatDescription& formatInfo,
193 const VkExtent3D& baseExtents,
194 const deUint32 planeNdx,
195 const deUint32 mipmapLevel);
196 tcu::UVec2 getPlaneExtent (const PlanarFormatDescription& formatInfo,
197 const tcu::UVec2& baseExtents,
198 const deUint32 planeNdx,
199 const deUint32 mipmapLevel);
200 tcu::UVec3 getImageSizeAlignment (VkFormat format);
201 tcu::UVec3 getImageSizeAlignment (const PlanarFormatDescription& formatInfo);
202 tcu::UVec2 getBlockExtent (VkFormat format);
203 tcu::UVec2 getBlockExtent (const PlanarFormatDescription& formatInfo);
204 VkFormat getPlaneCompatibleFormat (VkFormat format,
206 VkFormat getPlaneCompatibleFormat (const PlanarFormatDescription& formatInfo,
209 VkImageAspectFlagBits getPlaneAspect (deUint32 planeNdx);
210 deUint32 getAspectPlaneNdx (VkImageAspectFlagBits planeAspect);
211 bool isChromaSubsampled (VkFormat format);
212 bool isYCbCr422Format (VkFormat format);
213 bool isYCbCr420Format (VkFormat format);
215 tcu::PixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo,
216 const tcu::UVec2& size,
217 const deUint32* planeRowPitches,
218 void* const* planePtrs,
219 deUint32 channelNdx);
220 tcu::ConstPixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo,
221 const tcu::UVec2& size,
222 const deUint32* planeRowPitches,
223 const void* const* planePtrs,
224 deUint32 channelNdx);
225 tcu::PixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo,
226 const tcu::UVec3& size,
227 const deUint32* planeRowPitches,
228 void* const* planePtrs,
229 deUint32 channelNdx);
230 tcu::ConstPixelBufferAccess getChannelAccess (const PlanarFormatDescription& formatInfo,
231 const tcu::UVec3& size,
232 const deUint32* planeRowPitches,
233 const void* const* planePtrs,
234 deUint32 channelNdx);
235 VkImageAspectFlags getImageAspectFlags (const tcu::TextureFormat textureFormat);
236 VkExtent3D mipLevelExtents (const VkExtent3D& baseExtents,
237 const deUint32 mipLevel);
238 tcu::UVec3 alignedDivide (const VkExtent3D& extent,
239 const VkExtent3D& divisor);
241 /*--------------------------------------------------------------------*//*!
242 * Copies buffer data into an image. The buffer is expected to be
243 * in a state after host write.
244 *//*--------------------------------------------------------------------*/
245 void copyBufferToImage (const DeviceInterface& vk,
248 deUint32 queueFamilyIndex,
249 const vk::VkBuffer& buffer,
250 vk::VkDeviceSize bufferSize,
251 const std::vector<vk::VkBufferImageCopy>& copyRegions,
252 const vk::VkSemaphore* waitSemaphore,
253 vk::VkImageAspectFlags imageAspectFlags,
255 deUint32 arrayLayers,
256 vk::VkImage destImage,
257 VkImageLayout destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
258 VkPipelineStageFlags destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
259 const VkCommandPool* externalCommandPool = DE_NULL,
260 deUint32 baseMipLevel = 0);
262 void copyBufferToImage (const DeviceInterface& vk,
263 const VkCommandBuffer& cmdBuffer,
264 const VkBuffer& buffer,
265 vk::VkDeviceSize bufferSize,
266 const std::vector<VkBufferImageCopy>& copyRegions,
267 VkImageAspectFlags imageAspectFlags,
269 deUint32 arrayLayers,
271 VkImageLayout destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
272 VkPipelineStageFlags destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
273 deUint32 baseMipLevel = 0);
275 /*--------------------------------------------------------------------*//*!
276 * Copies image data into a buffer. The buffer is expected to be
278 *//*--------------------------------------------------------------------*/
279 void copyImageToBuffer (const DeviceInterface& vk,
280 vk::VkCommandBuffer cmdBuffer,
284 vk::VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
285 vk::VkImageLayout oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
286 deUint32 numLayers = 1u,
287 VkImageAspectFlags barrierAspect = VK_IMAGE_ASPECT_COLOR_BIT,
288 VkImageAspectFlags copyAspect = VK_IMAGE_ASPECT_COLOR_BIT,
289 VkPipelineStageFlags srcMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
291 void copyImageToBuffer (const DeviceInterface& vk,
292 vk::VkCommandBuffer cmdBuffer,
297 deUint32 mipLevel = 0u,
298 vk::VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
299 vk::VkImageLayout oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
300 deUint32 numLayers = 1u,
301 VkImageAspectFlags barrierAspect = VK_IMAGE_ASPECT_COLOR_BIT,
302 VkImageAspectFlags copyAspect = VK_IMAGE_ASPECT_COLOR_BIT);
304 /*--------------------------------------------------------------------*//*!
305 * Clear a color image
306 *//*--------------------------------------------------------------------*/
307 void clearColorImage (const DeviceInterface& vk,
308 const vk::VkDevice device,
309 const vk::VkQueue queue,
310 deUint32 queueFamilyIndex,
312 tcu::Vec4 clearColor,
313 vk::VkImageLayout oldLayout,
314 vk::VkImageLayout newLayout,
315 vk::VkPipelineStageFlags dstStageFlags,
316 deUint32 baseArrayLayer = 0u,
317 deUint32 layerCount = 1u,
318 deUint32 baseMipLevel = 0u,
319 deUint32 levelCount = 1u);
321 void clearColorImage (const DeviceInterface& vk,
322 const vk::VkDevice device,
323 const vk::VkQueue queue,
324 deUint32 queueFamilyIndex,
326 vk::VkClearColorValue clearColor,
327 vk::VkImageLayout oldLayout,
328 vk::VkImageLayout newLayout,
329 vk::VkAccessFlags dstAccessFlags,
330 vk::VkPipelineStageFlags dstStageFlags,
331 deUint32 baseArrayLayer = 0u,
332 deUint32 layerCount = 1u,
333 deUint32 baseMipLevel = 0u,
334 deUint32 levelCount = 1u);
336 /*--------------------------------------------------------------------*//*!
337 * Initialize color image with a chessboard pattern
338 *//*--------------------------------------------------------------------*/
339 void initColorImageChessboardPattern (const DeviceInterface& vk,
340 const vk::VkDevice device,
341 const vk::VkQueue queue,
342 deUint32 queueFamilyIndex,
343 Allocator& allocator,
346 tcu::Vec4 colorValue0,
347 tcu::Vec4 colorValue1,
349 deUint32 imageHeight,
351 vk::VkImageLayout oldLayout,
352 vk::VkImageLayout newLayout,
353 vk::VkPipelineStageFlags dstStageFlags);
355 /*--------------------------------------------------------------------*//*!
356 * Copies depth/stencil image data into two separate buffers.
357 * The buffers are expected to be read by the host.
358 *//*--------------------------------------------------------------------*/
359 void copyDepthStencilImageToBuffers (const DeviceInterface& vk,
360 vk::VkCommandBuffer cmdBuffer,
362 vk::VkBuffer depthBuffer,
363 vk::VkBuffer stencilBuffer,
365 vk::VkAccessFlags srcAccessMask,
366 vk::VkImageLayout oldLayout,
367 deUint32 numLayers = 1u);
369 /*--------------------------------------------------------------------*//*!
370 * Clear a depth/stencil image
371 *//*--------------------------------------------------------------------*/
372 void clearDepthStencilImage (const DeviceInterface& vk,
373 const vk::VkDevice device,
374 const vk::VkQueue queue,
375 deUint32 queueFamilyIndex,
379 deUint32 stencilValue,
380 vk::VkImageLayout oldLayout,
381 vk::VkImageLayout newLayout,
382 vk::VkAccessFlags dstAccessFlags,
383 vk::VkPipelineStageFlags dstStageFlags);
385 /*--------------------------------------------------------------------*//*!
386 * Initialize depth and stencil channels with a chessboard pattern
387 *//*--------------------------------------------------------------------*/
388 void initDepthStencilImageChessboardPattern (const DeviceInterface& vk,
389 const vk::VkDevice device,
390 const vk::VkQueue queue,
391 deUint32 queueFamilyIndex,
392 Allocator& allocator,
397 deUint32 stencilValue0,
398 deUint32 stencilValue1,
400 deUint32 imageHeight,
402 vk::VkImageLayout oldLayout,
403 vk::VkImageLayout newLayout,
404 vk::VkPipelineStageFlags dstStageFlags);
406 /*--------------------------------------------------------------------*//*!
407 * Makes common image subresource structures with common defaults
408 *//*--------------------------------------------------------------------*/
409 vk::VkImageSubresourceRange makeDefaultImageSubresourceRange();
411 vk::VkImageSubresourceLayers makeDefaultImageSubresourceLayers();
413 #ifndef CTS_USES_VULKANSC
414 /*--------------------------------------------------------------------*//*!
415 * Checks if the physical device supports creation of the specified
417 *//*--------------------------------------------------------------------*/
418 bool checkSparseImageFormatSupport (const VkPhysicalDevice physicalDevice,
419 const InstanceInterface& instance,
420 const VkFormat format,
421 const VkImageType imageType,
422 const VkSampleCountFlagBits sampleCount,
423 const VkImageUsageFlags usageFlags,
424 const VkImageTiling imageTiling);
426 bool checkSparseImageFormatSupport (const vk::VkPhysicalDevice physicalDevice,
427 const vk::InstanceInterface& instance,
428 const vk::VkImageCreateInfo& imageCreateInfo);
430 /*--------------------------------------------------------------------*//*!
431 * Allocates memory for a sparse image and handles the memory binding.
432 *//*--------------------------------------------------------------------*/
433 void allocateAndBindSparseImage (const vk::DeviceInterface& vk,
435 const vk::VkPhysicalDevice physicalDevice,
436 const vk::InstanceInterface& instance,
437 const vk::VkImageCreateInfo& imageCreateInfo,
438 const vk::VkSemaphore& signalSemaphore,
440 vk::Allocator& allocator,
441 std::vector<de::SharedPtr<vk::Allocation> >& allocations,
442 tcu::TextureFormat format,
443 vk::VkImage destImage);
444 #endif // CTS_USES_VULKANSC
447 #endif // _VKIMAGEUTIL_HPP