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