anv_formats.c revision 7c1660aa14094e40fba9f39ce194cb6238311b65
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 "brw_surface_formats.h"
26
27#define RGBA { 0, 1, 2, 3 }
28#define BGRA { 2, 1, 0, 3 }
29
30#define swiz_fmt(__vk_fmt, __hw_fmt, __swizzle, ...)     \
31   [__vk_fmt] = { \
32      .vk_format = __vk_fmt, \
33      .name = #__vk_fmt, \
34      .isl_format = __hw_fmt, \
35      .isl_layout = &isl_format_layouts[__hw_fmt], \
36      .swizzle = __swizzle, \
37      __VA_ARGS__ \
38   }
39
40#define fmt(__vk_fmt, __hw_fmt, ...) \
41   swiz_fmt(__vk_fmt, __hw_fmt, RGBA, __VA_ARGS__)
42
43/* HINT: For array formats, the ISL name should match the VK name.  For
44 * packed formats, they should have the channels in reverse order from each
45 * other.  The reason for this is that, for packed formats, the ISL (and
46 * bspec) names are in LSB -> MSB order while VK formats are MSB -> LSB.
47 */
48static const struct anv_format anv_formats[] = {
49   fmt(VK_FORMAT_UNDEFINED,               ISL_FORMAT_RAW),
50   fmt(VK_FORMAT_R4G4_UNORM_PACK8,        ISL_FORMAT_UNSUPPORTED),
51   fmt(VK_FORMAT_R4G4B4A4_UNORM_PACK16,   ISL_FORMAT_A4B4G4R4_UNORM),
52   swiz_fmt(VK_FORMAT_B4G4R4A4_UNORM_PACK16,   ISL_FORMAT_A4B4G4R4_UNORM,  BGRA),
53   fmt(VK_FORMAT_R5G6B5_UNORM_PACK16,     ISL_FORMAT_B5G6R5_UNORM),
54   swiz_fmt(VK_FORMAT_B5G6R5_UNORM_PACK16,     ISL_FORMAT_B5G6R5_UNORM, BGRA),
55   fmt(VK_FORMAT_R5G5B5A1_UNORM_PACK16,   ISL_FORMAT_A1B5G5R5_UNORM),
56   fmt(VK_FORMAT_B5G5R5A1_UNORM_PACK16,   ISL_FORMAT_UNSUPPORTED),
57   fmt(VK_FORMAT_A1R5G5B5_UNORM_PACK16,   ISL_FORMAT_B5G5R5A1_UNORM),
58   fmt(VK_FORMAT_R8_UNORM,                ISL_FORMAT_R8_UNORM),
59   fmt(VK_FORMAT_R8_SNORM,                ISL_FORMAT_R8_SNORM),
60   fmt(VK_FORMAT_R8_USCALED,              ISL_FORMAT_R8_USCALED),
61   fmt(VK_FORMAT_R8_SSCALED,              ISL_FORMAT_R8_SSCALED),
62   fmt(VK_FORMAT_R8_UINT,                 ISL_FORMAT_R8_UINT),
63   fmt(VK_FORMAT_R8_SINT,                 ISL_FORMAT_R8_SINT),
64   fmt(VK_FORMAT_R8_SRGB,                 ISL_FORMAT_UNSUPPORTED),
65   fmt(VK_FORMAT_R8G8_UNORM,              ISL_FORMAT_R8G8_UNORM),
66   fmt(VK_FORMAT_R8G8_SNORM,              ISL_FORMAT_R8G8_SNORM),
67   fmt(VK_FORMAT_R8G8_USCALED,            ISL_FORMAT_R8G8_USCALED),
68   fmt(VK_FORMAT_R8G8_SSCALED,            ISL_FORMAT_R8G8_SSCALED),
69   fmt(VK_FORMAT_R8G8_UINT,               ISL_FORMAT_R8G8_UINT),
70   fmt(VK_FORMAT_R8G8_SINT,               ISL_FORMAT_R8G8_SINT),
71   fmt(VK_FORMAT_R8G8_SRGB,               ISL_FORMAT_UNSUPPORTED), /* L8A8_UNORM_SRGB */
72   fmt(VK_FORMAT_R8G8B8_UNORM,            ISL_FORMAT_R8G8B8_UNORM),
73   fmt(VK_FORMAT_R8G8B8_SNORM,            ISL_FORMAT_R8G8B8_SNORM),
74   fmt(VK_FORMAT_R8G8B8_USCALED,          ISL_FORMAT_R8G8B8_USCALED),
75   fmt(VK_FORMAT_R8G8B8_SSCALED,          ISL_FORMAT_R8G8B8_SSCALED),
76   fmt(VK_FORMAT_R8G8B8_UINT,             ISL_FORMAT_R8G8B8_UINT),
77   fmt(VK_FORMAT_R8G8B8_SINT,             ISL_FORMAT_R8G8B8_SINT),
78   fmt(VK_FORMAT_R8G8B8_SRGB,             ISL_FORMAT_UNSUPPORTED), /* B8G8R8A8_UNORM_SRGB */
79   fmt(VK_FORMAT_R8G8B8A8_UNORM,          ISL_FORMAT_R8G8B8A8_UNORM),
80   fmt(VK_FORMAT_R8G8B8A8_SNORM,          ISL_FORMAT_R8G8B8A8_SNORM),
81   fmt(VK_FORMAT_R8G8B8A8_USCALED,        ISL_FORMAT_R8G8B8A8_USCALED),
82   fmt(VK_FORMAT_R8G8B8A8_SSCALED,        ISL_FORMAT_R8G8B8A8_SSCALED),
83   fmt(VK_FORMAT_R8G8B8A8_UINT,           ISL_FORMAT_R8G8B8A8_UINT),
84   fmt(VK_FORMAT_R8G8B8A8_SINT,           ISL_FORMAT_R8G8B8A8_SINT),
85   fmt(VK_FORMAT_R8G8B8A8_SRGB,           ISL_FORMAT_R8G8B8A8_UNORM_SRGB),
86   fmt(VK_FORMAT_A8B8G8R8_UNORM_PACK32,   ISL_FORMAT_R8G8B8A8_UNORM),
87   fmt(VK_FORMAT_A8B8G8R8_SNORM_PACK32,   ISL_FORMAT_R8G8B8A8_SNORM),
88   fmt(VK_FORMAT_A8B8G8R8_USCALED_PACK32, ISL_FORMAT_R8G8B8A8_USCALED),
89   fmt(VK_FORMAT_A8B8G8R8_SSCALED_PACK32, ISL_FORMAT_R8G8B8A8_SSCALED),
90   fmt(VK_FORMAT_A8B8G8R8_UINT_PACK32,    ISL_FORMAT_R8G8B8A8_UINT),
91   fmt(VK_FORMAT_A8B8G8R8_SINT_PACK32,    ISL_FORMAT_R8G8B8A8_SINT),
92   fmt(VK_FORMAT_A8B8G8R8_SRGB_PACK32,    ISL_FORMAT_R8G8B8A8_UNORM_SRGB),
93   fmt(VK_FORMAT_A2R10G10B10_UNORM_PACK32, ISL_FORMAT_B10G10R10A2_UNORM),
94   fmt(VK_FORMAT_A2R10G10B10_SNORM_PACK32, ISL_FORMAT_B10G10R10A2_SNORM),
95   fmt(VK_FORMAT_A2R10G10B10_USCALED_PACK32, ISL_FORMAT_B10G10R10A2_USCALED),
96   fmt(VK_FORMAT_A2R10G10B10_SSCALED_PACK32, ISL_FORMAT_B10G10R10A2_SSCALED),
97   fmt(VK_FORMAT_A2R10G10B10_UINT_PACK32, ISL_FORMAT_B10G10R10A2_UINT),
98   fmt(VK_FORMAT_A2R10G10B10_SINT_PACK32, ISL_FORMAT_B10G10R10A2_SINT),
99   fmt(VK_FORMAT_A2B10G10R10_UNORM_PACK32, ISL_FORMAT_R10G10B10A2_UNORM),
100   fmt(VK_FORMAT_A2B10G10R10_SNORM_PACK32, ISL_FORMAT_R10G10B10A2_SNORM),
101   fmt(VK_FORMAT_A2B10G10R10_USCALED_PACK32, ISL_FORMAT_R10G10B10A2_USCALED),
102   fmt(VK_FORMAT_A2B10G10R10_SSCALED_PACK32, ISL_FORMAT_R10G10B10A2_SSCALED),
103   fmt(VK_FORMAT_A2B10G10R10_UINT_PACK32, ISL_FORMAT_R10G10B10A2_UINT),
104   fmt(VK_FORMAT_A2B10G10R10_SINT_PACK32, ISL_FORMAT_R10G10B10A2_SINT),
105   fmt(VK_FORMAT_R16_UNORM,               ISL_FORMAT_R16_UNORM),
106   fmt(VK_FORMAT_R16_SNORM,               ISL_FORMAT_R16_SNORM),
107   fmt(VK_FORMAT_R16_USCALED,             ISL_FORMAT_R16_USCALED),
108   fmt(VK_FORMAT_R16_SSCALED,             ISL_FORMAT_R16_SSCALED),
109   fmt(VK_FORMAT_R16_UINT,                ISL_FORMAT_R16_UINT),
110   fmt(VK_FORMAT_R16_SINT,                ISL_FORMAT_R16_SINT),
111   fmt(VK_FORMAT_R16_SFLOAT,              ISL_FORMAT_R16_FLOAT),
112   fmt(VK_FORMAT_R16G16_UNORM,            ISL_FORMAT_R16G16_UNORM),
113   fmt(VK_FORMAT_R16G16_SNORM,            ISL_FORMAT_R16G16_SNORM),
114   fmt(VK_FORMAT_R16G16_USCALED,          ISL_FORMAT_R16G16_USCALED),
115   fmt(VK_FORMAT_R16G16_SSCALED,          ISL_FORMAT_R16G16_SSCALED),
116   fmt(VK_FORMAT_R16G16_UINT,             ISL_FORMAT_R16G16_UINT),
117   fmt(VK_FORMAT_R16G16_SINT,             ISL_FORMAT_R16G16_SINT),
118   fmt(VK_FORMAT_R16G16_SFLOAT,           ISL_FORMAT_R16G16_FLOAT),
119   fmt(VK_FORMAT_R16G16B16_UNORM,         ISL_FORMAT_R16G16B16_UNORM),
120   fmt(VK_FORMAT_R16G16B16_SNORM,         ISL_FORMAT_R16G16B16_SNORM),
121   fmt(VK_FORMAT_R16G16B16_USCALED,       ISL_FORMAT_R16G16B16_USCALED),
122   fmt(VK_FORMAT_R16G16B16_SSCALED,       ISL_FORMAT_R16G16B16_SSCALED),
123   fmt(VK_FORMAT_R16G16B16_UINT,          ISL_FORMAT_R16G16B16_UINT),
124   fmt(VK_FORMAT_R16G16B16_SINT,          ISL_FORMAT_R16G16B16_SINT),
125   fmt(VK_FORMAT_R16G16B16_SFLOAT,        ISL_FORMAT_R16G16B16_FLOAT),
126   fmt(VK_FORMAT_R16G16B16A16_UNORM,      ISL_FORMAT_R16G16B16A16_UNORM),
127   fmt(VK_FORMAT_R16G16B16A16_SNORM,      ISL_FORMAT_R16G16B16A16_SNORM),
128   fmt(VK_FORMAT_R16G16B16A16_USCALED,    ISL_FORMAT_R16G16B16A16_USCALED),
129   fmt(VK_FORMAT_R16G16B16A16_SSCALED,    ISL_FORMAT_R16G16B16A16_SSCALED),
130   fmt(VK_FORMAT_R16G16B16A16_UINT,       ISL_FORMAT_R16G16B16A16_UINT),
131   fmt(VK_FORMAT_R16G16B16A16_SINT,       ISL_FORMAT_R16G16B16A16_SINT),
132   fmt(VK_FORMAT_R16G16B16A16_SFLOAT,     ISL_FORMAT_R16G16B16A16_FLOAT),
133   fmt(VK_FORMAT_R32_UINT,                ISL_FORMAT_R32_UINT,),
134   fmt(VK_FORMAT_R32_SINT,                ISL_FORMAT_R32_SINT,),
135   fmt(VK_FORMAT_R32_SFLOAT,              ISL_FORMAT_R32_FLOAT,),
136   fmt(VK_FORMAT_R32G32_UINT,             ISL_FORMAT_R32G32_UINT,),
137   fmt(VK_FORMAT_R32G32_SINT,             ISL_FORMAT_R32G32_SINT,),
138   fmt(VK_FORMAT_R32G32_SFLOAT,           ISL_FORMAT_R32G32_FLOAT,),
139   fmt(VK_FORMAT_R32G32B32_UINT,          ISL_FORMAT_R32G32B32_UINT,),
140   fmt(VK_FORMAT_R32G32B32_SINT,          ISL_FORMAT_R32G32B32_SINT,),
141   fmt(VK_FORMAT_R32G32B32_SFLOAT,        ISL_FORMAT_R32G32B32_FLOAT,),
142   fmt(VK_FORMAT_R32G32B32A32_UINT,       ISL_FORMAT_R32G32B32A32_UINT,),
143   fmt(VK_FORMAT_R32G32B32A32_SINT,       ISL_FORMAT_R32G32B32A32_SINT,),
144   fmt(VK_FORMAT_R32G32B32A32_SFLOAT,     ISL_FORMAT_R32G32B32A32_FLOAT,),
145   fmt(VK_FORMAT_R64_UINT,                ISL_FORMAT_R64_PASSTHRU),
146   fmt(VK_FORMAT_R64_SINT,                ISL_FORMAT_R64_PASSTHRU),
147   fmt(VK_FORMAT_R64_SFLOAT,              ISL_FORMAT_R64_FLOAT),
148   fmt(VK_FORMAT_R64G64_UINT,             ISL_FORMAT_R64G64_PASSTHRU),
149   fmt(VK_FORMAT_R64G64_SINT,             ISL_FORMAT_R64G64_PASSTHRU),
150   fmt(VK_FORMAT_R64G64_SFLOAT,           ISL_FORMAT_R64G64_FLOAT),
151   fmt(VK_FORMAT_R64G64B64_UINT,          ISL_FORMAT_R64G64B64_PASSTHRU),
152   fmt(VK_FORMAT_R64G64B64_SINT,          ISL_FORMAT_R64G64B64_PASSTHRU),
153   fmt(VK_FORMAT_R64G64B64_SFLOAT,        ISL_FORMAT_R64G64B64_FLOAT),
154   fmt(VK_FORMAT_R64G64B64A64_UINT,       ISL_FORMAT_R64G64B64A64_PASSTHRU),
155   fmt(VK_FORMAT_R64G64B64A64_SINT,       ISL_FORMAT_R64G64B64A64_PASSTHRU),
156   fmt(VK_FORMAT_R64G64B64A64_SFLOAT,     ISL_FORMAT_R64G64B64A64_FLOAT),
157   fmt(VK_FORMAT_B10G11R11_UFLOAT_PACK32, ISL_FORMAT_R11G11B10_FLOAT),
158   fmt(VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,  ISL_FORMAT_R9G9B9E5_SHAREDEXP),
159
160   fmt(VK_FORMAT_D16_UNORM,               ISL_FORMAT_R16_UNORM,               .has_depth = true),
161   fmt(VK_FORMAT_X8_D24_UNORM_PACK32,     ISL_FORMAT_R24_UNORM_X8_TYPELESS,   .has_depth = true),
162   fmt(VK_FORMAT_D32_SFLOAT,              ISL_FORMAT_R32_FLOAT,               .has_depth = true),
163   fmt(VK_FORMAT_S8_UINT,                 ISL_FORMAT_R8_UINT,                                      .has_stencil = true),
164   fmt(VK_FORMAT_D16_UNORM_S8_UINT,       ISL_FORMAT_UNSUPPORTED),
165   fmt(VK_FORMAT_D24_UNORM_S8_UINT,       ISL_FORMAT_R24_UNORM_X8_TYPELESS,   .has_depth = true,   .has_stencil = true),
166   fmt(VK_FORMAT_D32_SFLOAT_S8_UINT,      ISL_FORMAT_R32_FLOAT,               .has_depth = true,   .has_stencil = true),
167
168   fmt(VK_FORMAT_BC1_RGB_UNORM_BLOCK,     ISL_FORMAT_DXT1_RGB),
169   fmt(VK_FORMAT_BC1_RGB_SRGB_BLOCK,      ISL_FORMAT_DXT1_RGB_SRGB),
170   fmt(VK_FORMAT_BC1_RGBA_UNORM_BLOCK,    ISL_FORMAT_BC1_UNORM),
171   fmt(VK_FORMAT_BC1_RGBA_SRGB_BLOCK,     ISL_FORMAT_BC1_UNORM_SRGB),
172   fmt(VK_FORMAT_BC2_UNORM_BLOCK,         ISL_FORMAT_BC2_UNORM),
173   fmt(VK_FORMAT_BC2_SRGB_BLOCK,          ISL_FORMAT_BC2_UNORM_SRGB),
174   fmt(VK_FORMAT_BC3_UNORM_BLOCK,         ISL_FORMAT_BC3_UNORM),
175   fmt(VK_FORMAT_BC3_SRGB_BLOCK,          ISL_FORMAT_BC3_UNORM_SRGB),
176   fmt(VK_FORMAT_BC4_UNORM_BLOCK,         ISL_FORMAT_BC4_UNORM),
177   fmt(VK_FORMAT_BC4_SNORM_BLOCK,         ISL_FORMAT_BC4_SNORM),
178   fmt(VK_FORMAT_BC5_UNORM_BLOCK,         ISL_FORMAT_BC5_UNORM),
179   fmt(VK_FORMAT_BC5_SNORM_BLOCK,         ISL_FORMAT_BC5_SNORM),
180   fmt(VK_FORMAT_BC6H_UFLOAT_BLOCK,       ISL_FORMAT_BC6H_UF16),
181   fmt(VK_FORMAT_BC6H_SFLOAT_BLOCK,       ISL_FORMAT_BC6H_SF16),
182   fmt(VK_FORMAT_BC7_UNORM_BLOCK,         ISL_FORMAT_BC7_UNORM),
183   fmt(VK_FORMAT_BC7_SRGB_BLOCK,          ISL_FORMAT_BC7_UNORM_SRGB),
184   fmt(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, ISL_FORMAT_ETC2_RGB8),
185   fmt(VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK,  ISL_FORMAT_ETC2_SRGB8),
186   fmt(VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK, ISL_FORMAT_ETC2_RGB8_PTA),
187   fmt(VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK, ISL_FORMAT_ETC2_SRGB8_PTA),
188   fmt(VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, ISL_FORMAT_ETC2_EAC_RGBA8),
189   fmt(VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK, ISL_FORMAT_ETC2_EAC_SRGB8_A8),
190   fmt(VK_FORMAT_EAC_R11_UNORM_BLOCK,     ISL_FORMAT_EAC_R11),
191   fmt(VK_FORMAT_EAC_R11_SNORM_BLOCK,     ISL_FORMAT_EAC_SIGNED_R11),
192   fmt(VK_FORMAT_EAC_R11G11_UNORM_BLOCK,  ISL_FORMAT_EAC_RG11),
193   fmt(VK_FORMAT_EAC_R11G11_SNORM_BLOCK,  ISL_FORMAT_EAC_SIGNED_RG11),
194   fmt(VK_FORMAT_ASTC_4x4_UNORM_BLOCK,    ISL_FORMAT_UNSUPPORTED),
195   fmt(VK_FORMAT_ASTC_4x4_SRGB_BLOCK,     ISL_FORMAT_UNSUPPORTED),
196   fmt(VK_FORMAT_ASTC_5x4_UNORM_BLOCK,    ISL_FORMAT_UNSUPPORTED),
197   fmt(VK_FORMAT_ASTC_5x4_SRGB_BLOCK,     ISL_FORMAT_UNSUPPORTED),
198   fmt(VK_FORMAT_ASTC_5x5_UNORM_BLOCK,    ISL_FORMAT_UNSUPPORTED),
199   fmt(VK_FORMAT_ASTC_5x5_SRGB_BLOCK,     ISL_FORMAT_UNSUPPORTED),
200   fmt(VK_FORMAT_ASTC_6x5_UNORM_BLOCK,    ISL_FORMAT_UNSUPPORTED),
201   fmt(VK_FORMAT_ASTC_6x5_SRGB_BLOCK,     ISL_FORMAT_UNSUPPORTED),
202   fmt(VK_FORMAT_ASTC_6x6_UNORM_BLOCK,    ISL_FORMAT_UNSUPPORTED),
203   fmt(VK_FORMAT_ASTC_6x6_SRGB_BLOCK,     ISL_FORMAT_UNSUPPORTED),
204   fmt(VK_FORMAT_ASTC_8x5_UNORM_BLOCK,    ISL_FORMAT_UNSUPPORTED),
205   fmt(VK_FORMAT_ASTC_8x5_SRGB_BLOCK,     ISL_FORMAT_UNSUPPORTED),
206   fmt(VK_FORMAT_ASTC_8x6_UNORM_BLOCK,    ISL_FORMAT_UNSUPPORTED),
207   fmt(VK_FORMAT_ASTC_8x6_SRGB_BLOCK,     ISL_FORMAT_UNSUPPORTED),
208   fmt(VK_FORMAT_ASTC_8x8_UNORM_BLOCK,    ISL_FORMAT_UNSUPPORTED),
209   fmt(VK_FORMAT_ASTC_8x8_SRGB_BLOCK,     ISL_FORMAT_UNSUPPORTED),
210   fmt(VK_FORMAT_ASTC_10x5_UNORM_BLOCK,   ISL_FORMAT_UNSUPPORTED),
211   fmt(VK_FORMAT_ASTC_10x5_SRGB_BLOCK,    ISL_FORMAT_UNSUPPORTED),
212   fmt(VK_FORMAT_ASTC_10x6_UNORM_BLOCK,   ISL_FORMAT_UNSUPPORTED),
213   fmt(VK_FORMAT_ASTC_10x6_SRGB_BLOCK,    ISL_FORMAT_UNSUPPORTED),
214   fmt(VK_FORMAT_ASTC_10x8_UNORM_BLOCK,   ISL_FORMAT_UNSUPPORTED),
215   fmt(VK_FORMAT_ASTC_10x8_SRGB_BLOCK,    ISL_FORMAT_UNSUPPORTED),
216   fmt(VK_FORMAT_ASTC_10x10_UNORM_BLOCK,  ISL_FORMAT_UNSUPPORTED),
217   fmt(VK_FORMAT_ASTC_10x10_SRGB_BLOCK,   ISL_FORMAT_UNSUPPORTED),
218   fmt(VK_FORMAT_ASTC_12x10_UNORM_BLOCK,  ISL_FORMAT_UNSUPPORTED),
219   fmt(VK_FORMAT_ASTC_12x10_SRGB_BLOCK,   ISL_FORMAT_UNSUPPORTED),
220   fmt(VK_FORMAT_ASTC_12x12_UNORM_BLOCK,  ISL_FORMAT_UNSUPPORTED),
221   fmt(VK_FORMAT_ASTC_12x12_SRGB_BLOCK,   ISL_FORMAT_UNSUPPORTED),
222   fmt(VK_FORMAT_B8G8R8_UNORM,            ISL_FORMAT_UNSUPPORTED),
223   fmt(VK_FORMAT_B8G8R8_SNORM,            ISL_FORMAT_UNSUPPORTED),
224   fmt(VK_FORMAT_B8G8R8_USCALED,          ISL_FORMAT_UNSUPPORTED),
225   fmt(VK_FORMAT_B8G8R8_SSCALED,          ISL_FORMAT_UNSUPPORTED),
226   fmt(VK_FORMAT_B8G8R8_UINT,             ISL_FORMAT_UNSUPPORTED),
227   fmt(VK_FORMAT_B8G8R8_SINT,             ISL_FORMAT_UNSUPPORTED),
228   fmt(VK_FORMAT_B8G8R8_SRGB,             ISL_FORMAT_UNSUPPORTED),
229   fmt(VK_FORMAT_B8G8R8A8_UNORM,          ISL_FORMAT_B8G8R8A8_UNORM),
230   fmt(VK_FORMAT_B8G8R8A8_SNORM,          ISL_FORMAT_UNSUPPORTED),
231   fmt(VK_FORMAT_B8G8R8A8_USCALED,        ISL_FORMAT_UNSUPPORTED),
232   fmt(VK_FORMAT_B8G8R8A8_SSCALED,        ISL_FORMAT_UNSUPPORTED),
233   fmt(VK_FORMAT_B8G8R8A8_UINT,           ISL_FORMAT_UNSUPPORTED),
234   fmt(VK_FORMAT_B8G8R8A8_SINT,           ISL_FORMAT_UNSUPPORTED),
235   fmt(VK_FORMAT_B8G8R8A8_SRGB,           ISL_FORMAT_B8G8R8A8_UNORM_SRGB),
236};
237
238#undef fmt
239
240const struct anv_format *
241anv_format_for_vk_format(VkFormat format)
242{
243   return &anv_formats[format];
244}
245
246/**
247 * Exactly one bit must be set in \a aspect.
248 */
249enum isl_format
250anv_get_isl_format(VkFormat format, VkImageAspectFlags aspect,
251                   VkImageTiling tiling, struct anv_format_swizzle *swizzle)
252{
253   const struct anv_format *anv_fmt = &anv_formats[format];
254
255   if (swizzle)
256      *swizzle = anv_fmt->swizzle;
257
258   switch (aspect) {
259   case VK_IMAGE_ASPECT_COLOR_BIT:
260      if (anv_fmt->isl_format == ISL_FORMAT_UNSUPPORTED) {
261         return ISL_FORMAT_UNSUPPORTED;
262      } else if (tiling == VK_IMAGE_TILING_OPTIMAL &&
263                 !util_is_power_of_two(anv_fmt->isl_layout->bs)) {
264         /* Tiled formats *must* be power-of-two because we need up upload
265          * them with the render pipeline.  For 3-channel formats, we fix
266          * this by switching them over to RGBX or RGBA formats under the
267          * hood.
268          */
269         enum isl_format rgbx = isl_format_rgb_to_rgbx(anv_fmt->isl_format);
270         if (rgbx != ISL_FORMAT_UNSUPPORTED)
271            return rgbx;
272         else
273            return isl_format_rgb_to_rgba(anv_fmt->isl_format);
274      } else {
275         return anv_fmt->isl_format;
276      }
277
278   case VK_IMAGE_ASPECT_DEPTH_BIT:
279   case (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT):
280      assert(anv_fmt->has_depth);
281      return anv_fmt->isl_format;
282
283   case VK_IMAGE_ASPECT_STENCIL_BIT:
284      assert(anv_fmt->has_stencil);
285      return ISL_FORMAT_R8_UINT;
286
287   default:
288      unreachable("bad VkImageAspect");
289      return ISL_FORMAT_UNSUPPORTED;
290   }
291}
292
293// Format capabilities
294
295void anv_validate_GetPhysicalDeviceFormatProperties(
296    VkPhysicalDevice                            physicalDevice,
297    VkFormat                                    _format,
298    VkFormatProperties*                         pFormatProperties)
299{
300   const struct anv_format *format = anv_format_for_vk_format(_format);
301   fprintf(stderr, "vkGetFormatProperties(%s)\n", format->name);
302   anv_GetPhysicalDeviceFormatProperties(physicalDevice, _format, pFormatProperties);
303}
304
305static VkFormatFeatureFlags
306get_image_format_properties(int gen, enum isl_format base,
307                            enum isl_format actual,
308                            struct anv_format_swizzle swizzle)
309{
310   const struct brw_surface_format_info *info = &surface_formats[actual];
311
312   if (actual == ISL_FORMAT_UNSUPPORTED || !info->exists)
313      return 0;
314
315   VkFormatFeatureFlags flags = 0;
316   if (info->sampling <= gen) {
317      flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
318               VK_FORMAT_FEATURE_BLIT_SRC_BIT;
319
320      if (info->filtering <= gen)
321         flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
322   }
323
324   /* We can render to swizzled formats.  However, if the alpha channel is
325    * moved, then blending won't work correctly.  The PRM tells us
326    * straight-up not to render to such a surface.
327    */
328   if (info->render_target <= gen && swizzle.a == 3) {
329      flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
330               VK_FORMAT_FEATURE_BLIT_DST_BIT;
331   }
332
333   if (info->alpha_blend <= gen && swizzle.a == 3)
334      flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
335
336   /* Load/store is determined based on base format.  This prevents RGB
337    * formats from showing up as load/store capable.
338    */
339   if (isl_is_storage_image_format(base))
340      flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
341
342   if (base == ISL_FORMAT_R32_SINT || base == ISL_FORMAT_R32_UINT)
343      flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
344
345   return flags;
346}
347
348static VkFormatFeatureFlags
349get_buffer_format_properties(int gen, enum isl_format format)
350{
351   const struct brw_surface_format_info *info = &surface_formats[format];
352
353   if (format == ISL_FORMAT_UNSUPPORTED || !info->exists)
354      return 0;
355
356   VkFormatFeatureFlags flags = 0;
357   if (info->sampling <= gen && !isl_format_is_compressed(format))
358      flags |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT;
359
360   if (info->input_vb <= gen)
361      flags |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
362
363   if (isl_is_storage_image_format(format))
364      flags |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
365
366   if (format == ISL_FORMAT_R32_SINT || format == ISL_FORMAT_R32_UINT)
367      flags |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
368
369   return flags;
370}
371
372static void
373anv_physical_device_get_format_properties(struct anv_physical_device *physical_device,
374                                          VkFormat format,
375                                          VkFormatProperties *out_properties)
376{
377   int gen = physical_device->info->gen * 10;
378   if (physical_device->info->is_haswell)
379      gen += 5;
380
381   VkFormatFeatureFlags linear = 0, tiled = 0, buffer = 0;
382   if (anv_format_is_depth_or_stencil(&anv_formats[format])) {
383      tiled |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
384      if (physical_device->info->gen >= 8) {
385         tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
386         tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT;
387      }
388      if (anv_formats[format].has_depth) {
389         tiled |= VK_FORMAT_FEATURE_BLIT_DST_BIT;
390      }
391   } else {
392      enum isl_format linear_fmt, tiled_fmt;
393      struct anv_format_swizzle linear_swizzle, tiled_swizzle;
394      linear_fmt = anv_get_isl_format(format, VK_IMAGE_ASPECT_COLOR_BIT,
395                                      VK_IMAGE_TILING_LINEAR, &linear_swizzle);
396      tiled_fmt = anv_get_isl_format(format, VK_IMAGE_ASPECT_COLOR_BIT,
397                                     VK_IMAGE_TILING_OPTIMAL, &tiled_swizzle);
398
399      linear = get_image_format_properties(gen, linear_fmt, linear_fmt,
400                                           linear_swizzle);
401      tiled = get_image_format_properties(gen, linear_fmt, tiled_fmt,
402                                          tiled_swizzle);
403      buffer = get_buffer_format_properties(gen, linear_fmt);
404
405      /* XXX: We handle 3-channel formats by switching them out for RGBX or
406       * RGBA formats behind-the-scenes.  This works fine for textures
407       * because the upload process will fill in the extra channel.
408       * We could also support it for render targets, but it will take
409       * substantially more work and we have enough RGBX formats to handle
410       * what most clients will want.
411       */
412      if (linear_fmt != ISL_FORMAT_UNSUPPORTED &&
413          !util_is_power_of_two(isl_format_layouts[linear_fmt].bs) &&
414          isl_format_rgb_to_rgbx(linear_fmt) == ISL_FORMAT_UNSUPPORTED) {
415         tiled &= ~VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT &
416                  ~VK_FORMAT_FEATURE_BLIT_DST_BIT;
417      }
418   }
419
420   out_properties->linearTilingFeatures = linear;
421   out_properties->optimalTilingFeatures = tiled;
422   out_properties->bufferFeatures = buffer;
423
424   return;
425}
426
427
428void anv_GetPhysicalDeviceFormatProperties(
429    VkPhysicalDevice                            physicalDevice,
430    VkFormat                                    format,
431    VkFormatProperties*                         pFormatProperties)
432{
433   ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
434
435   anv_physical_device_get_format_properties(
436               physical_device,
437               format,
438               pFormatProperties);
439}
440
441VkResult anv_GetPhysicalDeviceImageFormatProperties(
442    VkPhysicalDevice                            physicalDevice,
443    VkFormat                                    format,
444    VkImageType                                 type,
445    VkImageTiling                               tiling,
446    VkImageUsageFlags                           usage,
447    VkImageCreateFlags                          createFlags,
448    VkImageFormatProperties*                    pImageFormatProperties)
449{
450   ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
451   VkFormatProperties format_props;
452   VkFormatFeatureFlags format_feature_flags;
453   VkExtent3D maxExtent;
454   uint32_t maxMipLevels;
455   uint32_t maxArraySize;
456   VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
457
458   anv_physical_device_get_format_properties(physical_device, format,
459                                             &format_props);
460
461   /* Extract the VkFormatFeatureFlags that are relevant for the queried
462    * tiling.
463    */
464   if (tiling == VK_IMAGE_TILING_LINEAR) {
465      format_feature_flags = format_props.linearTilingFeatures;
466   } else if (tiling == VK_IMAGE_TILING_OPTIMAL) {
467      format_feature_flags = format_props.optimalTilingFeatures;
468   } else {
469      unreachable("bad VkImageTiling");
470   }
471
472   switch (type) {
473   default:
474      unreachable("bad VkImageType");
475   case VK_IMAGE_TYPE_1D:
476      maxExtent.width = 16384;
477      maxExtent.height = 1;
478      maxExtent.depth = 1;
479      maxMipLevels = 15; /* log2(maxWidth) + 1 */
480      maxArraySize = 2048;
481      sampleCounts = VK_SAMPLE_COUNT_1_BIT;
482      break;
483   case VK_IMAGE_TYPE_2D:
484      /* FINISHME: Does this really differ for cube maps? The documentation
485       * for RENDER_SURFACE_STATE suggests so.
486       */
487      maxExtent.width = 16384;
488      maxExtent.height = 16384;
489      maxExtent.depth = 1;
490      maxMipLevels = 15; /* log2(maxWidth) + 1 */
491      maxArraySize = 2048;
492      break;
493   case VK_IMAGE_TYPE_3D:
494      maxExtent.width = 2048;
495      maxExtent.height = 2048;
496      maxExtent.depth = 2048;
497      maxMipLevels = 12; /* log2(maxWidth) + 1 */
498      maxArraySize = 1;
499      break;
500   }
501
502   if (tiling == VK_IMAGE_TILING_OPTIMAL &&
503       type == VK_IMAGE_TYPE_2D &&
504       (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
505                                VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
506       !(createFlags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
507       !(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
508      sampleCounts = isl_device_get_sample_counts(&physical_device->isl_dev);
509   }
510
511   if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
512      /* Meta implements transfers by sampling from the source image. */
513      if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
514         goto unsupported;
515      }
516   }
517
518#if 0
519   if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
520      if (anv_format_for_vk_format(format)->has_stencil) {
521         /* Not yet implemented because copying to a W-tiled surface is crazy
522          * hard.
523          */
524         anv_finishme("support VK_IMAGE_USAGE_TRANSFER_DST_BIT for "
525                      "stencil format");
526         goto unsupported;
527      }
528   }
529#endif
530
531   if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
532      if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
533         goto unsupported;
534      }
535   }
536
537   if (usage & VK_IMAGE_USAGE_STORAGE_BIT) {
538      if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
539         goto unsupported;
540      }
541   }
542
543   if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
544      if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
545         goto unsupported;
546      }
547   }
548
549   if (usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
550      if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
551         goto unsupported;
552      }
553   }
554
555   if (usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
556      /* Nothing to check. */
557   }
558
559   if (usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
560      /* Ignore this flag because it was removed from the
561       * provisional_I_20150910 header.
562       */
563   }
564
565   *pImageFormatProperties = (VkImageFormatProperties) {
566      .maxExtent = maxExtent,
567      .maxMipLevels = maxMipLevels,
568      .maxArrayLayers = maxArraySize,
569      .sampleCounts = sampleCounts,
570
571      /* FINISHME: Accurately calculate
572       * VkImageFormatProperties::maxResourceSize.
573       */
574      .maxResourceSize = UINT32_MAX,
575   };
576
577   return VK_SUCCESS;
578
579unsupported:
580   *pImageFormatProperties = (VkImageFormatProperties) {
581      .maxExtent = { 0, 0, 0 },
582      .maxMipLevels = 0,
583      .maxArrayLayers = 0,
584      .sampleCounts = 0,
585      .maxResourceSize = 0,
586   };
587
588   return VK_SUCCESS;
589}
590
591void anv_GetPhysicalDeviceSparseImageFormatProperties(
592    VkPhysicalDevice                            physicalDevice,
593    VkFormat                                    format,
594    VkImageType                                 type,
595    uint32_t                                    samples,
596    VkImageUsageFlags                           usage,
597    VkImageTiling                               tiling,
598    uint32_t*                                   pNumProperties,
599    VkSparseImageFormatProperties*              pProperties)
600{
601   /* Sparse images are not yet supported. */
602   *pNumProperties = 0;
603}
604