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
ok_ubwc_format(struct fd_resource * rsc,enum pipe_format pfmt)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 if (pfmt == PIPE_FORMAT_R8_G8B8_420_UNORM)
62 return true;
63
64 switch (fd6_pipe2color(pfmt)) {
65 case FMT6_10_10_10_2_UINT:
66 case FMT6_10_10_10_2_UNORM_DEST:
67 case FMT6_11_11_10_FLOAT:
68 case FMT6_16_FLOAT:
69 case FMT6_16_16_16_16_FLOAT:
70 case FMT6_16_16_16_16_SINT:
71 case FMT6_16_16_16_16_UINT:
72 case FMT6_16_16_FLOAT:
73 case FMT6_16_16_SINT:
74 case FMT6_16_16_UINT:
75 case FMT6_16_SINT:
76 case FMT6_16_UINT:
77 case FMT6_32_32_32_32_SINT:
78 case FMT6_32_32_32_32_UINT:
79 case FMT6_32_32_SINT:
80 case FMT6_32_32_UINT:
81 case FMT6_5_6_5_UNORM:
82 case FMT6_8_8_8_8_SINT:
83 case FMT6_8_8_8_8_UINT:
84 case FMT6_8_8_8_8_UNORM:
85 case FMT6_8_8_8_X8_UNORM:
86 case FMT6_8_8_SINT:
87 case FMT6_8_8_UINT:
88 case FMT6_8_8_UNORM:
89 case FMT6_8_UNORM:
90 case FMT6_Z24_UNORM_S8_UINT:
91 case FMT6_Z24_UNORM_S8_UINT_AS_R8G8B8A8:
92 return true;
93 default:
94 return false;
95 }
96 }
97
98 /**
99 * Ensure the rsc is in an ok state to be used with the specified format.
100 * This handles the case of UBWC buffers used with non-UBWC compatible
101 * formats, by triggering an uncompress.
102 */
103 void
fd6_validate_format(struct fd_context * ctx,struct fd_resource * rsc,enum pipe_format format)104 fd6_validate_format(struct fd_context *ctx, struct fd_resource *rsc,
105 enum pipe_format format)
106 {
107 if (!rsc->layout.ubwc)
108 return;
109
110 if (ok_ubwc_format(rsc, format))
111 return;
112
113 fd_resource_uncompress(ctx, rsc);
114 }
115
116 static void
setup_lrz(struct fd_resource * rsc)117 setup_lrz(struct fd_resource *rsc)
118 {
119 struct fd_screen *screen = fd_screen(rsc->base.screen);
120 const uint32_t flags = DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
121 DRM_FREEDRENO_GEM_TYPE_KMEM; /* TODO */
122 unsigned width0 = rsc->base.width0;
123 unsigned height0 = rsc->base.height0;
124
125 /* LRZ buffer is super-sampled: */
126 switch (rsc->base.nr_samples) {
127 case 4:
128 width0 *= 2;
129 /* fallthru */
130 case 2:
131 height0 *= 2;
132 }
133
134 unsigned lrz_pitch = align(DIV_ROUND_UP(width0, 8), 32);
135 unsigned lrz_height = align(DIV_ROUND_UP(height0, 8), 16);
136
137 unsigned size = lrz_pitch * lrz_height * 2;
138
139 rsc->lrz_height = lrz_height;
140 rsc->lrz_width = lrz_pitch;
141 rsc->lrz_pitch = lrz_pitch;
142 rsc->lrz = fd_bo_new(screen->dev, size, flags, "lrz");
143 }
144
145 static uint32_t
fd6_setup_slices(struct fd_resource * rsc)146 fd6_setup_slices(struct fd_resource *rsc)
147 {
148 struct pipe_resource *prsc = &rsc->base;
149
150 if (!(fd_mesa_debug & FD_DBG_NOLRZ) && has_depth(rsc->base.format))
151 setup_lrz(rsc);
152
153 if (rsc->layout.ubwc && !ok_ubwc_format(rsc, rsc->base.format))
154 rsc->layout.ubwc = false;
155
156 fdl6_layout(&rsc->layout, prsc->format, fd_resource_nr_samples(prsc),
157 prsc->width0, prsc->height0, prsc->depth0,
158 prsc->last_level + 1, prsc->array_size,
159 prsc->target == PIPE_TEXTURE_3D,
160 NULL);
161
162 return rsc->layout.size;
163 }
164
165 static int
fill_ubwc_buffer_sizes(struct fd_resource * rsc)166 fill_ubwc_buffer_sizes(struct fd_resource *rsc)
167 {
168 struct pipe_resource *prsc = &rsc->base;
169 struct fdl_explicit_layout explicit = {
170 .offset = rsc->layout.slices[0].offset,
171 .pitch = rsc->layout.pitch0,
172 };
173
174 /* limit things to simple single level 2d for now: */
175 if ((prsc->depth0 != 1) || (prsc->array_size != 1) || (prsc->last_level != 0))
176 return -1;
177 if (prsc->target != PIPE_TEXTURE_2D)
178 return -1;
179 if (!ok_ubwc_format(rsc, prsc->format))
180 return -1;
181
182 rsc->layout.ubwc = true;
183 rsc->layout.tile_mode = TILE6_3;
184
185 if (!fdl6_layout(&rsc->layout, prsc->format, fd_resource_nr_samples(prsc),
186 prsc->width0, prsc->height0, prsc->depth0,
187 prsc->last_level + 1, prsc->array_size, false, &explicit))
188 return -1;
189
190 if (rsc->layout.size > fd_bo_size(rsc->bo))
191 return -1;
192
193 return 0;
194 }
195
196 static int
fd6_layout_resource_for_modifier(struct fd_resource * rsc,uint64_t modifier)197 fd6_layout_resource_for_modifier(struct fd_resource *rsc, uint64_t modifier)
198 {
199 switch (modifier) {
200 case DRM_FORMAT_MOD_QCOM_COMPRESSED:
201 return fill_ubwc_buffer_sizes(rsc);
202 case DRM_FORMAT_MOD_LINEAR:
203 case DRM_FORMAT_MOD_INVALID:
204 return 0;
205 default:
206 return -1;
207 }
208 }
209
210 static const uint64_t supported_modifiers[] = {
211 DRM_FORMAT_MOD_LINEAR,
212 DRM_FORMAT_MOD_QCOM_COMPRESSED,
213 };
214
215 void
fd6_resource_screen_init(struct pipe_screen * pscreen)216 fd6_resource_screen_init(struct pipe_screen *pscreen)
217 {
218 struct fd_screen *screen = fd_screen(pscreen);
219
220 screen->setup_slices = fd6_setup_slices;
221 screen->layout_resource_for_modifier = fd6_layout_resource_for_modifier;
222 screen->supported_modifiers = supported_modifiers;
223 screen->num_supported_modifiers = ARRAY_SIZE(supported_modifiers);
224 }
225