• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "anv_private.h"
25 #include "drm-uapi/drm_fourcc.h"
26 #include "vk_android.h"
27 #include "vk_enum_defines.h"
28 #include "vk_enum_to_str.h"
29 #include "vk_format.h"
30 #include "vk_util.h"
31 
32 /*
33  * gcc-4 and earlier don't allow compound literals where a constant
34  * is required in -std=c99/gnu99 mode, so we can't use ISL_SWIZZLE()
35  * here. -std=c89/gnu89 would allow it, but we depend on c99 features
36  * so using -std=c89/gnu89 is not an option. Starting from gcc-5
37  * compound literals can also be considered constant in -std=c99/gnu99
38  * mode.
39  */
40 #define _ISL_SWIZZLE(r, g, b, a) { \
41       ISL_CHANNEL_SELECT_##r, \
42       ISL_CHANNEL_SELECT_##g, \
43       ISL_CHANNEL_SELECT_##b, \
44       ISL_CHANNEL_SELECT_##a, \
45 }
46 
47 #define RGBA _ISL_SWIZZLE(RED, GREEN, BLUE, ALPHA)
48 #define BGRA _ISL_SWIZZLE(BLUE, GREEN, RED, ALPHA)
49 #define RGB1 _ISL_SWIZZLE(RED, GREEN, BLUE, ONE)
50 
51 #define swiz_fmt1(__vk_fmt, __hw_fmt, __swizzle) \
52    [VK_ENUM_OFFSET(__vk_fmt)] = { \
53       .planes = { \
54          { .isl_format = __hw_fmt, .swizzle = __swizzle, \
55            .aspect = VK_IMAGE_ASPECT_COLOR_BIT, \
56          }, \
57       }, \
58       .vk_format = __vk_fmt, \
59       .n_planes = 1, \
60    }
61 
62 #define swiz_fmt1_flags(__vk_fmt, __hw_fmt, __swizzle, __flags) \
63    [VK_ENUM_OFFSET(__vk_fmt)] = { \
64       .planes = { \
65          { .isl_format = __hw_fmt, .swizzle = __swizzle, \
66            .aspect = VK_IMAGE_ASPECT_COLOR_BIT, \
67          }, \
68       }, \
69       .vk_format = __vk_fmt, \
70       .n_planes = 1, \
71       .flags = __flags, \
72    }
73 
74 #define fmt1(__vk_fmt, __hw_fmt) \
75    swiz_fmt1(__vk_fmt, __hw_fmt, RGBA)
76 
77 #define d_fmt(__vk_fmt, __hw_fmt) \
78    [VK_ENUM_OFFSET(__vk_fmt)] = { \
79       .planes = { \
80          { .isl_format = __hw_fmt, .swizzle = RGBA, \
81            .aspect = VK_IMAGE_ASPECT_DEPTH_BIT, \
82          }, \
83       }, \
84       .vk_format = __vk_fmt, \
85       .n_planes = 1, \
86    }
87 
88 #define s_fmt(__vk_fmt, __hw_fmt) \
89    [VK_ENUM_OFFSET(__vk_fmt)] = { \
90       .planes = { \
91          { .isl_format = __hw_fmt, .swizzle = RGBA, \
92            .aspect = VK_IMAGE_ASPECT_STENCIL_BIT, \
93          }, \
94       }, \
95       .vk_format = __vk_fmt, \
96       .n_planes = 1, \
97    }
98 
99 #define ds_fmt2(__vk_fmt, __fmt1, __fmt2) \
100    [VK_ENUM_OFFSET(__vk_fmt)] = { \
101       .planes = { \
102          { .isl_format = __fmt1, .swizzle = RGBA, \
103            .aspect = VK_IMAGE_ASPECT_DEPTH_BIT, \
104          }, \
105          { .isl_format = __fmt2, .swizzle = RGBA, \
106            .aspect = VK_IMAGE_ASPECT_STENCIL_BIT, \
107          }, \
108       }, \
109       .vk_format = __vk_fmt, \
110       .n_planes = 2, \
111    }
112 
113 #define fmt_unsupported(__vk_fmt) \
114    [VK_ENUM_OFFSET(__vk_fmt)] = { \
115       .planes = { \
116          { .isl_format = ISL_FORMAT_UNSUPPORTED, }, \
117       }, \
118       .vk_format = VK_FORMAT_UNDEFINED, \
119    }
120 
121 #define ycbcr_plane(__plane, __hw_fmt, __swizzle) \
122    { .isl_format = __hw_fmt, \
123      .swizzle = __swizzle, \
124      .aspect = VK_IMAGE_ASPECT_PLANE_ ## __plane ## _BIT, \
125    }
126 
127 #define ycbcr_fmt(__vk_fmt, __n_planes, __can_ycbcr, __can_video, ...) \
128    [VK_ENUM_OFFSET(__vk_fmt)] = { \
129       .planes = { \
130          __VA_ARGS__, \
131       }, \
132       .vk_format = __vk_fmt, \
133       .n_planes = __n_planes, \
134       .flags = (__can_ycbcr ? ANV_FORMAT_FLAG_CAN_YCBCR : 0) | \
135                (__can_video ? ANV_FORMAT_FLAG_CAN_VIDEO : 0), \
136    }
137 
138 /* HINT: For array formats, the ISL name should match the VK name.  For
139  * packed formats, they should have the channels in reverse order from each
140  * other.  The reason for this is that, for packed formats, the ISL (and
141  * bspec) names are in LSB -> MSB order while VK formats are MSB -> LSB.
142  */
143 static const struct anv_format main_formats[] = {
144    fmt_unsupported(VK_FORMAT_UNDEFINED),
145    fmt_unsupported(VK_FORMAT_R4G4_UNORM_PACK8),
146    fmt1(VK_FORMAT_R4G4B4A4_UNORM_PACK16,             ISL_FORMAT_A4B4G4R4_UNORM),
147    swiz_fmt1(VK_FORMAT_B4G4R4A4_UNORM_PACK16,        ISL_FORMAT_A4B4G4R4_UNORM,  BGRA),
148    fmt1(VK_FORMAT_R5G6B5_UNORM_PACK16,               ISL_FORMAT_B5G6R5_UNORM),
149    swiz_fmt1(VK_FORMAT_B5G6R5_UNORM_PACK16,          ISL_FORMAT_B5G6R5_UNORM, BGRA),
150    fmt1(VK_FORMAT_R5G5B5A1_UNORM_PACK16,             ISL_FORMAT_A1B5G5R5_UNORM),
151    swiz_fmt1(VK_FORMAT_B5G5R5A1_UNORM_PACK16,        ISL_FORMAT_A1B5G5R5_UNORM, BGRA),
152    fmt1(VK_FORMAT_A1R5G5B5_UNORM_PACK16,             ISL_FORMAT_B5G5R5A1_UNORM),
153    fmt1(VK_FORMAT_R8_UNORM,                          ISL_FORMAT_R8_UNORM),
154    fmt1(VK_FORMAT_R8_SNORM,                          ISL_FORMAT_R8_SNORM),
155    fmt1(VK_FORMAT_R8_USCALED,                        ISL_FORMAT_R8_USCALED),
156    fmt1(VK_FORMAT_R8_SSCALED,                        ISL_FORMAT_R8_SSCALED),
157    fmt1(VK_FORMAT_R8_UINT,                           ISL_FORMAT_R8_UINT),
158    fmt1(VK_FORMAT_R8_SINT,                           ISL_FORMAT_R8_SINT),
159    swiz_fmt1(VK_FORMAT_R8_SRGB,                      ISL_FORMAT_L8_UNORM_SRGB,
160                                                      _ISL_SWIZZLE(RED, ZERO, ZERO, ONE)),
161    fmt1(VK_FORMAT_R8G8_UNORM,                        ISL_FORMAT_R8G8_UNORM),
162    fmt1(VK_FORMAT_R8G8_SNORM,                        ISL_FORMAT_R8G8_SNORM),
163    fmt1(VK_FORMAT_R8G8_USCALED,                      ISL_FORMAT_R8G8_USCALED),
164    fmt1(VK_FORMAT_R8G8_SSCALED,                      ISL_FORMAT_R8G8_SSCALED),
165    fmt1(VK_FORMAT_R8G8_UINT,                         ISL_FORMAT_R8G8_UINT),
166    fmt1(VK_FORMAT_R8G8_SINT,                         ISL_FORMAT_R8G8_SINT),
167    fmt_unsupported(VK_FORMAT_R8G8_SRGB),             /* L8A8_UNORM_SRGB */
168    fmt1(VK_FORMAT_R8G8B8_UNORM,                      ISL_FORMAT_R8G8B8_UNORM),
169    fmt1(VK_FORMAT_R8G8B8_SNORM,                      ISL_FORMAT_R8G8B8_SNORM),
170    fmt1(VK_FORMAT_R8G8B8_USCALED,                    ISL_FORMAT_R8G8B8_USCALED),
171    fmt1(VK_FORMAT_R8G8B8_SSCALED,                    ISL_FORMAT_R8G8B8_SSCALED),
172    fmt1(VK_FORMAT_R8G8B8_UINT,                       ISL_FORMAT_R8G8B8_UINT),
173    fmt1(VK_FORMAT_R8G8B8_SINT,                       ISL_FORMAT_R8G8B8_SINT),
174    fmt1(VK_FORMAT_R8G8B8_SRGB,                       ISL_FORMAT_R8G8B8_UNORM_SRGB),
175    fmt1(VK_FORMAT_R8G8B8A8_UNORM,                    ISL_FORMAT_R8G8B8A8_UNORM),
176    fmt1(VK_FORMAT_R8G8B8A8_SNORM,                    ISL_FORMAT_R8G8B8A8_SNORM),
177    fmt1(VK_FORMAT_R8G8B8A8_USCALED,                  ISL_FORMAT_R8G8B8A8_USCALED),
178    fmt1(VK_FORMAT_R8G8B8A8_SSCALED,                  ISL_FORMAT_R8G8B8A8_SSCALED),
179    fmt1(VK_FORMAT_R8G8B8A8_UINT,                     ISL_FORMAT_R8G8B8A8_UINT),
180    fmt1(VK_FORMAT_R8G8B8A8_SINT,                     ISL_FORMAT_R8G8B8A8_SINT),
181    fmt1(VK_FORMAT_R8G8B8A8_SRGB,                     ISL_FORMAT_R8G8B8A8_UNORM_SRGB),
182    fmt1(VK_FORMAT_A8B8G8R8_UNORM_PACK32,             ISL_FORMAT_R8G8B8A8_UNORM),
183    fmt1(VK_FORMAT_A8B8G8R8_SNORM_PACK32,             ISL_FORMAT_R8G8B8A8_SNORM),
184    fmt1(VK_FORMAT_A8B8G8R8_USCALED_PACK32,           ISL_FORMAT_R8G8B8A8_USCALED),
185    fmt1(VK_FORMAT_A8B8G8R8_SSCALED_PACK32,           ISL_FORMAT_R8G8B8A8_SSCALED),
186    fmt1(VK_FORMAT_A8B8G8R8_UINT_PACK32,              ISL_FORMAT_R8G8B8A8_UINT),
187    fmt1(VK_FORMAT_A8B8G8R8_SINT_PACK32,              ISL_FORMAT_R8G8B8A8_SINT),
188    fmt1(VK_FORMAT_A8B8G8R8_SRGB_PACK32,              ISL_FORMAT_R8G8B8A8_UNORM_SRGB),
189    fmt1(VK_FORMAT_A2R10G10B10_UNORM_PACK32,          ISL_FORMAT_B10G10R10A2_UNORM),
190    fmt1(VK_FORMAT_A2R10G10B10_SNORM_PACK32,          ISL_FORMAT_B10G10R10A2_SNORM),
191    fmt1(VK_FORMAT_A2R10G10B10_USCALED_PACK32,        ISL_FORMAT_B10G10R10A2_USCALED),
192    fmt1(VK_FORMAT_A2R10G10B10_SSCALED_PACK32,        ISL_FORMAT_B10G10R10A2_SSCALED),
193    fmt1(VK_FORMAT_A2R10G10B10_UINT_PACK32,           ISL_FORMAT_B10G10R10A2_UINT),
194    fmt1(VK_FORMAT_A2R10G10B10_SINT_PACK32,           ISL_FORMAT_B10G10R10A2_SINT),
195    fmt1(VK_FORMAT_A2B10G10R10_UNORM_PACK32,          ISL_FORMAT_R10G10B10A2_UNORM),
196    fmt1(VK_FORMAT_A2B10G10R10_SNORM_PACK32,          ISL_FORMAT_R10G10B10A2_SNORM),
197    fmt1(VK_FORMAT_A2B10G10R10_USCALED_PACK32,        ISL_FORMAT_R10G10B10A2_USCALED),
198    fmt1(VK_FORMAT_A2B10G10R10_SSCALED_PACK32,        ISL_FORMAT_R10G10B10A2_SSCALED),
199    fmt1(VK_FORMAT_A2B10G10R10_UINT_PACK32,           ISL_FORMAT_R10G10B10A2_UINT),
200    fmt1(VK_FORMAT_A2B10G10R10_SINT_PACK32,           ISL_FORMAT_R10G10B10A2_SINT),
201    fmt1(VK_FORMAT_R16_UNORM,                         ISL_FORMAT_R16_UNORM),
202    fmt1(VK_FORMAT_R16_SNORM,                         ISL_FORMAT_R16_SNORM),
203    fmt1(VK_FORMAT_R16_USCALED,                       ISL_FORMAT_R16_USCALED),
204    fmt1(VK_FORMAT_R16_SSCALED,                       ISL_FORMAT_R16_SSCALED),
205    fmt1(VK_FORMAT_R16_UINT,                          ISL_FORMAT_R16_UINT),
206    fmt1(VK_FORMAT_R16_SINT,                          ISL_FORMAT_R16_SINT),
207    fmt1(VK_FORMAT_R16_SFLOAT,                        ISL_FORMAT_R16_FLOAT),
208    fmt1(VK_FORMAT_R16G16_UNORM,                      ISL_FORMAT_R16G16_UNORM),
209    fmt1(VK_FORMAT_R16G16_SNORM,                      ISL_FORMAT_R16G16_SNORM),
210    fmt1(VK_FORMAT_R16G16_USCALED,                    ISL_FORMAT_R16G16_USCALED),
211    fmt1(VK_FORMAT_R16G16_SSCALED,                    ISL_FORMAT_R16G16_SSCALED),
212    fmt1(VK_FORMAT_R16G16_UINT,                       ISL_FORMAT_R16G16_UINT),
213    fmt1(VK_FORMAT_R16G16_SINT,                       ISL_FORMAT_R16G16_SINT),
214    fmt1(VK_FORMAT_R16G16_SFLOAT,                     ISL_FORMAT_R16G16_FLOAT),
215    fmt1(VK_FORMAT_R16G16B16_UNORM,                   ISL_FORMAT_R16G16B16_UNORM),
216    fmt1(VK_FORMAT_R16G16B16_SNORM,                   ISL_FORMAT_R16G16B16_SNORM),
217    fmt1(VK_FORMAT_R16G16B16_USCALED,                 ISL_FORMAT_R16G16B16_USCALED),
218    fmt1(VK_FORMAT_R16G16B16_SSCALED,                 ISL_FORMAT_R16G16B16_SSCALED),
219    fmt1(VK_FORMAT_R16G16B16_UINT,                    ISL_FORMAT_R16G16B16_UINT),
220    fmt1(VK_FORMAT_R16G16B16_SINT,                    ISL_FORMAT_R16G16B16_SINT),
221    fmt1(VK_FORMAT_R16G16B16_SFLOAT,                  ISL_FORMAT_R16G16B16_FLOAT),
222    fmt1(VK_FORMAT_R16G16B16A16_UNORM,                ISL_FORMAT_R16G16B16A16_UNORM),
223    fmt1(VK_FORMAT_R16G16B16A16_SNORM,                ISL_FORMAT_R16G16B16A16_SNORM),
224    fmt1(VK_FORMAT_R16G16B16A16_USCALED,              ISL_FORMAT_R16G16B16A16_USCALED),
225    fmt1(VK_FORMAT_R16G16B16A16_SSCALED,              ISL_FORMAT_R16G16B16A16_SSCALED),
226    fmt1(VK_FORMAT_R16G16B16A16_UINT,                 ISL_FORMAT_R16G16B16A16_UINT),
227    fmt1(VK_FORMAT_R16G16B16A16_SINT,                 ISL_FORMAT_R16G16B16A16_SINT),
228    fmt1(VK_FORMAT_R16G16B16A16_SFLOAT,               ISL_FORMAT_R16G16B16A16_FLOAT),
229    fmt1(VK_FORMAT_R32_UINT,                          ISL_FORMAT_R32_UINT),
230    fmt1(VK_FORMAT_R32_SINT,                          ISL_FORMAT_R32_SINT),
231    fmt1(VK_FORMAT_R32_SFLOAT,                        ISL_FORMAT_R32_FLOAT),
232    fmt1(VK_FORMAT_R32G32_UINT,                       ISL_FORMAT_R32G32_UINT),
233    fmt1(VK_FORMAT_R32G32_SINT,                       ISL_FORMAT_R32G32_SINT),
234    fmt1(VK_FORMAT_R32G32_SFLOAT,                     ISL_FORMAT_R32G32_FLOAT),
235    fmt1(VK_FORMAT_R32G32B32_UINT,                    ISL_FORMAT_R32G32B32_UINT),
236    fmt1(VK_FORMAT_R32G32B32_SINT,                    ISL_FORMAT_R32G32B32_SINT),
237    fmt1(VK_FORMAT_R32G32B32_SFLOAT,                  ISL_FORMAT_R32G32B32_FLOAT),
238    fmt1(VK_FORMAT_R32G32B32A32_UINT,                 ISL_FORMAT_R32G32B32A32_UINT),
239    fmt1(VK_FORMAT_R32G32B32A32_SINT,                 ISL_FORMAT_R32G32B32A32_SINT),
240    fmt1(VK_FORMAT_R32G32B32A32_SFLOAT,               ISL_FORMAT_R32G32B32A32_FLOAT),
241    fmt1(VK_FORMAT_R64_UINT,                          ISL_FORMAT_R64_PASSTHRU),
242    fmt1(VK_FORMAT_R64_SINT,                          ISL_FORMAT_R64_PASSTHRU),
243    fmt1(VK_FORMAT_R64_SFLOAT,                        ISL_FORMAT_R64_PASSTHRU),
244    fmt1(VK_FORMAT_R64G64_UINT,                       ISL_FORMAT_R64G64_PASSTHRU),
245    fmt1(VK_FORMAT_R64G64_SINT,                       ISL_FORMAT_R64G64_PASSTHRU),
246    fmt1(VK_FORMAT_R64G64_SFLOAT,                     ISL_FORMAT_R64G64_PASSTHRU),
247    fmt1(VK_FORMAT_R64G64B64_UINT,                    ISL_FORMAT_R64G64B64_PASSTHRU),
248    fmt1(VK_FORMAT_R64G64B64_SINT,                    ISL_FORMAT_R64G64B64_PASSTHRU),
249    fmt1(VK_FORMAT_R64G64B64_SFLOAT,                  ISL_FORMAT_R64G64B64_PASSTHRU),
250    fmt1(VK_FORMAT_R64G64B64A64_UINT,                 ISL_FORMAT_R64G64B64A64_PASSTHRU),
251    fmt1(VK_FORMAT_R64G64B64A64_SINT,                 ISL_FORMAT_R64G64B64A64_PASSTHRU),
252    fmt1(VK_FORMAT_R64G64B64A64_SFLOAT,               ISL_FORMAT_R64G64B64A64_PASSTHRU),
253    fmt1(VK_FORMAT_B10G11R11_UFLOAT_PACK32,           ISL_FORMAT_R11G11B10_FLOAT),
254    fmt1(VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,            ISL_FORMAT_R9G9B9E5_SHAREDEXP),
255 
256    d_fmt(VK_FORMAT_D16_UNORM,                        ISL_FORMAT_R16_UNORM),
257    d_fmt(VK_FORMAT_X8_D24_UNORM_PACK32,              ISL_FORMAT_R24_UNORM_X8_TYPELESS),
258    d_fmt(VK_FORMAT_D32_SFLOAT,                       ISL_FORMAT_R32_FLOAT),
259    s_fmt(VK_FORMAT_S8_UINT,                          ISL_FORMAT_R8_UINT),
260    fmt_unsupported(VK_FORMAT_D16_UNORM_S8_UINT),
261    ds_fmt2(VK_FORMAT_D24_UNORM_S8_UINT,              ISL_FORMAT_R24_UNORM_X8_TYPELESS, ISL_FORMAT_R8_UINT),
262    ds_fmt2(VK_FORMAT_D32_SFLOAT_S8_UINT,             ISL_FORMAT_R32_FLOAT, ISL_FORMAT_R8_UINT),
263 
264    swiz_fmt1(VK_FORMAT_BC1_RGB_UNORM_BLOCK,          ISL_FORMAT_BC1_UNORM, RGB1),
265    swiz_fmt1(VK_FORMAT_BC1_RGB_SRGB_BLOCK,           ISL_FORMAT_BC1_UNORM_SRGB, RGB1),
266    fmt1(VK_FORMAT_BC1_RGBA_UNORM_BLOCK,              ISL_FORMAT_BC1_UNORM),
267    fmt1(VK_FORMAT_BC1_RGBA_SRGB_BLOCK,               ISL_FORMAT_BC1_UNORM_SRGB),
268    fmt1(VK_FORMAT_BC2_UNORM_BLOCK,                   ISL_FORMAT_BC2_UNORM),
269    fmt1(VK_FORMAT_BC2_SRGB_BLOCK,                    ISL_FORMAT_BC2_UNORM_SRGB),
270    fmt1(VK_FORMAT_BC3_UNORM_BLOCK,                   ISL_FORMAT_BC3_UNORM),
271    fmt1(VK_FORMAT_BC3_SRGB_BLOCK,                    ISL_FORMAT_BC3_UNORM_SRGB),
272    fmt1(VK_FORMAT_BC4_UNORM_BLOCK,                   ISL_FORMAT_BC4_UNORM),
273    fmt1(VK_FORMAT_BC4_SNORM_BLOCK,                   ISL_FORMAT_BC4_SNORM),
274    fmt1(VK_FORMAT_BC5_UNORM_BLOCK,                   ISL_FORMAT_BC5_UNORM),
275    fmt1(VK_FORMAT_BC5_SNORM_BLOCK,                   ISL_FORMAT_BC5_SNORM),
276    fmt1(VK_FORMAT_BC6H_UFLOAT_BLOCK,                 ISL_FORMAT_BC6H_UF16),
277    fmt1(VK_FORMAT_BC6H_SFLOAT_BLOCK,                 ISL_FORMAT_BC6H_SF16),
278    fmt1(VK_FORMAT_BC7_UNORM_BLOCK,                   ISL_FORMAT_BC7_UNORM),
279    fmt1(VK_FORMAT_BC7_SRGB_BLOCK,                    ISL_FORMAT_BC7_UNORM_SRGB),
280    fmt1(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,           ISL_FORMAT_ETC2_RGB8),
281    fmt1(VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK,            ISL_FORMAT_ETC2_SRGB8),
282    fmt1(VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK,         ISL_FORMAT_ETC2_RGB8_PTA),
283    fmt1(VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK,          ISL_FORMAT_ETC2_SRGB8_PTA),
284    fmt1(VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK,         ISL_FORMAT_ETC2_EAC_RGBA8),
285    fmt1(VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK,          ISL_FORMAT_ETC2_EAC_SRGB8_A8),
286    fmt1(VK_FORMAT_EAC_R11_UNORM_BLOCK,               ISL_FORMAT_EAC_R11),
287    fmt1(VK_FORMAT_EAC_R11_SNORM_BLOCK,               ISL_FORMAT_EAC_SIGNED_R11),
288    fmt1(VK_FORMAT_EAC_R11G11_UNORM_BLOCK,            ISL_FORMAT_EAC_RG11),
289    fmt1(VK_FORMAT_EAC_R11G11_SNORM_BLOCK,            ISL_FORMAT_EAC_SIGNED_RG11),
290    fmt1(VK_FORMAT_ASTC_4x4_SRGB_BLOCK,               ISL_FORMAT_ASTC_LDR_2D_4X4_U8SRGB),
291    fmt1(VK_FORMAT_ASTC_5x4_SRGB_BLOCK,               ISL_FORMAT_ASTC_LDR_2D_5X4_U8SRGB),
292    fmt1(VK_FORMAT_ASTC_5x5_SRGB_BLOCK,               ISL_FORMAT_ASTC_LDR_2D_5X5_U8SRGB),
293    fmt1(VK_FORMAT_ASTC_6x5_SRGB_BLOCK,               ISL_FORMAT_ASTC_LDR_2D_6X5_U8SRGB),
294    fmt1(VK_FORMAT_ASTC_6x6_SRGB_BLOCK,               ISL_FORMAT_ASTC_LDR_2D_6X6_U8SRGB),
295    fmt1(VK_FORMAT_ASTC_8x5_SRGB_BLOCK,               ISL_FORMAT_ASTC_LDR_2D_8X5_U8SRGB),
296    fmt1(VK_FORMAT_ASTC_8x6_SRGB_BLOCK,               ISL_FORMAT_ASTC_LDR_2D_8X6_U8SRGB),
297    fmt1(VK_FORMAT_ASTC_8x8_SRGB_BLOCK,               ISL_FORMAT_ASTC_LDR_2D_8X8_U8SRGB),
298    fmt1(VK_FORMAT_ASTC_10x5_SRGB_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_10X5_U8SRGB),
299    fmt1(VK_FORMAT_ASTC_10x6_SRGB_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_10X6_U8SRGB),
300    fmt1(VK_FORMAT_ASTC_10x8_SRGB_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_10X8_U8SRGB),
301    fmt1(VK_FORMAT_ASTC_10x10_SRGB_BLOCK,             ISL_FORMAT_ASTC_LDR_2D_10X10_U8SRGB),
302    fmt1(VK_FORMAT_ASTC_12x10_SRGB_BLOCK,             ISL_FORMAT_ASTC_LDR_2D_12X10_U8SRGB),
303    fmt1(VK_FORMAT_ASTC_12x12_SRGB_BLOCK,             ISL_FORMAT_ASTC_LDR_2D_12X12_U8SRGB),
304    fmt1(VK_FORMAT_ASTC_4x4_UNORM_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_4X4_FLT16),
305    fmt1(VK_FORMAT_ASTC_5x4_UNORM_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_5X4_FLT16),
306    fmt1(VK_FORMAT_ASTC_5x5_UNORM_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_5X5_FLT16),
307    fmt1(VK_FORMAT_ASTC_6x5_UNORM_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_6X5_FLT16),
308    fmt1(VK_FORMAT_ASTC_6x6_UNORM_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_6X6_FLT16),
309    fmt1(VK_FORMAT_ASTC_8x5_UNORM_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_8X5_FLT16),
310    fmt1(VK_FORMAT_ASTC_8x6_UNORM_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_8X6_FLT16),
311    fmt1(VK_FORMAT_ASTC_8x8_UNORM_BLOCK,              ISL_FORMAT_ASTC_LDR_2D_8X8_FLT16),
312    fmt1(VK_FORMAT_ASTC_10x5_UNORM_BLOCK,             ISL_FORMAT_ASTC_LDR_2D_10X5_FLT16),
313    fmt1(VK_FORMAT_ASTC_10x6_UNORM_BLOCK,             ISL_FORMAT_ASTC_LDR_2D_10X6_FLT16),
314    fmt1(VK_FORMAT_ASTC_10x8_UNORM_BLOCK,             ISL_FORMAT_ASTC_LDR_2D_10X8_FLT16),
315    fmt1(VK_FORMAT_ASTC_10x10_UNORM_BLOCK,            ISL_FORMAT_ASTC_LDR_2D_10X10_FLT16),
316    fmt1(VK_FORMAT_ASTC_12x10_UNORM_BLOCK,            ISL_FORMAT_ASTC_LDR_2D_12X10_FLT16),
317    fmt1(VK_FORMAT_ASTC_12x12_UNORM_BLOCK,            ISL_FORMAT_ASTC_LDR_2D_12X12_FLT16),
318    fmt_unsupported(VK_FORMAT_B8G8R8_UNORM),
319    fmt_unsupported(VK_FORMAT_B8G8R8_SNORM),
320    fmt_unsupported(VK_FORMAT_B8G8R8_USCALED),
321    fmt_unsupported(VK_FORMAT_B8G8R8_SSCALED),
322    fmt_unsupported(VK_FORMAT_B8G8R8_UINT),
323    fmt_unsupported(VK_FORMAT_B8G8R8_SINT),
324    fmt_unsupported(VK_FORMAT_B8G8R8_SRGB),
325    fmt1(VK_FORMAT_B8G8R8A8_UNORM,                    ISL_FORMAT_B8G8R8A8_UNORM),
326    fmt_unsupported(VK_FORMAT_B8G8R8A8_SNORM),
327    fmt_unsupported(VK_FORMAT_B8G8R8A8_USCALED),
328    fmt_unsupported(VK_FORMAT_B8G8R8A8_SSCALED),
329    fmt_unsupported(VK_FORMAT_B8G8R8A8_UINT),
330    fmt_unsupported(VK_FORMAT_B8G8R8A8_SINT),
331    fmt1(VK_FORMAT_B8G8R8A8_SRGB,                     ISL_FORMAT_B8G8R8A8_UNORM_SRGB),
332 };
333 
334 static const struct anv_format _4444_formats[] = {
335    fmt1(VK_FORMAT_A4R4G4B4_UNORM_PACK16,            ISL_FORMAT_B4G4R4A4_UNORM),
336    swiz_fmt1_flags(VK_FORMAT_A4B4G4R4_UNORM_PACK16, ISL_FORMAT_B4G4R4A4_UNORM, BGRA,
337                                                     ANV_FORMAT_FLAG_NO_CBCWF),
338 };
339 
340 static const struct anv_format _2plane_444_formats[] = {
341    ycbcr_fmt(VK_FORMAT_G8_B8R8_2PLANE_444_UNORM, 2, true, false,
342              ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
343              ycbcr_plane(0, ISL_FORMAT_R8G8_UNORM, RGBA)),
344    fmt_unsupported(VK_FORMAT_G10X6_B10X6R10X6_2PLANE_444_UNORM_3PACK16),
345    fmt_unsupported(VK_FORMAT_G12X4_B12X4R12X4_2PLANE_444_UNORM_3PACK16),
346    ycbcr_fmt(VK_FORMAT_G16_B16R16_2PLANE_444_UNORM, 2, true, false,
347              ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
348              ycbcr_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA)),
349 };
350 
351 static const struct anv_format ycbcr_formats[] = {
352    ycbcr_fmt(VK_FORMAT_G8B8G8R8_422_UNORM, 1, true, false,
353              ycbcr_plane(0, ISL_FORMAT_YCRCB_NORMAL, RGBA)),
354    ycbcr_fmt(VK_FORMAT_B8G8R8G8_422_UNORM, 1, true, false,
355              ycbcr_plane(0, ISL_FORMAT_YCRCB_SWAPY, RGBA)),
356    ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, 3, true, false,
357              ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
358              ycbcr_plane(1, ISL_FORMAT_R8_UNORM, RGBA),
359              ycbcr_plane(2, ISL_FORMAT_R8_UNORM, RGBA)),
360    ycbcr_fmt(VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, 2, true, true,
361              ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
362              ycbcr_plane(1, ISL_FORMAT_R8G8_UNORM, RGBA)),
363    ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM, 3, true, false,
364              ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
365              ycbcr_plane(1, ISL_FORMAT_R8_UNORM, RGBA),
366              ycbcr_plane(2, ISL_FORMAT_R8_UNORM, RGBA)),
367    ycbcr_fmt(VK_FORMAT_G8_B8R8_2PLANE_422_UNORM, 2, true, false,
368              ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
369              ycbcr_plane(1, ISL_FORMAT_R8G8_UNORM, RGBA)),
370    ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM, 3, true, false,
371              ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
372              ycbcr_plane(1, ISL_FORMAT_R8_UNORM, RGBA),
373              ycbcr_plane(2, ISL_FORMAT_R8_UNORM, RGBA)),
374 
375    fmt_unsupported(VK_FORMAT_R10X6_UNORM_PACK16),
376    fmt_unsupported(VK_FORMAT_R10X6G10X6_UNORM_2PACK16),
377    fmt_unsupported(VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16),
378    fmt_unsupported(VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16),
379    fmt_unsupported(VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16),
380    fmt_unsupported(VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16),
381    ycbcr_fmt(VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16, 2, false, true,
382              ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
383              ycbcr_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA)),
384    fmt_unsupported(VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16),
385    fmt_unsupported(VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16),
386    fmt_unsupported(VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16),
387    fmt_unsupported(VK_FORMAT_R12X4_UNORM_PACK16),
388    fmt_unsupported(VK_FORMAT_R12X4G12X4_UNORM_2PACK16),
389    fmt_unsupported(VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16),
390    fmt_unsupported(VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16),
391    fmt_unsupported(VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16),
392    fmt_unsupported(VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16),
393    fmt_unsupported(VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16),
394    fmt_unsupported(VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16),
395    fmt_unsupported(VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16),
396    fmt_unsupported(VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16),
397    /* TODO: it is possible to enable the following 2 formats, but that
398     * requires further refactoring of how we handle multiplanar formats.
399     */
400    fmt_unsupported(VK_FORMAT_G16B16G16R16_422_UNORM),
401    fmt_unsupported(VK_FORMAT_B16G16R16G16_422_UNORM),
402 
403    ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM, 3, true, false,
404              ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
405              ycbcr_plane(1, ISL_FORMAT_R16_UNORM, RGBA),
406              ycbcr_plane(2, ISL_FORMAT_R16_UNORM, RGBA)),
407    ycbcr_fmt(VK_FORMAT_G16_B16R16_2PLANE_420_UNORM, 2, true, false,
408              ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
409              ycbcr_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA)),
410    ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM, 3, true, false,
411              ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
412              ycbcr_plane(1, ISL_FORMAT_R16_UNORM, RGBA),
413              ycbcr_plane(2, ISL_FORMAT_R16_UNORM, RGBA)),
414    ycbcr_fmt(VK_FORMAT_G16_B16R16_2PLANE_422_UNORM, 2, true, false,
415              ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
416              ycbcr_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA)),
417    ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM, 3, true, false,
418              ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
419              ycbcr_plane(1, ISL_FORMAT_R16_UNORM, RGBA),
420              ycbcr_plane(2, ISL_FORMAT_R16_UNORM, RGBA)),
421 };
422 
423 static const struct anv_format maintenance5_formats[] = {
424    fmt1(VK_FORMAT_A8_UNORM_KHR,                   ISL_FORMAT_A8_UNORM),
425    swiz_fmt1(VK_FORMAT_A1B5G5R5_UNORM_PACK16_KHR, ISL_FORMAT_B5G5R5A1_UNORM, BGRA)
426 };
427 
428 #undef _fmt
429 #undef swiz_fmt1
430 #undef fmt1
431 #undef fmt
432 
433 static const struct {
434    const struct anv_format *formats;
435    uint32_t n_formats;
436 } anv_formats[] = {
437    [0]                                       = { .formats = main_formats,
438                                                  .n_formats = ARRAY_SIZE(main_formats), },
439    [_VK_EXT_4444_formats_number]             = { .formats = _4444_formats,
440                                                  .n_formats = ARRAY_SIZE(_4444_formats), },
441    [_VK_KHR_sampler_ycbcr_conversion_number] = { .formats = ycbcr_formats,
442                                                  .n_formats = ARRAY_SIZE(ycbcr_formats), },
443    [_VK_KHR_maintenance5_number]             = { .formats = maintenance5_formats,
444                                                  .n_formats = ARRAY_SIZE(maintenance5_formats), },
445    [_VK_EXT_ycbcr_2plane_444_formats_number] = { .formats = _2plane_444_formats,
446                                                  .n_formats = ARRAY_SIZE(_2plane_444_formats), },
447 };
448 
449 const struct anv_format *
anv_get_format(const struct anv_physical_device * device,VkFormat vk_format)450 anv_get_format(const struct anv_physical_device *device, VkFormat vk_format)
451 {
452    uint32_t enum_offset = VK_ENUM_OFFSET(vk_format);
453    uint32_t ext_number = VK_ENUM_EXTENSION(vk_format);
454 
455    if (ext_number >= ARRAY_SIZE(anv_formats) ||
456        enum_offset >= anv_formats[ext_number].n_formats)
457       return NULL;
458 
459    const struct anv_format *format =
460       &anv_formats[ext_number].formats[enum_offset];
461    if (format->planes[0].isl_format == ISL_FORMAT_UNSUPPORTED)
462       return NULL;
463 
464    /* This format is only available if custom border colors without format is
465     * disabled.
466     */
467    if ((format->flags & ANV_FORMAT_FLAG_NO_CBCWF) &&
468        device->instance->custom_border_colors_without_format)
469       return NULL;
470 
471    return format;
472 }
473 
474 /** Return true if any format plane has non-power-of-two bits-per-block. */
475 static bool
anv_format_has_npot_plane(const struct anv_format * anv_format)476 anv_format_has_npot_plane(const struct anv_format *anv_format) {
477    for (uint32_t i = 0; i < anv_format->n_planes; ++i) {
478       const struct isl_format_layout *isl_layout =
479          isl_format_get_layout(anv_format->planes[i].isl_format);
480 
481       if (!util_is_power_of_two_or_zero(isl_layout->bpb))
482          return true;
483    }
484 
485    return false;
486 }
487 
488 /**
489  * Exactly one bit must be set in \a aspect.
490  *
491  * If tiling is VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT, then return the
492  * requested anv_format_plane without checking for compatibility with modifiers.
493  * It is the caller's responsibility to verify that the the returned
494  * anv_format_plane is compatible with a particular modifier.  (Observe that
495  * this function has no parameter for the DRM format modifier, and therefore
496  * _cannot_ check for compatibility).
497  */
498 struct anv_format_plane
anv_get_format_plane(const struct anv_physical_device * device,VkFormat vk_format,uint32_t plane,VkImageTiling tiling)499 anv_get_format_plane(const struct anv_physical_device *device,
500                      VkFormat vk_format, uint32_t plane,
501                      VkImageTiling tiling)
502 {
503    const struct anv_format *format = anv_get_format(device, vk_format);
504    const struct anv_format_plane unsupported = {
505       .isl_format = ISL_FORMAT_UNSUPPORTED,
506    };
507 
508    if (format == NULL)
509       return unsupported;
510 
511    assert(plane < format->n_planes);
512    struct anv_format_plane plane_format = format->planes[plane];
513    if (plane_format.isl_format == ISL_FORMAT_UNSUPPORTED)
514       return unsupported;
515 
516    if (tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
517       return plane_format;
518 
519    if (vk_format_is_depth_or_stencil(vk_format))
520       return plane_format;
521 
522    const struct isl_format_layout *isl_layout =
523       isl_format_get_layout(plane_format.isl_format);
524 
525    if (tiling == VK_IMAGE_TILING_OPTIMAL &&
526        !util_is_power_of_two_or_zero(isl_layout->bpb)) {
527       /* Tiled formats *must* be power-of-two because we need up upload
528        * them with the render pipeline.  For 3-channel formats, we fix
529        * this by switching them over to RGBX or RGBA formats under the
530        * hood.
531        */
532       enum isl_format rgbx = isl_format_rgb_to_rgbx(plane_format.isl_format);
533       if (rgbx != ISL_FORMAT_UNSUPPORTED &&
534           isl_format_supports_rendering(&device->info, rgbx)) {
535          plane_format.isl_format = rgbx;
536       } else {
537          plane_format.isl_format =
538             isl_format_rgb_to_rgba(plane_format.isl_format);
539          plane_format.swizzle = ISL_SWIZZLE(RED, GREEN, BLUE, ONE);
540       }
541    }
542 
543    return plane_format;
544 }
545 
546 struct anv_format_plane
anv_get_format_aspect(const struct anv_physical_device * device,VkFormat vk_format,VkImageAspectFlagBits aspect,VkImageTiling tiling)547 anv_get_format_aspect(const struct anv_physical_device *device,
548                       VkFormat vk_format,
549                       VkImageAspectFlagBits aspect, VkImageTiling tiling)
550 {
551    const uint32_t plane =
552       anv_aspect_to_plane(vk_format_aspects(vk_format), aspect);
553    return anv_get_format_plane(device, vk_format, plane, tiling);
554 }
555 
556 // Format capabilities
557 
558 VkFormatFeatureFlags2
anv_get_image_format_features2(const struct anv_physical_device * physical_device,VkFormat vk_format,const struct anv_format * anv_format,VkImageTiling vk_tiling,const struct isl_drm_modifier_info * isl_mod_info)559 anv_get_image_format_features2(const struct anv_physical_device *physical_device,
560                                VkFormat vk_format,
561                                const struct anv_format *anv_format,
562                                VkImageTiling vk_tiling,
563                                const struct isl_drm_modifier_info *isl_mod_info)
564 {
565    const struct intel_device_info *devinfo = &physical_device->info;
566    VkFormatFeatureFlags2 flags = 0;
567 
568    if (anv_format == NULL)
569       return 0;
570 
571    assert((isl_mod_info != NULL) ==
572           (vk_tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT));
573 
574    if (anv_is_format_emulated(physical_device, vk_format)) {
575       assert(isl_format_is_compressed(anv_format->planes[0].isl_format));
576 
577       /* require optimal tiling so that we can decompress on upload */
578       if (vk_tiling != VK_IMAGE_TILING_OPTIMAL)
579          return 0;
580 
581       /* required features for compressed formats */
582       flags |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT |
583                VK_FORMAT_FEATURE_2_BLIT_SRC_BIT |
584                VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT |
585                VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
586                VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
587 
588       return flags;
589    }
590 
591    const VkImageAspectFlags aspects = vk_format_aspects(vk_format);
592 
593    if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
594       if (vk_tiling == VK_IMAGE_TILING_LINEAR ||
595           vk_tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
596          return 0;
597 
598       flags |= VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT |
599                VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT |
600                VK_FORMAT_FEATURE_2_BLIT_SRC_BIT |
601                VK_FORMAT_FEATURE_2_BLIT_DST_BIT |
602                VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
603                VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT |
604                VK_FORMAT_FEATURE_2_HOST_IMAGE_TRANSFER_BIT_EXT;
605 
606       if (aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
607          flags |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT |
608                   VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT |
609                   VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_DEPTH_COMPARISON_BIT;
610       }
611 
612       return flags;
613    }
614 
615    assert(aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
616 
617    if (anv_format->flags & ANV_FORMAT_FLAG_CAN_VIDEO) {
618       flags |= physical_device->video_decode_enabled ?
619                   VK_FORMAT_FEATURE_2_VIDEO_DECODE_OUTPUT_BIT_KHR |
620                   VK_FORMAT_FEATURE_2_VIDEO_DECODE_DPB_BIT_KHR : 0;
621 
622       flags |= physical_device->video_encode_enabled ?
623                   VK_FORMAT_FEATURE_2_VIDEO_ENCODE_INPUT_BIT_KHR |
624                   VK_FORMAT_FEATURE_2_VIDEO_ENCODE_DPB_BIT_KHR : 0;
625    }
626 
627    const struct anv_format_plane plane_format =
628       anv_get_format_plane(physical_device, vk_format, 0, vk_tiling);
629 
630    if (plane_format.isl_format == ISL_FORMAT_UNSUPPORTED)
631       return 0;
632 
633    struct anv_format_plane base_plane_format = plane_format;
634    if (vk_tiling != VK_IMAGE_TILING_LINEAR) {
635       base_plane_format = anv_get_format_plane(physical_device, vk_format, 0,
636                                                VK_IMAGE_TILING_LINEAR);
637    }
638 
639    enum isl_format base_isl_format = base_plane_format.isl_format;
640 
641    if (isl_format_supports_sampling(devinfo, plane_format.isl_format)) {
642 
643       /* Unlike other surface formats, our sampler requires that the ASTC
644        * format only be used on surfaces in non-linearly-tiled memory.
645        * Thankfully, we can make an exception for linearly-tiled images that
646        * are only used for transfers. blorp_copy will reinterpret any
647        * compressed format to an uncompressed one.
648        *
649        * We handle modifier tilings further down in this function.
650        */
651       if (vk_tiling == VK_IMAGE_TILING_LINEAR &&
652           isl_format_get_layout(plane_format.isl_format)->txc == ISL_TXC_ASTC)
653          return VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
654                 VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
655 
656       flags |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT |
657                VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT |
658                VK_FORMAT_FEATURE_2_HOST_IMAGE_TRANSFER_BIT_EXT;
659 
660       if (isl_format_supports_filtering(devinfo, plane_format.isl_format))
661          flags |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
662    }
663 
664    /* We can render to swizzled formats.  However, if the alpha channel is
665     * moved, then blending won't work correctly.  The PRM tells us
666     * straight-up not to render to such a surface.
667     */
668    if (isl_format_supports_rendering(devinfo, plane_format.isl_format) &&
669        plane_format.swizzle.a == ISL_CHANNEL_SELECT_ALPHA) {
670       flags |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT;
671 
672       /* While we can render to swizzled formats, they don't blend correctly
673        * if there are blend constants involved.  The swizzle just remaps the
674        * output of the shader to different channels in the texture.  It
675        * doesn't change the interpretation of the constant blend factors in
676        * COLOR_CALC_STATE.
677        */
678       if (isl_format_supports_alpha_blending(devinfo, plane_format.isl_format) &&
679           isl_swizzle_is_identity(plane_format.swizzle))
680          flags |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT;
681    }
682 
683    /* Load/store is determined based on base format.  This prevents RGB
684     * formats from showing up as load/store capable.
685     */
686    if (isl_format_supports_typed_reads(devinfo, base_isl_format))
687       flags |= VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT;
688    if (isl_format_supports_typed_writes(devinfo, base_isl_format))
689       flags |= VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
690 
691    /* Keep this old behavior on VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT.
692     * When KHR_format_features2 is enabled, applications should only rely on
693     * it for the list of shader storage extended formats [1]. Before that,
694     * this applies to all VkFormats.
695     *
696     * [1] : https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#features-shaderStorageImageExtendedFormats
697     */
698    if (flags & VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT)
699       flags |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT;
700 
701    if (base_isl_format == ISL_FORMAT_R32_SINT ||
702        base_isl_format == ISL_FORMAT_R32_UINT ||
703        base_isl_format == ISL_FORMAT_R32_FLOAT)
704       flags |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_ATOMIC_BIT;
705 
706    if (flags) {
707       flags |= VK_FORMAT_FEATURE_2_BLIT_SRC_BIT |
708                VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
709                VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
710 
711       /* Blit destination requires rendering support. */
712       if (isl_format_supports_rendering(devinfo, plane_format.isl_format))
713          flags |= VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
714    }
715 
716    /* XXX: We handle 3-channel formats by switching them out for RGBX or
717     * RGBA formats behind-the-scenes.  This works fine for textures
718     * because the upload process will fill in the extra channel.
719     * We could also support it for render targets, but it will take
720     * substantially more work and we have enough RGBX formats to handle
721     * what most clients will want.
722     */
723    if (vk_tiling == VK_IMAGE_TILING_OPTIMAL &&
724        base_isl_format != ISL_FORMAT_UNSUPPORTED &&
725        !util_is_power_of_two_or_zero(isl_format_layouts[base_isl_format].bpb) &&
726        isl_format_rgb_to_rgbx(base_isl_format) == ISL_FORMAT_UNSUPPORTED) {
727       flags &= ~VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT;
728       flags &= ~VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
729    }
730 
731    const VkFormatFeatureFlags2 disallowed_ycbcr_image_features =
732       VK_FORMAT_FEATURE_2_BLIT_SRC_BIT |
733       VK_FORMAT_FEATURE_2_BLIT_DST_BIT |
734       VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT |
735       VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT |
736       VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT;
737 
738    if (anv_format->flags & ANV_FORMAT_FLAG_CAN_YCBCR) {
739       /* The sampler doesn't have support for mid point when it handles YUV on
740        * its own.
741        */
742       if (isl_format_is_yuv(anv_format->planes[0].isl_format)) {
743          /* TODO: We've disabled linear implicit reconstruction with the
744           * sampler. The failures show a slightly out of range values on the
745           * bottom left of the sampled image.
746           */
747          flags |= VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT;
748       } else {
749          flags |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT |
750                   VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT |
751                   VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_YCBCR_CONVERSION_SEPARATE_RECONSTRUCTION_FILTER_BIT;
752       }
753 
754       /* We can support cosited chroma locations when handle planes with our
755        * own shader snippets.
756        */
757       const struct vk_format_ycbcr_info *ycbcr_info =
758          vk_format_get_ycbcr_info(vk_format);
759       assert(anv_format->n_planes == ycbcr_info->n_planes);
760       for (unsigned p = 0; p < ycbcr_info->n_planes; p++) {
761          if (ycbcr_info->planes[p].denominator_scales[0] > 1 ||
762              ycbcr_info->planes[p].denominator_scales[1] > 1) {
763             flags |= VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT;
764             break;
765          }
766       }
767 
768       if (anv_format->n_planes > 1)
769          flags |= VK_FORMAT_FEATURE_2_DISJOINT_BIT;
770 
771       flags &= ~disallowed_ycbcr_image_features;
772    } else if (anv_format->flags & ANV_FORMAT_FLAG_CAN_VIDEO) {
773       /* This format is for video decoding. */
774       flags &= ~disallowed_ycbcr_image_features;
775    }
776 
777    if (vk_tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
778       if (!isl_drm_modifier_get_score(devinfo, isl_mod_info->modifier))
779          return 0;
780 
781       /* Try to restrict the supported formats to those in drm_fourcc.h. The
782        * VK_EXT_image_drm_format_modifier does not require this (after all, two
783        * Vulkan apps could share an image by exchanging its VkFormat instead of
784        * a DRM_FORMAT), but there exist no users of such non-drm_fourcc formats
785        * yet. And the restriction shrinks our test surface.
786        */
787       const struct isl_format_layout *isl_layout =
788          isl_format_get_layout(plane_format.isl_format);
789 
790       switch (isl_layout->colorspace) {
791       case ISL_COLORSPACE_LINEAR:
792       case ISL_COLORSPACE_SRGB:
793          /* Each DRM_FORMAT that we support uses unorm (if the DRM format name
794           * has no type suffix) or sfloat (if it has suffix F). No format
795           * contains mixed types. (as of 2021-06-14)
796           */
797          if (isl_layout->uniform_channel_type != ISL_UNORM &&
798              isl_layout->uniform_channel_type != ISL_SFLOAT)
799             return 0;
800          break;
801       case ISL_COLORSPACE_YUV:
802          anv_finishme("support YUV colorspace with DRM format modifiers");
803          return 0;
804       case ISL_COLORSPACE_NONE:
805          return 0;
806       }
807 
808       /* We could support compressed formats if we wanted to. */
809       if (isl_format_is_compressed(plane_format.isl_format))
810          return 0;
811 
812       /* No non-power-of-two fourcc formats exist.
813        *
814        * Even if non-power-of-two fourcc formats existed, we could support them
815        * only with DRM_FORMAT_MOD_LINEAR.  Tiled formats must be power-of-two
816        * because we implement transfers with the render pipeline.
817        */
818       if (anv_format_has_npot_plane(anv_format))
819          return 0;
820 
821       if (anv_format->n_planes > 1) {
822          /* For simplicity, keep DISJOINT disabled for multi-planar format. */
823          flags &= ~VK_FORMAT_FEATURE_2_DISJOINT_BIT;
824 
825          /* VK_ANDROID_external_memory_android_hardware_buffer in Virtio-GPU
826           * Venus driver layers on top of VK_EXT_image_drm_format_modifier of
827           * the host Vulkan driver, and both VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
828           * and VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM and required to support
829           * camera/media interop in Android.
830           */
831          if (vk_format != VK_FORMAT_G8_B8R8_2PLANE_420_UNORM &&
832              vk_format != VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM &&
833              vk_format != VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16) {
834             anv_finishme("support more multi-planar formats with DRM modifiers");
835             return 0;
836          }
837 
838          /* Currently there is no way to properly map memory planes to format
839           * planes and aux planes due to the lack of defined ABI for external
840           * multi-planar images.
841           */
842          if (isl_drm_modifier_has_aux(isl_mod_info->modifier)) {
843             return 0;
844          }
845       }
846 
847       if (isl_drm_modifier_has_aux(isl_mod_info->modifier) &&
848           !anv_format_supports_ccs_e(physical_device,
849                                      plane_format.isl_format)) {
850          return 0;
851       }
852 
853       if (isl_drm_modifier_has_aux(isl_mod_info->modifier)) {
854          /* Rejection DISJOINT for consistency with the GL driver. In
855           * eglCreateImage, we require that the dma_buf for the primary surface
856           * and the dma_buf for its aux surface refer to the same bo.
857           */
858          flags &= ~VK_FORMAT_FEATURE_2_DISJOINT_BIT;
859 
860          /* Gfx11 and prior bypass the aux surface when accessing storage
861           * images. We could support storage access on images with aux
862           * modifiers by resolving the aux surface prior to the storage
863           * access.
864           */
865          if (devinfo->ver <= 11)
866             flags &= ~VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT;
867 
868          /* Starting with gfx12.5, atomics are supported with compression.
869           * However, the performance of this combination is slow on gfx12.5.
870           * So, only allow atomic support on Xe2+.
871           */
872          if (devinfo->verx10 <= 125)
873             flags &= ~VK_FORMAT_FEATURE_2_STORAGE_IMAGE_ATOMIC_BIT;
874 
875          /* Host transfer don't touch the AUX data, so if that is required by
876           * the modifier, just drop support on the format.
877           */
878          flags &= ~VK_FORMAT_FEATURE_2_HOST_IMAGE_TRANSFER_BIT_EXT;
879       }
880 
881       if (isl_mod_info->supports_clear_color && plane_format.isl_format !=
882           blorp_copy_get_color_format(&physical_device->isl_dev,
883                                       plane_format.isl_format)) {
884          /* If the clear color is non-zero, blorp_copy() may interpret the raw
885           * clear color channels incorrectly when it changes the surface
886           * format. Disable support for this format as a copy destination.
887           */
888          flags &= ~VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
889       }
890    }
891 
892    if (devinfo->has_coarse_pixel_primitive_and_cb &&
893        vk_format == VK_FORMAT_R8_UINT &&
894        vk_tiling == VK_IMAGE_TILING_OPTIMAL)
895       flags |= VK_FORMAT_FEATURE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR;
896 
897    return flags;
898 }
899 
900 static VkFormatFeatureFlags2
get_buffer_format_features2(const struct intel_device_info * devinfo,VkFormat vk_format,const struct anv_format * anv_format)901 get_buffer_format_features2(const struct intel_device_info *devinfo,
902                             VkFormat vk_format,
903                             const struct anv_format *anv_format)
904 {
905    VkFormatFeatureFlags2 flags = 0;
906 
907    if (anv_format == NULL)
908       return 0;
909 
910    const enum isl_format isl_format = anv_format->planes[0].isl_format;
911 
912    if (isl_format == ISL_FORMAT_UNSUPPORTED)
913       return 0;
914 
915    if (anv_format->n_planes > 1)
916       return 0;
917 
918    if ((anv_format->flags & ANV_FORMAT_FLAG_CAN_YCBCR) ||
919        (anv_format->flags & ANV_FORMAT_FLAG_CAN_VIDEO))
920       return 0;
921 
922    if (vk_format_is_depth_or_stencil(vk_format))
923       return 0;
924 
925    if (isl_format_supports_sampling(devinfo, isl_format) &&
926        !isl_format_is_compressed(isl_format))
927       flags |= VK_FORMAT_FEATURE_2_UNIFORM_TEXEL_BUFFER_BIT;
928 
929    if (isl_format_supports_vertex_fetch(devinfo, isl_format))
930       flags |= VK_FORMAT_FEATURE_2_VERTEX_BUFFER_BIT;
931 
932    if (isl_is_storage_image_format(devinfo, isl_format))
933       flags |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_BIT;
934 
935    if (isl_format == ISL_FORMAT_R32_SINT || isl_format == ISL_FORMAT_R32_UINT)
936       flags |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
937 
938    if (isl_format_supports_typed_reads(devinfo, isl_format))
939       flags |= VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT;
940    if (isl_format_supports_typed_writes(devinfo, isl_format))
941       flags |= VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
942 
943    if (devinfo->has_ray_tracing) {
944 #if ANV_SUPPORT_RT_GRL
945       switch (vk_format) {
946       case VK_FORMAT_R32G32_SFLOAT:
947       case VK_FORMAT_R32G32B32_SFLOAT:
948       case VK_FORMAT_R16G16_SFLOAT:
949       case VK_FORMAT_R16G16B16A16_SFLOAT:
950       case VK_FORMAT_R16G16_SNORM:
951       case VK_FORMAT_R16G16B16A16_SNORM:
952       case VK_FORMAT_R16G16B16A16_UNORM:
953       case VK_FORMAT_R16G16_UNORM:
954       case VK_FORMAT_R8G8B8A8_UNORM:
955       case VK_FORMAT_R8G8_UNORM:
956       case VK_FORMAT_R8G8B8A8_SNORM:
957       case VK_FORMAT_R8G8_SNORM:
958          flags |= VK_FORMAT_FEATURE_ACCELERATION_STRUCTURE_VERTEX_BUFFER_BIT_KHR;
959          break;
960       default:
961          break;
962       }
963 #else
964       if (vk_acceleration_struct_vtx_format_supported(vk_format))
965          flags |= VK_FORMAT_FEATURE_ACCELERATION_STRUCTURE_VERTEX_BUFFER_BIT_KHR;
966 #endif
967    }
968 
969    return flags;
970 }
971 
972 static void
get_drm_format_modifier_properties_list(const struct anv_physical_device * physical_device,VkFormat vk_format,VkDrmFormatModifierPropertiesListEXT * list)973 get_drm_format_modifier_properties_list(const struct anv_physical_device *physical_device,
974                                         VkFormat vk_format,
975                                         VkDrmFormatModifierPropertiesListEXT *list)
976 {
977    const struct anv_format *anv_format = anv_get_format(physical_device, vk_format);
978 
979    VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierPropertiesEXT, out,
980                           list->pDrmFormatModifierProperties,
981                           &list->drmFormatModifierCount);
982 
983    isl_drm_modifier_info_for_each(isl_mod_info) {
984       VkFormatFeatureFlags2 features2 =
985          anv_get_image_format_features2(physical_device, vk_format, anv_format,
986                                         VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
987                                         isl_mod_info);
988       VkFormatFeatureFlags features = vk_format_features2_to_features(features2);
989       if (!features)
990          continue;
991 
992       const uint32_t planes =
993          isl_drm_modifier_get_plane_count(&physical_device->info,
994                                           isl_mod_info->modifier,
995                                           anv_format->n_planes);
996 
997       vk_outarray_append_typed(VkDrmFormatModifierPropertiesEXT, &out, out_props) {
998          *out_props = (VkDrmFormatModifierPropertiesEXT) {
999             .drmFormatModifier = isl_mod_info->modifier,
1000             .drmFormatModifierPlaneCount = planes,
1001             .drmFormatModifierTilingFeatures = features,
1002          };
1003       };
1004    }
1005 }
1006 
1007 static void
get_drm_format_modifier_properties_list_2(const struct anv_physical_device * physical_device,VkFormat vk_format,VkDrmFormatModifierPropertiesList2EXT * list)1008 get_drm_format_modifier_properties_list_2(const struct anv_physical_device *physical_device,
1009                                           VkFormat vk_format,
1010                                           VkDrmFormatModifierPropertiesList2EXT *list)
1011 {
1012    const struct anv_format *anv_format = anv_get_format(physical_device, vk_format);
1013 
1014    VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierProperties2EXT, out,
1015                           list->pDrmFormatModifierProperties,
1016                           &list->drmFormatModifierCount);
1017 
1018    isl_drm_modifier_info_for_each(isl_mod_info) {
1019       VkFormatFeatureFlags2 features2 =
1020          anv_get_image_format_features2(physical_device, vk_format, anv_format,
1021                                         VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
1022                                         isl_mod_info);
1023       if (!features2)
1024          continue;
1025 
1026       const uint32_t planes =
1027          isl_drm_modifier_get_plane_count(&physical_device->info,
1028                                           isl_mod_info->modifier,
1029                                           anv_format->n_planes);
1030 
1031       vk_outarray_append_typed(VkDrmFormatModifierProperties2EXT, &out, out_props) {
1032          *out_props = (VkDrmFormatModifierProperties2EXT) {
1033             .drmFormatModifier = isl_mod_info->modifier,
1034             .drmFormatModifierPlaneCount = planes,
1035             .drmFormatModifierTilingFeatures = features2,
1036          };
1037       };
1038    }
1039 }
1040 
anv_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,VkFormat vk_format,VkFormatProperties2 * pFormatProperties)1041 void anv_GetPhysicalDeviceFormatProperties2(
1042     VkPhysicalDevice                            physicalDevice,
1043     VkFormat                                    vk_format,
1044     VkFormatProperties2*                        pFormatProperties)
1045 {
1046    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1047    const struct intel_device_info *devinfo = &physical_device->info;
1048    const struct anv_format *anv_format = anv_get_format(physical_device, vk_format);
1049 
1050    assert(pFormatProperties->sType == VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2);
1051 
1052    VkFormatFeatureFlags2 linear2, optimal2, buffer2;
1053    linear2 = anv_get_image_format_features2(physical_device, vk_format,
1054                                             anv_format,
1055                                             VK_IMAGE_TILING_LINEAR, NULL);
1056    optimal2 = anv_get_image_format_features2(physical_device, vk_format,
1057                                              anv_format,
1058                                              VK_IMAGE_TILING_OPTIMAL, NULL);
1059    buffer2 = get_buffer_format_features2(devinfo, vk_format, anv_format);
1060 
1061    pFormatProperties->formatProperties = (VkFormatProperties) {
1062       .linearTilingFeatures = vk_format_features2_to_features(linear2),
1063       .optimalTilingFeatures = vk_format_features2_to_features(optimal2),
1064       .bufferFeatures = vk_format_features2_to_features(buffer2),
1065    };
1066 
1067    vk_foreach_struct(ext, pFormatProperties->pNext) {
1068       /* Use unsigned since some cases are not in the VkStructureType enum. */
1069       switch ((unsigned)ext->sType) {
1070       case VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT:
1071          get_drm_format_modifier_properties_list(physical_device, vk_format,
1072                                                  (void *)ext);
1073          break;
1074 
1075       case VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_2_EXT:
1076          get_drm_format_modifier_properties_list_2(physical_device, vk_format,
1077                                                    (void *)ext);
1078          break;
1079 
1080       case VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3: {
1081          VkFormatProperties3 *props = (VkFormatProperties3 *)ext;
1082          props->linearTilingFeatures = linear2;
1083          props->optimalTilingFeatures = optimal2;
1084          props->bufferFeatures = buffer2;
1085          break;
1086       }
1087       case VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR:
1088          /* don't have any thing to use this for yet */
1089          break;
1090       default:
1091          vk_debug_ignored_stype(ext->sType);
1092          break;
1093       }
1094    }
1095 }
1096 
1097 static bool
anv_format_supports_usage(VkFormatFeatureFlags2 format_feature_flags,VkImageUsageFlags usage_flags)1098 anv_format_supports_usage(
1099    VkFormatFeatureFlags2 format_feature_flags,
1100    VkImageUsageFlags usage_flags)
1101 {
1102    if (usage_flags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
1103       if (!(format_feature_flags & (VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
1104                                     VK_FORMAT_FEATURE_2_BLIT_SRC_BIT))) {
1105          return false;
1106       }
1107    }
1108 
1109    if (usage_flags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
1110       if (!(format_feature_flags & (VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT |
1111                                     VK_FORMAT_FEATURE_2_BLIT_DST_BIT))) {
1112          return false;
1113       }
1114    }
1115 
1116    if (usage_flags & VK_IMAGE_USAGE_SAMPLED_BIT) {
1117       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT)) {
1118          return false;
1119       }
1120    }
1121 
1122    if (usage_flags & VK_IMAGE_USAGE_STORAGE_BIT) {
1123       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT)) {
1124          return false;
1125       }
1126    }
1127 
1128    if (usage_flags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
1129       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT)) {
1130          return false;
1131       }
1132    }
1133 
1134    if (usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1135       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) {
1136          return false;
1137       }
1138    }
1139 
1140    if (usage_flags & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
1141       /* Nothing to check. */
1142    }
1143 
1144    if (usage_flags & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
1145       if (!(format_feature_flags & (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT |
1146                                     VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT))) {
1147          return false;
1148       }
1149    }
1150 
1151    if (usage_flags & VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR) {
1152       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR)) {
1153          return false;
1154       }
1155    }
1156 
1157    return true;
1158 }
1159 
1160 static bool
anv_formats_are_compatible(const struct anv_physical_device * physical_device,const struct anv_format * img_fmt,const struct anv_format * img_view_fmt,VkImageTiling tiling,bool allow_texel_compatible)1161 anv_formats_are_compatible(
1162    const struct anv_physical_device *physical_device,
1163    const struct anv_format *img_fmt, const struct anv_format *img_view_fmt,
1164    VkImageTiling tiling, bool allow_texel_compatible)
1165 {
1166    if (img_view_fmt->vk_format == VK_FORMAT_UNDEFINED)
1167       return false;
1168 
1169    if (img_fmt == img_view_fmt)
1170       return true;
1171 
1172    /* TODO: Handle multi-planar images that can have view of a plane with
1173     * possibly different type.
1174     */
1175    if (img_fmt->n_planes != 1 || img_view_fmt->n_planes != 1)
1176       return false;
1177 
1178    const enum isl_format img_isl_fmt =
1179       anv_get_format_plane(physical_device,
1180                            img_fmt->vk_format, 0, tiling).isl_format;
1181    const enum isl_format img_view_isl_fmt =
1182       anv_get_format_plane(physical_device,
1183                            img_view_fmt->vk_format, 0, tiling).isl_format;
1184    if (img_isl_fmt == ISL_FORMAT_UNSUPPORTED ||
1185        img_view_isl_fmt == ISL_FORMAT_UNSUPPORTED)
1186       return false;
1187 
1188    const struct isl_format_layout *img_fmt_layout =
1189          isl_format_get_layout(img_isl_fmt);
1190    const struct isl_format_layout *img_view_fmt_layout =
1191          isl_format_get_layout(img_view_isl_fmt);
1192 
1193    /* From the Vulkan 1.3.230 spec "12.5. Image Views"
1194     *
1195     *    "If image was created with the
1196     *    VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT flag, format must be
1197     *    compatible with the image’s format as described above; or must be
1198     *    an uncompressed format, in which case it must be size-compatible
1199     *    with the image’s format."
1200     */
1201    if (allow_texel_compatible &&
1202        isl_format_is_compressed(img_isl_fmt) &&
1203        !isl_format_is_compressed(img_view_isl_fmt) &&
1204        img_fmt_layout->bpb == img_view_fmt_layout->bpb)
1205       return true;
1206 
1207    if (isl_format_is_compressed(img_isl_fmt) !=
1208        isl_format_is_compressed(img_view_isl_fmt))
1209       return false;
1210 
1211    if (!isl_format_is_compressed(img_isl_fmt)) {
1212       /* From the Vulkan 1.3.224 spec "43.1.6. Format Compatibility Classes":
1213        *
1214        *    "Uncompressed color formats are compatible with each other if they
1215        *    occupy the same number of bits per texel block."
1216        */
1217       return img_fmt_layout->bpb == img_view_fmt_layout->bpb;
1218    }
1219 
1220    /* From the Vulkan 1.3.224 spec "43.1.6. Format Compatibility Classes":
1221     *
1222     *    "Compressed color formats are compatible with each other if the only
1223     *    difference between them is the numerical type of the uncompressed
1224     *    pixels (e.g. signed vs. unsigned, or SRGB vs. UNORM encoding)."
1225     */
1226    return img_fmt_layout->txc == img_view_fmt_layout->txc &&
1227           isl_formats_have_same_bits_per_channel(img_isl_fmt, img_view_isl_fmt);
1228 }
1229 
1230 /* Returns a set of feature flags supported by any of the VkFormat listed in
1231  * format_list_info or any VkFormat compatible with format.
1232  */
1233 static VkFormatFeatureFlags2
anv_formats_gather_format_features(const struct anv_physical_device * physical_device,const struct anv_format * format,VkImageTiling tiling,const struct isl_drm_modifier_info * isl_mod_info,const VkImageFormatListCreateInfo * format_list_info,bool allow_texel_compatible)1234 anv_formats_gather_format_features(
1235    const struct anv_physical_device *physical_device,
1236    const struct anv_format *format,
1237    VkImageTiling tiling,
1238    const struct isl_drm_modifier_info *isl_mod_info,
1239    const VkImageFormatListCreateInfo *format_list_info,
1240    bool allow_texel_compatible)
1241 {
1242    VkFormatFeatureFlags2 all_formats_feature_flags = 0;
1243 
1244    /* We need to check that each of the usage bits are allowed for at least
1245     * one of the potential formats.
1246     */
1247    if (!format_list_info || format_list_info->viewFormatCount == 0) {
1248       /* If we specify no list of possible formats, we need to assume that
1249        * every compatible format is possible and consider the features
1250        * supported by each of them.
1251        */
1252       for (uint32_t fmt_arr_ind = 0;
1253            fmt_arr_ind < ARRAY_SIZE(anv_formats);
1254            ++fmt_arr_ind) {
1255          for (uint32_t fmt_ind = 0;
1256               fmt_ind < anv_formats[fmt_arr_ind].n_formats;
1257               ++fmt_ind) {
1258             const struct anv_format *possible_anv_format =
1259                &(anv_formats[fmt_arr_ind].formats[fmt_ind]);
1260 
1261             if (anv_formats_are_compatible(physical_device,
1262                                            format, possible_anv_format,
1263                                            tiling, allow_texel_compatible)) {
1264                VkFormatFeatureFlags2 view_format_features =
1265                   anv_get_image_format_features2(physical_device,
1266                                                  possible_anv_format->vk_format,
1267                                                  possible_anv_format, tiling,
1268                                                  isl_mod_info);
1269                all_formats_feature_flags |= view_format_features;
1270             }
1271          }
1272       }
1273    } else {
1274       /* If we provide the list of possible formats, then check just them. */
1275       for (uint32_t i = 0; i < format_list_info->viewFormatCount; ++i) {
1276          VkFormat vk_view_format = format_list_info->pViewFormats[i];
1277 
1278          if (vk_view_format == VK_FORMAT_UNDEFINED)
1279             continue;
1280 
1281          const struct anv_format *anv_view_format =
1282             anv_get_format(physical_device, vk_view_format);
1283          VkFormatFeatureFlags2 view_format_features =
1284             anv_get_image_format_features2(physical_device,
1285                                            vk_view_format, anv_view_format,
1286                                            tiling, isl_mod_info);
1287          all_formats_feature_flags |= view_format_features;
1288       }
1289    }
1290 
1291    return all_formats_feature_flags;
1292 }
1293 
1294 /* Supports opaque fd but not dma_buf. */
1295 static const VkExternalMemoryProperties opaque_fd_only_props = {
1296    .externalMemoryFeatures =
1297       VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1298       VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1299    .exportFromImportedHandleTypes =
1300       VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
1301    .compatibleHandleTypes =
1302       VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
1303 };
1304 
1305 /* Supports opaque fd and dma_buf. */
1306 static const VkExternalMemoryProperties opaque_fd_dma_buf_props = {
1307    .externalMemoryFeatures =
1308       VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1309       VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1310    .exportFromImportedHandleTypes =
1311       VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1312       VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
1313    .compatibleHandleTypes =
1314       VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1315       VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
1316 };
1317 
1318 static const VkExternalMemoryProperties userptr_props = {
1319    .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1320    .exportFromImportedHandleTypes = 0,
1321    .compatibleHandleTypes =
1322       VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,
1323 };
1324 
1325 static const VkExternalMemoryProperties android_buffer_props = {
1326    .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1327                              VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1328    .exportFromImportedHandleTypes =
1329       VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1330    .compatibleHandleTypes =
1331       VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1332 };
1333 
1334 
1335 static const VkExternalMemoryProperties android_image_props = {
1336    /* VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT will be set dynamically */
1337    .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT |
1338                              VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT,
1339    .exportFromImportedHandleTypes =
1340       VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1341    .compatibleHandleTypes =
1342       VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1343 };
1344 
1345 static VkResult
anv_get_image_format_properties(struct anv_physical_device * physical_device,const VkPhysicalDeviceImageFormatInfo2 * info,VkImageFormatProperties2 * props)1346 anv_get_image_format_properties(
1347    struct anv_physical_device *physical_device,
1348    const VkPhysicalDeviceImageFormatInfo2 *info,
1349    VkImageFormatProperties2 *props)
1350 {
1351    VkFormatFeatureFlags2 format_feature_flags;
1352    VkExtent3D maxExtent;
1353    uint32_t maxMipLevels;
1354    uint32_t maxArraySize;
1355    VkSampleCountFlags sampleCounts;
1356    const struct intel_device_info *devinfo = &physical_device->info;
1357    const struct anv_format *format = anv_get_format(physical_device, info->format);
1358    const struct isl_drm_modifier_info *isl_mod_info = NULL;
1359    const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *modifier_info = NULL;
1360    const VkImageFormatListCreateInfo *format_list_info = NULL;
1361    const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
1362    VkExternalImageFormatProperties *external_props = NULL;
1363    VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;
1364    VkAndroidHardwareBufferUsageANDROID *android_usage = NULL;
1365    VkTextureLODGatherFormatPropertiesAMD *texture_lod_gather_props = NULL;
1366    VkImageCompressionPropertiesEXT *comp_props = NULL;
1367    VkHostImageCopyDevicePerformanceQueryEXT *host_props = NULL;
1368    bool from_wsi = false;
1369 
1370    /* Extract input structs */
1371    vk_foreach_struct_const(s, info->pNext) {
1372       switch ((unsigned)s->sType) {
1373       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
1374          external_info = (const void *) s;
1375          break;
1376       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT:
1377          modifier_info = (const void *)s;
1378          break;
1379       case VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO:
1380          format_list_info = (const void *)s;
1381          break;
1382       case VK_STRUCTURE_TYPE_IMAGE_STENCIL_USAGE_CREATE_INFO:
1383          /* Ignore but don't warn */
1384          break;
1385       case VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA:
1386          from_wsi = true;
1387          break;
1388       case VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR:
1389          /* Ignore but don't warn */
1390          break;
1391       case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT:
1392          /* Ignore but don't warn */
1393          break;
1394       default:
1395          vk_debug_ignored_stype(s->sType);
1396          break;
1397       }
1398    }
1399 
1400    /* Extract output structs */
1401    vk_foreach_struct(s, props->pNext) {
1402       switch (s->sType) {
1403       case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
1404          external_props = (void *) s;
1405          break;
1406       case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:
1407          ycbcr_props = (void *) s;
1408          break;
1409       case VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_USAGE_ANDROID:
1410          android_usage = (void *) s;
1411          break;
1412       case VK_STRUCTURE_TYPE_TEXTURE_LOD_GATHER_FORMAT_PROPERTIES_AMD:
1413          texture_lod_gather_props = (void *) s;
1414          break;
1415       case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT:
1416          comp_props = (void *) s;
1417          break;
1418       case VK_STRUCTURE_TYPE_HOST_IMAGE_COPY_DEVICE_PERFORMANCE_QUERY_EXT:
1419          host_props = (void *) s;
1420          break;
1421       default:
1422          vk_debug_ignored_stype(s->sType);
1423          break;
1424       }
1425    }
1426 
1427    if (format == NULL)
1428       goto unsupported;
1429 
1430    /* Would require some annoying tracking or kernel support. */
1431    if ((info->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) &&
1432        (info->usage & VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT))
1433       goto unsupported;
1434 
1435    if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
1436       isl_mod_info = isl_drm_modifier_get_info(modifier_info->drmFormatModifier);
1437       if (isl_mod_info == NULL)
1438          goto unsupported;
1439 
1440       /* only allow Y-tiling/Tile4 for video decode. */
1441       if (info->usage & VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR) {
1442          if (isl_mod_info->tiling != ISL_TILING_Y0 && isl_mod_info->tiling != ISL_TILING_4)
1443             goto unsupported;
1444       }
1445    }
1446 
1447    assert(format->vk_format == info->format);
1448 
1449    switch (info->type) {
1450    default:
1451       unreachable("bad VkImageType");
1452    case VK_IMAGE_TYPE_1D:
1453       maxExtent.width = 16384;
1454       maxExtent.height = 1;
1455       maxExtent.depth = 1;
1456       maxMipLevels = 15; /* log2(maxWidth) + 1 */
1457       maxArraySize = 2048;
1458       sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1459       break;
1460    case VK_IMAGE_TYPE_2D:
1461       /* FINISHME: Does this really differ for cube maps? The documentation
1462        * for RENDER_SURFACE_STATE suggests so.
1463        */
1464       maxExtent.width = 16384;
1465       maxExtent.height = 16384;
1466       maxExtent.depth = 1;
1467       maxMipLevels = 15; /* log2(maxWidth) + 1 */
1468       maxArraySize = 2048;
1469       sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1470       break;
1471    case VK_IMAGE_TYPE_3D:
1472       maxExtent.width = 2048;
1473       maxExtent.height = 2048;
1474       maxExtent.depth = 2048;
1475       maxMipLevels = 12; /* log2(maxWidth) + 1 */
1476       maxArraySize = 1;
1477       sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1478       break;
1479    }
1480 
1481    /* If any of the format in VkImageFormatListCreateInfo is completely
1482     * unsupported, report unsupported.
1483     */
1484    if ((info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) &&
1485        format_list_info != NULL) {
1486       for (uint32_t i = 0; i < format_list_info->viewFormatCount; i++) {
1487          const struct anv_format *view_format =
1488             anv_get_format(physical_device, format_list_info->pViewFormats[i]);
1489          if (view_format == NULL)
1490             goto unsupported;
1491       }
1492    }
1493 
1494    /* From the Vulkan 1.3.218 spec:
1495     *
1496     *    "For images created without VK_IMAGE_CREATE_EXTENDED_USAGE_BIT a usage
1497     *    bit is valid if it is supported for the format the image is created with.
1498     *    For images created with VK_IMAGE_CREATE_EXTENDED_USAGE_BIT a usage bit
1499     *    is valid if it is supported for at least one of the formats
1500     *    a VkImageView created from the image can have."
1501     *
1502     *    "VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT specifies that the image can be
1503     *    used to create a VkImageView with a different format from the image."
1504     *
1505     * So, if both VK_IMAGE_CREATE_EXTENDED_USAGE_BIT and
1506     * VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT are set, views can be created with
1507     * different usage than the image, so we can't always filter on usage.
1508     * There is one exception to this below for storage.
1509     */
1510    format_feature_flags = anv_get_image_format_features2(physical_device,
1511                                                          info->format, format,
1512                                                          info->tiling,
1513                                                          isl_mod_info);
1514 
1515    if (!anv_format_supports_usage(format_feature_flags, info->usage)) {
1516       /* If image format itself does not support the usage, and we don't allow
1517        * views formats to support it, then we can't support this usage at all.
1518        */
1519       if (!(info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) ||
1520           !(info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT))
1521          goto unsupported;
1522 
1523       /* We don't want emulated formats to gain unexpected usage (storage in
1524        * particular) from its compatible view formats.
1525        */
1526       if (anv_is_format_emulated(physical_device, info->format))
1527          goto unsupported;
1528 
1529       /* From the Vulkan 1.3.224 spec "43.1.6. Format Compatibility Classes":
1530        *
1531        *    "Each depth/stencil format is only compatible with itself."
1532        *
1533        * So, other formats also can't help.
1534        */
1535       if (vk_format_is_depth_or_stencil(info->format))
1536          goto unsupported;
1537 
1538       /* Gather all possible format feature flags for the formats listed in
1539        * the format list or all the compatible formats.
1540        */
1541       VkFormatFeatureFlags2 all_formats_feature_flags = format_feature_flags |
1542          anv_formats_gather_format_features(physical_device, format,
1543                                             info->tiling, isl_mod_info,
1544                                             format_list_info,
1545                                             info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT);
1546 
1547       if (!anv_format_supports_usage(all_formats_feature_flags, info->usage))
1548          goto unsupported;
1549    }
1550 
1551    if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
1552       /* We support modifiers only for "simple" (that is, non-array
1553        * non-mipmapped single-sample) 2D images.
1554        */
1555       if (info->type != VK_IMAGE_TYPE_2D) {
1556          vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1557                    "VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT "
1558                    "requires VK_IMAGE_TYPE_2D");
1559          goto unsupported;
1560       }
1561 
1562       maxArraySize = 1;
1563       maxMipLevels = 1;
1564       sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1565 
1566       if (isl_mod_info->supports_clear_color &&
1567           (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) &&
1568           (info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
1569          /* Format reinterpretation generally won't work correctly with the
1570           * raw clear color channels.
1571           */
1572          goto unsupported;
1573       }
1574 
1575       if (isl_drm_modifier_has_aux(isl_mod_info->modifier) &&
1576           !anv_formats_ccs_e_compatible(physical_device, info->flags, info->format,
1577                                         info->tiling, info->usage,
1578                                         format_list_info)) {
1579          goto unsupported;
1580       }
1581    }
1582 
1583    /* Our hardware doesn't support 1D compressed textures.
1584     *    From the SKL PRM, RENDER_SURFACE_STATE::SurfaceFormat:
1585     *    * This field cannot be a compressed (BC*, DXT*, FXT*, ETC*, EAC*) format
1586     *       if the Surface Type is SURFTYPE_1D.
1587     *    * This field cannot be ASTC format if the Surface Type is SURFTYPE_1D.
1588     */
1589    if (info->type == VK_IMAGE_TYPE_1D &&
1590        isl_format_is_compressed(format->planes[0].isl_format)) {
1591        goto unsupported;
1592    }
1593 
1594    if (info->tiling == VK_IMAGE_TILING_OPTIMAL &&
1595        info->type == VK_IMAGE_TYPE_2D &&
1596        (format_feature_flags & (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT |
1597                                 VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
1598        !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
1599        !(info->usage & VK_IMAGE_USAGE_STORAGE_BIT) &&
1600        isl_format_supports_multisampling(devinfo, format->planes[0].isl_format)) {
1601       sampleCounts = isl_device_get_sample_counts(&physical_device->isl_dev);
1602    }
1603 
1604    if (info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1605       /* Non-power-of-two formats can never be used as storage images.  We
1606        * only check plane 0 because there are no YCbCr formats with
1607        * non-power-of-two planes.
1608        */
1609       const struct isl_format_layout *isl_layout =
1610          isl_format_get_layout(format->planes[0].isl_format);
1611       if (!util_is_power_of_two_or_zero(isl_layout->bpb))
1612          goto unsupported;
1613    }
1614 
1615    if (info->flags & VK_IMAGE_CREATE_DISJOINT_BIT) {
1616       /* From the Vulkan 1.2.149 spec, VkImageCreateInfo:
1617        *
1618        *    If format is a multi-planar format, and if imageCreateFormatFeatures
1619        *    (as defined in Image Creation Limits) does not contain
1620        *    VK_FORMAT_FEATURE_2_DISJOINT_BIT, then flags must not contain
1621        *    VK_IMAGE_CREATE_DISJOINT_BIT.
1622        */
1623       if (format->n_planes > 1 &&
1624           !(format_feature_flags & VK_FORMAT_FEATURE_2_DISJOINT_BIT)) {
1625          goto unsupported;
1626       }
1627 
1628       /* From the Vulkan 1.2.149 spec, VkImageCreateInfo:
1629        *
1630        * If format is not a multi-planar format, and flags does not include
1631        * VK_IMAGE_CREATE_ALIAS_BIT, flags must not contain
1632        * VK_IMAGE_CREATE_DISJOINT_BIT.
1633        */
1634       if (format->n_planes == 1 &&
1635           !(info->flags & VK_IMAGE_CREATE_ALIAS_BIT)) {
1636           goto unsupported;
1637       }
1638 
1639       if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT &&
1640           isl_drm_modifier_has_aux(isl_mod_info->modifier)) {
1641          /* Rejection DISJOINT for consistency with the GL driver. In
1642           * eglCreateImage, we require that the dma_buf for the primary surface
1643           * and the dma_buf for its aux surface refer to the same bo.
1644           */
1645          goto unsupported;
1646       }
1647    }
1648 
1649    if ((info->flags & VK_IMAGE_CREATE_ALIAS_BIT) && !from_wsi) {
1650       /* Reject aliasing of images with non-linear DRM format modifiers because:
1651        *
1652        * 1. For modifiers with compression, we store aux tracking state in
1653        *    ANV_IMAGE_MEMORY_BINDING_PRIVATE, which is not aliasable because it's
1654        *    not client-bound.
1655        *
1656        * 2. For tiled modifiers without compression, we may attempt to compress
1657        *    them behind the scenes, in which case both the aux tracking state
1658        *    and the CCS data are bound to ANV_IMAGE_MEMORY_BINDING_PRIVATE.
1659        *
1660        * 3. For WSI we should ignore ALIAS_BIT because we have the ability to
1661        *    bind the ANV_MEMORY_BINDING_PRIVATE from the other WSI image.
1662        */
1663       if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT &&
1664           isl_mod_info->modifier != DRM_FORMAT_MOD_LINEAR) {
1665          goto unsupported;
1666       }
1667    }
1668 
1669    if ((info->usage & VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR) &&
1670        !devinfo->has_coarse_pixel_primitive_and_cb)
1671       goto unsupported;
1672 
1673    /* From the bspec section entitled "Surface Layout and Tiling",
1674     * Gfx9 has a 256 GB limitation and Gfx11+ has a 16 TB limitation.
1675     */
1676    uint64_t maxResourceSize = 0;
1677    if (devinfo->ver < 11)
1678       maxResourceSize = (uint64_t) 1 << 38;
1679    else
1680       maxResourceSize = (uint64_t) 1 << 44;
1681 
1682    props->imageFormatProperties = (VkImageFormatProperties) {
1683       .maxExtent = maxExtent,
1684       .maxMipLevels = maxMipLevels,
1685       .maxArrayLayers = maxArraySize,
1686       .sampleCounts = sampleCounts,
1687 
1688       /* FINISHME: Accurately calculate
1689        * VkImageFormatProperties::maxResourceSize.
1690        */
1691       .maxResourceSize = maxResourceSize,
1692    };
1693 
1694    if (ycbcr_props)
1695       ycbcr_props->combinedImageSamplerDescriptorCount = format->n_planes;
1696 
1697    if (texture_lod_gather_props) {
1698       texture_lod_gather_props->supportsTextureGatherLODBiasAMD =
1699          physical_device->info.ver >= 20;
1700    }
1701 
1702    bool ahw_supported =
1703       physical_device->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer;
1704 
1705    if (ahw_supported && android_usage) {
1706       android_usage->androidHardwareBufferUsage =
1707          vk_image_usage_to_ahb_usage(info->flags, info->usage);
1708 
1709       /* Limit maxArrayLayers to 1 for AHardwareBuffer based images for now. */
1710       props->imageFormatProperties.maxArrayLayers = 1;
1711    }
1712 
1713    /* From the Vulkan 1.0.42 spec:
1714     *
1715     *    If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
1716     *    behave as if VkPhysicalDeviceExternalImageFormatInfo was not
1717     *    present and VkExternalImageFormatProperties will be ignored.
1718     */
1719    if (external_info && external_info->handleType != 0) {
1720       /* Does there exist a method for app and driver to explicitly communicate
1721        * to each other the image's memory layout?
1722        */
1723       bool tiling_has_explicit_layout;
1724 
1725       switch (info->tiling) {
1726       default:
1727          unreachable("bad VkImageTiling");
1728       case VK_IMAGE_TILING_LINEAR:
1729          /* The app can query the image's memory layout with
1730           * vkGetImageSubresourceLayout.
1731           */
1732          tiling_has_explicit_layout = true;
1733          break;
1734       case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT:
1735          /* The app can provide the image's memory layout with
1736           * VkImageDrmFormatModifierExplicitCreateInfoEXT;
1737           * or the app can query it with vkGetImageSubresourceLayout.
1738           */
1739          tiling_has_explicit_layout = true;
1740          break;
1741       case VK_IMAGE_TILING_OPTIMAL:
1742          /* The app can neither query nor provide the image's memory layout. */
1743          tiling_has_explicit_layout = false;
1744          break;
1745       }
1746 
1747       /* Compatibility between tiling and external memory handles
1748        * --------------------------------------------------------
1749        * When importing or exporting an image, there must exist a method that
1750        * enables the app and driver to agree on the image's memory layout. If no
1751        * method exists, then we reject image creation here.
1752        *
1753        * If the memory handle requires matching
1754        * VkPhysicalDeviceIDProperties::driverUUID and ::deviceUUID, then the
1755        * match-requirement guarantees that all users of the image agree on the
1756        * image's memory layout.
1757        *
1758        * If the memory handle does not require matching
1759        * VkPhysicalDeviceIDProperties::driverUUID nor ::deviceUUID, then we
1760        * require that the app and driver be able to explicitly communicate to
1761        * each other the image's memory layout.
1762        *
1763        * (For restrictions on driverUUID and deviceUUID, see the Vulkan 1.2.149
1764        * spec, Table 73 "External memory handle types").
1765        */
1766       switch (external_info->handleType) {
1767       case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1768          if (external_props) {
1769             if (tiling_has_explicit_layout) {
1770                /* With an explicit memory layout, we don't care which type of fd
1771                 * the image belongs too. Both OPAQUE_FD and DMA_BUF are
1772                 * interchangeable here.
1773                 */
1774                external_props->externalMemoryProperties = opaque_fd_dma_buf_props;
1775             } else {
1776                /* With an implicit memory layout, we must rely on deviceUUID
1777                 * and driverUUID to determine the layout. Therefore DMA_BUF is
1778                 * incompatible here.
1779                 */
1780                external_props->externalMemoryProperties = opaque_fd_only_props;
1781             }
1782          }
1783          break;
1784       case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1785          /* This memory handle has no restrictions on driverUUID nor deviceUUID,
1786           * and therefore requires explicit memory layout.
1787           */
1788          if (!tiling_has_explicit_layout) {
1789             vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1790                       "VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT "
1791                       "requires VK_IMAGE_TILING_LINEAR or "
1792                       "VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT");
1793             goto unsupported;
1794          }
1795 
1796          /* With an explicit memory layout, we don't care which type of fd
1797           * the image belongs too. Both OPAQUE_FD and DMA_BUF are
1798           * interchangeable here.
1799           */
1800          if (external_props)
1801             external_props->externalMemoryProperties = opaque_fd_dma_buf_props;
1802          break;
1803       case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1804          /* This memory handle has no restrictions on driverUUID nor deviceUUID,
1805           * and therefore requires explicit memory layout.
1806           */
1807          if (!tiling_has_explicit_layout) {
1808             vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1809                       "VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT "
1810                       "requires VK_IMAGE_TILING_LINEAR or "
1811                       "VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT");
1812             goto unsupported;
1813          }
1814 
1815          if (external_props)
1816             external_props->externalMemoryProperties = userptr_props;
1817          break;
1818       case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
1819          /* This memory handle is magic. The Vulkan spec says it has no
1820           * requirements regarding deviceUUID nor driverUUID, but Android still
1821           * requires support for VK_IMAGE_TILING_OPTIMAL. Android systems
1822           * communicate the image's memory layout through backdoor channels.
1823           */
1824          if (ahw_supported) {
1825             if (external_props) {
1826                external_props->externalMemoryProperties = android_image_props;
1827                if (anv_ahb_format_for_vk_format(info->format)) {
1828                   external_props->externalMemoryProperties.externalMemoryFeatures |=
1829                      VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT;
1830                }
1831             }
1832             break;
1833          }
1834          FALLTHROUGH; /* If ahw not supported */
1835       default:
1836          /* From the Vulkan 1.0.42 spec:
1837           *
1838           *    If handleType is not compatible with the [parameters] specified
1839           *    in VkPhysicalDeviceImageFormatInfo2, then
1840           *    vkGetPhysicalDeviceImageFormatProperties2 returns
1841           *    VK_ERROR_FORMAT_NOT_SUPPORTED.
1842           */
1843          vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1844                    "unsupported VkExternalMemoryTypeFlagBits 0x%x",
1845                    external_info->handleType);
1846          goto unsupported;
1847       }
1848    }
1849 
1850    const bool aux_supported =
1851       vk_format_has_depth(info->format) ||
1852       isl_format_supports_ccs_d(devinfo, format->planes[0].isl_format) ||
1853       anv_formats_ccs_e_compatible(physical_device, info->flags, info->format,
1854                                    info->tiling, info->usage,
1855                                    format_list_info);
1856 
1857    if (comp_props) {
1858       comp_props->imageCompressionFixedRateFlags =
1859          VK_IMAGE_COMPRESSION_FIXED_RATE_NONE_EXT;
1860       comp_props->imageCompressionFlags = aux_supported ?
1861          VK_IMAGE_COMPRESSION_DEFAULT_EXT :
1862          VK_IMAGE_COMPRESSION_DISABLED_EXT;
1863    }
1864 
1865    if (host_props) {
1866       const bool compressed_format =
1867          isl_format_is_compressed(format->planes[0].isl_format);
1868       /* We're required to return optimalDeviceAccess for compressed formats:
1869        *
1870        *    "If VkPhysicalDeviceImageFormatInfo2::format is a block-compressed
1871        *     format and vkGetPhysicalDeviceImageFormatProperties2 returns
1872        *     VK_SUCCESS, the implementation must return VK_TRUE in
1873        *     optimalDeviceAccess."
1874        *
1875        * When compression is not supported, the size of the image will not be
1876        * changing to support host image transfers.
1877        *
1878        * TODO: We might be able to still allocate the compression data so that
1879        *       we can report identicalMemoryLayout=true, but we might still
1880        *       have to report optimalDeviceAccess=false to signal potential
1881        *       perf loss.
1882        */
1883       if (compressed_format || !aux_supported) {
1884          host_props->optimalDeviceAccess = true;
1885          host_props->identicalMemoryLayout = true;
1886       } else {
1887          host_props->optimalDeviceAccess = false;
1888          host_props->identicalMemoryLayout = false;
1889       }
1890    }
1891 
1892    return VK_SUCCESS;
1893 
1894 unsupported:
1895    /* From the Vulkan 1.0.42 spec:
1896     *
1897     *    If the combination of parameters to
1898     *    vkGetPhysicalDeviceImageFormatProperties2 is not supported by the
1899     *    implementation for use in vkCreateImage, then all members of
1900     *    imageFormatProperties will be filled with zero.
1901     */
1902    props->imageFormatProperties = (VkImageFormatProperties) {
1903       .maxExtent = { 0, 0, 0 },
1904       .maxMipLevels = 0,
1905       .maxArrayLayers = 0,
1906       .sampleCounts = 0,
1907       .maxResourceSize = 0,
1908    };
1909 
1910    return VK_ERROR_FORMAT_NOT_SUPPORTED;
1911 }
1912 
anv_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceImageFormatInfo2 * pImageFormatInfo,VkImageFormatProperties2 * pImageFormatProperties)1913 VkResult anv_GetPhysicalDeviceImageFormatProperties2(
1914     VkPhysicalDevice                            physicalDevice,
1915     const VkPhysicalDeviceImageFormatInfo2*     pImageFormatInfo,
1916     VkImageFormatProperties2*                   pImageFormatProperties)
1917 {
1918    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1919 
1920    return anv_get_image_format_properties(physical_device,
1921                                           pImageFormatInfo,
1922                                           pImageFormatProperties);
1923 }
1924 
anv_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSparseImageFormatInfo2 * pFormatInfo,uint32_t * pPropertyCount,VkSparseImageFormatProperties2 * pProperties)1925 void anv_GetPhysicalDeviceSparseImageFormatProperties2(
1926     VkPhysicalDevice                            physicalDevice,
1927     const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1928     uint32_t*                                   pPropertyCount,
1929     VkSparseImageFormatProperties2*             pProperties)
1930 {
1931    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1932    VkImageAspectFlags aspects = vk_format_aspects(pFormatInfo->format);
1933    VK_OUTARRAY_MAKE_TYPED(VkSparseImageFormatProperties2, props,
1934                           pProperties, pPropertyCount);
1935 
1936    if (physical_device->sparse_type == ANV_SPARSE_TYPE_NOT_SUPPORTED) {
1937       if (INTEL_DEBUG(DEBUG_SPARSE))
1938          fprintf(stderr, "=== [%s:%d] [%s]\n", __FILE__, __LINE__, __func__);
1939       return;
1940    }
1941 
1942    vk_foreach_struct_const(ext, pFormatInfo->pNext)
1943       vk_debug_ignored_stype(ext->sType);
1944 
1945    /* Check if the image is supported at all (regardless of being Sparse). */
1946    const VkPhysicalDeviceImageFormatInfo2 img_info = {
1947       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1948       .pNext = NULL,
1949       .format = pFormatInfo->format,
1950       .type = pFormatInfo->type,
1951       .tiling = pFormatInfo->tiling,
1952       .usage = pFormatInfo->usage,
1953       .flags = VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
1954                VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT,
1955    };
1956    VkImageFormatProperties2 img_props = {};
1957    if (anv_get_image_format_properties(physical_device,
1958                                        &img_info, &img_props) != VK_SUCCESS)
1959       return;
1960 
1961    if ((pFormatInfo->samples &
1962         img_props.imageFormatProperties.sampleCounts) == 0)
1963       return;
1964 
1965    if (anv_sparse_image_check_support(physical_device,
1966                                       VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
1967                                       VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT,
1968                                       pFormatInfo->tiling,
1969                                       pFormatInfo->samples,
1970                                       pFormatInfo->type,
1971                                       pFormatInfo->format) != VK_SUCCESS) {
1972       return;
1973    }
1974 
1975    VkExtent3D ds_granularity = {};
1976    VkSparseImageFormatProperties2 *ds_props_ptr = NULL;
1977 
1978    u_foreach_bit(b, aspects) {
1979       VkImageAspectFlagBits aspect = 1 << b;
1980 
1981       const uint32_t plane =
1982          anv_aspect_to_plane(vk_format_aspects(pFormatInfo->format), aspect);
1983       struct anv_format_plane anv_format_plane =
1984          anv_get_format_plane(physical_device, pFormatInfo->format, plane,
1985                               pFormatInfo->tiling);
1986       enum isl_format isl_format = anv_format_plane.isl_format;
1987       assert(isl_format != ISL_FORMAT_UNSUPPORTED);
1988 
1989       VkImageCreateFlags vk_create_flags =
1990          VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
1991          VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT;
1992 
1993       isl_surf_usage_flags_t isl_usage =
1994          anv_image_choose_isl_surf_usage(physical_device,
1995                                          vk_create_flags, pFormatInfo->usage,
1996                                          0, aspect,
1997                                          VK_IMAGE_COMPRESSION_DEFAULT_EXT);
1998 
1999       const enum isl_surf_dim isl_surf_dim =
2000          pFormatInfo->type == VK_IMAGE_TYPE_1D ? ISL_SURF_DIM_1D :
2001          pFormatInfo->type == VK_IMAGE_TYPE_2D ? ISL_SURF_DIM_2D :
2002          ISL_SURF_DIM_3D;
2003 
2004       struct isl_surf isl_surf;
2005       bool ok = isl_surf_init(&physical_device->isl_dev, &isl_surf,
2006                   .dim = isl_surf_dim,
2007                   .format = isl_format,
2008                   .width = 1,
2009                   .height = 1,
2010                   .depth = 1,
2011                   .levels = 1,
2012                   .array_len = 1,
2013                   .samples = pFormatInfo->samples,
2014                   .min_alignment_B = 0,
2015                   .row_pitch_B = 0,
2016                   .usage = isl_usage,
2017                   .tiling_flags = ISL_TILING_ANY_MASK);
2018       if (!ok) {
2019          /* There's no way to return an error code! */
2020          assert(false);
2021          *pPropertyCount = 0;
2022          return;
2023       }
2024 
2025       VkSparseImageFormatProperties format_props =
2026          anv_sparse_calc_image_format_properties(physical_device, aspect,
2027                                                  pFormatInfo->type,
2028                                                  pFormatInfo->samples,
2029                                                  &isl_surf);
2030 
2031       /* If both depth and stencil are the same, unify them if possible. */
2032       if (aspect & (VK_IMAGE_ASPECT_DEPTH_BIT |
2033                     VK_IMAGE_ASPECT_STENCIL_BIT)) {
2034          if (!ds_props_ptr) {
2035             ds_granularity = format_props.imageGranularity;
2036          } else if (ds_granularity.width ==
2037                     format_props.imageGranularity.width &&
2038                     ds_granularity.height ==
2039                     format_props.imageGranularity.height &&
2040                     ds_granularity.depth ==
2041                     format_props.imageGranularity.depth) {
2042             ds_props_ptr->properties.aspectMask |= aspect;
2043             continue;
2044          }
2045       }
2046 
2047       vk_outarray_append_typed(VkSparseImageFormatProperties2, &props, p) {
2048          p->properties = format_props;
2049          if (aspect & (VK_IMAGE_ASPECT_DEPTH_BIT |
2050                        VK_IMAGE_ASPECT_STENCIL_BIT))
2051             ds_props_ptr = p;
2052       }
2053    }
2054 }
2055 
anv_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalBufferInfo * pExternalBufferInfo,VkExternalBufferProperties * pExternalBufferProperties)2056 void anv_GetPhysicalDeviceExternalBufferProperties(
2057     VkPhysicalDevice                             physicalDevice,
2058     const VkPhysicalDeviceExternalBufferInfo*    pExternalBufferInfo,
2059     VkExternalBufferProperties*                  pExternalBufferProperties)
2060 {
2061    /* The Vulkan 1.0.42 spec says "handleType must be a valid
2062     * VkExternalMemoryHandleTypeFlagBits value" in
2063     * VkPhysicalDeviceExternalBufferInfo. This differs from
2064     * VkPhysicalDeviceExternalImageFormatInfo, which surprisingly permits
2065     * handleType == 0.
2066     */
2067    assert(pExternalBufferInfo->handleType != 0);
2068 
2069    /* All of the current flags are for sparse which we don't support yet.
2070     * Even when we do support it, doing sparse on external memory sounds
2071     * sketchy.  Also, just disallowing flags is the safe option.
2072     */
2073    if (pExternalBufferInfo->flags)
2074       goto unsupported;
2075 
2076    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
2077 
2078    switch (pExternalBufferInfo->handleType) {
2079    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
2080    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
2081       pExternalBufferProperties->externalMemoryProperties = opaque_fd_dma_buf_props;
2082       return;
2083    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
2084       pExternalBufferProperties->externalMemoryProperties = userptr_props;
2085       return;
2086    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
2087       if (physical_device->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer) {
2088          pExternalBufferProperties->externalMemoryProperties = android_buffer_props;
2089          return;
2090       }
2091       FALLTHROUGH; /* If ahw not supported */
2092    default:
2093       goto unsupported;
2094    }
2095 
2096  unsupported:
2097    /* From the Vulkan 1.1.113 spec:
2098     *
2099     *    compatibleHandleTypes must include at least handleType.
2100     */
2101    pExternalBufferProperties->externalMemoryProperties =
2102       (VkExternalMemoryProperties) {
2103          .compatibleHandleTypes = pExternalBufferInfo->handleType,
2104       };
2105 }
2106