1 /**************************************************************************
3 * Copyright 2017 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
27 #include "util/u_threaded_context.h"
28 #include "util/u_cpu_detect.h"
29 #include "util/u_format.h"
30 #include "util/u_inlines.h"
31 #include "util/u_memory.h"
32 #include "util/u_upload_mgr.h"
34 /* 0 = disabled, 1 = assertions, 2 = printfs */
38 #define tc_assert assert
44 #define tc_printf printf
45 #define tc_asprintf asprintf
46 #define tc_strcmp strcmp
48 #define tc_printf(...)
49 #define tc_asprintf(...) 0
50 #define tc_strcmp(...) 0
53 #define TC_SENTINEL 0x5ca1ab1e
56 #define CALL(name) TC_CALL_##name,
57 #include "u_threaded_context_calls.h"
62 typedef void (*tc_execute
)(struct pipe_context
*pipe
, union tc_payload
*payload
);
64 static const tc_execute execute_func
[TC_NUM_CALLS
];
67 tc_batch_check(struct tc_batch
*batch
)
69 tc_assert(batch
->sentinel
== TC_SENTINEL
);
70 tc_assert(batch
->sentinel2
== TC_SENTINEL
);
71 tc_assert(batch
->num_total_call_slots
<= TC_CALLS_PER_BATCH
);
75 tc_debug_check(struct threaded_context
*tc
)
77 for (unsigned i
= 0; i
< TC_MAX_BATCHES
; i
++) {
78 tc_batch_check(&tc
->batch_slots
[i
]);
79 tc_assert(tc
->batch_slots
[i
].pipe
== tc
->pipe
);
84 tc_batch_execute(void *job
, int thread_index
)
86 struct tc_batch
*batch
= job
;
87 struct pipe_context
*pipe
= batch
->pipe
;
88 struct tc_call
*last
= &batch
->call
[batch
->num_total_call_slots
];
90 tc_batch_check(batch
);
92 for (struct tc_call
*iter
= batch
->call
; iter
!= last
;
93 iter
+= iter
->num_call_slots
) {
94 tc_assert(iter
->sentinel
== TC_SENTINEL
);
95 execute_func
[iter
->call_id
](pipe
, &iter
->payload
);
98 tc_batch_check(batch
);
99 batch
->num_total_call_slots
= 0;
103 tc_batch_flush(struct threaded_context
*tc
)
105 struct tc_batch
*next
= &tc
->batch_slots
[tc
->next
];
107 tc_assert(next
->num_total_call_slots
!= 0);
108 tc_batch_check(next
);
110 p_atomic_add(&tc
->num_offloaded_slots
, next
->num_total_call_slots
);
112 util_queue_add_job(&tc
->queue
, next
, &next
->fence
, tc_batch_execute
,
115 tc
->next
= (tc
->next
+ 1) % TC_MAX_BATCHES
;
118 /* This is the function that adds variable-sized calls into the current
119 * batch. It also flushes the batch if there is not enough space there.
120 * All other higher-level "add" functions use it.
122 static union tc_payload
*
123 tc_add_sized_call(struct threaded_context
*tc
, enum tc_call_id id
,
124 unsigned payload_size
)
126 struct tc_batch
*next
= &tc
->batch_slots
[tc
->next
];
127 unsigned total_size
= offsetof(struct tc_call
, payload
) + payload_size
;
128 unsigned num_call_slots
= DIV_ROUND_UP(total_size
, sizeof(struct tc_call
));
132 if (unlikely(next
->num_total_call_slots
+ num_call_slots
> TC_CALLS_PER_BATCH
)) {
134 next
= &tc
->batch_slots
[tc
->next
];
135 tc_assert(next
->num_total_call_slots
== 0);
138 tc_assert(util_queue_fence_is_signalled(&next
->fence
));
140 struct tc_call
*call
= &next
->call
[next
->num_total_call_slots
];
141 next
->num_total_call_slots
+= num_call_slots
;
143 call
->sentinel
= TC_SENTINEL
;
145 call
->num_call_slots
= num_call_slots
;
148 return &call
->payload
;
151 #define tc_add_struct_typed_call(tc, execute, type) \
152 ((struct type*)tc_add_sized_call(tc, execute, sizeof(struct type)))
154 #define tc_add_slot_based_call(tc, execute, type, num_slots) \
155 ((struct type*)tc_add_sized_call(tc, execute, \
156 sizeof(struct type) + \
157 sizeof(((struct type*)NULL)->slot[0]) * \
160 static union tc_payload
*
161 tc_add_small_call(struct threaded_context
*tc
, enum tc_call_id id
)
163 return tc_add_sized_call(tc
, id
, 0);
167 _tc_sync(struct threaded_context
*tc
, const char *info
, const char *func
)
169 struct tc_batch
*last
= &tc
->batch_slots
[tc
->last
];
170 struct tc_batch
*next
= &tc
->batch_slots
[tc
->next
];
175 /* Only wait for queued calls... */
176 if (!util_queue_fence_is_signalled(&last
->fence
)) {
177 util_queue_fence_wait(&last
->fence
);
183 /* .. and execute unflushed calls directly. */
184 if (next
->num_total_call_slots
) {
185 p_atomic_add(&tc
->num_direct_slots
, next
->num_total_call_slots
);
186 tc_batch_execute(next
, 0);
191 p_atomic_inc(&tc
->num_syncs
);
193 if (tc_strcmp(func
, "tc_destroy") != 0)
194 tc_printf("sync %s %s\n", func
, info
);
200 #define tc_sync(tc) _tc_sync(tc, "", __func__)
201 #define tc_sync_msg(tc, info) _tc_sync(tc, info, __func__)
204 tc_set_resource_reference(struct pipe_resource
**dst
, struct pipe_resource
*src
)
207 pipe_resource_reference(dst
, src
);
211 threaded_resource_init(struct pipe_resource
*res
)
213 struct threaded_resource
*tres
= threaded_resource(res
);
215 tres
->latest
= &tres
->b
;
216 util_range_init(&tres
->valid_buffer_range
);
217 tres
->base_valid_buffer_range
= &tres
->valid_buffer_range
;
218 tres
->is_shared
= false;
219 tres
->is_user_ptr
= false;
223 threaded_resource_deinit(struct pipe_resource
*res
)
225 struct threaded_resource
*tres
= threaded_resource(res
);
227 if (tres
->latest
!= &tres
->b
)
228 pipe_resource_reference(&tres
->latest
, NULL
);
229 util_range_destroy(&tres
->valid_buffer_range
);
232 struct pipe_context
*
233 threaded_context_unwrap_sync(struct pipe_context
*pipe
)
235 if (!pipe
|| !pipe
->priv
)
238 tc_sync(threaded_context(pipe
));
239 return (struct pipe_context
*)pipe
->priv
;
243 /********************************************************************
247 #define TC_FUNC1(func, m_payload, qualifier, type, deref, deref2) \
249 tc_call_##func(struct pipe_context *pipe, union tc_payload *payload) \
251 pipe->func(pipe, deref2((type*)payload)); \
255 tc_##func(struct pipe_context *_pipe, qualifier type deref param) \
257 struct threaded_context *tc = threaded_context(_pipe); \
258 type *p = (type*)tc_add_sized_call(tc, TC_CALL_##func, sizeof(type)); \
262 TC_FUNC1(set_active_query_state
, flags
, , boolean
, , *)
264 TC_FUNC1(set_blend_color
, blend_color
, const, struct pipe_blend_color
, *, )
265 TC_FUNC1(set_stencil_ref
, stencil_ref
, const, struct pipe_stencil_ref
, *, )
266 TC_FUNC1(set_clip_state
, clip_state
, const, struct pipe_clip_state
, *, )
267 TC_FUNC1(set_sample_mask
, sample_mask
, , unsigned, , *)
268 TC_FUNC1(set_min_samples
, min_samples
, , unsigned, , *)
269 TC_FUNC1(set_polygon_stipple
, polygon_stipple
, const, struct pipe_poly_stipple
, *, )
271 TC_FUNC1(texture_barrier
, flags
, , unsigned, , *)
272 TC_FUNC1(memory_barrier
, flags
, , unsigned, , *)
275 /********************************************************************
279 static struct pipe_query
*
280 tc_create_query(struct pipe_context
*_pipe
, unsigned query_type
,
283 struct threaded_context
*tc
= threaded_context(_pipe
);
284 struct pipe_context
*pipe
= tc
->pipe
;
286 return pipe
->create_query(pipe
, query_type
, index
);
289 static struct pipe_query
*
290 tc_create_batch_query(struct pipe_context
*_pipe
, unsigned num_queries
,
291 unsigned *query_types
)
293 struct threaded_context
*tc
= threaded_context(_pipe
);
294 struct pipe_context
*pipe
= tc
->pipe
;
296 return pipe
->create_batch_query(pipe
, num_queries
, query_types
);
300 tc_call_destroy_query(struct pipe_context
*pipe
, union tc_payload
*payload
)
302 pipe
->destroy_query(pipe
, payload
->query
);
306 tc_destroy_query(struct pipe_context
*_pipe
, struct pipe_query
*query
)
308 struct threaded_context
*tc
= threaded_context(_pipe
);
309 struct threaded_query
*tq
= threaded_query(query
);
311 if (tq
->head_unflushed
.next
)
312 LIST_DEL(&tq
->head_unflushed
);
314 tc_add_small_call(tc
, TC_CALL_destroy_query
)->query
= query
;
318 tc_call_begin_query(struct pipe_context
*pipe
, union tc_payload
*payload
)
320 pipe
->begin_query(pipe
, payload
->query
);
324 tc_begin_query(struct pipe_context
*_pipe
, struct pipe_query
*query
)
326 struct threaded_context
*tc
= threaded_context(_pipe
);
327 union tc_payload
*payload
= tc_add_small_call(tc
, TC_CALL_begin_query
);
329 payload
->query
= query
;
330 return true; /* we don't care about the return value for this call */
334 tc_call_end_query(struct pipe_context
*pipe
, union tc_payload
*payload
)
336 pipe
->end_query(pipe
, payload
->query
);
340 tc_end_query(struct pipe_context
*_pipe
, struct pipe_query
*query
)
342 struct threaded_context
*tc
= threaded_context(_pipe
);
343 struct threaded_query
*tq
= threaded_query(query
);
344 union tc_payload
*payload
= tc_add_small_call(tc
, TC_CALL_end_query
);
346 payload
->query
= query
;
349 if (!tq
->head_unflushed
.next
)
350 LIST_ADD(&tq
->head_unflushed
, &tc
->unflushed_queries
);
352 return true; /* we don't care about the return value for this call */
356 tc_get_query_result(struct pipe_context
*_pipe
,
357 struct pipe_query
*query
, boolean wait
,
358 union pipe_query_result
*result
)
360 struct threaded_context
*tc
= threaded_context(_pipe
);
361 struct threaded_query
*tq
= threaded_query(query
);
362 struct pipe_context
*pipe
= tc
->pipe
;
365 tc_sync_msg(tc
, wait
? "wait" : "nowait");
367 bool success
= pipe
->get_query_result(pipe
, query
, wait
, result
);
371 if (tq
->head_unflushed
.next
)
372 LIST_DEL(&tq
->head_unflushed
);
377 struct tc_query_result_resource
{
378 struct pipe_query
*query
;
380 enum pipe_query_value_type result_type
;
382 struct pipe_resource
*resource
;
387 tc_call_get_query_result_resource(struct pipe_context
*pipe
,
388 union tc_payload
*payload
)
390 struct tc_query_result_resource
*p
= (struct tc_query_result_resource
*)payload
;
392 pipe
->get_query_result_resource(pipe
, p
->query
, p
->wait
, p
->result_type
,
393 p
->index
, p
->resource
, p
->offset
);
394 pipe_resource_reference(&p
->resource
, NULL
);
398 tc_get_query_result_resource(struct pipe_context
*_pipe
,
399 struct pipe_query
*query
, boolean wait
,
400 enum pipe_query_value_type result_type
, int index
,
401 struct pipe_resource
*resource
, unsigned offset
)
403 struct threaded_context
*tc
= threaded_context(_pipe
);
404 struct tc_query_result_resource
*p
=
405 tc_add_struct_typed_call(tc
, TC_CALL_get_query_result_resource
,
406 tc_query_result_resource
);
410 p
->result_type
= result_type
;
412 tc_set_resource_reference(&p
->resource
, resource
);
416 struct tc_render_condition
{
417 struct pipe_query
*query
;
423 tc_call_render_condition(struct pipe_context
*pipe
, union tc_payload
*payload
)
425 struct tc_render_condition
*p
= (struct tc_render_condition
*)payload
;
426 pipe
->render_condition(pipe
, p
->query
, p
->condition
, p
->mode
);
430 tc_render_condition(struct pipe_context
*_pipe
,
431 struct pipe_query
*query
, boolean condition
,
434 struct threaded_context
*tc
= threaded_context(_pipe
);
435 struct tc_render_condition
*p
=
436 tc_add_struct_typed_call(tc
, TC_CALL_render_condition
, tc_render_condition
);
439 p
->condition
= condition
;
444 /********************************************************************
445 * constant (immutable) states
448 #define TC_CSO_CREATE(name, sname) \
450 tc_create_##name##_state(struct pipe_context *_pipe, \
451 const struct pipe_##sname##_state *state) \
453 struct pipe_context *pipe = threaded_context(_pipe)->pipe; \
454 return pipe->create_##name##_state(pipe, state); \
457 #define TC_CSO_BIND(name) TC_FUNC1(bind_##name##_state, cso, , void *, , *)
458 #define TC_CSO_DELETE(name) TC_FUNC1(delete_##name##_state, cso, , void *, , *)
460 #define TC_CSO_WHOLE2(name, sname) \
461 TC_CSO_CREATE(name, sname) \
465 #define TC_CSO_WHOLE(name) TC_CSO_WHOLE2(name, name)
468 TC_CSO_WHOLE(rasterizer
)
469 TC_CSO_WHOLE(depth_stencil_alpha
)
470 TC_CSO_WHOLE(compute
)
471 TC_CSO_WHOLE2(fs
, shader
)
472 TC_CSO_WHOLE2(vs
, shader
)
473 TC_CSO_WHOLE2(gs
, shader
)
474 TC_CSO_WHOLE2(tcs
, shader
)
475 TC_CSO_WHOLE2(tes
, shader
)
476 TC_CSO_CREATE(sampler
, sampler
)
477 TC_CSO_DELETE(sampler
)
478 TC_CSO_BIND(vertex_elements
)
479 TC_CSO_DELETE(vertex_elements
)
482 tc_create_vertex_elements_state(struct pipe_context
*_pipe
, unsigned count
,
483 const struct pipe_vertex_element
*elems
)
485 struct pipe_context
*pipe
= threaded_context(_pipe
)->pipe
;
487 return pipe
->create_vertex_elements_state(pipe
, count
, elems
);
490 struct tc_sampler_states
{
491 ubyte shader
, start
, count
;
492 void *slot
[0]; /* more will be allocated if needed */
496 tc_call_bind_sampler_states(struct pipe_context
*pipe
, union tc_payload
*payload
)
498 struct tc_sampler_states
*p
= (struct tc_sampler_states
*)payload
;
499 pipe
->bind_sampler_states(pipe
, p
->shader
, p
->start
, p
->count
, p
->slot
);
503 tc_bind_sampler_states(struct pipe_context
*_pipe
,
504 enum pipe_shader_type shader
,
505 unsigned start
, unsigned count
, void **states
)
510 struct threaded_context
*tc
= threaded_context(_pipe
);
511 struct tc_sampler_states
*p
=
512 tc_add_slot_based_call(tc
, TC_CALL_bind_sampler_states
, tc_sampler_states
, count
);
517 memcpy(p
->slot
, states
, count
* sizeof(states
[0]));
521 /********************************************************************
526 tc_call_set_framebuffer_state(struct pipe_context
*pipe
, union tc_payload
*payload
)
528 struct pipe_framebuffer_state
*p
= (struct pipe_framebuffer_state
*)payload
;
530 pipe
->set_framebuffer_state(pipe
, p
);
532 unsigned nr_cbufs
= p
->nr_cbufs
;
533 for (unsigned i
= 0; i
< nr_cbufs
; i
++)
534 pipe_surface_reference(&p
->cbufs
[i
], NULL
);
535 pipe_surface_reference(&p
->zsbuf
, NULL
);
539 tc_set_framebuffer_state(struct pipe_context
*_pipe
,
540 const struct pipe_framebuffer_state
*fb
)
542 struct threaded_context
*tc
= threaded_context(_pipe
);
543 struct pipe_framebuffer_state
*p
=
544 tc_add_struct_typed_call(tc
, TC_CALL_set_framebuffer_state
,
545 pipe_framebuffer_state
);
546 unsigned nr_cbufs
= fb
->nr_cbufs
;
548 p
->width
= fb
->width
;
549 p
->height
= fb
->height
;
550 p
->samples
= fb
->samples
;
551 p
->layers
= fb
->layers
;
552 p
->nr_cbufs
= nr_cbufs
;
554 for (unsigned i
= 0; i
< nr_cbufs
; i
++) {
556 pipe_surface_reference(&p
->cbufs
[i
], fb
->cbufs
[i
]);
559 pipe_surface_reference(&p
->zsbuf
, fb
->zsbuf
);
563 tc_call_set_tess_state(struct pipe_context
*pipe
, union tc_payload
*payload
)
565 float *p
= (float*)payload
;
566 pipe
->set_tess_state(pipe
, p
, p
+ 4);
570 tc_set_tess_state(struct pipe_context
*_pipe
,
571 const float default_outer_level
[4],
572 const float default_inner_level
[2])
574 struct threaded_context
*tc
= threaded_context(_pipe
);
575 float *p
= (float*)tc_add_sized_call(tc
, TC_CALL_set_tess_state
,
578 memcpy(p
, default_outer_level
, 4 * sizeof(float));
579 memcpy(p
+ 4, default_inner_level
, 2 * sizeof(float));
582 struct tc_constant_buffer
{
584 struct pipe_constant_buffer cb
;
588 tc_call_set_constant_buffer(struct pipe_context
*pipe
, union tc_payload
*payload
)
590 struct tc_constant_buffer
*p
= (struct tc_constant_buffer
*)payload
;
592 pipe
->set_constant_buffer(pipe
,
596 pipe_resource_reference(&p
->cb
.buffer
, NULL
);
600 tc_set_constant_buffer(struct pipe_context
*_pipe
,
601 uint shader
, uint index
,
602 const struct pipe_constant_buffer
*cb
)
604 struct threaded_context
*tc
= threaded_context(_pipe
);
605 struct pipe_resource
*buffer
= NULL
;
608 /* This must be done before adding set_constant_buffer, because it could
609 * generate e.g. transfer_unmap and flush partially-uninitialized
610 * set_constant_buffer to the driver if it was done afterwards.
612 if (cb
&& cb
->user_buffer
) {
613 u_upload_data(tc
->base
.const_uploader
, 0, cb
->buffer_size
, 64,
614 cb
->user_buffer
, &offset
, &buffer
);
617 struct tc_constant_buffer
*p
=
618 tc_add_struct_typed_call(tc
, TC_CALL_set_constant_buffer
,
624 if (cb
->user_buffer
) {
625 p
->cb
.buffer_size
= cb
->buffer_size
;
626 p
->cb
.user_buffer
= NULL
;
627 p
->cb
.buffer_offset
= offset
;
628 p
->cb
.buffer
= buffer
;
630 tc_set_resource_reference(&p
->cb
.buffer
,
632 memcpy(&p
->cb
, cb
, sizeof(*cb
));
635 memset(&p
->cb
, 0, sizeof(*cb
));
641 struct pipe_scissor_state slot
[0]; /* more will be allocated if needed */
645 tc_call_set_scissor_states(struct pipe_context
*pipe
, union tc_payload
*payload
)
647 struct tc_scissors
*p
= (struct tc_scissors
*)payload
;
648 pipe
->set_scissor_states(pipe
, p
->start
, p
->count
, p
->slot
);
652 tc_set_scissor_states(struct pipe_context
*_pipe
,
653 unsigned start
, unsigned count
,
654 const struct pipe_scissor_state
*states
)
656 struct threaded_context
*tc
= threaded_context(_pipe
);
657 struct tc_scissors
*p
=
658 tc_add_slot_based_call(tc
, TC_CALL_set_scissor_states
, tc_scissors
, count
);
662 memcpy(&p
->slot
, states
, count
* sizeof(states
[0]));
665 struct tc_viewports
{
667 struct pipe_viewport_state slot
[0]; /* more will be allocated if needed */
671 tc_call_set_viewport_states(struct pipe_context
*pipe
, union tc_payload
*payload
)
673 struct tc_viewports
*p
= (struct tc_viewports
*)payload
;
674 pipe
->set_viewport_states(pipe
, p
->start
, p
->count
, p
->slot
);
678 tc_set_viewport_states(struct pipe_context
*_pipe
,
679 unsigned start
, unsigned count
,
680 const struct pipe_viewport_state
*states
)
685 struct threaded_context
*tc
= threaded_context(_pipe
);
686 struct tc_viewports
*p
=
687 tc_add_slot_based_call(tc
, TC_CALL_set_viewport_states
, tc_viewports
, count
);
691 memcpy(&p
->slot
, states
, count
* sizeof(states
[0]));
694 struct tc_window_rects
{
697 struct pipe_scissor_state slot
[0]; /* more will be allocated if needed */
701 tc_call_set_window_rectangles(struct pipe_context
*pipe
,
702 union tc_payload
*payload
)
704 struct tc_window_rects
*p
= (struct tc_window_rects
*)payload
;
705 pipe
->set_window_rectangles(pipe
, p
->include
, p
->count
, p
->slot
);
709 tc_set_window_rectangles(struct pipe_context
*_pipe
, boolean include
,
711 const struct pipe_scissor_state
*rects
)
713 struct threaded_context
*tc
= threaded_context(_pipe
);
714 struct tc_window_rects
*p
=
715 tc_add_slot_based_call(tc
, TC_CALL_set_window_rectangles
, tc_window_rects
, count
);
717 p
->include
= include
;
719 memcpy(p
->slot
, rects
, count
* sizeof(rects
[0]));
722 struct tc_sampler_views
{
723 ubyte shader
, start
, count
;
724 struct pipe_sampler_view
*slot
[0]; /* more will be allocated if needed */
728 tc_call_set_sampler_views(struct pipe_context
*pipe
, union tc_payload
*payload
)
730 struct tc_sampler_views
*p
= (struct tc_sampler_views
*)payload
;
731 unsigned count
= p
->count
;
733 pipe
->set_sampler_views(pipe
, p
->shader
, p
->start
, p
->count
, p
->slot
);
734 for (unsigned i
= 0; i
< count
; i
++)
735 pipe_sampler_view_reference(&p
->slot
[i
], NULL
);
739 tc_set_sampler_views(struct pipe_context
*_pipe
,
740 enum pipe_shader_type shader
,
741 unsigned start
, unsigned count
,
742 struct pipe_sampler_view
**views
)
747 struct threaded_context
*tc
= threaded_context(_pipe
);
748 struct tc_sampler_views
*p
=
749 tc_add_slot_based_call(tc
, TC_CALL_set_sampler_views
, tc_sampler_views
, count
);
756 for (unsigned i
= 0; i
< count
; i
++) {
758 pipe_sampler_view_reference(&p
->slot
[i
], views
[i
]);
761 memset(p
->slot
, 0, count
* sizeof(views
[0]));
765 struct tc_shader_images
{
766 ubyte shader
, start
, count
;
767 struct pipe_image_view slot
[0]; /* more will be allocated if needed */
771 tc_call_set_shader_images(struct pipe_context
*pipe
, union tc_payload
*payload
)
773 struct tc_shader_images
*p
= (struct tc_shader_images
*)payload
;
774 unsigned count
= p
->count
;
776 pipe
->set_shader_images(pipe
, p
->shader
, p
->start
, p
->count
, p
->slot
);
778 for (unsigned i
= 0; i
< count
; i
++)
779 pipe_resource_reference(&p
->slot
[i
].resource
, NULL
);
783 tc_set_shader_images(struct pipe_context
*_pipe
,
784 enum pipe_shader_type shader
,
785 unsigned start
, unsigned count
,
786 const struct pipe_image_view
*images
)
791 struct threaded_context
*tc
= threaded_context(_pipe
);
792 struct tc_shader_images
*p
=
793 tc_add_slot_based_call(tc
, TC_CALL_set_shader_images
, tc_shader_images
, count
);
800 for (unsigned i
= 0; i
< count
; i
++) {
801 tc_set_resource_reference(&p
->slot
[i
].resource
, images
[i
].resource
);
803 if (images
[i
].access
& PIPE_IMAGE_ACCESS_WRITE
&&
804 images
[i
].resource
&&
805 images
[i
].resource
->target
== PIPE_BUFFER
) {
806 struct threaded_resource
*tres
=
807 threaded_resource(images
[i
].resource
);
809 util_range_add(&tres
->valid_buffer_range
, images
[i
].u
.buf
.offset
,
810 images
[i
].u
.buf
.offset
+ images
[i
].u
.buf
.size
);
813 memcpy(p
->slot
, images
, count
* sizeof(images
[0]));
815 memset(p
->slot
, 0, count
* sizeof(images
[0]));
819 struct tc_shader_buffers
{
820 ubyte shader
, start
, count
;
821 struct pipe_shader_buffer slot
[0]; /* more will be allocated if needed */
825 tc_call_set_shader_buffers(struct pipe_context
*pipe
, union tc_payload
*payload
)
827 struct tc_shader_buffers
*p
= (struct tc_shader_buffers
*)payload
;
828 unsigned count
= p
->count
;
830 pipe
->set_shader_buffers(pipe
, p
->shader
, p
->start
, p
->count
, p
->slot
);
832 for (unsigned i
= 0; i
< count
; i
++)
833 pipe_resource_reference(&p
->slot
[i
].buffer
, NULL
);
837 tc_set_shader_buffers(struct pipe_context
*_pipe
, unsigned shader
,
838 unsigned start
, unsigned count
,
839 const struct pipe_shader_buffer
*buffers
)
844 struct threaded_context
*tc
= threaded_context(_pipe
);
845 struct tc_shader_buffers
*p
=
846 tc_add_slot_based_call(tc
, TC_CALL_set_shader_buffers
, tc_shader_buffers
, count
);
853 for (unsigned i
= 0; i
< count
; i
++) {
854 struct pipe_shader_buffer
*dst
= &p
->slot
[i
];
855 const struct pipe_shader_buffer
*src
= buffers
+ i
;
857 tc_set_resource_reference(&dst
->buffer
, src
->buffer
);
858 dst
->buffer_offset
= src
->buffer_offset
;
859 dst
->buffer_size
= src
->buffer_size
;
862 struct threaded_resource
*tres
= threaded_resource(src
->buffer
);
864 util_range_add(&tres
->valid_buffer_range
, src
->buffer_offset
,
865 src
->buffer_offset
+ src
->buffer_size
);
869 memset(p
->slot
, 0, count
* sizeof(buffers
[0]));
873 struct tc_vertex_buffers
{
876 struct pipe_vertex_buffer slot
[0]; /* more will be allocated if needed */
880 tc_call_set_vertex_buffers(struct pipe_context
*pipe
, union tc_payload
*payload
)
882 struct tc_vertex_buffers
*p
= (struct tc_vertex_buffers
*)payload
;
883 unsigned count
= p
->count
;
886 pipe
->set_vertex_buffers(pipe
, p
->start
, count
, NULL
);
890 for (unsigned i
= 0; i
< count
; i
++)
891 tc_assert(!p
->slot
[i
].is_user_buffer
);
893 pipe
->set_vertex_buffers(pipe
, p
->start
, count
, p
->slot
);
894 for (unsigned i
= 0; i
< count
; i
++)
895 pipe_resource_reference(&p
->slot
[i
].buffer
.resource
, NULL
);
899 tc_set_vertex_buffers(struct pipe_context
*_pipe
,
900 unsigned start
, unsigned count
,
901 const struct pipe_vertex_buffer
*buffers
)
903 struct threaded_context
*tc
= threaded_context(_pipe
);
909 struct tc_vertex_buffers
*p
=
910 tc_add_slot_based_call(tc
, TC_CALL_set_vertex_buffers
, tc_vertex_buffers
, count
);
915 for (unsigned i
= 0; i
< count
; i
++) {
916 struct pipe_vertex_buffer
*dst
= &p
->slot
[i
];
917 const struct pipe_vertex_buffer
*src
= buffers
+ i
;
919 tc_assert(!src
->is_user_buffer
);
920 dst
->stride
= src
->stride
;
921 dst
->is_user_buffer
= false;
922 tc_set_resource_reference(&dst
->buffer
.resource
,
923 src
->buffer
.resource
);
924 dst
->buffer_offset
= src
->buffer_offset
;
927 struct tc_vertex_buffers
*p
=
928 tc_add_slot_based_call(tc
, TC_CALL_set_vertex_buffers
, tc_vertex_buffers
, 0);
935 struct tc_stream_outputs
{
937 struct pipe_stream_output_target
*targets
[PIPE_MAX_SO_BUFFERS
];
938 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
942 tc_call_set_stream_output_targets(struct pipe_context
*pipe
, union tc_payload
*payload
)
944 struct tc_stream_outputs
*p
= (struct tc_stream_outputs
*)payload
;
945 unsigned count
= p
->count
;
947 pipe
->set_stream_output_targets(pipe
, count
, p
->targets
, p
->offsets
);
948 for (unsigned i
= 0; i
< count
; i
++)
949 pipe_so_target_reference(&p
->targets
[i
], NULL
);
953 tc_set_stream_output_targets(struct pipe_context
*_pipe
,
955 struct pipe_stream_output_target
**tgs
,
956 const unsigned *offsets
)
958 struct threaded_context
*tc
= threaded_context(_pipe
);
959 struct tc_stream_outputs
*p
=
960 tc_add_struct_typed_call(tc
, TC_CALL_set_stream_output_targets
,
963 for (unsigned i
= 0; i
< count
; i
++) {
964 p
->targets
[i
] = NULL
;
965 pipe_so_target_reference(&p
->targets
[i
], tgs
[i
]);
968 memcpy(p
->offsets
, offsets
, count
* sizeof(unsigned));
972 tc_set_compute_resources(struct pipe_context
*_pipe
, unsigned start
,
973 unsigned count
, struct pipe_surface
**resources
)
975 struct threaded_context
*tc
= threaded_context(_pipe
);
976 struct pipe_context
*pipe
= tc
->pipe
;
979 pipe
->set_compute_resources(pipe
, start
, count
, resources
);
983 tc_set_global_binding(struct pipe_context
*_pipe
, unsigned first
,
984 unsigned count
, struct pipe_resource
**resources
,
987 struct threaded_context
*tc
= threaded_context(_pipe
);
988 struct pipe_context
*pipe
= tc
->pipe
;
991 pipe
->set_global_binding(pipe
, first
, count
, resources
, handles
);
995 /********************************************************************
999 static struct pipe_surface
*
1000 tc_create_surface(struct pipe_context
*_pipe
,
1001 struct pipe_resource
*resource
,
1002 const struct pipe_surface
*surf_tmpl
)
1004 struct pipe_context
*pipe
= threaded_context(_pipe
)->pipe
;
1005 struct pipe_surface
*view
=
1006 pipe
->create_surface(pipe
, resource
, surf_tmpl
);
1009 view
->context
= _pipe
;
1014 tc_surface_destroy(struct pipe_context
*_pipe
,
1015 struct pipe_surface
*surf
)
1017 struct pipe_context
*pipe
= threaded_context(_pipe
)->pipe
;
1019 pipe
->surface_destroy(pipe
, surf
);
1022 static struct pipe_sampler_view
*
1023 tc_create_sampler_view(struct pipe_context
*_pipe
,
1024 struct pipe_resource
*resource
,
1025 const struct pipe_sampler_view
*templ
)
1027 struct pipe_context
*pipe
= threaded_context(_pipe
)->pipe
;
1028 struct pipe_sampler_view
*view
=
1029 pipe
->create_sampler_view(pipe
, resource
, templ
);
1032 view
->context
= _pipe
;
1037 tc_sampler_view_destroy(struct pipe_context
*_pipe
,
1038 struct pipe_sampler_view
*view
)
1040 struct pipe_context
*pipe
= threaded_context(_pipe
)->pipe
;
1042 pipe
->sampler_view_destroy(pipe
, view
);
1045 static struct pipe_stream_output_target
*
1046 tc_create_stream_output_target(struct pipe_context
*_pipe
,
1047 struct pipe_resource
*res
,
1048 unsigned buffer_offset
,
1049 unsigned buffer_size
)
1051 struct pipe_context
*pipe
= threaded_context(_pipe
)->pipe
;
1052 struct threaded_resource
*tres
= threaded_resource(res
);
1053 struct pipe_stream_output_target
*view
;
1055 tc_sync(threaded_context(_pipe
));
1056 util_range_add(&tres
->valid_buffer_range
, buffer_offset
,
1057 buffer_offset
+ buffer_size
);
1059 view
= pipe
->create_stream_output_target(pipe
, res
, buffer_offset
,
1062 view
->context
= _pipe
;
1067 tc_stream_output_target_destroy(struct pipe_context
*_pipe
,
1068 struct pipe_stream_output_target
*target
)
1070 struct pipe_context
*pipe
= threaded_context(_pipe
)->pipe
;
1072 pipe
->stream_output_target_destroy(pipe
, target
);
1076 /********************************************************************
1080 struct tc_replace_buffer_storage
{
1081 struct pipe_resource
*dst
;
1082 struct pipe_resource
*src
;
1083 tc_replace_buffer_storage_func func
;
1087 tc_call_replace_buffer_storage(struct pipe_context
*pipe
,
1088 union tc_payload
*payload
)
1090 struct tc_replace_buffer_storage
*p
=
1091 (struct tc_replace_buffer_storage
*)payload
;
1093 p
->func(pipe
, p
->dst
, p
->src
);
1094 pipe_resource_reference(&p
->dst
, NULL
);
1095 pipe_resource_reference(&p
->src
, NULL
);
1099 tc_invalidate_buffer(struct threaded_context
*tc
,
1100 struct threaded_resource
*tbuf
)
1102 /* We can't check if the buffer is idle, so we invalidate it
1103 * unconditionally. */
1104 struct pipe_screen
*screen
= tc
->base
.screen
;
1105 struct pipe_resource
*new_buf
;
1107 /* Shared, pinned, and sparse buffers can't be reallocated. */
1108 if (tbuf
->is_shared
||
1109 tbuf
->is_user_ptr
||
1110 tbuf
->b
.flags
& PIPE_RESOURCE_FLAG_SPARSE
)
1113 /* Allocate a new one. */
1114 new_buf
= screen
->resource_create(screen
, &tbuf
->b
);
1118 /* Replace the "latest" pointer. */
1119 if (tbuf
->latest
!= &tbuf
->b
)
1120 pipe_resource_reference(&tbuf
->latest
, NULL
);
1122 tbuf
->latest
= new_buf
;
1123 util_range_set_empty(&tbuf
->valid_buffer_range
);
1125 /* The valid range should point to the original buffer. */
1126 threaded_resource(new_buf
)->base_valid_buffer_range
=
1127 &tbuf
->valid_buffer_range
;
1129 /* Enqueue storage replacement of the original buffer. */
1130 struct tc_replace_buffer_storage
*p
=
1131 tc_add_struct_typed_call(tc
, TC_CALL_replace_buffer_storage
,
1132 tc_replace_buffer_storage
);
1134 p
->func
= tc
->replace_buffer_storage
;
1135 tc_set_resource_reference(&p
->dst
, &tbuf
->b
);
1136 tc_set_resource_reference(&p
->src
, new_buf
);
1141 tc_improve_map_buffer_flags(struct threaded_context
*tc
,
1142 struct threaded_resource
*tres
, unsigned usage
,
1143 unsigned offset
, unsigned size
)
1145 /* Sparse buffers can't be mapped directly and can't be reallocated
1146 * (fully invalidated). That may just be a radeonsi limitation, but
1147 * the threaded context must obey it with radeonsi.
1149 if (tres
->b
.flags
& PIPE_RESOURCE_FLAG_SPARSE
) {
1150 /* We can use DISCARD_RANGE instead of full discard. This is the only
1151 * fast path for sparse buffers that doesn't need thread synchronization.
1153 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
)
1154 usage
|= PIPE_TRANSFER_DISCARD_RANGE
;
1156 /* Allow DISCARD_WHOLE_RESOURCE and infering UNSYNCHRONIZED in drivers.
1157 * The threaded context doesn't do unsychronized mappings and invalida-
1158 * tions of sparse buffers, therefore a correct driver behavior won't
1159 * result in an incorrect behavior with the threaded context.
1164 /* Handle CPU reads trivially. */
1165 if (usage
& PIPE_TRANSFER_READ
) {
1166 /* Driver aren't allowed to do buffer invalidations. */
1167 return (usage
& ~PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) |
1168 TC_TRANSFER_MAP_NO_INVALIDATE
|
1169 TC_TRANSFER_MAP_IGNORE_VALID_RANGE
;
1172 /* See if the buffer range being mapped has never been initialized,
1173 * in which case it can be mapped unsynchronized. */
1174 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) &&
1176 !util_ranges_intersect(&tres
->valid_buffer_range
, offset
, offset
+ size
))
1177 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
1179 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
1180 /* If discarding the entire range, discard the whole resource instead. */
1181 if (usage
& PIPE_TRANSFER_DISCARD_RANGE
&&
1182 offset
== 0 && size
== tres
->b
.width0
)
1183 usage
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
1185 /* Discard the whole resource if needed. */
1186 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
1187 if (tc_invalidate_buffer(tc
, tres
))
1188 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
1190 usage
|= PIPE_TRANSFER_DISCARD_RANGE
; /* fallback */
1194 /* We won't need this flag anymore. */
1195 /* TODO: We might not need TC_TRANSFER_MAP_NO_INVALIDATE with this. */
1196 usage
&= ~PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
1198 /* GL_AMD_pinned_memory and persistent mappings can't use staging
1200 if (usage
& (PIPE_TRANSFER_UNSYNCHRONIZED
|
1201 PIPE_TRANSFER_PERSISTENT
) ||
1203 usage
&= ~PIPE_TRANSFER_DISCARD_RANGE
;
1205 /* Unsychronized buffer mappings don't have to synchronize the thread. */
1206 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)
1207 usage
|= TC_TRANSFER_MAP_THREADED_UNSYNC
; /* notify the driver */
1209 /* Never invalidate inside the driver and never infer "unsynchronized". */
1211 TC_TRANSFER_MAP_NO_INVALIDATE
|
1212 TC_TRANSFER_MAP_IGNORE_VALID_RANGE
;
1216 tc_transfer_map(struct pipe_context
*_pipe
,
1217 struct pipe_resource
*resource
, unsigned level
,
1218 unsigned usage
, const struct pipe_box
*box
,
1219 struct pipe_transfer
**transfer
)
1221 struct threaded_context
*tc
= threaded_context(_pipe
);
1222 struct threaded_resource
*tres
= threaded_resource(resource
);
1223 struct pipe_context
*pipe
= tc
->pipe
;
1225 if (resource
->target
== PIPE_BUFFER
) {
1226 usage
= tc_improve_map_buffer_flags(tc
, tres
, usage
, box
->x
, box
->width
);
1228 /* Do a staging transfer within the threaded context. The driver should
1229 * only get resource_copy_region.
1231 if (usage
& PIPE_TRANSFER_DISCARD_RANGE
) {
1232 struct threaded_transfer
*ttrans
= slab_alloc(&tc
->pool_transfers
);
1235 ttrans
->staging
= NULL
;
1237 u_upload_alloc(tc
->base
.stream_uploader
, 0,
1238 box
->width
+ (box
->x
% tc
->map_buffer_alignment
),
1239 64, &ttrans
->offset
, &ttrans
->staging
, (void**)&map
);
1241 slab_free(&tc
->pool_transfers
, ttrans
);
1245 tc_set_resource_reference(&ttrans
->b
.resource
, resource
);
1246 ttrans
->b
.level
= 0;
1247 ttrans
->b
.usage
= usage
;
1248 ttrans
->b
.box
= *box
;
1249 ttrans
->b
.stride
= 0;
1250 ttrans
->b
.layer_stride
= 0;
1251 *transfer
= &ttrans
->b
;
1252 return map
+ (box
->x
% tc
->map_buffer_alignment
);
1256 /* Unsychronized buffer mappings don't have to synchronize the thread. */
1257 if (!(usage
& TC_TRANSFER_MAP_THREADED_UNSYNC
))
1258 tc_sync_msg(tc
, resource
->target
!= PIPE_BUFFER
? " texture" :
1259 usage
& PIPE_TRANSFER_DISCARD_RANGE
? " discard_range" :
1260 usage
& PIPE_TRANSFER_READ
? " read" : " ??");
1262 return pipe
->transfer_map(pipe
, tres
->latest
? tres
->latest
: resource
,
1263 level
, usage
, box
, transfer
);
1266 struct tc_transfer_flush_region
{
1267 struct pipe_transfer
*transfer
;
1268 struct pipe_box box
;
1272 tc_call_transfer_flush_region(struct pipe_context
*pipe
,
1273 union tc_payload
*payload
)
1275 struct tc_transfer_flush_region
*p
=
1276 (struct tc_transfer_flush_region
*)payload
;
1278 pipe
->transfer_flush_region(pipe
, p
->transfer
, &p
->box
);
1281 struct tc_resource_copy_region
{
1282 struct pipe_resource
*dst
;
1284 unsigned dstx
, dsty
, dstz
;
1285 struct pipe_resource
*src
;
1287 struct pipe_box src_box
;
1291 tc_resource_copy_region(struct pipe_context
*_pipe
,
1292 struct pipe_resource
*dst
, unsigned dst_level
,
1293 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1294 struct pipe_resource
*src
, unsigned src_level
,
1295 const struct pipe_box
*src_box
);
1298 tc_buffer_do_flush_region(struct threaded_context
*tc
,
1299 struct threaded_transfer
*ttrans
,
1300 const struct pipe_box
*box
)
1302 struct threaded_resource
*tres
= threaded_resource(ttrans
->b
.resource
);
1304 if (ttrans
->staging
) {
1305 struct pipe_box src_box
;
1307 u_box_1d(ttrans
->offset
+ box
->x
% tc
->map_buffer_alignment
,
1308 box
->width
, &src_box
);
1310 /* Copy the staging buffer into the original one. */
1311 tc_resource_copy_region(&tc
->base
, ttrans
->b
.resource
, 0, box
->x
, 0, 0,
1312 ttrans
->staging
, 0, &src_box
);
1315 util_range_add(tres
->base_valid_buffer_range
, box
->x
, box
->x
+ box
->width
);
1319 tc_transfer_flush_region(struct pipe_context
*_pipe
,
1320 struct pipe_transfer
*transfer
,
1321 const struct pipe_box
*rel_box
)
1323 struct threaded_context
*tc
= threaded_context(_pipe
);
1324 struct threaded_transfer
*ttrans
= threaded_transfer(transfer
);
1325 struct threaded_resource
*tres
= threaded_resource(transfer
->resource
);
1326 unsigned required_usage
= PIPE_TRANSFER_WRITE
|
1327 PIPE_TRANSFER_FLUSH_EXPLICIT
;
1329 if (tres
->b
.target
== PIPE_BUFFER
) {
1330 if ((transfer
->usage
& required_usage
) == required_usage
) {
1331 struct pipe_box box
;
1333 u_box_1d(transfer
->box
.x
+ rel_box
->x
, rel_box
->width
, &box
);
1334 tc_buffer_do_flush_region(tc
, ttrans
, &box
);
1337 /* Staging transfers don't send the call to the driver. */
1338 if (ttrans
->staging
)
1342 struct tc_transfer_flush_region
*p
=
1343 tc_add_struct_typed_call(tc
, TC_CALL_transfer_flush_region
,
1344 tc_transfer_flush_region
);
1345 p
->transfer
= transfer
;
1350 tc_call_transfer_unmap(struct pipe_context
*pipe
, union tc_payload
*payload
)
1352 pipe
->transfer_unmap(pipe
, payload
->transfer
);
1356 tc_transfer_unmap(struct pipe_context
*_pipe
, struct pipe_transfer
*transfer
)
1358 struct threaded_context
*tc
= threaded_context(_pipe
);
1359 struct threaded_transfer
*ttrans
= threaded_transfer(transfer
);
1360 struct threaded_resource
*tres
= threaded_resource(transfer
->resource
);
1362 if (tres
->b
.target
== PIPE_BUFFER
) {
1363 if (transfer
->usage
& PIPE_TRANSFER_WRITE
&&
1364 !(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
))
1365 tc_buffer_do_flush_region(tc
, ttrans
, &transfer
->box
);
1367 /* Staging transfers don't send the call to the driver. */
1368 if (ttrans
->staging
) {
1369 pipe_resource_reference(&ttrans
->staging
, NULL
);
1370 pipe_resource_reference(&ttrans
->b
.resource
, NULL
);
1371 slab_free(&tc
->pool_transfers
, ttrans
);
1376 tc_add_small_call(tc
, TC_CALL_transfer_unmap
)->transfer
= transfer
;
1379 struct tc_buffer_subdata
{
1380 struct pipe_resource
*resource
;
1381 unsigned usage
, offset
, size
;
1382 char slot
[0]; /* more will be allocated if needed */
1386 tc_call_buffer_subdata(struct pipe_context
*pipe
, union tc_payload
*payload
)
1388 struct tc_buffer_subdata
*p
= (struct tc_buffer_subdata
*)payload
;
1390 pipe
->buffer_subdata(pipe
, p
->resource
, p
->usage
, p
->offset
, p
->size
,
1392 pipe_resource_reference(&p
->resource
, NULL
);
1396 tc_buffer_subdata(struct pipe_context
*_pipe
,
1397 struct pipe_resource
*resource
,
1398 unsigned usage
, unsigned offset
,
1399 unsigned size
, const void *data
)
1401 struct threaded_context
*tc
= threaded_context(_pipe
);
1402 struct threaded_resource
*tres
= threaded_resource(resource
);
1407 usage
|= PIPE_TRANSFER_WRITE
|
1408 PIPE_TRANSFER_DISCARD_RANGE
;
1410 usage
= tc_improve_map_buffer_flags(tc
, tres
, usage
, offset
, size
);
1412 /* Unsychronized and big transfers should use transfer_map. Also handle
1413 * full invalidations, because drivers aren't allowed to do them.
1415 if (usage
& (PIPE_TRANSFER_UNSYNCHRONIZED
|
1416 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) ||
1417 size
> TC_MAX_SUBDATA_BYTES
) {
1418 struct pipe_transfer
*transfer
;
1419 struct pipe_box box
;
1420 uint8_t *map
= NULL
;
1422 u_box_1d(offset
, size
, &box
);
1424 map
= tc_transfer_map(_pipe
, resource
, 0, usage
, &box
, &transfer
);
1426 memcpy(map
, data
, size
);
1427 tc_transfer_unmap(_pipe
, transfer
);
1432 util_range_add(&tres
->valid_buffer_range
, offset
, offset
+ size
);
1434 /* The upload is small. Enqueue it. */
1435 struct tc_buffer_subdata
*p
=
1436 tc_add_slot_based_call(tc
, TC_CALL_buffer_subdata
, tc_buffer_subdata
, size
);
1438 tc_set_resource_reference(&p
->resource
, resource
);
1442 memcpy(p
->slot
, data
, size
);
1445 struct tc_texture_subdata
{
1446 struct pipe_resource
*resource
;
1447 unsigned level
, usage
, stride
, layer_stride
;
1448 struct pipe_box box
;
1449 char slot
[0]; /* more will be allocated if needed */
1453 tc_call_texture_subdata(struct pipe_context
*pipe
, union tc_payload
*payload
)
1455 struct tc_texture_subdata
*p
= (struct tc_texture_subdata
*)payload
;
1457 pipe
->texture_subdata(pipe
, p
->resource
, p
->level
, p
->usage
, &p
->box
,
1458 p
->slot
, p
->stride
, p
->layer_stride
);
1459 pipe_resource_reference(&p
->resource
, NULL
);
1463 tc_texture_subdata(struct pipe_context
*_pipe
,
1464 struct pipe_resource
*resource
,
1465 unsigned level
, unsigned usage
,
1466 const struct pipe_box
*box
,
1467 const void *data
, unsigned stride
,
1468 unsigned layer_stride
)
1470 struct threaded_context
*tc
= threaded_context(_pipe
);
1473 assert(box
->height
>= 1);
1474 assert(box
->depth
>= 1);
1476 size
= (box
->depth
- 1) * layer_stride
+
1477 (box
->height
- 1) * stride
+
1478 box
->width
* util_format_get_blocksize(resource
->format
);
1482 /* Small uploads can be enqueued, big uploads must sync. */
1483 if (size
<= TC_MAX_SUBDATA_BYTES
) {
1484 struct tc_texture_subdata
*p
=
1485 tc_add_slot_based_call(tc
, TC_CALL_texture_subdata
, tc_texture_subdata
, size
);
1487 tc_set_resource_reference(&p
->resource
, resource
);
1492 p
->layer_stride
= layer_stride
;
1493 memcpy(p
->slot
, data
, size
);
1495 struct pipe_context
*pipe
= tc
->pipe
;
1498 pipe
->texture_subdata(pipe
, resource
, level
, usage
, box
, data
,
1499 stride
, layer_stride
);
1504 /********************************************************************
1508 #define TC_FUNC_SYNC_RET0(ret_type, func) \
1510 tc_##func(struct pipe_context *_pipe) \
1512 struct threaded_context *tc = threaded_context(_pipe); \
1513 struct pipe_context *pipe = tc->pipe; \
1515 return pipe->func(pipe); \
1518 TC_FUNC_SYNC_RET0(enum pipe_reset_status
, get_device_reset_status
)
1519 TC_FUNC_SYNC_RET0(uint64_t, get_timestamp
)
1522 tc_get_sample_position(struct pipe_context
*_pipe
,
1523 unsigned sample_count
, unsigned sample_index
,
1526 struct threaded_context
*tc
= threaded_context(_pipe
);
1527 struct pipe_context
*pipe
= tc
->pipe
;
1530 pipe
->get_sample_position(pipe
, sample_count
, sample_index
,
1535 tc_set_device_reset_callback(struct pipe_context
*_pipe
,
1536 const struct pipe_device_reset_callback
*cb
)
1538 struct threaded_context
*tc
= threaded_context(_pipe
);
1539 struct pipe_context
*pipe
= tc
->pipe
;
1542 pipe
->set_device_reset_callback(pipe
, cb
);
1545 struct tc_string_marker
{
1547 char slot
[0]; /* more will be allocated if needed */
1551 tc_call_emit_string_marker(struct pipe_context
*pipe
, union tc_payload
*payload
)
1553 struct tc_string_marker
*p
= (struct tc_string_marker
*)payload
;
1554 pipe
->emit_string_marker(pipe
, p
->slot
, p
->len
);
1558 tc_emit_string_marker(struct pipe_context
*_pipe
,
1559 const char *string
, int len
)
1561 struct threaded_context
*tc
= threaded_context(_pipe
);
1563 if (len
<= TC_MAX_STRING_MARKER_BYTES
) {
1564 struct tc_string_marker
*p
=
1565 tc_add_slot_based_call(tc
, TC_CALL_emit_string_marker
, tc_string_marker
, len
);
1567 memcpy(p
->slot
, string
, len
);
1570 struct pipe_context
*pipe
= tc
->pipe
;
1573 pipe
->emit_string_marker(pipe
, string
, len
);
1578 tc_dump_debug_state(struct pipe_context
*_pipe
, FILE *stream
,
1581 struct threaded_context
*tc
= threaded_context(_pipe
);
1582 struct pipe_context
*pipe
= tc
->pipe
;
1585 pipe
->dump_debug_state(pipe
, stream
, flags
);
1589 tc_set_debug_callback(struct pipe_context
*_pipe
,
1590 const struct pipe_debug_callback
*cb
)
1592 struct threaded_context
*tc
= threaded_context(_pipe
);
1593 struct pipe_context
*pipe
= tc
->pipe
;
1595 /* Drop all synchronous debug callbacks. Drivers are expected to be OK
1596 * with this. shader-db will use an environment variable to disable
1597 * the threaded context.
1599 if (cb
&& cb
->debug_message
&& !cb
->async
)
1603 pipe
->set_debug_callback(pipe
, cb
);
1607 tc_create_fence_fd(struct pipe_context
*_pipe
,
1608 struct pipe_fence_handle
**fence
, int fd
)
1610 struct threaded_context
*tc
= threaded_context(_pipe
);
1611 struct pipe_context
*pipe
= tc
->pipe
;
1614 pipe
->create_fence_fd(pipe
, fence
, fd
);
1618 tc_fence_server_sync(struct pipe_context
*_pipe
,
1619 struct pipe_fence_handle
*fence
)
1621 struct threaded_context
*tc
= threaded_context(_pipe
);
1622 struct pipe_context
*pipe
= tc
->pipe
;
1625 pipe
->fence_server_sync(pipe
, fence
);
1628 static struct pipe_video_codec
*
1629 tc_create_video_codec(struct pipe_context
*_pipe
,
1630 const struct pipe_video_codec
*templ
)
1632 unreachable("Threaded context should not be enabled for video APIs");
1636 static struct pipe_video_buffer
*
1637 tc_create_video_buffer(struct pipe_context
*_pipe
,
1638 const struct pipe_video_buffer
*templ
)
1640 unreachable("Threaded context should not be enabled for video APIs");
1645 /********************************************************************
1646 * draw, launch, clear, blit, copy, flush
1650 tc_flush(struct pipe_context
*_pipe
, struct pipe_fence_handle
**fence
,
1653 struct threaded_context
*tc
= threaded_context(_pipe
);
1654 struct pipe_context
*pipe
= tc
->pipe
;
1655 struct threaded_query
*tq
, *tmp
;
1657 LIST_FOR_EACH_ENTRY_SAFE(tq
, tmp
, &tc
->unflushed_queries
, head_unflushed
) {
1659 LIST_DEL(&tq
->head_unflushed
);
1662 /* TODO: deferred flushes? */
1663 tc_sync_msg(tc
, flags
& PIPE_FLUSH_END_OF_FRAME
? "end of frame" :
1664 flags
& PIPE_FLUSH_DEFERRED
? "deferred fence" : "normal");
1665 pipe
->flush(pipe
, fence
, flags
);
1668 /* This is actually variable-sized, because indirect isn't allocated if it's
1670 struct tc_full_draw_info
{
1671 struct pipe_draw_info draw
;
1672 struct pipe_draw_indirect_info indirect
;
1676 tc_call_draw_vbo(struct pipe_context
*pipe
, union tc_payload
*payload
)
1678 struct tc_full_draw_info
*info
= (struct tc_full_draw_info
*)payload
;
1680 pipe
->draw_vbo(pipe
, &info
->draw
);
1681 pipe_so_target_reference(&info
->draw
.count_from_stream_output
, NULL
);
1682 if (info
->draw
.index_size
)
1683 pipe_resource_reference(&info
->draw
.index
.resource
, NULL
);
1684 if (info
->draw
.indirect
) {
1685 pipe_resource_reference(&info
->indirect
.buffer
, NULL
);
1686 pipe_resource_reference(&info
->indirect
.indirect_draw_count
, NULL
);
1690 static struct tc_full_draw_info
*
1691 tc_add_draw_vbo(struct pipe_context
*_pipe
, bool indirect
)
1693 return (struct tc_full_draw_info
*)
1694 tc_add_sized_call(threaded_context(_pipe
), TC_CALL_draw_vbo
,
1695 indirect
? sizeof(struct tc_full_draw_info
) :
1696 sizeof(struct pipe_draw_info
));
1700 tc_draw_vbo(struct pipe_context
*_pipe
, const struct pipe_draw_info
*info
)
1702 struct threaded_context
*tc
= threaded_context(_pipe
);
1703 struct pipe_draw_indirect_info
*indirect
= info
->indirect
;
1704 unsigned index_size
= info
->index_size
;
1705 bool has_user_indices
= info
->has_user_indices
;
1707 if (index_size
&& has_user_indices
) {
1708 unsigned size
= info
->count
* index_size
;
1709 struct pipe_resource
*buffer
= NULL
;
1712 tc_assert(!indirect
);
1714 /* This must be done before adding draw_vbo, because it could generate
1715 * e.g. transfer_unmap and flush partially-uninitialized draw_vbo
1716 * to the driver if it was done afterwards.
1718 u_upload_data(tc
->base
.stream_uploader
, 0, size
, 4, info
->index
.user
,
1720 if (unlikely(!buffer
))
1723 struct tc_full_draw_info
*p
= tc_add_draw_vbo(_pipe
, false);
1724 p
->draw
.count_from_stream_output
= NULL
;
1725 pipe_so_target_reference(&p
->draw
.count_from_stream_output
,
1726 info
->count_from_stream_output
);
1727 memcpy(&p
->draw
, info
, sizeof(*info
));
1728 p
->draw
.has_user_indices
= false;
1729 p
->draw
.index
.resource
= buffer
;
1730 p
->draw
.start
= offset
/ index_size
;
1732 /* Non-indexed call or indexed with a real index buffer. */
1733 struct tc_full_draw_info
*p
= tc_add_draw_vbo(_pipe
, indirect
!= NULL
);
1734 p
->draw
.count_from_stream_output
= NULL
;
1735 pipe_so_target_reference(&p
->draw
.count_from_stream_output
,
1736 info
->count_from_stream_output
);
1738 tc_set_resource_reference(&p
->draw
.index
.resource
,
1739 info
->index
.resource
);
1741 memcpy(&p
->draw
, info
, sizeof(*info
));
1744 tc_set_resource_reference(&p
->draw
.indirect
->buffer
, indirect
->buffer
);
1745 tc_set_resource_reference(&p
->indirect
.indirect_draw_count
,
1746 indirect
->indirect_draw_count
);
1747 memcpy(&p
->indirect
, indirect
, sizeof(*indirect
));
1748 p
->draw
.indirect
= &p
->indirect
;
1754 tc_call_launch_grid(struct pipe_context
*pipe
, union tc_payload
*payload
)
1756 struct pipe_grid_info
*p
= (struct pipe_grid_info
*)payload
;
1758 pipe
->launch_grid(pipe
, p
);
1759 pipe_resource_reference(&p
->indirect
, NULL
);
1763 tc_launch_grid(struct pipe_context
*_pipe
,
1764 const struct pipe_grid_info
*info
)
1766 struct threaded_context
*tc
= threaded_context(_pipe
);
1767 struct pipe_grid_info
*p
= tc_add_struct_typed_call(tc
, TC_CALL_launch_grid
,
1769 assert(info
->input
== NULL
);
1771 tc_set_resource_reference(&p
->indirect
, info
->indirect
);
1772 memcpy(p
, info
, sizeof(*info
));
1776 tc_call_resource_copy_region(struct pipe_context
*pipe
, union tc_payload
*payload
)
1778 struct tc_resource_copy_region
*p
= (struct tc_resource_copy_region
*)payload
;
1780 pipe
->resource_copy_region(pipe
, p
->dst
, p
->dst_level
, p
->dstx
, p
->dsty
,
1781 p
->dstz
, p
->src
, p
->src_level
, &p
->src_box
);
1782 pipe_resource_reference(&p
->dst
, NULL
);
1783 pipe_resource_reference(&p
->src
, NULL
);
1787 tc_resource_copy_region(struct pipe_context
*_pipe
,
1788 struct pipe_resource
*dst
, unsigned dst_level
,
1789 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1790 struct pipe_resource
*src
, unsigned src_level
,
1791 const struct pipe_box
*src_box
)
1793 struct threaded_context
*tc
= threaded_context(_pipe
);
1794 struct threaded_resource
*tdst
= threaded_resource(dst
);
1795 struct tc_resource_copy_region
*p
=
1796 tc_add_struct_typed_call(tc
, TC_CALL_resource_copy_region
,
1797 tc_resource_copy_region
);
1799 tc_set_resource_reference(&p
->dst
, dst
);
1800 p
->dst_level
= dst_level
;
1804 tc_set_resource_reference(&p
->src
, src
);
1805 p
->src_level
= src_level
;
1806 p
->src_box
= *src_box
;
1808 if (dst
->target
== PIPE_BUFFER
)
1809 util_range_add(&tdst
->valid_buffer_range
, dstx
, dstx
+ src_box
->width
);
1813 tc_call_blit(struct pipe_context
*pipe
, union tc_payload
*payload
)
1815 struct pipe_blit_info
*blit
= (struct pipe_blit_info
*)payload
;
1817 pipe
->blit(pipe
, blit
);
1818 pipe_resource_reference(&blit
->dst
.resource
, NULL
);
1819 pipe_resource_reference(&blit
->src
.resource
, NULL
);
1823 tc_blit(struct pipe_context
*_pipe
, const struct pipe_blit_info
*info
)
1825 struct threaded_context
*tc
= threaded_context(_pipe
);
1826 struct pipe_blit_info
*blit
=
1827 tc_add_struct_typed_call(tc
, TC_CALL_blit
, pipe_blit_info
);
1829 tc_set_resource_reference(&blit
->dst
.resource
, info
->dst
.resource
);
1830 tc_set_resource_reference(&blit
->src
.resource
, info
->src
.resource
);
1831 memcpy(blit
, info
, sizeof(*info
));
1834 struct tc_generate_mipmap
{
1835 struct pipe_resource
*res
;
1836 enum pipe_format format
;
1837 unsigned base_level
;
1838 unsigned last_level
;
1839 unsigned first_layer
;
1840 unsigned last_layer
;
1844 tc_call_generate_mipmap(struct pipe_context
*pipe
, union tc_payload
*payload
)
1846 struct tc_generate_mipmap
*p
= (struct tc_generate_mipmap
*)payload
;
1847 bool result
= pipe
->generate_mipmap(pipe
, p
->res
, p
->format
, p
->base_level
,
1848 p
->last_level
, p
->first_layer
,
1851 pipe_resource_reference(&p
->res
, NULL
);
1855 tc_generate_mipmap(struct pipe_context
*_pipe
,
1856 struct pipe_resource
*res
,
1857 enum pipe_format format
,
1858 unsigned base_level
,
1859 unsigned last_level
,
1860 unsigned first_layer
,
1861 unsigned last_layer
)
1863 struct threaded_context
*tc
= threaded_context(_pipe
);
1864 struct pipe_context
*pipe
= tc
->pipe
;
1865 struct pipe_screen
*screen
= pipe
->screen
;
1866 unsigned bind
= PIPE_BIND_SAMPLER_VIEW
;
1868 if (util_format_is_depth_or_stencil(format
))
1869 bind
= PIPE_BIND_DEPTH_STENCIL
;
1871 bind
= PIPE_BIND_RENDER_TARGET
;
1873 if (!screen
->is_format_supported(screen
, format
, res
->target
,
1874 res
->nr_samples
, bind
))
1877 struct tc_generate_mipmap
*p
=
1878 tc_add_struct_typed_call(tc
, TC_CALL_generate_mipmap
, tc_generate_mipmap
);
1880 tc_set_resource_reference(&p
->res
, res
);
1882 p
->base_level
= base_level
;
1883 p
->last_level
= last_level
;
1884 p
->first_layer
= first_layer
;
1885 p
->last_layer
= last_layer
;
1890 tc_call_flush_resource(struct pipe_context
*pipe
, union tc_payload
*payload
)
1892 pipe
->flush_resource(pipe
, payload
->resource
);
1893 pipe_resource_reference(&payload
->resource
, NULL
);
1897 tc_flush_resource(struct pipe_context
*_pipe
,
1898 struct pipe_resource
*resource
)
1900 struct threaded_context
*tc
= threaded_context(_pipe
);
1901 union tc_payload
*payload
= tc_add_small_call(tc
, TC_CALL_flush_resource
);
1903 tc_set_resource_reference(&payload
->resource
, resource
);
1907 tc_call_invalidate_resource(struct pipe_context
*pipe
, union tc_payload
*payload
)
1909 pipe
->invalidate_resource(pipe
, payload
->resource
);
1910 pipe_resource_reference(&payload
->resource
, NULL
);
1914 tc_invalidate_resource(struct pipe_context
*_pipe
,
1915 struct pipe_resource
*resource
)
1917 struct threaded_context
*tc
= threaded_context(_pipe
);
1919 if (resource
->target
== PIPE_BUFFER
) {
1920 tc_invalidate_buffer(tc
, threaded_resource(resource
));
1924 union tc_payload
*payload
= tc_add_small_call(tc
, TC_CALL_invalidate_resource
);
1925 tc_set_resource_reference(&payload
->resource
, resource
);
1930 union pipe_color_union color
;
1936 tc_call_clear(struct pipe_context
*pipe
, union tc_payload
*payload
)
1938 struct tc_clear
*p
= (struct tc_clear
*)payload
;
1939 pipe
->clear(pipe
, p
->buffers
, &p
->color
, p
->depth
, p
->stencil
);
1943 tc_clear(struct pipe_context
*_pipe
, unsigned buffers
,
1944 const union pipe_color_union
*color
, double depth
,
1947 struct threaded_context
*tc
= threaded_context(_pipe
);
1948 struct tc_clear
*p
= tc_add_struct_typed_call(tc
, TC_CALL_clear
, tc_clear
);
1950 p
->buffers
= buffers
;
1953 p
->stencil
= stencil
;
1957 tc_clear_render_target(struct pipe_context
*_pipe
,
1958 struct pipe_surface
*dst
,
1959 const union pipe_color_union
*color
,
1960 unsigned dstx
, unsigned dsty
,
1961 unsigned width
, unsigned height
,
1962 bool render_condition_enabled
)
1964 struct threaded_context
*tc
= threaded_context(_pipe
);
1965 struct pipe_context
*pipe
= tc
->pipe
;
1968 pipe
->clear_render_target(pipe
, dst
, color
, dstx
, dsty
, width
, height
,
1969 render_condition_enabled
);
1973 tc_clear_depth_stencil(struct pipe_context
*_pipe
,
1974 struct pipe_surface
*dst
, unsigned clear_flags
,
1975 double depth
, unsigned stencil
, unsigned dstx
,
1976 unsigned dsty
, unsigned width
, unsigned height
,
1977 bool render_condition_enabled
)
1979 struct threaded_context
*tc
= threaded_context(_pipe
);
1980 struct pipe_context
*pipe
= tc
->pipe
;
1983 pipe
->clear_depth_stencil(pipe
, dst
, clear_flags
, depth
, stencil
,
1984 dstx
, dsty
, width
, height
,
1985 render_condition_enabled
);
1988 struct tc_clear_buffer
{
1989 struct pipe_resource
*res
;
1992 char clear_value
[16];
1993 int clear_value_size
;
1997 tc_call_clear_buffer(struct pipe_context
*pipe
, union tc_payload
*payload
)
1999 struct tc_clear_buffer
*p
= (struct tc_clear_buffer
*)payload
;
2001 pipe
->clear_buffer(pipe
, p
->res
, p
->offset
, p
->size
, p
->clear_value
,
2002 p
->clear_value_size
);
2003 pipe_resource_reference(&p
->res
, NULL
);
2007 tc_clear_buffer(struct pipe_context
*_pipe
, struct pipe_resource
*res
,
2008 unsigned offset
, unsigned size
,
2009 const void *clear_value
, int clear_value_size
)
2011 struct threaded_context
*tc
= threaded_context(_pipe
);
2012 struct threaded_resource
*tres
= threaded_resource(res
);
2013 struct tc_clear_buffer
*p
=
2014 tc_add_struct_typed_call(tc
, TC_CALL_clear_buffer
, tc_clear_buffer
);
2016 tc_set_resource_reference(&p
->res
, res
);
2019 memcpy(p
->clear_value
, clear_value
, clear_value_size
);
2020 p
->clear_value_size
= clear_value_size
;
2022 util_range_add(&tres
->valid_buffer_range
, offset
, offset
+ size
);
2025 struct tc_clear_texture
{
2026 struct pipe_resource
*res
;
2028 struct pipe_box box
;
2033 tc_call_clear_texture(struct pipe_context
*pipe
, union tc_payload
*payload
)
2035 struct tc_clear_texture
*p
= (struct tc_clear_texture
*)payload
;
2037 pipe
->clear_texture(pipe
, p
->res
, p
->level
, &p
->box
, p
->data
);
2038 pipe_resource_reference(&p
->res
, NULL
);
2042 tc_clear_texture(struct pipe_context
*_pipe
, struct pipe_resource
*res
,
2043 unsigned level
, const struct pipe_box
*box
, const void *data
)
2045 struct threaded_context
*tc
= threaded_context(_pipe
);
2046 struct tc_clear_texture
*p
=
2047 tc_add_struct_typed_call(tc
, TC_CALL_clear_texture
, tc_clear_texture
);
2049 tc_set_resource_reference(&p
->res
, res
);
2052 memcpy(p
->data
, data
,
2053 util_format_get_blocksize(res
->format
));
2056 struct tc_resource_commit
{
2057 struct pipe_resource
*res
;
2059 struct pipe_box box
;
2064 tc_call_resource_commit(struct pipe_context
*pipe
, union tc_payload
*payload
)
2066 struct tc_resource_commit
*p
= (struct tc_resource_commit
*)payload
;
2068 pipe
->resource_commit(pipe
, p
->res
, p
->level
, &p
->box
, p
->commit
);
2069 pipe_resource_reference(&p
->res
, NULL
);
2073 tc_resource_commit(struct pipe_context
*_pipe
, struct pipe_resource
*res
,
2074 unsigned level
, struct pipe_box
*box
, bool commit
)
2076 struct threaded_context
*tc
= threaded_context(_pipe
);
2077 struct tc_resource_commit
*p
=
2078 tc_add_struct_typed_call(tc
, TC_CALL_resource_commit
, tc_resource_commit
);
2080 tc_set_resource_reference(&p
->res
, res
);
2084 return true; /* we don't care about the return value for this call */
2088 /********************************************************************
2093 tc_destroy(struct pipe_context
*_pipe
)
2095 struct threaded_context
*tc
= threaded_context(_pipe
);
2096 struct pipe_context
*pipe
= tc
->pipe
;
2100 if (util_queue_is_initialized(&tc
->queue
)) {
2101 util_queue_destroy(&tc
->queue
);
2103 for (unsigned i
= 0; i
< TC_MAX_BATCHES
; i
++)
2104 util_queue_fence_destroy(&tc
->batch_slots
[i
].fence
);
2107 if (tc
->base
.const_uploader
&&
2108 tc
->base
.stream_uploader
!= tc
->base
.const_uploader
)
2109 u_upload_destroy(tc
->base
.const_uploader
);
2111 if (tc
->base
.stream_uploader
)
2112 u_upload_destroy(tc
->base
.stream_uploader
);
2114 slab_destroy_child(&tc
->pool_transfers
);
2115 pipe
->destroy(pipe
);
2119 static const tc_execute execute_func
[TC_NUM_CALLS
] = {
2120 #define CALL(name) tc_call_##name,
2121 #include "u_threaded_context_calls.h"
2126 * Wrap an existing pipe_context into a threaded_context.
2128 * \param pipe pipe_context to wrap
2129 * \param parent_transfer_pool parent slab pool set up for creating pipe_-
2130 * transfer objects; the driver should have one
2132 * \param replace_buffer callback for replacing a pipe_resource's storage
2133 * with another pipe_resource's storage.
2134 * \param out if successful, the threaded_context will be returned here in
2135 * addition to the return value if "out" != NULL
2137 struct pipe_context
*
2138 threaded_context_create(struct pipe_context
*pipe
,
2139 struct slab_parent_pool
*parent_transfer_pool
,
2140 tc_replace_buffer_storage_func replace_buffer
,
2141 struct threaded_context
**out
)
2143 struct threaded_context
*tc
;
2145 STATIC_ASSERT(sizeof(union tc_payload
) <= 8);
2146 STATIC_ASSERT(sizeof(struct tc_call
) <= 16);
2153 if (!debug_get_bool_option("GALLIUM_THREAD", util_cpu_caps
.nr_cpus
> 1))
2156 tc
= CALLOC_STRUCT(threaded_context
);
2158 pipe
->destroy(pipe
);
2162 /* The driver context isn't wrapped, so set its "priv" to NULL. */
2166 tc
->replace_buffer_storage
= replace_buffer
;
2167 tc
->map_buffer_alignment
=
2168 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT
);
2169 tc
->base
.priv
= pipe
; /* priv points to the wrapped driver context */
2170 tc
->base
.screen
= pipe
->screen
;
2171 tc
->base
.destroy
= tc_destroy
;
2173 tc
->base
.stream_uploader
= u_upload_clone(&tc
->base
, pipe
->stream_uploader
);
2174 if (pipe
->stream_uploader
== pipe
->const_uploader
)
2175 tc
->base
.const_uploader
= tc
->base
.stream_uploader
;
2177 tc
->base
.const_uploader
= u_upload_clone(&tc
->base
, pipe
->const_uploader
);
2179 if (!tc
->base
.stream_uploader
|| !tc
->base
.const_uploader
)
2182 /* The queue size is the number of batches "waiting". Batches are removed
2183 * from the queue before being executed, so keep one tc_batch slot for that
2184 * execution. Also, keep one unused slot for an unflushed batch.
2186 if (!util_queue_init(&tc
->queue
, "gallium_drv", TC_MAX_BATCHES
- 2, 1))
2189 for (unsigned i
= 0; i
< TC_MAX_BATCHES
; i
++) {
2190 tc
->batch_slots
[i
].sentinel
= TC_SENTINEL
;
2191 tc
->batch_slots
[i
].sentinel2
= TC_SENTINEL
;
2192 tc
->batch_slots
[i
].pipe
= pipe
;
2193 util_queue_fence_init(&tc
->batch_slots
[i
].fence
);
2196 LIST_INITHEAD(&tc
->unflushed_queries
);
2198 slab_create_child(&tc
->pool_transfers
, parent_transfer_pool
);
2200 #define CTX_INIT(_member) \
2201 tc->base._member = tc->pipe->_member ? tc_##_member : NULL
2205 CTX_INIT(launch_grid
);
2206 CTX_INIT(resource_copy_region
);
2209 CTX_INIT(clear_render_target
);
2210 CTX_INIT(clear_depth_stencil
);
2211 CTX_INIT(clear_buffer
);
2212 CTX_INIT(clear_texture
);
2213 CTX_INIT(flush_resource
);
2214 CTX_INIT(generate_mipmap
);
2215 CTX_INIT(render_condition
);
2216 CTX_INIT(create_query
);
2217 CTX_INIT(create_batch_query
);
2218 CTX_INIT(destroy_query
);
2219 CTX_INIT(begin_query
);
2220 CTX_INIT(end_query
);
2221 CTX_INIT(get_query_result
);
2222 CTX_INIT(get_query_result_resource
);
2223 CTX_INIT(set_active_query_state
);
2224 CTX_INIT(create_blend_state
);
2225 CTX_INIT(bind_blend_state
);
2226 CTX_INIT(delete_blend_state
);
2227 CTX_INIT(create_sampler_state
);
2228 CTX_INIT(bind_sampler_states
);
2229 CTX_INIT(delete_sampler_state
);
2230 CTX_INIT(create_rasterizer_state
);
2231 CTX_INIT(bind_rasterizer_state
);
2232 CTX_INIT(delete_rasterizer_state
);
2233 CTX_INIT(create_depth_stencil_alpha_state
);
2234 CTX_INIT(bind_depth_stencil_alpha_state
);
2235 CTX_INIT(delete_depth_stencil_alpha_state
);
2236 CTX_INIT(create_fs_state
);
2237 CTX_INIT(bind_fs_state
);
2238 CTX_INIT(delete_fs_state
);
2239 CTX_INIT(create_vs_state
);
2240 CTX_INIT(bind_vs_state
);
2241 CTX_INIT(delete_vs_state
);
2242 CTX_INIT(create_gs_state
);
2243 CTX_INIT(bind_gs_state
);
2244 CTX_INIT(delete_gs_state
);
2245 CTX_INIT(create_tcs_state
);
2246 CTX_INIT(bind_tcs_state
);
2247 CTX_INIT(delete_tcs_state
);
2248 CTX_INIT(create_tes_state
);
2249 CTX_INIT(bind_tes_state
);
2250 CTX_INIT(delete_tes_state
);
2251 CTX_INIT(create_compute_state
);
2252 CTX_INIT(bind_compute_state
);
2253 CTX_INIT(delete_compute_state
);
2254 CTX_INIT(create_vertex_elements_state
);
2255 CTX_INIT(bind_vertex_elements_state
);
2256 CTX_INIT(delete_vertex_elements_state
);
2257 CTX_INIT(set_blend_color
);
2258 CTX_INIT(set_stencil_ref
);
2259 CTX_INIT(set_sample_mask
);
2260 CTX_INIT(set_min_samples
);
2261 CTX_INIT(set_clip_state
);
2262 CTX_INIT(set_constant_buffer
);
2263 CTX_INIT(set_framebuffer_state
);
2264 CTX_INIT(set_polygon_stipple
);
2265 CTX_INIT(set_scissor_states
);
2266 CTX_INIT(set_viewport_states
);
2267 CTX_INIT(set_window_rectangles
);
2268 CTX_INIT(set_sampler_views
);
2269 CTX_INIT(set_tess_state
);
2270 CTX_INIT(set_shader_buffers
);
2271 CTX_INIT(set_shader_images
);
2272 CTX_INIT(set_vertex_buffers
);
2273 CTX_INIT(create_stream_output_target
);
2274 CTX_INIT(stream_output_target_destroy
);
2275 CTX_INIT(set_stream_output_targets
);
2276 CTX_INIT(create_sampler_view
);
2277 CTX_INIT(sampler_view_destroy
);
2278 CTX_INIT(create_surface
);
2279 CTX_INIT(surface_destroy
);
2280 CTX_INIT(transfer_map
);
2281 CTX_INIT(transfer_flush_region
);
2282 CTX_INIT(transfer_unmap
);
2283 CTX_INIT(buffer_subdata
);
2284 CTX_INIT(texture_subdata
);
2285 CTX_INIT(texture_barrier
);
2286 CTX_INIT(memory_barrier
);
2287 CTX_INIT(resource_commit
);
2288 CTX_INIT(create_video_codec
);
2289 CTX_INIT(create_video_buffer
);
2290 CTX_INIT(set_compute_resources
);
2291 CTX_INIT(set_global_binding
);
2292 CTX_INIT(get_sample_position
);
2293 CTX_INIT(invalidate_resource
);
2294 CTX_INIT(get_device_reset_status
);
2295 CTX_INIT(set_device_reset_callback
);
2296 CTX_INIT(dump_debug_state
);
2297 CTX_INIT(emit_string_marker
);
2298 CTX_INIT(set_debug_callback
);
2299 CTX_INIT(create_fence_fd
);
2300 CTX_INIT(fence_server_sync
);
2301 CTX_INIT(get_timestamp
);
2310 tc_destroy(&tc
->base
);