freedreno/layout: add explicit offset/pitch argument to fdl6_layout
[mesa.git] / src / gallium / drivers / freedreno / a6xx / fd6_resource.c
1 /*
2 * Copyright (C) 2018 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors:
25 * Rob Clark <robclark@freedesktop.org>
26 */
27
28 #include "drm-uapi/drm_fourcc.h"
29
30 #include "fd6_resource.h"
31 #include "fd6_format.h"
32
33 #include "a6xx.xml.h"
34
35 /* A subset of the valid tiled formats can be compressed. We do
36 * already require tiled in order to be compressed, but just because
37 * it can be tiled doesn't mean it can be compressed.
38 */
39 static bool
40 ok_ubwc_format(struct fd_resource *rsc, enum pipe_format pfmt)
41 {
42 /* NOTE: both x24s8 and z24s8 map to RB6_X8Z24_UNORM, but UBWC
43 * does not seem to work properly when sampling x24s8.. possibly
44 * because we sample it as TFMT6_8_8_8_8_UINT.
45 *
46 * This could possibly be a hw limitation, or maybe something
47 * else wrong somewhere (although z24s8 blits and sampling with
48 * UBWC seem fine). Recheck on a later revision of a6xx
49 */
50 if (pfmt == PIPE_FORMAT_X24S8_UINT)
51 return false;
52
53 /* We don't fully understand what's going wrong with this combination, but
54 * we haven't been able to make it work. It's enough of a corner-case
55 * that we can just disable UBWC for these resources.
56 */
57 if (rsc->base.target != PIPE_TEXTURE_2D &&
58 pfmt == PIPE_FORMAT_Z24_UNORM_S8_UINT)
59 return false;
60
61 switch (fd6_pipe2color(pfmt)) {
62 case FMT6_10_10_10_2_UINT:
63 case FMT6_10_10_10_2_UNORM_DEST:
64 case FMT6_11_11_10_FLOAT:
65 case FMT6_16_FLOAT:
66 case FMT6_16_16_16_16_FLOAT:
67 case FMT6_16_16_16_16_SINT:
68 case FMT6_16_16_16_16_UINT:
69 case FMT6_16_16_FLOAT:
70 case FMT6_16_16_SINT:
71 case FMT6_16_16_UINT:
72 case FMT6_16_SINT:
73 case FMT6_16_UINT:
74 case FMT6_32_32_32_32_SINT:
75 case FMT6_32_32_32_32_UINT:
76 case FMT6_32_32_SINT:
77 case FMT6_32_32_UINT:
78 case FMT6_5_6_5_UNORM:
79 case FMT6_8_8_8_8_SINT:
80 case FMT6_8_8_8_8_UINT:
81 case FMT6_8_8_8_8_UNORM:
82 case FMT6_8_8_8_X8_UNORM:
83 case FMT6_8_8_SINT:
84 case FMT6_8_8_UINT:
85 case FMT6_8_8_UNORM:
86 case FMT6_8_UNORM:
87 case FMT6_Z24_UNORM_S8_UINT:
88 case FMT6_Z24_UNORM_S8_UINT_AS_R8G8B8A8:
89 return true;
90 default:
91 return false;
92 }
93 }
94
95 /**
96 * Ensure the rsc is in an ok state to be used with the specified format.
97 * This handles the case of UBWC buffers used with non-UBWC compatible
98 * formats, by triggering an uncompress.
99 */
100 void
101 fd6_validate_format(struct fd_context *ctx, struct fd_resource *rsc,
102 enum pipe_format format)
103 {
104 if (!rsc->layout.ubwc)
105 return;
106
107 if (ok_ubwc_format(rsc, format))
108 return;
109
110 fd_resource_uncompress(ctx, rsc);
111 }
112
113 static void
114 setup_lrz(struct fd_resource *rsc)
115 {
116 struct fd_screen *screen = fd_screen(rsc->base.screen);
117 const uint32_t flags = DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
118 DRM_FREEDRENO_GEM_TYPE_KMEM; /* TODO */
119 unsigned width0 = rsc->base.width0;
120 unsigned height0 = rsc->base.height0;
121
122 /* LRZ buffer is super-sampled: */
123 switch (rsc->base.nr_samples) {
124 case 4:
125 width0 *= 2;
126 /* fallthru */
127 case 2:
128 height0 *= 2;
129 }
130
131 unsigned lrz_pitch = align(DIV_ROUND_UP(width0, 8), 32);
132 unsigned lrz_height = align(DIV_ROUND_UP(height0, 8), 16);
133
134 unsigned size = lrz_pitch * lrz_height * 2;
135
136 rsc->lrz_height = lrz_height;
137 rsc->lrz_width = lrz_pitch;
138 rsc->lrz_pitch = lrz_pitch;
139 rsc->lrz = fd_bo_new(screen->dev, size, flags, "lrz");
140 }
141
142 static uint32_t
143 fd6_setup_slices(struct fd_resource *rsc)
144 {
145 struct pipe_resource *prsc = &rsc->base;
146
147 if (!(fd_mesa_debug & FD_DBG_NOLRZ) && has_depth(rsc->base.format))
148 setup_lrz(rsc);
149
150 if (rsc->layout.ubwc && !ok_ubwc_format(rsc, rsc->base.format))
151 rsc->layout.ubwc = false;
152
153 fdl6_layout(&rsc->layout, prsc->format, fd_resource_nr_samples(prsc),
154 prsc->width0, prsc->height0, prsc->depth0,
155 prsc->last_level + 1, prsc->array_size,
156 prsc->target == PIPE_TEXTURE_3D,
157 NULL);
158
159 return rsc->layout.size;
160 }
161
162 static int
163 fill_ubwc_buffer_sizes(struct fd_resource *rsc)
164 {
165 struct pipe_resource *prsc = &rsc->base;
166 struct fdl_slice slice = *fd_resource_slice(rsc, 0);
167
168 /* limit things to simple single level 2d for now: */
169 if ((prsc->depth0 != 1) || (prsc->array_size != 1) || (prsc->last_level != 0))
170 return -1;
171 if (prsc->target != PIPE_TEXTURE_2D)
172 return -1;
173 if (!ok_ubwc_format(rsc, prsc->format))
174 return -1;
175
176 rsc->layout.ubwc = true;
177 rsc->layout.tile_mode = TILE6_3;
178
179 if (!fdl6_layout(&rsc->layout, prsc->format, fd_resource_nr_samples(prsc),
180 prsc->width0, prsc->height0, prsc->depth0,
181 prsc->last_level + 1, prsc->array_size, false, &slice))
182 return -1;
183
184 if (rsc->layout.size > fd_bo_size(rsc->bo))
185 return -1;
186
187 return 0;
188 }
189
190 static int
191 fd6_layout_resource_for_modifier(struct fd_resource *rsc, uint64_t modifier)
192 {
193 switch (modifier) {
194 case DRM_FORMAT_MOD_QCOM_COMPRESSED:
195 return fill_ubwc_buffer_sizes(rsc);
196 case DRM_FORMAT_MOD_LINEAR:
197 case DRM_FORMAT_MOD_INVALID:
198 return 0;
199 default:
200 return -1;
201 }
202 }
203
204 static const uint64_t supported_modifiers[] = {
205 DRM_FORMAT_MOD_LINEAR,
206 DRM_FORMAT_MOD_QCOM_COMPRESSED,
207 };
208
209 void
210 fd6_resource_screen_init(struct pipe_screen *pscreen)
211 {
212 struct fd_screen *screen = fd_screen(pscreen);
213
214 screen->setup_slices = fd6_setup_slices;
215 screen->layout_resource_for_modifier = fd6_layout_resource_for_modifier;
216 screen->supported_modifiers = supported_modifiers;
217 screen->num_supported_modifiers = ARRAY_SIZE(supported_modifiers);
218 }