intel/isl: Move iris's pipe-to-isl format function to isl.
[mesa.git] / src / gallium / drivers / iris / iris_formats.c
1 /*
2 * Copyright © 2017 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 shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /**
24 * @file iris_formats.c
25 *
26 * Converts Gallium formats (PIPE_FORMAT_*) to hardware ones (ISL_FORMAT_*).
27 * Provides information about which formats support what features.
28 */
29
30 #include "util/bitscan.h"
31 #include "util/macros.h"
32 #include "util/format/u_format.h"
33
34 #include "iris_resource.h"
35 #include "iris_screen.h"
36
37 struct iris_format_info
38 iris_format_for_usage(const struct gen_device_info *devinfo,
39 enum pipe_format pformat,
40 isl_surf_usage_flags_t usage)
41 {
42 enum isl_format format = isl_format_for_pipe_format(pformat);
43 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
44 struct isl_swizzle swizzle = ISL_SWIZZLE_IDENTITY;
45
46 if (!util_format_is_srgb(pformat)) {
47 if (util_format_is_intensity(pformat)) {
48 swizzle = ISL_SWIZZLE(RED, RED, RED, RED);
49 } else if (util_format_is_luminance(pformat)) {
50 swizzle = ISL_SWIZZLE(RED, RED, RED, ONE);
51 } else if (util_format_is_luminance_alpha(pformat)) {
52 swizzle = ISL_SWIZZLE(RED, RED, RED, GREEN);
53 } else if (util_format_is_alpha(pformat)) {
54 swizzle = ISL_SWIZZLE(ZERO, ZERO, ZERO, RED);
55 }
56 }
57
58 /* When faking RGBX pipe formats with RGBA ISL formats, override alpha. */
59 if (!util_format_has_alpha(pformat) && fmtl->channels.a.type != ISL_VOID) {
60 swizzle = ISL_SWIZZLE(RED, GREEN, BLUE, ONE);
61 }
62
63 if ((usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) &&
64 pformat == PIPE_FORMAT_A8_UNORM) {
65 /* Most of the hardware A/LA formats are not renderable, except
66 * for A8_UNORM. SURFACE_STATE's shader channel select fields
67 * cannot be used to swap RGB and A channels when rendering (as
68 * it could impact alpha blending), so we have to use the actual
69 * A8_UNORM format when rendering.
70 */
71 format = ISL_FORMAT_A8_UNORM;
72 swizzle = ISL_SWIZZLE_IDENTITY;
73 }
74
75 /* We choose RGBA over RGBX for rendering the hardware doesn't support
76 * rendering to RGBX. However, when this internal override is used on Gen9+,
77 * fast clears don't work correctly.
78 *
79 * i965 fixes this by pretending to not support RGBX formats, and the higher
80 * layers of Mesa pick the RGBA format instead. Gallium doesn't work that
81 * way, and might choose a different format, like BGRX instead of RGBX,
82 * which will also cause problems when sampling from a surface fast cleared
83 * as RGBX. So we always choose RGBA instead of RGBX explicitly
84 * here.
85 */
86 if (isl_format_is_rgbx(format) &&
87 !isl_format_supports_rendering(devinfo, format)) {
88 format = isl_format_rgbx_to_rgba(format);
89 swizzle = ISL_SWIZZLE(RED, GREEN, BLUE, ONE);
90 }
91
92 return (struct iris_format_info) { .fmt = format, .swizzle = swizzle };
93 }
94
95 /**
96 * The pscreen->is_format_supported() driver hook.
97 *
98 * Returns true if the given format is supported for the given usage
99 * (PIPE_BIND_*) and sample count.
100 */
101 bool
102 iris_is_format_supported(struct pipe_screen *pscreen,
103 enum pipe_format pformat,
104 enum pipe_texture_target target,
105 unsigned sample_count,
106 unsigned storage_sample_count,
107 unsigned usage)
108 {
109 struct iris_screen *screen = (struct iris_screen *) pscreen;
110 const struct gen_device_info *devinfo = &screen->devinfo;
111 uint32_t max_samples = devinfo->gen == 8 ? 8 : 16;
112
113 if (sample_count > max_samples ||
114 !util_is_power_of_two_or_zero(sample_count))
115 return false;
116
117 if (pformat == PIPE_FORMAT_NONE)
118 return true;
119
120 enum isl_format format = isl_format_for_pipe_format(pformat);
121
122 if (format == ISL_FORMAT_UNSUPPORTED)
123 return false;
124
125 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
126 const bool is_integer = isl_format_has_int_channel(format);
127 bool supported = true;
128
129 if (sample_count > 1)
130 supported &= isl_format_supports_multisampling(devinfo, format);
131
132 if (usage & PIPE_BIND_DEPTH_STENCIL) {
133 supported &= format == ISL_FORMAT_R32_FLOAT_X8X24_TYPELESS ||
134 format == ISL_FORMAT_R32_FLOAT ||
135 format == ISL_FORMAT_R24_UNORM_X8_TYPELESS ||
136 format == ISL_FORMAT_R16_UNORM ||
137 format == ISL_FORMAT_R8_UINT;
138 }
139
140 if (usage & PIPE_BIND_RENDER_TARGET) {
141 /* Alpha and luminance-alpha formats other than A8_UNORM are not
142 * renderable. For texturing, we can use R or RG formats with
143 * shader channel selects (SCS) to swizzle the data into the correct
144 * channels. But for render targets, the hardware prohibits using
145 * SCS to move shader outputs between the RGB and A channels, as it
146 * would alter what data is used for alpha blending.
147 *
148 * For BLORP, we can apply the swizzle in the shader. But for
149 * general rendering, this would mean recompiling the shader, which
150 * we'd like to avoid doing. So we mark these formats non-renderable.
151 *
152 * We do support A8_UNORM as it's required and is renderable.
153 */
154 if (pformat != PIPE_FORMAT_A8_UNORM &&
155 (util_format_is_alpha(pformat) ||
156 util_format_is_luminance_alpha(pformat)))
157 supported = false;
158
159 enum isl_format rt_format = format;
160
161 if (isl_format_is_rgbx(format) &&
162 !isl_format_supports_rendering(devinfo, format))
163 rt_format = isl_format_rgbx_to_rgba(format);
164
165 supported &= isl_format_supports_rendering(devinfo, rt_format);
166
167 if (!is_integer)
168 supported &= isl_format_supports_alpha_blending(devinfo, rt_format);
169 }
170
171 if (usage & PIPE_BIND_SHADER_IMAGE) {
172 /* Dataport doesn't support compression, and we can't resolve an MCS
173 * compressed surface. (Buffer images may have sample count of 0.)
174 */
175 supported &= sample_count == 0;
176
177 supported &= isl_format_supports_typed_writes(devinfo, format);
178 supported &= isl_has_matching_typed_storage_image_format(devinfo, format);
179 }
180
181 if (usage & PIPE_BIND_SAMPLER_VIEW) {
182 supported &= isl_format_supports_sampling(devinfo, format);
183 if (!is_integer)
184 supported &= isl_format_supports_filtering(devinfo, format);
185
186 /* Don't advertise 3-component RGB formats for non-buffer textures.
187 * This ensures that they are renderable from an API perspective since
188 * the state tracker will fall back to RGBA or RGBX, which are
189 * renderable. We want to render internally for copies and blits,
190 * even if the application doesn't.
191 *
192 * Buffer textures don't need to be renderable, so we support real RGB.
193 * This is useful for PBO upload, and 32-bit RGB support is mandatory.
194 */
195 if (target != PIPE_BUFFER)
196 supported &= fmtl->bpb != 24 && fmtl->bpb != 48 && fmtl->bpb != 96;
197 }
198
199 if (usage & PIPE_BIND_VERTEX_BUFFER)
200 supported &= isl_format_supports_vertex_fetch(devinfo, format);
201
202 if (usage & PIPE_BIND_INDEX_BUFFER) {
203 supported &= format == ISL_FORMAT_R8_UINT ||
204 format == ISL_FORMAT_R16_UINT ||
205 format == ISL_FORMAT_R32_UINT;
206 }
207
208 /* TODO: Support ASTC 5x5 on Gen9 properly. This means implementing
209 * a complex sampler workaround (see i965's gen9_apply_astc5x5_wa_flush).
210 * Without it, st/mesa will emulate ASTC 5x5 via uncompressed textures.
211 */
212 if (devinfo->gen == 9 && (format == ISL_FORMAT_ASTC_LDR_2D_5X5_FLT16 ||
213 format == ISL_FORMAT_ASTC_LDR_2D_5X5_U8SRGB))
214 return false;
215
216 return supported;
217 }
218