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