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