panfrost: Hoist job != NULL check
[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 <assert.h>
27
28 #include "pan_context.h"
29 #include "util/hash_table.h"
30 #include "util/ralloc.h"
31 #include "util/u_format.h"
32 #include "util/u_pack_color.h"
33
34 struct panfrost_job *
35 panfrost_create_job(struct panfrost_context *ctx)
36 {
37 struct panfrost_job *job = rzalloc(ctx, struct panfrost_job);
38
39 job->ctx = ctx;
40
41 job->bos = _mesa_set_create(job,
42 _mesa_hash_pointer,
43 _mesa_key_pointer_equal);
44
45 job->minx = job->miny = ~0;
46 job->maxx = job->maxy = 0;
47 job->transient_offset = 0;
48
49 util_dynarray_init(&job->headers, job);
50 util_dynarray_init(&job->gpu_headers, job);
51 util_dynarray_init(&job->transient_indices, job);
52
53 return job;
54 }
55
56 void
57 panfrost_free_job(struct panfrost_context *ctx, struct panfrost_job *job)
58 {
59 if (!job)
60 return;
61
62 set_foreach(job->bos, entry) {
63 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
64 panfrost_bo_unreference(ctx->base.screen, bo);
65 }
66
67 /* Free up the transient BOs we're sitting on */
68 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
69
70 util_dynarray_foreach(&job->transient_indices, unsigned, index) {
71 /* Mark it free */
72 BITSET_SET(screen->free_transient, *index);
73 }
74
75 /* Unreference the polygon list */
76 panfrost_bo_unreference(ctx->base.screen, job->polygon_list);
77
78 _mesa_hash_table_remove_key(ctx->jobs, &job->key);
79
80 if (ctx->job == job)
81 ctx->job = NULL;
82
83 ralloc_free(job);
84 }
85
86 struct panfrost_job *
87 panfrost_get_job(struct panfrost_context *ctx,
88 struct pipe_surface **cbufs, struct pipe_surface *zsbuf)
89 {
90 /* Lookup the job first */
91
92 struct panfrost_job_key key = {
93 .cbufs = {
94 cbufs[0],
95 cbufs[1],
96 cbufs[2],
97 cbufs[3],
98 },
99 .zsbuf = zsbuf
100 };
101
102 struct hash_entry *entry = _mesa_hash_table_search(ctx->jobs, &key);
103
104 if (entry)
105 return entry->data;
106
107 /* Otherwise, let's create a job */
108
109 struct panfrost_job *job = panfrost_create_job(ctx);
110
111 /* Save the created job */
112
113 memcpy(&job->key, &key, sizeof(key));
114 _mesa_hash_table_insert(ctx->jobs, &job->key, job);
115
116 return job;
117 }
118
119 /* Get the job corresponding to the FBO we're currently rendering into */
120
121 struct panfrost_job *
122 panfrost_get_job_for_fbo(struct panfrost_context *ctx)
123 {
124 /* If we're wallpapering, we special case to workaround
125 * u_blitter abuse */
126
127 if (ctx->wallpaper_batch)
128 return ctx->wallpaper_batch;
129
130 /* If we already began rendering, use that */
131
132 if (ctx->job) {
133 assert(ctx->job->key.zsbuf == ctx->pipe_framebuffer.zsbuf &&
134 !memcmp(ctx->job->key.cbufs,
135 ctx->pipe_framebuffer.cbufs,
136 sizeof(ctx->job->key.cbufs)));
137 return ctx->job;
138 }
139
140 /* If not, look up the job */
141
142 struct pipe_surface **cbufs = ctx->pipe_framebuffer.cbufs;
143 struct pipe_surface *zsbuf = ctx->pipe_framebuffer.zsbuf;
144 struct panfrost_job *job = panfrost_get_job(ctx, cbufs, zsbuf);
145
146 /* Set this job as the current FBO job. Will be reset when updating the
147 * FB state and when submitting or releasing a job.
148 */
149 ctx->job = job;
150 return job;
151 }
152
153 void
154 panfrost_job_add_bo(struct panfrost_job *job, struct panfrost_bo *bo)
155 {
156 if (!bo)
157 return;
158
159 if (_mesa_set_search(job->bos, bo))
160 return;
161
162 panfrost_bo_reference(bo);
163 _mesa_set_add(job->bos, bo);
164 }
165
166 /* Returns the polygon list's GPU address if available, or otherwise allocates
167 * the polygon list. It's perfectly fast to use allocate/free BO directly,
168 * since we'll hit the BO cache and this is one-per-batch anyway. */
169
170 mali_ptr
171 panfrost_job_get_polygon_list(struct panfrost_job *batch, unsigned size)
172 {
173 if (batch->polygon_list) {
174 assert(batch->polygon_list->size >= size);
175 } else {
176 struct panfrost_screen *screen = pan_screen(batch->ctx->base.screen);
177
178 /* Create the BO as invisible, as there's no reason to map */
179
180 batch->polygon_list = panfrost_drm_create_bo(screen,
181 size, PAN_ALLOCATE_INVISIBLE);
182 }
183
184 return batch->polygon_list->gpu;
185 }
186
187 void
188 panfrost_flush_jobs_writing_resource(struct panfrost_context *panfrost,
189 struct pipe_resource *prsc)
190 {
191 #if 0
192 struct hash_entry *entry = _mesa_hash_table_search(panfrost->write_jobs,
193 prsc);
194 if (entry) {
195 struct panfrost_job *job = entry->data;
196 panfrost_job_submit(panfrost, job);
197 }
198 #endif
199 /* TODO stub */
200 }
201
202 void
203 panfrost_job_submit(struct panfrost_context *ctx, struct panfrost_job *job)
204 {
205 int ret;
206
207 assert(job);
208 panfrost_scoreboard_link_batch(job);
209
210 bool has_draws = job->last_job.gpu;
211 bool is_scanout = panfrost_is_scanout(ctx);
212
213 ret = panfrost_drm_submit_vs_fs_job(ctx, has_draws, is_scanout);
214
215 if (ret)
216 fprintf(stderr, "panfrost_job_submit failed: %d\n", ret);
217
218 /* The job has been submitted, let's invalidate the current FBO job
219 * cache.
220 */
221 assert(!ctx->job || job == ctx->job);
222 ctx->job = NULL;
223
224 /* Remove the job from the ctx->jobs set so that future
225 * panfrost_get_job() calls don't see it.
226 * We must reset the job key to avoid removing another valid entry when
227 * the job is freed.
228 */
229 _mesa_hash_table_remove_key(ctx->jobs, &job->key);
230 memset(&job->key, 0, sizeof(job->key));
231 }
232
233 void
234 panfrost_job_set_requirements(struct panfrost_context *ctx,
235 struct panfrost_job *job)
236 {
237 if (ctx->rasterizer && ctx->rasterizer->base.multisample)
238 job->requirements |= PAN_REQ_MSAA;
239
240 if (ctx->depth_stencil && ctx->depth_stencil->depth.writemask)
241 job->requirements |= PAN_REQ_DEPTH_WRITE;
242 }
243
244 /* Helper to smear a 32-bit color across 128-bit components */
245
246 static void
247 pan_pack_color_32(uint32_t *packed, uint32_t v)
248 {
249 for (unsigned i = 0; i < 4; ++i)
250 packed[i] = v;
251 }
252
253 static void
254 pan_pack_color_64(uint32_t *packed, uint32_t lo, uint32_t hi)
255 {
256 for (unsigned i = 0; i < 4; i += 2) {
257 packed[i + 0] = lo;
258 packed[i + 1] = hi;
259 }
260 }
261
262 static void
263 pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
264 {
265 /* Alpha magicked to 1.0 if there is no alpha */
266
267 bool has_alpha = util_format_has_alpha(format);
268 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
269
270 /* Packed color depends on the framebuffer format */
271
272 const struct util_format_description *desc =
273 util_format_description(format);
274
275 if (util_format_is_rgba8_variant(desc)) {
276 pan_pack_color_32(packed,
277 (float_to_ubyte(clear_alpha) << 24) |
278 (float_to_ubyte(color->f[2]) << 16) |
279 (float_to_ubyte(color->f[1]) << 8) |
280 (float_to_ubyte(color->f[0]) << 0));
281 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
282 /* First, we convert the components to R5, G6, B5 separately */
283 unsigned r5 = CLAMP(color->f[0], 0.0, 1.0) * 31.0;
284 unsigned g6 = CLAMP(color->f[1], 0.0, 1.0) * 63.0;
285 unsigned b5 = CLAMP(color->f[2], 0.0, 1.0) * 31.0;
286
287 /* Then we pack into a sparse u32. TODO: Why these shifts? */
288 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
289 } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
290 /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
291 unsigned r4 = CLAMP(color->f[0], 0.0, 1.0) * 240.0;
292 unsigned g4 = CLAMP(color->f[1], 0.0, 1.0) * 240.0;
293 unsigned b4 = CLAMP(color->f[2], 0.0, 1.0) * 240.0;
294 unsigned a4 = CLAMP(clear_alpha, 0.0, 1.0) * 240.0;
295
296 /* Pack on *byte* intervals */
297 pan_pack_color_32(packed, (a4 << 24) | (b4 << 16) | (g4 << 8) | r4);
298 } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
299 /* Scale as expected but shift oddly */
300 unsigned r5 = round(CLAMP(color->f[0], 0.0, 1.0)) * 31.0;
301 unsigned g5 = round(CLAMP(color->f[1], 0.0, 1.0)) * 31.0;
302 unsigned b5 = round(CLAMP(color->f[2], 0.0, 1.0)) * 31.0;
303 unsigned a1 = round(CLAMP(clear_alpha, 0.0, 1.0)) * 1.0;
304
305 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
306 } else {
307 /* Try Gallium's generic default path. Doesn't work for all
308 * formats but it's a good guess. */
309
310 union util_color out;
311
312 if (util_format_is_pure_integer(format)) {
313 memcpy(out.ui, color->ui, 16);
314 } else {
315 util_pack_color(color->f, format, &out);
316 }
317
318 unsigned size = util_format_get_blocksize(format);
319
320 if (size == 1) {
321 unsigned b = out.ui[0];
322 unsigned s = b | (b << 8);
323 pan_pack_color_32(packed, s | (s << 16));
324 } else if (size == 2)
325 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
326 else if (size == 4)
327 pan_pack_color_32(packed, out.ui[0]);
328 else if (size == 8)
329 pan_pack_color_64(packed, out.ui[0], out.ui[1]);
330 else if (size == 16)
331 memcpy(packed, out.ui, 16);
332 else
333 unreachable("Unknown generic format size packing clear colour");
334 }
335 }
336
337 void
338 panfrost_job_clear(struct panfrost_context *ctx,
339 struct panfrost_job *job,
340 unsigned buffers,
341 const union pipe_color_union *color,
342 double depth, unsigned stencil)
343
344 {
345 if (buffers & PIPE_CLEAR_COLOR) {
346 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
347 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
348 continue;
349
350 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
351 pan_pack_color(job->clear_color[i], color, format);
352 }
353 }
354
355 if (buffers & PIPE_CLEAR_DEPTH) {
356 job->clear_depth = depth;
357 }
358
359 if (buffers & PIPE_CLEAR_STENCIL) {
360 job->clear_stencil = stencil;
361 }
362
363 job->clear |= buffers;
364
365 /* Clearing affects the entire framebuffer (by definition -- this is
366 * the Gallium clear callback, which clears the whole framebuffer. If
367 * the scissor test were enabled from the GL side, the state tracker
368 * would emit a quad instead and we wouldn't go down this code path) */
369
370 panfrost_job_union_scissor(job, 0, 0,
371 ctx->pipe_framebuffer.width,
372 ctx->pipe_framebuffer.height);
373 }
374
375 void
376 panfrost_flush_jobs_reading_resource(struct panfrost_context *panfrost,
377 struct pipe_resource *prsc)
378 {
379 struct panfrost_resource *rsc = pan_resource(prsc);
380
381 panfrost_flush_jobs_writing_resource(panfrost, prsc);
382
383 hash_table_foreach(panfrost->jobs, entry) {
384 struct panfrost_job *job = entry->data;
385
386 if (_mesa_set_search(job->bos, rsc->bo)) {
387 printf("TODO: submit job for flush\n");
388 //panfrost_job_submit(panfrost, job);
389 continue;
390 }
391 }
392 }
393
394 static bool
395 panfrost_job_compare(const void *a, const void *b)
396 {
397 return memcmp(a, b, sizeof(struct panfrost_job_key)) == 0;
398 }
399
400 static uint32_t
401 panfrost_job_hash(const void *key)
402 {
403 return _mesa_hash_data(key, sizeof(struct panfrost_job_key));
404 }
405
406 /* Given a new bounding rectangle (scissor), let the job cover the union of the
407 * new and old bounding rectangles */
408
409 void
410 panfrost_job_union_scissor(struct panfrost_job *job,
411 unsigned minx, unsigned miny,
412 unsigned maxx, unsigned maxy)
413 {
414 job->minx = MIN2(job->minx, minx);
415 job->miny = MIN2(job->miny, miny);
416 job->maxx = MAX2(job->maxx, maxx);
417 job->maxy = MAX2(job->maxy, maxy);
418 }
419
420 void
421 panfrost_job_intersection_scissor(struct panfrost_job *job,
422 unsigned minx, unsigned miny,
423 unsigned maxx, unsigned maxy)
424 {
425 job->minx = MAX2(job->minx, minx);
426 job->miny = MAX2(job->miny, miny);
427 job->maxx = MIN2(job->maxx, maxx);
428 job->maxy = MIN2(job->maxy, maxy);
429 }
430
431 void
432 panfrost_job_init(struct panfrost_context *ctx)
433 {
434 ctx->jobs = _mesa_hash_table_create(ctx,
435 panfrost_job_hash,
436 panfrost_job_compare);
437
438 ctx->write_jobs = _mesa_hash_table_create(ctx,
439 _mesa_hash_pointer,
440 _mesa_key_pointer_equal);
441 }