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