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