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
31static struct mali_rt_format
32panfrost_mfbd_format(struct pipe_surface *surf)
33{
34        /* Explode details on the format */
35
36        const struct util_format_description *desc =
37                util_format_description(surf->texture->format);
38
39        /* Fill in accordingly, defaulting to RGBA8888 (UNORM) */
40
41        struct mali_rt_format fmt = {
42                .unk1 = 0x4000000,
43                .unk2 = 0x1,
44                .nr_channels = MALI_POSITIVE(desc->nr_channels),
45                .flags = 0x444,
46                .swizzle = panfrost_translate_swizzle_4(desc->swizzle),
47                .unk4 = 0x8
48        };
49
50        /* Set flags for alternative formats */
51
52        if (surf->texture->format == PIPE_FORMAT_B5G6R5_UNORM) {
53                fmt.unk1 = 0x14000000;
54                fmt.nr_channels = MALI_POSITIVE(2);
55                fmt.flags |= 0x1;
56        }
57
58        return fmt;
59}
60
61
62static void
63panfrost_mfbd_clear(
64                struct panfrost_job *job,
65                struct bifrost_framebuffer *fb,
66                struct bifrost_fb_extra *fbx,
67                struct bifrost_render_target *rt)
68{
69        if (job->clear & PIPE_CLEAR_COLOR) {
70                rt->clear_color_1 = job->clear_color;
71                rt->clear_color_2 = job->clear_color;
72                rt->clear_color_3 = job->clear_color;
73                rt->clear_color_4 = job->clear_color;
74        }
75
76        if (job->clear & PIPE_CLEAR_DEPTH) {
77                fb->clear_depth = job->clear_depth;
78        }
79
80        if (job->clear & PIPE_CLEAR_STENCIL) {
81                fb->clear_stencil = job->clear_stencil;
82        }
83}
84
85static void
86panfrost_mfbd_set_cbuf(
87                struct bifrost_render_target *rt,
88                struct pipe_surface *surf,
89                bool flip_y)
90{
91        struct panfrost_resource *rsrc = pan_resource(surf->texture);
92        int stride = rsrc->bo->slices[0].stride;
93
94        rt->format = panfrost_mfbd_format(surf);
95
96        /* Now, we set the layout specific pieces */
97
98        if (rsrc->bo->layout == PAN_LINEAR) {
99                mali_ptr framebuffer = rsrc->bo->gpu;
100
101                if (flip_y) {
102                        framebuffer += stride * (surf->texture->height0 - 1);
103                        stride = -stride;
104                }
105
106                rt->framebuffer = framebuffer;
107                rt->framebuffer_stride = stride / 16;
108        } else if (rsrc->bo->layout == PAN_AFBC) {
109                rt->afbc.metadata = rsrc->bo->afbc_slab.gpu;
110                rt->afbc.stride = 0;
111                rt->afbc.unk = 0x30009;
112
113                rt->format.flags |= MALI_MFBD_FORMAT_AFBC;
114
115                mali_ptr afbc_main = rsrc->bo->afbc_slab.gpu + rsrc->bo->afbc_metadata_size;
116                rt->framebuffer = afbc_main;
117
118                /* TODO: Investigate shift */
119                rt->framebuffer_stride = stride << 1;
120        } else {
121                fprintf(stderr, "Invalid render layout (cbuf)");
122                assert(0);
123        }
124}
125
126static void
127panfrost_mfbd_set_zsbuf(
128                struct bifrost_framebuffer *fb,
129                struct bifrost_fb_extra *fbx,
130                struct pipe_surface *surf)
131{
132        struct panfrost_resource *rsrc = pan_resource(surf->texture);
133
134        if (rsrc->bo->layout == PAN_AFBC) {
135                fb->unk3 |= MALI_MFBD_EXTRA;
136
137                fbx->flags =
138                        MALI_EXTRA_PRESENT |
139                        MALI_EXTRA_AFBC |
140                        MALI_EXTRA_AFBC_ZS |
141                        MALI_EXTRA_ZS |
142                        0x1; /* unknown */
143
144                fbx->ds_afbc.depth_stencil_afbc_metadata = rsrc->bo->afbc_slab.gpu;
145                fbx->ds_afbc.depth_stencil_afbc_stride = 0;
146
147                fbx->ds_afbc.depth_stencil = rsrc->bo->afbc_slab.gpu + rsrc->bo->afbc_metadata_size;
148
149                fbx->ds_afbc.zero1 = 0x10009;
150                fbx->ds_afbc.padding = 0x1000;
151        } else if (rsrc->bo->layout == PAN_LINEAR) {
152                fb->unk3 |= MALI_MFBD_EXTRA;
153                fbx->flags |= MALI_EXTRA_PRESENT | MALI_EXTRA_ZS | 0x1;
154
155                fbx->ds_linear.depth = rsrc->bo->gpu;
156                fbx->ds_linear.depth_stride = rsrc->bo->slices[0].stride;
157        } else {
158                assert(0);
159        }
160}
161
162/* Helper for sequential uploads used for MFBD */
163
164#define UPLOAD(dest, offset, src, max) { \
165        size_t sz = sizeof(*src); \
166        memcpy(dest.cpu + offset, src, sz); \
167        assert((offset + sz) <= max); \
168        offset += sz; \
169}
170
171static mali_ptr
172panfrost_mfbd_upload(
173                struct panfrost_context *ctx,
174                struct bifrost_framebuffer *fb,
175                struct bifrost_fb_extra *fbx,
176                struct bifrost_render_target *rts,
177                unsigned cbufs)
178{
179        off_t offset = 0;
180
181        /* There may be extra data stuck in the middle */
182        bool has_extra = fb->unk3 & MALI_MFBD_EXTRA;
183
184        /* Compute total size for transfer */
185
186        size_t total_sz =
187                sizeof(struct bifrost_framebuffer) +
188                (has_extra ? sizeof(struct bifrost_fb_extra) : 0) +
189                sizeof(struct bifrost_render_target) * cbufs;
190
191        struct panfrost_transfer m_f_trans =
192                panfrost_allocate_transient(ctx, total_sz);
193
194        /* Do the transfer */
195
196        UPLOAD(m_f_trans, offset, fb, total_sz);
197
198        if (has_extra)
199                UPLOAD(m_f_trans, offset, fbx, total_sz);
200
201        for (unsigned c = 0; c < cbufs; ++c) {
202                UPLOAD(m_f_trans, offset, &rts[c], total_sz);
203        }
204
205        /* Return pointer suitable for the fragment section */
206        return m_f_trans.gpu | MALI_MFBD | (has_extra ? 2 : 0);
207}
208
209#undef UPLOAD
210
211/* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
212
213mali_ptr
214panfrost_mfbd_fragment(struct panfrost_context *ctx, bool flip_y)
215{
216        struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
217
218        struct bifrost_framebuffer fb = panfrost_emit_mfbd(ctx);
219        struct bifrost_fb_extra fbx = {};
220        struct bifrost_render_target rts[4] = {};
221
222        /* XXX: MRT case */
223        fb.rt_count_2 = 1;
224        fb.unk3 = 0x100;
225
226        /* TODO: MRT clear */
227        panfrost_mfbd_clear(job, &fb, &fbx, &rts[0]);
228
229        for (int cb = 0; cb < ctx->pipe_framebuffer.nr_cbufs; ++cb) {
230                struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[cb];
231                panfrost_mfbd_set_cbuf(&rts[cb], surf, flip_y);
232        }
233
234        if (ctx->pipe_framebuffer.zsbuf) {
235                panfrost_mfbd_set_zsbuf(&fb, &fbx, ctx->pipe_framebuffer.zsbuf);
236        }
237
238        /* For the special case of a depth-only FBO, we need to attach a dummy render target */
239
240        if (ctx->pipe_framebuffer.nr_cbufs == 0) {
241                struct mali_rt_format null_rt = {
242                        .unk1 = 0x4000000,
243                        .unk4 = 0x8
244                };
245
246                rts[0].format = null_rt;
247                rts[0].framebuffer = 0;
248                rts[0].framebuffer_stride = 0;
249        }
250
251        /* When scanning out, the depth buffer is immediately invalidated, so
252         * we don't need to waste bandwidth writing it out. This can improve
253         * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
254         * memory bandwidth!).
255         *
256         * The exception is ReadPixels, but this is not supported on GLES so we
257         * can safely ignore it. */
258
259        if (panfrost_is_scanout(ctx)) {
260                job->requirements &= ~PAN_REQ_DEPTH_WRITE;
261        }
262
263        /* Actualize the requirements */
264
265        if (job->requirements & PAN_REQ_MSAA) {
266                rts[0].format.flags |= MALI_MFBD_FORMAT_MSAA;
267
268                /* XXX */
269                fb.unk1 |= (1 << 4) | (1 << 1);
270                fb.rt_count_2 = 4;
271        }
272
273        if (job->requirements & PAN_REQ_DEPTH_WRITE)
274                fb.unk3 |= MALI_MFBD_DEPTH_WRITE;
275
276        if (ctx->pipe_framebuffer.nr_cbufs == 1) {
277                struct panfrost_resource *rsrc = (struct panfrost_resource *) ctx->pipe_framebuffer.cbufs[0]->texture;
278
279                if (rsrc->bo->has_checksum) {
280                        fb.unk3 |= MALI_MFBD_EXTRA;
281                        fbx.flags |= MALI_EXTRA_PRESENT;
282                        fbx.checksum_stride = rsrc->bo->checksum_stride;
283                        fbx.checksum = rsrc->bo->gpu + rsrc->bo->slices[0].stride * rsrc->base.height0;
284                }
285        }
286
287        /* We always upload at least one (dummy) cbuf */
288        unsigned cbufs = MAX2(ctx->pipe_framebuffer.nr_cbufs, 1);
289
290        return panfrost_mfbd_upload(ctx, &fb, &fbx, rts, cbufs);
291}
292