blob: bfa73a7f334f0996e638184d3ee9e83e759b629d [file] [log] [blame]
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +00001/*
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
Boris Brezillon20b00e12019-08-02 19:18:40 +020026#include <assert.h>
27
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +000028#include "pan_context.h"
29#include "util/hash_table.h"
30#include "util/ralloc.h"
Rohan Gargad284f72019-06-05 19:04:04 +020031#include "util/u_format.h"
Alyssa Rosenzweig7692ad12019-06-28 18:46:43 -070032#include "util/u_pack_color.h"
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +000033
34struct panfrost_job *
35panfrost_create_job(struct panfrost_context *ctx)
36{
Tomeu Vizoso4f881232019-06-19 13:16:45 +020037 struct panfrost_job *job = rzalloc(ctx, struct panfrost_job);
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +000038
39 job->ctx = ctx;
40
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +000041 job->bos = _mesa_set_create(job,
42 _mesa_hash_pointer,
43 _mesa_key_pointer_equal);
Alyssa Rosenzweigc3788292019-06-18 12:30:55 -070044
45 job->minx = job->miny = ~0;
46 job->maxx = job->maxy = 0;
Alyssa Rosenzweigee327002019-07-12 13:59:35 -070047 job->transient_offset = 0;
Alyssa Rosenzweigf0854742019-06-19 11:27:59 -070048
49 util_dynarray_init(&job->headers, job);
50 util_dynarray_init(&job->gpu_headers, job);
Alyssa Rosenzweig0f5ad9e2019-07-12 12:53:36 -070051 util_dynarray_init(&job->transient_indices, job);
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -070052
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +000053 return job;
54}
55
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +000056void
57panfrost_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
Alyssa Rosenzweig37097b22019-07-12 13:05:14 -070067 /* 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
Alyssa Rosenzweigcd98d942019-08-02 19:18:48 +020075 /* Unreference the polygon list */
76 panfrost_bo_unreference(ctx->base.screen, job->polygon_list);
77
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +000078 _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
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +000086struct panfrost_job *
87panfrost_get_job(struct panfrost_context *ctx,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -070088 struct pipe_surface **cbufs, struct pipe_surface *zsbuf)
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +000089{
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 };
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700101
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000102 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
121struct panfrost_job *
122panfrost_get_job_for_fbo(struct panfrost_context *ctx)
123{
Alyssa Rosenzweigf0854742019-06-19 11:27:59 -0700124 /* 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
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000130 /* If we already began rendering, use that */
131
Boris Brezillon20b00e12019-08-02 19:18:40 +0200132 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)));
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000137 return ctx->job;
Boris Brezillon20b00e12019-08-02 19:18:40 +0200138 }
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000139
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
Boris Brezillon20b00e12019-08-02 19:18:40 +0200146 /* 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;
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000150 return job;
151}
152
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +0000153void
154panfrost_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
Alyssa Rosenzweigcd98d942019-08-02 19:18:48 +0200166/* 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
170mali_ptr
171panfrost_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
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +0000187void
188panfrost_flush_jobs_writing_resource(struct panfrost_context *panfrost,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700189 struct pipe_resource *prsc)
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +0000190{
191#if 0
192 struct hash_entry *entry = _mesa_hash_table_search(panfrost->write_jobs,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700193 prsc);
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +0000194 if (entry) {
195 struct panfrost_job *job = entry->data;
196 panfrost_job_submit(panfrost, job);
197 }
198#endif
199 /* TODO stub */
200}
201
202void
Rohan Garg0f43a2a2019-06-05 16:20:59 +0200203panfrost_job_submit(struct panfrost_context *ctx, struct panfrost_job *job)
204{
Rohan Garg0f43a2a2019-06-05 16:20:59 +0200205 int ret;
206
Alyssa Rosenzweigf0854742019-06-19 11:27:59 -0700207 panfrost_scoreboard_link_batch(job);
208
209 bool has_draws = job->last_job.gpu;
Rohan Garg0f43a2a2019-06-05 16:20:59 +0200210 bool is_scanout = panfrost_is_scanout(ctx);
211
212 if (!job)
213 return;
214
Boris Brezillon5f816692019-06-19 16:06:38 +0200215 ret = panfrost_drm_submit_vs_fs_job(ctx, has_draws, is_scanout);
Rohan Garg0f43a2a2019-06-05 16:20:59 +0200216
217 if (ret)
218 fprintf(stderr, "panfrost_job_submit failed: %d\n", ret);
Boris Brezillon20b00e12019-08-02 19:18:40 +0200219
220 /* The job has been submitted, let's invalidate the current FBO job
221 * cache.
222 */
223 assert(!ctx->job || job == ctx->job);
224 ctx->job = NULL;
Boris Brezillonb5ca1e52019-08-02 19:18:41 +0200225
226 /* Remove the job from the ctx->jobs set so that future
227 * panfrost_get_job() calls don't see it.
228 * We must reset the job key to avoid removing another valid entry when
229 * the job is freed.
230 */
231 _mesa_hash_table_remove_key(ctx->jobs, &job->key);
232 memset(&job->key, 0, sizeof(job->key));
Rohan Garg0f43a2a2019-06-05 16:20:59 +0200233}
234
235void
Rohan Gargbfca21b2019-06-05 17:49:14 +0200236panfrost_job_set_requirements(struct panfrost_context *ctx,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700237 struct panfrost_job *job)
Rohan Gargbfca21b2019-06-05 17:49:14 +0200238{
239 if (ctx->rasterizer && ctx->rasterizer->base.multisample)
240 job->requirements |= PAN_REQ_MSAA;
241
242 if (ctx->depth_stencil && ctx->depth_stencil->depth.writemask)
243 job->requirements |= PAN_REQ_DEPTH_WRITE;
244}
245
Alyssa Rosenzweig8e4e4672019-07-01 11:49:06 -0700246/* Helper to smear a 32-bit color across 128-bit components */
247
248static void
249pan_pack_color_32(uint32_t *packed, uint32_t v)
250{
251 for (unsigned i = 0; i < 4; ++i)
252 packed[i] = v;
253}
254
255static void
256pan_pack_color_64(uint32_t *packed, uint32_t lo, uint32_t hi)
257{
258 for (unsigned i = 0; i < 4; i += 2) {
259 packed[i + 0] = lo;
260 packed[i + 1] = hi;
261 }
262}
263
264static void
265pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
Rohan Gargad284f72019-06-05 19:04:04 +0200266{
267 /* Alpha magicked to 1.0 if there is no alpha */
268
269 bool has_alpha = util_format_has_alpha(format);
270 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
271
272 /* Packed color depends on the framebuffer format */
273
274 const struct util_format_description *desc =
275 util_format_description(format);
276
277 if (util_format_is_rgba8_variant(desc)) {
Alyssa Rosenzweig8e4e4672019-07-01 11:49:06 -0700278 pan_pack_color_32(packed,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700279 (float_to_ubyte(clear_alpha) << 24) |
280 (float_to_ubyte(color->f[2]) << 16) |
281 (float_to_ubyte(color->f[1]) << 8) |
282 (float_to_ubyte(color->f[0]) << 0));
Rohan Gargad284f72019-06-05 19:04:04 +0200283 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
284 /* First, we convert the components to R5, G6, B5 separately */
285 unsigned r5 = CLAMP(color->f[0], 0.0, 1.0) * 31.0;
286 unsigned g6 = CLAMP(color->f[1], 0.0, 1.0) * 63.0;
287 unsigned b5 = CLAMP(color->f[2], 0.0, 1.0) * 31.0;
288
289 /* Then we pack into a sparse u32. TODO: Why these shifts? */
Alyssa Rosenzweig8e4e4672019-07-01 11:49:06 -0700290 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
291 } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
292 /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
293 unsigned r4 = CLAMP(color->f[0], 0.0, 1.0) * 240.0;
294 unsigned g4 = CLAMP(color->f[1], 0.0, 1.0) * 240.0;
295 unsigned b4 = CLAMP(color->f[2], 0.0, 1.0) * 240.0;
296 unsigned a4 = CLAMP(clear_alpha, 0.0, 1.0) * 240.0;
297
298 /* Pack on *byte* intervals */
299 pan_pack_color_32(packed, (a4 << 24) | (b4 << 16) | (g4 << 8) | r4);
300 } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
301 /* Scale as expected but shift oddly */
302 unsigned r5 = round(CLAMP(color->f[0], 0.0, 1.0)) * 31.0;
303 unsigned g5 = round(CLAMP(color->f[1], 0.0, 1.0)) * 31.0;
304 unsigned b5 = round(CLAMP(color->f[2], 0.0, 1.0)) * 31.0;
305 unsigned a1 = round(CLAMP(clear_alpha, 0.0, 1.0)) * 1.0;
306
307 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
Rohan Gargad284f72019-06-05 19:04:04 +0200308 } else {
Alyssa Rosenzweig7692ad12019-06-28 18:46:43 -0700309 /* Try Gallium's generic default path. Doesn't work for all
310 * formats but it's a good guess. */
311
312 union util_color out;
Rohan Gargad284f72019-06-05 19:04:04 +0200313
Alyssa Rosenzweig8e4e4672019-07-01 11:49:06 -0700314 if (util_format_is_pure_integer(format)) {
315 memcpy(out.ui, color->ui, 16);
316 } else {
317 util_pack_color(color->f, format, &out);
318 }
319
320 unsigned size = util_format_get_blocksize(format);
321
322 if (size == 1) {
323 unsigned b = out.ui[0];
324 unsigned s = b | (b << 8);
325 pan_pack_color_32(packed, s | (s << 16));
326 } else if (size == 2)
327 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
328 else if (size == 4)
329 pan_pack_color_32(packed, out.ui[0]);
330 else if (size == 8)
331 pan_pack_color_64(packed, out.ui[0], out.ui[1]);
332 else if (size == 16)
333 memcpy(packed, out.ui, 16);
334 else
335 unreachable("Unknown generic format size packing clear colour");
336 }
Rohan Gargad284f72019-06-05 19:04:04 +0200337}
338
339void
340panfrost_job_clear(struct panfrost_context *ctx,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700341 struct panfrost_job *job,
342 unsigned buffers,
343 const union pipe_color_union *color,
344 double depth, unsigned stencil)
Rohan Gargad284f72019-06-05 19:04:04 +0200345
346{
347 if (buffers & PIPE_CLEAR_COLOR) {
Alyssa Rosenzweig8e4e4672019-07-01 11:49:06 -0700348 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
349 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
350 continue;
351
352 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
353 pan_pack_color(job->clear_color[i], color, format);
354 }
Rohan Gargad284f72019-06-05 19:04:04 +0200355 }
356
357 if (buffers & PIPE_CLEAR_DEPTH) {
358 job->clear_depth = depth;
359 }
360
361 if (buffers & PIPE_CLEAR_STENCIL) {
362 job->clear_stencil = stencil;
363 }
364
365 job->clear |= buffers;
Alyssa Rosenzweigc3788292019-06-18 12:30:55 -0700366
367 /* Clearing affects the entire framebuffer (by definition -- this is
368 * the Gallium clear callback, which clears the whole framebuffer. If
369 * the scissor test were enabled from the GL side, the state tracker
370 * would emit a quad instead and we wouldn't go down this code path) */
371
372 panfrost_job_union_scissor(job, 0, 0,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700373 ctx->pipe_framebuffer.width,
374 ctx->pipe_framebuffer.height);
Rohan Gargad284f72019-06-05 19:04:04 +0200375}
376
Rohan Gargbfca21b2019-06-05 17:49:14 +0200377void
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +0000378panfrost_flush_jobs_reading_resource(struct panfrost_context *panfrost,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700379 struct pipe_resource *prsc)
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +0000380{
381 struct panfrost_resource *rsc = pan_resource(prsc);
382
383 panfrost_flush_jobs_writing_resource(panfrost, prsc);
384
385 hash_table_foreach(panfrost->jobs, entry) {
386 struct panfrost_job *job = entry->data;
387
388 if (_mesa_set_search(job->bos, rsc->bo)) {
389 printf("TODO: submit job for flush\n");
390 //panfrost_job_submit(panfrost, job);
391 continue;
392 }
393 }
394}
395
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000396static bool
397panfrost_job_compare(const void *a, const void *b)
398{
399 return memcmp(a, b, sizeof(struct panfrost_job_key)) == 0;
400}
401
402static uint32_t
403panfrost_job_hash(const void *key)
404{
405 return _mesa_hash_data(key, sizeof(struct panfrost_job_key));
406}
407
Alyssa Rosenzweigc3788292019-06-18 12:30:55 -0700408/* Given a new bounding rectangle (scissor), let the job cover the union of the
409 * new and old bounding rectangles */
410
411void
412panfrost_job_union_scissor(struct panfrost_job *job,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700413 unsigned minx, unsigned miny,
414 unsigned maxx, unsigned maxy)
Alyssa Rosenzweigc3788292019-06-18 12:30:55 -0700415{
416 job->minx = MIN2(job->minx, minx);
417 job->miny = MIN2(job->miny, miny);
418 job->maxx = MAX2(job->maxx, maxx);
419 job->maxy = MAX2(job->maxy, maxy);
420}
421
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000422void
Boris Brezillon65ae86b2019-08-12 12:07:08 +0200423panfrost_job_intersection_scissor(struct panfrost_job *job,
424 unsigned minx, unsigned miny,
425 unsigned maxx, unsigned maxy)
426{
427 job->minx = MAX2(job->minx, minx);
428 job->miny = MAX2(job->miny, miny);
429 job->maxx = MIN2(job->maxx, maxx);
430 job->maxy = MIN2(job->maxy, maxy);
431}
432
433void
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000434panfrost_job_init(struct panfrost_context *ctx)
435{
Tomeu Vizoso0fcf73b2019-06-18 14:24:57 +0200436 ctx->jobs = _mesa_hash_table_create(ctx,
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000437 panfrost_job_hash,
438 panfrost_job_compare);
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +0000439
Tomeu Vizoso0fcf73b2019-06-18 14:24:57 +0200440 ctx->write_jobs = _mesa_hash_table_create(ctx,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700441 _mesa_hash_pointer,
442 _mesa_key_pointer_equal);
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000443}