util: Move gallium's PIPE_FORMAT utils to /util/format/
[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 assert(surf->u.tex.first_layer == 0);
239
240 unsigned offset = rsrc->slices[level].offset;
241
242 if (rsrc->layout == PAN_AFBC) {
243 /* The only Z/S format we can compress is Z24S8 or variants
244 * thereof (handled by the state tracker) */
245 assert(panfrost_is_z24s8_variant(surf->format));
246
247 mali_ptr base = rsrc->bo->gpu + offset;
248 unsigned header_size = rsrc->slices[level].header_size;
249
250 fb->mfbd_flags |= MALI_MFBD_EXTRA;
251
252 fbx->flags =
253 MALI_EXTRA_PRESENT |
254 MALI_EXTRA_AFBC |
255 MALI_EXTRA_AFBC_ZS |
256 MALI_EXTRA_ZS |
257 0x1; /* unknown */
258
259 fbx->ds_afbc.depth_stencil = base + header_size;
260 fbx->ds_afbc.depth_stencil_afbc_metadata = base;
261 fbx->ds_afbc.depth_stencil_afbc_stride = 0;
262
263 fbx->ds_afbc.zero1 = 0x10009;
264 fbx->ds_afbc.padding = 0x1000;
265 } else if (rsrc->layout == PAN_LINEAR) {
266 /* TODO: Z32F(S8) support, which is always linear */
267
268 int stride = rsrc->slices[level].stride;
269
270 fb->mfbd_flags |= MALI_MFBD_EXTRA;
271 fbx->flags |= MALI_EXTRA_PRESENT | MALI_EXTRA_ZS;
272
273 fbx->ds_linear.depth = rsrc->bo->gpu + offset;
274 fbx->ds_linear.depth_stride = stride;
275
276 if (panfrost_is_z24s8_variant(surf->format)) {
277 fbx->flags |= 0x1;
278 } else if (surf->format == PIPE_FORMAT_Z32_UNORM) {
279 /* default flags (0 in bottom place) */
280 } else if (surf->format == PIPE_FORMAT_Z32_FLOAT) {
281 fbx->flags |= 0xA;
282 fb->mfbd_flags ^= 0x100;
283 fb->mfbd_flags |= 0x200;
284 } else if (surf->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
285 fbx->flags |= 0x1000A;
286 fb->mfbd_flags ^= 0x100;
287 fb->mfbd_flags |= 0x201;
288
289 struct panfrost_resource *stencil = rsrc->separate_stencil;
290 struct panfrost_slice stencil_slice = stencil->slices[level];
291
292 fbx->ds_linear.stencil = stencil->bo->gpu + stencil_slice.offset;
293 fbx->ds_linear.stencil_stride = stencil_slice.stride;
294 }
295
296 } else {
297 assert(0);
298 }
299 }
300
301 /* Helper for sequential uploads used for MFBD */
302
303 #define UPLOAD(dest, offset, src, max) { \
304 size_t sz = sizeof(*src); \
305 memcpy(dest.cpu + offset, src, sz); \
306 assert((offset + sz) <= max); \
307 offset += sz; \
308 }
309
310 static mali_ptr
311 panfrost_mfbd_upload(struct panfrost_batch *batch,
312 struct bifrost_framebuffer *fb,
313 struct bifrost_fb_extra *fbx,
314 struct bifrost_render_target *rts,
315 unsigned rt_count)
316 {
317 off_t offset = 0;
318
319 /* There may be extra data stuck in the middle */
320 bool has_extra = fb->mfbd_flags & MALI_MFBD_EXTRA;
321
322 /* Compute total size for transfer */
323
324 size_t total_sz =
325 sizeof(struct bifrost_framebuffer) +
326 (has_extra ? sizeof(struct bifrost_fb_extra) : 0) +
327 sizeof(struct bifrost_render_target) * 4;
328
329 struct panfrost_transfer m_f_trans =
330 panfrost_allocate_transient(batch, total_sz);
331
332 /* Do the transfer */
333
334 UPLOAD(m_f_trans, offset, fb, total_sz);
335
336 if (has_extra)
337 UPLOAD(m_f_trans, offset, fbx, total_sz);
338
339 for (unsigned c = 0; c < 4; ++c) {
340 UPLOAD(m_f_trans, offset, &rts[c], total_sz);
341 }
342
343 /* Return pointer suitable for the fragment section */
344 unsigned tag =
345 MALI_MFBD |
346 (has_extra ? MALI_MFBD_TAG_EXTRA : 0) |
347 (MALI_POSITIVE(rt_count) << 2);
348
349 return m_f_trans.gpu | tag;
350 }
351
352 #undef UPLOAD
353
354 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
355
356 mali_ptr
357 panfrost_mfbd_fragment(struct panfrost_batch *batch, bool has_draws)
358 {
359 struct bifrost_framebuffer fb = panfrost_emit_mfbd(batch, has_draws);
360 struct bifrost_fb_extra fbx = {};
361 struct bifrost_render_target rts[4] = {};
362
363 /* We always upload at least one dummy GL_NONE render target */
364
365 unsigned rt_descriptors = MAX2(batch->key.nr_cbufs, 1);
366
367 fb.rt_count_1 = MALI_POSITIVE(rt_descriptors);
368 fb.rt_count_2 = rt_descriptors;
369 fb.mfbd_flags = 0x100;
370
371 /* TODO: MRT clear */
372 panfrost_mfbd_clear(batch, &fb, &fbx, rts, fb.rt_count_2);
373
374
375 /* Upload either the render target or a dummy GL_NONE target */
376
377 for (int cb = 0; cb < rt_descriptors; ++cb) {
378 struct pipe_surface *surf = batch->key.cbufs[cb];
379
380 if (surf) {
381 panfrost_mfbd_set_cbuf(&rts[cb], surf);
382
383 /* What is this? Looks like some extension of the bpp
384 * field. Maybe it establishes how much internal
385 * tilebuffer space is reserved? */
386
387 unsigned bpp = util_format_get_blocksize(surf->format);
388 fb.rt_count_2 = MAX2(fb.rt_count_2, ALIGN_POT(bpp, 4) / 4);
389 } else {
390 struct mali_rt_format null_rt = {
391 .unk1 = 0x4000000,
392 .no_preload = true
393 };
394
395 rts[cb].format = null_rt;
396 rts[cb].framebuffer = 0;
397 rts[cb].framebuffer_stride = 0;
398 }
399
400 /* TODO: Break out the field */
401 rts[cb].format.unk1 |= (cb * 0x400);
402 }
403
404 if (batch->key.zsbuf) {
405 panfrost_mfbd_set_zsbuf(&fb, &fbx, batch->key.zsbuf);
406 }
407
408 /* When scanning out, the depth buffer is immediately invalidated, so
409 * we don't need to waste bandwidth writing it out. This can improve
410 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
411 * memory bandwidth!).
412 *
413 * The exception is ReadPixels, but this is not supported on GLES so we
414 * can safely ignore it. */
415
416 if (panfrost_batch_is_scanout(batch))
417 batch->requirements &= ~PAN_REQ_DEPTH_WRITE;
418
419 /* Actualize the requirements */
420
421 if (batch->requirements & PAN_REQ_MSAA) {
422 rts[0].format.flags |= MALI_MFBD_FORMAT_MSAA;
423
424 /* XXX */
425 fb.unk1 |= (1 << 4) | (1 << 1);
426 fb.rt_count_2 = 4;
427 }
428
429 if (batch->requirements & PAN_REQ_DEPTH_WRITE)
430 fb.mfbd_flags |= MALI_MFBD_DEPTH_WRITE;
431
432 /* Checksumming only works with a single render target */
433
434 if (batch->key.nr_cbufs == 1) {
435 struct pipe_surface *surf = batch->key.cbufs[0];
436 struct panfrost_resource *rsrc = pan_resource(surf->texture);
437 struct panfrost_bo *bo = rsrc->bo;
438
439 if (rsrc->checksummed) {
440 unsigned level = surf->u.tex.level;
441 struct panfrost_slice *slice = &rsrc->slices[level];
442
443 fb.mfbd_flags |= MALI_MFBD_EXTRA;
444 fbx.flags |= MALI_EXTRA_PRESENT;
445 fbx.checksum_stride = slice->checksum_stride;
446 fbx.checksum = bo->gpu + slice->checksum_offset;
447 }
448 }
449
450 return panfrost_mfbd_upload(batch, &fb, &fbx, rts, rt_descriptors);
451 }