1 /*
2 * Copyright © 2012 Rob Clark <[email protected]>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <[email protected]>
7 */
8
9 #include "freedreno_context.h"
10 #include "ir3/ir3_cache.h"
11 #include "util/u_upload_mgr.h"
12 #include "freedreno_blitter.h"
13 #include "freedreno_draw.h"
14 #include "freedreno_fence.h"
15 #include "freedreno_gmem.h"
16 #include "freedreno_program.h"
17 #include "freedreno_query.h"
18 #include "freedreno_query_hw.h"
19 #include "freedreno_resource.h"
20 #include "freedreno_state.h"
21 #include "freedreno_texture.h"
22 #include "freedreno_util.h"
23 #include "freedreno_tracepoints.h"
24 #include "util/u_trace_gallium.h"
25
26 static void
fd_context_flush(struct pipe_context * pctx,struct pipe_fence_handle ** fencep,unsigned flags)27 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fencep,
28 unsigned flags) in_dt
29 {
30 struct fd_context *ctx = fd_context(pctx);
31 struct pipe_fence_handle *fence = NULL;
32 struct fd_batch *batch = fd_bc_last_batch(ctx);
33
34 DBG("%p: %p: flush: flags=%x, fencep=%p", ctx, batch, flags, fencep);
35
36 if (fencep && !batch) {
37 batch = fd_context_batch(ctx);
38 } else if (!batch) {
39 return;
40 }
41
42 /* With TC_FLUSH_ASYNC, the fence will have been pre-created from
43 * the front-end thread. But not yet associated with a batch,
44 * because we cannot safely access ctx->batch outside of the driver
45 * thread. So instead, replace the existing batch->fence with the
46 * one created earlier
47 */
48 if ((flags & TC_FLUSH_ASYNC) && fencep) {
49 /* We don't currently expect async+flush in the fence-fd
50 * case.. for that to work properly we'd need TC to tell
51 * us in the create_fence callback that it needs an fd.
52 */
53 assert(!(flags & PIPE_FLUSH_FENCE_FD));
54
55 fd_pipe_fence_set_batch(*fencep, batch);
56 fd_pipe_fence_ref(&batch->fence, *fencep);
57
58 /* If we have nothing to flush, update the pre-created unflushed
59 * fence with the current state of the last-fence:
60 */
61 if (ctx->last_fence) {
62 fd_pipe_fence_repopulate(*fencep, ctx->last_fence);
63 fd_pipe_fence_ref(&fence, *fencep);
64 fd_bc_dump(ctx, "%p: (deferred) reuse last_fence, remaining:\n", ctx);
65 goto out;
66 }
67
68 /* async flush is not compatible with deferred flush, since
69 * nothing triggers the batch flush which fence_flush() would
70 * be waiting for
71 */
72 flags &= ~PIPE_FLUSH_DEFERRED;
73 } else if (!batch->fence) {
74 batch->fence = fd_pipe_fence_create(batch);
75 }
76
77 /* In some sequence of events, we can end up with a last_fence that is
78 * not an "fd" fence, which results in eglDupNativeFenceFDANDROID()
79 * errors.
80 */
81 if ((flags & PIPE_FLUSH_FENCE_FD) && ctx->last_fence &&
82 !fd_pipe_fence_is_fd(ctx->last_fence))
83 fd_pipe_fence_ref(&ctx->last_fence, NULL);
84
85 /* if no rendering since last flush, ie. app just decided it needed
86 * a fence, re-use the last one:
87 */
88 if (ctx->last_fence) {
89 fd_pipe_fence_ref(&fence, ctx->last_fence);
90 fd_bc_dump(ctx, "%p: reuse last_fence, remaining:\n", ctx);
91 goto out;
92 }
93
94 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
95 fd_pipe_fence_ref(&fence, batch->fence);
96
97 if (flags & PIPE_FLUSH_FENCE_FD)
98 fence->use_fence_fd = true;
99
100 fd_bc_dump(ctx, "%p: flushing %p<%u>, flags=0x%x, pending:\n", ctx,
101 batch, batch->seqno, flags);
102
103 /* If we get here, we need to flush for a fence, even if there is
104 * no rendering yet:
105 */
106 batch->needs_flush = true;
107
108 if (!ctx->screen->reorder) {
109 fd_batch_flush(batch);
110 } else {
111 fd_bc_add_flush_deps(ctx, batch);
112 if (!(flags & PIPE_FLUSH_DEFERRED))
113 fd_batch_flush(batch);
114 }
115
116 fd_bc_dump(ctx, "%p: remaining:\n", ctx);
117
118 out:
119 if (fencep)
120 fd_pipe_fence_ref(fencep, fence);
121
122 fd_pipe_fence_ref(&ctx->last_fence, fence);
123
124 fd_pipe_fence_ref(&fence, NULL);
125
126 fd_batch_reference(&batch, NULL);
127
128 u_trace_context_process(&ctx->trace_context,
129 !!(flags & PIPE_FLUSH_END_OF_FRAME));
130 }
131
132 static void
fd_texture_barrier(struct pipe_context * pctx,unsigned flags)133 fd_texture_barrier(struct pipe_context *pctx, unsigned flags) in_dt
134 {
135 /* On devices that could sample from GMEM we could possibly do better.
136 * Or if we knew that we were doing GMEM bypass we could just emit a
137 * cache flush, perhaps? But we don't know if future draws would cause
138 * us to use GMEM, and a flush in bypass isn't the end of the world.
139 */
140 fd_context_flush(pctx, NULL, 0);
141 }
142
143 static void
fd_memory_barrier(struct pipe_context * pctx,unsigned flags)144 fd_memory_barrier(struct pipe_context *pctx, unsigned flags)
145 {
146 if (!(flags & ~PIPE_BARRIER_UPDATE))
147 return;
148
149 fd_context_flush(pctx, NULL, 0);
150 }
151
152 static void
emit_string_tail(struct fd_ringbuffer * ring,const char * string,int len)153 emit_string_tail(struct fd_ringbuffer *ring, const char *string, int len)
154 {
155 const uint32_t *buf = (const void *)string;
156
157 while (len >= 4) {
158 OUT_RING(ring, *buf);
159 buf++;
160 len -= 4;
161 }
162
163 /* copy remainder bytes without reading past end of input string: */
164 if (len > 0) {
165 uint32_t w = 0;
166 memcpy(&w, buf, len);
167 OUT_RING(ring, w);
168 }
169 }
170
171 /* for prior to a5xx: */
172 void
fd_emit_string(struct fd_ringbuffer * ring,const char * string,int len)173 fd_emit_string(struct fd_ringbuffer *ring, const char *string, int len)
174 {
175 /* max packet size is 0x3fff+1 dwords: */
176 len = MIN2(len, 0x4000 * 4);
177
178 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
179 emit_string_tail(ring, string, len);
180 }
181
182 /* for a5xx+ */
183 void
fd_emit_string5(struct fd_ringbuffer * ring,const char * string,int len)184 fd_emit_string5(struct fd_ringbuffer *ring, const char *string, int len)
185 {
186 /* max packet size is 0x3fff dwords: */
187 len = MIN2(len, 0x3fff * 4);
188
189 OUT_PKT7(ring, CP_NOP, align(len, 4) / 4);
190 emit_string_tail(ring, string, len);
191 }
192
193 /**
194 * emit marker string as payload of a no-op packet, which can be
195 * decoded by cffdump.
196 */
197 static void
fd_emit_string_marker(struct pipe_context * pctx,const char * string,int len)198 fd_emit_string_marker(struct pipe_context *pctx, const char *string,
199 int len) in_dt
200 {
201 struct fd_context *ctx = fd_context(pctx);
202
203 DBG("%.*s", len, string);
204
205 if (!ctx->batch)
206 return;
207
208 struct fd_batch *batch = fd_context_batch(ctx);
209
210 fd_batch_needs_flush(batch);
211
212 if (ctx->screen->gen >= 5) {
213 fd_emit_string5(batch->draw, string, len);
214 } else {
215 fd_emit_string(batch->draw, string, len);
216 }
217
218 fd_batch_reference(&batch, NULL);
219 }
220
221 static void
fd_cs_magic_write_string(void * cs,struct u_trace_context * utctx,int magic,const char * fmt,va_list args)222 fd_cs_magic_write_string(void *cs, struct u_trace_context *utctx, int magic,
223 const char *fmt, va_list args)
224 {
225 struct fd_context *ctx =
226 container_of(utctx, struct fd_context, trace_context);
227 int fmt_len = vsnprintf(NULL, 0, fmt, args);
228 int len = 4 + fmt_len + 1;
229 char *string = (char *)malloc(len);
230
231 /* format: <magic><formatted string>\0 */
232 *(uint32_t *)string = magic;
233 vsnprintf(string + 4, fmt_len + 1, fmt, args);
234
235 if (ctx->screen->gen >= 5) {
236 fd_emit_string5((struct fd_ringbuffer *)cs, string, len);
237 } else {
238 fd_emit_string((struct fd_ringbuffer *)cs, string, len);
239 }
240 free(string);
241 }
242
243 void
fd_cs_trace_msg(struct u_trace_context * utctx,void * cs,const char * fmt,...)244 fd_cs_trace_msg(struct u_trace_context *utctx, void *cs, const char *fmt, ...)
245 {
246 va_list args;
247 va_start(args, fmt);
248 int magic = CP_NOP_MESG;
249 fd_cs_magic_write_string(cs, utctx, magic, fmt, args);
250 va_end(args);
251 }
252
253 void
fd_cs_trace_start(struct u_trace_context * utctx,void * cs,const char * fmt,...)254 fd_cs_trace_start(struct u_trace_context *utctx, void *cs, const char *fmt, ...)
255 {
256 va_list args;
257 va_start(args, fmt);
258 int magic = CP_NOP_BEGN;
259 fd_cs_magic_write_string(cs, utctx, magic, fmt, args);
260 va_end(args);
261 }
262
263 void
fd_cs_trace_end(struct u_trace_context * utctx,void * cs,const char * fmt,...)264 fd_cs_trace_end(struct u_trace_context *utctx, void *cs, const char *fmt, ...)
265 {
266 va_list args;
267 va_start(args, fmt);
268 int magic = CP_NOP_END;
269 fd_cs_magic_write_string(cs, utctx, magic, fmt, args);
270 va_end(args);
271 }
272
273 /**
274 * If we have a pending fence_server_sync() (GPU side sync), flush now.
275 * The alternative to try to track this with batch dependencies gets
276 * hairy quickly.
277 *
278 * Call this before switching to a different batch, to handle this case.
279 */
280 void
fd_context_switch_from(struct fd_context * ctx)281 fd_context_switch_from(struct fd_context *ctx)
282 {
283 if (ctx->batch && (ctx->batch->in_fence_fd != -1))
284 fd_batch_flush(ctx->batch);
285 }
286
287 /**
288 * If there is a pending fence-fd that we need to sync on, this will
289 * transfer the reference to the next batch we are going to render
290 * to.
291 */
292 void
fd_context_switch_to(struct fd_context * ctx,struct fd_batch * batch)293 fd_context_switch_to(struct fd_context *ctx, struct fd_batch *batch)
294 {
295 if (ctx->in_fence_fd != -1) {
296 sync_accumulate("freedreno", &batch->in_fence_fd, ctx->in_fence_fd);
297 close(ctx->in_fence_fd);
298 ctx->in_fence_fd = -1;
299 }
300 }
301
302 void
fd_context_add_private_bo(struct fd_context * ctx,struct fd_bo * bo)303 fd_context_add_private_bo(struct fd_context *ctx, struct fd_bo *bo)
304 {
305 assert(ctx->num_private_bos < ARRAY_SIZE(ctx->private_bos));
306 ctx->private_bos[ctx->num_private_bos++] = bo;
307 }
308
309 /**
310 * Return a reference to the current batch, caller must unref.
311 */
312 struct fd_batch *
fd_context_batch(struct fd_context * ctx)313 fd_context_batch(struct fd_context *ctx)
314 {
315 struct fd_batch *batch = NULL;
316
317 tc_assert_driver_thread(ctx->tc);
318
319 if (ctx->batch_nondraw) {
320 fd_batch_reference(&ctx->batch_nondraw, NULL);
321 fd_context_all_dirty(ctx);
322 }
323
324 fd_batch_reference(&batch, ctx->batch);
325
326 if (unlikely(!batch)) {
327 batch =
328 fd_batch_from_fb(ctx, &ctx->framebuffer);
329 fd_batch_reference(&ctx->batch, batch);
330 fd_context_all_dirty(ctx);
331 }
332 fd_context_switch_to(ctx, batch);
333
334 return batch;
335 }
336
337 /**
338 * Return a reference to the current non-draw (compute/blit) batch.
339 */
340 struct fd_batch *
fd_context_batch_nondraw(struct fd_context * ctx)341 fd_context_batch_nondraw(struct fd_context *ctx)
342 {
343 struct fd_batch *batch = NULL;
344
345 tc_assert_driver_thread(ctx->tc);
346
347 fd_batch_reference(&batch, ctx->batch_nondraw);
348
349 if (unlikely(!batch)) {
350 batch = fd_bc_alloc_batch(ctx, true);
351 fd_batch_reference(&ctx->batch_nondraw, batch);
352 fd_context_all_dirty(ctx);
353 }
354 fd_context_switch_to(ctx, batch);
355
356 return batch;
357 }
358
359 void
fd_context_destroy(struct pipe_context * pctx)360 fd_context_destroy(struct pipe_context *pctx)
361 {
362 struct fd_context *ctx = fd_context(pctx);
363 unsigned i;
364
365 DBG("");
366
367 fd_screen_lock(ctx->screen);
368 list_del(&ctx->node);
369 fd_screen_unlock(ctx->screen);
370
371 fd_pipe_fence_ref(&ctx->last_fence, NULL);
372
373 if (ctx->in_fence_fd != -1)
374 close(ctx->in_fence_fd);
375
376 for (i = 0; i < ARRAY_SIZE(ctx->pvtmem); i++) {
377 if (ctx->pvtmem[i].bo)
378 fd_bo_del(ctx->pvtmem[i].bo);
379 }
380
381 util_copy_framebuffer_state(&ctx->framebuffer, NULL);
382 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
383
384 /* Make sure nothing in the batch cache references our context any more. */
385 struct fd_batch *batch = fd_bc_last_batch(ctx);
386 if (batch) {
387 fd_bc_add_flush_deps(ctx, batch);
388 fd_batch_flush(batch);
389 fd_batch_reference(&batch, NULL);
390 }
391
392 fd_prog_fini(pctx);
393
394 if (ctx->blitter)
395 util_blitter_destroy(ctx->blitter);
396
397 if (pctx->stream_uploader)
398 u_upload_destroy(pctx->stream_uploader);
399
400 for (i = 0; i < ARRAY_SIZE(ctx->clear_rs_state); i++)
401 if (ctx->clear_rs_state[i])
402 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state[i]);
403
404 slab_destroy_child(&ctx->transfer_pool);
405 slab_destroy_child(&ctx->transfer_pool_unsync);
406
407 for (i = 0; i < ARRAY_SIZE(ctx->vsc_pipe_bo); i++) {
408 if (!ctx->vsc_pipe_bo[i])
409 break;
410 fd_bo_del(ctx->vsc_pipe_bo[i]);
411 }
412
413 fd_device_del(ctx->dev);
414 fd_pipe_purge(ctx->pipe);
415 fd_pipe_del(ctx->pipe);
416
417 simple_mtx_destroy(&ctx->gmem_lock);
418
419 u_trace_context_fini(&ctx->trace_context);
420
421 fd_autotune_fini(&ctx->autotune);
422
423 ir3_cache_destroy(ctx->shader_cache);
424
425 if (FD_DBG(BSTAT) || FD_DBG(MSGS)) {
426 mesa_logi(
427 "batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, "
428 "batch_restore=%u\n",
429 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem,
430 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_nondraw,
431 (uint32_t)ctx->stats.batch_restore);
432 }
433 }
434
435 static void
fd_set_debug_callback(struct pipe_context * pctx,const struct util_debug_callback * cb)436 fd_set_debug_callback(struct pipe_context *pctx,
437 const struct util_debug_callback *cb)
438 {
439 struct fd_context *ctx = fd_context(pctx);
440 struct fd_screen *screen = ctx->screen;
441
442 util_queue_finish(&screen->compile_queue);
443
444 if (cb)
445 ctx->debug = *cb;
446 else
447 memset(&ctx->debug, 0, sizeof(ctx->debug));
448 }
449
450 static uint32_t
fd_get_reset_count(struct fd_context * ctx,bool per_context)451 fd_get_reset_count(struct fd_context *ctx, bool per_context)
452 {
453 uint64_t val;
454 enum fd_param_id param = per_context ? FD_CTX_FAULTS : FD_GLOBAL_FAULTS;
455 ASSERTED int ret = fd_pipe_get_param(ctx->pipe, param, &val);
456 assert(!ret);
457 return val;
458 }
459
460 static enum pipe_reset_status
fd_get_device_reset_status(struct pipe_context * pctx)461 fd_get_device_reset_status(struct pipe_context *pctx)
462 {
463 struct fd_context *ctx = fd_context(pctx);
464 int context_faults = fd_get_reset_count(ctx, true);
465 int global_faults = fd_get_reset_count(ctx, false);
466 enum pipe_reset_status status;
467
468 if (context_faults != ctx->context_reset_count) {
469 status = PIPE_GUILTY_CONTEXT_RESET;
470 } else if (global_faults != ctx->global_reset_count) {
471 status = PIPE_INNOCENT_CONTEXT_RESET;
472 } else {
473 status = PIPE_NO_RESET;
474 }
475
476 ctx->context_reset_count = context_faults;
477 ctx->global_reset_count = global_faults;
478
479 return status;
480 }
481
482 static void
fd_trace_record_ts(struct u_trace * ut,void * cs,void * timestamps,uint64_t offset_B,uint32_t flags)483 fd_trace_record_ts(struct u_trace *ut, void *cs, void *timestamps,
484 uint64_t offset_B, uint32_t flags)
485 {
486 struct fd_batch *batch = container_of(ut, struct fd_batch, trace);
487 struct fd_ringbuffer *ring = cs;
488 struct pipe_resource *buffer = timestamps;
489
490 if (ring->cur == batch->last_timestamp_cmd) {
491 uint64_t *ts = fd_bo_map(fd_resource(buffer)->bo) + offset_B;
492 *ts = U_TRACE_NO_TIMESTAMP;
493 return;
494 }
495
496 batch->ctx->record_timestamp(ring, fd_resource(buffer)->bo, offset_B);
497 batch->last_timestamp_cmd = ring->cur;
498 }
499
500 static uint64_t
fd_trace_read_ts(struct u_trace_context * utctx,void * timestamps,uint64_t offset_B,void * flush_data)501 fd_trace_read_ts(struct u_trace_context *utctx,
502 void *timestamps, uint64_t offset_B, void *flush_data)
503 {
504 struct fd_context *ctx =
505 container_of(utctx, struct fd_context, trace_context);
506 struct pipe_resource *buffer = timestamps;
507 struct fd_bo *ts_bo = fd_resource(buffer)->bo;
508
509 /* Only need to stall on results for the first entry: */
510 if (offset_B == 0) {
511 /* Avoid triggering deferred submits from flushing, since that
512 * changes the behavior of what we are trying to measure:
513 */
514 while (fd_bo_cpu_prep(ts_bo, ctx->pipe, FD_BO_PREP_NOSYNC))
515 usleep(10000);
516 int ret = fd_bo_cpu_prep(ts_bo, ctx->pipe, FD_BO_PREP_READ);
517 if (ret)
518 return U_TRACE_NO_TIMESTAMP;
519 }
520
521 uint64_t *ts = fd_bo_map(ts_bo) + offset_B;
522
523 /* Don't translate the no-timestamp marker: */
524 if (*ts == U_TRACE_NO_TIMESTAMP)
525 return U_TRACE_NO_TIMESTAMP;
526
527 return ctx->ts_to_ns(*ts);
528 }
529
530 static void
fd_trace_delete_flush_data(struct u_trace_context * utctx,void * flush_data)531 fd_trace_delete_flush_data(struct u_trace_context *utctx, void *flush_data)
532 {
533 /* We don't use flush_data at the moment. */
534 }
535
536 /* TODO we could combine a few of these small buffers (solid_vbuf,
537 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
538 * save a tiny bit of memory
539 */
540
541 static struct pipe_resource *
create_solid_vertexbuf(struct pipe_context * pctx)542 create_solid_vertexbuf(struct pipe_context *pctx)
543 {
544 static const float init_shader_const[] = {
545 -1.000000f, +1.000000f, +1.000000f, +1.000000f, -1.000000f, +1.000000f,
546 };
547 struct pipe_resource *prsc =
548 pipe_buffer_create(pctx->screen, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
549 sizeof(init_shader_const));
550 pipe_buffer_write(pctx, prsc, 0, sizeof(init_shader_const),
551 init_shader_const);
552 return prsc;
553 }
554
555 static struct pipe_resource *
create_blit_texcoord_vertexbuf(struct pipe_context * pctx)556 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
557 {
558 struct pipe_resource *prsc = pipe_buffer_create(
559 pctx->screen, PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
560 return prsc;
561 }
562
563 void
fd_context_setup_common_vbos(struct fd_context * ctx)564 fd_context_setup_common_vbos(struct fd_context *ctx)
565 {
566 struct pipe_context *pctx = &ctx->base;
567
568 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
569 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
570
571 /* setup solid_vbuf_state: */
572 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
573 pctx, 1,
574 (struct pipe_vertex_element[]){{
575 .vertex_buffer_index = 0,
576 .src_offset = 0,
577 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
578 .src_stride = 12,
579 }});
580 ctx->solid_vbuf_state.vertexbuf.count = 1;
581 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->solid_vbuf;
582
583 /* setup blit_vbuf_state: */
584 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
585 pctx, 2,
586 (struct pipe_vertex_element[]){
587 {
588 .vertex_buffer_index = 0,
589 .src_offset = 0,
590 .src_format = PIPE_FORMAT_R32G32_FLOAT,
591 .src_stride = 8,
592 },
593 {
594 .vertex_buffer_index = 1,
595 .src_offset = 0,
596 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
597 .src_stride = 12,
598 }});
599 ctx->blit_vbuf_state.vertexbuf.count = 2;
600 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer.resource =
601 ctx->blit_texcoord_vbuf;
602 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer.resource = ctx->solid_vbuf;
603 }
604
605 void
fd_context_cleanup_common_vbos(struct fd_context * ctx)606 fd_context_cleanup_common_vbos(struct fd_context *ctx)
607 {
608 struct pipe_context *pctx = &ctx->base;
609
610 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
611 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
612
613 pipe_resource_reference(&ctx->solid_vbuf, NULL);
614 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
615 }
616
617 struct pipe_context *
fd_context_init(struct fd_context * ctx,struct pipe_screen * pscreen,void * priv,unsigned flags)618 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
619 void *priv, unsigned flags)
620 disable_thread_safety_analysis
621 {
622 struct fd_screen *screen = fd_screen(pscreen);
623 struct pipe_context *pctx;
624 unsigned prio = screen->prio_norm;
625
626 /* lower numerical value == higher priority: */
627 if (FD_DBG(HIPRIO))
628 prio = screen->prio_high;
629 else if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
630 prio = screen->prio_high;
631 else if (flags & PIPE_CONTEXT_LOW_PRIORITY)
632 prio = screen->prio_low;
633
634 /* Some of the stats will get printed out at context destroy, so
635 * make sure they are collected:
636 */
637 if (FD_DBG(BSTAT) || FD_DBG(MSGS))
638 ctx->stats_users++;
639
640 ctx->flags = flags;
641 ctx->screen = screen;
642 ctx->pipe = fd_pipe_new2(screen->dev, FD_PIPE_3D, prio);
643
644 ctx->in_fence_fd = -1;
645
646 if (fd_device_version(screen->dev) >= FD_VERSION_ROBUSTNESS) {
647 ctx->context_reset_count = fd_get_reset_count(ctx, true);
648 ctx->global_reset_count = fd_get_reset_count(ctx, false);
649 }
650
651 simple_mtx_init(&ctx->gmem_lock, mtx_plain);
652
653 /* need some sane default in case gallium frontends don't
654 * set some state:
655 */
656 ctx->sample_mask = 0xffff;
657 ctx->active_queries = true;
658
659 pctx = &ctx->base;
660 pctx->screen = pscreen;
661 pctx->priv = priv;
662 pctx->flush = fd_context_flush;
663 pctx->emit_string_marker = fd_emit_string_marker;
664 pctx->set_debug_callback = fd_set_debug_callback;
665 pctx->get_device_reset_status = fd_get_device_reset_status;
666 pctx->create_fence_fd = fd_create_pipe_fence_fd;
667 pctx->fence_server_sync = fd_pipe_fence_server_sync;
668 pctx->fence_server_signal = fd_pipe_fence_server_signal;
669 pctx->texture_barrier = fd_texture_barrier;
670 pctx->memory_barrier = fd_memory_barrier;
671
672 pctx->stream_uploader = u_upload_create_default(pctx);
673 if (!pctx->stream_uploader)
674 goto fail;
675 pctx->const_uploader = pctx->stream_uploader;
676
677 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
678 slab_create_child(&ctx->transfer_pool_unsync, &screen->transfer_pool);
679
680 fd_draw_init(pctx);
681 fd_resource_context_init(pctx);
682 fd_query_context_init(pctx);
683 fd_texture_init(pctx);
684 fd_state_init(pctx);
685
686 ctx->blitter = util_blitter_create(pctx);
687 if (!ctx->blitter)
688 goto fail;
689
690 list_inithead(&ctx->hw_active_queries);
691 list_inithead(&ctx->acc_active_queries);
692
693 fd_screen_lock(ctx->screen);
694 ctx->seqno = seqno_next_u16(&screen->ctx_seqno);
695 list_add(&ctx->node, &ctx->screen->context_list);
696 fd_screen_unlock(ctx->screen);
697
698 ctx->current_scissor = ctx->disabled_scissor;
699
700 fd_gpu_tracepoint_config_variable();
701 u_trace_pipe_context_init(&ctx->trace_context, pctx,
702 sizeof(uint64_t),
703 0,
704 fd_trace_record_ts,
705 fd_trace_read_ts,
706 NULL,
707 NULL,
708 fd_trace_delete_flush_data);
709
710 fd_autotune_init(&ctx->autotune, screen->dev);
711
712 return pctx;
713
714 fail:
715 pctx->destroy(pctx);
716 return NULL;
717 }
718
719 struct pipe_context *
fd_context_init_tc(struct pipe_context * pctx,unsigned flags)720 fd_context_init_tc(struct pipe_context *pctx, unsigned flags)
721 {
722 struct fd_context *ctx = fd_context(pctx);
723
724 if (!(flags & PIPE_CONTEXT_PREFER_THREADED))
725 return pctx;
726
727 /* Clover (compute-only) is unsupported. */
728 if (flags & PIPE_CONTEXT_COMPUTE_ONLY)
729 return pctx;
730
731 struct pipe_context *tc = threaded_context_create(
732 pctx, &ctx->screen->transfer_pool,
733 fd_replace_buffer_storage,
734 &(struct threaded_context_options){
735 .create_fence = fd_pipe_fence_create_unflushed,
736 .is_resource_busy = fd_resource_busy,
737 .unsynchronized_get_device_reset_status = true,
738 .unsynchronized_create_fence_fd = true,
739 },
740 &ctx->tc);
741
742 if (tc && tc != pctx) {
743 threaded_context_init_bytes_mapped_limit((struct threaded_context *)tc, 16);
744 ((struct threaded_context *)tc)->bytes_replaced_limit =
745 ((struct threaded_context *)tc)->bytes_mapped_limit / 4;
746 }
747
748 return tc;
749 }
750