u_threaded_context.c 89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**************************************************************************
 *
 * Copyright 2017 Advanced Micro Devices, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "util/u_threaded_context.h"
#include "util/u_cpu_detect.h"
29
#include "util/format/u_format.h"
30 31 32
#include "util/u_inlines.h"
#include "util/u_memory.h"
#include "util/u_upload_mgr.h"
33
#include "compiler/shader_info.h"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

/* 0 = disabled, 1 = assertions, 2 = printfs */
#define TC_DEBUG 0

#if TC_DEBUG >= 1
#define tc_assert assert
#else
#define tc_assert(x)
#endif

#if TC_DEBUG >= 2
#define tc_printf printf
#define tc_asprintf asprintf
#define tc_strcmp strcmp
#else
#define tc_printf(...)
#define tc_asprintf(...) 0
#define tc_strcmp(...) 0
#endif

#define TC_SENTINEL 0x5ca1ab1e

enum tc_call_id {
#define CALL(name) TC_CALL_##name,
#include "u_threaded_context_calls.h"
#undef CALL
   TC_NUM_CALLS,
};

63 64 65 66 67 68 69
/* This is actually variable-sized, because indirect isn't allocated if it's
 * not needed. */
struct tc_full_draw_info {
   struct pipe_draw_info draw;
   struct pipe_draw_indirect_info indirect;
};

70 71 72 73 74
typedef void (*tc_execute)(struct pipe_context *pipe, union tc_payload *payload);

static const tc_execute execute_func[TC_NUM_CALLS];

static void
75
tc_batch_check(UNUSED struct tc_batch *batch)
76 77 78 79 80 81 82 83 84 85 86 87 88 89
{
   tc_assert(batch->sentinel == TC_SENTINEL);
   tc_assert(batch->num_total_call_slots <= TC_CALLS_PER_BATCH);
}

static void
tc_debug_check(struct threaded_context *tc)
{
   for (unsigned i = 0; i < TC_MAX_BATCHES; i++) {
      tc_batch_check(&tc->batch_slots[i]);
      tc_assert(tc->batch_slots[i].pipe == tc->pipe);
   }
}

90 91 92 93 94 95 96 97 98 99 100 101 102
static bool
is_next_call_a_mergeable_draw(struct tc_full_draw_info *first_info,
                              struct tc_call *next,
                              struct tc_full_draw_info **next_info)
{
   return next->call_id == TC_CALL_draw_vbo &&
          (*next_info = (struct tc_full_draw_info*)&next->payload) &&
          /* All fields must be the same except start and count. */
          memcmp((uint32_t*)&first_info->draw + 2,
                 (uint32_t*)&(*next_info)->draw + 2,
                 sizeof(struct pipe_draw_info) - 8) == 0;
}

103
static void
104
tc_batch_execute(void *job, UNUSED int thread_index)
105 106 107 108 109 110 111
{
   struct tc_batch *batch = job;
   struct pipe_context *pipe = batch->pipe;
   struct tc_call *last = &batch->call[batch->num_total_call_slots];

   tc_batch_check(batch);

112 113
   assert(!batch->token);

114
   for (struct tc_call *iter = batch->call; iter != last;) {
115
      tc_assert(iter->sentinel == TC_SENTINEL);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

      /* Draw call merging. */
      if (iter->call_id == TC_CALL_draw_vbo) {
         struct tc_call *first = iter;
         struct tc_call *next = first + first->num_call_slots;
         struct tc_full_draw_info *first_info =
            (struct tc_full_draw_info*)&first->payload;
         struct tc_full_draw_info *next_info;

         /* If at least 2 consecutive draw calls can be merged... */
         if (next != last && next->call_id == TC_CALL_draw_vbo &&
             first_info->draw.drawid == 0 &&
             !first_info->draw.indirect &&
             is_next_call_a_mergeable_draw(first_info, next, &next_info)) {
            /* Merge up to 256 draw calls. */
            struct pipe_draw_start_count multi[256];
            unsigned num_draws = 2;

            multi[0].start = first_info->draw.start;
            multi[0].count = first_info->draw.count;
            multi[1].start = next_info->draw.start;
            multi[1].count = next_info->draw.count;

            if (next_info->draw.index_size)
               pipe_resource_reference(&next_info->draw.index.resource, NULL);

            /* Find how many other draws can be merged. */
            next = next + next->num_call_slots;
            for (; next != last && num_draws < ARRAY_SIZE(multi) &&
                 is_next_call_a_mergeable_draw(first_info, next, &next_info);
                 next += next->num_call_slots, num_draws++) {
               multi[num_draws].start = next_info->draw.start;
               multi[num_draws].count = next_info->draw.count;

               if (next_info->draw.index_size)
                  pipe_resource_reference(&next_info->draw.index.resource, NULL);
            }

            pipe->multi_draw(pipe, &first_info->draw, multi, num_draws);
            if (first_info->draw.index_size)
               pipe_resource_reference(&first_info->draw.index.resource, NULL);
            iter = next;
            continue;
         }
      }

162
      execute_func[iter->call_id](pipe, &iter->payload);
163
      iter += iter->num_call_slots;
164 165 166 167 168 169 170 171 172 173 174 175 176 177
   }

   tc_batch_check(batch);
   batch->num_total_call_slots = 0;
}

static void
tc_batch_flush(struct threaded_context *tc)
{
   struct tc_batch *next = &tc->batch_slots[tc->next];

   tc_assert(next->num_total_call_slots != 0);
   tc_batch_check(next);
   tc_debug_check(tc);
178
   tc->bytes_mapped_estimate = 0;
179 180
   p_atomic_add(&tc->num_offloaded_slots, next->num_total_call_slots);

181 182 183 184 185
   if (next->token) {
      next->token->tc = NULL;
      tc_unflushed_batch_token_reference(&next->token, NULL);
   }

186
   util_queue_add_job(&tc->queue, next, &next->fence, tc_batch_execute,
187
                      NULL, 0);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
   tc->last = tc->next;
   tc->next = (tc->next + 1) % TC_MAX_BATCHES;
}

/* This is the function that adds variable-sized calls into the current
 * batch. It also flushes the batch if there is not enough space there.
 * All other higher-level "add" functions use it.
 */
