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