1 
2 /*
3  * Copyright © 2016 Red Hat.
4  * Copyright © 2016 Bas Nieuwenhuizen
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include "tu_private.h"
27 
28 #include "adreno_common.xml.h"
29 #include "a6xx.xml.h"
30 
31 #include "vk_format.h"
32 #include "vk_util.h"
33 #include "drm-uapi/drm_fourcc.h"
34 
35 #define TU6_FMT(vkfmt, hwfmt, swapfmt, valid) \
36    [VK_FORMAT_##vkfmt] = {                   \
37       .fmt = FMT6_##hwfmt,                     \
38       .swap = swapfmt,                       \
39       .supported = valid,                    \
40    }
41 
42 #define TU6_VTC(vk, fmt, swap) TU6_FMT(vk, fmt, swap, FMT_VERTEX | FMT_TEXTURE | FMT_COLOR)
43 #define TU6_xTC(vk, fmt, swap) TU6_FMT(vk, fmt, swap, FMT_TEXTURE | FMT_COLOR)
44 #define TU6_Vxx(vk, fmt, swap) TU6_FMT(vk, fmt, swap, FMT_VERTEX)
45 #define TU6_xTx(vk, fmt, swap) TU6_FMT(vk, fmt, swap, FMT_TEXTURE)
46 #define TU6_xxx(vk, fmt, swap) TU6_FMT(vk, NONE, WZYX, 0)
47 
48 static const struct tu_native_format tu6_format_table[] = {
49    TU6_xxx(UNDEFINED,                  x,                 x),    /* 0 */
50 
51    /* 8-bit packed */
52    TU6_xxx(R4G4_UNORM_PACK8,           4_4_UNORM,         WZXY), /* 1 */
53 
54    /* 16-bit packed */
55    TU6_xTC(R4G4B4A4_UNORM_PACK16,      4_4_4_4_UNORM,     XYZW), /* 2 */
56    TU6_xTC(B4G4R4A4_UNORM_PACK16,      4_4_4_4_UNORM,     ZYXW), /* 3 */
57    TU6_xTC(R5G6B5_UNORM_PACK16,        5_6_5_UNORM,       WXYZ), /* 4 */
58    TU6_xTC(B5G6R5_UNORM_PACK16,        5_6_5_UNORM,       WZYX), /* 5 */
59    TU6_xTC(R5G5B5A1_UNORM_PACK16,      5_5_5_1_UNORM,     XYZW), /* 6 */
60    TU6_xTC(B5G5R5A1_UNORM_PACK16,      5_5_5_1_UNORM,     ZYXW), /* 7 */
61    TU6_xTC(A1R5G5B5_UNORM_PACK16,      5_5_5_1_UNORM,     WXYZ), /* 8 */
62 
63    /* 8-bit R */
64    TU6_VTC(R8_UNORM,                   8_UNORM,           WZYX), /* 9 */
65    TU6_VTC(R8_SNORM,                   8_SNORM,           WZYX), /* 10 */
66    TU6_Vxx(R8_USCALED,                 8_UINT,            WZYX), /* 11 */
67    TU6_Vxx(R8_SSCALED,                 8_SINT,            WZYX), /* 12 */
68    TU6_VTC(R8_UINT,                    8_UINT,            WZYX), /* 13 */
69    TU6_VTC(R8_SINT,                    8_SINT,            WZYX), /* 14 */
70    TU6_xTC(R8_SRGB,                    8_UNORM,           WZYX), /* 15 */
71 
72    /* 16-bit RG */
73    TU6_VTC(R8G8_UNORM,                 8_8_UNORM,         WZYX), /* 16 */
74    TU6_VTC(R8G8_SNORM,                 8_8_SNORM,         WZYX), /* 17 */
75    TU6_Vxx(R8G8_USCALED,               8_8_UINT,          WZYX), /* 18 */
76    TU6_Vxx(R8G8_SSCALED,               8_8_SINT,          WZYX), /* 19 */
77    TU6_VTC(R8G8_UINT,                  8_8_UINT,          WZYX), /* 20 */
78    TU6_VTC(R8G8_SINT,                  8_8_SINT,          WZYX), /* 21 */
79    TU6_xTC(R8G8_SRGB,                  8_8_UNORM,         WZYX), /* 22 */
80 
81    /* 24-bit RGB */
82    TU6_Vxx(R8G8B8_UNORM,               8_8_8_UNORM,       WZYX), /* 23 */
83    TU6_Vxx(R8G8B8_SNORM,               8_8_8_SNORM,       WZYX), /* 24 */
84    TU6_Vxx(R8G8B8_USCALED,             8_8_8_UINT,        WZYX), /* 25 */
85    TU6_Vxx(R8G8B8_SSCALED,             8_8_8_SINT,        WZYX), /* 26 */
86    TU6_Vxx(R8G8B8_UINT,                8_8_8_UINT,        WZYX), /* 27 */
87    TU6_Vxx(R8G8B8_SINT,                8_8_8_SINT,        WZYX), /* 28 */
88    TU6_xxx(R8G8B8_SRGB,                8_8_8_UNORM,       WZYX), /* 29 */
89 
90    /* 24-bit BGR */
91    TU6_xxx(B8G8R8_UNORM,               8_8_8_UNORM,       WXYZ), /* 30 */
92    TU6_xxx(B8G8R8_SNORM,               8_8_8_SNORM,       WXYZ), /* 31 */
93    TU6_xxx(B8G8R8_USCALED,             8_8_8_UINT,        WXYZ), /* 32 */
94    TU6_xxx(B8G8R8_SSCALED,             8_8_8_SINT,        WXYZ), /* 33 */
95    TU6_xxx(B8G8R8_UINT,                8_8_8_UINT,        WXYZ), /* 34 */
96    TU6_xxx(B8G8R8_SINT,                8_8_8_SINT,        WXYZ), /* 35 */
97    TU6_xxx(B8G8R8_SRGB,                8_8_8_UNORM,       WXYZ), /* 36 */
98 
99    /* 32-bit RGBA */
100    TU6_VTC(R8G8B8A8_UNORM,             8_8_8_8_UNORM,     WZYX), /* 37 */
101    TU6_VTC(R8G8B8A8_SNORM,             8_8_8_8_SNORM,     WZYX), /* 38 */
102    TU6_Vxx(R8G8B8A8_USCALED,           8_8_8_8_UINT,      WZYX), /* 39 */
103    TU6_Vxx(R8G8B8A8_SSCALED,           8_8_8_8_SINT,      WZYX), /* 40 */
104    TU6_VTC(R8G8B8A8_UINT,              8_8_8_8_UINT,      WZYX), /* 41 */
105    TU6_VTC(R8G8B8A8_SINT,              8_8_8_8_SINT,      WZYX), /* 42 */
106    TU6_xTC(R8G8B8A8_SRGB,              8_8_8_8_UNORM,     WZYX), /* 43 */
107 
108    /* 32-bit BGRA */
109    TU6_VTC(B8G8R8A8_UNORM,             8_8_8_8_UNORM,     WXYZ), /* 44 */
110    TU6_VTC(B8G8R8A8_SNORM,             8_8_8_8_SNORM,     WXYZ), /* 45 */
111    TU6_Vxx(B8G8R8A8_USCALED,           8_8_8_8_UINT,      WXYZ), /* 46 */
112    TU6_Vxx(B8G8R8A8_SSCALED,           8_8_8_8_SINT,      WXYZ), /* 47 */
113    TU6_VTC(B8G8R8A8_UINT,              8_8_8_8_UINT,      WXYZ), /* 48 */
114    TU6_VTC(B8G8R8A8_SINT,              8_8_8_8_SINT,      WXYZ), /* 49 */
115    TU6_xTC(B8G8R8A8_SRGB,              8_8_8_8_UNORM,     WXYZ), /* 50 */
116 
117    /* 32-bit packed */
118    TU6_VTC(A8B8G8R8_UNORM_PACK32,      8_8_8_8_UNORM,     WZYX), /* 51 */
119    TU6_VTC(A8B8G8R8_SNORM_PACK32,      8_8_8_8_SNORM,     WZYX), /* 52 */
120    TU6_Vxx(A8B8G8R8_USCALED_PACK32,    8_8_8_8_UINT,      WZYX), /* 53 */
121    TU6_Vxx(A8B8G8R8_SSCALED_PACK32,    8_8_8_8_SINT,      WZYX), /* 54 */
122    TU6_VTC(A8B8G8R8_UINT_PACK32,       8_8_8_8_UINT,      WZYX), /* 55 */
123    TU6_VTC(A8B8G8R8_SINT_PACK32,       8_8_8_8_SINT,      WZYX), /* 56 */
124    TU6_xTC(A8B8G8R8_SRGB_PACK32,       8_8_8_8_UNORM,     WZYX), /* 57 */
125    TU6_VTC(A2R10G10B10_UNORM_PACK32,   10_10_10_2_UNORM,  WXYZ), /* 58 */
126    TU6_Vxx(A2R10G10B10_SNORM_PACK32,   10_10_10_2_SNORM,  WXYZ), /* 59 */
127    TU6_Vxx(A2R10G10B10_USCALED_PACK32, 10_10_10_2_UINT,   WXYZ), /* 60 */
128    TU6_Vxx(A2R10G10B10_SSCALED_PACK32, 10_10_10_2_SINT,   WXYZ), /* 61 */
129    TU6_VTC(A2R10G10B10_UINT_PACK32,    10_10_10_2_UINT,   WXYZ), /* 62 */
130    TU6_Vxx(A2R10G10B10_SINT_PACK32,    10_10_10_2_SINT,   WXYZ), /* 63 */
131    TU6_VTC(A2B10G10R10_UNORM_PACK32,   10_10_10_2_UNORM,  WZYX), /* 64 */
132    TU6_Vxx(A2B10G10R10_SNORM_PACK32,   10_10_10_2_SNORM,  WZYX), /* 65 */
133    TU6_Vxx(A2B10G10R10_USCALED_PACK32, 10_10_10_2_UINT,   WZYX), /* 66 */
134    TU6_Vxx(A2B10G10R10_SSCALED_PACK32, 10_10_10_2_SINT,   WZYX), /* 67 */
135    TU6_VTC(A2B10G10R10_UINT_PACK32,    10_10_10_2_UINT,   WZYX), /* 68 */
136    TU6_Vxx(A2B10G10R10_SINT_PACK32,    10_10_10_2_SINT,   WZYX), /* 69 */
137 
138    /* 16-bit R */
139    TU6_VTC(R16_UNORM,                  16_UNORM,          WZYX), /* 70 */
140    TU6_VTC(R16_SNORM,                  16_SNORM,          WZYX), /* 71 */
141    TU6_Vxx(R16_USCALED,                16_UINT,           WZYX), /* 72 */
142    TU6_Vxx(R16_SSCALED,                16_SINT,           WZYX), /* 73 */
143    TU6_VTC(R16_UINT,                   16_UINT,           WZYX), /* 74 */
144    TU6_VTC(R16_SINT,                   16_SINT,           WZYX), /* 75 */
145    TU6_VTC(R16_SFLOAT,                 16_FLOAT,          WZYX), /* 76 */
146 
147    /* 32-bit RG */
148    TU6_VTC(R16G16_UNORM,               16_16_UNORM,       WZYX), /* 77 */
149    TU6_VTC(R16G16_SNORM,               16_16_SNORM,       WZYX), /* 78 */
150    TU6_Vxx(R16G16_USCALED,             16_16_UINT,        WZYX), /* 79 */
151    TU6_Vxx(R16G16_SSCALED,             16_16_SINT,        WZYX), /* 80 */
152    TU6_VTC(R16G16_UINT,                16_16_UINT,        WZYX), /* 81 */
153    TU6_VTC(R16G16_SINT,                16_16_SINT,        WZYX), /* 82 */
154    TU6_VTC(R16G16_SFLOAT,              16_16_FLOAT,       WZYX), /* 83 */
155 
156    /* 48-bit RGB */
157    TU6_Vxx(R16G16B16_UNORM,            16_16_16_UNORM,    WZYX), /* 84 */
158    TU6_Vxx(R16G16B16_SNORM,            16_16_16_SNORM,    WZYX), /* 85 */
159    TU6_Vxx(R16G16B16_USCALED,          16_16_16_UINT,     WZYX), /* 86 */
160    TU6_Vxx(R16G16B16_SSCALED,          16_16_16_SINT,     WZYX), /* 87 */
161    TU6_Vxx(R16G16B16_UINT,             16_16_16_UINT,     WZYX), /* 88 */
162    TU6_Vxx(R16G16B16_SINT,             16_16_16_SINT,     WZYX), /* 89 */
163    TU6_Vxx(R16G16B16_SFLOAT,           16_16_16_FLOAT,    WZYX), /* 90 */
164 
165    /* 64-bit RGBA */
166    TU6_VTC(R16G16B16A16_UNORM,         16_16_16_16_UNORM, WZYX), /* 91 */
167    TU6_VTC(R16G16B16A16_SNORM,         16_16_16_16_SNORM, WZYX), /* 92 */
168    TU6_Vxx(R16G16B16A16_USCALED,       16_16_16_16_UINT,  WZYX), /* 93 */
169    TU6_Vxx(R16G16B16A16_SSCALED,       16_16_16_16_SINT,  WZYX), /* 94 */
170    TU6_VTC(R16G16B16A16_UINT,          16_16_16_16_UINT,  WZYX), /* 95 */
171    TU6_VTC(R16G16B16A16_SINT,          16_16_16_16_SINT,  WZYX), /* 96 */
172    TU6_VTC(R16G16B16A16_SFLOAT,        16_16_16_16_FLOAT, WZYX), /* 97 */
173 
174    /* 32-bit R */
175    TU6_VTC(R32_UINT,                   32_UINT,           WZYX), /* 98 */
176    TU6_VTC(R32_SINT,                   32_SINT,           WZYX), /* 99 */
177    TU6_VTC(R32_SFLOAT,                 32_FLOAT,          WZYX), /* 100 */
178 
179    /* 64-bit RG */
180    TU6_VTC(R32G32_UINT,                32_32_UINT,        WZYX), /* 101 */
181    TU6_VTC(R32G32_SINT,                32_32_SINT,        WZYX), /* 102 */
182    TU6_VTC(R32G32_SFLOAT,              32_32_FLOAT,       WZYX), /* 103 */
183 
184    /* 96-bit RGB */
185    TU6_Vxx(R32G32B32_UINT,             32_32_32_UINT,     WZYX), /* 104 */
186    TU6_Vxx(R32G32B32_SINT,             32_32_32_SINT,     WZYX), /* 105 */
187    TU6_Vxx(R32G32B32_SFLOAT,           32_32_32_FLOAT,    WZYX), /* 106 */
188 
189    /* 128-bit RGBA */
190    TU6_VTC(R32G32B32A32_UINT,          32_32_32_32_UINT,  WZYX), /* 107 */
191    TU6_VTC(R32G32B32A32_SINT,          32_32_32_32_SINT,  WZYX), /* 108 */
192    TU6_VTC(R32G32B32A32_SFLOAT,        32_32_32_32_FLOAT, WZYX), /* 109 */
193 
194    /* 64-bit R */
195    TU6_xxx(R64_UINT,                   64_UINT,           WZYX), /* 110 */
196    TU6_xxx(R64_SINT,                   64_SINT,           WZYX), /* 111 */
197    TU6_xxx(R64_SFLOAT,                 64_FLOAT,          WZYX), /* 112 */
198 
199    /* 128-bit RG */
200    TU6_xxx(R64G64_UINT,                64_64_UINT,        WZYX), /* 113 */
201    TU6_xxx(R64G64_SINT,                64_64_SINT,        WZYX), /* 114 */
202    TU6_xxx(R64G64_SFLOAT,              64_64_FLOAT,       WZYX), /* 115 */
203 
204    /* 192-bit RGB */
205    TU6_xxx(R64G64B64_UINT,             64_64_64_UINT,     WZYX), /* 116 */
206    TU6_xxx(R64G64B64_SINT,             64_64_64_SINT,     WZYX), /* 117 */
207    TU6_xxx(R64G64B64_SFLOAT,           64_64_64_FLOAT,    WZYX), /* 118 */
208 
209    /* 256-bit RGBA */
210    TU6_xxx(R64G64B64A64_UINT,          64_64_64_64_UINT,  WZYX), /* 119 */
211    TU6_xxx(R64G64B64A64_SINT,          64_64_64_64_SINT,  WZYX), /* 120 */
212    TU6_xxx(R64G64B64A64_SFLOAT,        64_64_64_64_FLOAT, WZYX), /* 121 */
213 
214    /* 32-bit packed float */
215    TU6_VTC(B10G11R11_UFLOAT_PACK32,    11_11_10_FLOAT,    WZYX), /* 122 */
216    TU6_xTx(E5B9G9R9_UFLOAT_PACK32,     9_9_9_E5_FLOAT,    WZYX), /* 123 */
217 
218    /* depth/stencil
219     * X8_D24_UNORM/D24_UNORM_S8_UINT should be Z24_UNORM_S8_UINT_AS_R8G8B8A8
220     * but the format doesn't work on A630 when UBWC is disabled, so use
221     * 8_8_8_8_UNORM as the default and override it when UBWC is enabled
222     */
223    TU6_xTC(D16_UNORM,                  16_UNORM,                      WZYX), /* 124 */
224    TU6_xTC(X8_D24_UNORM_PACK32,        8_8_8_8_UNORM,                 WZYX), /* 125 */
225    TU6_xTC(D32_SFLOAT,                 32_FLOAT,                      WZYX), /* 126 */
226    TU6_xTC(S8_UINT,                    8_UINT,                        WZYX), /* 127 */
227    TU6_xxx(D16_UNORM_S8_UINT,          X8Z16_UNORM,                   WZYX), /* 128 */
228    TU6_xTC(D24_UNORM_S8_UINT,          8_8_8_8_UNORM,                 WZYX), /* 129 */
229    TU6_xTC(D32_SFLOAT_S8_UINT,         NONE,                          WZYX), /* 130 */
230 
231    /* compressed */
232    TU6_xTx(BC1_RGB_UNORM_BLOCK,        DXT1,              WZYX), /* 131 */
233    TU6_xTx(BC1_RGB_SRGB_BLOCK,         DXT1,              WZYX), /* 132 */
234    TU6_xTx(BC1_RGBA_UNORM_BLOCK,       DXT1,              WZYX), /* 133 */
235    TU6_xTx(BC1_RGBA_SRGB_BLOCK,        DXT1,              WZYX), /* 134 */
236    TU6_xTx(BC2_UNORM_BLOCK,            DXT3,              WZYX), /* 135 */
237    TU6_xTx(BC2_SRGB_BLOCK,             DXT3,              WZYX), /* 136 */
238    TU6_xTx(BC3_UNORM_BLOCK,            DXT5,              WZYX), /* 137 */
239    TU6_xTx(BC3_SRGB_BLOCK,             DXT5,              WZYX), /* 138 */
240    TU6_xTx(BC4_UNORM_BLOCK,            RGTC1_UNORM,       WZYX), /* 139 */
241    TU6_xTx(BC4_SNORM_BLOCK,            RGTC1_SNORM,       WZYX), /* 140 */
242    TU6_xTx(BC5_UNORM_BLOCK,            RGTC2_UNORM,       WZYX), /* 141 */
243    TU6_xTx(BC5_SNORM_BLOCK,            RGTC2_SNORM,       WZYX), /* 142 */
244    TU6_xTx(BC6H_UFLOAT_BLOCK,          BPTC_UFLOAT,       WZYX), /* 143 */
245    TU6_xTx(BC6H_SFLOAT_BLOCK,          BPTC_FLOAT,        WZYX), /* 144 */
246    TU6_xTx(BC7_UNORM_BLOCK,            BPTC,              WZYX), /* 145 */
247    TU6_xTx(BC7_SRGB_BLOCK,             BPTC,              WZYX), /* 146 */
248    TU6_xTx(ETC2_R8G8B8_UNORM_BLOCK,    ETC2_RGB8,         WZYX), /* 147 */
249    TU6_xTx(ETC2_R8G8B8_SRGB_BLOCK,     ETC2_RGB8,         WZYX), /* 148 */
250    TU6_xTx(ETC2_R8G8B8A1_UNORM_BLOCK,  ETC2_RGB8A1,       WZYX), /* 149 */
251    TU6_xTx(ETC2_R8G8B8A1_SRGB_BLOCK,   ETC2_RGB8A1,       WZYX), /* 150 */
252    TU6_xTx(ETC2_R8G8B8A8_UNORM_BLOCK,  ETC2_RGBA8,        WZYX), /* 151 */
253    TU6_xTx(ETC2_R8G8B8A8_SRGB_BLOCK,   ETC2_RGBA8,        WZYX), /* 152 */
254    TU6_xTx(EAC_R11_UNORM_BLOCK,        ETC2_R11_UNORM,    WZYX), /* 153 */
255    TU6_xTx(EAC_R11_SNORM_BLOCK,        ETC2_R11_SNORM,    WZYX), /* 154 */
256    TU6_xTx(EAC_R11G11_UNORM_BLOCK,     ETC2_RG11_UNORM,   WZYX), /* 155 */
257    TU6_xTx(EAC_R11G11_SNORM_BLOCK,     ETC2_RG11_SNORM,   WZYX), /* 156 */
258    TU6_xTx(ASTC_4x4_UNORM_BLOCK,       ASTC_4x4,          WZYX), /* 157 */
259    TU6_xTx(ASTC_4x4_SRGB_BLOCK,        ASTC_4x4,          WZYX), /* 158 */
260    TU6_xTx(ASTC_5x4_UNORM_BLOCK,       ASTC_5x4,          WZYX), /* 159 */
261    TU6_xTx(ASTC_5x4_SRGB_BLOCK,        ASTC_5x4,          WZYX), /* 160 */
262    TU6_xTx(ASTC_5x5_UNORM_BLOCK,       ASTC_5x5,          WZYX), /* 161 */
263    TU6_xTx(ASTC_5x5_SRGB_BLOCK,        ASTC_5x5,          WZYX), /* 162 */
264    TU6_xTx(ASTC_6x5_UNORM_BLOCK,       ASTC_6x5,          WZYX), /* 163 */
265    TU6_xTx(ASTC_6x5_SRGB_BLOCK,        ASTC_6x5,          WZYX), /* 164 */
266    TU6_xTx(ASTC_6x6_UNORM_BLOCK,       ASTC_6x6,          WZYX), /* 165 */
267    TU6_xTx(ASTC_6x6_SRGB_BLOCK,        ASTC_6x6,          WZYX), /* 166 */
268    TU6_xTx(ASTC_8x5_UNORM_BLOCK,       ASTC_8x5,          WZYX), /* 167 */
269    TU6_xTx(ASTC_8x5_SRGB_BLOCK,        ASTC_8x5,          WZYX), /* 168 */
270    TU6_xTx(ASTC_8x6_UNORM_BLOCK,       ASTC_8x6,          WZYX), /* 169 */
271    TU6_xTx(ASTC_8x6_SRGB_BLOCK,        ASTC_8x6,          WZYX), /* 170 */
272    TU6_xTx(ASTC_8x8_UNORM_BLOCK,       ASTC_8x8,          WZYX), /* 171 */
273    TU6_xTx(ASTC_8x8_SRGB_BLOCK,        ASTC_8x8,          WZYX), /* 172 */
274    TU6_xTx(ASTC_10x5_UNORM_BLOCK,      ASTC_10x5,         WZYX), /* 173 */
275    TU6_xTx(ASTC_10x5_SRGB_BLOCK,       ASTC_10x5,         WZYX), /* 174 */
276    TU6_xTx(ASTC_10x6_UNORM_BLOCK,      ASTC_10x6,         WZYX), /* 175 */
277    TU6_xTx(ASTC_10x6_SRGB_BLOCK,       ASTC_10x6,         WZYX), /* 176 */
278    TU6_xTx(ASTC_10x8_UNORM_BLOCK,      ASTC_10x8,         WZYX), /* 177 */
279    TU6_xTx(ASTC_10x8_SRGB_BLOCK,       ASTC_10x8,         WZYX), /* 178 */
280    TU6_xTx(ASTC_10x10_UNORM_BLOCK,     ASTC_10x10,        WZYX), /* 179 */
281    TU6_xTx(ASTC_10x10_SRGB_BLOCK,      ASTC_10x10,        WZYX), /* 180 */
282    TU6_xTx(ASTC_12x10_UNORM_BLOCK,     ASTC_12x10,        WZYX), /* 181 */
283    TU6_xTx(ASTC_12x10_SRGB_BLOCK,      ASTC_12x10,        WZYX), /* 182 */
284    TU6_xTx(ASTC_12x12_UNORM_BLOCK,     ASTC_12x12,        WZYX), /* 183 */
285    TU6_xTx(ASTC_12x12_SRGB_BLOCK,      ASTC_12x12,        WZYX), /* 184 */
286 };
287 
288 #undef TU6_FMT
289 #define TU6_FMT(vkfmt, hwfmt, swapfmt, valid)   \
290    case VK_FORMAT_##vkfmt:                      \
291       fmt = (struct tu_native_format) {         \
292          .fmt = FMT6_##hwfmt,                   \
293          .swap = swapfmt,                       \
294          .supported = valid,                    \
295       }; break;
296 
297 static struct tu_native_format
tu6_get_native_format(VkFormat format)298 tu6_get_native_format(VkFormat format)
299 {
300    struct tu_native_format fmt = {};
301 
302    if (format < ARRAY_SIZE(tu6_format_table)) {
303       fmt = tu6_format_table[format];
304    } else {
305       switch (format) {
306       TU6_xTx(G8B8G8R8_422_UNORM,         R8G8R8B8_422_UNORM,        WZYX)
307       TU6_xTx(B8G8R8G8_422_UNORM,         G8R8B8R8_422_UNORM,        WZYX)
308       TU6_xTx(G8_B8_R8_3PLANE_420_UNORM,  R8_G8_B8_3PLANE_420_UNORM, WZYX)
309       TU6_xTx(G8_B8R8_2PLANE_420_UNORM,   R8_G8B8_2PLANE_420_UNORM,  WZYX)
310       TU6_xTC(A4R4G4B4_UNORM_PACK16_EXT,  4_4_4_4_UNORM,             WXYZ)
311       TU6_xTC(A4B4G4R4_UNORM_PACK16_EXT,  4_4_4_4_UNORM,             WZYX)
312       default:
313          break;
314       }
315    }
316 
317    if (fmt.supported && vk_format_to_pipe_format(format) == PIPE_FORMAT_NONE) {
318       tu_finishme("vk_format %d missing matching pipe format.\n", format);
319       fmt.supported = false;
320    }
321 
322    return fmt;
323 }
324 
325 struct tu_native_format
tu6_format_vtx(VkFormat format)326 tu6_format_vtx(VkFormat format)
327 {
328    struct tu_native_format fmt = tu6_get_native_format(format);
329    assert(fmt.supported & FMT_VERTEX);
330    return fmt;
331 }
332 
333 struct tu_native_format
tu6_format_color(VkFormat format,enum a6xx_tile_mode tile_mode)334 tu6_format_color(VkFormat format, enum a6xx_tile_mode tile_mode)
335 {
336    struct tu_native_format fmt = tu6_get_native_format(format);
337    assert(fmt.supported & FMT_COLOR);
338 
339    if (fmt.fmt == FMT6_10_10_10_2_UNORM)
340       fmt.fmt = FMT6_10_10_10_2_UNORM_DEST;
341 
342    if (tile_mode)
343       fmt.swap = WZYX;
344 
345    return fmt;
346 }
347 
348 struct tu_native_format
tu6_format_texture(VkFormat format,enum a6xx_tile_mode tile_mode)349 tu6_format_texture(VkFormat format, enum a6xx_tile_mode tile_mode)
350 {
351    struct tu_native_format fmt = tu6_get_native_format(format);
352    assert(fmt.supported & FMT_TEXTURE);
353 
354    if (!tile_mode) {
355       /* different from format table when used as linear src */
356       if (format == VK_FORMAT_R5G5B5A1_UNORM_PACK16)
357          fmt.fmt = FMT6_1_5_5_5_UNORM, fmt.swap = WXYZ;
358       if (format == VK_FORMAT_B5G5R5A1_UNORM_PACK16)
359          fmt.fmt = FMT6_1_5_5_5_UNORM, fmt.swap = WZYX;
360    } else {
361       fmt.swap = WZYX;
362    }
363 
364    return fmt;
365 }
366 
367 static void
tu_physical_device_get_format_properties(struct tu_physical_device * physical_device,VkFormat format,VkFormatProperties * out_properties)368 tu_physical_device_get_format_properties(
369    struct tu_physical_device *physical_device,
370    VkFormat format,
371    VkFormatProperties *out_properties)
372 {
373    VkFormatFeatureFlags linear = 0, optimal = 0, buffer = 0;
374    const struct util_format_description *desc = vk_format_description(format);
375    const struct tu_native_format native_fmt = tu6_get_native_format(format);
376    if (!desc || !native_fmt.supported) {
377       goto end;
378    }
379 
380    buffer |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
381    if (native_fmt.supported & FMT_VERTEX)
382       buffer |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
383 
384    if (native_fmt.supported & FMT_TEXTURE) {
385       optimal |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
386                  VK_FORMAT_FEATURE_TRANSFER_DST_BIT |
387                  VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
388                  VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT |
389                  VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT |
390                  VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT |
391                  VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT;
392 
393       buffer |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT;
394 
395       /* no blit src bit for YUYV/NV12/I420 formats */
396       if (desc->layout != UTIL_FORMAT_LAYOUT_SUBSAMPLED &&
397           desc->layout != UTIL_FORMAT_LAYOUT_PLANAR2 &&
398           desc->layout != UTIL_FORMAT_LAYOUT_PLANAR3)
399          optimal |= VK_FORMAT_FEATURE_BLIT_SRC_BIT;
400 
401       if (desc->layout != UTIL_FORMAT_LAYOUT_SUBSAMPLED)
402          optimal |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT;
403 
404       if (physical_device->supported_extensions.EXT_filter_cubic)
405          optimal |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_EXT;
406    }
407 
408    if (native_fmt.supported & FMT_COLOR) {
409       assert(native_fmt.supported & FMT_TEXTURE);
410       optimal |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
411                  VK_FORMAT_FEATURE_BLIT_DST_BIT;
412 
413       /* IBO's don't have a swap field at all, so swapped formats can't be
414        * supported, even with linear images.
415        *
416        * TODO: See if setting the swap field from the tex descriptor works,
417        * after we enable shaderStorageImageReadWithoutFormat and there are
418        * tests for these formats.
419        */
420       if (native_fmt.swap == WZYX) {
421          optimal |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
422          buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
423       }
424 
425       /* TODO: The blob also exposes these for R16G16_UINT/R16G16_SINT, but we
426        * don't have any tests for those.
427        */
428       if (format == VK_FORMAT_R32_UINT || format == VK_FORMAT_R32_SINT) {
429          optimal |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
430          buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
431       }
432 
433       if (vk_format_is_float(format) ||
434           vk_format_is_unorm(format) ||
435           vk_format_is_snorm(format) ||
436           vk_format_is_srgb(format)) {
437          optimal |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
438       }
439    }
440 
441    /* For the most part, we can do anything with a linear image that we could
442     * do with a tiled image. However, we can't support sysmem rendering with a
443     * linear depth texture, because we don't know if there's a bit to control
444     * the tiling of the depth buffer in BYPASS mode, and the blob also
445     * disables linear depth rendering, so there's no way to discover it. We
446     * also can't force GMEM mode, because there are other situations where we
447     * have to use sysmem rendering. So follow the blob here, and only enable
448     * DEPTH_STENCIL_ATTACHMENT_BIT for the optimal features.
449     */
450    linear = optimal;
451    if (tu6_pipe2depth(format) != (enum a6xx_depth_format)~0)
452       optimal |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
453 
454    /* D32_SFLOAT_S8_UINT is tiled as two images, so no linear format
455     * blob enables some linear features, but its not useful, so don't bother.
456     */
457    if (format == VK_FORMAT_D32_SFLOAT_S8_UINT)
458       linear = 0;
459 
460 end:
461    out_properties->linearTilingFeatures = linear;
462    out_properties->optimalTilingFeatures = optimal;
463    out_properties->bufferFeatures = buffer;
464 }
465 
466 void
tu_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties * pFormatProperties)467 tu_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
468                                      VkFormat format,
469                                      VkFormatProperties *pFormatProperties)
470 {
471    TU_FROM_HANDLE(tu_physical_device, physical_device, physicalDevice);
472 
473    tu_physical_device_get_format_properties(physical_device, format,
474                                             pFormatProperties);
475 }
476 
477 void
tu_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties2 * pFormatProperties)478 tu_GetPhysicalDeviceFormatProperties2(
479    VkPhysicalDevice physicalDevice,
480    VkFormat format,
481    VkFormatProperties2 *pFormatProperties)
482 {
483    TU_FROM_HANDLE(tu_physical_device, physical_device, physicalDevice);
484 
485    tu_physical_device_get_format_properties(
486       physical_device, format, &pFormatProperties->formatProperties);
487 
488    VkDrmFormatModifierPropertiesListEXT *list =
489       vk_find_struct(pFormatProperties->pNext, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT);
490    if (list) {
491       VK_OUTARRAY_MAKE(out, list->pDrmFormatModifierProperties,
492                        &list->drmFormatModifierCount);
493 
494       vk_outarray_append(&out, mod_props) {
495          mod_props->drmFormatModifier = DRM_FORMAT_MOD_LINEAR;
496          mod_props->drmFormatModifierPlaneCount = 1;
497       }
498 
499       /* TODO: any cases where this should be disabled? */
500       vk_outarray_append(&out, mod_props) {
501          mod_props->drmFormatModifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
502          mod_props->drmFormatModifierPlaneCount = 1;
503       }
504    }
505 }
506 
507 static VkResult
tu_get_image_format_properties(struct tu_physical_device * physical_device,const VkPhysicalDeviceImageFormatInfo2 * info,VkImageFormatProperties * pImageFormatProperties,VkFormatFeatureFlags * p_feature_flags)508 tu_get_image_format_properties(
509    struct tu_physical_device *physical_device,
510    const VkPhysicalDeviceImageFormatInfo2 *info,
511    VkImageFormatProperties *pImageFormatProperties,
512    VkFormatFeatureFlags *p_feature_flags)
513 {
514    VkFormatProperties format_props;
515    VkFormatFeatureFlags format_feature_flags;
516    VkExtent3D maxExtent;
517    uint32_t maxMipLevels;
518    uint32_t maxArraySize;
519    VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
520 
521    tu_physical_device_get_format_properties(physical_device, info->format,
522                                             &format_props);
523 
524    switch (info->tiling) {
525    case VK_IMAGE_TILING_LINEAR:
526       format_feature_flags = format_props.linearTilingFeatures;
527       break;
528 
529    case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT:
530       /* The only difference between optimal and linear is currently whether
531        * depth/stencil attachments are allowed on depth/stencil formats.
532        * There's no reason to allow importing depth/stencil textures, so just
533        * disallow it and then this annoying edge case goes away.
534        *
535        * TODO: If anyone cares, we could enable this by looking at the
536        * modifier and checking if it's LINEAR or not.
537        */
538       if (vk_format_is_depth_or_stencil(info->format))
539          goto unsupported;
540 
541       assert(format_props.optimalTilingFeatures == format_props.linearTilingFeatures);
542       /* fallthrough */
543    case VK_IMAGE_TILING_OPTIMAL:
544       format_feature_flags = format_props.optimalTilingFeatures;
545       break;
546    default:
547       unreachable("bad VkPhysicalDeviceImageFormatInfo2");
548    }
549 
550    if (format_feature_flags == 0)
551       goto unsupported;
552 
553    if (info->type != VK_IMAGE_TYPE_2D &&
554        vk_format_is_depth_or_stencil(info->format))
555       goto unsupported;
556 
557    switch (info->type) {
558    default:
559       unreachable("bad vkimage type\n");
560    case VK_IMAGE_TYPE_1D:
561       maxExtent.width = 16384;
562       maxExtent.height = 1;
563       maxExtent.depth = 1;
564       maxMipLevels = 15; /* log2(maxWidth) + 1 */
565       maxArraySize = 2048;
566       break;
567    case VK_IMAGE_TYPE_2D:
568       maxExtent.width = 16384;
569       maxExtent.height = 16384;
570       maxExtent.depth = 1;
571       maxMipLevels = 15; /* log2(maxWidth) + 1 */
572       maxArraySize = 2048;
573       break;
574    case VK_IMAGE_TYPE_3D:
575       maxExtent.width = 2048;
576       maxExtent.height = 2048;
577       maxExtent.depth = 2048;
578       maxMipLevels = 12; /* log2(maxWidth) + 1 */
579       maxArraySize = 1;
580       break;
581    }
582 
583    if (info->tiling == VK_IMAGE_TILING_OPTIMAL &&
584        info->type == VK_IMAGE_TYPE_2D &&
585        (format_feature_flags &
586         (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
587          VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
588        !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
589        !(info->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
590       sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
591       /* note: most operations support 8 samples (GMEM render/resolve do at least)
592        * but some do not (which ones?), just disable 8 samples completely,
593        * (no 8x msaa matches the blob driver behavior)
594        */
595    }
596 
597    if (info->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
598       if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
599          goto unsupported;
600       }
601    }
602 
603    if (info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
604       if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
605          goto unsupported;
606       }
607    }
608 
609    if (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
610       if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
611          goto unsupported;
612       }
613    }
614 
615    if (info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
616       if (!(format_feature_flags &
617             VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
618          goto unsupported;
619       }
620    }
621 
622    *pImageFormatProperties = (VkImageFormatProperties) {
623       .maxExtent = maxExtent,
624       .maxMipLevels = maxMipLevels,
625       .maxArrayLayers = maxArraySize,
626       .sampleCounts = sampleCounts,
627 
628       /* FINISHME: Accurately calculate
629        * VkImageFormatProperties::maxResourceSize.
630        */
631       .maxResourceSize = UINT32_MAX,
632    };
633 
634    if (p_feature_flags)
635       *p_feature_flags = format_feature_flags;
636 
637    return VK_SUCCESS;
638 unsupported:
639    *pImageFormatProperties = (VkImageFormatProperties) {
640       .maxExtent = { 0, 0, 0 },
641       .maxMipLevels = 0,
642       .maxArrayLayers = 0,
643       .sampleCounts = 0,
644       .maxResourceSize = 0,
645    };
646 
647    return VK_ERROR_FORMAT_NOT_SUPPORTED;
648 }
649 
650 VkResult
tu_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,VkImageTiling tiling,VkImageUsageFlags usage,VkImageCreateFlags createFlags,VkImageFormatProperties * pImageFormatProperties)651 tu_GetPhysicalDeviceImageFormatProperties(
652    VkPhysicalDevice physicalDevice,
653    VkFormat format,
654    VkImageType type,
655    VkImageTiling tiling,
656    VkImageUsageFlags usage,
657    VkImageCreateFlags createFlags,
658    VkImageFormatProperties *pImageFormatProperties)
659 {
660    TU_FROM_HANDLE(tu_physical_device, physical_device, physicalDevice);
661 
662    const VkPhysicalDeviceImageFormatInfo2 info = {
663       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
664       .pNext = NULL,
665       .format = format,
666       .type = type,
667       .tiling = tiling,
668       .usage = usage,
669       .flags = createFlags,
670    };
671 
672    return tu_get_image_format_properties(physical_device, &info,
673                                          pImageFormatProperties, NULL);
674 }
675 
676 static VkResult
tu_get_external_image_format_properties(const struct tu_physical_device * physical_device,const VkPhysicalDeviceImageFormatInfo2 * pImageFormatInfo,VkExternalMemoryHandleTypeFlagBits handleType,VkExternalMemoryProperties * external_properties)677 tu_get_external_image_format_properties(
678    const struct tu_physical_device *physical_device,
679    const VkPhysicalDeviceImageFormatInfo2 *pImageFormatInfo,
680    VkExternalMemoryHandleTypeFlagBits handleType,
681    VkExternalMemoryProperties *external_properties)
682 {
683    VkExternalMemoryFeatureFlagBits flags = 0;
684    VkExternalMemoryHandleTypeFlags export_flags = 0;
685    VkExternalMemoryHandleTypeFlags compat_flags = 0;
686 
687    /* From the Vulkan 1.1.98 spec:
688     *
689     *    If handleType is not compatible with the format, type, tiling,
690     *    usage, and flags specified in VkPhysicalDeviceImageFormatInfo2,
691     *    then vkGetPhysicalDeviceImageFormatProperties2 returns
692     *    VK_ERROR_FORMAT_NOT_SUPPORTED.
693     */
694 
695    switch (handleType) {
696    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
697    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
698       switch (pImageFormatInfo->type) {
699       case VK_IMAGE_TYPE_2D:
700          flags = VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT |
701                  VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
702                  VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
703          compat_flags = export_flags =
704             VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
705             VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
706          break;
707       default:
708          return vk_errorf(physical_device->instance, VK_ERROR_FORMAT_NOT_SUPPORTED,
709                           "VkExternalMemoryTypeFlagBits(0x%x) unsupported for VkImageType(%d)",
710                           handleType, pImageFormatInfo->type);
711       }
712       break;
713    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
714       flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
715       compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
716       break;
717    default:
718       return vk_errorf(physical_device->instance, VK_ERROR_FORMAT_NOT_SUPPORTED,
719                        "VkExternalMemoryTypeFlagBits(0x%x) unsupported",
720                        handleType);
721    }
722 
723    *external_properties = (VkExternalMemoryProperties) {
724       .externalMemoryFeatures = flags,
725       .exportFromImportedHandleTypes = export_flags,
726       .compatibleHandleTypes = compat_flags,
727    };
728 
729    return VK_SUCCESS;
730 }
731 
732 VkResult
tu_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceImageFormatInfo2 * base_info,VkImageFormatProperties2 * base_props)733 tu_GetPhysicalDeviceImageFormatProperties2(
734    VkPhysicalDevice physicalDevice,
735    const VkPhysicalDeviceImageFormatInfo2 *base_info,
736    VkImageFormatProperties2 *base_props)
737 {
738    TU_FROM_HANDLE(tu_physical_device, physical_device, physicalDevice);
739    const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
740    const VkPhysicalDeviceImageViewImageFormatInfoEXT *image_view_info = NULL;
741    VkExternalImageFormatProperties *external_props = NULL;
742    VkFilterCubicImageViewImageFormatPropertiesEXT *cubic_props = NULL;
743    VkFormatFeatureFlags format_feature_flags;
744    VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;
745    VkResult result;
746 
747    result = tu_get_image_format_properties(physical_device,
748       base_info, &base_props->imageFormatProperties, &format_feature_flags);
749    if (result != VK_SUCCESS)
750       return result;
751 
752    /* Extract input structs */
753    vk_foreach_struct_const(s, base_info->pNext)
754    {
755       switch (s->sType) {
756       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
757          external_info = (const void *) s;
758          break;
759       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_VIEW_IMAGE_FORMAT_INFO_EXT:
760          image_view_info = (const void *) s;
761          break;
762       default:
763          break;
764       }
765    }
766 
767    /* Extract output structs */
768    vk_foreach_struct(s, base_props->pNext)
769    {
770       switch (s->sType) {
771       case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
772          external_props = (void *) s;
773          break;
774       case VK_STRUCTURE_TYPE_FILTER_CUBIC_IMAGE_VIEW_IMAGE_FORMAT_PROPERTIES_EXT:
775          cubic_props = (void *) s;
776          break;
777       case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:
778          ycbcr_props = (void *) s;
779          break;
780       default:
781          break;
782       }
783    }
784 
785    /* From the Vulkan 1.0.42 spec:
786     *
787     *    If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
788     *    behave as if VkPhysicalDeviceExternalImageFormatInfo was not
789     *    present and VkExternalImageFormatProperties will be ignored.
790     */
791    if (external_info && external_info->handleType != 0) {
792       result = tu_get_external_image_format_properties(
793          physical_device, base_info, external_info->handleType,
794          &external_props->externalMemoryProperties);
795       if (result != VK_SUCCESS)
796          goto fail;
797    }
798 
799    if (cubic_props) {
800       /* note: blob only allows cubic filtering for 2D and 2D array views
801        * its likely we can enable it for 1D and CUBE, needs testing however
802        */
803       if ((image_view_info->imageViewType == VK_IMAGE_VIEW_TYPE_2D ||
804            image_view_info->imageViewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY) &&
805           (format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_EXT)) {
806          cubic_props->filterCubic = true;
807          cubic_props->filterCubicMinmax = true;
808       } else {
809          cubic_props->filterCubic = false;
810          cubic_props->filterCubicMinmax = false;
811       }
812    }
813 
814    if (ycbcr_props)
815       ycbcr_props->combinedImageSamplerDescriptorCount = 1;
816 
817    return VK_SUCCESS;
818 
819 fail:
820    if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
821       /* From the Vulkan 1.0.42 spec:
822        *
823        *    If the combination of parameters to
824        *    vkGetPhysicalDeviceImageFormatProperties2 is not supported by
825        *    the implementation for use in vkCreateImage, then all members of
826        *    imageFormatProperties will be filled with zero.
827        */
828       base_props->imageFormatProperties = (VkImageFormatProperties) {};
829    }
830 
831    return result;
832 }
833 
834 void
tu_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,uint32_t samples,VkImageUsageFlags usage,VkImageTiling tiling,uint32_t * pNumProperties,VkSparseImageFormatProperties * pProperties)835 tu_GetPhysicalDeviceSparseImageFormatProperties(
836    VkPhysicalDevice physicalDevice,
837    VkFormat format,
838    VkImageType type,
839    uint32_t samples,
840    VkImageUsageFlags usage,
841    VkImageTiling tiling,
842    uint32_t *pNumProperties,
843    VkSparseImageFormatProperties *pProperties)
844 {
845    /* Sparse images are not yet supported. */
846    *pNumProperties = 0;
847 }
848 
849 void
tu_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSparseImageFormatInfo2 * pFormatInfo,uint32_t * pPropertyCount,VkSparseImageFormatProperties2 * pProperties)850 tu_GetPhysicalDeviceSparseImageFormatProperties2(
851    VkPhysicalDevice physicalDevice,
852    const VkPhysicalDeviceSparseImageFormatInfo2 *pFormatInfo,
853    uint32_t *pPropertyCount,
854    VkSparseImageFormatProperties2 *pProperties)
855 {
856    /* Sparse images are not yet supported. */
857    *pPropertyCount = 0;
858 }
859 
860 void
tu_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalBufferInfo * pExternalBufferInfo,VkExternalBufferProperties * pExternalBufferProperties)861 tu_GetPhysicalDeviceExternalBufferProperties(
862    VkPhysicalDevice physicalDevice,
863    const VkPhysicalDeviceExternalBufferInfo *pExternalBufferInfo,
864    VkExternalBufferProperties *pExternalBufferProperties)
865 {
866    VkExternalMemoryFeatureFlagBits flags = 0;
867    VkExternalMemoryHandleTypeFlags export_flags = 0;
868    VkExternalMemoryHandleTypeFlags compat_flags = 0;
869    switch (pExternalBufferInfo->handleType) {
870    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
871    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
872       flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
873               VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
874       compat_flags = export_flags =
875          VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
876          VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
877       break;
878    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
879       flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
880       compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
881       break;
882    default:
883       break;
884    }
885    pExternalBufferProperties->externalMemoryProperties =
886       (VkExternalMemoryProperties) {
887          .externalMemoryFeatures = flags,
888          .exportFromImportedHandleTypes = export_flags,
889          .compatibleHandleTypes = compat_flags,
890       };
891 }
892