static union tc_payload *
tc_add_sized_call(struct threaded_context *tc, enum tc_call_id id,
                  unsigned payload_size)
{
   struct tc_batch *next = &tc->batch_slots[tc->next];
   unsigned total_size = offsetof(struct tc_call, payload) + payload_size;
   unsigned num_call_slots = DIV_ROUND_UP(total_size, sizeof(struct tc_call));

   tc_debug_check(tc);

   if (unlikely(next->num_total_call_slots + num_call_slots > TC_CALLS_PER_BATCH)) {
      tc_batch_flush(tc);
      next = &tc->batch_slots[tc->next];
      tc_assert(next->num_total_call_slots == 0);
   }

   tc_assert(util_queue_fence_is_signalled(&next->fence));

   struct tc_call *call = &next->call[next->num_total_call_slots];
   next->num_total_call_slots += num_call_slots;

   call->sentinel = TC_SENTINEL;
   call->call_id = id;
   call->num_call_slots = num_call_slots;

   tc_debug_check(tc);
   return &call->payload;
}

#define tc_add_struct_typed_call(tc, execute, type) \
   ((struct type*)tc_add_sized_call(tc, execute, sizeof(struct type)))

#define tc_add_slot_based_call(tc, execute, type, num_slots) \
   ((struct type*)tc_add_sized_call(tc, execute, \
                                    sizeof(struct type) + \
                                    sizeof(((struct type*)NULL)->slot[0]) * \
232
                                    (num_slots)))
233 234 235 236 237 238 239

static union tc_payload *
tc_add_small_call(struct threaded_context *tc, enum tc_call_id id)
{
   return tc_add_sized_call(tc, id, 0);
}

240 241 242 243 244 245 246 247 248 249
static bool
tc_is_sync(struct threaded_context *tc)
{
   struct tc_batch *last = &tc->batch_slots[tc->last];
   struct tc_batch *next = &tc->batch_slots[tc->next];

   return util_queue_fence_is_signalled(&last->fence) &&
          !next->num_total_call_slots;
}

250
static void
251
_tc_sync(struct threaded_context *tc, UNUSED const char *info, UNUSED const char *func)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
{
   struct tc_batch *last = &tc->batch_slots[tc->last];
   struct tc_batch *next = &tc->batch_slots[tc->next];
   bool synced = false;

   tc_debug_check(tc);

   /* Only wait for queued calls... */
   if (!util_queue_fence_is_signalled(&last->fence)) {
      util_queue_fence_wait(&last->fence);
      synced = true;
   }

   tc_debug_check(tc);

267 268 269 270 271
   if (next->token) {
      next->token->tc = NULL;
      tc_unflushed_batch_token_reference(&next->token, NULL);
   }

272 273 274
   /* .. and execute unflushed calls directly. */
   if (next->num_total_call_slots) {
      p_atomic_add(&tc->num_direct_slots, next->num_total_call_slots);
275
      tc->bytes_mapped_estimate = 0;
276 277 278 279 280 281 282
      tc_batch_execute(next, 0);
      synced = true;
   }

   if (synced) {
      p_atomic_inc(&tc->num_syncs);

283
      if (tc_strcmp(func, "tc_destroy") != 0) {
284
         tc_printf("sync %s %s\n", func, info);
285
	  }
286 287 288 289 290 291 292 293
   }

   tc_debug_check(tc);
}

#define tc_sync(tc) _tc_sync(tc, "", __func__)
#define tc_sync_msg(tc, info) _tc_sync(tc, info, __func__)

294 295 296 297 298 299 300 301 302
/**
 * Call this from fence_finish for same-context fence waits of deferred fences
 * that haven't been flushed yet.
 *
 * The passed pipe_context must be the one passed to pipe_screen::fence_finish,
 * i.e., the wrapped one.
 */
void
threaded_context_flush(struct pipe_context *_pipe,
303 304
                       struct tc_unflushed_batch_token *token,
                       bool prefer_async)
305 306 307
{
   struct threaded_context *tc = threaded_context(_pipe);

308
   /* This is called from the gallium frontend / application thread. */
309 310 311 312 313 314 315 316 317 318 319
   if (token->tc && token->tc == tc) {
      struct tc_batch *last = &tc->batch_slots[tc->last];

      /* Prefer to do the flush in the driver thread if it is already
       * running. That should be better for cache locality.
       */
      if (prefer_async || !util_queue_fence_is_signalled(&last->fence))
         tc_batch_flush(tc);
      else
         tc_sync(token->tc);
   }
320 321
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
static void
tc_set_resource_reference(struct pipe_resource **dst, struct pipe_resource *src)
{
   *dst = NULL;
   pipe_resource_reference(dst, src);
}

void
threaded_resource_init(struct pipe_resource *res)
{
   struct threaded_resource *tres = threaded_resource(res);

   tres->latest = &tres->b;
   util_range_init(&tres->valid_buffer_range);
   tres->base_valid_buffer_range = &tres->valid_buffer_range;
   tres->is_shared = false;
   tres->is_user_ptr = false;
}

void
threaded_resource_deinit(struct pipe_resource *res)
{
   struct threaded_resource *tres = threaded_resource(res);

   if (tres->latest != &tres->b)
           pipe_resource_reference(&tres->latest, NULL);
   util_range_destroy(&tres->valid_buffer_range);
}

struct pipe_context *
threaded_context_unwrap_sync(struct pipe_context *pipe)
{
   if (!pipe || !pipe->priv)
      return pipe;

   tc_sync(threaded_context(pipe));
   return (struct pipe_context*)pipe->priv;
}


/********************************************************************
 * simple functions
 */

#define TC_FUNC1(func, m_payload, qualifier, type, deref, deref2) \
   static void \
   tc_call_##func(struct pipe_context *pipe, union tc_payload *payload) \
   { \
      pipe->func(pipe, deref2((type*)payload)); \
   } \
   \
   static void \
   tc_##func(struct pipe_context *_pipe, qualifier type deref param) \
   { \
      struct threaded_context *tc = threaded_context(_pipe); \
      type *p = (type*)tc_add_sized_call(tc, TC_CALL_##func, sizeof(type)); \
      *p = deref(param); \
   }

381
TC_FUNC1(set_active_query_state, flags, , bool, , *)
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

TC_FUNC1(set_blend_color, blend_color, const, struct pipe_blend_color, *, )
TC_FUNC1(set_stencil_ref, stencil_ref, const, struct pipe_stencil_ref, *, )
TC_FUNC1(set_clip_state, clip_state, const, struct pipe_clip_state, *, )
TC_FUNC1(set_sample_mask, sample_mask, , unsigned, , *)
TC_FUNC1(set_min_samples, min_samples, , unsigned, , *)
TC_FUNC1(set_polygon_stipple, polygon_stipple, const, struct pipe_poly_stipple, *, )

TC_FUNC1(texture_barrier, flags, , unsigned, , *)
TC_FUNC1(memory_barrier, flags, , unsigned, , *)


/********************************************************************
 * queries
 */

static struct pipe_query *
tc_create_query(struct pipe_context *_pipe, unsigned query_type,
                unsigned index)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct pipe_context *pipe = tc->pipe;

   return pipe->create_query(pipe, query_type, index);
}

static struct pipe_query *
tc_create_batch_query(struct pipe_context *_pipe, unsigned num_queries,
                      unsigned *query_types)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct pipe_context *pipe = tc->pipe;

   return pipe->create_batch_query(pipe, num_queries, query_types);
}

