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