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