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