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