static void
tc_call_destroy_query(struct pipe_context *pipe, union tc_payload *payload)
{
421 422 423
   struct threaded_query *tq = threaded_query(payload->query);

   if (tq->head_unflushed.next)
Timothy Arceri's avatar
Timothy Arceri committed
424
      list_del(&tq->head_unflushed);
425

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
   pipe->destroy_query(pipe, payload->query);
}

static void
tc_destroy_query(struct pipe_context *_pipe, struct pipe_query *query)
{
   struct threaded_context *tc = threaded_context(_pipe);

   tc_add_small_call(tc, TC_CALL_destroy_query)->query = query;
}

static void
tc_call_begin_query(struct pipe_context *pipe, union tc_payload *payload)
{
   pipe->begin_query(pipe, payload->query);
}

443
static bool
444 445 446 447 448 449 450 451 452
tc_begin_query(struct pipe_context *_pipe, struct pipe_query *query)
{
   struct threaded_context *tc = threaded_context(_pipe);
   union tc_payload *payload = tc_add_small_call(tc, TC_CALL_begin_query);

   payload->query = query;
   return true; /* we don't care about the return value for this call */
}

453 454 455 456 457
struct tc_end_query_payload {
   struct threaded_context *tc;
   struct pipe_query *query;
};

458 459 460
static void
tc_call_end_query(struct pipe_context *pipe, union tc_payload *payload)
{
461 462 463 464
   struct tc_end_query_payload *p = (struct tc_end_query_payload *)payload;
   struct threaded_query *tq = threaded_query(p->query);

   if (!tq->head_unflushed.next)
Timothy Arceri's avatar
Timothy Arceri committed
465
      list_add(&tq->head_unflushed, &p->tc->unflushed_queries);
466 467

   pipe->end_query(pipe, p->query);
468 469 470 471 472 473 474
}

static bool
tc_end_query(struct pipe_context *_pipe, struct pipe_query *query)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct threaded_query *tq = threaded_query(query);
475 476 477 478
   struct tc_end_query_payload *payload =
      tc_add_struct_typed_call(tc, TC_CALL_end_query, tc_end_query_payload);

   payload->tc = tc;
479 480 481 482 483 484 485
   payload->query = query;

   tq->flushed = false;

   return true; /* we don't care about the return value for this call */
}

486
static bool
487
tc_get_query_result(struct pipe_context *_pipe,
488
                    struct pipe_query *query, bool wait,
489 490 491 492 493 494 495 496 497 498 499 500 501
                    union pipe_query_result *result)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct threaded_query *tq = threaded_query(query);
   struct pipe_context *pipe = tc->pipe;

   if (!tq->flushed)
      tc_sync_msg(tc, wait ? "wait" : "nowait");

   bool success = pipe->get_query_result(pipe, query, wait, result);

   if (success) {
      tq->flushed = true;
502 503
      if (tq->head_unflushed.next) {
         /* This is safe because it can only happen after we sync'd. */
Timothy Arceri's avatar
Timothy Arceri committed
504
         list_del(&tq->head_unflushed);
505
      }
506 507 508 509 510 511
   }
   return success;
}

struct tc_query_result_resource {
   struct pipe_query *query;
512
   bool wait;
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
   enum pipe_query_value_type result_type;
   int index;
   struct pipe_resource *resource;
   unsigned offset;
};

static void
tc_call_get_query_result_resource(struct pipe_context *pipe,
                                  union tc_payload *payload)
{
   struct tc_query_result_resource *p = (struct tc_query_result_resource *)payload;

   pipe->get_query_result_resource(pipe, p->query, p->wait, p->result_type,
                                   p->index, p->resource, p->offset);
   pipe_resource_reference(&p->resource, NULL);
}

static void
tc_get_query_result_resource(struct pipe_context *_pipe,
532
                             struct pipe_query *query, bool wait,
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
                             enum pipe_query_value_type result_type, int index,
                             struct pipe_resource *resource, unsigned offset)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_query_result_resource *p =
      tc_add_struct_typed_call(tc, TC_CALL_get_query_result_resource,
                               tc_query_result_resource);

   p->query = query;
   p->wait = wait;
   p->result_type = result_type;
   p->index = index;
   tc_set_resource_reference(&p->resource, resource);
   p->offset = offset;
}

struct tc_render_condition {
   struct pipe_query *query;
   bool condition;
   unsigned mode;
};

static void
tc_call_render_condition(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_render_condition *p = (struct tc_render_condition *)payload;
   pipe->render_condition(pipe, p->query, p->condition, p->mode);
}

static void
tc_render_condition(struct pipe_context *_pipe,
564
                    struct pipe_query *query, bool condition,
565
                    enum pipe_render_cond_flag mode)
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_render_condition *p =
      tc_add_struct_typed_call(tc, TC_CALL_render_condition, tc_render_condition);

   p->query = query;
   p->condition = condition;
   p->mode = mode;
}


/********************************************************************
 * constant (immutable) states
 */

#define TC_CSO_CREATE(name, sname) \
   static void * \
   tc_create_##name##_state(struct pipe_context *_pipe, \
                            const struct pipe_##sname##_state *state) \
   { \
      struct pipe_context *pipe = threaded_context(_pipe)->pipe; \
      return pipe->create_##name##_state(pipe, state); \
   }

