c9f3dc315a0fb8f139f572554befa630f60870ca
[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 void
32 panfrost_invert_swizzle(const unsigned char *in, unsigned char *out)
33 {
34 /* First, default to all zeroes to prevent uninitialized junk */
35
36 for (unsigned c = 0; c < 4; ++c)
37 out[c] = PIPE_SWIZZLE_0;
38
39 /* Now "do" what the swizzle says */
40
41 for (unsigned c = 0; c < 4; ++c) {
42 unsigned char i = in[c];
43
44 /* Who cares? */
45 if (i < PIPE_SWIZZLE_X || i > PIPE_SWIZZLE_W)
46 continue;
47
48 /* Invert */
49 unsigned idx = i - PIPE_SWIZZLE_X;
50 out[idx] = PIPE_SWIZZLE_X + c;
51 }
52 }
53
54 static struct mali_rt_format
55 panfrost_mfbd_format(struct pipe_surface *surf)
56 {
57 /* Explode details on the format */
58
59 const struct util_format_description *desc =
60 util_format_description(surf->format);
61
62 /* The swizzle for rendering is inverted from texturing */
63
64 unsigned char swizzle[4];
65 panfrost_invert_swizzle(desc->swizzle, swizzle);
66
67 /* Fill in accordingly, defaulting to 8-bit UNORM */
68
69 struct mali_rt_format fmt = {
70 .unk1 = 0x4000000,
71 .unk2 = 0x1,
72 .nr_channels = MALI_POSITIVE(desc->nr_channels),
73 .unk3 = 0x4,
74 .flags = 0x8,
75 .swizzle = panfrost_translate_swizzle_4(swizzle),
76 .unk4 = 0x8
77 };
78
79 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
80 fmt.flags |= MALI_MFBD_FORMAT_SRGB;
81
82 /* sRGB handled as a dedicated flag */
83 enum pipe_format linearized = util_format_linear(surf->format);
84
85 /* If RGB, we're good to go */
86 if (util_format_is_unorm8(desc))
87 return fmt;
88
89 /* Set flags for alternative formats */
90
91 switch (linearized) {
92 case PIPE_FORMAT_B5G6R5_UNORM:
93 fmt.unk1 = 0x14000000;
94 fmt.nr_channels = MALI_POSITIVE(2);
95 fmt.unk3 |= 0x1;
96 break;
97
98 case PIPE_FORMAT_A4B4G4R4_UNORM:
99 case PIPE_FORMAT_B4G4R4A4_UNORM:
100 fmt.unk1 = 0x10000000;
101 fmt.unk3 = 0x5;
102 fmt.nr_channels = MALI_POSITIVE(1);
103 break;
104
105 case PIPE_FORMAT_R10G10B10A2_UNORM:
106 case PIPE_FORMAT_B10G10R10A2_UNORM:
107 case PIPE_FORMAT_R10G10B10X2_UNORM:
108 case PIPE_FORMAT_B10G10R10X2_UNORM:
109 fmt.unk1 = 0x08000000;
110 fmt.unk3 = 0x6;
111 fmt.nr_channels = MALI_POSITIVE(1);
112 break;
113
114 /* Generic 8-bit */
115 case PIPE_FORMAT_R8_UINT:
116 case PIPE_FORMAT_R8_SINT:
117 fmt.unk1 = 0x80000000;
118 fmt.unk3 = 0x0;
119 fmt.nr_channels = MALI_POSITIVE(1);
120 break;
121
122 /* Generic 32-bit */
123 case PIPE_FORMAT_R11G11B10_FLOAT:
124 case PIPE_FORMAT_R8G8B8A8_UINT:
125 case PIPE_FORMAT_R8G8B8A8_SINT:
126 case PIPE_FORMAT_R16G16_FLOAT:
127 case PIPE_FORMAT_R16G16_UINT:
128 case PIPE_FORMAT_R16G16_SINT:
129 case PIPE_FORMAT_R32_FLOAT:
130 case PIPE_FORMAT_R32_UINT:
131 case PIPE_FORMAT_R32_SINT:
132 case PIPE_FORMAT_R10G10B10A2_UINT:
133 fmt.unk1 = 0x88000000;
134 fmt.unk3 = 0x0;
135 fmt.nr_channels = MALI_POSITIVE(4);
136 break;
137
138 /* Generic 16-bit */
139 case PIPE_FORMAT_R8G8_UINT:
140 case PIPE_FORMAT_R8G8_SINT:
141 case PIPE_FORMAT_R16_FLOAT:
142 case PIPE_FORMAT_R16_UINT:
143 case PIPE_FORMAT_R16_SINT:
144 case PIPE_FORMAT_B5G5R5A1_UNORM:
145 fmt.unk1 = 0x84000000;
146 fmt.unk3 = 0x0;
147 fmt.nr_channels = MALI_POSITIVE(2);
148 break;
149
150 /* Generic 64-bit */
151 case PIPE_FORMAT_R32G32_FLOAT:
152 case PIPE_FORMAT_R32G32_SINT:
153 case PIPE_FORMAT_R32G32_UINT:
154 case PIPE_FORMAT_R16G16B16A16_FLOAT:
155 case PIPE_FORMAT_R16G16B16A16_SINT:
156 case PIPE_FORMAT_R16G16B16A16_UINT:
157 fmt.unk1 = 0x8c000000;
158 fmt.unk3 = 0x1;
159 fmt.nr_channels = MALI_POSITIVE(2);
160 break;
161
162 /* Generic 128-bit */
163 case PIPE_FORMAT_R32G32B32A32_FLOAT:
164 case PIPE_FORMAT_R32G32B32A32_SINT:
165 case PIPE_FORMAT_R32G32B32A32_UINT:
166 fmt.unk1 = 0x90000000;
167 fmt.unk3 = 0x1;
168 fmt.nr_channels = MALI_POSITIVE(4);
169 break;
170
171 default:
172 unreachable("Invalid format rendering");
173 }
174
175 return fmt;
176 }
177
178
179 static void
180 panfrost_mfbd_clear(
181 struct panfrost_job *job,
182 struct bifrost_framebuffer *fb,
183 struct bifrost_fb_extra *fbx,
184 struct bifrost_render_target *rts,
185 unsigned rt_count)
186 {
187 for (unsigned i = 0; i < rt_count; ++i) {
188 if (!(job->clear & (PIPE_CLEAR_COLOR0 << i)))
189 continue;
190
191 rts[i].clear_color_1 = job->clear_color[i][0];
192 rts[i].clear_color_2 = job->clear_color[i][1];
193 rts[i].clear_color_3 = job->clear_color[i][2];
194 rts[i].clear_color_4 = job->clear_color[i][3];
195 }
196
197 if (job->clear & PIPE_CLEAR_DEPTH) {
198 fb->clear_depth = job->clear_depth;
199 }
200
201 if (job->clear & PIPE_CLEAR_STENCIL) {
202 fb->clear_stencil = job->clear_stencil;
203 }
204 }
205
206 static void
207 panfrost_mfbd_set_cbuf(
208 struct bifrost_render_target *rt,
209 struct pipe_surface *surf)
210 {
211 struct panfrost_resource *rsrc = pan_resource(surf->texture);
212
213 unsigned level = surf->u.tex.level;
214 unsigned first_layer = surf->u.tex.first_layer;
215 assert(surf->u.tex.last_layer == first_layer);
216 int stride = rsrc->slices[level].stride;
217
218 mali_ptr base = panfrost_get_texture_address(rsrc, level, first_layer);
219
220 rt->format = panfrost_mfbd_format(surf);
221
222 /* Now, we set the layout specific pieces */
223
224 if (rsrc->layout == PAN_LINEAR) {
225 rt->format.block = MALI_MFBD_BLOCK_LINEAR;
226 rt->framebuffer = base;
227 rt->framebuffer_stride = stride / 16;
228 } else if (rsrc->layout == PAN_TILED) {
229 rt->format.block = MALI_MFBD_BLOCK_TILED;
230 rt->framebuffer = base;
231 rt->framebuffer_stride = stride;
232 } else if (rsrc->layout == PAN_AFBC) {
233 rt->format.block = MALI_MFBD_BLOCK_AFBC;
234
235 unsigned header_size = rsrc->slices[level].header_size;
236
237 rt->framebuffer = base + header_size;
238 rt->afbc.metadata = base;
239 rt->afbc.stride = 0;
240 rt->afbc.unk = 0x30009;
241
242 /* TODO: The blob sets this to something nonzero, but it's not
243 * clear what/how to calculate/if it matters */
244 rt->framebuffer_stride = 0;
245 } else {
246 fprintf(stderr, "Invalid render layout (cbuf)");
247 assert(0);
248 }
249 }
250
251 /* Is a format encoded like Z24S8 and therefore compatible for render? */
252
253 static bool
254 panfrost_is_z24s8_variant(enum pipe_format fmt)
255 {
256 switch (fmt) {
257 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
258 case PIPE_FORMAT_Z24X8_UNORM:
259 return true;
260 default:
261 return false;
262 }
263 }
264
265 static void
266 panfrost_mfbd_set_zsbuf(
267 struct bifrost_framebuffer *fb,
268 struct bifrost_fb_extra *fbx,
269 struct pipe_surface *surf)
270 {
271 struct panfrost_resource *rsrc = pan_resource(surf->texture);
272
273 unsigned level = surf->u.tex.level;
274 assert(surf->u.tex.first_layer == 0);
275
276 unsigned offset = rsrc->slices[level].offset;
277
278 if (rsrc->layout == PAN_AFBC) {
279 /* The only Z/S format we can compress is Z24S8 or variants
280 * thereof (handled by the state tracker) */
281 assert(panfrost_is_z24s8_variant(surf->format));
282
283 mali_ptr base = rsrc->bo->gpu + offset;
284 unsigned header_size = rsrc->slices[level].header_size;
285
286 fb->mfbd_flags |= MALI_MFBD_EXTRA;
287
288 fbx->flags =
289 MALI_EXTRA_PRESENT |
290 MALI_EXTRA_AFBC |
291 MALI_EXTRA_AFBC_ZS |
292 MALI_EXTRA_ZS |
293 0x1; /* unknown */
294
295 fbx->ds_afbc.depth_stencil = base + header_size;
296 fbx->ds_afbc.depth_stencil_afbc_metadata = base;
297 fbx->ds_afbc.depth_stencil_afbc_stride = 0;
298
299 fbx->ds_afbc.zero1 = 0x10009;
300 fbx->ds_afbc.padding = 0x1000;
301 } else if (rsrc->layout == PAN_LINEAR) {
302 /* TODO: Z32F(S8) support, which is always linear */
303
304 int stride = rsrc->slices[level].stride;
305
306 fb->mfbd_flags |= MALI_MFBD_EXTRA;
307 fbx->flags |= MALI_EXTRA_PRESENT | MALI_EXTRA_ZS;
308
309 fbx->ds_linear.depth = rsrc->bo->gpu + offset;
310 fbx->ds_linear.depth_stride = stride;
311
312 if (panfrost_is_z24s8_variant(surf->format)) {
313 fbx->flags |= 0x1;
314 } else if (surf->format == PIPE_FORMAT_Z32_UNORM) {
315 /* default flags (0 in bottom place) */
316 } else if (surf->format == PIPE_FORMAT_Z32_FLOAT) {
317 fbx->flags |= 0xA;
318 fb->mfbd_flags ^= 0x100;
319 fb->mfbd_flags |= 0x200;
320 } else if (surf->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
321 fbx->flags |= 0x1000A;
322 fb->mfbd_flags ^= 0x100;
323 fb->mfbd_flags |= 0x201;
324
325 struct panfrost_resource *stencil = rsrc->separate_stencil;
326 struct panfrost_slice stencil_slice = stencil->slices[level];
327
328 fbx->ds_linear.stencil = stencil->bo->gpu + stencil_slice.offset;
329 fbx->ds_linear.stencil_stride = stencil_slice.stride;
330 }
331
332 } else {
333 assert(0);
334 }
335 }
336
337 /* Helper for sequential uploads used for MFBD */
338
339 #define UPLOAD(dest, offset, src, max) { \
340 size_t sz = sizeof(*src); \
341 memcpy(dest.cpu + offset, src, sz); \
342 assert((offset + sz) <= max); \
343 offset += sz; \
344 }
345
346 static mali_ptr
347 panfrost_mfbd_upload(
348 struct panfrost_context *ctx,
349 struct bifrost_framebuffer *fb,
350 struct bifrost_fb_extra *fbx,
351 struct bifrost_render_target *rts,
352 unsigned cbufs)
353 {
354 off_t offset = 0;
355
356 /* There may be extra data stuck in the middle */
357 bool has_extra = fb->mfbd_flags & MALI_MFBD_EXTRA;
358
359 /* Compute total size for transfer */
360
361 size_t total_sz =
362 sizeof(struct bifrost_framebuffer) +
363 (has_extra ? sizeof(struct bifrost_fb_extra) : 0) +
364 sizeof(struct bifrost_render_target) * cbufs;
365
366 struct panfrost_transfer m_f_trans =
367 panfrost_allocate_transient(ctx, total_sz);
368
369 /* Do the transfer */
370
371 UPLOAD(m_f_trans, offset, fb, total_sz);
372
373 if (has_extra)
374 UPLOAD(m_f_trans, offset, fbx, total_sz);
375
376 for (unsigned c = 0; c < cbufs; ++c) {
377 UPLOAD(m_f_trans, offset, &rts[c], total_sz);
378 }
379
380 /* Return pointer suitable for the fragment section */
381 return m_f_trans.gpu | MALI_MFBD | (has_extra ? 2 : 0);
382 }
383
384 #undef UPLOAD
385
386 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
387
388 mali_ptr
389 panfrost_mfbd_fragment(struct panfrost_context *ctx, bool has_draws)
390 {
391 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
392
393 struct bifrost_framebuffer fb = panfrost_emit_mfbd(ctx, has_draws);
394 struct bifrost_fb_extra fbx = {};
395 struct bifrost_render_target rts[4] = {};
396
397 /* XXX: MRT case */
398 fb.rt_count_2 = 1;
399 fb.mfbd_flags = 0x100;
400
401 /* TODO: MRT clear */
402 panfrost_mfbd_clear(job, &fb, &fbx, rts, fb.rt_count_2);
403
404 for (int cb = 0; cb < ctx->pipe_framebuffer.nr_cbufs; ++cb) {
405 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[cb];
406
407 if (!surf)
408 continue;
409
410 unsigned bpp = util_format_get_blocksize(surf->format);
411
412 panfrost_mfbd_set_cbuf(&rts[cb], surf);
413
414 /* What is this? Looks like some extension of the bpp field.
415 * Maybe it establishes how much internal tilebuffer space is
416 * reserved? */
417 fb.rt_count_2 = MAX2(fb.rt_count_2, ALIGN_POT(bpp, 4) / 4);
418 }
419
420 if (ctx->pipe_framebuffer.zsbuf) {
421 panfrost_mfbd_set_zsbuf(&fb, &fbx, ctx->pipe_framebuffer.zsbuf);
422 }
423
424 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
425
426 if (ctx->pipe_framebuffer.nr_cbufs == 0) {
427 struct mali_rt_format null_rt = {
428 .unk1 = 0x4000000,
429 .unk4 = 0x8
430 };
431
432 rts[0].format = null_rt;
433 rts[0].framebuffer = 0;
434 rts[0].framebuffer_stride = 0;
435 }
436
437 /* When scanning out, the depth buffer is immediately invalidated, so
438 * we don't need to waste bandwidth writing it out. This can improve
439 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
440 * memory bandwidth!).
441 *
442 * The exception is ReadPixels, but this is not supported on GLES so we
443 * can safely ignore it. */
444
445 if (panfrost_is_scanout(ctx)) {
446 job->requirements &= ~PAN_REQ_DEPTH_WRITE;
447 }
448
449 /* Actualize the requirements */
450
451 if (job->requirements & PAN_REQ_MSAA) {
452 rts[0].format.flags |= MALI_MFBD_FORMAT_MSAA;
453
454 /* XXX */
455 fb.unk1 |= (1 << 4) | (1 << 1);
456 fb.rt_count_2 = 4;
457 }
458
459 if (job->requirements & PAN_REQ_DEPTH_WRITE)
460 fb.mfbd_flags |= MALI_MFBD_DEPTH_WRITE;
461
462 /* Checksumming only works with a single render target */
463
464 if (ctx->pipe_framebuffer.nr_cbufs == 1) {
465 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[0];
466 struct panfrost_resource *rsrc = pan_resource(surf->texture);
467 struct panfrost_bo *bo = rsrc->bo;
468
469 if (rsrc->checksummed) {
470 unsigned level = surf->u.tex.level;
471 struct panfrost_slice *slice = &rsrc->slices[level];
472
473 fb.mfbd_flags |= MALI_MFBD_EXTRA;
474 fbx.flags |= MALI_EXTRA_PRESENT;
475 fbx.checksum_stride = slice->checksum_stride;
476 fbx.checksum = bo->gpu + slice->checksum_offset;
477 }
478 }
479
480 /* We always upload at least one (dummy) cbuf */
481 unsigned cbufs = MAX2(ctx->pipe_framebuffer.nr_cbufs, 1);
482
483 return panfrost_mfbd_upload(ctx, &fb, &fbx, rts, cbufs);
484 }