• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "anv_private.h"
25 #include "drm-uapi/drm_fourcc.h"
26 #include "vk_android.h"
27 #include "vk_enum_defines.h"
28 #include "vk_enum_to_str.h"
29 #include "vk_format.h"
30 #include "vk_util.h"
31 
32 /*
33  * gcc-4 and earlier don't allow compound literals where a constant
34  * is required in -std=c99/gnu99 mode, so we can't use ISL_SWIZZLE()
35  * here. -std=c89/gnu89 would allow it, but we depend on c99 features
36  * so using -std=c89/gnu89 is not an option. Starting from gcc-5
37  * compound literals can also be considered constant in -std=c99/gnu99
38  * mode.
39  */
40 #define _ISL_SWIZZLE(r, g, b, a) { \
41       ISL_CHANNEL_SELECT_##r, \
42       ISL_CHANNEL_SELECT_##g, \
43       ISL_CHANNEL_SELECT_##b, \
44       ISL_CHANNEL_SELECT_##a, \
45 }
46 
47 #define RGBA _ISL_SWIZZLE(RED, GREEN, BLUE, ALPHA)
48 #define BGRA _ISL_SWIZZLE(BLUE, GREEN, RED, ALPHA)
49 #define RGB1 _ISL_SWIZZLE(RED, GREEN, BLUE, ONE)
50 
51 #define swiz_fmt1(__vk_fmt, __hw_fmt, __swizzle) \
52    [VK_ENUM_OFFSET(__vk_fmt)] = { \
53       .planes = { \
54          { .isl_format = __hw_fmt, .swizzle = __swizzle, \
55            .aspect = VK_IMAGE_ASPECT_COLOR_BIT, \
56          }, \
57       }, \
58       .vk_format = __vk_fmt, \
59       .n_planes = 1, \
60    }
61 
62 #define 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 (physical_device->video_decode_enabled &&
584        anv_format->can_video) {
585       flags |= VK_FORMAT_FEATURE_2_VIDEO_DECODE_OUTPUT_BIT_KHR |
586                VK_FORMAT_FEATURE_2_VIDEO_DECODE_DPB_BIT_KHR;
587    }
588 
589    const struct anv_format_plane plane_format =
590       anv_get_format_plane(devinfo, vk_format, 0, vk_tiling);
591 
592    if (plane_format.isl_format == ISL_FORMAT_UNSUPPORTED)
593       return 0;
594 
595    struct anv_format_plane base_plane_format = plane_format;
596    if (vk_tiling != VK_IMAGE_TILING_LINEAR) {
597       base_plane_format = anv_get_format_plane(devinfo, vk_format, 0,
598                                                VK_IMAGE_TILING_LINEAR);
599    }
600 
601    enum isl_format base_isl_format = base_plane_format.isl_format;
602 
603    if (isl_format_supports_sampling(devinfo, plane_format.isl_format)) {
604 
605       /* Unlike other surface formats, our sampler requires that the ASTC
606        * format only be used on surfaces in non-linearly-tiled memory.
607        * Thankfully, we can make an exception for linearly-tiled images that
608        * are only used for transfers. blorp_copy will reinterpret any
609        * compressed format to an uncompressed one.
610        *
611        * We handle modifier tilings further down in this function.
612        */
613       if (vk_tiling == VK_IMAGE_TILING_LINEAR &&
614           isl_format_get_layout(plane_format.isl_format)->txc == ISL_TXC_ASTC)
615          return VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
616                 VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
617 
618       flags |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT |
619                VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
620 
621       if (isl_format_supports_filtering(devinfo, plane_format.isl_format))
622          flags |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
623    }
624 
625    /* We can render to swizzled formats.  However, if the alpha channel is
626     * moved, then blending won't work correctly.  The PRM tells us
627     * straight-up not to render to such a surface.
628     */
629    if (isl_format_supports_rendering(devinfo, plane_format.isl_format) &&
630        plane_format.swizzle.a == ISL_CHANNEL_SELECT_ALPHA) {
631       flags |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT;
632 
633       /* While we can render to swizzled formats, they don't blend correctly
634        * if there are blend constants involved.  The swizzle just remaps the
635        * output of the shader to different channels in the texture.  It
636        * doesn't change the interpretation of the constant blend factors in
637        * COLOR_CALC_STATE.
638        */
639       if (isl_format_supports_alpha_blending(devinfo, plane_format.isl_format) &&
640           isl_swizzle_is_identity(plane_format.swizzle))
641          flags |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT;
642    }
643 
644    /* Load/store is determined based on base format.  This prevents RGB
645     * formats from showing up as load/store capable.
646     */
647    if (isl_format_supports_typed_reads(devinfo, base_isl_format))
648       flags |= VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT;
649    if (isl_format_supports_typed_writes(devinfo, base_isl_format))
650       flags |= VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
651 
652    /* Keep this old behavior on VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT.
653     * When KHR_format_features2 is enabled, applications should only rely on
654     * it for the list of shader storage extended formats [1]. Before that,
655     * this applies to all VkFormats.
656     *
657     * [1] : https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#features-shaderStorageImageExtendedFormats
658     */
659    if (flags & VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT)
660       flags |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT;
661 
662    if (base_isl_format == ISL_FORMAT_R32_SINT ||
663        base_isl_format == ISL_FORMAT_R32_UINT ||
664        base_isl_format == ISL_FORMAT_R32_FLOAT)
665       flags |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_ATOMIC_BIT;
666 
667    if (flags) {
668       flags |= VK_FORMAT_FEATURE_2_BLIT_SRC_BIT |
669                VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
670                VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
671 
672       /* Blit destination requires rendering support. */
673       if (isl_format_supports_rendering(devinfo, plane_format.isl_format))
674          flags |= VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
675    }
676 
677    /* XXX: We handle 3-channel formats by switching them out for RGBX or
678     * RGBA formats behind-the-scenes.  This works fine for textures
679     * because the upload process will fill in the extra channel.
680     * We could also support it for render targets, but it will take
681     * substantially more work and we have enough RGBX formats to handle
682     * what most clients will want.
683     */
684    if (vk_tiling == VK_IMAGE_TILING_OPTIMAL &&
685        base_isl_format != ISL_FORMAT_UNSUPPORTED &&
686        !util_is_power_of_two_or_zero(isl_format_layouts[base_isl_format].bpb) &&
687        isl_format_rgb_to_rgbx(base_isl_format) == ISL_FORMAT_UNSUPPORTED) {
688       flags &= ~VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT;
689       flags &= ~VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
690    }
691 
692    const VkFormatFeatureFlags2 disallowed_ycbcr_image_features =
693       VK_FORMAT_FEATURE_2_BLIT_SRC_BIT |
694       VK_FORMAT_FEATURE_2_BLIT_DST_BIT |
695       VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT |
696       VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT |
697       VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT;
698 
699    if (anv_format->can_ycbcr) {
700       /* The sampler doesn't have support for mid point when it handles YUV on
701        * its own.
702        */
703       if (isl_format_is_yuv(anv_format->planes[0].isl_format)) {
704          /* TODO: We've disabled linear implicit reconstruction with the
705           * sampler. The failures show a slightly out of range values on the
706           * bottom left of the sampled image.
707           */
708          flags |= VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT;
709       } else {
710          flags |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT |
711                   VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT |
712                   VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_YCBCR_CONVERSION_SEPARATE_RECONSTRUCTION_FILTER_BIT;
713       }
714 
715       /* We can support cosited chroma locations when handle planes with our
716        * own shader snippets.
717        */
718       const struct vk_format_ycbcr_info *ycbcr_info =
719          vk_format_get_ycbcr_info(vk_format);
720       assert(anv_format->n_planes == ycbcr_info->n_planes);
721       for (unsigned p = 0; p < ycbcr_info->n_planes; p++) {
722          if (ycbcr_info->planes[p].denominator_scales[0] > 1 ||
723              ycbcr_info->planes[p].denominator_scales[1] > 1) {
724             flags |= VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT;
725             break;
726          }
727       }
728 
729       if (anv_format->n_planes > 1)
730          flags |= VK_FORMAT_FEATURE_2_DISJOINT_BIT;
731 
732       flags &= ~disallowed_ycbcr_image_features;
733    } else if (anv_format->can_video) {
734       /* This format is for video decoding. */
735       flags &= ~disallowed_ycbcr_image_features;
736    }
737 
738    if (vk_tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
739       if (!isl_drm_modifier_get_score(devinfo, isl_mod_info->modifier))
740          return 0;
741 
742       /* Try to restrict the supported formats to those in drm_fourcc.h. The
743        * VK_EXT_image_drm_format_modifier does not require this (after all, two
744        * Vulkan apps could share an image by exchanging its VkFormat instead of
745        * a DRM_FORMAT), but there exist no users of such non-drm_fourcc formats
746        * yet. And the restriction shrinks our test surface.
747        */
748       const struct isl_format_layout *isl_layout =
749          isl_format_get_layout(plane_format.isl_format);
750 
751       switch (isl_layout->colorspace) {
752       case ISL_COLORSPACE_LINEAR:
753       case ISL_COLORSPACE_SRGB:
754          /* Each DRM_FORMAT that we support uses unorm (if the DRM format name
755           * has no type suffix) or sfloat (if it has suffix F). No format
756           * contains mixed types. (as of 2021-06-14)
757           */
758          if (isl_layout->uniform_channel_type != ISL_UNORM &&
759              isl_layout->uniform_channel_type != ISL_SFLOAT)
760             return 0;
761          break;
762       case ISL_COLORSPACE_YUV:
763          anv_finishme("support YUV colorspace with DRM format modifiers");
764          return 0;
765       case ISL_COLORSPACE_NONE:
766          return 0;
767       }
768 
769       /* We could support compressed formats if we wanted to. */
770       if (isl_format_is_compressed(plane_format.isl_format))
771          return 0;
772 
773       /* No non-power-of-two fourcc formats exist.
774        *
775        * Even if non-power-of-two fourcc formats existed, we could support them
776        * only with DRM_FORMAT_MOD_LINEAR.  Tiled formats must be power-of-two
777        * because we implement transfers with the render pipeline.
778        */
779       if (anv_format_has_npot_plane(anv_format))
780          return 0;
781 
782       if (anv_format->n_planes > 1) {
783          /* For simplicity, keep DISJOINT disabled for multi-planar format. */
784          flags &= ~VK_FORMAT_FEATURE_2_DISJOINT_BIT;
785 
786          /* VK_ANDROID_external_memory_android_hardware_buffer in Virtio-GPU
787           * Venus driver layers on top of VK_EXT_image_drm_format_modifier of
788           * the host Vulkan driver, and both VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
789           * and VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM and required to support
790           * camera/media interop in Android.
791           */
792          if (vk_format != VK_FORMAT_G8_B8R8_2PLANE_420_UNORM &&
793              vk_format != VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM) {
794             anv_finishme("support more multi-planar formats with DRM modifiers");
795             return 0;
796          }
797 
798          /* Currently there is no way to properly map memory planes to format
799           * planes and aux planes due to the lack of defined ABI for external
800           * multi-planar images.
801           */
802          if (isl_drm_modifier_has_aux(isl_mod_info->modifier)) {
803             return 0;
804          }
805       }
806 
807       if (isl_drm_modifier_has_aux(isl_mod_info->modifier) &&
808           !anv_format_supports_ccs_e(devinfo, plane_format.isl_format)) {
809          return 0;
810       }
811 
812       if (isl_drm_modifier_has_aux(isl_mod_info->modifier)) {
813          /* Rejection DISJOINT for consistency with the GL driver. In
814           * eglCreateImage, we require that the dma_buf for the primary surface
815           * and the dma_buf for its aux surface refer to the same bo.
816           */
817          flags &= ~VK_FORMAT_FEATURE_2_DISJOINT_BIT;
818 
819          /* When the hardware accesses a storage image, it bypasses the aux
820           * surface. We could support storage access on images with aux
821           * modifiers by resolving the aux surface prior to the storage access.
822           */
823          flags &= ~VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT;
824          flags &= ~VK_FORMAT_FEATURE_2_STORAGE_IMAGE_ATOMIC_BIT;
825       }
826    }
827 
828    if (devinfo->has_coarse_pixel_primitive_and_cb &&
829        vk_format == VK_FORMAT_R8_UINT &&
830        vk_tiling == VK_IMAGE_TILING_OPTIMAL)
831       flags |= VK_FORMAT_FEATURE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR;
832 
833    return flags;
834 }
835 
836 static VkFormatFeatureFlags2
get_buffer_format_features2(const struct intel_device_info * devinfo,VkFormat vk_format,const struct anv_format * anv_format)837 get_buffer_format_features2(const struct intel_device_info *devinfo,
838                             VkFormat vk_format,
839                             const struct anv_format *anv_format)
840 {
841    VkFormatFeatureFlags2 flags = 0;
842 
843    if (anv_format == NULL)
844       return 0;
845 
846    const enum isl_format isl_format = anv_format->planes[0].isl_format;
847 
848    if (isl_format == ISL_FORMAT_UNSUPPORTED)
849       return 0;
850 
851    if (anv_format->n_planes > 1)
852       return 0;
853 
854    if (anv_format->can_ycbcr || anv_format->can_video)
855       return 0;
856 
857    if (vk_format_is_depth_or_stencil(vk_format))
858       return 0;
859 
860    if (isl_format_supports_sampling(devinfo, isl_format) &&
861        !isl_format_is_compressed(isl_format))
862       flags |= VK_FORMAT_FEATURE_2_UNIFORM_TEXEL_BUFFER_BIT;
863 
864    if (isl_format_supports_vertex_fetch(devinfo, isl_format))
865       flags |= VK_FORMAT_FEATURE_2_VERTEX_BUFFER_BIT;
866 
867    if (isl_is_storage_image_format(devinfo, isl_format))
868       flags |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_BIT;
869 
870    if (isl_format == ISL_FORMAT_R32_SINT || isl_format == ISL_FORMAT_R32_UINT)
871       flags |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
872 
873    if (isl_format_supports_typed_reads(devinfo, isl_format))
874       flags |= VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT;
875    if (isl_format_supports_typed_writes(devinfo, isl_format))
876       flags |= VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
877 
878    if (devinfo->has_ray_tracing) {
879       switch (vk_format) {
880       case VK_FORMAT_R32G32_SFLOAT:
881       case VK_FORMAT_R32G32B32_SFLOAT:
882       case VK_FORMAT_R16G16_SFLOAT:
883       case VK_FORMAT_R16G16B16A16_SFLOAT:
884       case VK_FORMAT_R16G16_SNORM:
885       case VK_FORMAT_R16G16B16A16_SNORM:
886       case VK_FORMAT_R16G16B16A16_UNORM:
887       case VK_FORMAT_R16G16_UNORM:
888       case VK_FORMAT_R8G8B8A8_UNORM:
889       case VK_FORMAT_R8G8_UNORM:
890       case VK_FORMAT_R8G8B8A8_SNORM:
891       case VK_FORMAT_R8G8_SNORM:
892          flags |= VK_FORMAT_FEATURE_ACCELERATION_STRUCTURE_VERTEX_BUFFER_BIT_KHR;
893          break;
894       default:
895          break;
896       }
897    }
898 
899    return flags;
900 }
901 
902 static void
get_drm_format_modifier_properties_list(const struct anv_physical_device * physical_device,VkFormat vk_format,VkDrmFormatModifierPropertiesListEXT * list)903 get_drm_format_modifier_properties_list(const struct anv_physical_device *physical_device,
904                                         VkFormat vk_format,
905                                         VkDrmFormatModifierPropertiesListEXT *list)
906 {
907    const struct anv_format *anv_format = anv_get_format(vk_format);
908 
909    VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierPropertiesEXT, out,
910                           list->pDrmFormatModifierProperties,
911                           &list->drmFormatModifierCount);
912 
913    isl_drm_modifier_info_for_each(isl_mod_info) {
914       VkFormatFeatureFlags2 features2 =
915          anv_get_image_format_features2(physical_device, vk_format, anv_format,
916                                         VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
917                                         isl_mod_info);
918       VkFormatFeatureFlags features = vk_format_features2_to_features(features2);
919       if (!features)
920          continue;
921 
922       const uint32_t planes =
923          isl_drm_modifier_get_plane_count(&physical_device->info,
924                                           isl_mod_info->modifier,
925                                           anv_format->n_planes);
926 
927       vk_outarray_append_typed(VkDrmFormatModifierPropertiesEXT, &out, out_props) {
928          *out_props = (VkDrmFormatModifierPropertiesEXT) {
929             .drmFormatModifier = isl_mod_info->modifier,
930             .drmFormatModifierPlaneCount = planes,
931             .drmFormatModifierTilingFeatures = features,
932          };
933       };
934    }
935 }
936 
937 static void
get_drm_format_modifier_properties_list_2(const struct anv_physical_device * physical_device,VkFormat vk_format,VkDrmFormatModifierPropertiesList2EXT * list)938 get_drm_format_modifier_properties_list_2(const struct anv_physical_device *physical_device,
939                                           VkFormat vk_format,
940                                           VkDrmFormatModifierPropertiesList2EXT *list)
941 {
942    const struct anv_format *anv_format = anv_get_format(vk_format);
943 
944    VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierProperties2EXT, out,
945                           list->pDrmFormatModifierProperties,
946                           &list->drmFormatModifierCount);
947 
948    isl_drm_modifier_info_for_each(isl_mod_info) {
949       VkFormatFeatureFlags2 features2 =
950          anv_get_image_format_features2(physical_device, vk_format, anv_format,
951                                         VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
952                                         isl_mod_info);
953       if (!features2)
954          continue;
955 
956       const uint32_t planes =
957          isl_drm_modifier_get_plane_count(&physical_device->info,
958                                           isl_mod_info->modifier,
959                                           anv_format->n_planes);
960 
961       vk_outarray_append_typed(VkDrmFormatModifierProperties2EXT, &out, out_props) {
962          *out_props = (VkDrmFormatModifierProperties2EXT) {
963             .drmFormatModifier = isl_mod_info->modifier,
964             .drmFormatModifierPlaneCount = planes,
965             .drmFormatModifierTilingFeatures = features2,
966          };
967       };
968    }
969 }
970 
anv_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,VkFormat vk_format,VkFormatProperties2 * pFormatProperties)971 void anv_GetPhysicalDeviceFormatProperties2(
972     VkPhysicalDevice                            physicalDevice,
973     VkFormat                                    vk_format,
974     VkFormatProperties2*                        pFormatProperties)
975 {
976    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
977    const struct intel_device_info *devinfo = &physical_device->info;
978    const struct anv_format *anv_format = anv_get_format(vk_format);
979 
980    assert(pFormatProperties->sType == VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2);
981 
982    VkFormatFeatureFlags2 linear2, optimal2, buffer2;
983    linear2 = anv_get_image_format_features2(physical_device, vk_format,
984                                             anv_format,
985                                             VK_IMAGE_TILING_LINEAR, NULL);
986    optimal2 = anv_get_image_format_features2(physical_device, vk_format,
987                                              anv_format,
988                                              VK_IMAGE_TILING_OPTIMAL, NULL);
989    buffer2 = get_buffer_format_features2(devinfo, vk_format, anv_format);
990 
991    pFormatProperties->formatProperties = (VkFormatProperties) {
992       .linearTilingFeatures = vk_format_features2_to_features(linear2),
993       .optimalTilingFeatures = vk_format_features2_to_features(optimal2),
994       .bufferFeatures = vk_format_features2_to_features(buffer2),
995    };
996 
997    vk_foreach_struct(ext, pFormatProperties->pNext) {
998       /* Use unsigned since some cases are not in the VkStructureType enum. */
999       switch ((unsigned)ext->sType) {
1000       case VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT:
1001          get_drm_format_modifier_properties_list(physical_device, vk_format,
1002                                                  (void *)ext);
1003          break;
1004 
1005       case VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_2_EXT:
1006          get_drm_format_modifier_properties_list_2(physical_device, vk_format,
1007                                                    (void *)ext);
1008          break;
1009 
1010       case VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3: {
1011          VkFormatProperties3 *props = (VkFormatProperties3 *)ext;
1012          props->linearTilingFeatures = linear2;
1013          props->optimalTilingFeatures = optimal2;
1014          props->bufferFeatures = buffer2;
1015          break;
1016       }
1017       case VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR:
1018          /* don't have any thing to use this for yet */
1019          break;
1020       default:
1021          anv_debug_ignored_stype(ext->sType);
1022          break;
1023       }
1024    }
1025 }
1026 
1027 static bool
anv_format_supports_usage(VkFormatFeatureFlags2 format_feature_flags,VkImageUsageFlags usage_flags)1028 anv_format_supports_usage(
1029    VkFormatFeatureFlags2 format_feature_flags,
1030    VkImageUsageFlags usage_flags)
1031 {
1032    if (usage_flags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
1033       if (!(format_feature_flags & (VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
1034                                     VK_FORMAT_FEATURE_2_BLIT_SRC_BIT))) {
1035          return false;
1036       }
1037    }
1038 
1039    if (usage_flags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
1040       if (!(format_feature_flags & (VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT |
1041                                     VK_FORMAT_FEATURE_2_BLIT_DST_BIT))) {
1042          return false;
1043       }
1044    }
1045 
1046    if (usage_flags & VK_IMAGE_USAGE_SAMPLED_BIT) {
1047       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT)) {
1048          return false;
1049       }
1050    }
1051 
1052    if (usage_flags & VK_IMAGE_USAGE_STORAGE_BIT) {
1053       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT)) {
1054          return false;
1055       }
1056    }
1057 
1058    if (usage_flags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
1059       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT)) {
1060          return false;
1061       }
1062    }
1063 
1064    if (usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1065       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) {
1066          return false;
1067       }
1068    }
1069 
1070    if (usage_flags & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
1071       /* Nothing to check. */
1072    }
1073 
1074    if (usage_flags & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
1075       /* Ignore this flag because it was removed from the
1076        * provisional_I_20150910 header.
1077        */
1078    }
1079 
1080    return true;
1081 }
1082 
1083 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)1084 anv_formats_are_compatible(
1085    const struct anv_format *img_fmt, const struct anv_format *img_view_fmt,
1086    const struct intel_device_info *devinfo, VkImageTiling tiling,
1087    bool allow_texel_compatible)
1088 {
1089    if (img_view_fmt->vk_format == VK_FORMAT_UNDEFINED)
1090       return false;
1091 
1092    if (img_fmt == img_view_fmt)
1093       return true;
1094 
1095    /* TODO: Handle multi-planar images that can have view of a plane with
1096     * possibly different type.
1097     */
1098    if (img_fmt->n_planes != 1 || img_view_fmt->n_planes != 1)
1099       return false;
1100 
1101    const enum isl_format img_isl_fmt =
1102       anv_get_format_plane(devinfo, img_fmt->vk_format, 0, tiling).isl_format;
1103    const enum isl_format img_view_isl_fmt =
1104       anv_get_format_plane(devinfo, img_view_fmt->vk_format, 0, tiling).isl_format;
1105    if (img_isl_fmt == ISL_FORMAT_UNSUPPORTED ||
1106        img_view_isl_fmt == ISL_FORMAT_UNSUPPORTED)
1107       return false;
1108 
1109    const struct isl_format_layout *img_fmt_layout =
1110          isl_format_get_layout(img_isl_fmt);
1111    const struct isl_format_layout *img_view_fmt_layout =
1112          isl_format_get_layout(img_view_isl_fmt);
1113 
1114    /* From the Vulkan 1.3.230 spec "12.5. Image Views"
1115     *
1116     *    "If image was created with the
1117     *    VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT flag, format must be
1118     *    compatible with the image’s format as described above; or must be
1119     *    an uncompressed format, in which case it must be size-compatible
1120     *    with the image’s format."
1121     */
1122    if (allow_texel_compatible &&
1123        isl_format_is_compressed(img_isl_fmt) &&
1124        !isl_format_is_compressed(img_view_isl_fmt) &&
1125        img_fmt_layout->bpb == img_view_fmt_layout->bpb)
1126       return true;
1127 
1128    if (isl_format_is_compressed(img_isl_fmt) !=
1129        isl_format_is_compressed(img_view_isl_fmt))
1130       return false;
1131 
1132    if (!isl_format_is_compressed(img_isl_fmt)) {
1133       /* From the Vulkan 1.3.224 spec "43.1.6. Format Compatibility Classes":
1134        *
1135        *    "Uncompressed color formats are compatible with each other if they
1136        *    occupy the same number of bits per texel block."
1137        */
1138       return img_fmt_layout->bpb == img_view_fmt_layout->bpb;
1139    }
1140 
1141    /* From the Vulkan 1.3.224 spec "43.1.6. Format Compatibility Classes":
1142     *
1143     *    "Compressed color formats are compatible with each other if the only
1144     *    difference between them is the numerical type of the uncompressed
1145     *    pixels (e.g. signed vs. unsigned, or SRGB vs. UNORM encoding)."
1146     */
1147    return img_fmt_layout->txc == img_view_fmt_layout->txc &&
1148           isl_formats_have_same_bits_per_channel(img_isl_fmt, img_view_isl_fmt);
1149 }
1150 
1151 /* Returns a set of feature flags supported by any of the VkFormat listed in
1152  * format_list_info or any VkFormat compatible with format.
1153  */
1154 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)1155 anv_formats_gather_format_features(
1156    const struct anv_physical_device *physical_device,
1157    const struct anv_format *format,
1158    VkImageTiling tiling,
1159    const struct isl_drm_modifier_info *isl_mod_info,
1160    const VkImageFormatListCreateInfo *format_list_info,
1161    bool allow_texel_compatible)
1162 {
1163    const struct intel_device_info *devinfo = &physical_device->info;
1164    VkFormatFeatureFlags2 all_formats_feature_flags = 0;
1165 
1166    /* We need to check that each of the usage bits are allowed for at least
1167     * one of the potential formats.
1168     */
1169    if (!format_list_info || format_list_info->viewFormatCount == 0) {
1170       /* If we specify no list of possible formats, we need to assume that
1171        * every compatible format is possible and consider the features
1172        * supported by each of them.
1173        */
1174       for (uint32_t fmt_arr_ind = 0;
1175            fmt_arr_ind < ARRAY_SIZE(anv_formats);
1176            ++fmt_arr_ind) {
1177          for (uint32_t fmt_ind = 0;
1178               fmt_ind < anv_formats[fmt_arr_ind].n_formats;
1179               ++fmt_ind) {
1180             const struct anv_format *possible_anv_format =
1181                &(anv_formats[fmt_arr_ind].formats[fmt_ind]);
1182 
1183             if (anv_formats_are_compatible(format, possible_anv_format,
1184                                            devinfo, tiling,
1185                                            allow_texel_compatible)) {
1186                VkFormatFeatureFlags2 view_format_features =
1187                   anv_get_image_format_features2(physical_device,
1188                                                  possible_anv_format->vk_format,
1189                                                  possible_anv_format, tiling,
1190                                                  isl_mod_info);
1191                all_formats_feature_flags |= view_format_features;
1192             }
1193          }
1194       }
1195    } else {
1196       /* If we provide the list of possible formats, then check just them. */
1197       for (uint32_t i = 0; i < format_list_info->viewFormatCount; ++i) {
1198          VkFormat vk_view_format = format_list_info->pViewFormats[i];
1199 
1200          if (vk_view_format == VK_FORMAT_UNDEFINED)
1201             continue;
1202 
1203          const struct anv_format *anv_view_format =
1204             anv_get_format(vk_view_format);
1205          VkFormatFeatureFlags2 view_format_features =
1206             anv_get_image_format_features2(physical_device,
1207                                            vk_view_format, anv_view_format,
1208                                            tiling, isl_mod_info);
1209          all_formats_feature_flags |= view_format_features;
1210       }
1211    }
1212 
1213    return all_formats_feature_flags;
1214 }
1215 
1216 
1217 static VkResult
anv_get_image_format_properties(struct anv_physical_device * physical_device,const VkPhysicalDeviceImageFormatInfo2 * info,VkImageFormatProperties * pImageFormatProperties,VkSamplerYcbcrConversionImageFormatProperties * pYcbcrImageFormatProperties,bool from_wsi)1218 anv_get_image_format_properties(
1219    struct anv_physical_device *physical_device,
1220    const VkPhysicalDeviceImageFormatInfo2 *info,
1221    VkImageFormatProperties *pImageFormatProperties,
1222    VkSamplerYcbcrConversionImageFormatProperties *pYcbcrImageFormatProperties,
1223    bool from_wsi)
1224 {
1225    VkFormatFeatureFlags2 format_feature_flags;
1226    VkExtent3D maxExtent;
1227    uint32_t maxMipLevels;
1228    uint32_t maxArraySize;
1229    VkSampleCountFlags sampleCounts;
1230    const struct intel_device_info *devinfo = &physical_device->info;
1231    const struct anv_format *format = anv_get_format(info->format);
1232    const struct isl_drm_modifier_info *isl_mod_info = NULL;
1233    const VkImageFormatListCreateInfo *format_list_info =
1234       vk_find_struct_const(info->pNext, IMAGE_FORMAT_LIST_CREATE_INFO);
1235 
1236    if (format == NULL)
1237       goto unsupported;
1238 
1239    if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
1240       const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *vk_mod_info =
1241          vk_find_struct_const(info->pNext, PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
1242 
1243       isl_mod_info = isl_drm_modifier_get_info(vk_mod_info->drmFormatModifier);
1244       if (isl_mod_info == NULL)
1245          goto unsupported;
1246 
1247       /* only allow Y-tiling/Tile4 for video decode. */
1248       if (info->usage & VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR) {
1249          if (isl_mod_info->tiling != ISL_TILING_Y0 && isl_mod_info->tiling != ISL_TILING_4)
1250             goto unsupported;
1251       }
1252    }
1253 
1254    assert(format->vk_format == info->format);
1255 
1256    switch (info->type) {
1257    default:
1258       unreachable("bad VkImageType");
1259    case VK_IMAGE_TYPE_1D:
1260       maxExtent.width = 16384;
1261       maxExtent.height = 1;
1262       maxExtent.depth = 1;
1263       maxMipLevels = 15; /* log2(maxWidth) + 1 */
1264       maxArraySize = 2048;
1265       sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1266       break;
1267    case VK_IMAGE_TYPE_2D:
1268       /* FINISHME: Does this really differ for cube maps? The documentation
1269        * for RENDER_SURFACE_STATE suggests so.
1270        */
1271       maxExtent.width = 16384;
1272       maxExtent.height = 16384;
1273       maxExtent.depth = 1;
1274       maxMipLevels = 15; /* log2(maxWidth) + 1 */
1275       maxArraySize = 2048;
1276       sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1277       break;
1278    case VK_IMAGE_TYPE_3D:
1279       maxExtent.width = 2048;
1280       maxExtent.height = 2048;
1281       maxExtent.depth = 2048;
1282       maxMipLevels = 12; /* log2(maxWidth) + 1 */
1283       maxArraySize = 1;
1284       sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1285       break;
1286    }
1287 
1288    /* If any of the format in VkImageFormatListCreateInfo is completely
1289     * unsupported, report unsupported.
1290     */
1291    if ((info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) &&
1292        format_list_info != NULL) {
1293       for (uint32_t i = 0; i < format_list_info->viewFormatCount; i++) {
1294          const struct anv_format *view_format =
1295             anv_get_format(format_list_info->pViewFormats[i]);
1296          if (view_format == NULL)
1297             goto unsupported;
1298       }
1299    }
1300 
1301    /* From the Vulkan 1.3.218 spec:
1302     *
1303     *    "For images created without VK_IMAGE_CREATE_EXTENDED_USAGE_BIT a usage
1304     *    bit is valid if it is supported for the format the image is created with.
1305     *    For images created with VK_IMAGE_CREATE_EXTENDED_USAGE_BIT a usage bit
1306     *    is valid if it is supported for at least one of the formats
1307     *    a VkImageView created from the image can have."
1308     *
1309     *    "VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT specifies that the image can be
1310     *    used to create a VkImageView with a different format from the image."
1311     *
1312     * So, if both VK_IMAGE_CREATE_EXTENDED_USAGE_BIT and
1313     * VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT are set, views can be created with
1314     * different usage than the image, so we can't always filter on usage.
1315     * There is one exception to this below for storage.
1316     */
1317    format_feature_flags = anv_get_image_format_features2(physical_device,
1318                                                          info->format, format,
1319                                                          info->tiling,
1320                                                          isl_mod_info);
1321 
1322    if (!anv_format_supports_usage(format_feature_flags, info->usage)) {
1323       /* If image format itself does not support the usage, and we don't allow
1324        * views formats to support it, then we can't support this usage at all.
1325        */
1326       if (!(info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) ||
1327           !(info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT))
1328          goto unsupported;
1329 
1330       /* We don't want emulated formats to gain unexpected usage (storage in
1331        * particular) from its compatible view formats.
1332        */
1333       if (anv_is_format_emulated(physical_device, info->format))
1334          goto unsupported;
1335 
1336       /* From the Vulkan 1.3.224 spec "43.1.6. Format Compatibility Classes":
1337        *
1338        *    "Each depth/stencil format is only compatible with itself."
1339        *
1340        * So, other formats also can't help.
1341        */
1342       if (vk_format_is_depth_or_stencil(info->format))
1343          goto unsupported;
1344 
1345       /* Gather all possible format feature flags for the formats listed in
1346        * the format list or all the compatible formats.
1347        */
1348       VkFormatFeatureFlags2 all_formats_feature_flags = format_feature_flags |
1349          anv_formats_gather_format_features(physical_device, format,
1350                                             info->tiling, isl_mod_info,
1351                                             format_list_info,
1352                                             info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT);
1353 
1354       if (!anv_format_supports_usage(all_formats_feature_flags, info->usage))
1355          goto unsupported;
1356    }
1357 
1358    if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
1359       /* We support modifiers only for "simple" (that is, non-array
1360        * non-mipmapped single-sample) 2D images.
1361        */
1362       if (info->type != VK_IMAGE_TYPE_2D) {
1363          vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1364                    "VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT "
1365                    "requires VK_IMAGE_TYPE_2D");
1366          goto unsupported;
1367       }
1368 
1369       maxArraySize = 1;
1370       maxMipLevels = 1;
1371       sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1372 
1373       if (isl_drm_modifier_has_aux(isl_mod_info->modifier) &&
1374           !anv_formats_ccs_e_compatible(devinfo, info->flags, info->format,
1375                                         info->tiling, info->usage,
1376                                         format_list_info)) {
1377          goto unsupported;
1378       }
1379    }
1380 
1381    /* Our hardware doesn't support 1D compressed textures.
1382     *    From the SKL PRM, RENDER_SURFACE_STATE::SurfaceFormat:
1383     *    * This field cannot be a compressed (BC*, DXT*, FXT*, ETC*, EAC*) format
1384     *       if the Surface Type is SURFTYPE_1D.
1385     *    * This field cannot be ASTC format if the Surface Type is SURFTYPE_1D.
1386     */
1387    if (info->type == VK_IMAGE_TYPE_1D &&
1388        isl_format_is_compressed(format->planes[0].isl_format)) {
1389        goto unsupported;
1390    }
1391 
1392    if (info->tiling == VK_IMAGE_TILING_OPTIMAL &&
1393        info->type == VK_IMAGE_TYPE_2D &&
1394        (format_feature_flags & (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT |
1395                                 VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
1396        !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
1397        !(info->usage & VK_IMAGE_USAGE_STORAGE_BIT) &&
1398        isl_format_supports_multisampling(devinfo, format->planes[0].isl_format)) {
1399       sampleCounts = isl_device_get_sample_counts(&physical_device->isl_dev);
1400    }
1401 
1402    if (info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1403       /* Non-power-of-two formats can never be used as storage images.  We
1404        * only check plane 0 because there are no YCbCr formats with
1405        * non-power-of-two planes.
1406        */
1407       const struct isl_format_layout *isl_layout =
1408          isl_format_get_layout(format->planes[0].isl_format);
1409       if (!util_is_power_of_two_or_zero(isl_layout->bpb))
1410          goto unsupported;
1411    }
1412 
1413    if (info->flags & VK_IMAGE_CREATE_DISJOINT_BIT) {
1414       /* From the Vulkan 1.2.149 spec, VkImageCreateInfo:
1415        *
1416        *    If format is a multi-planar format, and if imageCreateFormatFeatures
1417        *    (as defined in Image Creation Limits) does not contain
1418        *    VK_FORMAT_FEATURE_2_DISJOINT_BIT, then flags must not contain
1419        *    VK_IMAGE_CREATE_DISJOINT_BIT.
1420        */
1421       if (format->n_planes > 1 &&
1422           !(format_feature_flags & VK_FORMAT_FEATURE_2_DISJOINT_BIT)) {
1423          goto unsupported;
1424       }
1425 
1426       /* From the Vulkan 1.2.149 spec, VkImageCreateInfo:
1427        *
1428        * If format is not a multi-planar format, and flags does not include
1429        * VK_IMAGE_CREATE_ALIAS_BIT, flags must not contain
1430        * VK_IMAGE_CREATE_DISJOINT_BIT.
1431        */
1432       if (format->n_planes == 1 &&
1433           !(info->flags & VK_IMAGE_CREATE_ALIAS_BIT)) {
1434           goto unsupported;
1435       }
1436 
1437       if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT &&
1438           isl_drm_modifier_has_aux(isl_mod_info->modifier)) {
1439          /* Rejection DISJOINT for consistency with the GL driver. In
1440           * eglCreateImage, we require that the dma_buf for the primary surface
1441           * and the dma_buf for its aux surface refer to the same bo.
1442           */
1443          goto unsupported;
1444       }
1445    }
1446 
1447    if ((info->flags & VK_IMAGE_CREATE_ALIAS_BIT) && !from_wsi) {
1448       /* Reject aliasing of images with non-linear DRM format modifiers because:
1449        *
1450        * 1. For modifiers with compression, we store aux tracking state in
1451        *    ANV_IMAGE_MEMORY_BINDING_PRIVATE, which is not aliasable because it's
1452        *    not client-bound.
1453        *
1454        * 2. For tiled modifiers without compression, we may attempt to compress
1455        *    them behind the scenes, in which case both the aux tracking state
1456        *    and the CCS data are bound to ANV_IMAGE_MEMORY_BINDING_PRIVATE.
1457        *
1458        * 3. For WSI we should ignore ALIAS_BIT because we have the ability to
1459        *    bind the ANV_MEMORY_BINDING_PRIVATE from the other WSI image.
1460        */
1461       if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT &&
1462           isl_mod_info->modifier != DRM_FORMAT_MOD_LINEAR) {
1463          goto unsupported;
1464       }
1465    }
1466 
1467    /* From the bspec section entitled "Surface Layout and Tiling",
1468     * Gfx9 has a 256 GB limitation and Gfx11+ has a 16 TB limitation.
1469     */
1470    uint64_t maxResourceSize = 0;
1471    if (devinfo->ver < 11)
1472       maxResourceSize = (uint64_t) 1 << 38;
1473    else
1474       maxResourceSize = (uint64_t) 1 << 44;
1475 
1476    *pImageFormatProperties = (VkImageFormatProperties) {
1477       .maxExtent = maxExtent,
1478       .maxMipLevels = maxMipLevels,
1479       .maxArrayLayers = maxArraySize,
1480       .sampleCounts = sampleCounts,
1481 
1482       /* FINISHME: Accurately calculate
1483        * VkImageFormatProperties::maxResourceSize.
1484        */
1485       .maxResourceSize = maxResourceSize,
1486    };
1487 
1488    if (pYcbcrImageFormatProperties) {
1489       pYcbcrImageFormatProperties->combinedImageSamplerDescriptorCount =
1490          format->n_planes;
1491    }
1492 
1493    return VK_SUCCESS;
1494 
1495 unsupported:
1496    *pImageFormatProperties = (VkImageFormatProperties) {
1497       .maxExtent = { 0, 0, 0 },
1498       .maxMipLevels = 0,
1499       .maxArrayLayers = 0,
1500       .sampleCounts = 0,
1501       .maxResourceSize = 0,
1502    };
1503 
1504    return VK_ERROR_FORMAT_NOT_SUPPORTED;
1505 }
1506 
anv_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,VkImageTiling tiling,VkImageUsageFlags usage,VkImageCreateFlags createFlags,VkImageFormatProperties * pImageFormatProperties)1507 VkResult anv_GetPhysicalDeviceImageFormatProperties(
1508     VkPhysicalDevice                            physicalDevice,
1509     VkFormat                                    format,
1510     VkImageType                                 type,
1511     VkImageTiling                               tiling,
1512     VkImageUsageFlags                           usage,
1513     VkImageCreateFlags                          createFlags,
1514     VkImageFormatProperties*                    pImageFormatProperties)
1515 {
1516    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1517 
1518    const VkPhysicalDeviceImageFormatInfo2 info = {
1519       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1520       .pNext = NULL,
1521       .format = format,
1522       .type = type,
1523       .tiling = tiling,
1524       .usage = usage,
1525       .flags = createFlags,
1526    };
1527 
1528    return anv_get_image_format_properties(physical_device, &info,
1529                                           pImageFormatProperties, NULL, false);
1530 }
1531 
1532 
1533 /* Supports opaque fd but not dma_buf. */
1534 static const VkExternalMemoryProperties opaque_fd_only_props = {
1535    .externalMemoryFeatures =
1536       VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1537       VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1538    .exportFromImportedHandleTypes =
1539       VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
1540    .compatibleHandleTypes =
1541       VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
1542 };
1543 
1544 /* Supports opaque fd and dma_buf. */
1545 static const VkExternalMemoryProperties opaque_fd_dma_buf_props = {
1546    .externalMemoryFeatures =
1547       VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1548       VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1549    .exportFromImportedHandleTypes =
1550       VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1551       VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
1552    .compatibleHandleTypes =
1553       VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1554       VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
1555 };
1556 
1557 static const VkExternalMemoryProperties userptr_props = {
1558    .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1559    .exportFromImportedHandleTypes = 0,
1560    .compatibleHandleTypes =
1561       VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,
1562 };
1563 
1564 static const VkExternalMemoryProperties android_buffer_props = {
1565    .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1566                              VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1567    .exportFromImportedHandleTypes =
1568       VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1569    .compatibleHandleTypes =
1570       VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1571 };
1572 
1573 
1574 static const VkExternalMemoryProperties android_image_props = {
1575    /* VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT will be set dynamically */
1576    .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT |
1577                              VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT,
1578    .exportFromImportedHandleTypes =
1579       VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1580    .compatibleHandleTypes =
1581       VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1582 };
1583 
anv_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceImageFormatInfo2 * base_info,VkImageFormatProperties2 * base_props)1584 VkResult anv_GetPhysicalDeviceImageFormatProperties2(
1585     VkPhysicalDevice                            physicalDevice,
1586     const VkPhysicalDeviceImageFormatInfo2*     base_info,
1587     VkImageFormatProperties2*                   base_props)
1588 {
1589    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1590    const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
1591    VkExternalImageFormatProperties *external_props = NULL;
1592    VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;
1593    UNUSED VkAndroidHardwareBufferUsageANDROID *android_usage = NULL;
1594    VkTextureLODGatherFormatPropertiesAMD *texture_lod_gather_props = NULL;
1595    VkResult result;
1596    bool from_wsi = false;
1597 
1598    /* Extract input structs */
1599    vk_foreach_struct_const(s, base_info->pNext) {
1600       switch ((unsigned)s->sType) {
1601       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
1602          external_info = (const void *) s;
1603          break;
1604       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT:
1605       case VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO:
1606          /* anv_get_image_format_properties will handle these */
1607          break;
1608       case VK_STRUCTURE_TYPE_IMAGE_STENCIL_USAGE_CREATE_INFO:
1609          /* Ignore but don't warn */
1610          break;
1611       case VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA:
1612          from_wsi = true;
1613          break;
1614       case VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR:
1615          /* Ignore but don't warn */
1616          break;
1617       default:
1618          anv_debug_ignored_stype(s->sType);
1619          break;
1620       }
1621    }
1622 
1623    /* Extract output structs */
1624    vk_foreach_struct(s, base_props->pNext) {
1625       switch (s->sType) {
1626       case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
1627          external_props = (void *) s;
1628          break;
1629       case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:
1630          ycbcr_props = (void *) s;
1631          break;
1632       case VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_USAGE_ANDROID:
1633          android_usage = (void *) s;
1634          break;
1635       case VK_STRUCTURE_TYPE_TEXTURE_LOD_GATHER_FORMAT_PROPERTIES_AMD:
1636          texture_lod_gather_props = (void *) s;
1637          break;
1638       default:
1639          anv_debug_ignored_stype(s->sType);
1640          break;
1641       }
1642    }
1643 
1644    result = anv_get_image_format_properties(physical_device, base_info,
1645                &base_props->imageFormatProperties, ycbcr_props, from_wsi);
1646    if (result != VK_SUCCESS)
1647       goto fail;
1648 
1649    bool ahw_supported =
1650       physical_device->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer;
1651 
1652    if (ahw_supported && android_usage) {
1653       android_usage->androidHardwareBufferUsage =
1654          vk_image_usage_to_ahb_usage(base_info->flags, base_info->usage);
1655 
1656       /* Limit maxArrayLayers to 1 for AHardwareBuffer based images for now. */
1657       base_props->imageFormatProperties.maxArrayLayers = 1;
1658    }
1659 
1660    /* From the Vulkan 1.0.42 spec:
1661     *
1662     *    If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
1663     *    behave as if VkPhysicalDeviceExternalImageFormatInfo was not
1664     *    present and VkExternalImageFormatProperties will be ignored.
1665     */
1666    if (external_info && external_info->handleType != 0) {
1667       /* Does there exist a method for app and driver to explicitly communicate
1668        * to each other the image's memory layout?
1669        */
1670       bool tiling_has_explicit_layout;
1671 
1672       switch (base_info->tiling) {
1673       default:
1674          unreachable("bad VkImageTiling");
1675       case VK_IMAGE_TILING_LINEAR:
1676          /* The app can query the image's memory layout with
1677           * vkGetImageSubresourceLayout.
1678           */
1679          tiling_has_explicit_layout = true;
1680          break;
1681       case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT:
1682          /* The app can provide the image's memory layout with
1683           * VkImageDrmFormatModifierExplicitCreateInfoEXT;
1684           * or the app can query it with vkGetImageSubresourceLayout.
1685           */
1686          tiling_has_explicit_layout = true;
1687          break;
1688       case VK_IMAGE_TILING_OPTIMAL:
1689          /* The app can neither query nor provide the image's memory layout. */
1690          tiling_has_explicit_layout = false;
1691          break;
1692       }
1693 
1694       /* Compatibility between tiling and external memory handles
1695        * --------------------------------------------------------
1696        * When importing or exporting an image, there must exist a method that
1697        * enables the app and driver to agree on the image's memory layout. If no
1698        * method exists, then we reject image creation here.
1699        *
1700        * If the memory handle requires matching
1701        * VkPhysicalDeviceIDProperties::driverUUID and ::deviceUUID, then the
1702        * match-requirement guarantees that all users of the image agree on the
1703        * image's memory layout.
1704        *
1705        * If the memory handle does not require matching
1706        * VkPhysicalDeviceIDProperties::driverUUID nor ::deviceUUID, then we
1707        * require that the app and driver be able to explicitly communicate to
1708        * each other the image's memory layout.
1709        *
1710        * (For restrictions on driverUUID and deviceUUID, see the Vulkan 1.2.149
1711        * spec, Table 73 "External memory handle types").
1712        */
1713       switch (external_info->handleType) {
1714       case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1715          if (external_props) {
1716             if (tiling_has_explicit_layout) {
1717                /* With an explicit memory layout, we don't care which type of fd
1718                 * the image belongs too. Both OPAQUE_FD and DMA_BUF are
1719                 * interchangeable here.
1720                 */
1721                external_props->externalMemoryProperties = opaque_fd_dma_buf_props;
1722             } else {
1723                /* With an implicit memory layout, we must rely on deviceUUID
1724                 * and driverUUID to determine the layout. Therefore DMA_BUF is
1725                 * incompatible here.
1726                 */
1727                external_props->externalMemoryProperties = opaque_fd_only_props;
1728             }
1729          }
1730          break;
1731       case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1732          /* This memory handle has no restrictions on driverUUID nor deviceUUID,
1733           * and therefore requires explicit memory layout.
1734           */
1735          if (!tiling_has_explicit_layout) {
1736             result = vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1737                                "VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT "
1738                                "requires VK_IMAGE_TILING_LINEAR or "
1739                                "VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT");
1740             goto fail;
1741          }
1742 
1743          /* With an explicit memory layout, we don't care which type of fd
1744           * the image belongs too. Both OPAQUE_FD and DMA_BUF are
1745           * interchangeable here.
1746           */
1747          if (external_props)
1748             external_props->externalMemoryProperties = opaque_fd_dma_buf_props;
1749          break;
1750       case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1751          /* This memory handle has no restrictions on driverUUID nor deviceUUID,
1752           * and therefore requires explicit memory layout.
1753           */
1754          if (!tiling_has_explicit_layout) {
1755             result = vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1756                                "VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT "
1757                                "requires VK_IMAGE_TILING_LINEAR or "
1758                                "VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT");
1759             goto fail;
1760          }
1761 
1762          if (external_props)
1763             external_props->externalMemoryProperties = userptr_props;
1764          break;
1765       case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
1766          /* This memory handle is magic. The Vulkan spec says it has no
1767           * requirements regarding deviceUUID nor driverUUID, but Android still
1768           * requires support for VK_IMAGE_TILING_OPTIMAL. Android systems
1769           * communicate the image's memory layout through backdoor channels.
1770           */
1771          if (ahw_supported) {
1772             if (external_props) {
1773                external_props->externalMemoryProperties = android_image_props;
1774                if (anv_ahb_format_for_vk_format(base_info->format)) {
1775                   external_props->externalMemoryProperties.externalMemoryFeatures |=
1776                      VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT;
1777                }
1778             }
1779             break;
1780          }
1781          FALLTHROUGH; /* If ahw not supported */
1782       default:
1783          /* From the Vulkan 1.0.42 spec:
1784           *
1785           *    If handleType is not compatible with the [parameters] specified
1786           *    in VkPhysicalDeviceImageFormatInfo2, then
1787           *    vkGetPhysicalDeviceImageFormatProperties2 returns
1788           *    VK_ERROR_FORMAT_NOT_SUPPORTED.
1789           */
1790          result = vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1791                             "unsupported VkExternalMemoryTypeFlagBits 0x%x",
1792                             external_info->handleType);
1793          goto fail;
1794       }
1795    }
1796 
1797    if (texture_lod_gather_props) {
1798       texture_lod_gather_props->supportsTextureGatherLODBiasAMD =
1799          physical_device->info.ver >= 20;
1800    }
1801 
1802    return VK_SUCCESS;
1803 
1804  fail:
1805    if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
1806       /* From the Vulkan 1.0.42 spec:
1807        *
1808        *    If the combination of parameters to
1809        *    vkGetPhysicalDeviceImageFormatProperties2 is not supported by
1810        *    the implementation for use in vkCreateImage, then all members of
1811        *    imageFormatProperties will be filled with zero.
1812        */
1813       base_props->imageFormatProperties = (VkImageFormatProperties) {};
1814    }
1815 
1816    return result;
1817 }
1818 
anv_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSparseImageFormatInfo2 * pFormatInfo,uint32_t * pPropertyCount,VkSparseImageFormatProperties2 * pProperties)1819 void anv_GetPhysicalDeviceSparseImageFormatProperties2(
1820     VkPhysicalDevice                            physicalDevice,
1821     const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1822     uint32_t*                                   pPropertyCount,
1823     VkSparseImageFormatProperties2*             pProperties)
1824 {
1825    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1826    const struct intel_device_info *devinfo = &physical_device->info;
1827    VkImageAspectFlags aspects = vk_format_aspects(pFormatInfo->format);
1828    VK_OUTARRAY_MAKE_TYPED(VkSparseImageFormatProperties2, props,
1829                           pProperties, pPropertyCount);
1830 
1831    if (!physical_device->has_sparse) {
1832       if (INTEL_DEBUG(DEBUG_SPARSE))
1833          fprintf(stderr, "=== [%s:%d] [%s]\n", __FILE__, __LINE__, __func__);
1834       return;
1835    }
1836 
1837    vk_foreach_struct_const(ext, pFormatInfo->pNext)
1838       anv_debug_ignored_stype(ext->sType);
1839 
1840    /* Check if the image is supported at all (regardless of being Sparse). */
1841    const VkPhysicalDeviceImageFormatInfo2 img_info = {
1842       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1843       .pNext = NULL,
1844       .format = pFormatInfo->format,
1845       .type = pFormatInfo->type,
1846       .tiling = pFormatInfo->tiling,
1847       .usage = pFormatInfo->usage,
1848       .flags = VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
1849                VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT,
1850    };
1851    VkImageFormatProperties img_props;
1852    if (anv_get_image_format_properties(physical_device, &img_info,
1853                                        &img_props, NULL, false) != VK_SUCCESS)
1854       return;
1855 
1856    if (anv_sparse_image_check_support(physical_device,
1857                                       VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
1858                                       VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT,
1859                                       pFormatInfo->tiling,
1860                                       pFormatInfo->samples,
1861                                       pFormatInfo->type,
1862                                       pFormatInfo->format) != VK_SUCCESS) {
1863       return;
1864    }
1865 
1866    VkExtent3D ds_granularity = {};
1867    VkSparseImageFormatProperties2 *ds_props_ptr = NULL;
1868 
1869    u_foreach_bit(b, aspects) {
1870       VkImageAspectFlagBits aspect = 1 << b;
1871 
1872       const uint32_t plane =
1873          anv_aspect_to_plane(vk_format_aspects(pFormatInfo->format), aspect);
1874       struct anv_format_plane anv_format_plane =
1875          anv_get_format_plane(devinfo, pFormatInfo->format, plane,
1876                               pFormatInfo->tiling);
1877       enum isl_format isl_format = anv_format_plane.isl_format;
1878       assert(isl_format != ISL_FORMAT_UNSUPPORTED);
1879 
1880       VkImageCreateFlags vk_create_flags =
1881          VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
1882          VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT;
1883 
1884       isl_surf_usage_flags_t isl_usage =
1885          anv_image_choose_isl_surf_usage(physical_device,
1886                                          vk_create_flags, pFormatInfo->usage,
1887                                          0, aspect);
1888 
1889       const enum isl_surf_dim isl_surf_dim =
1890          pFormatInfo->type == VK_IMAGE_TYPE_1D ? ISL_SURF_DIM_1D :
1891          pFormatInfo->type == VK_IMAGE_TYPE_2D ? ISL_SURF_DIM_2D :
1892          ISL_SURF_DIM_3D;
1893 
1894       struct isl_surf isl_surf;
1895       bool ok = isl_surf_init(&physical_device->isl_dev, &isl_surf,
1896                   .dim = isl_surf_dim,
1897                   .format = isl_format,
1898                   .width = 1,
1899                   .height = 1,
1900                   .depth = 1,
1901                   .levels = 1,
1902                   .array_len = 1,
1903                   .samples = pFormatInfo->samples,
1904                   .min_alignment_B = 0,
1905                   .row_pitch_B = 0,
1906                   .usage = isl_usage,
1907                   .tiling_flags = ISL_TILING_ANY_MASK);
1908       if (!ok) {
1909          /* There's no way to return an error code! */
1910          assert(false);
1911          *pPropertyCount = 0;
1912          return;
1913       }
1914 
1915       VkSparseImageFormatProperties format_props =
1916          anv_sparse_calc_image_format_properties(physical_device, aspect,
1917                                                  pFormatInfo->type,
1918                                                  &isl_surf);
1919 
1920       /* If both depth and stencil are the same, unify them if possible. */
1921       if (aspect & (VK_IMAGE_ASPECT_DEPTH_BIT |
1922                     VK_IMAGE_ASPECT_STENCIL_BIT)) {
1923          if (!ds_props_ptr) {
1924             ds_granularity = format_props.imageGranularity;
1925          } else if (ds_granularity.width ==
1926                     format_props.imageGranularity.width &&
1927                     ds_granularity.height ==
1928                     format_props.imageGranularity.height &&
1929                     ds_granularity.depth ==
1930                     format_props.imageGranularity.depth) {
1931             ds_props_ptr->properties.aspectMask |= aspect;
1932             continue;
1933          }
1934       }
1935 
1936       vk_outarray_append_typed(VkSparseImageFormatProperties2, &props, p) {
1937          p->properties = format_props;
1938          if (aspect & (VK_IMAGE_ASPECT_DEPTH_BIT |
1939                        VK_IMAGE_ASPECT_STENCIL_BIT))
1940             ds_props_ptr = p;
1941       }
1942    }
1943 }
1944 
anv_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalBufferInfo * pExternalBufferInfo,VkExternalBufferProperties * pExternalBufferProperties)1945 void anv_GetPhysicalDeviceExternalBufferProperties(
1946     VkPhysicalDevice                             physicalDevice,
1947     const VkPhysicalDeviceExternalBufferInfo*    pExternalBufferInfo,
1948     VkExternalBufferProperties*                  pExternalBufferProperties)
1949 {
1950    /* The Vulkan 1.0.42 spec says "handleType must be a valid
1951     * VkExternalMemoryHandleTypeFlagBits value" in
1952     * VkPhysicalDeviceExternalBufferInfo. This differs from
1953     * VkPhysicalDeviceExternalImageFormatInfo, which surprisingly permits
1954     * handleType == 0.
1955     */
1956    assert(pExternalBufferInfo->handleType != 0);
1957 
1958    /* All of the current flags are for sparse which we don't support yet.
1959     * Even when we do support it, doing sparse on external memory sounds
1960     * sketchy.  Also, just disallowing flags is the safe option.
1961     */
1962    if (pExternalBufferInfo->flags)
1963       goto unsupported;
1964 
1965    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1966 
1967    switch (pExternalBufferInfo->handleType) {
1968    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1969    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1970       pExternalBufferProperties->externalMemoryProperties = opaque_fd_dma_buf_props;
1971       return;
1972    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1973       pExternalBufferProperties->externalMemoryProperties = userptr_props;
1974       return;
1975    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
1976       if (physical_device->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer) {
1977          pExternalBufferProperties->externalMemoryProperties = android_buffer_props;
1978          return;
1979       }
1980       FALLTHROUGH; /* If ahw not supported */
1981    default:
1982       goto unsupported;
1983    }
1984 
1985  unsupported:
1986    /* From the Vulkan 1.1.113 spec:
1987     *
1988     *    compatibleHandleTypes must include at least handleType.
1989     */
1990    pExternalBufferProperties->externalMemoryProperties =
1991       (VkExternalMemoryProperties) {
1992          .compatibleHandleTypes = pExternalBufferInfo->handleType,
1993       };
1994 }
1995