2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2014 Marek Olšák <marek.olsak@amd.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "util/u_memory.h"
29 struct r600_query_buffer
{
30 /* The buffer where query results are stored. */
31 struct r600_resource
*buf
;
32 /* Offset of the next free result after current query data */
34 /* If a query buffer is full, a new buffer is created and the old one
35 * is put in here. When we calculate the result, we sum up the samples
36 * from all buffers. */
37 struct r600_query_buffer
*previous
;
41 /* The query buffer and how many results are in it. */
42 struct r600_query_buffer buffer
;
43 /* The type of query */
45 /* Size of the result in memory for both begin_query and end_query,
46 * this can be one or two numbers, or it could even be a size of a structure. */
48 /* The number of dwords for begin_query or end_query. */
50 /* linked list of queries */
51 struct list_head list
;
52 /* for custom non-GPU queries */
53 uint64_t begin_result
;
58 static bool r600_is_timer_query(unsigned type
)
60 return type
== PIPE_QUERY_TIME_ELAPSED
||
61 type
== PIPE_QUERY_TIMESTAMP
||
62 type
== PIPE_QUERY_TIMESTAMP_DISJOINT
;
65 static bool r600_query_needs_begin(unsigned type
)
67 return type
!= PIPE_QUERY_GPU_FINISHED
&&
68 type
!= PIPE_QUERY_TIMESTAMP
;
71 static struct r600_resource
*r600_new_query_buffer(struct r600_common_context
*ctx
, unsigned type
)
73 unsigned j
, i
, num_results
, buf_size
= 4096;
76 /* Non-GPU queries. */
78 case R600_QUERY_DRAW_CALLS
:
79 case R600_QUERY_REQUESTED_VRAM
:
80 case R600_QUERY_REQUESTED_GTT
:
81 case R600_QUERY_BUFFER_WAIT_TIME
:
85 /* Queries are normally read by the CPU after
86 * being written by the gpu, hence staging is probably a good
89 struct r600_resource
*buf
= (struct r600_resource
*)
90 pipe_buffer_create(ctx
->b
.screen
, PIPE_BIND_CUSTOM
,
91 PIPE_USAGE_STAGING
, buf_size
);
94 case PIPE_QUERY_OCCLUSION_COUNTER
:
95 case PIPE_QUERY_OCCLUSION_PREDICATE
:
96 results
= r600_buffer_map_sync_with_rings(ctx
, buf
, PIPE_TRANSFER_WRITE
);
97 memset(results
, 0, buf_size
);
99 /* Set top bits for unused backends. */
100 num_results
= buf_size
/ (16 * ctx
->max_db
);
101 for (j
= 0; j
< num_results
; j
++) {
102 for (i
= 0; i
< ctx
->max_db
; i
++) {
103 if (!(ctx
->backend_mask
& (1<<i
))) {
104 results
[(i
* 4)+1] = 0x80000000;
105 results
[(i
* 4)+3] = 0x80000000;
108 results
+= 4 * ctx
->max_db
;
110 ctx
->ws
->buffer_unmap(buf
->cs_buf
);
112 case PIPE_QUERY_TIME_ELAPSED
:
113 case PIPE_QUERY_TIMESTAMP
:
115 case PIPE_QUERY_PRIMITIVES_EMITTED
:
116 case PIPE_QUERY_PRIMITIVES_GENERATED
:
117 case PIPE_QUERY_SO_STATISTICS
:
118 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
119 case PIPE_QUERY_PIPELINE_STATISTICS
:
120 results
= r600_buffer_map_sync_with_rings(ctx
, buf
, PIPE_TRANSFER_WRITE
);
121 memset(results
, 0, buf_size
);
122 ctx
->ws
->buffer_unmap(buf
->cs_buf
);
130 static void r600_update_occlusion_query_state(struct r600_common_context
*rctx
,
131 unsigned type
, int diff
)
133 if (type
== PIPE_QUERY_OCCLUSION_COUNTER
||
134 type
== PIPE_QUERY_OCCLUSION_PREDICATE
) {
135 bool old_enable
= rctx
->num_occlusion_queries
!= 0;
138 rctx
->num_occlusion_queries
+= diff
;
139 assert(rctx
->num_occlusion_queries
>= 0);
141 enable
= rctx
->num_occlusion_queries
!= 0;
143 if (enable
!= old_enable
) {
144 rctx
->set_occlusion_query_state(&rctx
->b
, enable
);
149 static void r600_emit_query_begin(struct r600_common_context
*ctx
, struct r600_query
*query
)
151 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
154 r600_update_occlusion_query_state(ctx
, query
->type
, 1);
155 r600_update_prims_generated_query_state(ctx
, query
->type
, 1);
156 ctx
->need_gfx_cs_space(&ctx
->b
, query
->num_cs_dw
* 2, TRUE
);
158 /* Get a new query buffer if needed. */
159 if (query
->buffer
.results_end
+ query
->result_size
> query
->buffer
.buf
->b
.b
.width0
) {
160 struct r600_query_buffer
*qbuf
= MALLOC_STRUCT(r600_query_buffer
);
161 *qbuf
= query
->buffer
;
162 query
->buffer
.buf
= r600_new_query_buffer(ctx
, query
->type
);
163 query
->buffer
.results_end
= 0;
164 query
->buffer
.previous
= qbuf
;
167 /* emit begin query */
168 va
= r600_resource_va(ctx
->b
.screen
, (void*)query
->buffer
.buf
);
169 va
+= query
->buffer
.results_end
;
171 switch (query
->type
) {
172 case PIPE_QUERY_OCCLUSION_COUNTER
:
173 case PIPE_QUERY_OCCLUSION_PREDICATE
:
174 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
175 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
177 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
179 case PIPE_QUERY_PRIMITIVES_EMITTED
:
180 case PIPE_QUERY_PRIMITIVES_GENERATED
:
181 case PIPE_QUERY_SO_STATISTICS
:
182 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
183 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
184 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_SAMPLE_STREAMOUTSTATS
) | EVENT_INDEX(3));
186 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
188 case PIPE_QUERY_TIME_ELAPSED
:
189 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE_EOP
, 4, 0));
190 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_CACHE_FLUSH_AND_INV_TS_EVENT
) | EVENT_INDEX(5));
192 radeon_emit(cs
, (3 << 29) | ((va
>> 32UL) & 0xFF));
196 case PIPE_QUERY_PIPELINE_STATISTICS
:
197 if (!ctx
->num_pipelinestat_queries
) {
198 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
199 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_PIPELINESTAT_START
) | EVENT_INDEX(0));
201 ctx
->num_pipelinestat_queries
++;
202 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
203 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2));
205 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
210 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, query
->buffer
.buf
, RADEON_USAGE_WRITE
,
213 if (!r600_is_timer_query(query
->type
)) {
214 ctx
->num_cs_dw_nontimer_queries_suspend
+= query
->num_cs_dw
;
218 static void r600_emit_query_end(struct r600_common_context
*ctx
, struct r600_query
*query
)
220 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
223 /* The queries which need begin already called this in begin_query. */
224 if (!r600_query_needs_begin(query
->type
)) {
225 ctx
->need_gfx_cs_space(&ctx
->b
, query
->num_cs_dw
, FALSE
);
228 va
= r600_resource_va(ctx
->b
.screen
, (void*)query
->buffer
.buf
);
230 switch (query
->type
) {
231 case PIPE_QUERY_OCCLUSION_COUNTER
:
232 case PIPE_QUERY_OCCLUSION_PREDICATE
:
233 va
+= query
->buffer
.results_end
+ 8;
234 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
235 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
237 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
239 case PIPE_QUERY_PRIMITIVES_EMITTED
:
240 case PIPE_QUERY_PRIMITIVES_GENERATED
:
241 case PIPE_QUERY_SO_STATISTICS
:
242 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
243 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
244 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
245 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_SAMPLE_STREAMOUTSTATS
) | EVENT_INDEX(3));
247 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
249 case PIPE_QUERY_TIME_ELAPSED
:
250 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
252 case PIPE_QUERY_TIMESTAMP
:
253 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE_EOP
, 4, 0));
254 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_CACHE_FLUSH_AND_INV_TS_EVENT
) | EVENT_INDEX(5));
256 radeon_emit(cs
, (3 << 29) | ((va
>> 32UL) & 0xFF));
260 case PIPE_QUERY_PIPELINE_STATISTICS
:
261 assert(ctx
->num_pipelinestat_queries
> 0);
262 ctx
->num_pipelinestat_queries
--;
263 if (!ctx
->num_pipelinestat_queries
) {
264 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
265 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_PIPELINESTAT_STOP
) | EVENT_INDEX(0));
267 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
268 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
269 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2));
271 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
276 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, query
->buffer
.buf
, RADEON_USAGE_WRITE
,
279 query
->buffer
.results_end
+= query
->result_size
;
281 if (r600_query_needs_begin(query
->type
)) {
282 if (!r600_is_timer_query(query
->type
)) {
283 ctx
->num_cs_dw_nontimer_queries_suspend
-= query
->num_cs_dw
;
287 r600_update_occlusion_query_state(ctx
, query
->type
, -1);
288 r600_update_prims_generated_query_state(ctx
, query
->type
, -1);
291 static void r600_emit_query_predication(struct r600_common_context
*ctx
, struct r600_query
*query
,
292 int operation
, bool flag_wait
)
294 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
296 if (operation
== PREDICATION_OP_CLEAR
) {
297 ctx
->need_gfx_cs_space(&ctx
->b
, 3, FALSE
);
299 radeon_emit(cs
, PKT3(PKT3_SET_PREDICATION
, 1, 0));
301 radeon_emit(cs
, PRED_OP(PREDICATION_OP_CLEAR
));
303 struct r600_query_buffer
*qbuf
;
307 /* Find how many results there are. */
309 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
310 count
+= qbuf
->results_end
/ query
->result_size
;
313 ctx
->need_gfx_cs_space(&ctx
->b
, 5 * count
, TRUE
);
315 op
= PRED_OP(operation
) | PREDICATION_DRAW_VISIBLE
|
316 (flag_wait
? PREDICATION_HINT_WAIT
: PREDICATION_HINT_NOWAIT_DRAW
);
318 /* emit predicate packets for all data blocks */
319 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
320 unsigned results_base
= 0;
321 uint64_t va
= r600_resource_va(ctx
->b
.screen
, &qbuf
->buf
->b
.b
);
323 while (results_base
< qbuf
->results_end
) {
324 radeon_emit(cs
, PKT3(PKT3_SET_PREDICATION
, 1, 0));
325 radeon_emit(cs
, (va
+ results_base
) & 0xFFFFFFFFUL
);
326 radeon_emit(cs
, op
| (((va
+ results_base
) >> 32UL) & 0xFF));
327 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, qbuf
->buf
, RADEON_USAGE_READ
,
329 results_base
+= query
->result_size
;
331 /* set CONTINUE bit for all packets except the first */
332 op
|= PREDICATION_CONTINUE
;
338 static struct pipe_query
*r600_create_query(struct pipe_context
*ctx
, unsigned query_type
)
340 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
341 struct r600_query
*query
;
342 bool skip_allocation
= false;
344 query
= CALLOC_STRUCT(r600_query
);
348 query
->type
= query_type
;
350 switch (query_type
) {
351 case PIPE_QUERY_OCCLUSION_COUNTER
:
352 case PIPE_QUERY_OCCLUSION_PREDICATE
:
353 query
->result_size
= 16 * rctx
->max_db
;
354 query
->num_cs_dw
= 6;
356 case PIPE_QUERY_TIME_ELAPSED
:
357 query
->result_size
= 16;
358 query
->num_cs_dw
= 8;
360 case PIPE_QUERY_TIMESTAMP
:
361 query
->result_size
= 8;
362 query
->num_cs_dw
= 8;
364 case PIPE_QUERY_PRIMITIVES_EMITTED
:
365 case PIPE_QUERY_PRIMITIVES_GENERATED
:
366 case PIPE_QUERY_SO_STATISTICS
:
367 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
368 /* NumPrimitivesWritten, PrimitiveStorageNeeded. */
369 query
->result_size
= 32;
370 query
->num_cs_dw
= 6;
372 case PIPE_QUERY_PIPELINE_STATISTICS
:
373 /* 11 values on EG, 8 on R600. */
374 query
->result_size
= (rctx
->chip_class
>= EVERGREEN
? 11 : 8) * 16;
375 query
->num_cs_dw
= 8;
377 /* Non-GPU queries. */
378 case R600_QUERY_DRAW_CALLS
:
379 case R600_QUERY_REQUESTED_VRAM
:
380 case R600_QUERY_REQUESTED_GTT
:
381 case R600_QUERY_BUFFER_WAIT_TIME
:
382 skip_allocation
= true;
390 if (!skip_allocation
) {
391 query
->buffer
.buf
= r600_new_query_buffer(rctx
, query_type
);
392 if (!query
->buffer
.buf
) {
397 return (struct pipe_query
*)query
;
400 static void r600_destroy_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
402 struct r600_query
*rquery
= (struct r600_query
*)query
;
403 struct r600_query_buffer
*prev
= rquery
->buffer
.previous
;
405 /* Release all query buffers. */
407 struct r600_query_buffer
*qbuf
= prev
;
408 prev
= prev
->previous
;
409 pipe_resource_reference((struct pipe_resource
**)&qbuf
->buf
, NULL
);
413 pipe_resource_reference((struct pipe_resource
**)&rquery
->buffer
.buf
, NULL
);
417 static void r600_begin_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
419 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
420 struct r600_query
*rquery
= (struct r600_query
*)query
;
421 struct r600_query_buffer
*prev
= rquery
->buffer
.previous
;
423 if (!r600_query_needs_begin(rquery
->type
)) {
428 /* Non-GPU queries. */
429 switch (rquery
->type
) {
430 case R600_QUERY_DRAW_CALLS
:
431 rquery
->begin_result
= rctx
->num_draw_calls
;
433 case R600_QUERY_REQUESTED_VRAM
:
434 case R600_QUERY_REQUESTED_GTT
:
435 rquery
->begin_result
= 0;
437 case R600_QUERY_BUFFER_WAIT_TIME
:
438 rquery
->begin_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_BUFFER_WAIT_TIME_NS
);
442 /* Discard the old query buffers. */
444 struct r600_query_buffer
*qbuf
= prev
;
445 prev
= prev
->previous
;
446 pipe_resource_reference((struct pipe_resource
**)&qbuf
->buf
, NULL
);
450 /* Obtain a new buffer if the current one can't be mapped without a stall. */
451 if (r600_rings_is_buffer_referenced(rctx
, rquery
->buffer
.buf
->cs_buf
, RADEON_USAGE_READWRITE
) ||
452 rctx
->ws
->buffer_is_busy(rquery
->buffer
.buf
->buf
, RADEON_USAGE_READWRITE
)) {
453 pipe_resource_reference((struct pipe_resource
**)&rquery
->buffer
.buf
, NULL
);
454 rquery
->buffer
.buf
= r600_new_query_buffer(rctx
, rquery
->type
);
457 rquery
->buffer
.results_end
= 0;
458 rquery
->buffer
.previous
= NULL
;
460 r600_emit_query_begin(rctx
, rquery
);
462 if (!r600_is_timer_query(rquery
->type
)) {
463 LIST_ADDTAIL(&rquery
->list
, &rctx
->active_nontimer_queries
);
467 static void r600_end_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
469 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
470 struct r600_query
*rquery
= (struct r600_query
*)query
;
472 /* Non-GPU queries. */
473 switch (rquery
->type
) {
474 case R600_QUERY_DRAW_CALLS
:
475 rquery
->end_result
= rctx
->num_draw_calls
;
477 case R600_QUERY_REQUESTED_VRAM
:
478 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_REQUESTED_VRAM_MEMORY
);
480 case R600_QUERY_REQUESTED_GTT
:
481 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_REQUESTED_GTT_MEMORY
);
483 case R600_QUERY_BUFFER_WAIT_TIME
:
484 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_BUFFER_WAIT_TIME_NS
);
488 r600_emit_query_end(rctx
, rquery
);
490 if (r600_query_needs_begin(rquery
->type
) && !r600_is_timer_query(rquery
->type
)) {
491 LIST_DELINIT(&rquery
->list
);
495 static unsigned r600_query_read_result(char *map
, unsigned start_index
, unsigned end_index
,
496 bool test_status_bit
)
498 uint32_t *current_result
= (uint32_t*)map
;
501 start
= (uint64_t)current_result
[start_index
] |
502 (uint64_t)current_result
[start_index
+1] << 32;
503 end
= (uint64_t)current_result
[end_index
] |
504 (uint64_t)current_result
[end_index
+1] << 32;
506 if (!test_status_bit
||
507 ((start
& 0x8000000000000000UL
) && (end
& 0x8000000000000000UL
))) {
513 static boolean
r600_get_query_buffer_result(struct r600_common_context
*ctx
,
514 struct r600_query
*query
,
515 struct r600_query_buffer
*qbuf
,
517 union pipe_query_result
*result
)
519 unsigned results_base
= 0;
522 /* Non-GPU queries. */
523 switch (query
->type
) {
524 case R600_QUERY_DRAW_CALLS
:
525 case R600_QUERY_REQUESTED_VRAM
:
526 case R600_QUERY_REQUESTED_GTT
:
527 case R600_QUERY_BUFFER_WAIT_TIME
:
528 result
->u64
= query
->end_result
- query
->begin_result
;
532 map
= r600_buffer_map_sync_with_rings(ctx
, qbuf
->buf
,
534 (wait
? 0 : PIPE_TRANSFER_DONTBLOCK
));
538 /* count all results across all data blocks */
539 switch (query
->type
) {
540 case PIPE_QUERY_OCCLUSION_COUNTER
:
541 while (results_base
!= qbuf
->results_end
) {
543 r600_query_read_result(map
+ results_base
, 0, 2, true);
547 case PIPE_QUERY_OCCLUSION_PREDICATE
:
548 while (results_base
!= qbuf
->results_end
) {
549 result
->b
= result
->b
||
550 r600_query_read_result(map
+ results_base
, 0, 2, true) != 0;
554 case PIPE_QUERY_TIME_ELAPSED
:
555 while (results_base
!= qbuf
->results_end
) {
557 r600_query_read_result(map
+ results_base
, 0, 2, false);
558 results_base
+= query
->result_size
;
561 case PIPE_QUERY_TIMESTAMP
:
563 uint32_t *current_result
= (uint32_t*)map
;
564 result
->u64
= (uint64_t)current_result
[0] |
565 (uint64_t)current_result
[1] << 32;
568 case PIPE_QUERY_PRIMITIVES_EMITTED
:
569 /* SAMPLE_STREAMOUTSTATS stores this structure:
571 * u64 NumPrimitivesWritten;
572 * u64 PrimitiveStorageNeeded;
574 * We only need NumPrimitivesWritten here. */
575 while (results_base
!= qbuf
->results_end
) {
577 r600_query_read_result(map
+ results_base
, 2, 6, true);
578 results_base
+= query
->result_size
;
581 case PIPE_QUERY_PRIMITIVES_GENERATED
:
582 /* Here we read PrimitiveStorageNeeded. */
583 while (results_base
!= qbuf
->results_end
) {
585 r600_query_read_result(map
+ results_base
, 0, 4, true);
586 results_base
+= query
->result_size
;
589 case PIPE_QUERY_SO_STATISTICS
:
590 while (results_base
!= qbuf
->results_end
) {
591 result
->so_statistics
.num_primitives_written
+=
592 r600_query_read_result(map
+ results_base
, 2, 6, true);
593 result
->so_statistics
.primitives_storage_needed
+=
594 r600_query_read_result(map
+ results_base
, 0, 4, true);
595 results_base
+= query
->result_size
;
598 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
599 while (results_base
!= qbuf
->results_end
) {
600 result
->b
= result
->b
||
601 r600_query_read_result(map
+ results_base
, 2, 6, true) !=
602 r600_query_read_result(map
+ results_base
, 0, 4, true);
603 results_base
+= query
->result_size
;
606 case PIPE_QUERY_PIPELINE_STATISTICS
:
607 if (ctx
->chip_class
>= EVERGREEN
) {
608 while (results_base
!= qbuf
->results_end
) {
609 result
->pipeline_statistics
.ps_invocations
+=
610 r600_query_read_result(map
+ results_base
, 0, 22, false);
611 result
->pipeline_statistics
.c_primitives
+=
612 r600_query_read_result(map
+ results_base
, 2, 24, false);
613 result
->pipeline_statistics
.c_invocations
+=
614 r600_query_read_result(map
+ results_base
, 4, 26, false);
615 result
->pipeline_statistics
.vs_invocations
+=
616 r600_query_read_result(map
+ results_base
, 6, 28, false);
617 result
->pipeline_statistics
.gs_invocations
+=
618 r600_query_read_result(map
+ results_base
, 8, 30, false);
619 result
->pipeline_statistics
.gs_primitives
+=
620 r600_query_read_result(map
+ results_base
, 10, 32, false);
621 result
->pipeline_statistics
.ia_primitives
+=
622 r600_query_read_result(map
+ results_base
, 12, 34, false);
623 result
->pipeline_statistics
.ia_vertices
+=
624 r600_query_read_result(map
+ results_base
, 14, 36, false);
625 result
->pipeline_statistics
.hs_invocations
+=
626 r600_query_read_result(map
+ results_base
, 16, 38, false);
627 result
->pipeline_statistics
.ds_invocations
+=
628 r600_query_read_result(map
+ results_base
, 18, 40, false);
629 result
->pipeline_statistics
.cs_invocations
+=
630 r600_query_read_result(map
+ results_base
, 20, 42, false);
631 results_base
+= query
->result_size
;
634 while (results_base
!= qbuf
->results_end
) {
635 result
->pipeline_statistics
.ps_invocations
+=
636 r600_query_read_result(map
+ results_base
, 0, 16, false);
637 result
->pipeline_statistics
.c_primitives
+=
638 r600_query_read_result(map
+ results_base
, 2, 18, false);
639 result
->pipeline_statistics
.c_invocations
+=
640 r600_query_read_result(map
+ results_base
, 4, 20, false);
641 result
->pipeline_statistics
.vs_invocations
+=
642 r600_query_read_result(map
+ results_base
, 6, 22, false);
643 result
->pipeline_statistics
.gs_invocations
+=
644 r600_query_read_result(map
+ results_base
, 8, 24, false);
645 result
->pipeline_statistics
.gs_primitives
+=
646 r600_query_read_result(map
+ results_base
, 10, 26, false);
647 result
->pipeline_statistics
.ia_primitives
+=
648 r600_query_read_result(map
+ results_base
, 12, 28, false);
649 result
->pipeline_statistics
.ia_vertices
+=
650 r600_query_read_result(map
+ results_base
, 14, 30, false);
651 results_base
+= query
->result_size
;
654 #if 0 /* for testing */
655 printf("Pipeline stats: IA verts=%llu, IA prims=%llu, VS=%llu, HS=%llu, "
656 "DS=%llu, GS=%llu, GS prims=%llu, Clipper=%llu, "
657 "Clipper prims=%llu, PS=%llu, CS=%llu\n",
658 result
->pipeline_statistics
.ia_vertices
,
659 result
->pipeline_statistics
.ia_primitives
,
660 result
->pipeline_statistics
.vs_invocations
,
661 result
->pipeline_statistics
.hs_invocations
,
662 result
->pipeline_statistics
.ds_invocations
,
663 result
->pipeline_statistics
.gs_invocations
,
664 result
->pipeline_statistics
.gs_primitives
,
665 result
->pipeline_statistics
.c_invocations
,
666 result
->pipeline_statistics
.c_primitives
,
667 result
->pipeline_statistics
.ps_invocations
,
668 result
->pipeline_statistics
.cs_invocations
);
675 ctx
->ws
->buffer_unmap(qbuf
->buf
->cs_buf
);
679 static boolean
r600_get_query_result(struct pipe_context
*ctx
,
680 struct pipe_query
*query
,
681 boolean wait
, union pipe_query_result
*result
)
683 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
684 struct r600_query
*rquery
= (struct r600_query
*)query
;
685 struct r600_query_buffer
*qbuf
;
687 util_query_clear_result(result
, rquery
->type
);
689 for (qbuf
= &rquery
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
690 if (!r600_get_query_buffer_result(rctx
, rquery
, qbuf
, wait
, result
)) {
695 /* Convert the time to expected units. */
696 if (rquery
->type
== PIPE_QUERY_TIME_ELAPSED
||
697 rquery
->type
== PIPE_QUERY_TIMESTAMP
) {
698 result
->u64
= (1000000 * result
->u64
) / rctx
->screen
->info
.r600_clock_crystal_freq
;
703 static void r600_render_condition(struct pipe_context
*ctx
,
704 struct pipe_query
*query
,
708 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
709 struct r600_query
*rquery
= (struct r600_query
*)query
;
710 bool wait_flag
= false;
712 rctx
->current_render_cond
= query
;
713 rctx
->current_render_cond_cond
= condition
;
714 rctx
->current_render_cond_mode
= mode
;
717 if (rctx
->predicate_drawing
) {
718 rctx
->predicate_drawing
= false;
719 r600_emit_query_predication(rctx
, NULL
, PREDICATION_OP_CLEAR
, false);
724 if (mode
== PIPE_RENDER_COND_WAIT
||
725 mode
== PIPE_RENDER_COND_BY_REGION_WAIT
) {
729 rctx
->predicate_drawing
= true;
731 switch (rquery
->type
) {
732 case PIPE_QUERY_OCCLUSION_COUNTER
:
733 case PIPE_QUERY_OCCLUSION_PREDICATE
:
734 r600_emit_query_predication(rctx
, rquery
, PREDICATION_OP_ZPASS
, wait_flag
);
736 case PIPE_QUERY_PRIMITIVES_EMITTED
:
737 case PIPE_QUERY_PRIMITIVES_GENERATED
:
738 case PIPE_QUERY_SO_STATISTICS
:
739 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
740 r600_emit_query_predication(rctx
, rquery
, PREDICATION_OP_PRIMCOUNT
, wait_flag
);
747 void r600_suspend_nontimer_queries(struct r600_common_context
*ctx
)
749 struct r600_query
*query
;
751 LIST_FOR_EACH_ENTRY(query
, &ctx
->active_nontimer_queries
, list
) {
752 r600_emit_query_end(ctx
, query
);
754 assert(ctx
->num_cs_dw_nontimer_queries_suspend
== 0);
757 void r600_resume_nontimer_queries(struct r600_common_context
*ctx
)
759 struct r600_query
*query
;
761 assert(ctx
->num_cs_dw_nontimer_queries_suspend
== 0);
763 LIST_FOR_EACH_ENTRY(query
, &ctx
->active_nontimer_queries
, list
) {
764 r600_emit_query_begin(ctx
, query
);
768 /* Get backends mask */
769 void r600_query_init_backend_mask(struct r600_common_context
*ctx
)
771 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
772 struct r600_resource
*buffer
;
774 unsigned num_backends
= ctx
->screen
->info
.r600_num_backends
;
775 unsigned i
, mask
= 0;
778 /* if backend_map query is supported by the kernel */
779 if (ctx
->screen
->info
.r600_backend_map_valid
) {
780 unsigned num_tile_pipes
= ctx
->screen
->info
.r600_num_tile_pipes
;
781 unsigned backend_map
= ctx
->screen
->info
.r600_backend_map
;
782 unsigned item_width
, item_mask
;
784 if (ctx
->chip_class
>= EVERGREEN
) {
792 while(num_tile_pipes
--) {
793 i
= backend_map
& item_mask
;
795 backend_map
>>= item_width
;
798 ctx
->backend_mask
= mask
;
803 /* otherwise backup path for older kernels */
805 /* create buffer for event data */
806 buffer
= (struct r600_resource
*)
807 pipe_buffer_create(ctx
->b
.screen
, PIPE_BIND_CUSTOM
,
808 PIPE_USAGE_STAGING
, ctx
->max_db
*16);
811 va
= r600_resource_va(ctx
->b
.screen
, (void*)buffer
);
813 /* initialize buffer with zeroes */
814 results
= r600_buffer_map_sync_with_rings(ctx
, buffer
, PIPE_TRANSFER_WRITE
);
816 memset(results
, 0, ctx
->max_db
* 4 * 4);
817 ctx
->ws
->buffer_unmap(buffer
->cs_buf
);
819 /* emit EVENT_WRITE for ZPASS_DONE */
820 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
821 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
823 radeon_emit(cs
, va
>> 32);
825 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, buffer
, RADEON_USAGE_WRITE
, RADEON_PRIO_MIN
);
827 /* analyze results */
828 results
= r600_buffer_map_sync_with_rings(ctx
, buffer
, PIPE_TRANSFER_READ
);
830 for(i
= 0; i
< ctx
->max_db
; i
++) {
831 /* at least highest bit will be set if backend is used */
832 if (results
[i
*4 + 1])
835 ctx
->ws
->buffer_unmap(buffer
->cs_buf
);
839 pipe_resource_reference((struct pipe_resource
**)&buffer
, NULL
);
842 ctx
->backend_mask
= mask
;
847 /* fallback to old method - set num_backends lower bits to 1 */
848 ctx
->backend_mask
= (~((uint32_t)0))>>(32-num_backends
);
852 void r600_query_init(struct r600_common_context
*rctx
)
854 rctx
->b
.create_query
= r600_create_query
;
855 rctx
->b
.destroy_query
= r600_destroy_query
;
856 rctx
->b
.begin_query
= r600_begin_query
;
857 rctx
->b
.end_query
= r600_end_query
;
858 rctx
->b
.get_query_result
= r600_get_query_result
;
860 if (((struct r600_common_screen
*)rctx
->b
.screen
)->info
.r600_num_backends
> 0)
861 rctx
->b
.render_condition
= r600_render_condition
;
863 LIST_INITHEAD(&rctx
->active_nontimer_queries
);