Path: blob/21.2-virgl/src/intel/vulkan/anv_formats.c
4547 views
/*1* Copyright © 2015 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "anv_private.h"24#include "drm-uapi/drm_fourcc.h"25#include "vk_enum_to_str.h"26#include "vk_format.h"27#include "vk_util.h"2829/*30* gcc-4 and earlier don't allow compound literals where a constant31* is required in -std=c99/gnu99 mode, so we can't use ISL_SWIZZLE()32* here. -std=c89/gnu89 would allow it, but we depend on c99 features33* so using -std=c89/gnu89 is not an option. Starting from gcc-534* compound literals can also be considered constant in -std=c99/gnu9935* mode.36*/37#define _ISL_SWIZZLE(r, g, b, a) { \38ISL_CHANNEL_SELECT_##r, \39ISL_CHANNEL_SELECT_##g, \40ISL_CHANNEL_SELECT_##b, \41ISL_CHANNEL_SELECT_##a, \42}4344#define RGBA _ISL_SWIZZLE(RED, GREEN, BLUE, ALPHA)45#define BGRA _ISL_SWIZZLE(BLUE, GREEN, RED, ALPHA)46#define RGB1 _ISL_SWIZZLE(RED, GREEN, BLUE, ONE)4748#define swiz_fmt1(__vk_fmt, __hw_fmt, __swizzle) \49[VK_ENUM_OFFSET(__vk_fmt)] = { \50.planes = { \51{ .isl_format = __hw_fmt, .swizzle = __swizzle, \52.denominator_scales = { 1, 1, }, \53.aspect = VK_IMAGE_ASPECT_COLOR_BIT, \54}, \55}, \56.vk_format = __vk_fmt, \57.n_planes = 1, \58}5960#define fmt1(__vk_fmt, __hw_fmt) \61swiz_fmt1(__vk_fmt, __hw_fmt, RGBA)6263#define d_fmt(__vk_fmt, __hw_fmt) \64[VK_ENUM_OFFSET(__vk_fmt)] = { \65.planes = { \66{ .isl_format = __hw_fmt, .swizzle = RGBA, \67.denominator_scales = { 1, 1, }, \68.aspect = VK_IMAGE_ASPECT_DEPTH_BIT, \69}, \70}, \71.vk_format = __vk_fmt, \72.n_planes = 1, \73}7475#define s_fmt(__vk_fmt, __hw_fmt) \76[VK_ENUM_OFFSET(__vk_fmt)] = { \77.planes = { \78{ .isl_format = __hw_fmt, .swizzle = RGBA, \79.denominator_scales = { 1, 1, }, \80.aspect = VK_IMAGE_ASPECT_STENCIL_BIT, \81}, \82}, \83.vk_format = __vk_fmt, \84.n_planes = 1, \85}8687#define ds_fmt2(__vk_fmt, __fmt1, __fmt2) \88[VK_ENUM_OFFSET(__vk_fmt)] = { \89.planes = { \90{ .isl_format = __fmt1, .swizzle = RGBA, \91.denominator_scales = { 1, 1, }, \92.aspect = VK_IMAGE_ASPECT_DEPTH_BIT, \93}, \94{ .isl_format = __fmt2, .swizzle = RGBA, \95.denominator_scales = { 1, 1, }, \96.aspect = VK_IMAGE_ASPECT_STENCIL_BIT, \97}, \98}, \99.vk_format = __vk_fmt, \100.n_planes = 2, \101}102103#define fmt_unsupported(__vk_fmt) \104[VK_ENUM_OFFSET(__vk_fmt)] = { \105.planes = { \106{ .isl_format = ISL_FORMAT_UNSUPPORTED, }, \107}, \108.vk_format = VK_FORMAT_UNDEFINED, \109}110111#define y_plane(__plane, __hw_fmt, __swizzle, __ycbcr_swizzle, dhs, dvs) \112{ .isl_format = __hw_fmt, \113.swizzle = __swizzle, \114.ycbcr_swizzle = __ycbcr_swizzle, \115.denominator_scales = { dhs, dvs, }, \116.has_chroma = false, \117.aspect = VK_IMAGE_ASPECT_PLANE_0_BIT, /* Y plane is always plane 0 */ \118}119120#define chroma_plane(__plane, __hw_fmt, __swizzle, __ycbcr_swizzle, dhs, dvs) \121{ .isl_format = __hw_fmt, \122.swizzle = __swizzle, \123.ycbcr_swizzle = __ycbcr_swizzle, \124.denominator_scales = { dhs, dvs, }, \125.has_chroma = true, \126.aspect = VK_IMAGE_ASPECT_PLANE_ ## __plane ## _BIT, \127}128129#define ycbcr_fmt(__vk_fmt, __n_planes, ...) \130[VK_ENUM_OFFSET(__vk_fmt)] = { \131.planes = { \132__VA_ARGS__, \133}, \134.vk_format = __vk_fmt, \135.n_planes = __n_planes, \136.can_ycbcr = true, \137}138139/* HINT: For array formats, the ISL name should match the VK name. For140* packed formats, they should have the channels in reverse order from each141* other. The reason for this is that, for packed formats, the ISL (and142* bspec) names are in LSB -> MSB order while VK formats are MSB -> LSB.143*/144static const struct anv_format main_formats[] = {145fmt_unsupported(VK_FORMAT_UNDEFINED),146fmt_unsupported(VK_FORMAT_R4G4_UNORM_PACK8),147fmt1(VK_FORMAT_R4G4B4A4_UNORM_PACK16, ISL_FORMAT_A4B4G4R4_UNORM),148swiz_fmt1(VK_FORMAT_B4G4R4A4_UNORM_PACK16, ISL_FORMAT_A4B4G4R4_UNORM, BGRA),149fmt1(VK_FORMAT_R5G6B5_UNORM_PACK16, ISL_FORMAT_B5G6R5_UNORM),150fmt_unsupported(VK_FORMAT_B5G6R5_UNORM_PACK16),151fmt1(VK_FORMAT_R5G5B5A1_UNORM_PACK16, ISL_FORMAT_A1B5G5R5_UNORM),152fmt_unsupported(VK_FORMAT_B5G5R5A1_UNORM_PACK16),153fmt1(VK_FORMAT_A1R5G5B5_UNORM_PACK16, ISL_FORMAT_B5G5R5A1_UNORM),154fmt1(VK_FORMAT_R8_UNORM, ISL_FORMAT_R8_UNORM),155fmt1(VK_FORMAT_R8_SNORM, ISL_FORMAT_R8_SNORM),156fmt1(VK_FORMAT_R8_USCALED, ISL_FORMAT_R8_USCALED),157fmt1(VK_FORMAT_R8_SSCALED, ISL_FORMAT_R8_SSCALED),158fmt1(VK_FORMAT_R8_UINT, ISL_FORMAT_R8_UINT),159fmt1(VK_FORMAT_R8_SINT, ISL_FORMAT_R8_SINT),160swiz_fmt1(VK_FORMAT_R8_SRGB, ISL_FORMAT_L8_UNORM_SRGB,161_ISL_SWIZZLE(RED, ZERO, ZERO, ONE)),162fmt1(VK_FORMAT_R8G8_UNORM, ISL_FORMAT_R8G8_UNORM),163fmt1(VK_FORMAT_R8G8_SNORM, ISL_FORMAT_R8G8_SNORM),164fmt1(VK_FORMAT_R8G8_USCALED, ISL_FORMAT_R8G8_USCALED),165fmt1(VK_FORMAT_R8G8_SSCALED, ISL_FORMAT_R8G8_SSCALED),166fmt1(VK_FORMAT_R8G8_UINT, ISL_FORMAT_R8G8_UINT),167fmt1(VK_FORMAT_R8G8_SINT, ISL_FORMAT_R8G8_SINT),168fmt_unsupported(VK_FORMAT_R8G8_SRGB), /* L8A8_UNORM_SRGB */169fmt1(VK_FORMAT_R8G8B8_UNORM, ISL_FORMAT_R8G8B8_UNORM),170fmt1(VK_FORMAT_R8G8B8_SNORM, ISL_FORMAT_R8G8B8_SNORM),171fmt1(VK_FORMAT_R8G8B8_USCALED, ISL_FORMAT_R8G8B8_USCALED),172fmt1(VK_FORMAT_R8G8B8_SSCALED, ISL_FORMAT_R8G8B8_SSCALED),173fmt1(VK_FORMAT_R8G8B8_UINT, ISL_FORMAT_R8G8B8_UINT),174fmt1(VK_FORMAT_R8G8B8_SINT, ISL_FORMAT_R8G8B8_SINT),175fmt1(VK_FORMAT_R8G8B8_SRGB, ISL_FORMAT_R8G8B8_UNORM_SRGB),176fmt1(VK_FORMAT_R8G8B8A8_UNORM, ISL_FORMAT_R8G8B8A8_UNORM),177fmt1(VK_FORMAT_R8G8B8A8_SNORM, ISL_FORMAT_R8G8B8A8_SNORM),178fmt1(VK_FORMAT_R8G8B8A8_USCALED, ISL_FORMAT_R8G8B8A8_USCALED),179fmt1(VK_FORMAT_R8G8B8A8_SSCALED, ISL_FORMAT_R8G8B8A8_SSCALED),180fmt1(VK_FORMAT_R8G8B8A8_UINT, ISL_FORMAT_R8G8B8A8_UINT),181fmt1(VK_FORMAT_R8G8B8A8_SINT, ISL_FORMAT_R8G8B8A8_SINT),182fmt1(VK_FORMAT_R8G8B8A8_SRGB, ISL_FORMAT_R8G8B8A8_UNORM_SRGB),183fmt1(VK_FORMAT_A8B8G8R8_UNORM_PACK32, ISL_FORMAT_R8G8B8A8_UNORM),184fmt1(VK_FORMAT_A8B8G8R8_SNORM_PACK32, ISL_FORMAT_R8G8B8A8_SNORM),185fmt1(VK_FORMAT_A8B8G8R8_USCALED_PACK32, ISL_FORMAT_R8G8B8A8_USCALED),186fmt1(VK_FORMAT_A8B8G8R8_SSCALED_PACK32, ISL_FORMAT_R8G8B8A8_SSCALED),187fmt1(VK_FORMAT_A8B8G8R8_UINT_PACK32, ISL_FORMAT_R8G8B8A8_UINT),188fmt1(VK_FORMAT_A8B8G8R8_SINT_PACK32, ISL_FORMAT_R8G8B8A8_SINT),189fmt1(VK_FORMAT_A8B8G8R8_SRGB_PACK32, ISL_FORMAT_R8G8B8A8_UNORM_SRGB),190fmt1(VK_FORMAT_A2R10G10B10_UNORM_PACK32, ISL_FORMAT_B10G10R10A2_UNORM),191fmt1(VK_FORMAT_A2R10G10B10_SNORM_PACK32, ISL_FORMAT_B10G10R10A2_SNORM),192fmt1(VK_FORMAT_A2R10G10B10_USCALED_PACK32, ISL_FORMAT_B10G10R10A2_USCALED),193fmt1(VK_FORMAT_A2R10G10B10_SSCALED_PACK32, ISL_FORMAT_B10G10R10A2_SSCALED),194fmt1(VK_FORMAT_A2R10G10B10_UINT_PACK32, ISL_FORMAT_B10G10R10A2_UINT),195fmt1(VK_FORMAT_A2R10G10B10_SINT_PACK32, ISL_FORMAT_B10G10R10A2_SINT),196fmt1(VK_FORMAT_A2B10G10R10_UNORM_PACK32, ISL_FORMAT_R10G10B10A2_UNORM),197fmt1(VK_FORMAT_A2B10G10R10_SNORM_PACK32, ISL_FORMAT_R10G10B10A2_SNORM),198fmt1(VK_FORMAT_A2B10G10R10_USCALED_PACK32, ISL_FORMAT_R10G10B10A2_USCALED),199fmt1(VK_FORMAT_A2B10G10R10_SSCALED_PACK32, ISL_FORMAT_R10G10B10A2_SSCALED),200fmt1(VK_FORMAT_A2B10G10R10_UINT_PACK32, ISL_FORMAT_R10G10B10A2_UINT),201fmt1(VK_FORMAT_A2B10G10R10_SINT_PACK32, ISL_FORMAT_R10G10B10A2_SINT),202fmt1(VK_FORMAT_R16_UNORM, ISL_FORMAT_R16_UNORM),203fmt1(VK_FORMAT_R16_SNORM, ISL_FORMAT_R16_SNORM),204fmt1(VK_FORMAT_R16_USCALED, ISL_FORMAT_R16_USCALED),205fmt1(VK_FORMAT_R16_SSCALED, ISL_FORMAT_R16_SSCALED),206fmt1(VK_FORMAT_R16_UINT, ISL_FORMAT_R16_UINT),207fmt1(VK_FORMAT_R16_SINT, ISL_FORMAT_R16_SINT),208fmt1(VK_FORMAT_R16_SFLOAT, ISL_FORMAT_R16_FLOAT),209fmt1(VK_FORMAT_R16G16_UNORM, ISL_FORMAT_R16G16_UNORM),210fmt1(VK_FORMAT_R16G16_SNORM, ISL_FORMAT_R16G16_SNORM),211fmt1(VK_FORMAT_R16G16_USCALED, ISL_FORMAT_R16G16_USCALED),212fmt1(VK_FORMAT_R16G16_SSCALED, ISL_FORMAT_R16G16_SSCALED),213fmt1(VK_FORMAT_R16G16_UINT, ISL_FORMAT_R16G16_UINT),214fmt1(VK_FORMAT_R16G16_SINT, ISL_FORMAT_R16G16_SINT),215fmt1(VK_FORMAT_R16G16_SFLOAT, ISL_FORMAT_R16G16_FLOAT),216fmt1(VK_FORMAT_R16G16B16_UNORM, ISL_FORMAT_R16G16B16_UNORM),217fmt1(VK_FORMAT_R16G16B16_SNORM, ISL_FORMAT_R16G16B16_SNORM),218fmt1(VK_FORMAT_R16G16B16_USCALED, ISL_FORMAT_R16G16B16_USCALED),219fmt1(VK_FORMAT_R16G16B16_SSCALED, ISL_FORMAT_R16G16B16_SSCALED),220fmt1(VK_FORMAT_R16G16B16_UINT, ISL_FORMAT_R16G16B16_UINT),221fmt1(VK_FORMAT_R16G16B16_SINT, ISL_FORMAT_R16G16B16_SINT),222fmt1(VK_FORMAT_R16G16B16_SFLOAT, ISL_FORMAT_R16G16B16_FLOAT),223fmt1(VK_FORMAT_R16G16B16A16_UNORM, ISL_FORMAT_R16G16B16A16_UNORM),224fmt1(VK_FORMAT_R16G16B16A16_SNORM, ISL_FORMAT_R16G16B16A16_SNORM),225fmt1(VK_FORMAT_R16G16B16A16_USCALED, ISL_FORMAT_R16G16B16A16_USCALED),226fmt1(VK_FORMAT_R16G16B16A16_SSCALED, ISL_FORMAT_R16G16B16A16_SSCALED),227fmt1(VK_FORMAT_R16G16B16A16_UINT, ISL_FORMAT_R16G16B16A16_UINT),228fmt1(VK_FORMAT_R16G16B16A16_SINT, ISL_FORMAT_R16G16B16A16_SINT),229fmt1(VK_FORMAT_R16G16B16A16_SFLOAT, ISL_FORMAT_R16G16B16A16_FLOAT),230fmt1(VK_FORMAT_R32_UINT, ISL_FORMAT_R32_UINT),231fmt1(VK_FORMAT_R32_SINT, ISL_FORMAT_R32_SINT),232fmt1(VK_FORMAT_R32_SFLOAT, ISL_FORMAT_R32_FLOAT),233fmt1(VK_FORMAT_R32G32_UINT, ISL_FORMAT_R32G32_UINT),234fmt1(VK_FORMAT_R32G32_SINT, ISL_FORMAT_R32G32_SINT),235fmt1(VK_FORMAT_R32G32_SFLOAT, ISL_FORMAT_R32G32_FLOAT),236fmt1(VK_FORMAT_R32G32B32_UINT, ISL_FORMAT_R32G32B32_UINT),237fmt1(VK_FORMAT_R32G32B32_SINT, ISL_FORMAT_R32G32B32_SINT),238fmt1(VK_FORMAT_R32G32B32_SFLOAT, ISL_FORMAT_R32G32B32_FLOAT),239fmt1(VK_FORMAT_R32G32B32A32_UINT, ISL_FORMAT_R32G32B32A32_UINT),240fmt1(VK_FORMAT_R32G32B32A32_SINT, ISL_FORMAT_R32G32B32A32_SINT),241fmt1(VK_FORMAT_R32G32B32A32_SFLOAT, ISL_FORMAT_R32G32B32A32_FLOAT),242fmt1(VK_FORMAT_R64_UINT, ISL_FORMAT_R64_PASSTHRU),243fmt1(VK_FORMAT_R64_SINT, ISL_FORMAT_R64_PASSTHRU),244fmt1(VK_FORMAT_R64_SFLOAT, ISL_FORMAT_R64_PASSTHRU),245fmt1(VK_FORMAT_R64G64_UINT, ISL_FORMAT_R64G64_PASSTHRU),246fmt1(VK_FORMAT_R64G64_SINT, ISL_FORMAT_R64G64_PASSTHRU),247fmt1(VK_FORMAT_R64G64_SFLOAT, ISL_FORMAT_R64G64_PASSTHRU),248fmt1(VK_FORMAT_R64G64B64_UINT, ISL_FORMAT_R64G64B64_PASSTHRU),249fmt1(VK_FORMAT_R64G64B64_SINT, ISL_FORMAT_R64G64B64_PASSTHRU),250fmt1(VK_FORMAT_R64G64B64_SFLOAT, ISL_FORMAT_R64G64B64_PASSTHRU),251fmt1(VK_FORMAT_R64G64B64A64_UINT, ISL_FORMAT_R64G64B64A64_PASSTHRU),252fmt1(VK_FORMAT_R64G64B64A64_SINT, ISL_FORMAT_R64G64B64A64_PASSTHRU),253fmt1(VK_FORMAT_R64G64B64A64_SFLOAT, ISL_FORMAT_R64G64B64A64_PASSTHRU),254fmt1(VK_FORMAT_B10G11R11_UFLOAT_PACK32, ISL_FORMAT_R11G11B10_FLOAT),255fmt1(VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, ISL_FORMAT_R9G9B9E5_SHAREDEXP),256257d_fmt(VK_FORMAT_D16_UNORM, ISL_FORMAT_R16_UNORM),258d_fmt(VK_FORMAT_X8_D24_UNORM_PACK32, ISL_FORMAT_R24_UNORM_X8_TYPELESS),259d_fmt(VK_FORMAT_D32_SFLOAT, ISL_FORMAT_R32_FLOAT),260s_fmt(VK_FORMAT_S8_UINT, ISL_FORMAT_R8_UINT),261fmt_unsupported(VK_FORMAT_D16_UNORM_S8_UINT),262ds_fmt2(VK_FORMAT_D24_UNORM_S8_UINT, ISL_FORMAT_R24_UNORM_X8_TYPELESS, ISL_FORMAT_R8_UINT),263ds_fmt2(VK_FORMAT_D32_SFLOAT_S8_UINT, ISL_FORMAT_R32_FLOAT, ISL_FORMAT_R8_UINT),264265swiz_fmt1(VK_FORMAT_BC1_RGB_UNORM_BLOCK, ISL_FORMAT_BC1_UNORM, RGB1),266swiz_fmt1(VK_FORMAT_BC1_RGB_SRGB_BLOCK, ISL_FORMAT_BC1_UNORM_SRGB, RGB1),267fmt1(VK_FORMAT_BC1_RGBA_UNORM_BLOCK, ISL_FORMAT_BC1_UNORM),268fmt1(VK_FORMAT_BC1_RGBA_SRGB_BLOCK, ISL_FORMAT_BC1_UNORM_SRGB),269fmt1(VK_FORMAT_BC2_UNORM_BLOCK, ISL_FORMAT_BC2_UNORM),270fmt1(VK_FORMAT_BC2_SRGB_BLOCK, ISL_FORMAT_BC2_UNORM_SRGB),271fmt1(VK_FORMAT_BC3_UNORM_BLOCK, ISL_FORMAT_BC3_UNORM),272fmt1(VK_FORMAT_BC3_SRGB_BLOCK, ISL_FORMAT_BC3_UNORM_SRGB),273fmt1(VK_FORMAT_BC4_UNORM_BLOCK, ISL_FORMAT_BC4_UNORM),274fmt1(VK_FORMAT_BC4_SNORM_BLOCK, ISL_FORMAT_BC4_SNORM),275fmt1(VK_FORMAT_BC5_UNORM_BLOCK, ISL_FORMAT_BC5_UNORM),276fmt1(VK_FORMAT_BC5_SNORM_BLOCK, ISL_FORMAT_BC5_SNORM),277fmt1(VK_FORMAT_BC6H_UFLOAT_BLOCK, ISL_FORMAT_BC6H_UF16),278fmt1(VK_FORMAT_BC6H_SFLOAT_BLOCK, ISL_FORMAT_BC6H_SF16),279fmt1(VK_FORMAT_BC7_UNORM_BLOCK, ISL_FORMAT_BC7_UNORM),280fmt1(VK_FORMAT_BC7_SRGB_BLOCK, ISL_FORMAT_BC7_UNORM_SRGB),281fmt1(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, ISL_FORMAT_ETC2_RGB8),282fmt1(VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK, ISL_FORMAT_ETC2_SRGB8),283fmt1(VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK, ISL_FORMAT_ETC2_RGB8_PTA),284fmt1(VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK, ISL_FORMAT_ETC2_SRGB8_PTA),285fmt1(VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, ISL_FORMAT_ETC2_EAC_RGBA8),286fmt1(VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK, ISL_FORMAT_ETC2_EAC_SRGB8_A8),287fmt1(VK_FORMAT_EAC_R11_UNORM_BLOCK, ISL_FORMAT_EAC_R11),288fmt1(VK_FORMAT_EAC_R11_SNORM_BLOCK, ISL_FORMAT_EAC_SIGNED_R11),289fmt1(VK_FORMAT_EAC_R11G11_UNORM_BLOCK, ISL_FORMAT_EAC_RG11),290fmt1(VK_FORMAT_EAC_R11G11_SNORM_BLOCK, ISL_FORMAT_EAC_SIGNED_RG11),291fmt1(VK_FORMAT_ASTC_4x4_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_4X4_U8SRGB),292fmt1(VK_FORMAT_ASTC_5x4_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_5X4_U8SRGB),293fmt1(VK_FORMAT_ASTC_5x5_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_5X5_U8SRGB),294fmt1(VK_FORMAT_ASTC_6x5_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_6X5_U8SRGB),295fmt1(VK_FORMAT_ASTC_6x6_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_6X6_U8SRGB),296fmt1(VK_FORMAT_ASTC_8x5_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X5_U8SRGB),297fmt1(VK_FORMAT_ASTC_8x6_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X6_U8SRGB),298fmt1(VK_FORMAT_ASTC_8x8_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X8_U8SRGB),299fmt1(VK_FORMAT_ASTC_10x5_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X5_U8SRGB),300fmt1(VK_FORMAT_ASTC_10x6_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X6_U8SRGB),301fmt1(VK_FORMAT_ASTC_10x8_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X8_U8SRGB),302fmt1(VK_FORMAT_ASTC_10x10_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X10_U8SRGB),303fmt1(VK_FORMAT_ASTC_12x10_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_12X10_U8SRGB),304fmt1(VK_FORMAT_ASTC_12x12_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_12X12_U8SRGB),305fmt1(VK_FORMAT_ASTC_4x4_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_4X4_FLT16),306fmt1(VK_FORMAT_ASTC_5x4_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_5X4_FLT16),307fmt1(VK_FORMAT_ASTC_5x5_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_5X5_FLT16),308fmt1(VK_FORMAT_ASTC_6x5_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_6X5_FLT16),309fmt1(VK_FORMAT_ASTC_6x6_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_6X6_FLT16),310fmt1(VK_FORMAT_ASTC_8x5_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X5_FLT16),311fmt1(VK_FORMAT_ASTC_8x6_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X6_FLT16),312fmt1(VK_FORMAT_ASTC_8x8_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X8_FLT16),313fmt1(VK_FORMAT_ASTC_10x5_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X5_FLT16),314fmt1(VK_FORMAT_ASTC_10x6_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X6_FLT16),315fmt1(VK_FORMAT_ASTC_10x8_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X8_FLT16),316fmt1(VK_FORMAT_ASTC_10x10_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X10_FLT16),317fmt1(VK_FORMAT_ASTC_12x10_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_12X10_FLT16),318fmt1(VK_FORMAT_ASTC_12x12_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_12X12_FLT16),319fmt_unsupported(VK_FORMAT_B8G8R8_UNORM),320fmt_unsupported(VK_FORMAT_B8G8R8_SNORM),321fmt_unsupported(VK_FORMAT_B8G8R8_USCALED),322fmt_unsupported(VK_FORMAT_B8G8R8_SSCALED),323fmt_unsupported(VK_FORMAT_B8G8R8_UINT),324fmt_unsupported(VK_FORMAT_B8G8R8_SINT),325fmt_unsupported(VK_FORMAT_B8G8R8_SRGB),326fmt1(VK_FORMAT_B8G8R8A8_UNORM, ISL_FORMAT_B8G8R8A8_UNORM),327fmt_unsupported(VK_FORMAT_B8G8R8A8_SNORM),328fmt_unsupported(VK_FORMAT_B8G8R8A8_USCALED),329fmt_unsupported(VK_FORMAT_B8G8R8A8_SSCALED),330fmt_unsupported(VK_FORMAT_B8G8R8A8_UINT),331fmt_unsupported(VK_FORMAT_B8G8R8A8_SINT),332fmt1(VK_FORMAT_B8G8R8A8_SRGB, ISL_FORMAT_B8G8R8A8_UNORM_SRGB),333};334335static const struct anv_format _4444_formats[] = {336fmt1(VK_FORMAT_A4R4G4B4_UNORM_PACK16_EXT, ISL_FORMAT_B4G4R4A4_UNORM),337fmt_unsupported(VK_FORMAT_A4B4G4R4_UNORM_PACK16_EXT),338};339340static const struct anv_format ycbcr_formats[] = {341ycbcr_fmt(VK_FORMAT_G8B8G8R8_422_UNORM, 1,342y_plane(0, ISL_FORMAT_YCRCB_SWAPUV, RGBA, _ISL_SWIZZLE(BLUE, GREEN, RED, ZERO), 1, 1)),343ycbcr_fmt(VK_FORMAT_B8G8R8G8_422_UNORM, 1,344y_plane(0, ISL_FORMAT_YCRCB_SWAPUVY, RGBA, _ISL_SWIZZLE(BLUE, GREEN, RED, ZERO), 1, 1)),345ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, 3,346y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),347chroma_plane(1, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 2, 2),348chroma_plane(2, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 2)),349ycbcr_fmt(VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, 2,350y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),351chroma_plane(1, ISL_FORMAT_R8G8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, RED, ZERO, ZERO), 2, 2)),352ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM, 3,353y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),354chroma_plane(1, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 2, 1),355chroma_plane(2, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 1)),356ycbcr_fmt(VK_FORMAT_G8_B8R8_2PLANE_422_UNORM, 2,357y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),358chroma_plane(1, ISL_FORMAT_R8G8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, RED, ZERO, ZERO), 2, 1)),359ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM, 3,360y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),361chroma_plane(1, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 1, 1),362chroma_plane(2, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 1, 1)),363364fmt_unsupported(VK_FORMAT_R10X6_UNORM_PACK16),365fmt_unsupported(VK_FORMAT_R10X6G10X6_UNORM_2PACK16),366fmt_unsupported(VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16),367fmt_unsupported(VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16),368fmt_unsupported(VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16),369fmt_unsupported(VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16),370fmt_unsupported(VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16),371fmt_unsupported(VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16),372fmt_unsupported(VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16),373fmt_unsupported(VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16),374fmt_unsupported(VK_FORMAT_R12X4_UNORM_PACK16),375fmt_unsupported(VK_FORMAT_R12X4G12X4_UNORM_2PACK16),376fmt_unsupported(VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16),377fmt_unsupported(VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16),378fmt_unsupported(VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16),379fmt_unsupported(VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16),380fmt_unsupported(VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16),381fmt_unsupported(VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16),382fmt_unsupported(VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16),383fmt_unsupported(VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16),384/* TODO: it is possible to enable the following 2 formats, but that385* requires further refactoring of how we handle multiplanar formats.386*/387fmt_unsupported(VK_FORMAT_G16B16G16R16_422_UNORM),388fmt_unsupported(VK_FORMAT_B16G16R16G16_422_UNORM),389390ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM, 3,391y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),392chroma_plane(1, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 2, 2),393chroma_plane(2, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 2)),394ycbcr_fmt(VK_FORMAT_G16_B16R16_2PLANE_420_UNORM, 2,395y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),396chroma_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, RED, ZERO, ZERO), 2, 2)),397ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM, 3,398y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),399chroma_plane(1, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 2, 1),400chroma_plane(2, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 1)),401ycbcr_fmt(VK_FORMAT_G16_B16R16_2PLANE_422_UNORM, 2,402y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),403chroma_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, RED, ZERO, ZERO), 2, 1)),404ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM, 3,405y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),406chroma_plane(1, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 1, 1),407chroma_plane(2, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 1, 1)),408};409410#undef _fmt411#undef swiz_fmt1412#undef fmt1413#undef fmt414415static const struct {416const struct anv_format *formats;417uint32_t n_formats;418} anv_formats[] = {419[0] = { .formats = main_formats,420.n_formats = ARRAY_SIZE(main_formats), },421[_VK_EXT_4444_formats_number] = { .formats = _4444_formats,422.n_formats = ARRAY_SIZE(_4444_formats), },423[_VK_KHR_sampler_ycbcr_conversion_number] = { .formats = ycbcr_formats,424.n_formats = ARRAY_SIZE(ycbcr_formats), },425};426427const struct anv_format *428anv_get_format(VkFormat vk_format)429{430uint32_t enum_offset = VK_ENUM_OFFSET(vk_format);431uint32_t ext_number = VK_ENUM_EXTENSION(vk_format);432433if (ext_number >= ARRAY_SIZE(anv_formats) ||434enum_offset >= anv_formats[ext_number].n_formats)435return NULL;436437const struct anv_format *format =438&anv_formats[ext_number].formats[enum_offset];439if (format->planes[0].isl_format == ISL_FORMAT_UNSUPPORTED)440return NULL;441442return format;443}444445/** Return true if any format plane has non-power-of-two bits-per-block. */446static bool447anv_format_has_npot_plane(const struct anv_format *anv_format) {448for (uint32_t i = 0; i < anv_format->n_planes; ++i) {449const struct isl_format_layout *isl_layout =450isl_format_get_layout(anv_format->planes[i].isl_format);451452if (!util_is_power_of_two_or_zero(isl_layout->bpb))453return true;454}455456return false;457}458459/**460* Exactly one bit must be set in \a aspect.461*462* If tiling is VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT, then return the463* requested anv_format_plane without checking for compatibility with modifiers.464* It is the caller's responsibility to verify that the the returned465* anv_format_plane is compatible with a particular modifier. (Observe that466* this function has no parameter for the DRM format modifier, and therefore467* _cannot_ check for compatibility).468*/469struct anv_format_plane470anv_get_format_plane(const struct intel_device_info *devinfo,471VkFormat vk_format,472VkImageAspectFlagBits aspect, VkImageTiling tiling)473{474const struct anv_format *format = anv_get_format(vk_format);475const struct anv_format_plane unsupported = {476.isl_format = ISL_FORMAT_UNSUPPORTED,477};478479if (format == NULL)480return unsupported;481482uint32_t plane = anv_image_aspect_to_plane(vk_format_aspects(vk_format), aspect);483struct anv_format_plane plane_format = format->planes[plane];484if (plane_format.isl_format == ISL_FORMAT_UNSUPPORTED)485return unsupported;486487if (tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)488return plane_format;489490if (aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {491assert(vk_format_aspects(vk_format) &492(VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));493494/* There's no reason why we strictly can't support depth or stencil with495* modifiers but there's also no reason why we should.496*/497return plane_format;498}499500assert((aspect & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0);501502const struct isl_format_layout *isl_layout =503isl_format_get_layout(plane_format.isl_format);504505/* On Ivy Bridge we don't even have enough 24 and 48-bit formats that we506* can reliably do texture upload with BLORP so just don't claim support507* for any of them.508*/509if (devinfo->verx10 == 70 &&510(isl_layout->bpb == 24 || isl_layout->bpb == 48))511return unsupported;512513if (tiling == VK_IMAGE_TILING_OPTIMAL &&514!util_is_power_of_two_or_zero(isl_layout->bpb)) {515/* Tiled formats *must* be power-of-two because we need up upload516* them with the render pipeline. For 3-channel formats, we fix517* this by switching them over to RGBX or RGBA formats under the518* hood.519*/520enum isl_format rgbx = isl_format_rgb_to_rgbx(plane_format.isl_format);521if (rgbx != ISL_FORMAT_UNSUPPORTED &&522isl_format_supports_rendering(devinfo, rgbx)) {523plane_format.isl_format = rgbx;524} else {525plane_format.isl_format =526isl_format_rgb_to_rgba(plane_format.isl_format);527plane_format.swizzle = ISL_SWIZZLE(RED, GREEN, BLUE, ONE);528}529}530531/* The B4G4R4A4 format isn't available prior to Broadwell so we have to fall532* back to a format with a more complex swizzle.533*/534if (vk_format == VK_FORMAT_B4G4R4A4_UNORM_PACK16 && devinfo->ver < 8) {535plane_format.isl_format = ISL_FORMAT_B4G4R4A4_UNORM;536plane_format.swizzle = ISL_SWIZZLE(GREEN, RED, ALPHA, BLUE);537}538539return plane_format;540}541542// Format capabilities543544VkFormatFeatureFlags545anv_get_image_format_features(const struct intel_device_info *devinfo,546VkFormat vk_format,547const struct anv_format *anv_format,548VkImageTiling vk_tiling,549const struct isl_drm_modifier_info *isl_mod_info)550{551VkFormatFeatureFlags flags = 0;552553if (anv_format == NULL)554return 0;555556assert((isl_mod_info != NULL) ==557(vk_tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT));558559const VkImageAspectFlags aspects = vk_format_aspects(vk_format);560561if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {562if (vk_tiling == VK_IMAGE_TILING_LINEAR ||563vk_tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)564return 0;565566flags |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT |567VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |568VK_FORMAT_FEATURE_BLIT_SRC_BIT |569VK_FORMAT_FEATURE_BLIT_DST_BIT |570VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |571VK_FORMAT_FEATURE_TRANSFER_DST_BIT;572573if (aspects & VK_IMAGE_ASPECT_DEPTH_BIT)574flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;575576if ((aspects & VK_IMAGE_ASPECT_DEPTH_BIT) && devinfo->ver >= 9)577flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT_EXT;578579return flags;580}581582const struct anv_format_plane plane_format =583anv_get_format_plane(devinfo, vk_format, VK_IMAGE_ASPECT_COLOR_BIT,584vk_tiling);585586if (plane_format.isl_format == ISL_FORMAT_UNSUPPORTED)587return 0;588589struct anv_format_plane base_plane_format = plane_format;590if (vk_tiling != VK_IMAGE_TILING_LINEAR) {591base_plane_format = anv_get_format_plane(devinfo, vk_format,592VK_IMAGE_ASPECT_COLOR_BIT,593VK_IMAGE_TILING_LINEAR);594}595596enum isl_format base_isl_format = base_plane_format.isl_format;597598/* ASTC textures must be in Y-tiled memory, and we reject compressed formats599* with modifiers.600*/601if (vk_tiling != VK_IMAGE_TILING_OPTIMAL &&602isl_format_get_layout(plane_format.isl_format)->txc == ISL_TXC_ASTC)603return 0;604605/* ASTC requires nasty workarounds on BSW so we just disable it for now.606*607* TODO: Figure out the ASTC workarounds and re-enable on BSW.608*/609if (devinfo->ver < 9 &&610isl_format_get_layout(plane_format.isl_format)->txc == ISL_TXC_ASTC)611return 0;612613if (isl_format_supports_sampling(devinfo, plane_format.isl_format)) {614flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;615616if (devinfo->ver >= 9)617flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT_EXT;618619if (isl_format_supports_filtering(devinfo, plane_format.isl_format))620flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;621}622623/* We can render to swizzled formats. However, if the alpha channel is624* moved, then blending won't work correctly. The PRM tells us625* straight-up not to render to such a surface.626*/627if (isl_format_supports_rendering(devinfo, plane_format.isl_format) &&628plane_format.swizzle.a == ISL_CHANNEL_SELECT_ALPHA) {629flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;630631if (isl_format_supports_alpha_blending(devinfo, plane_format.isl_format))632flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;633}634635/* Load/store is determined based on base format. This prevents RGB636* formats from showing up as load/store capable.637*/638if (isl_format_supports_typed_writes(devinfo, base_isl_format))639flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;640641if (base_isl_format == ISL_FORMAT_R32_SINT ||642base_isl_format == ISL_FORMAT_R32_UINT ||643base_isl_format == ISL_FORMAT_R32_FLOAT)644flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;645646if (flags) {647flags |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |648VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |649VK_FORMAT_FEATURE_TRANSFER_DST_BIT;650651/* Blit destination requires rendering support. */652if (isl_format_supports_rendering(devinfo, plane_format.isl_format))653flags |= VK_FORMAT_FEATURE_BLIT_DST_BIT;654}655656/* XXX: We handle 3-channel formats by switching them out for RGBX or657* RGBA formats behind-the-scenes. This works fine for textures658* because the upload process will fill in the extra channel.659* We could also support it for render targets, but it will take660* substantially more work and we have enough RGBX formats to handle661* what most clients will want.662*/663if (vk_tiling == VK_IMAGE_TILING_OPTIMAL &&664base_isl_format != ISL_FORMAT_UNSUPPORTED &&665!util_is_power_of_two_or_zero(isl_format_layouts[base_isl_format].bpb) &&666isl_format_rgb_to_rgbx(base_isl_format) == ISL_FORMAT_UNSUPPORTED) {667flags &= ~VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;668flags &= ~VK_FORMAT_FEATURE_BLIT_DST_BIT;669}670671if (anv_format->can_ycbcr) {672/* The sampler doesn't have support for mid point when it handles YUV on673* its own.674*/675if (isl_format_is_yuv(anv_format->planes[0].isl_format)) {676/* TODO: We've disabled linear implicit reconstruction with the677* sampler. The failures show a slightly out of range values on the678* bottom left of the sampled image.679*/680flags |= VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT;681} else {682flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT |683VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT |684VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_SEPARATE_RECONSTRUCTION_FILTER_BIT;685}686687/* We can support cosited chroma locations when handle planes with our688* own shader snippets.689*/690for (unsigned p = 0; p < anv_format->n_planes; p++) {691if (anv_format->planes[p].denominator_scales[0] > 1 ||692anv_format->planes[p].denominator_scales[1] > 1) {693flags |= VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT;694break;695}696}697698if (anv_format->n_planes > 1)699flags |= VK_FORMAT_FEATURE_DISJOINT_BIT;700701const VkFormatFeatureFlags disallowed_ycbcr_image_features =702VK_FORMAT_FEATURE_BLIT_SRC_BIT |703VK_FORMAT_FEATURE_BLIT_DST_BIT |704VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |705VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT |706VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;707708flags &= ~disallowed_ycbcr_image_features;709}710711if (vk_tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {712if (!isl_drm_modifier_get_score(devinfo, isl_mod_info->modifier))713return 0;714715/* Try to restrict the supported formats to those in drm_fourcc.h. The716* VK_EXT_image_drm_format_modifier does not require this (after all, two717* Vulkan apps could share an image by exchanging its VkFormat instead of718* a DRM_FORMAT), but there exist no users of such non-drm_fourcc formats719* yet. And the restriction shrinks our test surface.720*/721const struct isl_format_layout *isl_layout =722isl_format_get_layout(plane_format.isl_format);723724switch (isl_layout->colorspace) {725case ISL_COLORSPACE_LINEAR:726case ISL_COLORSPACE_SRGB:727/* Each DRM_FORMAT that we support uses unorm (if the DRM format name728* has no type suffix) or sfloat (if it has suffix F). No format729* contains mixed types. (as of 2021-06-14)730*/731if (isl_layout->uniform_channel_type != ISL_UNORM &&732isl_layout->uniform_channel_type != ISL_SFLOAT)733return 0;734break;735case ISL_COLORSPACE_YUV:736anv_finishme("support YUV colorspace with DRM format modifiers");737return 0;738case ISL_COLORSPACE_NONE:739return 0;740}741742/* We could support compressed formats if we wanted to. */743if (isl_format_is_compressed(plane_format.isl_format))744return 0;745746/* No non-power-of-two fourcc formats exist.747*748* Even if non-power-of-two fourcc formats existed, we could support them749* only with DRM_FORMAT_MOD_LINEAR. Tiled formats must be power-of-two750* because we implement transfers with the render pipeline.751*/752if (anv_format_has_npot_plane(anv_format))753return 0;754755if (anv_format->n_planes > 1) {756/* For simplicity, keep DISJOINT disabled for multi-planar format. */757flags &= ~VK_FORMAT_FEATURE_DISJOINT_BIT;758759/* VK_ANDROID_external_memory_android_hardware_buffer in Virtio-GPU760* Venus driver layers on top of VK_EXT_image_drm_format_modifier of761* the host Vulkan driver, and VK_FORMAT_G8_B8R8_2PLANE_420_UNORM is762* required to support camera/media interop in Android.763*/764if (vk_format != VK_FORMAT_G8_B8R8_2PLANE_420_UNORM) {765anv_finishme("support more multi-planar formats with DRM modifiers");766return 0;767}768769/* Currently there is no way to properly map memory planes to format770* planes and aux planes due to the lack of defined ABI for external771* multi-planar images.772*/773if (isl_mod_info->aux_usage != ISL_AUX_USAGE_NONE) {774return 0;775}776}777778if (isl_mod_info->aux_usage == ISL_AUX_USAGE_CCS_E &&779!isl_format_supports_ccs_e(devinfo, plane_format.isl_format)) {780return 0;781}782783if (isl_mod_info->aux_usage != ISL_AUX_USAGE_NONE) {784/* Rejection DISJOINT for consistency with the GL driver. In785* eglCreateImage, we require that the dma_buf for the primary surface786* and the dma_buf for its aux surface refer to the same bo.787*/788flags &= ~VK_FORMAT_FEATURE_DISJOINT_BIT;789790/* When the hardware accesses a storage image, it bypasses the aux791* surface. We could support storage access on images with aux792* modifiers by resolving the aux surface prior to the storage access.793*/794flags &= ~VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;795flags &= ~VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;796}797}798799return flags;800}801802static VkFormatFeatureFlags803get_buffer_format_features(const struct intel_device_info *devinfo,804VkFormat vk_format,805const struct anv_format *anv_format)806{807VkFormatFeatureFlags flags = 0;808809if (anv_format == NULL)810return 0;811812const enum isl_format isl_format = anv_format->planes[0].isl_format;813814if (isl_format == ISL_FORMAT_UNSUPPORTED)815return 0;816817if (anv_format->n_planes > 1)818return 0;819820if (anv_format->can_ycbcr)821return 0;822823if (vk_format_is_depth_or_stencil(vk_format))824return 0;825826if (isl_format_supports_sampling(devinfo, isl_format) &&827!isl_format_is_compressed(isl_format))828flags |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT;829830if (isl_format_supports_vertex_fetch(devinfo, isl_format))831flags |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;832833if (isl_is_storage_image_format(isl_format))834flags |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;835836if (isl_format == ISL_FORMAT_R32_SINT || isl_format == ISL_FORMAT_R32_UINT)837flags |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;838839return flags;840}841842static void843get_drm_format_modifier_properties_list(const struct anv_physical_device *physical_device,844VkFormat vk_format,845VkDrmFormatModifierPropertiesListEXT *list)846{847const struct intel_device_info *devinfo = &physical_device->info;848const struct anv_format *anv_format = anv_get_format(vk_format);849850VK_OUTARRAY_MAKE(out, list->pDrmFormatModifierProperties,851&list->drmFormatModifierCount);852853isl_drm_modifier_info_for_each(isl_mod_info) {854VkFormatFeatureFlags features =855anv_get_image_format_features(devinfo, vk_format, anv_format,856VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,857isl_mod_info);858if (!features)859continue;860861uint32_t planes = anv_format->n_planes;862if (isl_mod_info->aux_usage != ISL_AUX_USAGE_NONE)863++planes;864865vk_outarray_append(&out, out_props) {866*out_props = (VkDrmFormatModifierPropertiesEXT) {867.drmFormatModifier = isl_mod_info->modifier,868.drmFormatModifierPlaneCount = planes,869.drmFormatModifierTilingFeatures = features,870};871};872}873}874875void anv_GetPhysicalDeviceFormatProperties(876VkPhysicalDevice physicalDevice,877VkFormat vk_format,878VkFormatProperties* pFormatProperties)879{880ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);881const struct intel_device_info *devinfo = &physical_device->info;882const struct anv_format *anv_format = anv_get_format(vk_format);883884*pFormatProperties = (VkFormatProperties) {885.linearTilingFeatures =886anv_get_image_format_features(devinfo, vk_format, anv_format,887VK_IMAGE_TILING_LINEAR, NULL),888.optimalTilingFeatures =889anv_get_image_format_features(devinfo, vk_format, anv_format,890VK_IMAGE_TILING_OPTIMAL, NULL),891.bufferFeatures =892get_buffer_format_features(devinfo, vk_format, anv_format),893};894}895896void anv_GetPhysicalDeviceFormatProperties2(897VkPhysicalDevice physicalDevice,898VkFormat format,899VkFormatProperties2* pFormatProperties)900{901ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);902anv_GetPhysicalDeviceFormatProperties(physicalDevice, format,903&pFormatProperties->formatProperties);904905vk_foreach_struct(ext, pFormatProperties->pNext) {906/* Use unsigned since some cases are not in the VkStructureType enum. */907switch ((unsigned)ext->sType) {908case VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT:909get_drm_format_modifier_properties_list(physical_device, format,910(void *)ext);911break;912default:913anv_debug_ignored_stype(ext->sType);914break;915}916}917}918919static VkResult920anv_get_image_format_properties(921struct anv_physical_device *physical_device,922const VkPhysicalDeviceImageFormatInfo2 *info,923VkImageFormatProperties *pImageFormatProperties,924VkSamplerYcbcrConversionImageFormatProperties *pYcbcrImageFormatProperties)925{926VkFormatFeatureFlags format_feature_flags;927VkExtent3D maxExtent;928uint32_t maxMipLevels;929uint32_t maxArraySize;930VkSampleCountFlags sampleCounts;931struct anv_instance *instance = physical_device->instance;932const struct intel_device_info *devinfo = &physical_device->info;933const struct anv_format *format = anv_get_format(info->format);934const struct isl_drm_modifier_info *isl_mod_info = NULL;935const VkImageFormatListCreateInfo *format_list_info =936vk_find_struct_const(info->pNext, IMAGE_FORMAT_LIST_CREATE_INFO);937938if (format == NULL)939goto unsupported;940941if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {942const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *vk_mod_info =943vk_find_struct_const(info->pNext, PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);944945isl_mod_info = isl_drm_modifier_get_info(vk_mod_info->drmFormatModifier);946if (isl_mod_info == NULL)947goto unsupported;948}949950assert(format->vk_format == info->format);951format_feature_flags = anv_get_image_format_features(devinfo, info->format,952format, info->tiling,953isl_mod_info);954955/* Remove the VkFormatFeatureFlags that are incompatible with any declared956* image view format. (Removals are more likely to occur when a DRM format957* modifier is present).958*/959if ((info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) && format_list_info) {960for (uint32_t i = 0; i < format_list_info->viewFormatCount; ++i) {961VkFormat vk_view_format = format_list_info->pViewFormats[i];962const struct anv_format *anv_view_format = anv_get_format(vk_view_format);963VkFormatFeatureFlags view_format_features =964anv_get_image_format_features(devinfo, vk_view_format,965anv_view_format,966info->tiling,967isl_mod_info);968format_feature_flags &= view_format_features;969}970}971972if (!format_feature_flags)973goto unsupported;974975switch (info->type) {976default:977unreachable("bad VkImageType");978case VK_IMAGE_TYPE_1D:979maxExtent.width = 16384;980maxExtent.height = 1;981maxExtent.depth = 1;982maxMipLevels = 15; /* log2(maxWidth) + 1 */983maxArraySize = 2048;984sampleCounts = VK_SAMPLE_COUNT_1_BIT;985break;986case VK_IMAGE_TYPE_2D:987/* FINISHME: Does this really differ for cube maps? The documentation988* for RENDER_SURFACE_STATE suggests so.989*/990maxExtent.width = 16384;991maxExtent.height = 16384;992maxExtent.depth = 1;993maxMipLevels = 15; /* log2(maxWidth) + 1 */994maxArraySize = 2048;995sampleCounts = VK_SAMPLE_COUNT_1_BIT;996break;997case VK_IMAGE_TYPE_3D:998maxExtent.width = 2048;999maxExtent.height = 2048;1000maxExtent.depth = 2048;1001/* Prior to SKL, the mipmaps for 3D surfaces are laid out in a way1002* that make it impossible to represent in the way that1003* VkSubresourceLayout expects. Since we can't tell users how to make1004* sense of them, don't report them as available.1005*/1006if (devinfo->ver < 9 && info->tiling == VK_IMAGE_TILING_LINEAR)1007maxMipLevels = 1;1008else1009maxMipLevels = 12; /* log2(maxWidth) + 1 */1010maxArraySize = 1;1011sampleCounts = VK_SAMPLE_COUNT_1_BIT;1012break;1013}10141015if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {1016/* We support modifiers only for "simple" (that is, non-array1017* non-mipmapped single-sample) 2D images.1018*/1019if (info->type != VK_IMAGE_TYPE_2D) {1020vk_errorfi(instance, &physical_device->vk.base,1021VK_ERROR_FORMAT_NOT_SUPPORTED,1022"VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT "1023"requires VK_IMAGE_TYPE_2D");1024goto unsupported;1025}10261027maxArraySize = 1;1028maxMipLevels = 1;1029sampleCounts = VK_SAMPLE_COUNT_1_BIT;10301031if (isl_mod_info->aux_usage == ISL_AUX_USAGE_CCS_E &&1032!anv_formats_ccs_e_compatible(devinfo, info->flags, info->format,1033info->tiling, format_list_info)) {1034goto unsupported;1035}1036}10371038/* Our hardware doesn't support 1D compressed textures.1039* From the SKL PRM, RENDER_SURFACE_STATE::SurfaceFormat:1040* * This field cannot be a compressed (BC*, DXT*, FXT*, ETC*, EAC*) format1041* if the Surface Type is SURFTYPE_1D.1042* * This field cannot be ASTC format if the Surface Type is SURFTYPE_1D.1043*/1044if (info->type == VK_IMAGE_TYPE_1D &&1045isl_format_is_compressed(format->planes[0].isl_format)) {1046goto unsupported;1047}10481049if (info->tiling == VK_IMAGE_TILING_OPTIMAL &&1050info->type == VK_IMAGE_TYPE_2D &&1051(format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |1052VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&1053!(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&1054!(info->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {1055sampleCounts = isl_device_get_sample_counts(&physical_device->isl_dev);1056}10571058if (info->usage & (VK_IMAGE_USAGE_TRANSFER_SRC_BIT |1059VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {1060/* Accept transfers on anything we can sample from or renderer to. */1061if (!(format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |1062VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT |1063VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))) {1064goto unsupported;1065}1066}10671068if (info->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {1069if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {1070goto unsupported;1071}1072}10731074if (info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {1075if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {1076goto unsupported;1077}1078}10791080if (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {1081if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {1082goto unsupported;1083}1084}10851086if (info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {1087if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {1088goto unsupported;1089}1090}10911092if (info->flags & VK_IMAGE_CREATE_DISJOINT_BIT) {1093/* From the Vulkan 1.2.149 spec, VkImageCreateInfo:1094*1095* If format is a multi-planar format, and if imageCreateFormatFeatures1096* (as defined in Image Creation Limits) does not contain1097* VK_FORMAT_FEATURE_DISJOINT_BIT, then flags must not contain1098* VK_IMAGE_CREATE_DISJOINT_BIT.1099*/1100if (format->n_planes > 1 &&1101!(format_feature_flags & VK_FORMAT_FEATURE_DISJOINT_BIT)) {1102goto unsupported;1103}11041105/* From the Vulkan 1.2.149 spec, VkImageCreateInfo:1106*1107* If format is not a multi-planar format, and flags does not include1108* VK_IMAGE_CREATE_ALIAS_BIT, flags must not contain1109* VK_IMAGE_CREATE_DISJOINT_BIT.1110*/1111if (format->n_planes == 1 &&1112!(info->flags & VK_IMAGE_CREATE_ALIAS_BIT)) {1113goto unsupported;1114}11151116if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT &&1117isl_mod_info->aux_usage != ISL_AUX_USAGE_NONE) {1118/* Rejection DISJOINT for consistency with the GL driver. In1119* eglCreateImage, we require that the dma_buf for the primary surface1120* and the dma_buf for its aux surface refer to the same bo.1121*/1122goto unsupported;1123}1124}11251126if (info->flags & VK_IMAGE_CREATE_ALIAS_BIT) {1127/* Reject aliasing of images with non-linear DRM format modifiers because:1128*1129* 1. For modifiers with compression, we store aux tracking state in1130* ANV_IMAGE_MEMORY_BINDING_PRIVATE, which is not aliasable because it's1131* not client-bound.1132*1133* 2. For tiled modifiers without compression, we may attempt to compress1134* them behind the scenes, in which case both the aux tracking state1135* and the CCS data are bound to ANV_IMAGE_MEMORY_BINDING_PRIVATE.1136*/1137if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT &&1138isl_mod_info->modifier != DRM_FORMAT_MOD_LINEAR) {1139goto unsupported;1140}1141}11421143if (info->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {1144/* Nothing to check. */1145}11461147if (info->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {1148/* Ignore this flag because it was removed from the1149* provisional_I_20150910 header.1150*/1151}11521153/* From the bspec section entitled "Surface Layout and Tiling",1154* pre-gfx9 has a 2 GB limitation of the size in bytes,1155* gfx9 and gfx10 have a 256 GB limitation and gfx11+1156* has a 16 TB limitation.1157*/1158uint64_t maxResourceSize = 0;1159if (devinfo->ver < 9)1160maxResourceSize = (uint64_t) 1 << 31;1161else if (devinfo->ver < 11)1162maxResourceSize = (uint64_t) 1 << 38;1163else1164maxResourceSize = (uint64_t) 1 << 44;11651166*pImageFormatProperties = (VkImageFormatProperties) {1167.maxExtent = maxExtent,1168.maxMipLevels = maxMipLevels,1169.maxArrayLayers = maxArraySize,1170.sampleCounts = sampleCounts,11711172/* FINISHME: Accurately calculate1173* VkImageFormatProperties::maxResourceSize.1174*/1175.maxResourceSize = maxResourceSize,1176};11771178if (pYcbcrImageFormatProperties) {1179pYcbcrImageFormatProperties->combinedImageSamplerDescriptorCount =1180format->n_planes;1181}11821183return VK_SUCCESS;11841185unsupported:1186*pImageFormatProperties = (VkImageFormatProperties) {1187.maxExtent = { 0, 0, 0 },1188.maxMipLevels = 0,1189.maxArrayLayers = 0,1190.sampleCounts = 0,1191.maxResourceSize = 0,1192};11931194return VK_ERROR_FORMAT_NOT_SUPPORTED;1195}11961197VkResult anv_GetPhysicalDeviceImageFormatProperties(1198VkPhysicalDevice physicalDevice,1199VkFormat format,1200VkImageType type,1201VkImageTiling tiling,1202VkImageUsageFlags usage,1203VkImageCreateFlags createFlags,1204VkImageFormatProperties* pImageFormatProperties)1205{1206ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);12071208const VkPhysicalDeviceImageFormatInfo2 info = {1209.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,1210.pNext = NULL,1211.format = format,1212.type = type,1213.tiling = tiling,1214.usage = usage,1215.flags = createFlags,1216};12171218return anv_get_image_format_properties(physical_device, &info,1219pImageFormatProperties, NULL);1220}122112221223/* Supports opaque fd but not dma_buf. */1224static const VkExternalMemoryProperties opaque_fd_only_props = {1225.externalMemoryFeatures =1226VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |1227VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,1228.exportFromImportedHandleTypes =1229VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,1230.compatibleHandleTypes =1231VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,1232};12331234/* Supports opaque fd and dma_buf. */1235static const VkExternalMemoryProperties opaque_fd_dma_buf_props = {1236.externalMemoryFeatures =1237VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |1238VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,1239.exportFromImportedHandleTypes =1240VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |1241VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,1242.compatibleHandleTypes =1243VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |1244VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,1245};12461247static const VkExternalMemoryProperties userptr_props = {1248.externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,1249.exportFromImportedHandleTypes = 0,1250.compatibleHandleTypes =1251VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,1252};12531254static const VkExternalMemoryProperties android_buffer_props = {1255.externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |1256VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,1257.exportFromImportedHandleTypes =1258VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,1259.compatibleHandleTypes =1260VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,1261};126212631264static const VkExternalMemoryProperties android_image_props = {1265.externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |1266VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT |1267VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT,1268.exportFromImportedHandleTypes =1269VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,1270.compatibleHandleTypes =1271VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,1272};12731274VkResult anv_GetPhysicalDeviceImageFormatProperties2(1275VkPhysicalDevice physicalDevice,1276const VkPhysicalDeviceImageFormatInfo2* base_info,1277VkImageFormatProperties2* base_props)1278{1279ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);1280struct anv_instance *instance = physical_device->instance;1281const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;1282VkExternalImageFormatProperties *external_props = NULL;1283VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;1284VkAndroidHardwareBufferUsageANDROID *android_usage = NULL;1285VkResult result;12861287/* Extract input structs */1288vk_foreach_struct_const(s, base_info->pNext) {1289switch (s->sType) {1290case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:1291external_info = (const void *) s;1292break;1293case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT:1294/* anv_get_image_format_properties will handle this */1295break;1296case VK_STRUCTURE_TYPE_IMAGE_STENCIL_USAGE_CREATE_INFO_EXT:1297/* Ignore but don't warn */1298break;1299default:1300anv_debug_ignored_stype(s->sType);1301break;1302}1303}13041305/* Extract output structs */1306vk_foreach_struct(s, base_props->pNext) {1307switch (s->sType) {1308case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:1309external_props = (void *) s;1310break;1311case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:1312ycbcr_props = (void *) s;1313break;1314case VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_USAGE_ANDROID:1315android_usage = (void *) s;1316break;1317default:1318anv_debug_ignored_stype(s->sType);1319break;1320}1321}13221323result = anv_get_image_format_properties(physical_device, base_info,1324&base_props->imageFormatProperties, ycbcr_props);1325if (result != VK_SUCCESS)1326goto fail;13271328bool ahw_supported =1329physical_device->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer;13301331if (ahw_supported && android_usage) {1332android_usage->androidHardwareBufferUsage =1333anv_ahw_usage_from_vk_usage(base_info->flags,1334base_info->usage);13351336/* Limit maxArrayLayers to 1 for AHardwareBuffer based images for now. */1337base_props->imageFormatProperties.maxArrayLayers = 1;1338}13391340/* From the Vulkan 1.0.42 spec:1341*1342* If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will1343* behave as if VkPhysicalDeviceExternalImageFormatInfo was not1344* present and VkExternalImageFormatProperties will be ignored.1345*/1346if (external_info && external_info->handleType != 0) {1347/* Does there exist a method for app and driver to explicitly communicate1348* to each other the image's memory layout?1349*/1350bool tiling_has_explicit_layout;13511352switch (base_info->tiling) {1353default:1354unreachable("bad VkImageTiling");1355case VK_IMAGE_TILING_LINEAR:1356/* The app can query the image's memory layout with1357* vkGetImageSubresourceLayout.1358*/1359tiling_has_explicit_layout = true;1360break;1361case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT:1362/* The app can provide the image's memory layout with1363* VkImageDrmFormatModifierExplicitCreateInfoEXT;1364* or the app can query it with vkGetImageSubresourceLayout.1365*/1366tiling_has_explicit_layout = true;1367break;1368case VK_IMAGE_TILING_OPTIMAL:1369/* The app can neither query nor provide the image's memory layout. */1370tiling_has_explicit_layout = false;1371break;1372}13731374/* Compatibility between tiling and external memory handles1375* --------------------------------------------------------1376* When importing or exporting an image, there must exist a method that1377* enables the app and driver to agree on the image's memory layout. If no1378* method exists, then we reject image creation here.1379*1380* If the memory handle requires matching1381* VkPhysicalDeviceIDPropertiesKHR::driverUUID and ::deviceUUID, then the1382* match-requirement guarantees that all users of the image agree on the1383* image's memory layout.1384*1385* If the memory handle does not require matching1386* VkPhysicalDeviceIDPropertiesKHR::driverUUID nor ::deviceUUID, then we1387* require that the app and driver be able to explicitly communicate to1388* each other the image's memory layout.1389*1390* (For restrictions on driverUUID and deviceUUID, see the Vulkan 1.2.1491391* spec, Table 73 "External memory handle types").1392*/1393switch (external_info->handleType) {1394case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:13951396/* Disable stencil export, there are issues. */1397if (vk_format_aspects(base_info->format) & VK_IMAGE_ASPECT_STENCIL_BIT) {1398result = vk_errorfi(instance, &physical_device->vk.base,1399VK_ERROR_FORMAT_NOT_SUPPORTED,1400"External stencil buffers are not supported yet");1401goto fail;1402}14031404if (external_props) {1405if (tiling_has_explicit_layout) {1406/* With an explicit memory layout, we don't care which type of fd1407* the image belongs too. Both OPAQUE_FD and DMA_BUF are1408* interchangeable here.1409*/1410external_props->externalMemoryProperties = opaque_fd_dma_buf_props;1411} else {1412/* With an implicit memory layout, we must rely on deviceUUID1413* and driverUUID to determine the layout. Therefore DMA_BUF is1414* incompatible here.1415*/1416external_props->externalMemoryProperties = opaque_fd_only_props;1417}1418}1419break;1420case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:1421/* This memory handle has no restrictions on driverUUID nor deviceUUID,1422* and therefore requires explicit memory layout.1423*/1424if (!tiling_has_explicit_layout) {1425result = vk_errorfi(instance, &physical_device->vk.base,1426VK_ERROR_FORMAT_NOT_SUPPORTED,1427"VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT "1428"requires VK_IMAGE_TILING_LINEAR or "1429"VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT");1430goto fail;1431}14321433/* With an explicit memory layout, we don't care which type of fd1434* the image belongs too. Both OPAQUE_FD and DMA_BUF are1435* interchangeable here.1436*/1437if (external_props)1438external_props->externalMemoryProperties = opaque_fd_dma_buf_props;1439break;1440case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:1441/* This memory handle has no restrictions on driverUUID nor deviceUUID,1442* and therefore requires explicit memory layout.1443*/1444if (!tiling_has_explicit_layout) {1445result = vk_errorfi(instance, &physical_device->vk.base,1446VK_ERROR_FORMAT_NOT_SUPPORTED,1447"VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT "1448"requires VK_IMAGE_TILING_LINEAR or "1449"VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT");1450goto fail;1451}14521453if (external_props)1454external_props->externalMemoryProperties = userptr_props;1455break;1456case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:1457/* This memory handle is magic. The Vulkan spec says it has no1458* requirements regarding deviceUUID nor driverUUID, but Android still1459* requires support for VK_IMAGE_TILING_OPTIMAL. Android systems1460* communicate the image's memory layout through backdoor channels.1461*/1462if (ahw_supported && external_props) {1463external_props->externalMemoryProperties = android_image_props;1464break;1465}1466FALLTHROUGH; /* If ahw not supported */1467default:1468/* From the Vulkan 1.0.42 spec:1469*1470* If handleType is not compatible with the [parameters] specified1471* in VkPhysicalDeviceImageFormatInfo2, then1472* vkGetPhysicalDeviceImageFormatProperties2 returns1473* VK_ERROR_FORMAT_NOT_SUPPORTED.1474*/1475result = vk_errorfi(instance, &physical_device->vk.base,1476VK_ERROR_FORMAT_NOT_SUPPORTED,1477"unsupported VkExternalMemoryTypeFlagBits 0x%x",1478external_info->handleType);1479goto fail;1480}1481}14821483return VK_SUCCESS;14841485fail:1486if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {1487/* From the Vulkan 1.0.42 spec:1488*1489* If the combination of parameters to1490* vkGetPhysicalDeviceImageFormatProperties2 is not supported by1491* the implementation for use in vkCreateImage, then all members of1492* imageFormatProperties will be filled with zero.1493*/1494base_props->imageFormatProperties = (VkImageFormatProperties) {};1495}14961497return result;1498}14991500void anv_GetPhysicalDeviceSparseImageFormatProperties(1501VkPhysicalDevice physicalDevice,1502VkFormat format,1503VkImageType type,1504uint32_t samples,1505VkImageUsageFlags usage,1506VkImageTiling tiling,1507uint32_t* pNumProperties,1508VkSparseImageFormatProperties* pProperties)1509{1510/* Sparse images are not yet supported. */1511*pNumProperties = 0;1512}15131514void anv_GetPhysicalDeviceSparseImageFormatProperties2(1515VkPhysicalDevice physicalDevice,1516const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,1517uint32_t* pPropertyCount,1518VkSparseImageFormatProperties2* pProperties)1519{1520/* Sparse images are not yet supported. */1521*pPropertyCount = 0;1522}15231524void anv_GetPhysicalDeviceExternalBufferProperties(1525VkPhysicalDevice physicalDevice,1526const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,1527VkExternalBufferProperties* pExternalBufferProperties)1528{1529/* The Vulkan 1.0.42 spec says "handleType must be a valid1530* VkExternalMemoryHandleTypeFlagBits value" in1531* VkPhysicalDeviceExternalBufferInfo. This differs from1532* VkPhysicalDeviceExternalImageFormatInfo, which surprisingly permits1533* handleType == 0.1534*/1535assert(pExternalBufferInfo->handleType != 0);15361537/* All of the current flags are for sparse which we don't support yet.1538* Even when we do support it, doing sparse on external memory sounds1539* sketchy. Also, just disallowing flags is the safe option.1540*/1541if (pExternalBufferInfo->flags)1542goto unsupported;15431544ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);15451546switch (pExternalBufferInfo->handleType) {1547case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:1548case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:1549pExternalBufferProperties->externalMemoryProperties = opaque_fd_dma_buf_props;1550return;1551case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:1552pExternalBufferProperties->externalMemoryProperties = userptr_props;1553return;1554case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:1555if (physical_device->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer) {1556pExternalBufferProperties->externalMemoryProperties = android_buffer_props;1557return;1558}1559FALLTHROUGH; /* If ahw not supported */1560default:1561goto unsupported;1562}15631564unsupported:1565/* From the Vulkan 1.1.113 spec:1566*1567* compatibleHandleTypes must include at least handleType.1568*/1569pExternalBufferProperties->externalMemoryProperties =1570(VkExternalMemoryProperties) {1571.compatibleHandleTypes = pExternalBufferInfo->handleType,1572};1573}15741575VkResult anv_CreateSamplerYcbcrConversion(1576VkDevice _device,1577const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,1578const VkAllocationCallbacks* pAllocator,1579VkSamplerYcbcrConversion* pYcbcrConversion)1580{1581ANV_FROM_HANDLE(anv_device, device, _device);1582struct anv_ycbcr_conversion *conversion;15831584/* Search for VkExternalFormatANDROID and resolve the format. */1585struct anv_format *ext_format = NULL;1586const VkExternalFormatANDROID *ext_info =1587vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_FORMAT_ANDROID);15881589uint64_t format = ext_info ? ext_info->externalFormat : 0;1590if (format) {1591assert(pCreateInfo->format == VK_FORMAT_UNDEFINED);1592ext_format = (struct anv_format *) (uintptr_t) format;1593}15941595assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO);15961597conversion = vk_object_zalloc(&device->vk, pAllocator, sizeof(*conversion),1598VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION);1599if (!conversion)1600return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);16011602conversion->format = anv_get_format(pCreateInfo->format);1603conversion->ycbcr_model = pCreateInfo->ycbcrModel;1604conversion->ycbcr_range = pCreateInfo->ycbcrRange;16051606/* The Vulkan 1.1.95 spec says "When creating an external format conversion,1607* the value of components if ignored."1608*/1609if (!ext_format) {1610conversion->mapping[0] = pCreateInfo->components.r;1611conversion->mapping[1] = pCreateInfo->components.g;1612conversion->mapping[2] = pCreateInfo->components.b;1613conversion->mapping[3] = pCreateInfo->components.a;1614}16151616conversion->chroma_offsets[0] = pCreateInfo->xChromaOffset;1617conversion->chroma_offsets[1] = pCreateInfo->yChromaOffset;1618conversion->chroma_filter = pCreateInfo->chromaFilter;16191620/* Setup external format. */1621if (ext_format)1622conversion->format = ext_format;16231624bool has_chroma_subsampled = false;1625for (uint32_t p = 0; p < conversion->format->n_planes; p++) {1626if (conversion->format->planes[p].has_chroma &&1627(conversion->format->planes[p].denominator_scales[0] > 1 ||1628conversion->format->planes[p].denominator_scales[1] > 1))1629has_chroma_subsampled = true;1630}1631conversion->chroma_reconstruction = has_chroma_subsampled &&1632(conversion->chroma_offsets[0] == VK_CHROMA_LOCATION_COSITED_EVEN ||1633conversion->chroma_offsets[1] == VK_CHROMA_LOCATION_COSITED_EVEN);16341635*pYcbcrConversion = anv_ycbcr_conversion_to_handle(conversion);16361637return VK_SUCCESS;1638}16391640void anv_DestroySamplerYcbcrConversion(1641VkDevice _device,1642VkSamplerYcbcrConversion YcbcrConversion,1643const VkAllocationCallbacks* pAllocator)1644{1645ANV_FROM_HANDLE(anv_device, device, _device);1646ANV_FROM_HANDLE(anv_ycbcr_conversion, conversion, YcbcrConversion);16471648if (!conversion)1649return;16501651vk_object_free(&device->vk, pAllocator, conversion);1652}165316541655