#define TC_CSO_BIND(name) TC_FUNC1(bind_##name##_state, cso, , void *, , *)
#define TC_CSO_DELETE(name) TC_FUNC1(delete_##name##_state, cso, , void *, , *)

#define TC_CSO_WHOLE2(name, sname) \
   TC_CSO_CREATE(name, sname) \
   TC_CSO_BIND(name) \
   TC_CSO_DELETE(name)

#define TC_CSO_WHOLE(name) TC_CSO_WHOLE2(name, name)

TC_CSO_WHOLE(blend)
TC_CSO_WHOLE(rasterizer)
TC_CSO_WHOLE(depth_stencil_alpha)
TC_CSO_WHOLE(compute)
TC_CSO_WHOLE2(fs, shader)
TC_CSO_WHOLE2(vs, shader)
TC_CSO_WHOLE2(gs, shader)
TC_CSO_WHOLE2(tcs, shader)
TC_CSO_WHOLE2(tes, shader)
TC_CSO_CREATE(sampler, sampler)
TC_CSO_DELETE(sampler)
TC_CSO_BIND(vertex_elements)
TC_CSO_DELETE(vertex_elements)

static void *
tc_create_vertex_elements_state(struct pipe_context *_pipe, unsigned count,
                                const struct pipe_vertex_element *elems)
{
   struct pipe_context *pipe = threaded_context(_pipe)->pipe;

   return pipe->create_vertex_elements_state(pipe, count, elems);
}

struct tc_sampler_states {
   ubyte shader, start, count;
   void *slot[0]; /* more will be allocated if needed */
};

static void
tc_call_bind_sampler_states(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_sampler_states *p = (struct tc_sampler_states *)payload;
   pipe->bind_sampler_states(pipe, p->shader, p->start, p->count, p->slot);
}

static void
tc_bind_sampler_states(struct pipe_context *_pipe,
                       enum pipe_shader_type shader,
                       unsigned start, unsigned count, void **states)
{
   if (!count)
      return;

   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_sampler_states *p =
      tc_add_slot_based_call(tc, TC_CALL_bind_sampler_states, tc_sampler_states, count);

   p->shader = shader;
   p->start = start;
   p->count = count;
   memcpy(p->slot, states, count * sizeof(states[0]));
}


/********************************************************************
 * immediate states
 */

static void
tc_call_set_framebuffer_state(struct pipe_context *pipe, union tc_payload *payload)
{
   struct pipe_framebuffer_state *p = (struct pipe_framebuffer_state *)payload;

   pipe->set_framebuffer_state(pipe, p);

   unsigned nr_cbufs = p->nr_cbufs;
   for (unsigned i = 0; i < nr_cbufs; i++)
      pipe_surface_reference(&p->cbufs[i], NULL);
   pipe_surface_reference(&p->zsbuf, NULL);
}

static void
tc_set_framebuffer_state(struct pipe_context *_pipe,
                         const struct pipe_framebuffer_state *fb)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct pipe_framebuffer_state *p =
      tc_add_struct_typed_call(tc, TC_CALL_set_framebuffer_state,
                               pipe_framebuffer_state);
   unsigned nr_cbufs = fb->nr_cbufs;

   p->width = fb->width;
   p->height = fb->height;
   p->samples = fb->samples;
   p->layers = fb->layers;
   p->nr_cbufs = nr_cbufs;

   for (unsigned i = 0; i < nr_cbufs; i++) {
      p->cbufs[i] = NULL;
      pipe_surface_reference(&p->cbufs[i], fb->cbufs[i]);
   }
   p->zsbuf = NULL;
   pipe_surface_reference(&p->zsbuf, fb->zsbuf);
}

static void
tc_call_set_tess_state(struct pipe_context *pipe, union tc_payload *payload)
{
   float *p = (float*)payload;
   pipe->set_tess_state(pipe, p, p + 4);
}

static void
tc_set_tess_state(struct pipe_context *_pipe,
                  const float default_outer_level[4],
                  const float default_inner_level[2])
{
   struct threaded_context *tc = threaded_context(_pipe);
   float *p = (float*)tc_add_sized_call(tc, TC_CALL_set_tess_state,
                                        sizeof(float) * 6);

   memcpy(p, default_outer_level, 4 * sizeof(float));
   memcpy(p + 4, default_inner_level, 2 * sizeof(float));
}

struct tc_constant_buffer {
   ubyte shader, index;
   struct pipe_constant_buffer cb;
};

static void
tc_call_set_constant_buffer(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_constant_buffer *p = (struct tc_constant_buffer *)payload;

   pipe->set_constant_buffer(pipe,
                             p->shader,
                             p->index,
                             &p->cb);
   pipe_resource_reference(&p->cb.buffer, NULL);
}

static void
tc_set_constant_buffer(struct pipe_context *_pipe,
734
                       enum pipe_shader_type shader, uint index,
735 736 737 738 739 740 741 742 743 744 745
                       const struct pipe_constant_buffer *cb)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct pipe_resource *buffer = NULL;
   unsigned offset;

   /* This must be done before adding set_constant_buffer, because it could
    * generate e.g. transfer_unmap and flush partially-uninitialized
    * set_constant_buffer to the driver if it was done afterwards.
    */
   if (cb && cb->user_buffer) {
746
      u_upload_data(tc->base.const_uploader, 0, cb->buffer_size, tc->ubo_alignment,
747
                    cb->user_buffer, &offset, &buffer);
748
      u_upload_unmap(tc->base.const_uploader);
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
   }

   struct tc_constant_buffer *p =
      tc_add_struct_typed_call(tc, TC_CALL_set_constant_buffer,
                               tc_constant_buffer);
   p->shader = shader;
   p->index = index;

   if (cb) {
      if (cb->user_buffer) {
         p->cb.buffer_size = cb->buffer_size;
         p->cb.user_buffer = NULL;
         p->cb.buffer_offset = offset;
         p->cb.buffer = buffer;
      } else {
         tc_set_resource_reference(&p->cb.buffer,
                                   cb->buffer);
766 767 768
         p->cb.user_buffer = NULL;
         p->cb.buffer_offset = cb->buffer_offset;
         p->cb.buffer_size = cb->buffer_size;
769 770 771 772 773 774
      }
   } else {
      memset(&p->cb, 0, sizeof(*cb));
   }
}

