panfrost: Set rt_count_2 for bpp>4 formats
[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 /* Set flags for alternative formats */
83
84 bool float_16 =
85 surf->format == PIPE_FORMAT_R16_FLOAT;
86
87 bool float_32 =
88 surf->format == PIPE_FORMAT_R11G11B10_FLOAT ||
89 surf->format == PIPE_FORMAT_R32_FLOAT ||
90 surf->format == PIPE_FORMAT_R16G16_FLOAT;
91
92 bool float_64 =
93 surf->format == PIPE_FORMAT_R32G32_FLOAT ||
94 surf->format == PIPE_FORMAT_R16G16B16A16_FLOAT;
95
96 bool float_128 =
97 surf->format == PIPE_FORMAT_R32G32B32A32_FLOAT;
98
99 if (surf->format == PIPE_FORMAT_B5G6R5_UNORM) {
100 fmt.unk1 = 0x14000000;
101 fmt.nr_channels = MALI_POSITIVE(2);
102 fmt.unk3 |= 0x1;
103 } else if (float_32) {
104 fmt.unk1 = 0x88000000;
105 fmt.unk3 = 0x0;
106 fmt.nr_channels = MALI_POSITIVE(4);
107 } else if (float_16) {
108 fmt.unk1 = 0x84000000;
109 fmt.unk3 = 0x0;
110 fmt.nr_channels = MALI_POSITIVE(2);
111 } else if (float_64) {
112 fmt.unk1 = 0x8c000000;
113 fmt.unk3 = 0x1;
114 fmt.nr_channels = MALI_POSITIVE(2);
115 } else if (float_128) {
116 fmt.unk1 = 0x90000000;
117 fmt.unk3 = 0x1;
118 fmt.nr_channels = MALI_POSITIVE(4);
119 }
120
121 return fmt;
122 }
123
124
125 static void
126 panfrost_mfbd_clear(
127 struct panfrost_job *job,
128 struct bifrost_framebuffer *fb,
129 struct bifrost_fb_extra *fbx,
130 struct bifrost_render_target *rts,
131 unsigned rt_count)
132 {
133 for (unsigned i = 0; i < rt_count; ++i) {
134 if (!(job->clear & (PIPE_CLEAR_COLOR0 << i)))
135 continue;
136
137 rts[i].clear_color_1 = job->clear_color[i][0];
138 rts[i].clear_color_2 = job->clear_color[i][1];
139 rts[i].clear_color_3 = job->clear_color[i][2];
140 rts[i].clear_color_4 = job->clear_color[i][3];
141 }
142
143 if (job->clear & PIPE_CLEAR_DEPTH) {
144 fb->clear_depth = job->clear_depth;
145 }
146
147 if (job->clear & PIPE_CLEAR_STENCIL) {
148 fb->clear_stencil = job->clear_stencil;
149 }
150 }
151
152 static void
153 panfrost_mfbd_set_cbuf(
154 struct bifrost_render_target *rt,
155 struct pipe_surface *surf)
156 {
157 struct panfrost_resource *rsrc = pan_resource(surf->texture);
158
159 unsigned level = surf->u.tex.level;
160 unsigned first_layer = surf->u.tex.first_layer;
161 assert(surf->u.tex.last_layer == first_layer);
162 int stride = rsrc->slices[level].stride;
163
164 mali_ptr base = panfrost_get_texture_address(rsrc, level, first_layer);
165
166 rt->format = panfrost_mfbd_format(surf);
167
168 /* Now, we set the layout specific pieces */
169
170 if (rsrc->layout == PAN_LINEAR) {
171 rt->format.block = MALI_MFBD_BLOCK_LINEAR;
172 rt->framebuffer = base;
173 rt->framebuffer_stride = stride / 16;
174 } else if (rsrc->layout == PAN_TILED) {
175 rt->format.block = MALI_MFBD_BLOCK_TILED;
176 rt->framebuffer = base;
177 rt->framebuffer_stride = stride;
178 } else if (rsrc->layout == PAN_AFBC) {
179 rt->format.block = MALI_MFBD_BLOCK_AFBC;
180
181 unsigned header_size = rsrc->slices[level].header_size;
182
183 rt->framebuffer = base + header_size;
184 rt->afbc.metadata = base;
185 rt->afbc.stride = 0;
186 rt->afbc.unk = 0x30009;
187
188 /* TODO: Investigate shift */
189 rt->framebuffer_stride = stride << 1;
190 } else {
191 fprintf(stderr, "Invalid render layout (cbuf)");
192 assert(0);
193 }
194 }
195
196 static void
197 panfrost_mfbd_set_zsbuf(
198 struct bifrost_framebuffer *fb,
199 struct bifrost_fb_extra *fbx,
200 struct pipe_surface *surf)
201 {
202 struct panfrost_resource *rsrc = pan_resource(surf->texture);
203
204 unsigned level = surf->u.tex.level;
205 assert(surf->u.tex.first_layer == 0);
206
207 unsigned offset = rsrc->slices[level].offset;
208
209 if (rsrc->layout == PAN_AFBC) {
210 mali_ptr base = rsrc->bo->gpu + offset;
211 unsigned header_size = rsrc->slices[level].header_size;
212
213 fb->mfbd_flags |= MALI_MFBD_EXTRA;
214
215 fbx->flags =
216 MALI_EXTRA_PRESENT |
217 MALI_EXTRA_AFBC |
218 MALI_EXTRA_AFBC_ZS |
219 MALI_EXTRA_ZS |
220 0x1; /* unknown */
221
222 fbx->ds_afbc.depth_stencil = base + header_size;
223 fbx->ds_afbc.depth_stencil_afbc_metadata = base;
224 fbx->ds_afbc.depth_stencil_afbc_stride = 0;
225
226 fbx->ds_afbc.zero1 = 0x10009;
227 fbx->ds_afbc.padding = 0x1000;
228 } else if (rsrc->layout == PAN_LINEAR) {
229 int stride = rsrc->slices[level].stride;
230 fb->mfbd_flags |= MALI_MFBD_EXTRA;
231
232 fbx->flags |= MALI_EXTRA_PRESENT | MALI_EXTRA_ZS | 0x1;
233
234 fbx->ds_linear.depth = rsrc->bo->gpu + offset;
235 fbx->ds_linear.depth_stride = stride;
236 } else {
237 assert(0);
238 }
239 }
240
241 /* Helper for sequential uploads used for MFBD */
242
243 #define UPLOAD(dest, offset, src, max) { \
244 size_t sz = sizeof(*src); \
245 memcpy(dest.cpu + offset, src, sz); \
246 assert((offset + sz) <= max); \
247 offset += sz; \
248 }
249
250 static mali_ptr
251 panfrost_mfbd_upload(
252 struct panfrost_context *ctx,
253 struct bifrost_framebuffer *fb,
254 struct bifrost_fb_extra *fbx,
255 struct bifrost_render_target *rts,
256 unsigned cbufs)
257 {
258 off_t offset = 0;
259
260 /* There may be extra data stuck in the middle */
261 bool has_extra = fb->mfbd_flags & MALI_MFBD_EXTRA;
262
263 /* Compute total size for transfer */
264
265 size_t total_sz =
266 sizeof(struct bifrost_framebuffer) +
267 (has_extra ? sizeof(struct bifrost_fb_extra) : 0) +
268 sizeof(struct bifrost_render_target) * cbufs;
269
270 struct panfrost_transfer m_f_trans =
271 panfrost_allocate_transient(ctx, total_sz);
272
273 /* Do the transfer */
274
275 UPLOAD(m_f_trans, offset, fb, total_sz);
276
277 if (has_extra)
278 UPLOAD(m_f_trans, offset, fbx, total_sz);
279
280 for (unsigned c = 0; c < cbufs; ++c) {
281 UPLOAD(m_f_trans, offset, &rts[c], total_sz);
282 }
283
284 /* Return pointer suitable for the fragment section */
285 return m_f_trans.gpu | MALI_MFBD | (has_extra ? 2 : 0);
286 }
287
288 #undef UPLOAD
289
290 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
291
292 mali_ptr
293 panfrost_mfbd_fragment(struct panfrost_context *ctx, bool has_draws)
294 {
295 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
296
297 struct bifrost_framebuffer fb = panfrost_emit_mfbd(ctx, has_draws);
298 struct bifrost_fb_extra fbx = {};
299 struct bifrost_render_target rts[4] = {};
300
301 /* XXX: MRT case */
302 fb.rt_count_2 = 1;
303 fb.mfbd_flags = 0x100;
304
305 /* TODO: MRT clear */
306 panfrost_mfbd_clear(job, &fb, &fbx, rts, fb.rt_count_2);
307
308 for (int cb = 0; cb < ctx->pipe_framebuffer.nr_cbufs; ++cb) {
309 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[cb];
310 unsigned bpp = util_format_get_blocksize(surf->format);
311
312 panfrost_mfbd_set_cbuf(&rts[cb], surf);
313
314 /* What is this? Looks like some extension of the bpp field.
315 * Maybe it establishes how much internal tilebuffer space is
316 * reserved? */
317 fb.rt_count_2 = MAX2(fb.rt_count_2, ALIGN_POT(bpp, 4) / 4);
318 }
319
320 if (ctx->pipe_framebuffer.zsbuf) {
321 panfrost_mfbd_set_zsbuf(&fb, &fbx, ctx->pipe_framebuffer.zsbuf);
322 }
323
324 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
325
326 if (ctx->pipe_framebuffer.nr_cbufs == 0) {
327 struct mali_rt_format null_rt = {
328 .unk1 = 0x4000000,
329 .unk4 = 0x8
330 };
331
332 rts[0].format = null_rt;
333 rts[0].framebuffer = 0;
334 rts[0].framebuffer_stride = 0;
335 }
336
337 /* When scanning out, the depth buffer is immediately invalidated, so
338 * we don't need to waste bandwidth writing it out. This can improve
339 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
340 * memory bandwidth!).
341 *
342 * The exception is ReadPixels, but this is not supported on GLES so we
343 * can safely ignore it. */
344
345 if (panfrost_is_scanout(ctx)) {
346 job->requirements &= ~PAN_REQ_DEPTH_WRITE;
347 }
348
349 /* Actualize the requirements */
350
351 if (job->requirements & PAN_REQ_MSAA) {
352 rts[0].format.flags |= MALI_MFBD_FORMAT_MSAA;
353
354 /* XXX */
355 fb.unk1 |= (1 << 4) | (1 << 1);
356 fb.rt_count_2 = 4;
357 }
358
359 if (job->requirements & PAN_REQ_DEPTH_WRITE)
360 fb.mfbd_flags |= MALI_MFBD_DEPTH_WRITE;
361
362 /* Checksumming only works with a single render target */
363
364 if (ctx->pipe_framebuffer.nr_cbufs == 1) {
365 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[0];
366 struct panfrost_resource *rsrc = pan_resource(surf->texture);
367 struct panfrost_bo *bo = rsrc->bo;
368
369 if (rsrc->checksummed) {
370 unsigned level = surf->u.tex.level;
371 struct panfrost_slice *slice = &rsrc->slices[level];
372
373 fb.mfbd_flags |= MALI_MFBD_EXTRA;
374 fbx.flags |= MALI_EXTRA_PRESENT;
375 fbx.checksum_stride = slice->checksum_stride;
376 fbx.checksum = bo->gpu + slice->checksum_offset;
377 }
378 }
379
380 /* We always upload at least one (dummy) cbuf */
381 unsigned cbufs = MAX2(ctx->pipe_framebuffer.nr_cbufs, 1);
382
383 return panfrost_mfbd_upload(ctx, &fb, &fbx, rts, cbufs);
384 }