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