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