775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
struct tc_inlinable_constants {
   ubyte shader;
   ubyte num_values;
   uint32_t values[MAX_INLINABLE_UNIFORMS];
};

static void
tc_call_set_inlinable_constants(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_inlinable_constants *p = (struct tc_inlinable_constants *)payload;

   pipe->set_inlinable_constants(pipe, p->shader, p->num_values, p->values);
}

static void
tc_set_inlinable_constants(struct pipe_context *_pipe,
                           enum pipe_shader_type shader,
                           uint num_values, uint32_t *values)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_inlinable_constants *p =
      tc_add_struct_typed_call(tc, TC_CALL_set_inlinable_constants,
                               tc_inlinable_constants);
   p->shader = shader;
   p->num_values = num_values;
   memcpy(p->values, values, num_values * 4);
}

803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
struct tc_scissors {
   ubyte start, count;
   struct pipe_scissor_state slot[0]; /* more will be allocated if needed */
};

static void
tc_call_set_scissor_states(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_scissors *p = (struct tc_scissors *)payload;
   pipe->set_scissor_states(pipe, p->start, p->count, p->slot);
}

static void
tc_set_scissor_states(struct pipe_context *_pipe,
                      unsigned start, unsigned count,
                      const struct pipe_scissor_state *states)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_scissors *p =
      tc_add_slot_based_call(tc, TC_CALL_set_scissor_states, tc_scissors, count);

   p->start = start;
   p->count = count;
   memcpy(&p->slot, states, count * sizeof(states[0]));
}

struct tc_viewports {
   ubyte start, count;
   struct pipe_viewport_state slot[0]; /* more will be allocated if needed */
};

static void
tc_call_set_viewport_states(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_viewports *p = (struct tc_viewports *)payload;
   pipe->set_viewport_states(pipe, p->start, p->count, p->slot);
}

static void
tc_set_viewport_states(struct pipe_context *_pipe,
                       unsigned start, unsigned count,
                       const struct pipe_viewport_state *states)
{
   if (!count)
      return;

   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_viewports *p =
      tc_add_slot_based_call(tc, TC_CALL_set_viewport_states, tc_viewports, count);

   p->start = start;
   p->count = count;
   memcpy(&p->slot, states, count * sizeof(states[0]));
}

struct tc_window_rects {
   bool include;
   ubyte count;
   struct pipe_scissor_state slot[0]; /* more will be allocated if needed */
};

static void
tc_call_set_window_rectangles(struct pipe_context *pipe,
                              union tc_payload *payload)
{
   struct tc_window_rects *p = (struct tc_window_rects *)payload;
   pipe->set_window_rectangles(pipe, p->include, p->count, p->slot);
}

static void
873
tc_set_window_rectangles(struct pipe_context *_pipe, bool include,
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
                         unsigned count,
                         const struct pipe_scissor_state *rects)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_window_rects *p =
      tc_add_slot_based_call(tc, TC_CALL_set_window_rectangles, tc_window_rects, count);

   p->include = include;
   p->count = count;
   memcpy(p->slot, rects, count * sizeof(rects[0]));
}

struct tc_sampler_views {
   ubyte shader, start, count;
   struct pipe_sampler_view *slot[0]; /* more will be allocated if needed */
};

static void
tc_call_set_sampler_views(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_sampler_views *p = (struct tc_sampler_views *)payload;
   unsigned count = p->count;

   pipe->set_sampler_views(pipe, p->shader, p->start, p->count, p->slot);
   for (unsigned i = 0; i < count; i++)
      pipe_sampler_view_reference(&p->slot[i], NULL);
}

static void
tc_set_sampler_views(struct pipe_context *_pipe,
                     enum pipe_shader_type shader,
                     unsigned start, unsigned count,
                     struct pipe_sampler_view **views)
{
   if (!count)
      return;

   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_sampler_views *p =
      tc_add_slot_based_call(tc, TC_CALL_set_sampler_views, tc_sampler_views, count);

   p->shader = shader;
   p->start = start;
   p->count = count;

   if (views) {
      for (unsigned i = 0; i < count; i++) {
         p->slot[i] = NULL;
         pipe_sampler_view_reference(&p->slot[i], views[i]);
      }
   } else {
      memset(p->slot, 0, count * sizeof(views[0]));
   }
}

struct tc_shader_images {
   ubyte shader, start, count;
931
   bool unbind;
932 933 934 935 936 937 938 939 940
   struct pipe_image_view slot[0]; /* more will be allocated if needed */
};

static void
tc_call_set_shader_images(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_shader_images *p = (struct tc_shader_images *)payload;
   unsigned count = p->count;

941 942 943 944 945
   if (p->unbind) {
      pipe->set_shader_images(pipe, p->shader, p->start, p->count, NULL);
      return;
   }

946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
   pipe->set_shader_images(pipe, p->shader, p->start, p->count, p->slot);

   for (unsigned i = 0; i < count; i++)
      pipe_resource_reference(&p->slot[i].resource, NULL);
}

static void
tc_set_shader_images(struct pipe_context *_pipe,
                     enum pipe_shader_type shader,
                     unsigned start, unsigned count,
                     const struct pipe_image_view *images)
{
   if (!count)
      return;

   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_shader_images *p =
963 964
      tc_add_slot_based_call(tc, TC_CALL_set_shader_images, tc_shader_images,
                             images ? count : 0);
965 966 967 968

   p->shader = shader;
   p->start = start;
   p->count = count;
969
   p->unbind = images == NULL;
970 971 972 973 974 975 976 977 978 979 980

   if (images) {
      for (unsigned i = 0; i < count; i++) {
         tc_set_resource_reference(&p->slot[i].resource, images[i].resource);

         if (images[i].access & PIPE_IMAGE_ACCESS_WRITE &&
             images[i].resource &&
             images[i].resource->target == PIPE_BUFFER) {
            struct threaded_resource *tres =
               threaded_resource(images[i].resource);

981 982
            util_range_add(&tres->b, &tres->valid_buffer_range,
                           images[i].u.buf.offset,
983 984 985 986 987 988 989 990 991
                           images[i].u.buf.offset + images[i].u.buf.size);
         }
      }
      memcpy(p->slot, images, count * sizeof(images[0]));
   }
}

