intel: Enable CCS_E for some formats on Gen12
[mesa.git] / src / intel / isl / isl_format.c
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 <assert.h>
25
26 #include "isl.h"
27 #include "isl_priv.h"
28 #include "dev/gen_device_info.h"
29
30 #include "main/macros.h" /* Needed for MAX3 and MAX2 for format_rgb9e5 */
31 #include "util/format_srgb.h"
32 #include "util/format_rgb9e5.h"
33 #include "util/format_r11g11b10f.h"
34
35 /* Header-only format conversion include */
36 #include "main/format_utils.h"
37
38 struct surface_format_info {
39 bool exists;
40 uint8_t sampling;
41 uint8_t filtering;
42 uint8_t shadow_compare;
43 uint8_t chroma_key;
44 uint8_t render_target;
45 uint8_t alpha_blend;
46 uint8_t input_vb;
47 uint8_t streamed_output_vb;
48 uint8_t color_processing;
49 uint8_t typed_write;
50 uint8_t typed_read;
51 uint8_t ccs_e;
52 };
53
54 /* This macro allows us to write the table almost as it appears in the PRM,
55 * while restructuring it to turn it into the C code we want.
56 */
57 #define SF(sampl, filt, shad, ck, rt, ab, vb, so, color, tw, tr, ccs_e, sf) \
58 [ISL_FORMAT_##sf] = { true, sampl, filt, shad, ck, rt, ab, vb, so, color, tw, tr, ccs_e},
59
60 #define Y 0
61 #define x 255
62 /**
63 * This is the table of support for surface (texture, renderbuffer, and vertex
64 * buffer, but not depthbuffer) formats across the various hardware generations.
65 *
66 * The table is formatted to match the documentation, except that the docs have
67 * this ridiculous mapping of Y[*+~^#&] for "supported on DevWhatever". To put
68 * it in our table, here's the mapping:
69 *
70 * Y*: 45
71 * Y+: 45 (g45/gm45)
72 * Y~: 50 (gen5)
73 * Y^: 60 (gen6)
74 * Y#: 70 (gen7)
75 *
76 * The abbreviations in the header below are:
77 * smpl - Sampling Engine
78 * filt - Sampling Engine Filtering
79 * shad - Sampling Engine Shadow Map
80 * CK - Sampling Engine Chroma Key
81 * RT - Render Target
82 * AB - Alpha Blend Render Target
83 * VB - Input Vertex Buffer
84 * SO - Steamed Output Vertex Buffers (transform feedback)
85 * color - Color Processing
86 * ccs_e - Lossless Compression Support (gen9+ only)
87 * sf - Surface Format
88 *
89 * See page 88 of the Sandybridge PRM VOL4_Part1 PDF.
90 *
91 * As of Ivybridge, the columns are no longer in that table and the
92 * information can be found spread across:
93 *
94 * - VOL2_Part1 section 2.5.11 Format Conversion (vertex fetch).
95 * - VOL4_Part1 section 2.12.2.1.2 Sampler Output Channel Mapping.
96 * - VOL4_Part1 section 3.9.11 Render Target Write.
97 * - Render Target Surface Types [SKL+]
98 */
99 static const struct surface_format_info format_info[] = {
100 /* smpl filt shad CK RT AB VB SO color TW TR ccs_e */
101 SF( Y, 50, x, x, Y, Y, Y, Y, x, 70, 90, 90, R32G32B32A32_FLOAT)
102 SF( Y, x, x, x, Y, x, Y, Y, x, 70, 90, 90, R32G32B32A32_SINT)
103 SF( Y, x, x, x, Y, x, Y, Y, x, 70, 90, 90, R32G32B32A32_UINT)
104 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32B32A32_UNORM)
105 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32B32A32_SNORM)
106 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R64G64_FLOAT)
107 SF( Y, 50, x, x, 100, 100, x, x, x, x, x, 100, R32G32B32X32_FLOAT)
108 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32B32A32_SSCALED)
109 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32B32A32_USCALED)
110 SF( x, x, x, x, x, x, 75, x, x, x, x, x, R32G32B32A32_SFIXED)
111 SF( x, x, x, x, x, x, 80, x, x, x, x, x, R64G64_PASSTHRU)
112 SF( Y, 50, x, x, x, x, Y, Y, x, x, x, x, R32G32B32_FLOAT)
113 SF( Y, x, x, x, x, x, Y, Y, x, x, x, x, R32G32B32_SINT)
114 SF( Y, x, x, x, x, x, Y, Y, x, x, x, x, R32G32B32_UINT)
115 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32B32_UNORM)
116 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32B32_SNORM)
117 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32B32_SSCALED)
118 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32B32_USCALED)
119 SF( x, x, x, x, x, x, 75, x, x, x, x, x, R32G32B32_SFIXED)
120 SF( Y, Y, x, x, Y, 45, Y, x, 60, 70, 110, 90, R16G16B16A16_UNORM)
121 SF( Y, Y, x, x, Y, 60, Y, x, x, 70, 110, 90, R16G16B16A16_SNORM)
122 SF( Y, x, x, x, Y, x, Y, x, x, 70, 90, 90, R16G16B16A16_SINT)
123 SF( Y, x, x, x, Y, x, Y, x, x, 70, 75, 90, R16G16B16A16_UINT)
124 SF( Y, Y, x, x, Y, Y, Y, x, x, 70, 90, 90, R16G16B16A16_FLOAT)
125 SF( Y, 50, x, x, Y, Y, Y, Y, x, 70, 90, 90, R32G32_FLOAT)
126 SF( Y, 70, x, x, Y, Y, Y, Y, x, x, x, x, R32G32_FLOAT_LD)
127 SF( Y, x, x, x, Y, x, Y, Y, x, 70, 90, 90, R32G32_SINT)
128 SF( Y, x, x, x, Y, x, Y, Y, x, 70, 90, 90, R32G32_UINT)
129 SF( Y, 50, Y, x, x, x, x, x, x, x, x, x, R32_FLOAT_X8X24_TYPELESS)
130 SF( Y, x, x, x, x, x, x, x, x, x, x, x, X32_TYPELESS_G8X24_UINT)
131 SF( Y, 50, x, x, x, x, x, x, x, x, x, x, L32A32_FLOAT)
132 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32_UNORM)
133 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32_SNORM)
134 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R64_FLOAT)
135 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, R16G16B16X16_UNORM)
136 SF( Y, Y, x, x, 90, 90, x, x, x, x, x, 90, R16G16B16X16_FLOAT)
137 SF( Y, 50, x, x, x, x, x, x, x, x, x, x, A32X32_FLOAT)
138 SF( Y, 50, x, x, x, x, x, x, x, x, x, x, L32X32_FLOAT)
139 SF( Y, 50, x, x, x, x, x, x, x, x, x, x, I32X32_FLOAT)
140 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R16G16B16A16_SSCALED)
141 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R16G16B16A16_USCALED)
142 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32_SSCALED)
143 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32G32_USCALED)
144 SF( x, x, x, x, x, x, 75, x, x, x, x, x, R32G32_SFIXED)
145 SF( x, x, x, x, x, x, 80, x, x, x, x, x, R64_PASSTHRU)
146 SF( Y, Y, x, Y, Y, Y, Y, x, 60, 70, x, 90, B8G8R8A8_UNORM)
147 SF( Y, Y, x, x, Y, Y, x, x, x, x, x, 100, B8G8R8A8_UNORM_SRGB)
148 /* smpl filt shad CK RT AB VB SO color TW TR ccs_e */
149 SF( Y, Y, x, x, Y, Y, Y, x, 60, 70, x, 100, R10G10B10A2_UNORM)
150 SF( Y, Y, x, x, x, x, x, x, 60, x, x, 120, R10G10B10A2_UNORM_SRGB)
151 SF( Y, x, x, x, Y, x, Y, x, x, 70, x, 100, R10G10B10A2_UINT)
152 SF( Y, Y, x, x, x, x, Y, x, x, x, x, x, R10G10B10_SNORM_A2_UNORM)
153 SF( Y, Y, x, x, Y, Y, Y, x, 60, 70, 110, 90, R8G8B8A8_UNORM)
154 SF( Y, Y, x, x, Y, Y, x, x, 60, x, x, 100, R8G8B8A8_UNORM_SRGB)
155 SF( Y, Y, x, x, Y, 60, Y, x, x, 70, 110, 90, R8G8B8A8_SNORM)
156 SF( Y, x, x, x, Y, x, Y, x, x, 70, 90, 90, R8G8B8A8_SINT)
157 SF( Y, x, x, x, Y, x, Y, x, x, 70, 75, 90, R8G8B8A8_UINT)
158 SF( Y, Y, x, x, Y, 45, Y, x, x, 70, 110, 90, R16G16_UNORM)
159 SF( Y, Y, x, x, Y, 60, Y, x, x, 70, 110, 90, R16G16_SNORM)
160 SF( Y, x, x, x, Y, x, Y, x, x, 70, 90, 90, R16G16_SINT)
161 SF( Y, x, x, x, Y, x, Y, x, x, 70, 75, 90, R16G16_UINT)
162 SF( Y, Y, x, x, Y, Y, Y, x, x, 70, 90, 90, R16G16_FLOAT)
163 SF( Y, Y, x, x, Y, Y, 75, x, 60, 70, x, 100, B10G10R10A2_UNORM)
164 SF( Y, Y, x, x, Y, Y, x, x, 60, x, x, 100, B10G10R10A2_UNORM_SRGB)
165 SF( Y, Y, x, x, Y, Y, Y, x, x, 70, x, 100, R11G11B10_FLOAT)
166 SF(120, 120, x, x, 120, 120, x, x, x, x, x, 120, R10G10B10_FLOAT_A2_UNORM)
167 SF( Y, x, x, x, Y, x, Y, Y, x, 70, 70, 90, R32_SINT)
168 SF( Y, x, x, x, Y, x, Y, Y, x, 70, 70, 90, R32_UINT)
169 SF( Y, 50, Y, x, Y, Y, Y, Y, x, 70, 70, 90, R32_FLOAT)
170 SF( Y, 50, Y, x, x, x, x, x, x, x, x, x, R24_UNORM_X8_TYPELESS)
171 SF( Y, x, x, x, x, x, x, x, x, x, x, x, X24_TYPELESS_G8_UINT)
172 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, L16A16_UNORM)
173 SF( Y, 50, Y, x, x, x, x, x, x, x, x, x, I24X8_UNORM)
174 SF( Y, 50, Y, x, x, x, x, x, x, x, x, x, L24X8_UNORM)
175 SF( Y, 50, Y, x, x, x, x, x, x, x, x, x, A24X8_UNORM)
176 SF( Y, 50, Y, x, x, x, x, x, x, x, x, x, I32_FLOAT)
177 SF( Y, 50, Y, x, x, x, x, x, x, x, x, x, L32_FLOAT)
178 SF( Y, 50, Y, x, x, x, x, x, x, x, x, x, A32_FLOAT)
179 SF( Y, Y, x, Y, 80, 80, x, x, 60, x, x, 90, B8G8R8X8_UNORM)
180 SF( Y, Y, x, x, 80, 80, x, x, x, x, x, 100, B8G8R8X8_UNORM_SRGB)
181 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, R8G8B8X8_UNORM)
182 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, R8G8B8X8_UNORM_SRGB)
183 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, R9G9B9E5_SHAREDEXP)
184 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, B10G10R10X2_UNORM)
185 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, L16A16_FLOAT)
186 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32_UNORM)
187 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32_SNORM)
188 /* smpl filt shad CK RT AB VB SO color TW TR ccs_e */
189 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R10G10B10X2_USCALED)
190 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R8G8B8A8_SSCALED)
191 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R8G8B8A8_USCALED)
192 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R16G16_SSCALED)
193 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R16G16_USCALED)
194 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32_SSCALED)
195 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R32_USCALED)
196 SF( Y, Y, x, Y, Y, Y, x, x, x, 70, x, 120, B5G6R5_UNORM)
197 SF( Y, Y, x, x, Y, Y, x, x, x, x, x, 120, B5G6R5_UNORM_SRGB)
198 SF( Y, Y, x, Y, Y, Y, x, x, x, 70, x, 120, B5G5R5A1_UNORM)
199 SF( Y, Y, x, x, Y, Y, x, x, x, x, x, 120, B5G5R5A1_UNORM_SRGB)
200 SF( Y, Y, x, Y, Y, Y, x, x, x, 70, x, 120, B4G4R4A4_UNORM)
201 SF( Y, Y, x, x, Y, Y, x, x, x, x, x, 120, B4G4R4A4_UNORM_SRGB)
202 SF( Y, Y, x, x, Y, Y, Y, x, x, 70, 110, 120, R8G8_UNORM)
203 SF( Y, Y, x, Y, Y, 60, Y, x, x, 70, 110, 120, R8G8_SNORM)
204 SF( Y, x, x, x, Y, x, Y, x, x, 70, 90, 120, R8G8_SINT)
205 SF( Y, x, x, x, Y, x, Y, x, x, 70, 75, 120, R8G8_UINT)
206 SF( Y, Y, Y, x, Y, 45, Y, x, 70, 70, 110, 120, R16_UNORM)
207 SF( Y, Y, x, x, Y, 60, Y, x, x, 70, 110, 120, R16_SNORM)
208 SF( Y, x, x, x, Y, x, Y, x, x, 70, 90, 120, R16_SINT)
209 SF( Y, x, x, x, Y, x, Y, x, x, 70, 75, 120, R16_UINT)
210 SF( Y, Y, x, x, Y, Y, Y, x, x, 70, 90, 120, R16_FLOAT)
211 SF( 50, 50, x, x, x, x, x, x, x, x, x, x, A8P8_UNORM_PALETTE0)
212 SF( 50, 50, x, x, x, x, x, x, x, x, x, x, A8P8_UNORM_PALETTE1)
213 SF( Y, Y, Y, x, x, x, x, x, x, x, x, x, I16_UNORM)
214 SF( Y, Y, Y, x, x, x, x, x, x, x, x, x, L16_UNORM)
215 SF( Y, Y, Y, x, x, x, x, x, x, x, x, x, A16_UNORM)
216 SF( Y, Y, x, Y, x, x, x, x, x, x, x, x, L8A8_UNORM)
217 SF( Y, Y, Y, x, x, x, x, x, x, x, x, x, I16_FLOAT)
218 SF( Y, Y, Y, x, x, x, x, x, x, x, x, x, L16_FLOAT)
219 SF( Y, Y, Y, x, x, x, x, x, x, x, x, x, A16_FLOAT)
220 SF( 45, 45, x, x, x, x, x, x, x, x, x, x, L8A8_UNORM_SRGB)
221 SF( Y, Y, x, Y, x, x, x, x, x, x, x, x, R5G5_SNORM_B6_UNORM)
222 SF( x, x, x, x, Y, Y, x, x, x, 70, x, 120, B5G5R5X1_UNORM)
223 SF( x, x, x, x, Y, Y, x, x, x, x, x, 120, B5G5R5X1_UNORM_SRGB)
224 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R8G8_SSCALED)
225 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R8G8_USCALED)
226 /* smpl filt shad CK RT AB VB SO color TW TR ccs_e */
227 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R16_SSCALED)
228 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R16_USCALED)
229 SF( 50, 50, x, x, x, x, x, x, x, x, x, x, P8A8_UNORM_PALETTE0)
230 SF( 50, 50, x, x, x, x, x, x, x, x, x, x, P8A8_UNORM_PALETTE1)
231 SF(120, 120, x, x, 120, 120, x, x, x, x, x, 120, A1B5G5R5_UNORM)
232 /* According to the PRM, A4B4G4R4_UNORM isn't supported until Sky Lake
233 * but empirical testing indicates that at least sampling works just fine
234 * on Broadwell.
235 */
236 SF( 80, 80, x, x, 90, 120, x, x, x, x, x, 120, A4B4G4R4_UNORM)
237 SF( 90, x, x, x, x, x, x, x, x, x, x, x, L8A8_UINT)
238 SF( 90, x, x, x, x, x, x, x, x, x, x, x, L8A8_SINT)
239 SF( Y, Y, x, 45, Y, Y, Y, x, x, 70, 110, 120, R8_UNORM)
240 SF( Y, Y, x, x, Y, 60, Y, x, x, 70, 110, 120, R8_SNORM)
241 SF( Y, x, x, x, Y, x, Y, x, x, 70, 90, 120, R8_SINT)
242 SF( Y, x, x, x, Y, x, Y, x, x, 70, 75, 120, R8_UINT)
243 SF( Y, Y, x, Y, Y, Y, x, x, x, 70, 110, 120, A8_UNORM)
244 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, I8_UNORM)
245 SF( Y, Y, x, Y, x, x, x, x, x, x, x, x, L8_UNORM)
246 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, P4A4_UNORM_PALETTE0)
247 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, A4P4_UNORM_PALETTE0)
248 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R8_SSCALED)
249 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R8_USCALED)
250 SF( 45, 45, x, x, x, x, x, x, x, x, x, x, P8_UNORM_PALETTE0)
251 SF( 45, 45, x, x, x, x, x, x, x, x, x, x, L8_UNORM_SRGB)
252 SF( 45, 45, x, x, x, x, x, x, x, x, x, x, P8_UNORM_PALETTE1)
253 SF( 45, 45, x, x, x, x, x, x, x, x, x, x, P4A4_UNORM_PALETTE1)
254 SF( 45, 45, x, x, x, x, x, x, x, x, x, x, A4P4_UNORM_PALETTE1)
255 SF( x, x, x, x, x, x, x, x, x, x, x, x, Y8_UNORM)
256 SF( 90, x, x, x, x, x, x, x, x, x, x, x, L8_UINT)
257 SF( 90, x, x, x, x, x, x, x, x, x, x, x, L8_SINT)
258 SF( 90, x, x, x, x, x, x, x, x, x, x, x, I8_UINT)
259 SF( 90, x, x, x, x, x, x, x, x, x, x, x, I8_SINT)
260 SF( 45, 45, x, x, x, x, x, x, x, x, x, x, DXT1_RGB_SRGB)
261 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, R1_UNORM)
262 SF( Y, Y, x, Y, Y, x, x, x, 60, x, x, x, YCRCB_NORMAL)
263 SF( Y, Y, x, Y, Y, x, x, x, 60, x, x, x, YCRCB_SWAPUVY)
264 SF( 45, 45, x, x, x, x, x, x, x, x, x, x, P2_UNORM_PALETTE0)
265 SF( 45, 45, x, x, x, x, x, x, x, x, x, x, P2_UNORM_PALETTE1)
266 SF( Y, Y, x, Y, x, x, x, x, x, x, x, x, BC1_UNORM)
267 SF( Y, Y, x, Y, x, x, x, x, x, x, x, x, BC2_UNORM)
268 SF( Y, Y, x, Y, x, x, x, x, x, x, x, x, BC3_UNORM)
269 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, BC4_UNORM)
270 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, BC5_UNORM)
271 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, BC1_UNORM_SRGB)
272 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, BC2_UNORM_SRGB)
273 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, BC3_UNORM_SRGB)
274 SF( Y, x, x, x, x, x, x, x, x, x, x, x, MONO8)
275 SF( Y, Y, x, x, Y, x, x, x, 60, x, x, x, YCRCB_SWAPUV)
276 SF( Y, Y, x, x, Y, x, x, x, 60, x, x, x, YCRCB_SWAPY)
277 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, DXT1_RGB)
278 /* smpl filt shad CK RT AB VB SO color TW TR ccs_e */
279 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, FXT1)
280 SF( 75, 75, x, x, x, x, Y, x, x, x, x, x, R8G8B8_UNORM)
281 SF( 75, 75, x, x, x, x, Y, x, x, x, x, x, R8G8B8_SNORM)
282 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R8G8B8_SSCALED)
283 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R8G8B8_USCALED)
284 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R64G64B64A64_FLOAT)
285 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R64G64B64_FLOAT)
286 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, BC4_SNORM)
287 SF( Y, Y, x, x, x, x, x, x, x, x, x, x, BC5_SNORM)
288 SF( 50, 50, x, x, x, x, 60, x, x, x, x, x, R16G16B16_FLOAT)
289 SF( 75, 75, x, x, x, x, Y, x, x, x, x, x, R16G16B16_UNORM)
290 SF( 75, 75, x, x, x, x, Y, x, x, x, x, x, R16G16B16_SNORM)
291 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R16G16B16_SSCALED)
292 SF( x, x, x, x, x, x, Y, x, x, x, x, x, R16G16B16_USCALED)
293 SF( 70, 70, x, x, x, x, x, x, x, x, x, x, BC6H_SF16)
294 SF( 70, 70, x, x, x, x, x, x, x, x, x, x, BC7_UNORM)
295 SF( 70, 70, x, x, x, x, x, x, x, x, x, x, BC7_UNORM_SRGB)
296 SF( 70, 70, x, x, x, x, x, x, x, x, x, x, BC6H_UF16)
297 SF( x, x, x, x, x, x, x, x, x, x, x, x, PLANAR_420_8)
298 /* The format enum for R8G8B8_UNORM_SRGB first shows up in the HSW PRM but
299 * empirical testing indicates that it doesn't actually sRGB decode and
300 * acts identical to R8G8B8_UNORM. It does work on gen8+.
301 */
302 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, R8G8B8_UNORM_SRGB)
303 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, ETC1_RGB8)
304 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, ETC2_RGB8)
305 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, EAC_R11)
306 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, EAC_RG11)
307 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, EAC_SIGNED_R11)
308 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, EAC_SIGNED_RG11)
309 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, ETC2_SRGB8)
310 SF( 90, x, x, x, x, x, 75, x, x, x, x, x, R16G16B16_UINT)
311 SF( 90, x, x, x, x, x, 75, x, x, x, x, x, R16G16B16_SINT)
312 SF( x, x, x, x, x, x, 75, x, x, x, x, x, R32_SFIXED)
313 SF( x, x, x, x, x, x, 75, x, x, x, x, x, R10G10B10A2_SNORM)
314 SF( x, x, x, x, x, x, 75, x, x, x, x, x, R10G10B10A2_USCALED)
315 SF( x, x, x, x, x, x, 75, x, x, x, x, x, R10G10B10A2_SSCALED)
316 SF( x, x, x, x, x, x, 75, x, x, x, x, x, R10G10B10A2_SINT)
317 SF( x, x, x, x, x, x, 75, x, x, x, x, x, B10G10R10A2_SNORM)
318 SF( x, x, x, x, x, x, 75, x, x, x, x, x, B10G10R10A2_USCALED)
319 SF( x, x, x, x, x, x, 75, x, x, x, x, x, B10G10R10A2_SSCALED)
320 SF( x, x, x, x, x, x, 75, x, x, x, x, x, B10G10R10A2_UINT)
321 SF( x, x, x, x, x, x, 75, x, x, x, x, x, B10G10R10A2_SINT)
322 SF( x, x, x, x, x, x, 80, x, x, x, x, x, R64G64B64A64_PASSTHRU)
323 SF( x, x, x, x, x, x, 80, x, x, x, x, x, R64G64B64_PASSTHRU)
324 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, ETC2_RGB8_PTA)
325 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, ETC2_SRGB8_PTA)
326 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, ETC2_EAC_RGBA8)
327 SF( 80, 80, x, x, x, x, x, x, x, x, x, x, ETC2_EAC_SRGB8_A8)
328 SF( 90, x, x, x, x, x, 75, x, x, x, x, x, R8G8B8_UINT)
329 SF( 90, x, x, x, x, x, 75, x, x, x, x, x, R8G8B8_SINT)
330 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_4X4_FLT16)
331 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_5X4_FLT16)
332 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_5X5_FLT16)
333 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_6X5_FLT16)
334 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_6X6_FLT16)
335 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_8X5_FLT16)
336 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_8X6_FLT16)
337 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_8X8_FLT16)
338 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_10X5_FLT16)
339 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_10X6_FLT16)
340 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_10X8_FLT16)
341 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_10X10_FLT16)
342 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_12X10_FLT16)
343 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_12X12_FLT16)
344 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_4X4_U8SRGB)
345 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_5X4_U8SRGB)
346 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_5X5_U8SRGB)
347 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_6X5_U8SRGB)
348 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_6X6_U8SRGB)
349 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_8X5_U8SRGB)
350 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_8X6_U8SRGB)
351 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_8X8_U8SRGB)
352 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_10X5_U8SRGB)
353 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_10X6_U8SRGB)
354 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_10X8_U8SRGB)
355 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_10X10_U8SRGB)
356 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_12X10_U8SRGB)
357 SF( 90, 90, x, x, x, x, x, x, x, x, x, x, ASTC_LDR_2D_12X12_U8SRGB)
358 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_4X4_FLT16)
359 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_5X4_FLT16)
360 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_5X5_FLT16)
361 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_6X5_FLT16)
362 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_6X6_FLT16)
363 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_8X5_FLT16)
364 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_8X6_FLT16)
365 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_8X8_FLT16)
366 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_10X5_FLT16)
367 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_10X6_FLT16)
368 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_10X8_FLT16)
369 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_10X10_FLT16)
370 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_12X10_FLT16)
371 SF(100, 100, x, x, x, x, x, x, x, x, x, x, ASTC_HDR_2D_12X12_FLT16)
372 };
373 #undef x
374 #undef Y
375
376 static unsigned
377 format_gen(const struct gen_device_info *devinfo)
378 {
379 return devinfo->gen * 10 + (devinfo->is_g4x || devinfo->is_haswell) * 5;
380 }
381
382 static bool
383 format_info_exists(enum isl_format format)
384 {
385 assert(format != ISL_FORMAT_UNSUPPORTED);
386 assert(format < ISL_NUM_FORMATS);
387 return format < ARRAY_SIZE(format_info) && format_info[format].exists;
388 }
389
390 bool
391 isl_format_supports_rendering(const struct gen_device_info *devinfo,
392 enum isl_format format)
393 {
394 if (!format_info_exists(format))
395 return false;
396
397 return format_gen(devinfo) >= format_info[format].render_target;
398 }
399
400 bool
401 isl_format_supports_alpha_blending(const struct gen_device_info *devinfo,
402 enum isl_format format)
403 {
404 if (!format_info_exists(format))
405 return false;
406
407 return format_gen(devinfo) >= format_info[format].alpha_blend;
408 }
409
410 bool
411 isl_format_supports_sampling(const struct gen_device_info *devinfo,
412 enum isl_format format)
413 {
414 if (!format_info_exists(format))
415 return false;
416
417 if (devinfo->is_baytrail) {
418 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
419 /* Support for ETC1 and ETC2 exists on Bay Trail even though big-core
420 * GPUs didn't get it until Broadwell.
421 */
422 if (fmtl->txc == ISL_TXC_ETC1 || fmtl->txc == ISL_TXC_ETC2)
423 return true;
424 } else if (devinfo->is_cherryview) {
425 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
426 /* Support for ASTC LDR exists on Cherry View even though big-core
427 * GPUs didn't get it until Skylake.
428 */
429 if (fmtl->txc == ISL_TXC_ASTC)
430 return format < ISL_FORMAT_ASTC_HDR_2D_4X4_FLT16;
431 } else if (gen_device_info_is_9lp(devinfo)) {
432 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
433 /* Support for ASTC HDR exists on Broxton even though big-core
434 * GPUs didn't get it until Cannonlake.
435 */
436 if (fmtl->txc == ISL_TXC_ASTC)
437 return true;
438 }
439
440 return format_gen(devinfo) >= format_info[format].sampling;
441 }
442
443 bool
444 isl_format_supports_filtering(const struct gen_device_info *devinfo,
445 enum isl_format format)
446 {
447 if (!format_info_exists(format))
448 return false;
449
450 if (devinfo->is_baytrail) {
451 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
452 /* Support for ETC1 and ETC2 exists on Bay Trail even though big-core
453 * GPUs didn't get it until Broadwell.
454 */
455 if (fmtl->txc == ISL_TXC_ETC1 || fmtl->txc == ISL_TXC_ETC2)
456 return true;
457 } else if (devinfo->is_cherryview) {
458 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
459 /* Support for ASTC LDR exists on Cherry View even though big-core
460 * GPUs didn't get it until Skylake.
461 */
462 if (fmtl->txc == ISL_TXC_ASTC)
463 return format < ISL_FORMAT_ASTC_HDR_2D_4X4_FLT16;
464 } else if (gen_device_info_is_9lp(devinfo)) {
465 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
466 /* Support for ASTC HDR exists on Broxton even though big-core
467 * GPUs didn't get it until Cannonlake.
468 */
469 if (fmtl->txc == ISL_TXC_ASTC)
470 return true;
471 }
472
473 return format_gen(devinfo) >= format_info[format].filtering;
474 }
475
476 bool
477 isl_format_supports_vertex_fetch(const struct gen_device_info *devinfo,
478 enum isl_format format)
479 {
480 if (!format_info_exists(format))
481 return false;
482
483 /* For vertex fetch, Bay Trail supports the same set of formats as Haswell
484 * but is a superset of Ivy Bridge.
485 */
486 if (devinfo->is_baytrail)
487 return 75 >= format_info[format].input_vb;
488
489 return format_gen(devinfo) >= format_info[format].input_vb;
490 }
491
492 /**
493 * Returns true if the given format can support typed writes.
494 */
495 bool
496 isl_format_supports_typed_writes(const struct gen_device_info *devinfo,
497 enum isl_format format)
498 {
499 if (!format_info_exists(format))
500 return false;
501
502 return format_gen(devinfo) >= format_info[format].typed_write;
503 }
504
505
506 /**
507 * Returns true if the given format can support typed reads with format
508 * conversion fully handled by hardware. On Sky Lake, all formats which are
509 * supported for typed writes also support typed reads but some of them return
510 * the raw image data and don't provide format conversion.
511 *
512 * For anyone looking to find this data in the PRM, the easiest way to find
513 * format tables is to search for R11G11B10. There are only a few
514 * occurrences.
515 */
516 bool
517 isl_format_supports_typed_reads(const struct gen_device_info *devinfo,
518 enum isl_format format)
519 {
520 if (!format_info_exists(format))
521 return false;
522
523 return format_gen(devinfo) >= format_info[format].typed_read;
524 }
525
526 /**
527 * Returns true if the given format can support single-sample fast clears.
528 * This function only checks the format. In order to determine if a surface
529 * supports CCS_E, several other factors need to be considered such as tiling
530 * and sample count. See isl_surf_get_ccs_surf for details.
531 */
532 bool
533 isl_format_supports_ccs_d(const struct gen_device_info *devinfo,
534 enum isl_format format)
535 {
536 /* Clear-only compression was first added on Ivy Bridge and was last
537 * implemented on Ice lake (see BSpec: 43862).
538 */
539 if (devinfo->gen < 7 || devinfo->gen > 11)
540 return false;
541
542 if (!isl_format_supports_rendering(devinfo, format))
543 return false;
544
545 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
546
547 return fmtl->bpb == 32 || fmtl->bpb == 64 || fmtl->bpb == 128;
548 }
549
550 /**
551 * Returns true if the given format can support single-sample color
552 * compression. This function only checks the format. In order to determine
553 * if a surface supports CCS_E, several other factors need to be considered
554 * such as tiling and sample count. See isl_surf_get_ccs_surf for details.
555 */
556 bool
557 isl_format_supports_ccs_e(const struct gen_device_info *devinfo,
558 enum isl_format format)
559 {
560 if (!format_info_exists(format))
561 return false;
562
563 /* For simplicity, only report that a format supports CCS_E if blorp can
564 * perform bit-for-bit copies with an image of that format while compressed.
565 * This allows ISL users to avoid having to resolve the image before
566 * performing such a copy. We may want to change this behavior in the
567 * future.
568 *
569 * The following formats have no equivalent UINT format. Given how
570 * blorp_copy currently works, bit-for-bit copy operations are not possible
571 * without an intermediate resolve.
572 */
573 switch (format) {
574 case ISL_FORMAT_R11G11B10_FLOAT:
575 case ISL_FORMAT_B5G5R5X1_UNORM:
576 case ISL_FORMAT_B5G5R5X1_UNORM_SRGB:
577 case ISL_FORMAT_B5G5R5A1_UNORM:
578 case ISL_FORMAT_B5G5R5A1_UNORM_SRGB:
579 case ISL_FORMAT_A4B4G4R4_UNORM:
580 case ISL_FORMAT_B4G4R4A4_UNORM:
581 case ISL_FORMAT_B4G4R4A4_UNORM_SRGB:
582 case ISL_FORMAT_B5G6R5_UNORM:
583 case ISL_FORMAT_B5G6R5_UNORM_SRGB:
584 case ISL_FORMAT_A1B5G5R5_UNORM:
585 case ISL_FORMAT_A8_UNORM:
586 return false;
587 default:
588 break;
589 }
590
591 return format_gen(devinfo) >= format_info[format].ccs_e;
592 }
593
594 bool
595 isl_format_supports_multisampling(const struct gen_device_info *devinfo,
596 enum isl_format format)
597 {
598 /* From the Sandybridge PRM, Volume 4 Part 1 p72, SURFACE_STATE, Surface
599 * Format:
600 *
601 * If Number of Multisamples is set to a value other than
602 * MULTISAMPLECOUNT_1, this field cannot be set to the following
603 * formats:
604 *
605 * - any format with greater than 64 bits per element
606 * - any compressed texture format (BC*)
607 * - any YCRCB* format
608 *
609 * The restriction on the format's size is removed on Broadwell. Moreover,
610 * empirically it looks that even IvyBridge can handle multisampled surfaces
611 * with format sizes all the way to 128-bits (RGBA32F, RGBA32I, RGBA32UI).
612 *
613 * Also, there is an exception for HiZ which we treat as a compressed
614 * format and is allowed to be multisampled on Broadwell and earlier.
615 */
616 if (format == ISL_FORMAT_HIZ) {
617 /* On SKL+, HiZ is always single-sampled even when the primary surface
618 * is multisampled. See also isl_surf_get_hiz_surf().
619 */
620 return devinfo->gen <= 8;
621 } else if (devinfo->gen < 7 && isl_format_get_layout(format)->bpb > 64) {
622 return false;
623 } else if (isl_format_is_compressed(format)) {
624 return false;
625 } else if (isl_format_is_yuv(format)) {
626 return false;
627 } else {
628 return true;
629 }
630 }
631
632 /**
633 * Returns true if the two formats are "CCS_E compatible" meaning that you can
634 * render in one format with CCS_E enabled and then texture using the other
635 * format without needing a resolve.
636 *
637 * Note: Even if the formats are compatible, special care must be taken if a
638 * clear color is involved because the encoding of the clear color is heavily
639 * format-dependent.
640 */
641 bool
642 isl_formats_are_ccs_e_compatible(const struct gen_device_info *devinfo,
643 enum isl_format format1,
644 enum isl_format format2)
645 {
646 /* They must support CCS_E */
647 if (!isl_format_supports_ccs_e(devinfo, format1) ||
648 !isl_format_supports_ccs_e(devinfo, format2))
649 return false;
650
651 const struct isl_format_layout *fmtl1 = isl_format_get_layout(format1);
652 const struct isl_format_layout *fmtl2 = isl_format_get_layout(format2);
653
654 /* The compression used by CCS is not dependent on the actual data encoding
655 * of the format but only depends on the bit-layout of the channels.
656 */
657 return fmtl1->channels.r.bits == fmtl2->channels.r.bits &&
658 fmtl1->channels.g.bits == fmtl2->channels.g.bits &&
659 fmtl1->channels.b.bits == fmtl2->channels.b.bits &&
660 fmtl1->channels.a.bits == fmtl2->channels.a.bits;
661 }
662
663 static bool
664 isl_format_has_channel_type(enum isl_format fmt, enum isl_base_type type)
665 {
666 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
667
668 return fmtl->channels.r.type == type ||
669 fmtl->channels.g.type == type ||
670 fmtl->channels.b.type == type ||
671 fmtl->channels.a.type == type ||
672 fmtl->channels.l.type == type ||
673 fmtl->channels.i.type == type ||
674 fmtl->channels.p.type == type;
675 }
676
677 bool
678 isl_format_has_unorm_channel(enum isl_format fmt)
679 {
680 return isl_format_has_channel_type(fmt, ISL_UNORM);
681 }
682
683 bool
684 isl_format_has_snorm_channel(enum isl_format fmt)
685 {
686 return isl_format_has_channel_type(fmt, ISL_SNORM);
687 }
688
689 bool
690 isl_format_has_ufloat_channel(enum isl_format fmt)
691 {
692 return isl_format_has_channel_type(fmt, ISL_UFLOAT);
693 }
694
695 bool
696 isl_format_has_sfloat_channel(enum isl_format fmt)
697 {
698 return isl_format_has_channel_type(fmt, ISL_SFLOAT);
699 }
700
701 bool
702 isl_format_has_uint_channel(enum isl_format fmt)
703 {
704 return isl_format_has_channel_type(fmt, ISL_UINT);
705 }
706
707 bool
708 isl_format_has_sint_channel(enum isl_format fmt)
709 {
710 return isl_format_has_channel_type(fmt, ISL_SINT);
711 }
712
713 bool
714 isl_format_has_color_component(enum isl_format fmt, int component)
715 {
716 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
717 const uint8_t intensity = fmtl->channels.i.bits;
718 const uint8_t luminance = fmtl->channels.l.bits;
719
720 switch (component) {
721 case 0:
722 return (fmtl->channels.r.bits + intensity + luminance) > 0;
723 case 1:
724 return (fmtl->channels.g.bits + intensity + luminance) > 0;
725 case 2:
726 return (fmtl->channels.b.bits + intensity + luminance) > 0;
727 case 3:
728 return (fmtl->channels.a.bits + intensity) > 0;
729 default:
730 assert(!"Invalid color component: must be 0..3");
731 return false;
732 }
733 }
734
735 unsigned
736 isl_format_get_num_channels(enum isl_format fmt)
737 {
738 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
739
740 assert(fmtl->channels.p.bits == 0);
741
742 return (fmtl->channels.r.bits > 0) +
743 (fmtl->channels.g.bits > 0) +
744 (fmtl->channels.b.bits > 0) +
745 (fmtl->channels.a.bits > 0) +
746 (fmtl->channels.l.bits > 0) +
747 (fmtl->channels.i.bits > 0);
748 }
749
750 uint32_t
751 isl_format_get_depth_format(enum isl_format fmt, bool has_stencil)
752 {
753 switch (fmt) {
754 default:
755 unreachable("bad isl depth format");
756 case ISL_FORMAT_R32_FLOAT_X8X24_TYPELESS:
757 assert(has_stencil);
758 return 0; /* D32_FLOAT_S8X24_UINT */
759 case ISL_FORMAT_R32_FLOAT:
760 assert(!has_stencil);
761 return 1; /* D32_FLOAT */
762 case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
763 if (has_stencil) {
764 return 2; /* D24_UNORM_S8_UINT */
765 } else {
766 return 3; /* D24_UNORM_X8_UINT */
767 }
768 case ISL_FORMAT_R16_UNORM:
769 assert(!has_stencil);
770 return 5; /* D16_UNORM */
771 }
772 }
773
774 enum isl_format
775 isl_format_rgb_to_rgba(enum isl_format rgb)
776 {
777 assert(isl_format_is_rgb(rgb));
778
779 switch (rgb) {
780 case ISL_FORMAT_R32G32B32_FLOAT: return ISL_FORMAT_R32G32B32A32_FLOAT;
781 case ISL_FORMAT_R32G32B32_SINT: return ISL_FORMAT_R32G32B32A32_SINT;
782 case ISL_FORMAT_R32G32B32_UINT: return ISL_FORMAT_R32G32B32A32_UINT;
783 case ISL_FORMAT_R32G32B32_UNORM: return ISL_FORMAT_R32G32B32A32_UNORM;
784 case ISL_FORMAT_R32G32B32_SNORM: return ISL_FORMAT_R32G32B32A32_SNORM;
785 case ISL_FORMAT_R32G32B32_SSCALED: return ISL_FORMAT_R32G32B32A32_SSCALED;
786 case ISL_FORMAT_R32G32B32_USCALED: return ISL_FORMAT_R32G32B32A32_USCALED;
787 case ISL_FORMAT_R32G32B32_SFIXED: return ISL_FORMAT_R32G32B32A32_SFIXED;
788 case ISL_FORMAT_R8G8B8_UNORM: return ISL_FORMAT_R8G8B8A8_UNORM;
789 case ISL_FORMAT_R8G8B8_SNORM: return ISL_FORMAT_R8G8B8A8_SNORM;
790 case ISL_FORMAT_R8G8B8_SSCALED: return ISL_FORMAT_R8G8B8A8_SSCALED;
791 case ISL_FORMAT_R8G8B8_USCALED: return ISL_FORMAT_R8G8B8A8_USCALED;
792 case ISL_FORMAT_R16G16B16_FLOAT: return ISL_FORMAT_R16G16B16A16_FLOAT;
793 case ISL_FORMAT_R16G16B16_UNORM: return ISL_FORMAT_R16G16B16A16_UNORM;
794 case ISL_FORMAT_R16G16B16_SNORM: return ISL_FORMAT_R16G16B16A16_SNORM;
795 case ISL_FORMAT_R16G16B16_SSCALED: return ISL_FORMAT_R16G16B16A16_SSCALED;
796 case ISL_FORMAT_R16G16B16_USCALED: return ISL_FORMAT_R16G16B16A16_USCALED;
797 case ISL_FORMAT_R8G8B8_UNORM_SRGB: return ISL_FORMAT_R8G8B8A8_UNORM_SRGB;
798 case ISL_FORMAT_R16G16B16_UINT: return ISL_FORMAT_R16G16B16A16_UINT;
799 case ISL_FORMAT_R16G16B16_SINT: return ISL_FORMAT_R16G16B16A16_SINT;
800 case ISL_FORMAT_R8G8B8_UINT: return ISL_FORMAT_R8G8B8A8_UINT;
801 case ISL_FORMAT_R8G8B8_SINT: return ISL_FORMAT_R8G8B8A8_SINT;
802 default:
803 return ISL_FORMAT_UNSUPPORTED;
804 }
805 }
806
807 enum isl_format
808 isl_format_rgb_to_rgbx(enum isl_format rgb)
809 {
810 assert(isl_format_is_rgb(rgb));
811
812 switch (rgb) {
813 case ISL_FORMAT_R32G32B32_FLOAT:
814 return ISL_FORMAT_R32G32B32X32_FLOAT;
815 case ISL_FORMAT_R16G16B16_UNORM:
816 return ISL_FORMAT_R16G16B16X16_UNORM;
817 case ISL_FORMAT_R16G16B16_FLOAT:
818 return ISL_FORMAT_R16G16B16X16_FLOAT;
819 case ISL_FORMAT_R8G8B8_UNORM:
820 return ISL_FORMAT_R8G8B8X8_UNORM;
821 case ISL_FORMAT_R8G8B8_UNORM_SRGB:
822 return ISL_FORMAT_R8G8B8X8_UNORM_SRGB;
823 default:
824 return ISL_FORMAT_UNSUPPORTED;
825 }
826 }
827
828 enum isl_format
829 isl_format_rgbx_to_rgba(enum isl_format rgbx)
830 {
831 assert(isl_format_is_rgbx(rgbx));
832
833 switch (rgbx) {
834 case ISL_FORMAT_R32G32B32X32_FLOAT:
835 return ISL_FORMAT_R32G32B32A32_FLOAT;
836 case ISL_FORMAT_R16G16B16X16_UNORM:
837 return ISL_FORMAT_R16G16B16A16_UNORM;
838 case ISL_FORMAT_R16G16B16X16_FLOAT:
839 return ISL_FORMAT_R16G16B16A16_FLOAT;
840 case ISL_FORMAT_B8G8R8X8_UNORM:
841 return ISL_FORMAT_B8G8R8A8_UNORM;
842 case ISL_FORMAT_B8G8R8X8_UNORM_SRGB:
843 return ISL_FORMAT_B8G8R8A8_UNORM_SRGB;
844 case ISL_FORMAT_R8G8B8X8_UNORM:
845 return ISL_FORMAT_R8G8B8A8_UNORM;
846 case ISL_FORMAT_R8G8B8X8_UNORM_SRGB:
847 return ISL_FORMAT_R8G8B8A8_UNORM_SRGB;
848 case ISL_FORMAT_B10G10R10X2_UNORM:
849 return ISL_FORMAT_B10G10R10A2_UNORM;
850 case ISL_FORMAT_B5G5R5X1_UNORM:
851 return ISL_FORMAT_B5G5R5A1_UNORM;
852 case ISL_FORMAT_B5G5R5X1_UNORM_SRGB:
853 return ISL_FORMAT_B5G5R5A1_UNORM_SRGB;
854 default:
855 assert(!"Invalid RGBX format");
856 return rgbx;
857 }
858 }
859
860 static inline void
861 pack_channel(const union isl_color_value *value, unsigned i,
862 const struct isl_channel_layout *layout,
863 enum isl_colorspace colorspace,
864 uint32_t data_out[4])
865 {
866 if (layout->type == ISL_VOID)
867 return;
868
869 if (colorspace == ISL_COLORSPACE_SRGB)
870 assert(layout->type == ISL_UNORM);
871
872 uint32_t packed;
873 switch (layout->type) {
874 case ISL_UNORM:
875 if (colorspace == ISL_COLORSPACE_SRGB) {
876 if (layout->bits == 8) {
877 packed = util_format_linear_float_to_srgb_8unorm(value->f32[i]);
878 } else {
879 float srgb = util_format_linear_to_srgb_float(value->f32[i]);
880 packed = _mesa_float_to_unorm(srgb, layout->bits);
881 }
882 } else {
883 packed = _mesa_float_to_unorm(value->f32[i], layout->bits);
884 }
885 break;
886 case ISL_SNORM:
887 packed = _mesa_float_to_snorm(value->f32[i], layout->bits);
888 break;
889 case ISL_SFLOAT:
890 assert(layout->bits == 16 || layout->bits == 32);
891 if (layout->bits == 16) {
892 packed = _mesa_float_to_half(value->f32[i]);
893 } else {
894 packed = value->u32[i];
895 }
896 break;
897 case ISL_UINT:
898 packed = MIN(value->u32[i], MAX_UINT(layout->bits));
899 break;
900 case ISL_SINT:
901 packed = MIN(MAX(value->u32[i], MIN_INT(layout->bits)),
902 MAX_INT(layout->bits));
903 break;
904
905 default:
906 unreachable("Invalid channel type");
907 }
908
909 unsigned dword = layout->start_bit / 32;
910 unsigned bit = layout->start_bit % 32;
911 assert(bit + layout->bits <= 32);
912 data_out[dword] |= (packed & MAX_UINT(layout->bits)) << bit;
913 }
914
915 /**
916 * Take an isl_color_value and pack it into the actual bits as specified by
917 * the isl_format. This function is very slow for a format conversion
918 * function but should be fine for a single pixel worth of data.
919 */
920 void
921 isl_color_value_pack(const union isl_color_value *value,
922 enum isl_format format,
923 uint32_t *data_out)
924 {
925 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
926 assert(fmtl->colorspace == ISL_COLORSPACE_LINEAR ||
927 fmtl->colorspace == ISL_COLORSPACE_SRGB);
928 assert(!isl_format_is_compressed(format));
929
930 memset(data_out, 0, isl_align(fmtl->bpb, 32) / 8);
931
932 if (format == ISL_FORMAT_R9G9B9E5_SHAREDEXP) {
933 data_out[0] = float3_to_rgb9e5(value->f32);
934 return;
935 } else if (format == ISL_FORMAT_R11G11B10_FLOAT) {
936 data_out[0] = float3_to_r11g11b10f(value->f32);
937 return;
938 }
939
940 pack_channel(value, 0, &fmtl->channels.r, fmtl->colorspace, data_out);
941 pack_channel(value, 1, &fmtl->channels.g, fmtl->colorspace, data_out);
942 pack_channel(value, 2, &fmtl->channels.b, fmtl->colorspace, data_out);
943 pack_channel(value, 3, &fmtl->channels.a, ISL_COLORSPACE_LINEAR, data_out);
944 pack_channel(value, 0, &fmtl->channels.l, fmtl->colorspace, data_out);
945 pack_channel(value, 0, &fmtl->channels.i, ISL_COLORSPACE_LINEAR, data_out);
946 assert(fmtl->channels.p.bits == 0);
947 }
948
949 /** Extend an N-bit signed integer to 32 bits */
950 static inline int32_t
951 sign_extend(int32_t x, unsigned bits)
952 {
953 if (bits < 32) {
954 unsigned shift = 32 - bits;
955 return (x << shift) >> shift;
956 } else {
957 return x;
958 }
959 }
960
961 static inline void
962 unpack_channel(union isl_color_value *value,
963 unsigned start, unsigned count,
964 const struct isl_channel_layout *layout,
965 enum isl_colorspace colorspace,
966 const uint32_t *data_in)
967 {
968 if (layout->type == ISL_VOID)
969 return;
970
971 unsigned dword = layout->start_bit / 32;
972 unsigned bit = layout->start_bit % 32;
973 assert(bit + layout->bits <= 32);
974 uint32_t packed = (data_in[dword] >> bit) & MAX_UINT(layout->bits);
975
976 union {
977 uint32_t u32;
978 float f32;
979 } unpacked;
980
981 if (colorspace == ISL_COLORSPACE_SRGB)
982 assert(layout->type == ISL_UNORM);
983
984 switch (layout->type) {
985 case ISL_UNORM:
986 unpacked.f32 = _mesa_unorm_to_float(packed, layout->bits);
987 if (colorspace == ISL_COLORSPACE_SRGB) {
988 if (layout->bits == 8) {
989 unpacked.f32 = util_format_srgb_8unorm_to_linear_float(packed);
990 } else {
991 float srgb = _mesa_unorm_to_float(packed, layout->bits);
992 unpacked.f32 = util_format_srgb_to_linear_float(srgb);
993 }
994 } else {
995 unpacked.f32 = _mesa_unorm_to_float(packed, layout->bits);
996 }
997 break;
998 case ISL_SNORM:
999 unpacked.f32 = _mesa_snorm_to_float(sign_extend(packed, layout->bits),
1000 layout->bits);
1001 break;
1002 case ISL_SFLOAT:
1003 assert(layout->bits == 16 || layout->bits == 32);
1004 if (layout->bits == 16) {
1005 unpacked.f32 = _mesa_half_to_float(packed);
1006 } else {
1007 unpacked.u32 = packed;
1008 }
1009 break;
1010 case ISL_UINT:
1011 unpacked.u32 = packed;
1012 break;
1013 case ISL_SINT:
1014 unpacked.u32 = sign_extend(packed, layout->bits);
1015 break;
1016
1017 default:
1018 unreachable("Invalid channel type");
1019 }
1020
1021 for (unsigned i = 0; i < count; i++)
1022 value->u32[start + i] = unpacked.u32;
1023 }
1024
1025 /**
1026 * Take unpack an isl_color_value from the actual bits as specified by
1027 * the isl_format. This function is very slow for a format conversion
1028 * function but should be fine for a single pixel worth of data.
1029 */
1030 void
1031 isl_color_value_unpack(union isl_color_value *value,
1032 enum isl_format format,
1033 const uint32_t data_in[4])
1034 {
1035 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
1036 assert(fmtl->colorspace == ISL_COLORSPACE_LINEAR ||
1037 fmtl->colorspace == ISL_COLORSPACE_SRGB);
1038 assert(!isl_format_is_compressed(format));
1039
1040 /* Default to opaque black. */
1041 memset(value, 0, sizeof(*value));
1042 if (isl_format_has_int_channel(format)) {
1043 value->u32[3] = 1u;
1044 } else {
1045 value->f32[3] = 1.0f;
1046 }
1047
1048 if (format == ISL_FORMAT_R9G9B9E5_SHAREDEXP) {
1049 rgb9e5_to_float3(data_in[0], value->f32);
1050 return;
1051 } else if (format == ISL_FORMAT_R11G11B10_FLOAT) {
1052 r11g11b10f_to_float3(data_in[0], value->f32);
1053 return;
1054 }
1055
1056 unpack_channel(value, 0, 1, &fmtl->channels.r, fmtl->colorspace, data_in);
1057 unpack_channel(value, 1, 1, &fmtl->channels.g, fmtl->colorspace, data_in);
1058 unpack_channel(value, 2, 1, &fmtl->channels.b, fmtl->colorspace, data_in);
1059 unpack_channel(value, 3, 1, &fmtl->channels.a, ISL_COLORSPACE_LINEAR, data_in);
1060 unpack_channel(value, 0, 3, &fmtl->channels.l, fmtl->colorspace, data_in);
1061 unpack_channel(value, 0, 4, &fmtl->channels.i, ISL_COLORSPACE_LINEAR, data_in);
1062 assert(fmtl->channels.p.bits == 0);
1063 }