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