struct tc_shader_buffers {
   ubyte shader, start, count;
992
   bool unbind;
993
   unsigned writable_bitmask;
994 995 996 997 998 999 1000 1001 1002
   struct pipe_shader_buffer slot[0]; /* more will be allocated if needed */
};

static void
tc_call_set_shader_buffers(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_shader_buffers *p = (struct tc_shader_buffers *)payload;
   unsigned count = p->count;

1003
   if (p->unbind) {
1004
      pipe->set_shader_buffers(pipe, p->shader, p->start, p->count, NULL, 0);
1005 1006 1007
      return;
   }

1008 1009
   pipe->set_shader_buffers(pipe, p->shader, p->start, p->count, p->slot,
                            p->writable_bitmask);
1010 1011 1012 1013 1014 1015

   for (unsigned i = 0; i < count; i++)
      pipe_resource_reference(&p->slot[i].buffer, NULL);
}

static void
1016 1017
tc_set_shader_buffers(struct pipe_context *_pipe,
                      enum pipe_shader_type shader,
1018
                      unsigned start, unsigned count,
1019 1020
                      const struct pipe_shader_buffer *buffers,
                      unsigned writable_bitmask)
1021 1022 1023 1024 1025 1026
{
   if (!count)
      return;

   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_shader_buffers *p =
1027 1028
      tc_add_slot_based_call(tc, TC_CALL_set_shader_buffers, tc_shader_buffers,
                             buffers ? count : 0);
1029 1030 1031 1032

   p->shader = shader;
   p->start = start;
   p->count = count;
1033
   p->unbind = buffers == NULL;
1034
   p->writable_bitmask = writable_bitmask;
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

   if (buffers) {
      for (unsigned i = 0; i < count; i++) {
         struct pipe_shader_buffer *dst = &p->slot[i];
         const struct pipe_shader_buffer *src = buffers + i;

         tc_set_resource_reference(&dst->buffer, src->buffer);
         dst->buffer_offset = src->buffer_offset;
         dst->buffer_size = src->buffer_size;

         if (src->buffer) {
            struct threaded_resource *tres = threaded_resource(src->buffer);

1048 1049
            util_range_add(&tres->b, &tres->valid_buffer_range,
                           src->buffer_offset,
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
                           src->buffer_offset + src->buffer_size);
         }
      }
   }
}

struct tc_vertex_buffers {
   ubyte start, count;
   bool unbind;
   struct pipe_vertex_buffer slot[0]; /* more will be allocated if needed */
};

static void
tc_call_set_vertex_buffers(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_vertex_buffers *p = (struct tc_vertex_buffers *)payload;
   unsigned count = p->count;

   if (p->unbind) {
      pipe->set_vertex_buffers(pipe, p->start, count, NULL);
      return;
   }

   for (unsigned i = 0; i < count; i++)
      tc_assert(!p->slot[i].is_user_buffer);

   pipe->set_vertex_buffers(pipe, p->start, count, p->slot);
   for (unsigned i = 0; i < count; i++)
      pipe_resource_reference(&p->slot[i].buffer.resource, NULL);
}

static void
tc_set_vertex_buffers(struct pipe_context *_pipe,
                      unsigned start, unsigned count,
                      const struct pipe_vertex_buffer *buffers)
{
   struct threaded_context *tc = threaded_context(_pipe);

   if (!count)
      return;

   if (buffers) {
      struct tc_vertex_buffers *p =
         tc_add_slot_based_call(tc, TC_CALL_set_vertex_buffers, tc_vertex_buffers, count);
      p->start = start;
      p->count = count;
      p->unbind = false;

      for (unsigned i = 0; i < count; i++) {
         struct pipe_vertex_buffer *dst = &p->slot[i];
         const struct pipe_vertex_buffer *src = buffers + i;

         tc_assert(!src->is_user_buffer);
         dst->stride = src->stride;
         dst->is_user_buffer = false;
         tc_set_resource_reference(&dst->buffer.resource,
                                   src->buffer.resource);
         dst->buffer_offset = src->buffer_offset;
      }
   } else {
      struct tc_vertex_buffers *p =
         tc_add_slot_based_call(tc, TC_CALL_set_vertex_buffers, tc_vertex_buffers, 0);
      p->start = start;
      p->count = count;
      p->unbind = true;
   }
}

struct tc_stream_outputs {
   unsigned count;
   struct pipe_stream_output_target *targets[PIPE_MAX_SO_BUFFERS];
   unsigned offsets[PIPE_MAX_SO_BUFFERS];
};

static void
tc_call_set_stream_output_targets(struct pipe_context *pipe, union tc_payload *payload)
{
   struct tc_stream_outputs *p = (struct tc_stream_outputs *)payload;
   unsigned count = p->count;

   pipe->set_stream_output_targets(pipe, count, p->targets, p->offsets);
   for (unsigned i = 0; i < count; i++)
      pipe_so_target_reference(&p->targets[i], NULL);
}

static void
tc_set_stream_output_targets(struct pipe_context *_pipe,
                             unsigned count,
                             struct pipe_stream_output_target **tgs,
                             const unsigned *offsets)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_stream_outputs *p =
      tc_add_struct_typed_call(tc, TC_CALL_set_stream_output_targets,
                               tc_stream_outputs);

   for (unsigned i = 0; i < count; i++) {
      p->targets[i] = NULL;
      pipe_so_target_reference(&p->targets[i], tgs[i]);
   }
   p->count = count;
   memcpy(p->offsets, offsets, count * sizeof(unsigned));
}

static void
tc_set_compute_resources(struct pipe_context *_pipe, unsigned start,
                         unsigned count, struct pipe_surface **resources)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct pipe_context *pipe = tc->pipe;

   tc_sync(tc);
   pipe->set_compute_resources(pipe, start, count, resources);
}

static void
tc_set_global_binding(struct pipe_context *_pipe, unsigned first,
                      unsigned count, struct pipe_resource **resources,
1168
                      uint32_t **handles)
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct pipe_context *pipe = tc->pipe;

   tc_sync(tc);
   pipe->set_global_binding(pipe, first, count, resources, handles);
}


/********************************************************************
 * views
 */

