anv_formats.c revision 28d0bc72fbce26fd986b0e3f3a1f346a9d6eba00
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
387      tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |
388               VK_FORMAT_FEATURE_BLIT_DST_BIT;
389   } else {
390      enum isl_format linear_fmt, tiled_fmt;
391      struct anv_format_swizzle linear_swizzle, tiled_swizzle;
392      linear_fmt = anv_get_isl_format(format, VK_IMAGE_ASPECT_COLOR_BIT,
393                                      VK_IMAGE_TILING_LINEAR, &linear_swizzle);
394      tiled_fmt = anv_get_isl_format(format, VK_IMAGE_ASPECT_COLOR_BIT,
395                                     VK_IMAGE_TILING_OPTIMAL, &tiled_swizzle);
396
397      linear = get_image_format_properties(gen, linear_fmt, linear_fmt,
398                                           linear_swizzle);
399      tiled = get_image_format_properties(gen, linear_fmt, tiled_fmt,
400                                          tiled_swizzle);
401      buffer = get_buffer_format_properties(gen, linear_fmt);
402
403      /* XXX: We handle 3-channel formats by switching them out for RGBX or
404       * RGBA formats behind-the-scenes.  This works fine for textures
405       * because the upload process will fill in the extra channel.
406       * We could also support it for render targets, but it will take
407       * substantially more work and we have enough RGBX formats to handle
408       * what most clients will want.
409       */
410      if (linear_fmt != ISL_FORMAT_UNSUPPORTED &&
411          !util_is_power_of_two(isl_format_layouts[linear_fmt].bs) &&
412          isl_format_rgb_to_rgbx(linear_fmt) == ISL_FORMAT_UNSUPPORTED) {
413         tiled &= ~VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT &
414                  ~VK_FORMAT_FEATURE_BLIT_DST_BIT;
415      }
416   }
417
418   out_properties->linearTilingFeatures = linear;
419   out_properties->optimalTilingFeatures = tiled;
420   out_properties->bufferFeatures = buffer;
421
422   return;
423}
424
425
426void anv_GetPhysicalDeviceFormatProperties(
427    VkPhysicalDevice                            physicalDevice,
428    VkFormat                                    format,
429    VkFormatProperties*                         pFormatProperties)
430{
431   ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
432
433   anv_physical_device_get_format_properties(
434               physical_device,
435               format,
436               pFormatProperties);
437}
438
439VkResult anv_GetPhysicalDeviceImageFormatProperties(
440    VkPhysicalDevice                            physicalDevice,
441    VkFormat                                    format,
442    VkImageType                                 type,
443    VkImageTiling                               tiling,
444    VkImageUsageFlags                           usage,
445    VkImageCreateFlags                          createFlags,
446    VkImageFormatProperties*                    pImageFormatProperties)
447{
448   ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
449   VkFormatProperties format_props;
450   VkFormatFeatureFlags format_feature_flags;
451   VkExtent3D maxExtent;
452   uint32_t maxMipLevels;
453   uint32_t maxArraySize;
454   VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
455
456   anv_physical_device_get_format_properties(physical_device, format,
457                                             &format_props);
458
459   /* Extract the VkFormatFeatureFlags that are relevant for the queried
460    * tiling.
461    */
462   if (tiling == VK_IMAGE_TILING_LINEAR) {
463      format_feature_flags = format_props.linearTilingFeatures;
464   } else if (tiling == VK_IMAGE_TILING_OPTIMAL) {
465      format_feature_flags = format_props.optimalTilingFeatures;
466   } else {
467      unreachable("bad VkImageTiling");
468   }
469
470   switch (type) {
471   default:
472      unreachable("bad VkImageType");
473   case VK_IMAGE_TYPE_1D:
474      maxExtent.width = 16384;
475      maxExtent.height = 1;
476      maxExtent.depth = 1;
477      maxMipLevels = 15; /* log2(maxWidth) + 1 */
478      maxArraySize = 2048;
479      sampleCounts = VK_SAMPLE_COUNT_1_BIT;
480      break;
481   case VK_IMAGE_TYPE_2D:
482      /* FINISHME: Does this really differ for cube maps? The documentation
483       * for RENDER_SURFACE_STATE suggests so.
484       */
485      maxExtent.width = 16384;
486      maxExtent.height = 16384;
487      maxExtent.depth = 1;
488      maxMipLevels = 15; /* log2(maxWidth) + 1 */
489      maxArraySize = 2048;
490      break;
491   case VK_IMAGE_TYPE_3D:
492      maxExtent.width = 2048;
493      maxExtent.height = 2048;
494      maxExtent.depth = 2048;
495      maxMipLevels = 12; /* log2(maxWidth) + 1 */
496      maxArraySize = 1;
497      break;
498   }
499
500   if (tiling == VK_IMAGE_TILING_OPTIMAL &&
501       type == VK_IMAGE_TYPE_2D &&
502       (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
503                                VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
504       !(createFlags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
505       !(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
506      sampleCounts = isl_device_get_sample_counts(&physical_device->isl_dev);
507   }
508
509   if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
510      /* Meta implements transfers by sampling from the source image. */
511      if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
512         goto unsupported;
513      }
514   }
515
516#if 0
517   if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
518      if (anv_format_for_vk_format(format)->has_stencil) {
519         /* Not yet implemented because copying to a W-tiled surface is crazy
520          * hard.
521          */
522         anv_finishme("support VK_IMAGE_USAGE_TRANSFER_DST_BIT for "
523                      "stencil format");
524         goto unsupported;
525      }
526   }
527#endif
528
529   if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
530      if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
531         goto unsupported;
532      }
533   }
534
535   if (usage & VK_IMAGE_USAGE_STORAGE_BIT) {
536      if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
537         goto unsupported;
538      }
539   }
540
541   if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
542      if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
543         goto unsupported;
544      }
545   }
546
547   if (usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
548      if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
549         goto unsupported;
550      }
551   }
552
553   if (usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
554      /* Nothing to check. */
555   }
556
557   if (usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
558      /* Ignore this flag because it was removed from the
559       * provisional_I_20150910 header.
560       */
561   }
562
563   *pImageFormatProperties = (VkImageFormatProperties) {
564      .maxExtent = maxExtent,
565      .maxMipLevels = maxMipLevels,
566      .maxArrayLayers = maxArraySize,
567      .sampleCounts = sampleCounts,
568
569      /* FINISHME: Accurately calculate
570       * VkImageFormatProperties::maxResourceSize.
571       */
572      .maxResourceSize = UINT32_MAX,
573   };
574
575   return VK_SUCCESS;
576
577unsupported:
578   *pImageFormatProperties = (VkImageFormatProperties) {
579      .maxExtent = { 0, 0, 0 },
580      .maxMipLevels = 0,
581      .maxArrayLayers = 0,
582      .sampleCounts = 0,
583      .maxResourceSize = 0,
584   };
585
586   return VK_ERROR_FORMAT_NOT_SUPPORTED;
587}
588
589void anv_GetPhysicalDeviceSparseImageFormatProperties(
590    VkPhysicalDevice                            physicalDevice,
591    VkFormat                                    format,
592    VkImageType                                 type,
593    uint32_t                                    samples,
594    VkImageUsageFlags                           usage,
595    VkImageTiling                               tiling,
596    uint32_t*                                   pNumProperties,
597    VkSparseImageFormatProperties*              pProperties)
598{
599   /* Sparse images are not yet supported. */
600   *pNumProperties = 0;
601}
602