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