static struct pipe_surface *
tc_create_surface(struct pipe_context *_pipe,
                  struct pipe_resource *resource,
                  const struct pipe_surface *surf_tmpl)
{
   struct pipe_context *pipe = threaded_context(_pipe)->pipe;
   struct pipe_surface *view =
         pipe->create_surface(pipe, resource, surf_tmpl);

   if (view)
      view->context = _pipe;
   return view;
}

static void
tc_surface_destroy(struct pipe_context *_pipe,
                   struct pipe_surface *surf)
{
   struct pipe_context *pipe = threaded_context(_pipe)->pipe;

   pipe->surface_destroy(pipe, surf);
}

static struct pipe_sampler_view *
tc_create_sampler_view(struct pipe_context *_pipe,
                       struct pipe_resource *resource,
                       const struct pipe_sampler_view *templ)
{
   struct pipe_context *pipe = threaded_context(_pipe)->pipe;
   struct pipe_sampler_view *view =
         pipe->create_sampler_view(pipe, resource, templ);

   if (view)
      view->context = _pipe;
   return view;
}

static void
tc_sampler_view_destroy(struct pipe_context *_pipe,
                        struct pipe_sampler_view *view)
{
   struct pipe_context *pipe = threaded_context(_pipe)->pipe;

   pipe->sampler_view_destroy(pipe, view);
}

static struct pipe_stream_output_target *
tc_create_stream_output_target(struct pipe_context *_pipe,
                               struct pipe_resource *res,
                               unsigned buffer_offset,
                               unsigned buffer_size)
{
   struct pipe_context *pipe = threaded_context(_pipe)->pipe;
   struct threaded_resource *tres = threaded_resource(res);
   struct pipe_stream_output_target *view;

   tc_sync(threaded_context(_pipe));
1239
   util_range_add(&tres->b, &tres->valid_buffer_range, buffer_offset,
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
                  buffer_offset + buffer_size);

   view = pipe->create_stream_output_target(pipe, res, buffer_offset,
                                            buffer_size);
   if (view)
      view->context = _pipe;
   return view;
}

static void
tc_stream_output_target_destroy(struct pipe_context *_pipe,
                                struct pipe_stream_output_target *target)
{
   struct pipe_context *pipe = threaded_context(_pipe)->pipe;

   pipe->stream_output_target_destroy(pipe, target);
}


1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
/********************************************************************
 * bindless
 */

static uint64_t
tc_create_texture_handle(struct pipe_context *_pipe,
                         struct pipe_sampler_view *view,
                         const struct pipe_sampler_state *state)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct pipe_context *pipe = tc->pipe;

   tc_sync(tc);
   return pipe->create_texture_handle(pipe, view, state);
}

static void
tc_call_delete_texture_handle(struct pipe_context *pipe,
                              union tc_payload *payload)
{
   pipe->delete_texture_handle(pipe, payload->handle);
}

static void
tc_delete_texture_handle(struct pipe_context *_pipe, uint64_t handle)
{
   struct threaded_context *tc = threaded_context(_pipe);
   union tc_payload *payload =
      tc_add_small_call(tc, TC_CALL_delete_texture_handle);

   payload->handle = handle;
}

struct tc_make_texture_handle_resident
{
   uint64_t handle;
   bool resident;
};

static void
tc_call_make_texture_handle_resident(struct pipe_context *pipe,
                                     union tc_payload *payload)
{
   struct tc_make_texture_handle_resident *p =
      (struct tc_make_texture_handle_resident *)payload;

   pipe->make_texture_handle_resident(pipe, p->handle, p->resident);
}

static void
tc_make_texture_handle_resident(struct pipe_context *_pipe, uint64_t handle,
                                bool resident)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_make_texture_handle_resident *p =
      tc_add_struct_typed_call(tc, TC_CALL_make_texture_handle_resident,
                               tc_make_texture_handle_resident);

   p->handle = handle;
   p->resident = resident;
}

static uint64_t
tc_create_image_handle(struct pipe_context *_pipe,
                       const struct pipe_image_view *image)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct pipe_context *pipe = tc->pipe;

   tc_sync(tc);
   return pipe->create_image_handle(pipe, image);
}

static void
tc_call_delete_image_handle(struct pipe_context *pipe,
                            union tc_payload *payload)
{
   pipe->delete_image_handle(pipe, payload->handle);
}

static void
tc_delete_image_handle(struct pipe_context *_pipe, uint64_t handle)
{
   struct threaded_context *tc = threaded_context(_pipe);
   union tc_payload *payload =
      tc_add_small_call(tc, TC_CALL_delete_image_handle);

   payload->handle = handle;
}

struct tc_make_image_handle_resident
{
   uint64_t handle;
   unsigned access;
   bool resident;
};

static void
tc_call_make_image_handle_resident(struct pipe_context *pipe,
                                     union tc_payload *payload)
{
   struct tc_make_image_handle_resident *p =
      (struct tc_make_image_handle_resident *)payload;

   pipe->make_image_handle_resident(pipe, p->handle, p->access, p->resident);
}

static void
tc_make_image_handle_resident(struct pipe_context *_pipe, uint64_t handle,
                              unsigned access, bool resident)
{
   struct threaded_context *tc = threaded_context(_pipe);
   struct tc_make_image_handle_resident *p =
      tc_add_struct_typed_call(tc, TC_CALL_make_image_handle_resident,
                               tc_make_image_handle_resident);

   p->handle = handle;
   p->access = access;
   p->resident = resident;
}


1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
/********************************************************************
 * transfer
 */

struct tc_replace_buffer_storage {
   struct pipe_resource *dst;
   struct pipe_resource *src;
   tc_replace_buffer_storage_func func;
};

static void
tc_call_replace_buffer_storage(struct pipe_context *pipe,
                               union tc_payload *payload)
{
   struct tc_replace_buffer_storage *p =
      (struct tc_replace_buffer_storage *)payload;

   p->func(pipe, p->dst, p->src);
   pipe_resource_reference(&p->dst, NULL);
   pipe_resource_reference(&p->src, NULL);
}

static bool
tc_invalidate_buffer(struct threaded_context *tc,
                     struct threaded_resource *tbuf)
{
   /* We can't check if the buffer is idle, so we invalidate it
    * unconditionally. */
   struct pipe_screen *screen = tc->base.screen;
   struct pipe_resource *new_buf;

