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