panfrost: Merge AFBC slab with BO backing
[mesa.git] / src / gallium / drivers / panfrost / pan_mfbd.c
1 /*
2 * Copyright 2018-2019 Alyssa Rosenzweig
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 */
24
25 #include "pan_context.h"
26 #include "pan_util.h"
27 #include "pan_format.h"
28
29 #include "util/u_format.h"
30
31 static struct mali_rt_format
32 panfrost_mfbd_format(struct pipe_surface *surf)
33 {
34 /* Explode details on the format */
35
36 const struct util_format_description *desc =
37 util_format_description(surf->texture->format);
38
39 /* Fill in accordingly, defaulting to 8-bit UNORM */
40
41 struct mali_rt_format fmt = {
42 .unk1 = 0x4000000,
43 .unk2 = 0x1,
44 .nr_channels = MALI_POSITIVE(desc->nr_channels),
45 .unk3 = 0x4,
46 .flags = 0x8,
47 .swizzle = panfrost_translate_swizzle_4(desc->swizzle),
48 .unk4 = 0x8
49 };
50
51 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
52 fmt.flags |= MALI_MFBD_FORMAT_SRGB;
53
54 /* Set flags for alternative formats */
55
56 if (surf->texture->format == PIPE_FORMAT_B5G6R5_UNORM) {
57 fmt.unk1 = 0x14000000;
58 fmt.nr_channels = MALI_POSITIVE(2);
59 fmt.unk3 |= 0x1;
60 }
61
62 return fmt;
63 }
64
65
66 static void
67 panfrost_mfbd_clear(
68 struct panfrost_job *job,
69 struct bifrost_framebuffer *fb,
70 struct bifrost_fb_extra *fbx,
71 struct bifrost_render_target *rt)
72 {
73 if (job->clear & PIPE_CLEAR_COLOR) {
74 rt->clear_color_1 = job->clear_color;
75 rt->clear_color_2 = job->clear_color;
76 rt->clear_color_3 = job->clear_color;
77 rt->clear_color_4 = job->clear_color;
78 }
79
80 if (job->clear & PIPE_CLEAR_DEPTH) {
81 fb->clear_depth = job->clear_depth;
82 }
83
84 if (job->clear & PIPE_CLEAR_STENCIL) {
85 fb->clear_stencil = job->clear_stencil;
86 }
87 }
88
89 static void
90 panfrost_mfbd_set_cbuf(
91 struct bifrost_render_target *rt,
92 struct pipe_surface *surf)
93 {
94 struct panfrost_resource *rsrc = pan_resource(surf->texture);
95
96 unsigned level = surf->u.tex.level;
97 assert(surf->u.tex.first_layer == 0);
98
99 int stride = rsrc->bo->slices[level].stride;
100 unsigned offset = rsrc->bo->slices[level].offset;
101
102 rt->format = panfrost_mfbd_format(surf);
103
104 /* Now, we set the layout specific pieces */
105
106 if (rsrc->bo->layout == PAN_LINEAR) {
107 rt->format.block = MALI_MFBD_BLOCK_LINEAR;
108 rt->framebuffer = rsrc->bo->gpu + offset;
109 rt->framebuffer_stride = stride / 16;
110 } else if (rsrc->bo->layout == PAN_TILED) {
111 rt->format.block = MALI_MFBD_BLOCK_TILED;
112 rt->framebuffer = rsrc->bo->gpu + offset;
113 rt->framebuffer_stride = stride;
114 } else if (rsrc->bo->layout == PAN_AFBC) {
115 rt->format.block = MALI_MFBD_BLOCK_AFBC;
116
117 mali_ptr base = rsrc->bo->gpu + offset;
118 unsigned header_size = rsrc->bo->slices[level].header_size;
119
120 rt->framebuffer = base + header_size;
121 rt->afbc.metadata = base;
122 rt->afbc.stride = 0;
123 rt->afbc.unk = 0x30009;
124
125 /* TODO: Investigate shift */
126 rt->framebuffer_stride = stride << 1;
127 } else {
128 fprintf(stderr, "Invalid render layout (cbuf)");
129 assert(0);
130 }
131 }
132
133 static void
134 panfrost_mfbd_set_zsbuf(
135 struct bifrost_framebuffer *fb,
136 struct bifrost_fb_extra *fbx,
137 struct pipe_surface *surf)
138 {
139 struct panfrost_resource *rsrc = pan_resource(surf->texture);
140
141 unsigned level = surf->u.tex.level;
142 assert(surf->u.tex.first_layer == 0);
143
144 unsigned offset = rsrc->bo->slices[level].offset;
145
146 if (rsrc->bo->layout == PAN_AFBC) {
147 mali_ptr base = rsrc->bo->gpu + offset;
148 unsigned header_size = rsrc->bo->slices[level].header_size;
149
150 fb->mfbd_flags |= MALI_MFBD_EXTRA;
151
152 fbx->flags =
153 MALI_EXTRA_PRESENT |
154 MALI_EXTRA_AFBC |
155 MALI_EXTRA_AFBC_ZS |
156 MALI_EXTRA_ZS |
157 0x1; /* unknown */
158
159 fbx->ds_afbc.depth_stencil = base + header_size;
160 fbx->ds_afbc.depth_stencil_afbc_metadata = base;
161 fbx->ds_afbc.depth_stencil_afbc_stride = 0;
162
163 fbx->ds_afbc.zero1 = 0x10009;
164 fbx->ds_afbc.padding = 0x1000;
165 } else if (rsrc->bo->layout == PAN_LINEAR) {
166 int stride = rsrc->bo->slices[level].stride;
167 fb->mfbd_flags |= MALI_MFBD_EXTRA;
168
169 fbx->flags |= MALI_EXTRA_PRESENT | MALI_EXTRA_ZS | 0x1;
170
171 fbx->ds_linear.depth = rsrc->bo->gpu + offset;
172 fbx->ds_linear.depth_stride = stride;
173 } else {
174 assert(0);
175 }
176 }
177
178 /* Helper for sequential uploads used for MFBD */
179
180 #define UPLOAD(dest, offset, src, max) { \
181 size_t sz = sizeof(*src); \
182 memcpy(dest.cpu + offset, src, sz); \
183 assert((offset + sz) <= max); \
184 offset += sz; \
185 }
186
187 static mali_ptr
188 panfrost_mfbd_upload(
189 struct panfrost_context *ctx,
190 struct bifrost_framebuffer *fb,
191 struct bifrost_fb_extra *fbx,
192 struct bifrost_render_target *rts,
193 unsigned cbufs)
194 {
195 off_t offset = 0;
196
197 /* There may be extra data stuck in the middle */
198 bool has_extra = fb->mfbd_flags & MALI_MFBD_EXTRA;
199
200 /* Compute total size for transfer */
201
202 size_t total_sz =
203 sizeof(struct bifrost_framebuffer) +
204 (has_extra ? sizeof(struct bifrost_fb_extra) : 0) +
205 sizeof(struct bifrost_render_target) * cbufs;
206
207 struct panfrost_transfer m_f_trans =
208 panfrost_allocate_transient(ctx, total_sz);
209
210 /* Do the transfer */
211
212 UPLOAD(m_f_trans, offset, fb, total_sz);
213
214 if (has_extra)
215 UPLOAD(m_f_trans, offset, fbx, total_sz);
216
217 for (unsigned c = 0; c < cbufs; ++c) {
218 UPLOAD(m_f_trans, offset, &rts[c], total_sz);
219 }
220
221 /* Return pointer suitable for the fragment section */
222 return m_f_trans.gpu | MALI_MFBD | (has_extra ? 2 : 0);
223 }
224
225 #undef UPLOAD
226
227 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
228
229 mali_ptr
230 panfrost_mfbd_fragment(struct panfrost_context *ctx, bool has_draws)
231 {
232 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
233
234 struct bifrost_framebuffer fb = panfrost_emit_mfbd(ctx, has_draws);
235 struct bifrost_fb_extra fbx = {};
236 struct bifrost_render_target rts[4] = {};
237
238 /* XXX: MRT case */
239 fb.rt_count_2 = 1;
240 fb.mfbd_flags = 0x100;
241
242 /* TODO: MRT clear */
243 panfrost_mfbd_clear(job, &fb, &fbx, &rts[0]);
244
245 for (int cb = 0; cb < ctx->pipe_framebuffer.nr_cbufs; ++cb) {
246 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[cb];
247 panfrost_mfbd_set_cbuf(&rts[cb], surf);
248 }
249
250 if (ctx->pipe_framebuffer.zsbuf) {
251 panfrost_mfbd_set_zsbuf(&fb, &fbx, ctx->pipe_framebuffer.zsbuf);
252 }
253
254 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
255
256 if (ctx->pipe_framebuffer.nr_cbufs == 0) {
257 struct mali_rt_format null_rt = {
258 .unk1 = 0x4000000,
259 .unk4 = 0x8
260 };
261
262 rts[0].format = null_rt;
263 rts[0].framebuffer = 0;
264 rts[0].framebuffer_stride = 0;
265 }
266
267 /* When scanning out, the depth buffer is immediately invalidated, so
268 * we don't need to waste bandwidth writing it out. This can improve
269 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
270 * memory bandwidth!).
271 *
272 * The exception is ReadPixels, but this is not supported on GLES so we
273 * can safely ignore it. */
274
275 if (panfrost_is_scanout(ctx)) {
276 job->requirements &= ~PAN_REQ_DEPTH_WRITE;
277 }
278
279 /* Actualize the requirements */
280
281 if (job->requirements & PAN_REQ_MSAA) {
282 rts[0].format.flags |= MALI_MFBD_FORMAT_MSAA;
283
284 /* XXX */
285 fb.unk1 |= (1 << 4) | (1 << 1);
286 fb.rt_count_2 = 4;
287 }
288
289 if (job->requirements & PAN_REQ_DEPTH_WRITE)
290 fb.mfbd_flags |= MALI_MFBD_DEPTH_WRITE;
291
292 if (ctx->pipe_framebuffer.nr_cbufs == 1) {
293 struct panfrost_resource *rsrc = (struct panfrost_resource *) ctx->pipe_framebuffer.cbufs[0]->texture;
294
295 if (rsrc->bo->has_checksum) {
296 fb.mfbd_flags |= MALI_MFBD_EXTRA;
297 fbx.flags |= MALI_EXTRA_PRESENT;
298 fbx.checksum_stride = rsrc->bo->checksum_stride;
299 fbx.checksum = rsrc->bo->gpu + rsrc->bo->slices[0].stride * rsrc->base.height0;
300 }
301 }
302
303 /* We always upload at least one (dummy) cbuf */
304 unsigned cbufs = MAX2(ctx->pipe_framebuffer.nr_cbufs, 1);
305
306 return panfrost_mfbd_upload(ctx, &fb, &fbx, rts, cbufs);
307 }