   /* Shared, pinned, and sparse buffers can't be reallocated. */
   if (tbuf->is_shared ||
       tbuf->is_user_ptr ||
       tbuf->b.flags & PIPE_RESOURCE_FLAG_SPARSE)
      return false;

   /* Allocate a new one. */
   new_buf = screen->resource_create(screen, &tbuf->b);
   if (!new_buf)
      return false;

   /* Replace the "latest" pointer. */
   if (tbuf->latest != &tbuf->b)
      pipe_resource_reference(&tbuf->latest, NULL);

   tbuf->latest = new_buf;
   util_range_set_empty(&tbuf->valid_buffer_range);

   /* The valid range should point to the original buffer. */
   threaded_resource(new_buf)->base_valid_buffer_range =
      &tbuf->valid_buffer_range;

   /* Enqueue storage replacement of the original buffer. */
   struct tc_replace_buffer_storage *p =
      tc_add_struct_typed_call(tc, TC_CALL_replace_buffer_storage,
                               tc_replace_buffer_storage);

   p->func = tc->replace_buffer_storage;
   tc_set_resource_reference(&p->dst, &tbuf->b);
   tc_set_resource_reference(&p->src, new_buf);
   return true;
}

static unsigned
tc_improve_map_buffer_flags(struct threaded_context *tc,
                            struct threaded_resource *tres, unsigned usage,
                            unsigned offset, unsigned size)
{
1450 1451 1452 1453 1454 1455 1456 1457
   /* Never invalidate inside the driver and never infer "unsynchronized". */
   unsigned tc_flags = TC_TRANSFER_MAP_NO_INVALIDATE |
                       TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED;

   /* Prevent a reentry. */
   if (usage & tc_flags)
      return usage;

1458
   /* Use the staging upload if it's preferred. */
1459 1460 1461
   if (usage & (PIPE_MAP_DISCARD_RANGE |
                PIPE_MAP_DISCARD_WHOLE_RESOURCE) &&
       !(usage & PIPE_MAP_PERSISTENT) &&
1462 1463 1464 1465
       /* Try not to decrement the counter if it's not positive. Still racy,
        * but it makes it harder to wrap the counter from INT_MIN to INT_MAX. */
       tres->max_forced_staging_uploads > 0 &&
       p_atomic_dec_return(&tres->max_forced_staging_uploads) >= 0) {
1466 1467
      usage &= ~(PIPE_MAP_DISCARD_WHOLE_RESOURCE |
                 PIPE_MAP_UNSYNCHRONIZED);
1468

1469
      return usage | tc_flags | PIPE_MAP_DISCARD_RANGE;
1470 1471
   }

1472 1473 1474 1475 1476 1477 1478 1479
   /* Sparse buffers can't be mapped directly and can't be reallocated
    * (fully invalidated). That may just be a radeonsi limitation, but
    * the threaded context must obey it with radeonsi.
    */
   if (tres->b.flags & PIPE_RESOURCE_FLAG_SPARSE) {
      /* We can use DISCARD_RANGE instead of full discard. This is the only
       * fast path for sparse buffers that doesn't need thread synchronization.
       */
1480 1481
      if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE)
         usage |= PIPE_MAP_DISCARD_RANGE;
1482 1483 1484 1485 1486 1487 1488 1489 1490

      /* Allow DISCARD_WHOLE_RESOURCE and infering UNSYNCHRONIZED in drivers.
       * The threaded context doesn't do unsychronized mappings and invalida-
       * tions of sparse buffers, therefore a correct driver behavior won't
       * result in an incorrect behavior with the threaded context.
       */
      return usage;
   }

1491 1492
   usage |= tc_flags;

1493
   /* Handle CPU reads trivially. */
1494 1495
   if (usage & PIPE_MAP_READ) {
      if (usage & PIPE_MAP_UNSYNCHRONIZED)
1496 1497
         usage |= TC_TRANSFER_MAP_THREADED_UNSYNC; /* don't sync */

Marek Olšák's avatar
Marek Olšák committed
1498
      /* Drivers aren't allowed to do buffer invalidations. */
1499
      return usage & ~PIPE_MAP_DISCARD_WHOLE_RESOURCE;
1500 1501 1502 1503
   }

   /* See if the buffer range being mapped has never been initialized,
    * in which case it can be mapped unsynchronized. */
1504
   if (!(usage & PIPE_MAP_UNSYNCHRONIZED) &&
1505 1506
       !tres->is_shared &&
       !util_ranges_intersect(&tres->valid_buffer_range, offset, offset + size))
1507
      usage |= PIPE_MAP_UNSYNCHRONIZED;
1508

1509
   if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
1510
      /* If discarding the entire range, discard the whole resource instead. */
1511
      if (usage & PIPE_MAP_DISCARD_RANGE &&
1512
          offset == 0 && size == tres->b.width0)
1513
         usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
1514 1515

      /* Discard the whole resource if needed. */
1516
      if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {
1517
         if (tc_invalidate_buffer(tc, tres))
1518
            usage |= PIPE_MAP_UNSYNCHRONIZED;
1519
         else
1520
            usage |= PIPE_MAP_DISCARD_RANGE; /* fallback */
1521 1522 1523 1524 1525
      }
   }

   /* We won't need this flag anymore. */
   /* TODO: We might not need TC_TRANSFER_MAP_NO_INVALIDATE with this. */
1526
   usage &= ~PIPE_MAP_DISCARD_WHOLE_RESOURCE;
1527 1528 1529

   /* GL_AMD_pinned_memory and persistent mappings can't use staging
    * buffers. */
1530 1531
   if (usage & (PIPE_MAP_UNSYNCHRONIZED |
                PIPE_MAP_PERSISTENT) ||
1532
       tres->is_user_ptr)
1533
      usage &= ~PIPE_MAP_DISCARD_RANGE;
1534 1535

   /* Unsychronized buffer mappings don't have to synchronize the thread. */
1536 1537
   if (usage & PIPE_MAP_UNSYNCHRONIZED) {
      usage &= ~PIPE_MAP_DISCARD_RANGE;
1538
      usage |= TC_TRANSFER_MAP_THREADED_UNSYNC; /* notify the driver */
1539
   }
1540

1541
   return usage;