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