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