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