panfrost: Decode rendering block type
[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_AFBC) {
108 assert(level == 0);
109 rt->afbc.metadata = rsrc->bo->afbc_slab.gpu;
110 rt->afbc.stride = 0;
111 rt->afbc.unk = 0x30009;
112
113 rt->format.block = MALI_MFBD_BLOCK_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
126 static void
127 panfrost_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 unsigned level = surf->u.tex.level;
135 assert(surf->u.tex.first_layer == 0);
136
137 unsigned offset = rsrc->bo->slices[level].offset;
138
139 if (rsrc->bo->layout == PAN_AFBC) {
140 assert(level == 0);
141 fb->mfbd_flags |= MALI_MFBD_EXTRA;
142
143 fbx->flags =
144 MALI_EXTRA_PRESENT |
145 MALI_EXTRA_AFBC |
146 MALI_EXTRA_AFBC_ZS |
147 MALI_EXTRA_ZS |
148 0x1; /* unknown */
149
150 fbx->ds_afbc.depth_stencil_afbc_metadata = rsrc->bo->afbc_slab.gpu;
151 fbx->ds_afbc.depth_stencil_afbc_stride = 0;
152
153 fbx->ds_afbc.depth_stencil = rsrc->bo->afbc_slab.gpu + rsrc->bo->afbc_metadata_size;
154
155 fbx->ds_afbc.zero1 = 0x10009;
156 fbx->ds_afbc.padding = 0x1000;
157 } else if (rsrc->bo->layout == PAN_LINEAR) {
158 int stride = rsrc->bo->slices[level].stride;
159 fb->mfbd_flags |= MALI_MFBD_EXTRA;
160
161 fbx->flags |= MALI_EXTRA_PRESENT | MALI_EXTRA_ZS | 0x1;
162
163 fbx->ds_linear.depth = rsrc->bo->gpu + offset;
164 fbx->ds_linear.depth_stride = stride;
165 } else {
166 assert(0);
167 }
168 }
169
170 /* Helper for sequential uploads used for MFBD */
171
172 #define UPLOAD(dest, offset, src, max) { \
173 size_t sz = sizeof(*src); \
174 memcpy(dest.cpu + offset, src, sz); \
175 assert((offset + sz) <= max); \
176 offset += sz; \
177 }
178
179 static mali_ptr
180 panfrost_mfbd_upload(
181 struct panfrost_context *ctx,
182 struct bifrost_framebuffer *fb,
183 struct bifrost_fb_extra *fbx,
184 struct bifrost_render_target *rts,
185 unsigned cbufs)
186 {
187 off_t offset = 0;
188
189 /* There may be extra data stuck in the middle */
190 bool has_extra = fb->mfbd_flags & MALI_MFBD_EXTRA;
191
192 /* Compute total size for transfer */
193
194 size_t total_sz =
195 sizeof(struct bifrost_framebuffer) +
196 (has_extra ? sizeof(struct bifrost_fb_extra) : 0) +
197 sizeof(struct bifrost_render_target) * cbufs;
198
199 struct panfrost_transfer m_f_trans =
200 panfrost_allocate_transient(ctx, total_sz);
201
202 /* Do the transfer */
203
204 UPLOAD(m_f_trans, offset, fb, total_sz);
205
206 if (has_extra)
207 UPLOAD(m_f_trans, offset, fbx, total_sz);
208
209 for (unsigned c = 0; c < cbufs; ++c) {
210 UPLOAD(m_f_trans, offset, &rts[c], total_sz);
211 }
212
213 /* Return pointer suitable for the fragment section */
214 return m_f_trans.gpu | MALI_MFBD | (has_extra ? 2 : 0);
215 }
216
217 #undef UPLOAD
218
219 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
220
221 mali_ptr
222 panfrost_mfbd_fragment(struct panfrost_context *ctx, bool has_draws)
223 {
224 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
225
226 struct bifrost_framebuffer fb = panfrost_emit_mfbd(ctx, has_draws);
227 struct bifrost_fb_extra fbx = {};
228 struct bifrost_render_target rts[4] = {};
229
230 /* XXX: MRT case */
231 fb.rt_count_2 = 1;
232 fb.mfbd_flags = 0x100;
233
234 /* TODO: MRT clear */
235 panfrost_mfbd_clear(job, &fb, &fbx, &rts[0]);
236
237 for (int cb = 0; cb < ctx->pipe_framebuffer.nr_cbufs; ++cb) {
238 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[cb];
239 panfrost_mfbd_set_cbuf(&rts[cb], surf);
240 }
241
242 if (ctx->pipe_framebuffer.zsbuf) {
243 panfrost_mfbd_set_zsbuf(&fb, &fbx, ctx->pipe_framebuffer.zsbuf);
244 }
245
246 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
247
248 if (ctx->pipe_framebuffer.nr_cbufs == 0) {
249 struct mali_rt_format null_rt = {
250 .unk1 = 0x4000000,
251 .unk4 = 0x8
252 };
253
254 rts[0].format = null_rt;
255 rts[0].framebuffer = 0;
256 rts[0].framebuffer_stride = 0;
257 }
258
259 /* When scanning out, the depth buffer is immediately invalidated, so
260 * we don't need to waste bandwidth writing it out. This can improve
261 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
262 * memory bandwidth!).
263 *
264 * The exception is ReadPixels, but this is not supported on GLES so we
265 * can safely ignore it. */
266
267 if (panfrost_is_scanout(ctx)) {
268 job->requirements &= ~PAN_REQ_DEPTH_WRITE;
269 }
270
271 /* Actualize the requirements */
272
273 if (job->requirements & PAN_REQ_MSAA) {
274 rts[0].format.flags |= MALI_MFBD_FORMAT_MSAA;
275
276 /* XXX */
277 fb.unk1 |= (1 << 4) | (1 << 1);
278 fb.rt_count_2 = 4;
279 }
280
281 if (job->requirements & PAN_REQ_DEPTH_WRITE)
282 fb.mfbd_flags |= MALI_MFBD_DEPTH_WRITE;
283
284 if (ctx->pipe_framebuffer.nr_cbufs == 1) {
285 struct panfrost_resource *rsrc = (struct panfrost_resource *) ctx->pipe_framebuffer.cbufs[0]->texture;
286
287 if (rsrc->bo->has_checksum) {
288 fb.mfbd_flags |= MALI_MFBD_EXTRA;
289 fbx.flags |= MALI_EXTRA_PRESENT;
290 fbx.checksum_stride = rsrc->bo->checksum_stride;
291 fbx.checksum = rsrc->bo->gpu + rsrc->bo->slices[0].stride * rsrc->base.height0;
292 }
293 }
294
295 /* We always upload at least one (dummy) cbuf */
296 unsigned cbufs = MAX2(ctx->pipe_framebuffer.nr_cbufs, 1);
297
298 return panfrost_mfbd_upload(ctx, &fb, &fbx, rts, cbufs);
299 }