panfrost: Subdivide fixed-size transient slabs
[mesa.git] / src / gallium / drivers / panfrost / pan_job.c
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #include "pan_context.h"
27 #include "util/hash_table.h"
28 #include "util/ralloc.h"
29 #include "util/u_format.h"
30 #include "util/u_pack_color.h"
31
32 struct panfrost_job *
33 panfrost_create_job(struct panfrost_context *ctx)
34 {
35 struct panfrost_job *job = rzalloc(ctx, struct panfrost_job);
36
37 job->ctx = ctx;
38
39 job->bos = _mesa_set_create(job,
40 _mesa_hash_pointer,
41 _mesa_key_pointer_equal);
42
43 job->minx = job->miny = ~0;
44 job->maxx = job->maxy = 0;
45 job->transient_offset = 0;
46
47 util_dynarray_init(&job->headers, job);
48 util_dynarray_init(&job->gpu_headers, job);
49 util_dynarray_init(&job->transient_indices, job);
50
51 return job;
52 }
53
54 void
55 panfrost_free_job(struct panfrost_context *ctx, struct panfrost_job *job)
56 {
57 if (!job)
58 return;
59
60 set_foreach(job->bos, entry) {
61 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
62 panfrost_bo_unreference(ctx->base.screen, bo);
63 }
64
65 /* Free up the transient BOs we're sitting on */
66 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
67
68 util_dynarray_foreach(&job->transient_indices, unsigned, index) {
69 /* Mark it free */
70 BITSET_SET(screen->free_transient, *index);
71 }
72
73 _mesa_hash_table_remove_key(ctx->jobs, &job->key);
74
75 if (ctx->job == job)
76 ctx->job = NULL;
77
78 ralloc_free(job);
79 }
80
81 struct panfrost_job *
82 panfrost_get_job(struct panfrost_context *ctx,
83 struct pipe_surface **cbufs, struct pipe_surface *zsbuf)
84 {
85 /* Lookup the job first */
86
87 struct panfrost_job_key key = {
88 .cbufs = {
89 cbufs[0],
90 cbufs[1],
91 cbufs[2],
92 cbufs[3],
93 },
94 .zsbuf = zsbuf
95 };
96
97 struct hash_entry *entry = _mesa_hash_table_search(ctx->jobs, &key);
98
99 if (entry)
100 return entry->data;
101
102 /* Otherwise, let's create a job */
103
104 struct panfrost_job *job = panfrost_create_job(ctx);
105
106 /* Save the created job */
107
108 memcpy(&job->key, &key, sizeof(key));
109 _mesa_hash_table_insert(ctx->jobs, &job->key, job);
110
111 return job;
112 }
113
114 /* Get the job corresponding to the FBO we're currently rendering into */
115
116 struct panfrost_job *
117 panfrost_get_job_for_fbo(struct panfrost_context *ctx)
118 {
119 /* If we're wallpapering, we special case to workaround
120 * u_blitter abuse */
121
122 if (ctx->wallpaper_batch)
123 return ctx->wallpaper_batch;
124
125 /* If we already began rendering, use that */
126
127 if (ctx->job)
128 return ctx->job;
129
130 /* If not, look up the job */
131
132 struct pipe_surface **cbufs = ctx->pipe_framebuffer.cbufs;
133 struct pipe_surface *zsbuf = ctx->pipe_framebuffer.zsbuf;
134 struct panfrost_job *job = panfrost_get_job(ctx, cbufs, zsbuf);
135
136 return job;
137 }
138
139 void
140 panfrost_job_add_bo(struct panfrost_job *job, struct panfrost_bo *bo)
141 {
142 if (!bo)
143 return;
144
145 if (_mesa_set_search(job->bos, bo))
146 return;
147
148 panfrost_bo_reference(bo);
149 _mesa_set_add(job->bos, bo);
150 }
151
152 void
153 panfrost_flush_jobs_writing_resource(struct panfrost_context *panfrost,
154 struct pipe_resource *prsc)
155 {
156 #if 0
157 struct hash_entry *entry = _mesa_hash_table_search(panfrost->write_jobs,
158 prsc);
159 if (entry) {
160 struct panfrost_job *job = entry->data;
161 panfrost_job_submit(panfrost, job);
162 }
163 #endif
164 /* TODO stub */
165 }
166
167 void
168 panfrost_job_submit(struct panfrost_context *ctx, struct panfrost_job *job)
169 {
170 int ret;
171
172 panfrost_scoreboard_link_batch(job);
173
174 bool has_draws = job->last_job.gpu;
175 bool is_scanout = panfrost_is_scanout(ctx);
176
177 if (!job)
178 return;
179
180 ret = panfrost_drm_submit_vs_fs_job(ctx, has_draws, is_scanout);
181
182 if (ret)
183 fprintf(stderr, "panfrost_job_submit failed: %d\n", ret);
184 }
185
186 void
187 panfrost_job_set_requirements(struct panfrost_context *ctx,
188 struct panfrost_job *job)
189 {
190 if (ctx->rasterizer && ctx->rasterizer->base.multisample)
191 job->requirements |= PAN_REQ_MSAA;
192
193 if (ctx->depth_stencil && ctx->depth_stencil->depth.writemask)
194 job->requirements |= PAN_REQ_DEPTH_WRITE;
195 }
196
197 /* Helper to smear a 32-bit color across 128-bit components */
198
199 static void
200 pan_pack_color_32(uint32_t *packed, uint32_t v)
201 {
202 for (unsigned i = 0; i < 4; ++i)
203 packed[i] = v;
204 }
205
206 static void
207 pan_pack_color_64(uint32_t *packed, uint32_t lo, uint32_t hi)
208 {
209 for (unsigned i = 0; i < 4; i += 2) {
210 packed[i + 0] = lo;
211 packed[i + 1] = hi;
212 }
213 }
214
215 static void
216 pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
217 {
218 /* Alpha magicked to 1.0 if there is no alpha */
219
220 bool has_alpha = util_format_has_alpha(format);
221 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
222
223 /* Packed color depends on the framebuffer format */
224
225 const struct util_format_description *desc =
226 util_format_description(format);
227
228 if (util_format_is_rgba8_variant(desc)) {
229 pan_pack_color_32(packed,
230 (float_to_ubyte(clear_alpha) << 24) |
231 (float_to_ubyte(color->f[2]) << 16) |
232 (float_to_ubyte(color->f[1]) << 8) |
233 (float_to_ubyte(color->f[0]) << 0));
234 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
235 /* First, we convert the components to R5, G6, B5 separately */
236 unsigned r5 = CLAMP(color->f[0], 0.0, 1.0) * 31.0;
237 unsigned g6 = CLAMP(color->f[1], 0.0, 1.0) * 63.0;
238 unsigned b5 = CLAMP(color->f[2], 0.0, 1.0) * 31.0;
239
240 /* Then we pack into a sparse u32. TODO: Why these shifts? */
241 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
242 } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
243 /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
244 unsigned r4 = CLAMP(color->f[0], 0.0, 1.0) * 240.0;
245 unsigned g4 = CLAMP(color->f[1], 0.0, 1.0) * 240.0;
246 unsigned b4 = CLAMP(color->f[2], 0.0, 1.0) * 240.0;
247 unsigned a4 = CLAMP(clear_alpha, 0.0, 1.0) * 240.0;
248
249 /* Pack on *byte* intervals */
250 pan_pack_color_32(packed, (a4 << 24) | (b4 << 16) | (g4 << 8) | r4);
251 } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
252 /* Scale as expected but shift oddly */
253 unsigned r5 = round(CLAMP(color->f[0], 0.0, 1.0)) * 31.0;
254 unsigned g5 = round(CLAMP(color->f[1], 0.0, 1.0)) * 31.0;
255 unsigned b5 = round(CLAMP(color->f[2], 0.0, 1.0)) * 31.0;
256 unsigned a1 = round(CLAMP(clear_alpha, 0.0, 1.0)) * 1.0;
257
258 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
259 } else {
260 /* Try Gallium's generic default path. Doesn't work for all
261 * formats but it's a good guess. */
262
263 union util_color out;
264
265 if (util_format_is_pure_integer(format)) {
266 memcpy(out.ui, color->ui, 16);
267 } else {
268 util_pack_color(color->f, format, &out);
269 }
270
271 unsigned size = util_format_get_blocksize(format);
272
273 if (size == 1) {
274 unsigned b = out.ui[0];
275 unsigned s = b | (b << 8);
276 pan_pack_color_32(packed, s | (s << 16));
277 } else if (size == 2)
278 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
279 else if (size == 4)
280 pan_pack_color_32(packed, out.ui[0]);
281 else if (size == 8)
282 pan_pack_color_64(packed, out.ui[0], out.ui[1]);
283 else if (size == 16)
284 memcpy(packed, out.ui, 16);
285 else
286 unreachable("Unknown generic format size packing clear colour");
287 }
288 }
289
290 void
291 panfrost_job_clear(struct panfrost_context *ctx,
292 struct panfrost_job *job,
293 unsigned buffers,
294 const union pipe_color_union *color,
295 double depth, unsigned stencil)
296
297 {
298 if (buffers & PIPE_CLEAR_COLOR) {
299 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
300 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
301 continue;
302
303 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
304 pan_pack_color(job->clear_color[i], color, format);
305 }
306 }
307
308 if (buffers & PIPE_CLEAR_DEPTH) {
309 job->clear_depth = depth;
310 }
311
312 if (buffers & PIPE_CLEAR_STENCIL) {
313 job->clear_stencil = stencil;
314 }
315
316 job->clear |= buffers;
317
318 /* Clearing affects the entire framebuffer (by definition -- this is
319 * the Gallium clear callback, which clears the whole framebuffer. If
320 * the scissor test were enabled from the GL side, the state tracker
321 * would emit a quad instead and we wouldn't go down this code path) */
322
323 panfrost_job_union_scissor(job, 0, 0,
324 ctx->pipe_framebuffer.width,
325 ctx->pipe_framebuffer.height);
326 }
327
328 void
329 panfrost_flush_jobs_reading_resource(struct panfrost_context *panfrost,
330 struct pipe_resource *prsc)
331 {
332 struct panfrost_resource *rsc = pan_resource(prsc);
333
334 panfrost_flush_jobs_writing_resource(panfrost, prsc);
335
336 hash_table_foreach(panfrost->jobs, entry) {
337 struct panfrost_job *job = entry->data;
338
339 if (_mesa_set_search(job->bos, rsc->bo)) {
340 printf("TODO: submit job for flush\n");
341 //panfrost_job_submit(panfrost, job);
342 continue;
343 }
344 }
345 }
346
347 static bool
348 panfrost_job_compare(const void *a, const void *b)
349 {
350 return memcmp(a, b, sizeof(struct panfrost_job_key)) == 0;
351 }
352
353 static uint32_t
354 panfrost_job_hash(const void *key)
355 {
356 return _mesa_hash_data(key, sizeof(struct panfrost_job_key));
357 }
358
359 /* Given a new bounding rectangle (scissor), let the job cover the union of the
360 * new and old bounding rectangles */
361
362 void
363 panfrost_job_union_scissor(struct panfrost_job *job,
364 unsigned minx, unsigned miny,
365 unsigned maxx, unsigned maxy)
366 {
367 job->minx = MIN2(job->minx, minx);
368 job->miny = MIN2(job->miny, miny);
369 job->maxx = MAX2(job->maxx, maxx);
370 job->maxy = MAX2(job->maxy, maxy);
371 }
372
373 void
374 panfrost_job_init(struct panfrost_context *ctx)
375 {
376 ctx->jobs = _mesa_hash_table_create(ctx,
377 panfrost_job_hash,
378 panfrost_job_compare);
379
380 ctx->write_jobs = _mesa_hash_table_create(ctx,
381 _mesa_hash_pointer,
382 _mesa_key_pointer_equal);
383 }