• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "anv_private.h"
25 #include "vk_format_info.h"
26 
27 /*
28  * gcc-4 and earlier don't allow compound literals where a constant
29  * is required in -std=c99/gnu99 mode, so we can't use ISL_SWIZZLE()
30  * here. -std=c89/gnu89 would allow it, but we depend on c99 features
31  * so using -std=c89/gnu89 is not an option. Starting from gcc-5
32  * compound literals can also be considered constant in -std=c99/gnu99
33  * mode.
34  */
35 #define _ISL_SWIZZLE(r, g, b, a) { \
36       ISL_CHANNEL_SELECT_##r, \
37       ISL_CHANNEL_SELECT_##g, \
38       ISL_CHANNEL_SELECT_##b, \
39       ISL_CHANNEL_SELECT_##a, \
40 }
41 
42 #define RGBA _ISL_SWIZZLE(RED, GREEN, BLUE, ALPHA)
43 #define BGRA _ISL_SWIZZLE(BLUE, GREEN, RED, ALPHA)
44 #define RGB1 _ISL_SWIZZLE(RED, GREEN, BLUE, ONE)
45 
46 #define swiz_fmt(__vk_fmt, __hw_fmt, __swizzle)     \
47    [__vk_fmt] = { \
48       .isl_format = __hw_fmt, \
49       .swizzle = __swizzle, \
50    }
51 
52 #define fmt(__vk_fmt, __hw_fmt) \
53    swiz_fmt(__vk_fmt, __hw_fmt, RGBA)
54 
55 /* HINT: For array formats, the ISL name should match the VK name.  For
56  * packed formats, they should have the channels in reverse order from each
57  * other.  The reason for this is that, for packed formats, the ISL (and
58  * bspec) names are in LSB -> MSB order while VK formats are MSB -> LSB.
59  */
60 static const struct anv_format anv_formats[] = {
61    fmt(VK_FORMAT_UNDEFINED,               ISL_FORMAT_UNSUPPORTED),
62    fmt(VK_FORMAT_R4G4_UNORM_PACK8,        ISL_FORMAT_UNSUPPORTED),
63    fmt(VK_FORMAT_R4G4B4A4_UNORM_PACK16,   ISL_FORMAT_A4B4G4R4_UNORM),
64    swiz_fmt(VK_FORMAT_B4G4R4A4_UNORM_PACK16,   ISL_FORMAT_A4B4G4R4_UNORM,  BGRA),
65    fmt(VK_FORMAT_R5G6B5_UNORM_PACK16,     ISL_FORMAT_B5G6R5_UNORM),
66    swiz_fmt(VK_FORMAT_B5G6R5_UNORM_PACK16,     ISL_FORMAT_B5G6R5_UNORM, BGRA),
67    fmt(VK_FORMAT_R5G5B5A1_UNORM_PACK16,   ISL_FORMAT_A1B5G5R5_UNORM),
68    fmt(VK_FORMAT_B5G5R5A1_UNORM_PACK16,   ISL_FORMAT_UNSUPPORTED),
69    fmt(VK_FORMAT_A1R5G5B5_UNORM_PACK16,   ISL_FORMAT_B5G5R5A1_UNORM),
70    fmt(VK_FORMAT_R8_UNORM,                ISL_FORMAT_R8_UNORM),
71    fmt(VK_FORMAT_R8_SNORM,                ISL_FORMAT_R8_SNORM),
72    fmt(VK_FORMAT_R8_USCALED,              ISL_FORMAT_R8_USCALED),
73    fmt(VK_FORMAT_R8_SSCALED,              ISL_FORMAT_R8_SSCALED),
74    fmt(VK_FORMAT_R8_UINT,                 ISL_FORMAT_R8_UINT),
75    fmt(VK_FORMAT_R8_SINT,                 ISL_FORMAT_R8_SINT),
76    fmt(VK_FORMAT_R8_SRGB,                 ISL_FORMAT_UNSUPPORTED),
77    fmt(VK_FORMAT_R8G8_UNORM,              ISL_FORMAT_R8G8_UNORM),
78    fmt(VK_FORMAT_R8G8_SNORM,              ISL_FORMAT_R8G8_SNORM),
79    fmt(VK_FORMAT_R8G8_USCALED,            ISL_FORMAT_R8G8_USCALED),
80    fmt(VK_FORMAT_R8G8_SSCALED,            ISL_FORMAT_R8G8_SSCALED),
81    fmt(VK_FORMAT_R8G8_UINT,               ISL_FORMAT_R8G8_UINT),
82    fmt(VK_FORMAT_R8G8_SINT,               ISL_FORMAT_R8G8_SINT),
83    fmt(VK_FORMAT_R8G8_SRGB,               ISL_FORMAT_UNSUPPORTED), /* L8A8_UNORM_SRGB */
84    fmt(VK_FORMAT_R8G8B8_UNORM,            ISL_FORMAT_R8G8B8_UNORM),
85    fmt(VK_FORMAT_R8G8B8_SNORM,            ISL_FORMAT_R8G8B8_SNORM),
86    fmt(VK_FORMAT_R8G8B8_USCALED,          ISL_FORMAT_R8G8B8_USCALED),
87    fmt(VK_FORMAT_R8G8B8_SSCALED,          ISL_FORMAT_R8G8B8_SSCALED),
88    fmt(VK_FORMAT_R8G8B8_UINT,             ISL_FORMAT_R8G8B8_UINT),
89    fmt(VK_FORMAT_R8G8B8_SINT,             ISL_FORMAT_R8G8B8_SINT),
90    fmt(VK_FORMAT_R8G8B8_SRGB,             ISL_FORMAT_R8G8B8_UNORM_SRGB),
91    fmt(VK_FORMAT_R8G8B8A8_UNORM,          ISL_FORMAT_R8G8B8A8_UNORM),
92    fmt(VK_FORMAT_R8G8B8A8_SNORM,          ISL_FORMAT_R8G8B8A8_SNORM),
93    fmt(VK_FORMAT_R8G8B8A8_USCALED,        ISL_FORMAT_R8G8B8A8_USCALED),
94    fmt(VK_FORMAT_R8G8B8A8_SSCALED,        ISL_FORMAT_R8G8B8A8_SSCALED),
95    fmt(VK_FORMAT_R8G8B8A8_UINT,           ISL_FORMAT_R8G8B8A8_UINT),
96    fmt(VK_FORMAT_R8G8B8A8_SINT,           ISL_FORMAT_R8G8B8A8_SINT),
97    fmt(VK_FORMAT_R8G8B8A8_SRGB,           ISL_FORMAT_R8G8B8A8_UNORM_SRGB),
98    fmt(VK_FORMAT_A8B8G8R8_UNORM_PACK32,   ISL_FORMAT_R8G8B8A8_UNORM),
99    fmt(VK_FORMAT_A8B8G8R8_SNORM_PACK32,   ISL_FORMAT_R8G8B8A8_SNORM),
100    fmt(VK_FORMAT_A8B8G8R8_USCALED_PACK32, ISL_FORMAT_R8G8B8A8_USCALED),
101    fmt(VK_FORMAT_A8B8G8R8_SSCALED_PACK32, ISL_FORMAT_R8G8B8A8_SSCALED),
102    fmt(VK_FORMAT_A8B8G8R8_UINT_PACK32,    ISL_FORMAT_R8G8B8A8_UINT),
103    fmt(VK_FORMAT_A8B8G8R8_SINT_PACK32,    ISL_FORMAT_R8G8B8A8_SINT),
104    fmt(VK_FORMAT_A8B8G8R8_SRGB_PACK32,    ISL_FORMAT_R8G8B8A8_UNORM_SRGB),
105    fmt(VK_FORMAT_A2R10G10B10_UNORM_PACK32, ISL_FORMAT_B10G10R10A2_UNORM),
106    fmt(VK_FORMAT_A2R10G10B10_SNORM_PACK32, ISL_FORMAT_B10G10R10A2_SNORM),
107    fmt(VK_FORMAT_A2R10G10B10_USCALED_PACK32, ISL_FORMAT_B10G10R10A2_USCALED),
108    fmt(VK_FORMAT_A2R10G10B10_SSCALED_PACK32, ISL_FORMAT_B10G10R10A2_SSCALED),
109    fmt(VK_FORMAT_A2R10G10B10_UINT_PACK32, ISL_FORMAT_B10G10R10A2_UINT),
110    fmt(VK_FORMAT_A2R10G10B10_SINT_PACK32, ISL_FORMAT_B10G10R10A2_SINT),
111    fmt(VK_FORMAT_A2B10G10R10_UNORM_PACK32, ISL_FORMAT_R10G10B10A2_UNORM),
112    fmt(VK_FORMAT_A2B10G10R10_SNORM_PACK32, ISL_FORMAT_R10G10B10A2_SNORM),
113    fmt(VK_FORMAT_A2B10G10R10_USCALED_PACK32, ISL_FORMAT_R10G10B10A2_USCALED),
114    fmt(VK_FORMAT_A2B10G10R10_SSCALED_PACK32, ISL_FORMAT_R10G10B10A2_SSCALED),
115    fmt(VK_FORMAT_A2B10G10R10_UINT_PACK32, ISL_FORMAT_R10G10B10A2_UINT),
116    fmt(VK_FORMAT_A2B10G10R10_SINT_PACK32, ISL_FORMAT_R10G10B10A2_SINT),
117    fmt(VK_FORMAT_R16_UNORM,               ISL_FORMAT_R16_UNORM),
118    fmt(VK_FORMAT_R16_SNORM,               ISL_FORMAT_R16_SNORM),
119    fmt(VK_FORMAT_R16_USCALED,             ISL_FORMAT_R16_USCALED),
120    fmt(VK_FORMAT_R16_SSCALED,             ISL_FORMAT_R16_SSCALED),
121    fmt(VK_FORMAT_R16_UINT,                ISL_FORMAT_R16_UINT),
122    fmt(VK_FORMAT_R16_SINT,                ISL_FORMAT_R16_SINT),
123    fmt(VK_FORMAT_R16_SFLOAT,              ISL_FORMAT_R16_FLOAT),
124    fmt(VK_FORMAT_R16G16_UNORM,            ISL_FORMAT_R16G16_UNORM),
125    fmt(VK_FORMAT_R16G16_SNORM,            ISL_FORMAT_R16G16_SNORM),
126    fmt(VK_FORMAT_R16G16_USCALED,          ISL_FORMAT_R16G16_USCALED),
127    fmt(VK_FORMAT_R16G16_SSCALED,          ISL_FORMAT_R16G16_SSCALED),
128    fmt(VK_FORMAT_R16G16_UINT,             ISL_FORMAT_R16G16_UINT),
129    fmt(VK_FORMAT_R16G16_SINT,             ISL_FORMAT_R16G16_SINT),
130    fmt(VK_FORMAT_R16G16_SFLOAT,           ISL_FORMAT_R16G16_FLOAT),
131    fmt(VK_FORMAT_R16G16B16_UNORM,         ISL_FORMAT_R16G16B16_UNORM),
132    fmt(VK_FORMAT_R16G16B16_SNORM,         ISL_FORMAT_R16G16B16_SNORM),
133    fmt(VK_FORMAT_R16G16B16_USCALED,       ISL_FORMAT_R16G16B16_USCALED),
134    fmt(VK_FORMAT_R16G16B16_SSCALED,       ISL_FORMAT_R16G16B16_SSCALED),
135    fmt(VK_FORMAT_R16G16B16_UINT,          ISL_FORMAT_R16G16B16_UINT),
136    fmt(VK_FORMAT_R16G16B16_SINT,          ISL_FORMAT_R16G16B16_SINT),
137    fmt(VK_FORMAT_R16G16B16_SFLOAT,        ISL_FORMAT_R16G16B16_FLOAT),
138    fmt(VK_FORMAT_R16G16B16A16_UNORM,      ISL_FORMAT_R16G16B16A16_UNORM),
139    fmt(VK_FORMAT_R16G16B16A16_SNORM,      ISL_FORMAT_R16G16B16A16_SNORM),
140    fmt(VK_FORMAT_R16G16B16A16_USCALED,    ISL_FORMAT_R16G16B16A16_USCALED),
141    fmt(VK_FORMAT_R16G16B16A16_SSCALED,    ISL_FORMAT_R16G16B16A16_SSCALED),
142    fmt(VK_FORMAT_R16G16B16A16_UINT,       ISL_FORMAT_R16G16B16A16_UINT),
143    fmt(VK_FORMAT_R16G16B16A16_SINT,       ISL_FORMAT_R16G16B16A16_SINT),
144    fmt(VK_FORMAT_R16G16B16A16_SFLOAT,     ISL_FORMAT_R16G16B16A16_FLOAT),
145    fmt(VK_FORMAT_R32_UINT,                ISL_FORMAT_R32_UINT),
146    fmt(VK_FORMAT_R32_SINT,                ISL_FORMAT_R32_SINT),
147    fmt(VK_FORMAT_R32_SFLOAT,              ISL_FORMAT_R32_FLOAT),
148    fmt(VK_FORMAT_R32G32_UINT,             ISL_FORMAT_R32G32_UINT),
149    fmt(VK_FORMAT_R32G32_SINT,             ISL_FORMAT_R32G32_SINT),
150    fmt(VK_FORMAT_R32G32_SFLOAT,           ISL_FORMAT_R32G32_FLOAT),
151    fmt(VK_FORMAT_R32G32B32_UINT,          ISL_FORMAT_R32G32B32_UINT),
152    fmt(VK_FORMAT_R32G32B32_SINT,          ISL_FORMAT_R32G32B32_SINT),
153    fmt(VK_FORMAT_R32G32B32_SFLOAT,        ISL_FORMAT_R32G32B32_FLOAT),
154    fmt(VK_FORMAT_R32G32B32A32_UINT,       ISL_FORMAT_R32G32B32A32_UINT),
155    fmt(VK_FORMAT_R32G32B32A32_SINT,       ISL_FORMAT_R32G32B32A32_SINT),
156    fmt(VK_FORMAT_R32G32B32A32_SFLOAT,     ISL_FORMAT_R32G32B32A32_FLOAT),
157    fmt(VK_FORMAT_R64_UINT,                ISL_FORMAT_R64_PASSTHRU),
158    fmt(VK_FORMAT_R64_SINT,                ISL_FORMAT_R64_PASSTHRU),
159    fmt(VK_FORMAT_R64_SFLOAT,              ISL_FORMAT_R64_PASSTHRU),
160    fmt(VK_FORMAT_R64G64_UINT,             ISL_FORMAT_R64G64_PASSTHRU),
161    fmt(VK_FORMAT_R64G64_SINT,             ISL_FORMAT_R64G64_PASSTHRU),
162    fmt(VK_FORMAT_R64G64_SFLOAT,           ISL_FORMAT_R64G64_PASSTHRU),
163    fmt(VK_FORMAT_R64G64B64_UINT,          ISL_FORMAT_R64G64B64_PASSTHRU),
164    fmt(VK_FORMAT_R64G64B64_SINT,          ISL_FORMAT_R64G64B64_PASSTHRU),
165    fmt(VK_FORMAT_R64G64B64_SFLOAT,        ISL_FORMAT_R64G64B64_PASSTHRU),
166    fmt(VK_FORMAT_R64G64B64A64_UINT,       ISL_FORMAT_R64G64B64A64_PASSTHRU),
167    fmt(VK_FORMAT_R64G64B64A64_SINT,       ISL_FORMAT_R64G64B64A64_PASSTHRU),
168    fmt(VK_FORMAT_R64G64B64A64_SFLOAT,     ISL_FORMAT_R64G64B64A64_PASSTHRU),
169    fmt(VK_FORMAT_B10G11R11_UFLOAT_PACK32, ISL_FORMAT_R11G11B10_FLOAT),
170    fmt(VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,  ISL_FORMAT_R9G9B9E5_SHAREDEXP),
171 
172    fmt(VK_FORMAT_D16_UNORM,               ISL_FORMAT_R16_UNORM),
173    fmt(VK_FORMAT_X8_D24_UNORM_PACK32,     ISL_FORMAT_R24_UNORM_X8_TYPELESS),
174    fmt(VK_FORMAT_D32_SFLOAT,              ISL_FORMAT_R32_FLOAT),
175    fmt(VK_FORMAT_S8_UINT,                 ISL_FORMAT_R8_UINT),
176    fmt(VK_FORMAT_D16_UNORM_S8_UINT,       ISL_FORMAT_UNSUPPORTED),
177    fmt(VK_FORMAT_D24_UNORM_S8_UINT,       ISL_FORMAT_R24_UNORM_X8_TYPELESS),
178    fmt(VK_FORMAT_D32_SFLOAT_S8_UINT,      ISL_FORMAT_R32_FLOAT),
179 
180    fmt(VK_FORMAT_BC1_RGB_UNORM_BLOCK,     ISL_FORMAT_DXT1_RGB),
181    fmt(VK_FORMAT_BC1_RGB_SRGB_BLOCK,      ISL_FORMAT_DXT1_RGB_SRGB),
182    fmt(VK_FORMAT_BC1_RGBA_UNORM_BLOCK,    ISL_FORMAT_BC1_UNORM),
183    fmt(VK_FORMAT_BC1_RGBA_SRGB_BLOCK,     ISL_FORMAT_BC1_UNORM_SRGB),
184    fmt(VK_FORMAT_BC2_UNORM_BLOCK,         ISL_FORMAT_BC2_UNORM),
185    fmt(VK_FORMAT_BC2_SRGB_BLOCK,          ISL_FORMAT_BC2_UNORM_SRGB),
186    fmt(VK_FORMAT_BC3_UNORM_BLOCK,         ISL_FORMAT_BC3_UNORM),
187    fmt(VK_FORMAT_BC3_SRGB_BLOCK,          ISL_FORMAT_BC3_UNORM_SRGB),
188    fmt(VK_FORMAT_BC4_UNORM_BLOCK,         ISL_FORMAT_BC4_UNORM),
189    fmt(VK_FORMAT_BC4_SNORM_BLOCK,         ISL_FORMAT_BC4_SNORM),
190    fmt(VK_FORMAT_BC5_UNORM_BLOCK,         ISL_FORMAT_BC5_UNORM),
191    fmt(VK_FORMAT_BC5_SNORM_BLOCK,         ISL_FORMAT_BC5_SNORM),
192    fmt(VK_FORMAT_BC6H_UFLOAT_BLOCK,       ISL_FORMAT_BC6H_UF16),
193    fmt(VK_FORMAT_BC6H_SFLOAT_BLOCK,       ISL_FORMAT_BC6H_SF16),
194    fmt(VK_FORMAT_BC7_UNORM_BLOCK,         ISL_FORMAT_BC7_UNORM),
195    fmt(VK_FORMAT_BC7_SRGB_BLOCK,          ISL_FORMAT_BC7_UNORM_SRGB),
196    fmt(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, ISL_FORMAT_ETC2_RGB8),
197    fmt(VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK,  ISL_FORMAT_ETC2_SRGB8),
198    fmt(VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK, ISL_FORMAT_ETC2_RGB8_PTA),
199    fmt(VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK, ISL_FORMAT_ETC2_SRGB8_PTA),
200    fmt(VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, ISL_FORMAT_ETC2_EAC_RGBA8),
201    fmt(VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK, ISL_FORMAT_ETC2_EAC_SRGB8_A8),
202    fmt(VK_FORMAT_EAC_R11_UNORM_BLOCK,     ISL_FORMAT_EAC_R11),
203    fmt(VK_FORMAT_EAC_R11_SNORM_BLOCK,     ISL_FORMAT_EAC_SIGNED_R11),
204    fmt(VK_FORMAT_EAC_R11G11_UNORM_BLOCK,  ISL_FORMAT_EAC_RG11),
205    fmt(VK_FORMAT_EAC_R11G11_SNORM_BLOCK,  ISL_FORMAT_EAC_SIGNED_RG11),
206    fmt(VK_FORMAT_ASTC_4x4_SRGB_BLOCK,     ISL_FORMAT_ASTC_LDR_2D_4X4_U8SRGB),
207    fmt(VK_FORMAT_ASTC_5x4_SRGB_BLOCK,     ISL_FORMAT_ASTC_LDR_2D_5X4_U8SRGB),
208    fmt(VK_FORMAT_ASTC_5x5_SRGB_BLOCK,     ISL_FORMAT_ASTC_LDR_2D_5X5_U8SRGB),
209    fmt(VK_FORMAT_ASTC_6x5_SRGB_BLOCK,     ISL_FORMAT_ASTC_LDR_2D_6X5_U8SRGB),
210    fmt(VK_FORMAT_ASTC_6x6_SRGB_BLOCK,     ISL_FORMAT_ASTC_LDR_2D_6X6_U8SRGB),
211    fmt(VK_FORMAT_ASTC_8x5_SRGB_BLOCK,     ISL_FORMAT_ASTC_LDR_2D_8X5_U8SRGB),
212    fmt(VK_FORMAT_ASTC_8x6_SRGB_BLOCK,     ISL_FORMAT_ASTC_LDR_2D_8X6_U8SRGB),
213    fmt(VK_FORMAT_ASTC_8x8_SRGB_BLOCK,     ISL_FORMAT_ASTC_LDR_2D_8X8_U8SRGB),
214    fmt(VK_FORMAT_ASTC_10x5_SRGB_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_10X5_U8SRGB),
215    fmt(VK_FORMAT_ASTC_10x6_SRGB_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_10X6_U8SRGB),
216    fmt(VK_FORMAT_ASTC_10x8_SRGB_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_10X8_U8SRGB),
217    fmt(VK_FORMAT_ASTC_10x10_SRGB_BLOCK,   ISL_FORMAT_ASTC_LDR_2D_10X10_U8SRGB),
218    fmt(VK_FORMAT_ASTC_12x10_SRGB_BLOCK,   ISL_FORMAT_ASTC_LDR_2D_12X10_U8SRGB),
219    fmt(VK_FORMAT_ASTC_12x12_SRGB_BLOCK,   ISL_FORMAT_ASTC_LDR_2D_12X12_U8SRGB),
220    fmt(VK_FORMAT_ASTC_4x4_UNORM_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_4X4_FLT16),
221    fmt(VK_FORMAT_ASTC_5x4_UNORM_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_5X4_FLT16),
222    fmt(VK_FORMAT_ASTC_5x5_UNORM_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_5X5_FLT16),
223    fmt(VK_FORMAT_ASTC_6x5_UNORM_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_6X5_FLT16),
224    fmt(VK_FORMAT_ASTC_6x6_UNORM_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_6X6_FLT16),
225    fmt(VK_FORMAT_ASTC_8x5_UNORM_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_8X5_FLT16),
226    fmt(VK_FORMAT_ASTC_8x6_UNORM_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_8X6_FLT16),
227    fmt(VK_FORMAT_ASTC_8x8_UNORM_BLOCK,    ISL_FORMAT_ASTC_LDR_2D_8X8_FLT16),
228    fmt(VK_FORMAT_ASTC_10x5_UNORM_BLOCK,   ISL_FORMAT_ASTC_LDR_2D_10X5_FLT16),
229    fmt(VK_FORMAT_ASTC_10x6_UNORM_BLOCK,   ISL_FORMAT_ASTC_LDR_2D_10X6_FLT16),
230    fmt(VK_FORMAT_ASTC_10x8_UNORM_BLOCK,   ISL_FORMAT_ASTC_LDR_2D_10X8_FLT16),
231    fmt(VK_FORMAT_ASTC_10x10_UNORM_BLOCK,  ISL_FORMAT_ASTC_LDR_2D_10X10_FLT16),
232    fmt(VK_FORMAT_ASTC_12x10_UNORM_BLOCK,  ISL_FORMAT_ASTC_LDR_2D_12X10_FLT16),
233    fmt(VK_FORMAT_ASTC_12x12_UNORM_BLOCK,  ISL_FORMAT_ASTC_LDR_2D_12X12_FLT16),
234    fmt(VK_FORMAT_B8G8R8_UNORM,            ISL_FORMAT_UNSUPPORTED),
235    fmt(VK_FORMAT_B8G8R8_SNORM,            ISL_FORMAT_UNSUPPORTED),
236    fmt(VK_FORMAT_B8G8R8_USCALED,          ISL_FORMAT_UNSUPPORTED),
237    fmt(VK_FORMAT_B8G8R8_SSCALED,          ISL_FORMAT_UNSUPPORTED),
238    fmt(VK_FORMAT_B8G8R8_UINT,             ISL_FORMAT_UNSUPPORTED),
239    fmt(VK_FORMAT_B8G8R8_SINT,             ISL_FORMAT_UNSUPPORTED),
240    fmt(VK_FORMAT_B8G8R8_SRGB,             ISL_FORMAT_UNSUPPORTED),
241    fmt(VK_FORMAT_B8G8R8A8_UNORM,          ISL_FORMAT_B8G8R8A8_UNORM),
242    fmt(VK_FORMAT_B8G8R8A8_SNORM,          ISL_FORMAT_UNSUPPORTED),
243    fmt(VK_FORMAT_B8G8R8A8_USCALED,        ISL_FORMAT_UNSUPPORTED),
244    fmt(VK_FORMAT_B8G8R8A8_SSCALED,        ISL_FORMAT_UNSUPPORTED),
245    fmt(VK_FORMAT_B8G8R8A8_UINT,           ISL_FORMAT_UNSUPPORTED),
246    fmt(VK_FORMAT_B8G8R8A8_SINT,           ISL_FORMAT_UNSUPPORTED),
247    fmt(VK_FORMAT_B8G8R8A8_SRGB,           ISL_FORMAT_B8G8R8A8_UNORM_SRGB),
248 };
249 
250 #undef fmt
251 
252 /**
253  * Exactly one bit must be set in \a aspect.
254  */
255 struct anv_format
anv_get_format(const struct gen_device_info * devinfo,VkFormat vk_format,VkImageAspectFlags aspect,VkImageTiling tiling)256 anv_get_format(const struct gen_device_info *devinfo, VkFormat vk_format,
257                VkImageAspectFlags aspect, VkImageTiling tiling)
258 {
259    struct anv_format format = anv_formats[vk_format];
260 
261    if (format.isl_format == ISL_FORMAT_UNSUPPORTED)
262       return format;
263 
264    if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
265       assert(vk_format_aspects(vk_format) & VK_IMAGE_ASPECT_STENCIL_BIT);
266       format.isl_format = ISL_FORMAT_R8_UINT;
267       return format;
268    }
269 
270    if (aspect & VK_IMAGE_ASPECT_DEPTH_BIT) {
271       assert(vk_format_aspects(vk_format) & VK_IMAGE_ASPECT_DEPTH_BIT);
272       return format;
273    }
274 
275    assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
276    assert(vk_format_aspects(vk_format) == VK_IMAGE_ASPECT_COLOR_BIT);
277 
278    const struct isl_format_layout *isl_layout =
279       isl_format_get_layout(format.isl_format);
280 
281    if (tiling == VK_IMAGE_TILING_OPTIMAL &&
282        !util_is_power_of_two(isl_layout->bpb)) {
283       /* Tiled formats *must* be power-of-two because we need up upload
284        * them with the render pipeline.  For 3-channel formats, we fix
285        * this by switching them over to RGBX or RGBA formats under the
286        * hood.
287        */
288       enum isl_format rgbx = isl_format_rgb_to_rgbx(format.isl_format);
289       if (rgbx != ISL_FORMAT_UNSUPPORTED &&
290           isl_format_supports_rendering(devinfo, rgbx)) {
291          format.isl_format = rgbx;
292       } else {
293          format.isl_format = isl_format_rgb_to_rgba(format.isl_format);
294          format.swizzle = ISL_SWIZZLE(RED, GREEN, BLUE, ONE);
295       }
296    }
297 
298    /* The B4G4R4A4 format isn't available prior to Broadwell so we have to fall
299     * back to a format with a more complex swizzle.
300     */
301    if (vk_format == VK_FORMAT_B4G4R4A4_UNORM_PACK16 && devinfo->gen < 8) {
302       return (struct anv_format) {
303          .isl_format = ISL_FORMAT_B4G4R4A4_UNORM,
304          .swizzle = ISL_SWIZZLE(GREEN, RED, ALPHA, BLUE),
305       };
306    }
307 
308    return format;
309 }
310 
311 // Format capabilities
312 
313 static VkFormatFeatureFlags
get_image_format_properties(const struct gen_device_info * devinfo,enum isl_format base,struct anv_format format)314 get_image_format_properties(const struct gen_device_info *devinfo,
315                             enum isl_format base, struct anv_format format)
316 {
317    if (format.isl_format == ISL_FORMAT_UNSUPPORTED)
318       return 0;
319 
320    VkFormatFeatureFlags flags = 0;
321    if (isl_format_supports_sampling(devinfo, format.isl_format)) {
322       flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
323                VK_FORMAT_FEATURE_BLIT_SRC_BIT;
324 
325       if (isl_format_supports_filtering(devinfo, format.isl_format))
326          flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
327    }
328 
329    /* We can render to swizzled formats.  However, if the alpha channel is
330     * moved, then blending won't work correctly.  The PRM tells us
331     * straight-up not to render to such a surface.
332     */
333    if (isl_format_supports_rendering(devinfo, format.isl_format) &&
334        format.swizzle.a == ISL_CHANNEL_SELECT_ALPHA) {
335       flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
336                VK_FORMAT_FEATURE_BLIT_DST_BIT;
337 
338       if (isl_format_supports_alpha_blending(devinfo, format.isl_format))
339          flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
340    }
341 
342    /* Load/store is determined based on base format.  This prevents RGB
343     * formats from showing up as load/store capable.
344     */
345    if (isl_is_storage_image_format(base))
346       flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
347 
348    if (base == ISL_FORMAT_R32_SINT || base == ISL_FORMAT_R32_UINT)
349       flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
350 
351    return flags;
352 }
353 
354 static VkFormatFeatureFlags
get_buffer_format_properties(const struct gen_device_info * devinfo,enum isl_format format)355 get_buffer_format_properties(const struct gen_device_info *devinfo,
356                              enum isl_format format)
357 {
358    if (format == ISL_FORMAT_UNSUPPORTED)
359       return 0;
360 
361    VkFormatFeatureFlags flags = 0;
362    if (isl_format_supports_sampling(devinfo, format) &&
363        !isl_format_is_compressed(format))
364       flags |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT;
365 
366    if (isl_format_supports_vertex_fetch(devinfo, format))
367       flags |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
368 
369    if (isl_is_storage_image_format(format))
370       flags |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
371 
372    if (format == ISL_FORMAT_R32_SINT || format == ISL_FORMAT_R32_UINT)
373       flags |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
374 
375    return flags;
376 }
377 
378 static void
anv_physical_device_get_format_properties(struct anv_physical_device * physical_device,VkFormat format,VkFormatProperties * out_properties)379 anv_physical_device_get_format_properties(struct anv_physical_device *physical_device,
380                                           VkFormat format,
381                                           VkFormatProperties *out_properties)
382 {
383    int gen = physical_device->info.gen * 10;
384    if (physical_device->info.is_haswell)
385       gen += 5;
386 
387    VkFormatFeatureFlags linear = 0, tiled = 0, buffer = 0;
388    if (anv_formats[format].isl_format == ISL_FORMAT_UNSUPPORTED) {
389       /* Nothing to do here */
390    } else if (vk_format_is_depth_or_stencil(format)) {
391       tiled |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
392       if (physical_device->info.gen >= 8)
393          tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
394 
395       tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |
396                VK_FORMAT_FEATURE_BLIT_DST_BIT;
397    } else {
398       struct anv_format linear_fmt, tiled_fmt;
399       linear_fmt = anv_get_format(&physical_device->info, format,
400                                   VK_IMAGE_ASPECT_COLOR_BIT,
401                                   VK_IMAGE_TILING_LINEAR);
402       tiled_fmt = anv_get_format(&physical_device->info, format,
403                                  VK_IMAGE_ASPECT_COLOR_BIT,
404                                  VK_IMAGE_TILING_OPTIMAL);
405 
406       linear = get_image_format_properties(&physical_device->info,
407                                            linear_fmt.isl_format, linear_fmt);
408       tiled = get_image_format_properties(&physical_device->info,
409                                           linear_fmt.isl_format, tiled_fmt);
410       buffer = get_buffer_format_properties(&physical_device->info,
411                                             linear_fmt.isl_format);
412 
413       /* XXX: We handle 3-channel formats by switching them out for RGBX or
414        * RGBA formats behind-the-scenes.  This works fine for textures
415        * because the upload process will fill in the extra channel.
416        * We could also support it for render targets, but it will take
417        * substantially more work and we have enough RGBX formats to handle
418        * what most clients will want.
419        */
420       if (linear_fmt.isl_format != ISL_FORMAT_UNSUPPORTED &&
421           !util_is_power_of_two(isl_format_layouts[linear_fmt.isl_format].bpb) &&
422           isl_format_rgb_to_rgbx(linear_fmt.isl_format) == ISL_FORMAT_UNSUPPORTED) {
423          tiled &= ~VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT &
424                   ~VK_FORMAT_FEATURE_BLIT_DST_BIT;
425       }
426 
427       /* ASTC textures must be in Y-tiled memory */
428       if (isl_format_get_layout(linear_fmt.isl_format)->txc == ISL_TXC_ASTC)
429          linear = 0;
430    }
431 
432    out_properties->linearTilingFeatures = linear;
433    out_properties->optimalTilingFeatures = tiled;
434    out_properties->bufferFeatures = buffer;
435 
436    return;
437 }
438 
439 
anv_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties * pFormatProperties)440 void anv_GetPhysicalDeviceFormatProperties(
441     VkPhysicalDevice                            physicalDevice,
442     VkFormat                                    format,
443     VkFormatProperties*                         pFormatProperties)
444 {
445    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
446 
447    anv_physical_device_get_format_properties(
448                physical_device,
449                format,
450                pFormatProperties);
451 }
452 
anv_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,VkImageTiling tiling,VkImageUsageFlags usage,VkImageCreateFlags createFlags,VkImageFormatProperties * pImageFormatProperties)453 VkResult anv_GetPhysicalDeviceImageFormatProperties(
454     VkPhysicalDevice                            physicalDevice,
455     VkFormat                                    format,
456     VkImageType                                 type,
457     VkImageTiling                               tiling,
458     VkImageUsageFlags                           usage,
459     VkImageCreateFlags                          createFlags,
460     VkImageFormatProperties*                    pImageFormatProperties)
461 {
462    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
463    VkFormatProperties format_props;
464    VkFormatFeatureFlags format_feature_flags;
465    VkExtent3D maxExtent;
466    uint32_t maxMipLevels;
467    uint32_t maxArraySize;
468    VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
469 
470    if (anv_formats[format].isl_format == ISL_FORMAT_UNSUPPORTED)
471       goto unsupported;
472 
473    anv_physical_device_get_format_properties(physical_device, format,
474                                              &format_props);
475 
476    /* Extract the VkFormatFeatureFlags that are relevant for the queried
477     * tiling.
478     */
479    if (tiling == VK_IMAGE_TILING_LINEAR) {
480       format_feature_flags = format_props.linearTilingFeatures;
481    } else if (tiling == VK_IMAGE_TILING_OPTIMAL) {
482       format_feature_flags = format_props.optimalTilingFeatures;
483    } else {
484       unreachable("bad VkImageTiling");
485    }
486 
487    switch (type) {
488    default:
489       unreachable("bad VkImageType");
490    case VK_IMAGE_TYPE_1D:
491       maxExtent.width = 16384;
492       maxExtent.height = 1;
493       maxExtent.depth = 1;
494       maxMipLevels = 15; /* log2(maxWidth) + 1 */
495       maxArraySize = 2048;
496       sampleCounts = VK_SAMPLE_COUNT_1_BIT;
497       break;
498    case VK_IMAGE_TYPE_2D:
499       /* FINISHME: Does this really differ for cube maps? The documentation
500        * for RENDER_SURFACE_STATE suggests so.
501        */
502       maxExtent.width = 16384;
503       maxExtent.height = 16384;
504       maxExtent.depth = 1;
505       maxMipLevels = 15; /* log2(maxWidth) + 1 */
506       maxArraySize = 2048;
507       break;
508    case VK_IMAGE_TYPE_3D:
509       maxExtent.width = 2048;
510       maxExtent.height = 2048;
511       maxExtent.depth = 2048;
512       maxMipLevels = 12; /* log2(maxWidth) + 1 */
513       maxArraySize = 1;
514       break;
515    }
516 
517    /* Our hardware doesn't support 1D compressed textures.
518     *    From the SKL PRM, RENDER_SURFACE_STATE::SurfaceFormat:
519     *    * This field cannot be a compressed (BC*, DXT*, FXT*, ETC*, EAC*) format
520     *       if the Surface Type is SURFTYPE_1D.
521     *    * This field cannot be ASTC format if the Surface Type is SURFTYPE_1D.
522     */
523    if (type == VK_IMAGE_TYPE_1D &&
524        isl_format_is_compressed(anv_formats[format].isl_format)) {
525        goto unsupported;
526    }
527 
528    if (tiling == VK_IMAGE_TILING_OPTIMAL &&
529        type == VK_IMAGE_TYPE_2D &&
530        (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
531                                 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
532        !(createFlags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
533        !(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
534       sampleCounts = isl_device_get_sample_counts(&physical_device->isl_dev);
535    }
536 
537    if (usage & (VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
538                 VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
539       /* Accept transfers on anything we can sample from or renderer to. */
540       if (!(format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
541                                     VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT |
542                                     VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))) {
543          goto unsupported;
544       }
545    }
546 
547    if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
548       if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
549          goto unsupported;
550       }
551    }
552 
553    if (usage & VK_IMAGE_USAGE_STORAGE_BIT) {
554       if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
555          goto unsupported;
556       }
557    }
558 
559    if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
560       if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
561          goto unsupported;
562       }
563    }
564 
565    if (usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
566       if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
567          goto unsupported;
568       }
569    }
570 
571    if (usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
572       /* Nothing to check. */
573    }
574 
575    if (usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
576       /* Ignore this flag because it was removed from the
577        * provisional_I_20150910 header.
578        */
579    }
580 
581    *pImageFormatProperties = (VkImageFormatProperties) {
582       .maxExtent = maxExtent,
583       .maxMipLevels = maxMipLevels,
584       .maxArrayLayers = maxArraySize,
585       .sampleCounts = sampleCounts,
586 
587       /* FINISHME: Accurately calculate
588        * VkImageFormatProperties::maxResourceSize.
589        */
590       .maxResourceSize = UINT32_MAX,
591    };
592 
593    return VK_SUCCESS;
594 
595 unsupported:
596    *pImageFormatProperties = (VkImageFormatProperties) {
597       .maxExtent = { 0, 0, 0 },
598       .maxMipLevels = 0,
599       .maxArrayLayers = 0,
600       .sampleCounts = 0,
601       .maxResourceSize = 0,
602    };
603 
604    return VK_ERROR_FORMAT_NOT_SUPPORTED;
605 }
606 
anv_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,uint32_t samples,VkImageUsageFlags usage,VkImageTiling tiling,uint32_t * pNumProperties,VkSparseImageFormatProperties * pProperties)607 void anv_GetPhysicalDeviceSparseImageFormatProperties(
608     VkPhysicalDevice                            physicalDevice,
609     VkFormat                                    format,
610     VkImageType                                 type,
611     uint32_t                                    samples,
612     VkImageUsageFlags                           usage,
613     VkImageTiling                               tiling,
614     uint32_t*                                   pNumProperties,
615     VkSparseImageFormatProperties*              pProperties)
616 {
617    /* Sparse images are not yet supported. */
618    *pNumProperties = 0;
619 }
620