anv: Advertise support for VK_EXT_shader_atomic_float
[mesa.git] / src / intel / vulkan / anv_formats.c
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_enum_to_str.h"
27 #include "vk_format_info.h"
28 #include "vk_util.h"
29
30 /*
31 * gcc-4 and earlier don't allow compound literals where a constant
32 * is required in -std=c99/gnu99 mode, so we can't use ISL_SWIZZLE()
33 * here. -std=c89/gnu89 would allow it, but we depend on c99 features
34 * so using -std=c89/gnu89 is not an option. Starting from gcc-5
35 * compound literals can also be considered constant in -std=c99/gnu99
36 * mode.
37 */
38 #define _ISL_SWIZZLE(r, g, b, a) { \
39 ISL_CHANNEL_SELECT_##r, \
40 ISL_CHANNEL_SELECT_##g, \
41 ISL_CHANNEL_SELECT_##b, \
42 ISL_CHANNEL_SELECT_##a, \
43 }
44
45 #define RGBA _ISL_SWIZZLE(RED, GREEN, BLUE, ALPHA)
46 #define BGRA _ISL_SWIZZLE(BLUE, GREEN, RED, ALPHA)
47 #define RGB1 _ISL_SWIZZLE(RED, GREEN, BLUE, ONE)
48
49 #define swiz_fmt1(__vk_fmt, __hw_fmt, __swizzle) \
50 [VK_ENUM_OFFSET(__vk_fmt)] = { \
51 .planes = { \
52 { .isl_format = __hw_fmt, .swizzle = __swizzle, \
53 .denominator_scales = { 1, 1, }, \
54 .aspect = VK_IMAGE_ASPECT_COLOR_BIT, \
55 }, \
56 }, \
57 .vk_format = __vk_fmt, \
58 .n_planes = 1, \
59 }
60
61 #define fmt1(__vk_fmt, __hw_fmt) \
62 swiz_fmt1(__vk_fmt, __hw_fmt, RGBA)
63
64 #define d_fmt(__vk_fmt, __hw_fmt) \
65 [VK_ENUM_OFFSET(__vk_fmt)] = { \
66 .planes = { \
67 { .isl_format = __hw_fmt, .swizzle = RGBA, \
68 .denominator_scales = { 1, 1, }, \
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 .denominator_scales = { 1, 1, }, \
81 .aspect = VK_IMAGE_ASPECT_STENCIL_BIT, \
82 }, \
83 }, \
84 .vk_format = __vk_fmt, \
85 .n_planes = 1, \
86 }
87
88 #define ds_fmt2(__vk_fmt, __fmt1, __fmt2) \
89 [VK_ENUM_OFFSET(__vk_fmt)] = { \
90 .planes = { \
91 { .isl_format = __fmt1, .swizzle = RGBA, \
92 .denominator_scales = { 1, 1, }, \
93 .aspect = VK_IMAGE_ASPECT_DEPTH_BIT, \
94 }, \
95 { .isl_format = __fmt2, .swizzle = RGBA, \
96 .denominator_scales = { 1, 1, }, \
97 .aspect = VK_IMAGE_ASPECT_STENCIL_BIT, \
98 }, \
99 }, \
100 .vk_format = __vk_fmt, \
101 .n_planes = 2, \
102 }
103
104 #define fmt_unsupported(__vk_fmt) \
105 [VK_ENUM_OFFSET(__vk_fmt)] = { \
106 .planes = { \
107 { .isl_format = ISL_FORMAT_UNSUPPORTED, }, \
108 }, \
109 .vk_format = VK_FORMAT_UNDEFINED, \
110 }
111
112 #define y_plane(__plane, __hw_fmt, __swizzle, __ycbcr_swizzle, dhs, dvs) \
113 { .isl_format = __hw_fmt, \
114 .swizzle = __swizzle, \
115 .ycbcr_swizzle = __ycbcr_swizzle, \
116 .denominator_scales = { dhs, dvs, }, \
117 .has_chroma = false, \
118 .aspect = VK_IMAGE_ASPECT_PLANE_0_BIT, /* Y plane is always plane 0 */ \
119 }
120
121 #define chroma_plane(__plane, __hw_fmt, __swizzle, __ycbcr_swizzle, dhs, dvs) \
122 { .isl_format = __hw_fmt, \
123 .swizzle = __swizzle, \
124 .ycbcr_swizzle = __ycbcr_swizzle, \
125 .denominator_scales = { dhs, dvs, }, \
126 .has_chroma = true, \
127 .aspect = VK_IMAGE_ASPECT_PLANE_ ## __plane ## _BIT, \
128 }
129
130 #define ycbcr_fmt(__vk_fmt, __n_planes, ...) \
131 [VK_ENUM_OFFSET(__vk_fmt)] = { \
132 .planes = { \
133 __VA_ARGS__, \
134 }, \
135 .vk_format = __vk_fmt, \
136 .n_planes = __n_planes, \
137 .can_ycbcr = true, \
138 }
139
140 /* HINT: For array formats, the ISL name should match the VK name. For
141 * packed formats, they should have the channels in reverse order from each
142 * other. The reason for this is that, for packed formats, the ISL (and
143 * bspec) names are in LSB -> MSB order while VK formats are MSB -> LSB.
144 */
145 static const struct anv_format main_formats[] = {
146 fmt_unsupported(VK_FORMAT_UNDEFINED),
147 fmt_unsupported(VK_FORMAT_R4G4_UNORM_PACK8),
148 fmt1(VK_FORMAT_R4G4B4A4_UNORM_PACK16, ISL_FORMAT_A4B4G4R4_UNORM),
149 swiz_fmt1(VK_FORMAT_B4G4R4A4_UNORM_PACK16, ISL_FORMAT_A4B4G4R4_UNORM, BGRA),
150 fmt1(VK_FORMAT_R5G6B5_UNORM_PACK16, ISL_FORMAT_B5G6R5_UNORM),
151 fmt_unsupported(VK_FORMAT_B5G6R5_UNORM_PACK16),
152 fmt1(VK_FORMAT_R5G5B5A1_UNORM_PACK16, ISL_FORMAT_A1B5G5R5_UNORM),
153 fmt_unsupported(VK_FORMAT_B5G5R5A1_UNORM_PACK16),
154 fmt1(VK_FORMAT_A1R5G5B5_UNORM_PACK16, ISL_FORMAT_B5G5R5A1_UNORM),
155 fmt1(VK_FORMAT_R8_UNORM, ISL_FORMAT_R8_UNORM),
156 fmt1(VK_FORMAT_R8_SNORM, ISL_FORMAT_R8_SNORM),
157 fmt1(VK_FORMAT_R8_USCALED, ISL_FORMAT_R8_USCALED),
158 fmt1(VK_FORMAT_R8_SSCALED, ISL_FORMAT_R8_SSCALED),
159 fmt1(VK_FORMAT_R8_UINT, ISL_FORMAT_R8_UINT),
160 fmt1(VK_FORMAT_R8_SINT, ISL_FORMAT_R8_SINT),
161 swiz_fmt1(VK_FORMAT_R8_SRGB, ISL_FORMAT_L8_UNORM_SRGB,
162 _ISL_SWIZZLE(RED, ZERO, ZERO, ONE)),
163 fmt1(VK_FORMAT_R8G8_UNORM, ISL_FORMAT_R8G8_UNORM),
164 fmt1(VK_FORMAT_R8G8_SNORM, ISL_FORMAT_R8G8_SNORM),
165 fmt1(VK_FORMAT_R8G8_USCALED, ISL_FORMAT_R8G8_USCALED),
166 fmt1(VK_FORMAT_R8G8_SSCALED, ISL_FORMAT_R8G8_SSCALED),
167 fmt1(VK_FORMAT_R8G8_UINT, ISL_FORMAT_R8G8_UINT),
168 fmt1(VK_FORMAT_R8G8_SINT, ISL_FORMAT_R8G8_SINT),
169 fmt_unsupported(VK_FORMAT_R8G8_SRGB), /* L8A8_UNORM_SRGB */
170 fmt1(VK_FORMAT_R8G8B8_UNORM, ISL_FORMAT_R8G8B8_UNORM),
171 fmt1(VK_FORMAT_R8G8B8_SNORM, ISL_FORMAT_R8G8B8_SNORM),
172 fmt1(VK_FORMAT_R8G8B8_USCALED, ISL_FORMAT_R8G8B8_USCALED),
173 fmt1(VK_FORMAT_R8G8B8_SSCALED, ISL_FORMAT_R8G8B8_SSCALED),
174 fmt1(VK_FORMAT_R8G8B8_UINT, ISL_FORMAT_R8G8B8_UINT),
175 fmt1(VK_FORMAT_R8G8B8_SINT, ISL_FORMAT_R8G8B8_SINT),
176 fmt1(VK_FORMAT_R8G8B8_SRGB, ISL_FORMAT_R8G8B8_UNORM_SRGB),
177 fmt1(VK_FORMAT_R8G8B8A8_UNORM, ISL_FORMAT_R8G8B8A8_UNORM),
178 fmt1(VK_FORMAT_R8G8B8A8_SNORM, ISL_FORMAT_R8G8B8A8_SNORM),
179 fmt1(VK_FORMAT_R8G8B8A8_USCALED, ISL_FORMAT_R8G8B8A8_USCALED),
180 fmt1(VK_FORMAT_R8G8B8A8_SSCALED, ISL_FORMAT_R8G8B8A8_SSCALED),
181 fmt1(VK_FORMAT_R8G8B8A8_UINT, ISL_FORMAT_R8G8B8A8_UINT),
182 fmt1(VK_FORMAT_R8G8B8A8_SINT, ISL_FORMAT_R8G8B8A8_SINT),
183 fmt1(VK_FORMAT_R8G8B8A8_SRGB, ISL_FORMAT_R8G8B8A8_UNORM_SRGB),
184 fmt1(VK_FORMAT_A8B8G8R8_UNORM_PACK32, ISL_FORMAT_R8G8B8A8_UNORM),
185 fmt1(VK_FORMAT_A8B8G8R8_SNORM_PACK32, ISL_FORMAT_R8G8B8A8_SNORM),
186 fmt1(VK_FORMAT_A8B8G8R8_USCALED_PACK32, ISL_FORMAT_R8G8B8A8_USCALED),
187 fmt1(VK_FORMAT_A8B8G8R8_SSCALED_PACK32, ISL_FORMAT_R8G8B8A8_SSCALED),
188 fmt1(VK_FORMAT_A8B8G8R8_UINT_PACK32, ISL_FORMAT_R8G8B8A8_UINT),
189 fmt1(VK_FORMAT_A8B8G8R8_SINT_PACK32, ISL_FORMAT_R8G8B8A8_SINT),
190 fmt1(VK_FORMAT_A8B8G8R8_SRGB_PACK32, ISL_FORMAT_R8G8B8A8_UNORM_SRGB),
191 fmt1(VK_FORMAT_A2R10G10B10_UNORM_PACK32, ISL_FORMAT_B10G10R10A2_UNORM),
192 fmt1(VK_FORMAT_A2R10G10B10_SNORM_PACK32, ISL_FORMAT_B10G10R10A2_SNORM),
193 fmt1(VK_FORMAT_A2R10G10B10_USCALED_PACK32, ISL_FORMAT_B10G10R10A2_USCALED),
194 fmt1(VK_FORMAT_A2R10G10B10_SSCALED_PACK32, ISL_FORMAT_B10G10R10A2_SSCALED),
195 fmt1(VK_FORMAT_A2R10G10B10_UINT_PACK32, ISL_FORMAT_B10G10R10A2_UINT),
196 fmt1(VK_FORMAT_A2R10G10B10_SINT_PACK32, ISL_FORMAT_B10G10R10A2_SINT),
197 fmt1(VK_FORMAT_A2B10G10R10_UNORM_PACK32, ISL_FORMAT_R10G10B10A2_UNORM),
198 fmt1(VK_FORMAT_A2B10G10R10_SNORM_PACK32, ISL_FORMAT_R10G10B10A2_SNORM),
199 fmt1(VK_FORMAT_A2B10G10R10_USCALED_PACK32, ISL_FORMAT_R10G10B10A2_USCALED),
200 fmt1(VK_FORMAT_A2B10G10R10_SSCALED_PACK32, ISL_FORMAT_R10G10B10A2_SSCALED),
201 fmt1(VK_FORMAT_A2B10G10R10_UINT_PACK32, ISL_FORMAT_R10G10B10A2_UINT),
202 fmt1(VK_FORMAT_A2B10G10R10_SINT_PACK32, ISL_FORMAT_R10G10B10A2_SINT),
203 fmt1(VK_FORMAT_R16_UNORM, ISL_FORMAT_R16_UNORM),
204 fmt1(VK_FORMAT_R16_SNORM, ISL_FORMAT_R16_SNORM),
205 fmt1(VK_FORMAT_R16_USCALED, ISL_FORMAT_R16_USCALED),
206 fmt1(VK_FORMAT_R16_SSCALED, ISL_FORMAT_R16_SSCALED),
207 fmt1(VK_FORMAT_R16_UINT, ISL_FORMAT_R16_UINT),
208 fmt1(VK_FORMAT_R16_SINT, ISL_FORMAT_R16_SINT),
209 fmt1(VK_FORMAT_R16_SFLOAT, ISL_FORMAT_R16_FLOAT),
210 fmt1(VK_FORMAT_R16G16_UNORM, ISL_FORMAT_R16G16_UNORM),
211 fmt1(VK_FORMAT_R16G16_SNORM, ISL_FORMAT_R16G16_SNORM),
212 fmt1(VK_FORMAT_R16G16_USCALED, ISL_FORMAT_R16G16_USCALED),
213 fmt1(VK_FORMAT_R16G16_SSCALED, ISL_FORMAT_R16G16_SSCALED),
214 fmt1(VK_FORMAT_R16G16_UINT, ISL_FORMAT_R16G16_UINT),
215 fmt1(VK_FORMAT_R16G16_SINT, ISL_FORMAT_R16G16_SINT),
216 fmt1(VK_FORMAT_R16G16_SFLOAT, ISL_FORMAT_R16G16_FLOAT),
217 fmt1(VK_FORMAT_R16G16B16_UNORM, ISL_FORMAT_R16G16B16_UNORM),
218 fmt1(VK_FORMAT_R16G16B16_SNORM, ISL_FORMAT_R16G16B16_SNORM),
219 fmt1(VK_FORMAT_R16G16B16_USCALED, ISL_FORMAT_R16G16B16_USCALED),
220 fmt1(VK_FORMAT_R16G16B16_SSCALED, ISL_FORMAT_R16G16B16_SSCALED),
221 fmt1(VK_FORMAT_R16G16B16_UINT, ISL_FORMAT_R16G16B16_UINT),
222 fmt1(VK_FORMAT_R16G16B16_SINT, ISL_FORMAT_R16G16B16_SINT),
223 fmt1(VK_FORMAT_R16G16B16_SFLOAT, ISL_FORMAT_R16G16B16_FLOAT),
224 fmt1(VK_FORMAT_R16G16B16A16_UNORM, ISL_FORMAT_R16G16B16A16_UNORM),
225 fmt1(VK_FORMAT_R16G16B16A16_SNORM, ISL_FORMAT_R16G16B16A16_SNORM),
226 fmt1(VK_FORMAT_R16G16B16A16_USCALED, ISL_FORMAT_R16G16B16A16_USCALED),
227 fmt1(VK_FORMAT_R16G16B16A16_SSCALED, ISL_FORMAT_R16G16B16A16_SSCALED),
228 fmt1(VK_FORMAT_R16G16B16A16_UINT, ISL_FORMAT_R16G16B16A16_UINT),
229 fmt1(VK_FORMAT_R16G16B16A16_SINT, ISL_FORMAT_R16G16B16A16_SINT),
230 fmt1(VK_FORMAT_R16G16B16A16_SFLOAT, ISL_FORMAT_R16G16B16A16_FLOAT),
231 fmt1(VK_FORMAT_R32_UINT, ISL_FORMAT_R32_UINT),
232 fmt1(VK_FORMAT_R32_SINT, ISL_FORMAT_R32_SINT),
233 fmt1(VK_FORMAT_R32_SFLOAT, ISL_FORMAT_R32_FLOAT),
234 fmt1(VK_FORMAT_R32G32_UINT, ISL_FORMAT_R32G32_UINT),
235 fmt1(VK_FORMAT_R32G32_SINT, ISL_FORMAT_R32G32_SINT),
236 fmt1(VK_FORMAT_R32G32_SFLOAT, ISL_FORMAT_R32G32_FLOAT),
237 fmt1(VK_FORMAT_R32G32B32_UINT, ISL_FORMAT_R32G32B32_UINT),
238 fmt1(VK_FORMAT_R32G32B32_SINT, ISL_FORMAT_R32G32B32_SINT),
239 fmt1(VK_FORMAT_R32G32B32_SFLOAT, ISL_FORMAT_R32G32B32_FLOAT),
240 fmt1(VK_FORMAT_R32G32B32A32_UINT, ISL_FORMAT_R32G32B32A32_UINT),
241 fmt1(VK_FORMAT_R32G32B32A32_SINT, ISL_FORMAT_R32G32B32A32_SINT),
242 fmt1(VK_FORMAT_R32G32B32A32_SFLOAT, ISL_FORMAT_R32G32B32A32_FLOAT),
243 fmt1(VK_FORMAT_R64_UINT, ISL_FORMAT_R64_PASSTHRU),
244 fmt1(VK_FORMAT_R64_SINT, ISL_FORMAT_R64_PASSTHRU),
245 fmt1(VK_FORMAT_R64_SFLOAT, ISL_FORMAT_R64_PASSTHRU),
246 fmt1(VK_FORMAT_R64G64_UINT, ISL_FORMAT_R64G64_PASSTHRU),
247 fmt1(VK_FORMAT_R64G64_SINT, ISL_FORMAT_R64G64_PASSTHRU),
248 fmt1(VK_FORMAT_R64G64_SFLOAT, ISL_FORMAT_R64G64_PASSTHRU),
249 fmt1(VK_FORMAT_R64G64B64_UINT, ISL_FORMAT_R64G64B64_PASSTHRU),
250 fmt1(VK_FORMAT_R64G64B64_SINT, ISL_FORMAT_R64G64B64_PASSTHRU),
251 fmt1(VK_FORMAT_R64G64B64_SFLOAT, ISL_FORMAT_R64G64B64_PASSTHRU),
252 fmt1(VK_FORMAT_R64G64B64A64_UINT, ISL_FORMAT_R64G64B64A64_PASSTHRU),
253 fmt1(VK_FORMAT_R64G64B64A64_SINT, ISL_FORMAT_R64G64B64A64_PASSTHRU),
254 fmt1(VK_FORMAT_R64G64B64A64_SFLOAT, ISL_FORMAT_R64G64B64A64_PASSTHRU),
255 fmt1(VK_FORMAT_B10G11R11_UFLOAT_PACK32, ISL_FORMAT_R11G11B10_FLOAT),
256 fmt1(VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, ISL_FORMAT_R9G9B9E5_SHAREDEXP),
257
258 d_fmt(VK_FORMAT_D16_UNORM, ISL_FORMAT_R16_UNORM),
259 d_fmt(VK_FORMAT_X8_D24_UNORM_PACK32, ISL_FORMAT_R24_UNORM_X8_TYPELESS),
260 d_fmt(VK_FORMAT_D32_SFLOAT, ISL_FORMAT_R32_FLOAT),
261 s_fmt(VK_FORMAT_S8_UINT, ISL_FORMAT_R8_UINT),
262 fmt_unsupported(VK_FORMAT_D16_UNORM_S8_UINT),
263 ds_fmt2(VK_FORMAT_D24_UNORM_S8_UINT, ISL_FORMAT_R24_UNORM_X8_TYPELESS, ISL_FORMAT_R8_UINT),
264 ds_fmt2(VK_FORMAT_D32_SFLOAT_S8_UINT, ISL_FORMAT_R32_FLOAT, ISL_FORMAT_R8_UINT),
265
266 swiz_fmt1(VK_FORMAT_BC1_RGB_UNORM_BLOCK, ISL_FORMAT_BC1_UNORM, RGB1),
267 swiz_fmt1(VK_FORMAT_BC1_RGB_SRGB_BLOCK, ISL_FORMAT_BC1_UNORM_SRGB, RGB1),
268 fmt1(VK_FORMAT_BC1_RGBA_UNORM_BLOCK, ISL_FORMAT_BC1_UNORM),
269 fmt1(VK_FORMAT_BC1_RGBA_SRGB_BLOCK, ISL_FORMAT_BC1_UNORM_SRGB),
270 fmt1(VK_FORMAT_BC2_UNORM_BLOCK, ISL_FORMAT_BC2_UNORM),
271 fmt1(VK_FORMAT_BC2_SRGB_BLOCK, ISL_FORMAT_BC2_UNORM_SRGB),
272 fmt1(VK_FORMAT_BC3_UNORM_BLOCK, ISL_FORMAT_BC3_UNORM),
273 fmt1(VK_FORMAT_BC3_SRGB_BLOCK, ISL_FORMAT_BC3_UNORM_SRGB),
274 fmt1(VK_FORMAT_BC4_UNORM_BLOCK, ISL_FORMAT_BC4_UNORM),
275 fmt1(VK_FORMAT_BC4_SNORM_BLOCK, ISL_FORMAT_BC4_SNORM),
276 fmt1(VK_FORMAT_BC5_UNORM_BLOCK, ISL_FORMAT_BC5_UNORM),
277 fmt1(VK_FORMAT_BC5_SNORM_BLOCK, ISL_FORMAT_BC5_SNORM),
278 fmt1(VK_FORMAT_BC6H_UFLOAT_BLOCK, ISL_FORMAT_BC6H_UF16),
279 fmt1(VK_FORMAT_BC6H_SFLOAT_BLOCK, ISL_FORMAT_BC6H_SF16),
280 fmt1(VK_FORMAT_BC7_UNORM_BLOCK, ISL_FORMAT_BC7_UNORM),
281 fmt1(VK_FORMAT_BC7_SRGB_BLOCK, ISL_FORMAT_BC7_UNORM_SRGB),
282 fmt1(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, ISL_FORMAT_ETC2_RGB8),
283 fmt1(VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK, ISL_FORMAT_ETC2_SRGB8),
284 fmt1(VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK, ISL_FORMAT_ETC2_RGB8_PTA),
285 fmt1(VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK, ISL_FORMAT_ETC2_SRGB8_PTA),
286 fmt1(VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, ISL_FORMAT_ETC2_EAC_RGBA8),
287 fmt1(VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK, ISL_FORMAT_ETC2_EAC_SRGB8_A8),
288 fmt1(VK_FORMAT_EAC_R11_UNORM_BLOCK, ISL_FORMAT_EAC_R11),
289 fmt1(VK_FORMAT_EAC_R11_SNORM_BLOCK, ISL_FORMAT_EAC_SIGNED_R11),
290 fmt1(VK_FORMAT_EAC_R11G11_UNORM_BLOCK, ISL_FORMAT_EAC_RG11),
291 fmt1(VK_FORMAT_EAC_R11G11_SNORM_BLOCK, ISL_FORMAT_EAC_SIGNED_RG11),
292 fmt1(VK_FORMAT_ASTC_4x4_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_4X4_U8SRGB),
293 fmt1(VK_FORMAT_ASTC_5x4_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_5X4_U8SRGB),
294 fmt1(VK_FORMAT_ASTC_5x5_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_5X5_U8SRGB),
295 fmt1(VK_FORMAT_ASTC_6x5_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_6X5_U8SRGB),
296 fmt1(VK_FORMAT_ASTC_6x6_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_6X6_U8SRGB),
297 fmt1(VK_FORMAT_ASTC_8x5_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X5_U8SRGB),
298 fmt1(VK_FORMAT_ASTC_8x6_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X6_U8SRGB),
299 fmt1(VK_FORMAT_ASTC_8x8_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X8_U8SRGB),
300 fmt1(VK_FORMAT_ASTC_10x5_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X5_U8SRGB),
301 fmt1(VK_FORMAT_ASTC_10x6_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X6_U8SRGB),
302 fmt1(VK_FORMAT_ASTC_10x8_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X8_U8SRGB),
303 fmt1(VK_FORMAT_ASTC_10x10_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X10_U8SRGB),
304 fmt1(VK_FORMAT_ASTC_12x10_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_12X10_U8SRGB),
305 fmt1(VK_FORMAT_ASTC_12x12_SRGB_BLOCK, ISL_FORMAT_ASTC_LDR_2D_12X12_U8SRGB),
306 fmt1(VK_FORMAT_ASTC_4x4_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_4X4_FLT16),
307 fmt1(VK_FORMAT_ASTC_5x4_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_5X4_FLT16),
308 fmt1(VK_FORMAT_ASTC_5x5_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_5X5_FLT16),
309 fmt1(VK_FORMAT_ASTC_6x5_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_6X5_FLT16),
310 fmt1(VK_FORMAT_ASTC_6x6_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_6X6_FLT16),
311 fmt1(VK_FORMAT_ASTC_8x5_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X5_FLT16),
312 fmt1(VK_FORMAT_ASTC_8x6_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X6_FLT16),
313 fmt1(VK_FORMAT_ASTC_8x8_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_8X8_FLT16),
314 fmt1(VK_FORMAT_ASTC_10x5_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X5_FLT16),
315 fmt1(VK_FORMAT_ASTC_10x6_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X6_FLT16),
316 fmt1(VK_FORMAT_ASTC_10x8_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X8_FLT16),
317 fmt1(VK_FORMAT_ASTC_10x10_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_10X10_FLT16),
318 fmt1(VK_FORMAT_ASTC_12x10_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_12X10_FLT16),
319 fmt1(VK_FORMAT_ASTC_12x12_UNORM_BLOCK, ISL_FORMAT_ASTC_LDR_2D_12X12_FLT16),
320 fmt_unsupported(VK_FORMAT_B8G8R8_UNORM),
321 fmt_unsupported(VK_FORMAT_B8G8R8_SNORM),
322 fmt_unsupported(VK_FORMAT_B8G8R8_USCALED),
323 fmt_unsupported(VK_FORMAT_B8G8R8_SSCALED),
324 fmt_unsupported(VK_FORMAT_B8G8R8_UINT),
325 fmt_unsupported(VK_FORMAT_B8G8R8_SINT),
326 fmt_unsupported(VK_FORMAT_B8G8R8_SRGB),
327 fmt1(VK_FORMAT_B8G8R8A8_UNORM, ISL_FORMAT_B8G8R8A8_UNORM),
328 fmt_unsupported(VK_FORMAT_B8G8R8A8_SNORM),
329 fmt_unsupported(VK_FORMAT_B8G8R8A8_USCALED),
330 fmt_unsupported(VK_FORMAT_B8G8R8A8_SSCALED),
331 fmt_unsupported(VK_FORMAT_B8G8R8A8_UINT),
332 fmt_unsupported(VK_FORMAT_B8G8R8A8_SINT),
333 fmt1(VK_FORMAT_B8G8R8A8_SRGB, ISL_FORMAT_B8G8R8A8_UNORM_SRGB),
334 };
335
336 static const struct anv_format ycbcr_formats[] = {
337 ycbcr_fmt(VK_FORMAT_G8B8G8R8_422_UNORM, 1,
338 y_plane(0, ISL_FORMAT_YCRCB_SWAPUV, RGBA, _ISL_SWIZZLE(BLUE, GREEN, RED, ZERO), 1, 1)),
339 ycbcr_fmt(VK_FORMAT_B8G8R8G8_422_UNORM, 1,
340 y_plane(0, ISL_FORMAT_YCRCB_SWAPUVY, RGBA, _ISL_SWIZZLE(BLUE, GREEN, RED, ZERO), 1, 1)),
341 ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, 3,
342 y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
343 chroma_plane(1, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 2, 2),
344 chroma_plane(2, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 2)),
345 ycbcr_fmt(VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, 2,
346 y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
347 chroma_plane(1, ISL_FORMAT_R8G8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, RED, ZERO, ZERO), 2, 2)),
348 ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_422_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, 1),
351 chroma_plane(2, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 1)),
352 ycbcr_fmt(VK_FORMAT_G8_B8R8_2PLANE_422_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, 1)),
355 ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_444_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), 1, 1),
358 chroma_plane(2, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 1, 1)),
359
360 fmt_unsupported(VK_FORMAT_R10X6_UNORM_PACK16),
361 fmt_unsupported(VK_FORMAT_R10X6G10X6_UNORM_2PACK16),
362 fmt_unsupported(VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16),
363 fmt_unsupported(VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16),
364 fmt_unsupported(VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16),
365 fmt_unsupported(VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16),
366 fmt_unsupported(VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16),
367 fmt_unsupported(VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16),
368 fmt_unsupported(VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16),
369 fmt_unsupported(VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16),
370 fmt_unsupported(VK_FORMAT_R12X4_UNORM_PACK16),
371 fmt_unsupported(VK_FORMAT_R12X4G12X4_UNORM_2PACK16),
372 fmt_unsupported(VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16),
373 fmt_unsupported(VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16),
374 fmt_unsupported(VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16),
375 fmt_unsupported(VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16),
376 fmt_unsupported(VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16),
377 fmt_unsupported(VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16),
378 fmt_unsupported(VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16),
379 fmt_unsupported(VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16),
380 /* TODO: it is possible to enable the following 2 formats, but that
381 * requires further refactoring of how we handle multiplanar formats.
382 */
383 fmt_unsupported(VK_FORMAT_G16B16G16R16_422_UNORM),
384 fmt_unsupported(VK_FORMAT_B16G16R16G16_422_UNORM),
385
386 ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM, 3,
387 y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
388 chroma_plane(1, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 2, 2),
389 chroma_plane(2, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 2)),
390 ycbcr_fmt(VK_FORMAT_G16_B16R16_2PLANE_420_UNORM, 2,
391 y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
392 chroma_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, RED, ZERO, ZERO), 2, 2)),
393 ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_422_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, 1),
396 chroma_plane(2, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 1)),
397 ycbcr_fmt(VK_FORMAT_G16_B16R16_2PLANE_422_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, 1)),
400 ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_444_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), 1, 1),
403 chroma_plane(2, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 1, 1)),
404 };
405
406 #undef _fmt
407 #undef swiz_fmt1
408 #undef fmt1
409 #undef fmt
410
411 static const struct {
412 const struct anv_format *formats;
413 uint32_t n_formats;
414 } anv_formats[] = {
415 [0] = { .formats = main_formats,
416 .n_formats = ARRAY_SIZE(main_formats), },
417 [_VK_KHR_sampler_ycbcr_conversion_number] = { .formats = ycbcr_formats,
418 .n_formats = ARRAY_SIZE(ycbcr_formats), },
419 };
420
421 const struct anv_format *
422 anv_get_format(VkFormat vk_format)
423 {
424 uint32_t enum_offset = VK_ENUM_OFFSET(vk_format);
425 uint32_t ext_number = VK_ENUM_EXTENSION(vk_format);
426
427 if (ext_number >= ARRAY_SIZE(anv_formats) ||
428 enum_offset >= anv_formats[ext_number].n_formats)
429 return NULL;
430
431 const struct anv_format *format =
432 &anv_formats[ext_number].formats[enum_offset];
433 if (format->planes[0].isl_format == ISL_FORMAT_UNSUPPORTED)
434 return NULL;
435
436 return format;
437 }
438
439 /**
440 * Exactly one bit must be set in \a aspect.
441 */
442 struct anv_format_plane
443 anv_get_format_plane(const struct gen_device_info *devinfo, VkFormat vk_format,
444 VkImageAspectFlagBits aspect, VkImageTiling tiling)
445 {
446 const struct anv_format *format = anv_get_format(vk_format);
447 const struct anv_format_plane unsupported = {
448 .isl_format = ISL_FORMAT_UNSUPPORTED,
449 };
450
451 if (format == NULL)
452 return unsupported;
453
454 uint32_t plane = anv_image_aspect_to_plane(vk_format_aspects(vk_format), aspect);
455 struct anv_format_plane plane_format = format->planes[plane];
456 if (plane_format.isl_format == ISL_FORMAT_UNSUPPORTED)
457 return unsupported;
458
459 if (aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
460 assert(vk_format_aspects(vk_format) &
461 (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
462
463 /* There's no reason why we strictly can't support depth or stencil with
464 * modifiers but there's also no reason why we should.
465 */
466 if (tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
467 return unsupported;
468
469 return plane_format;
470 }
471
472 assert((aspect & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0);
473
474 const struct isl_format_layout *isl_layout =
475 isl_format_get_layout(plane_format.isl_format);
476
477 /* On Ivy Bridge we don't even have enough 24 and 48-bit formats that we
478 * can reliably do texture upload with BLORP so just don't claim support
479 * for any of them.
480 */
481 if (devinfo->gen == 7 && !devinfo->is_haswell &&
482 (isl_layout->bpb == 24 || isl_layout->bpb == 48))
483 return unsupported;
484
485 if (tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
486 /* No non-power-of-two fourcc formats exist */
487 if (!util_is_power_of_two_or_zero(isl_layout->bpb))
488 return unsupported;
489
490 if (isl_format_is_compressed(plane_format.isl_format))
491 return unsupported;
492 }
493
494 if (tiling == VK_IMAGE_TILING_OPTIMAL &&
495 !util_is_power_of_two_or_zero(isl_layout->bpb)) {
496 /* Tiled formats *must* be power-of-two because we need up upload
497 * them with the render pipeline. For 3-channel formats, we fix
498 * this by switching them over to RGBX or RGBA formats under the
499 * hood.
500 */
501 enum isl_format rgbx = isl_format_rgb_to_rgbx(plane_format.isl_format);
502 if (rgbx != ISL_FORMAT_UNSUPPORTED &&
503 isl_format_supports_rendering(devinfo, rgbx)) {
504 plane_format.isl_format = rgbx;
505 } else {
506 plane_format.isl_format =
507 isl_format_rgb_to_rgba(plane_format.isl_format);
508 plane_format.swizzle = ISL_SWIZZLE(RED, GREEN, BLUE, ONE);
509 }
510 }
511
512 /* The B4G4R4A4 format isn't available prior to Broadwell so we have to fall
513 * back to a format with a more complex swizzle.
514 */
515 if (vk_format == VK_FORMAT_B4G4R4A4_UNORM_PACK16 && devinfo->gen < 8) {
516 plane_format.isl_format = ISL_FORMAT_B4G4R4A4_UNORM;
517 plane_format.swizzle = ISL_SWIZZLE(GREEN, RED, ALPHA, BLUE);
518 }
519
520 return plane_format;
521 }
522
523 // Format capabilities
524
525 VkFormatFeatureFlags
526 anv_get_image_format_features(const struct gen_device_info *devinfo,
527 VkFormat vk_format,
528 const struct anv_format *anv_format,
529 VkImageTiling vk_tiling)
530 {
531 VkFormatFeatureFlags flags = 0;
532
533 if (anv_format == NULL)
534 return 0;
535
536 const VkImageAspectFlags aspects = vk_format_aspects(vk_format);
537
538 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
539 if (vk_tiling == VK_IMAGE_TILING_LINEAR)
540 return 0;
541
542 flags |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT |
543 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
544 VK_FORMAT_FEATURE_BLIT_SRC_BIT |
545 VK_FORMAT_FEATURE_BLIT_DST_BIT |
546 VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
547 VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
548
549 if ((aspects & VK_IMAGE_ASPECT_DEPTH_BIT) && devinfo->gen >= 9)
550 flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT_EXT;
551
552 return flags;
553 }
554
555 const struct anv_format_plane plane_format =
556 anv_get_format_plane(devinfo, vk_format, VK_IMAGE_ASPECT_COLOR_BIT,
557 vk_tiling);
558
559 if (plane_format.isl_format == ISL_FORMAT_UNSUPPORTED)
560 return 0;
561
562 struct anv_format_plane base_plane_format = plane_format;
563 if (vk_tiling != VK_IMAGE_TILING_LINEAR) {
564 base_plane_format = anv_get_format_plane(devinfo, vk_format,
565 VK_IMAGE_ASPECT_COLOR_BIT,
566 VK_IMAGE_TILING_LINEAR);
567 }
568
569 enum isl_format base_isl_format = base_plane_format.isl_format;
570
571 /* ASTC textures must be in Y-tiled memory */
572 if (vk_tiling == VK_IMAGE_TILING_LINEAR &&
573 isl_format_get_layout(plane_format.isl_format)->txc == ISL_TXC_ASTC)
574 return 0;
575
576 /* ASTC requires nasty workarounds on BSW so we just disable it for now.
577 *
578 * TODO: Figure out the ASTC workarounds and re-enable on BSW.
579 */
580 if (devinfo->gen < 9 &&
581 isl_format_get_layout(plane_format.isl_format)->txc == ISL_TXC_ASTC)
582 return 0;
583
584 if (isl_format_supports_sampling(devinfo, plane_format.isl_format)) {
585 flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
586
587 if (devinfo->gen >= 9)
588 flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT_EXT;
589
590 if (isl_format_supports_filtering(devinfo, plane_format.isl_format))
591 flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
592 }
593
594 /* We can render to swizzled formats. However, if the alpha channel is
595 * moved, then blending won't work correctly. The PRM tells us
596 * straight-up not to render to such a surface.
597 */
598 if (isl_format_supports_rendering(devinfo, plane_format.isl_format) &&
599 plane_format.swizzle.a == ISL_CHANNEL_SELECT_ALPHA) {
600 flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;
601
602 if (isl_format_supports_alpha_blending(devinfo, plane_format.isl_format))
603 flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
604 }
605
606 /* Load/store is determined based on base format. This prevents RGB
607 * formats from showing up as load/store capable.
608 */
609 if (isl_is_storage_image_format(base_isl_format))
610 flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
611
612 if (base_isl_format == ISL_FORMAT_R32_SINT ||
613 base_isl_format == ISL_FORMAT_R32_UINT ||
614 base_isl_format == ISL_FORMAT_R32_FLOAT)
615 flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
616
617 if (flags) {
618 flags |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |
619 VK_FORMAT_FEATURE_BLIT_DST_BIT |
620 VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
621 VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
622 }
623
624 /* XXX: We handle 3-channel formats by switching them out for RGBX or
625 * RGBA formats behind-the-scenes. This works fine for textures
626 * because the upload process will fill in the extra channel.
627 * We could also support it for render targets, but it will take
628 * substantially more work and we have enough RGBX formats to handle
629 * what most clients will want.
630 */
631 if (vk_tiling == VK_IMAGE_TILING_OPTIMAL &&
632 base_isl_format != ISL_FORMAT_UNSUPPORTED &&
633 !util_is_power_of_two_or_zero(isl_format_layouts[base_isl_format].bpb) &&
634 isl_format_rgb_to_rgbx(base_isl_format) == ISL_FORMAT_UNSUPPORTED) {
635 flags &= ~VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;
636 flags &= ~VK_FORMAT_FEATURE_BLIT_DST_BIT;
637 }
638
639 if (anv_format->can_ycbcr) {
640 /* The sampler doesn't have support for mid point when it handles YUV on
641 * its own.
642 */
643 if (isl_format_is_yuv(anv_format->planes[0].isl_format)) {
644 /* TODO: We've disabled linear implicit reconstruction with the
645 * sampler. The failures show a slightly out of range values on the
646 * bottom left of the sampled image.
647 */
648 flags |= VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT;
649 } else {
650 flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT |
651 VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT |
652 VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_SEPARATE_RECONSTRUCTION_FILTER_BIT;
653 }
654
655 /* We can support cosited chroma locations when handle planes with our
656 * own shader snippets.
657 */
658 for (unsigned p = 0; p < anv_format->n_planes; p++) {
659 if (anv_format->planes[p].denominator_scales[0] > 1 ||
660 anv_format->planes[p].denominator_scales[1] > 1) {
661 flags |= VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT;
662 break;
663 }
664 }
665
666 if (anv_format->n_planes > 1)
667 flags |= VK_FORMAT_FEATURE_DISJOINT_BIT;
668
669 const VkFormatFeatureFlags disallowed_ycbcr_image_features =
670 VK_FORMAT_FEATURE_BLIT_SRC_BIT |
671 VK_FORMAT_FEATURE_BLIT_DST_BIT |
672 VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
673 VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT |
674 VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
675
676 flags &= ~disallowed_ycbcr_image_features;
677 }
678
679 return flags;
680 }
681
682 static VkFormatFeatureFlags
683 get_buffer_format_features(const struct gen_device_info *devinfo,
684 VkFormat vk_format,
685 const struct anv_format *anv_format)
686 {
687 VkFormatFeatureFlags flags = 0;
688
689 if (anv_format == NULL)
690 return 0;
691
692 const enum isl_format isl_format = anv_format->planes[0].isl_format;
693
694 if (isl_format == ISL_FORMAT_UNSUPPORTED)
695 return 0;
696
697 if (anv_format->n_planes > 1)
698 return 0;
699
700 if (anv_format->can_ycbcr)
701 return 0;
702
703 if (vk_format_is_depth_or_stencil(vk_format))
704 return 0;
705
706 if (isl_format_supports_sampling(devinfo, isl_format) &&
707 !isl_format_is_compressed(isl_format))
708 flags |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT;
709
710 if (isl_format_supports_vertex_fetch(devinfo, isl_format))
711 flags |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
712
713 if (isl_is_storage_image_format(isl_format))
714 flags |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
715
716 if (isl_format == ISL_FORMAT_R32_SINT || isl_format == ISL_FORMAT_R32_UINT)
717 flags |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
718
719 return flags;
720 }
721
722 static void
723 get_wsi_format_modifier_properties_list(const struct anv_physical_device *physical_device,
724 VkFormat vk_format,
725 VkDrmFormatModifierPropertiesListEXT *list)
726 {
727 const struct anv_format *anv_format = anv_get_format(vk_format);
728
729 VK_OUTARRAY_MAKE(out, list->pDrmFormatModifierProperties,
730 &list->drmFormatModifierCount);
731
732 /* This is a simplified list where all the modifiers are available */
733 assert(vk_format == VK_FORMAT_B8G8R8_SRGB ||
734 vk_format == VK_FORMAT_B8G8R8_UNORM ||
735 vk_format == VK_FORMAT_B8G8R8A8_SRGB ||
736 vk_format == VK_FORMAT_B8G8R8A8_UNORM);
737
738 uint64_t modifiers[] = {
739 DRM_FORMAT_MOD_LINEAR,
740 I915_FORMAT_MOD_X_TILED,
741 I915_FORMAT_MOD_Y_TILED,
742 I915_FORMAT_MOD_Y_TILED_CCS,
743 };
744
745 for (uint32_t i = 0; i < ARRAY_SIZE(modifiers); i++) {
746 const struct isl_drm_modifier_info *mod_info =
747 isl_drm_modifier_get_info(modifiers[i]);
748
749 if (mod_info->aux_usage == ISL_AUX_USAGE_CCS_E &&
750 !isl_format_supports_ccs_e(&physical_device->info,
751 anv_format->planes[0].isl_format))
752 continue;
753
754 /* Gen12's CCS layout changes compared to Gen9-11. */
755 if (mod_info->modifier == I915_FORMAT_MOD_Y_TILED_CCS &&
756 physical_device->info.gen >= 12)
757 continue;
758
759 vk_outarray_append(&out, mod_props) {
760 mod_props->drmFormatModifier = modifiers[i];
761 if (isl_drm_modifier_has_aux(modifiers[i]))
762 mod_props->drmFormatModifierPlaneCount = 2;
763 else
764 mod_props->drmFormatModifierPlaneCount = anv_format->n_planes;
765 }
766 }
767 }
768
769 void anv_GetPhysicalDeviceFormatProperties(
770 VkPhysicalDevice physicalDevice,
771 VkFormat vk_format,
772 VkFormatProperties* pFormatProperties)
773 {
774 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
775 const struct gen_device_info *devinfo = &physical_device->info;
776 const struct anv_format *anv_format = anv_get_format(vk_format);
777
778 *pFormatProperties = (VkFormatProperties) {
779 .linearTilingFeatures =
780 anv_get_image_format_features(devinfo, vk_format, anv_format,
781 VK_IMAGE_TILING_LINEAR),
782 .optimalTilingFeatures =
783 anv_get_image_format_features(devinfo, vk_format, anv_format,
784 VK_IMAGE_TILING_OPTIMAL),
785 .bufferFeatures =
786 get_buffer_format_features(devinfo, vk_format, anv_format),
787 };
788 }
789
790 void anv_GetPhysicalDeviceFormatProperties2(
791 VkPhysicalDevice physicalDevice,
792 VkFormat format,
793 VkFormatProperties2* pFormatProperties)
794 {
795 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
796 anv_GetPhysicalDeviceFormatProperties(physicalDevice, format,
797 &pFormatProperties->formatProperties);
798
799 vk_foreach_struct(ext, pFormatProperties->pNext) {
800 /* Use unsigned since some cases are not in the VkStructureType enum. */
801 switch ((unsigned)ext->sType) {
802 case VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT:
803 get_wsi_format_modifier_properties_list(physical_device, format,
804 (void *)ext);
805 break;
806 default:
807 anv_debug_ignored_stype(ext->sType);
808 break;
809 }
810 }
811 }
812
813 static VkResult
814 anv_get_image_format_properties(
815 struct anv_physical_device *physical_device,
816 const VkPhysicalDeviceImageFormatInfo2 *info,
817 VkImageFormatProperties *pImageFormatProperties,
818 VkSamplerYcbcrConversionImageFormatProperties *pYcbcrImageFormatProperties)
819 {
820 VkFormatFeatureFlags format_feature_flags;
821 VkExtent3D maxExtent;
822 uint32_t maxMipLevels;
823 uint32_t maxArraySize;
824 VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
825 const struct gen_device_info *devinfo = &physical_device->info;
826 const struct anv_format *format = anv_get_format(info->format);
827
828 if (format == NULL)
829 goto unsupported;
830
831 assert(format->vk_format == info->format);
832 format_feature_flags = anv_get_image_format_features(devinfo, info->format,
833 format, info->tiling);
834
835 switch (info->type) {
836 default:
837 unreachable("bad VkImageType");
838 case VK_IMAGE_TYPE_1D:
839 maxExtent.width = 16384;
840 maxExtent.height = 1;
841 maxExtent.depth = 1;
842 maxMipLevels = 15; /* log2(maxWidth) + 1 */
843 maxArraySize = 2048;
844 sampleCounts = VK_SAMPLE_COUNT_1_BIT;
845 break;
846 case VK_IMAGE_TYPE_2D:
847 /* FINISHME: Does this really differ for cube maps? The documentation
848 * for RENDER_SURFACE_STATE suggests so.
849 */
850 maxExtent.width = 16384;
851 maxExtent.height = 16384;
852 maxExtent.depth = 1;
853 maxMipLevels = 15; /* log2(maxWidth) + 1 */
854 maxArraySize = 2048;
855 break;
856 case VK_IMAGE_TYPE_3D:
857 maxExtent.width = 2048;
858 maxExtent.height = 2048;
859 maxExtent.depth = 2048;
860 maxMipLevels = 12; /* log2(maxWidth) + 1 */
861 maxArraySize = 1;
862 break;
863 }
864
865 /* Our hardware doesn't support 1D compressed textures.
866 * From the SKL PRM, RENDER_SURFACE_STATE::SurfaceFormat:
867 * * This field cannot be a compressed (BC*, DXT*, FXT*, ETC*, EAC*) format
868 * if the Surface Type is SURFTYPE_1D.
869 * * This field cannot be ASTC format if the Surface Type is SURFTYPE_1D.
870 */
871 if (info->type == VK_IMAGE_TYPE_1D &&
872 isl_format_is_compressed(format->planes[0].isl_format)) {
873 goto unsupported;
874 }
875
876 if (info->tiling == VK_IMAGE_TILING_OPTIMAL &&
877 info->type == VK_IMAGE_TYPE_2D &&
878 (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
879 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
880 !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
881 !(info->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
882 sampleCounts = isl_device_get_sample_counts(&physical_device->isl_dev);
883 }
884
885 if (info->usage & (VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
886 VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
887 /* Accept transfers on anything we can sample from or renderer to. */
888 if (!(format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
889 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT |
890 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))) {
891 goto unsupported;
892 }
893 }
894
895 if (info->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
896 if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
897 goto unsupported;
898 }
899 }
900
901 if (info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
902 if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
903 goto unsupported;
904 }
905 }
906
907 if (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
908 if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
909 goto unsupported;
910 }
911 }
912
913 if (info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
914 if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
915 goto unsupported;
916 }
917 }
918
919 if (info->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
920 /* Nothing to check. */
921 }
922
923 if (info->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
924 /* Ignore this flag because it was removed from the
925 * provisional_I_20150910 header.
926 */
927 }
928
929 if (info->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
930 const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *modifier_info =
931 vk_find_struct_const(info->pNext,
932 PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
933
934 /* Modifiers are only supported on simple 2D images */
935 if (info->type != VK_IMAGE_TYPE_2D)
936 goto unsupported;
937 maxArraySize = 1;
938 maxMipLevels = 1;
939 assert(sampleCounts == VK_SAMPLE_COUNT_1_BIT);
940
941 /* Modifiers are not yet supported for YCbCr */
942 const struct anv_format *format = anv_get_format(info->format);
943 if (format->n_planes > 1)
944 goto unsupported;
945
946 const struct isl_drm_modifier_info *isl_mod_info =
947 isl_drm_modifier_get_info(modifier_info->drmFormatModifier);
948 if (isl_mod_info->aux_usage == ISL_AUX_USAGE_CCS_E) {
949 /* If we have a CCS modifier, ensure that the format supports CCS
950 * and, if VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT is set, all of the
951 * formats in the format list are CCS compatible.
952 */
953 const VkImageFormatListCreateInfoKHR *fmt_list =
954 vk_find_struct_const(info->pNext,
955 IMAGE_FORMAT_LIST_CREATE_INFO_KHR);
956 if (!anv_formats_ccs_e_compatible(devinfo, info->flags,
957 info->format, info->tiling,
958 fmt_list))
959 goto unsupported;
960 }
961 }
962
963 /* From the bspec section entitled "Surface Layout and Tiling",
964 * pre-gen9 has a 2 GB limitation of the size in bytes,
965 * gen9 and gen10 have a 256 GB limitation and gen11+
966 * has a 16 TB limitation.
967 */
968 uint64_t maxResourceSize = 0;
969 if (devinfo->gen < 9)
970 maxResourceSize = (uint64_t) 1 << 31;
971 else if (devinfo->gen < 11)
972 maxResourceSize = (uint64_t) 1 << 38;
973 else
974 maxResourceSize = (uint64_t) 1 << 44;
975
976 *pImageFormatProperties = (VkImageFormatProperties) {
977 .maxExtent = maxExtent,
978 .maxMipLevels = maxMipLevels,
979 .maxArrayLayers = maxArraySize,
980 .sampleCounts = sampleCounts,
981
982 /* FINISHME: Accurately calculate
983 * VkImageFormatProperties::maxResourceSize.
984 */
985 .maxResourceSize = maxResourceSize,
986 };
987
988 if (pYcbcrImageFormatProperties) {
989 pYcbcrImageFormatProperties->combinedImageSamplerDescriptorCount =
990 format->n_planes;
991 }
992
993 return VK_SUCCESS;
994
995 unsupported:
996 *pImageFormatProperties = (VkImageFormatProperties) {
997 .maxExtent = { 0, 0, 0 },
998 .maxMipLevels = 0,
999 .maxArrayLayers = 0,
1000 .sampleCounts = 0,
1001 .maxResourceSize = 0,
1002 };
1003
1004 return VK_ERROR_FORMAT_NOT_SUPPORTED;
1005 }
1006
1007 VkResult anv_GetPhysicalDeviceImageFormatProperties(
1008 VkPhysicalDevice physicalDevice,
1009 VkFormat format,
1010 VkImageType type,
1011 VkImageTiling tiling,
1012 VkImageUsageFlags usage,
1013 VkImageCreateFlags createFlags,
1014 VkImageFormatProperties* pImageFormatProperties)
1015 {
1016 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1017
1018 const VkPhysicalDeviceImageFormatInfo2 info = {
1019 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1020 .pNext = NULL,
1021 .format = format,
1022 .type = type,
1023 .tiling = tiling,
1024 .usage = usage,
1025 .flags = createFlags,
1026 };
1027
1028 return anv_get_image_format_properties(physical_device, &info,
1029 pImageFormatProperties, NULL);
1030 }
1031
1032 static const VkExternalMemoryProperties prime_fd_props = {
1033 /* If we can handle external, then we can both import and export it. */
1034 .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1035 VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1036 /* For the moment, let's not support mixing and matching */
1037 .exportFromImportedHandleTypes =
1038 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1039 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
1040 .compatibleHandleTypes =
1041 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1042 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
1043 };
1044
1045 static const VkExternalMemoryProperties userptr_props = {
1046 .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1047 .exportFromImportedHandleTypes = 0,
1048 .compatibleHandleTypes =
1049 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,
1050 };
1051
1052 static const VkExternalMemoryProperties android_buffer_props = {
1053 .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1054 VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
1055 .exportFromImportedHandleTypes =
1056 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1057 .compatibleHandleTypes =
1058 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1059 };
1060
1061
1062 static const VkExternalMemoryProperties android_image_props = {
1063 .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1064 VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT |
1065 VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT,
1066 .exportFromImportedHandleTypes =
1067 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1068 .compatibleHandleTypes =
1069 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1070 };
1071
1072 VkResult anv_GetPhysicalDeviceImageFormatProperties2(
1073 VkPhysicalDevice physicalDevice,
1074 const VkPhysicalDeviceImageFormatInfo2* base_info,
1075 VkImageFormatProperties2* base_props)
1076 {
1077 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1078 const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
1079 VkExternalImageFormatProperties *external_props = NULL;
1080 VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;
1081 VkAndroidHardwareBufferUsageANDROID *android_usage = NULL;
1082 VkResult result;
1083
1084 /* Extract input structs */
1085 vk_foreach_struct_const(s, base_info->pNext) {
1086 switch (s->sType) {
1087 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
1088 external_info = (const void *) s;
1089 break;
1090 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT:
1091 /* anv_get_image_format_properties will handle this */
1092 break;
1093 case VK_STRUCTURE_TYPE_IMAGE_STENCIL_USAGE_CREATE_INFO_EXT:
1094 /* Ignore but don't warn */
1095 break;
1096 default:
1097 anv_debug_ignored_stype(s->sType);
1098 break;
1099 }
1100 }
1101
1102 /* Extract output structs */
1103 vk_foreach_struct(s, base_props->pNext) {
1104 switch (s->sType) {
1105 case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
1106 external_props = (void *) s;
1107 break;
1108 case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:
1109 ycbcr_props = (void *) s;
1110 break;
1111 case VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_USAGE_ANDROID:
1112 android_usage = (void *) s;
1113 break;
1114 default:
1115 anv_debug_ignored_stype(s->sType);
1116 break;
1117 }
1118 }
1119
1120 result = anv_get_image_format_properties(physical_device, base_info,
1121 &base_props->imageFormatProperties, ycbcr_props);
1122 if (result != VK_SUCCESS)
1123 goto fail;
1124
1125 bool ahw_supported =
1126 physical_device->supported_extensions.ANDROID_external_memory_android_hardware_buffer;
1127
1128 if (ahw_supported && android_usage) {
1129 android_usage->androidHardwareBufferUsage =
1130 anv_ahw_usage_from_vk_usage(base_info->flags,
1131 base_info->usage);
1132
1133 /* Limit maxArrayLayers to 1 for AHardwareBuffer based images for now. */
1134 base_props->imageFormatProperties.maxArrayLayers = 1;
1135 }
1136
1137 /* From the Vulkan 1.0.42 spec:
1138 *
1139 * If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
1140 * behave as if VkPhysicalDeviceExternalImageFormatInfo was not
1141 * present and VkExternalImageFormatProperties will be ignored.
1142 */
1143 if (external_info && external_info->handleType != 0) {
1144 switch (external_info->handleType) {
1145 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1146 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1147 if (external_props)
1148 external_props->externalMemoryProperties = prime_fd_props;
1149 break;
1150 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1151 if (external_props)
1152 external_props->externalMemoryProperties = userptr_props;
1153 break;
1154 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
1155 if (ahw_supported && external_props) {
1156 external_props->externalMemoryProperties = android_image_props;
1157 break;
1158 }
1159 /* fallthrough - if ahw not supported */
1160 default:
1161 /* From the Vulkan 1.0.42 spec:
1162 *
1163 * If handleType is not compatible with the [parameters] specified
1164 * in VkPhysicalDeviceImageFormatInfo2, then
1165 * vkGetPhysicalDeviceImageFormatProperties2 returns
1166 * VK_ERROR_FORMAT_NOT_SUPPORTED.
1167 */
1168 result = vk_errorfi(physical_device->instance, physical_device,
1169 VK_ERROR_FORMAT_NOT_SUPPORTED,
1170 "unsupported VkExternalMemoryTypeFlagBits 0x%x",
1171 external_info->handleType);
1172 goto fail;
1173 }
1174 }
1175
1176 return VK_SUCCESS;
1177
1178 fail:
1179 if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
1180 /* From the Vulkan 1.0.42 spec:
1181 *
1182 * If the combination of parameters to
1183 * vkGetPhysicalDeviceImageFormatProperties2 is not supported by
1184 * the implementation for use in vkCreateImage, then all members of
1185 * imageFormatProperties will be filled with zero.
1186 */
1187 base_props->imageFormatProperties = (VkImageFormatProperties) {};
1188 }
1189
1190 return result;
1191 }
1192
1193 void anv_GetPhysicalDeviceSparseImageFormatProperties(
1194 VkPhysicalDevice physicalDevice,
1195 VkFormat format,
1196 VkImageType type,
1197 uint32_t samples,
1198 VkImageUsageFlags usage,
1199 VkImageTiling tiling,
1200 uint32_t* pNumProperties,
1201 VkSparseImageFormatProperties* pProperties)
1202 {
1203 /* Sparse images are not yet supported. */
1204 *pNumProperties = 0;
1205 }
1206
1207 void anv_GetPhysicalDeviceSparseImageFormatProperties2(
1208 VkPhysicalDevice physicalDevice,
1209 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1210 uint32_t* pPropertyCount,
1211 VkSparseImageFormatProperties2* pProperties)
1212 {
1213 /* Sparse images are not yet supported. */
1214 *pPropertyCount = 0;
1215 }
1216
1217 void anv_GetPhysicalDeviceExternalBufferProperties(
1218 VkPhysicalDevice physicalDevice,
1219 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1220 VkExternalBufferProperties* pExternalBufferProperties)
1221 {
1222 /* The Vulkan 1.0.42 spec says "handleType must be a valid
1223 * VkExternalMemoryHandleTypeFlagBits value" in
1224 * VkPhysicalDeviceExternalBufferInfo. This differs from
1225 * VkPhysicalDeviceExternalImageFormatInfo, which surprisingly permits
1226 * handleType == 0.
1227 */
1228 assert(pExternalBufferInfo->handleType != 0);
1229
1230 /* All of the current flags are for sparse which we don't support yet.
1231 * Even when we do support it, doing sparse on external memory sounds
1232 * sketchy. Also, just disallowing flags is the safe option.
1233 */
1234 if (pExternalBufferInfo->flags)
1235 goto unsupported;
1236
1237 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1238
1239 switch (pExternalBufferInfo->handleType) {
1240 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1241 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1242 pExternalBufferProperties->externalMemoryProperties = prime_fd_props;
1243 return;
1244 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1245 pExternalBufferProperties->externalMemoryProperties = userptr_props;
1246 return;
1247 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
1248 if (physical_device->supported_extensions.ANDROID_external_memory_android_hardware_buffer) {
1249 pExternalBufferProperties->externalMemoryProperties = android_buffer_props;
1250 return;
1251 }
1252 /* fallthrough if ahw not supported */
1253 default:
1254 goto unsupported;
1255 }
1256
1257 unsupported:
1258 /* From the Vulkan 1.1.113 spec:
1259 *
1260 * compatibleHandleTypes must include at least handleType.
1261 */
1262 pExternalBufferProperties->externalMemoryProperties =
1263 (VkExternalMemoryProperties) {
1264 .compatibleHandleTypes = pExternalBufferInfo->handleType,
1265 };
1266 }
1267
1268 VkResult anv_CreateSamplerYcbcrConversion(
1269 VkDevice _device,
1270 const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
1271 const VkAllocationCallbacks* pAllocator,
1272 VkSamplerYcbcrConversion* pYcbcrConversion)
1273 {
1274 ANV_FROM_HANDLE(anv_device, device, _device);
1275 struct anv_ycbcr_conversion *conversion;
1276
1277 /* Search for VkExternalFormatANDROID and resolve the format. */
1278 struct anv_format *ext_format = NULL;
1279 const VkExternalFormatANDROID *ext_info =
1280 vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_FORMAT_ANDROID);
1281
1282 uint64_t format = ext_info ? ext_info->externalFormat : 0;
1283 if (format) {
1284 assert(pCreateInfo->format == VK_FORMAT_UNDEFINED);
1285 ext_format = (struct anv_format *) (uintptr_t) format;
1286 }
1287
1288 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO);
1289
1290 conversion = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*conversion), 8,
1291 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1292 if (!conversion)
1293 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1294
1295 memset(conversion, 0, sizeof(*conversion));
1296
1297 vk_object_base_init(&device->vk, &conversion->base,
1298 VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION);
1299 conversion->format = anv_get_format(pCreateInfo->format);
1300 conversion->ycbcr_model = pCreateInfo->ycbcrModel;
1301 conversion->ycbcr_range = pCreateInfo->ycbcrRange;
1302
1303 /* The Vulkan 1.1.95 spec says "When creating an external format conversion,
1304 * the value of components if ignored."
1305 */
1306 if (!ext_format) {
1307 conversion->mapping[0] = pCreateInfo->components.r;
1308 conversion->mapping[1] = pCreateInfo->components.g;
1309 conversion->mapping[2] = pCreateInfo->components.b;
1310 conversion->mapping[3] = pCreateInfo->components.a;
1311 }
1312
1313 conversion->chroma_offsets[0] = pCreateInfo->xChromaOffset;
1314 conversion->chroma_offsets[1] = pCreateInfo->yChromaOffset;
1315 conversion->chroma_filter = pCreateInfo->chromaFilter;
1316
1317 /* Setup external format. */
1318 if (ext_format)
1319 conversion->format = ext_format;
1320
1321 bool has_chroma_subsampled = false;
1322 for (uint32_t p = 0; p < conversion->format->n_planes; p++) {
1323 if (conversion->format->planes[p].has_chroma &&
1324 (conversion->format->planes[p].denominator_scales[0] > 1 ||
1325 conversion->format->planes[p].denominator_scales[1] > 1))
1326 has_chroma_subsampled = true;
1327 }
1328 conversion->chroma_reconstruction = has_chroma_subsampled &&
1329 (conversion->chroma_offsets[0] == VK_CHROMA_LOCATION_COSITED_EVEN ||
1330 conversion->chroma_offsets[1] == VK_CHROMA_LOCATION_COSITED_EVEN);
1331
1332 *pYcbcrConversion = anv_ycbcr_conversion_to_handle(conversion);
1333
1334 return VK_SUCCESS;
1335 }
1336
1337 void anv_DestroySamplerYcbcrConversion(
1338 VkDevice _device,
1339 VkSamplerYcbcrConversion YcbcrConversion,
1340 const VkAllocationCallbacks* pAllocator)
1341 {
1342 ANV_FROM_HANDLE(anv_device, device, _device);
1343 ANV_FROM_HANDLE(anv_ycbcr_conversion, conversion, YcbcrConversion);
1344
1345 if (!conversion)
1346 return;
1347
1348 vk_object_base_finish(&conversion->base);
1349 vk_free2(&device->vk.alloc, pAllocator, conversion);
1350 }