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 ctx
->need_gfx_cs_space(&ctx
->b
, query
->num_cs_dw
* 2, TRUE
);
157 /* Get a new query buffer if needed. */
158 if (query
->buffer
.results_end
+ query
->result_size
> query
->buffer
.buf
->b
.b
.width0
) {
159 struct r600_query_buffer
*qbuf
= MALLOC_STRUCT(r600_query_buffer
);
160 *qbuf
= query
->buffer
;
161 query
->buffer
.buf
= r600_new_query_buffer(ctx
, query
->type
);
162 query
->buffer
.results_end
= 0;
163 query
->buffer
.previous
= qbuf
;
166 /* emit begin query */
167 va
= r600_resource_va(ctx
->b
.screen
, (void*)query
->buffer
.buf
);
168 va
+= query
->buffer
.results_end
;
170 switch (query
->type
) {
171 case PIPE_QUERY_OCCLUSION_COUNTER
:
172 case PIPE_QUERY_OCCLUSION_PREDICATE
:
173 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE
, 2, 0);
174 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1);
175 cs
->buf
[cs
->cdw
++] = va
;
176 cs
->buf
[cs
->cdw
++] = (va
>> 32UL) & 0xFF;
178 case PIPE_QUERY_PRIMITIVES_EMITTED
:
179 case PIPE_QUERY_PRIMITIVES_GENERATED
:
180 case PIPE_QUERY_SO_STATISTICS
:
181 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
182 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE
, 2, 0);
183 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_SAMPLE_STREAMOUTSTATS
) | EVENT_INDEX(3);
184 cs
->buf
[cs
->cdw
++] = va
;
185 cs
->buf
[cs
->cdw
++] = (va
>> 32UL) & 0xFF;
187 case PIPE_QUERY_TIME_ELAPSED
:
188 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE_EOP
, 4, 0);
189 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_CACHE_FLUSH_AND_INV_TS_EVENT
) | EVENT_INDEX(5);
190 cs
->buf
[cs
->cdw
++] = va
;
191 cs
->buf
[cs
->cdw
++] = (3 << 29) | ((va
>> 32UL) & 0xFF);
192 cs
->buf
[cs
->cdw
++] = 0;
193 cs
->buf
[cs
->cdw
++] = 0;
195 case PIPE_QUERY_PIPELINE_STATISTICS
:
196 if (!ctx
->num_pipelinestat_queries
) {
197 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE
, 0, 0);
198 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_PIPELINESTAT_START
) | EVENT_INDEX(0);
200 ctx
->num_pipelinestat_queries
++;
201 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE
, 2, 0);
202 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2);
203 cs
->buf
[cs
->cdw
++] = va
;
204 cs
->buf
[cs
->cdw
++] = (va
>> 32UL) & 0xFF;
209 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_NOP
, 0, 0);
210 cs
->buf
[cs
->cdw
++] = r600_context_bo_reloc(ctx
, &ctx
->rings
.gfx
, query
->buffer
.buf
, RADEON_USAGE_WRITE
);
212 if (!r600_is_timer_query(query
->type
)) {
213 ctx
->num_cs_dw_nontimer_queries_suspend
+= query
->num_cs_dw
;
217 static void r600_emit_query_end(struct r600_common_context
*ctx
, struct r600_query
*query
)
219 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
222 /* The queries which need begin already called this in begin_query. */
223 if (!r600_query_needs_begin(query
->type
)) {
224 ctx
->need_gfx_cs_space(&ctx
->b
, query
->num_cs_dw
, FALSE
);
227 va
= r600_resource_va(ctx
->b
.screen
, (void*)query
->buffer
.buf
);
229 switch (query
->type
) {
230 case PIPE_QUERY_OCCLUSION_COUNTER
:
231 case PIPE_QUERY_OCCLUSION_PREDICATE
:
232 va
+= query
->buffer
.results_end
+ 8;
233 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE
, 2, 0);
234 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1);
235 cs
->buf
[cs
->cdw
++] = va
;
236 cs
->buf
[cs
->cdw
++] = (va
>> 32UL) & 0xFF;
238 case PIPE_QUERY_PRIMITIVES_EMITTED
:
239 case PIPE_QUERY_PRIMITIVES_GENERATED
:
240 case PIPE_QUERY_SO_STATISTICS
:
241 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
242 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
243 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE
, 2, 0);
244 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_SAMPLE_STREAMOUTSTATS
) | EVENT_INDEX(3);
245 cs
->buf
[cs
->cdw
++] = va
;
246 cs
->buf
[cs
->cdw
++] = (va
>> 32UL) & 0xFF;
248 case PIPE_QUERY_TIME_ELAPSED
:
249 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
251 case PIPE_QUERY_TIMESTAMP
:
252 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE_EOP
, 4, 0);
253 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_CACHE_FLUSH_AND_INV_TS_EVENT
) | EVENT_INDEX(5);
254 cs
->buf
[cs
->cdw
++] = va
;
255 cs
->buf
[cs
->cdw
++] = (3 << 29) | ((va
>> 32UL) & 0xFF);
256 cs
->buf
[cs
->cdw
++] = 0;
257 cs
->buf
[cs
->cdw
++] = 0;
259 case PIPE_QUERY_PIPELINE_STATISTICS
:
260 assert(ctx
->num_pipelinestat_queries
> 0);
261 ctx
->num_pipelinestat_queries
--;
262 if (!ctx
->num_pipelinestat_queries
) {
263 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE
, 0, 0);
264 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_PIPELINESTAT_STOP
) | EVENT_INDEX(0);
266 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
267 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE
, 2, 0);
268 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2);
269 cs
->buf
[cs
->cdw
++] = va
;
270 cs
->buf
[cs
->cdw
++] = (va
>> 32UL) & 0xFF;
275 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_NOP
, 0, 0);
276 cs
->buf
[cs
->cdw
++] = r600_context_bo_reloc(ctx
, &ctx
->rings
.gfx
, query
->buffer
.buf
, RADEON_USAGE_WRITE
);
278 query
->buffer
.results_end
+= query
->result_size
;
280 if (r600_query_needs_begin(query
->type
)) {
281 if (!r600_is_timer_query(query
->type
)) {
282 ctx
->num_cs_dw_nontimer_queries_suspend
-= query
->num_cs_dw
;
286 r600_update_occlusion_query_state(ctx
, query
->type
, -1);
289 static void r600_emit_query_predication(struct r600_common_context
*ctx
, struct r600_query
*query
,
290 int operation
, bool flag_wait
)
292 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
294 if (operation
== PREDICATION_OP_CLEAR
) {
295 ctx
->need_gfx_cs_space(&ctx
->b
, 3, FALSE
);
297 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_SET_PREDICATION
, 1, 0);
298 cs
->buf
[cs
->cdw
++] = 0;
299 cs
->buf
[cs
->cdw
++] = PRED_OP(PREDICATION_OP_CLEAR
);
301 struct r600_query_buffer
*qbuf
;
305 /* Find how many results there are. */
307 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
308 count
+= qbuf
->results_end
/ query
->result_size
;
311 ctx
->need_gfx_cs_space(&ctx
->b
, 5 * count
, TRUE
);
313 op
= PRED_OP(operation
) | PREDICATION_DRAW_VISIBLE
|
314 (flag_wait
? PREDICATION_HINT_WAIT
: PREDICATION_HINT_NOWAIT_DRAW
);
316 /* emit predicate packets for all data blocks */
317 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
318 unsigned results_base
= 0;
319 uint64_t va
= r600_resource_va(ctx
->b
.screen
, &qbuf
->buf
->b
.b
);
321 while (results_base
< qbuf
->results_end
) {
322 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_SET_PREDICATION
, 1, 0);
323 cs
->buf
[cs
->cdw
++] = (va
+ results_base
) & 0xFFFFFFFFUL
;
324 cs
->buf
[cs
->cdw
++] = op
| (((va
+ results_base
) >> 32UL) & 0xFF);
325 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_NOP
, 0, 0);
326 cs
->buf
[cs
->cdw
++] = r600_context_bo_reloc(ctx
, &ctx
->rings
.gfx
, qbuf
->buf
, RADEON_USAGE_READ
);
327 results_base
+= query
->result_size
;
329 /* set CONTINUE bit for all packets except the first */
330 op
|= PREDICATION_CONTINUE
;
336 static struct pipe_query
*r600_create_query(struct pipe_context
*ctx
, unsigned query_type
)
338 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
339 struct r600_query
*query
;
340 bool skip_allocation
= false;
342 query
= CALLOC_STRUCT(r600_query
);
346 query
->type
= query_type
;
348 switch (query_type
) {
349 case PIPE_QUERY_OCCLUSION_COUNTER
:
350 case PIPE_QUERY_OCCLUSION_PREDICATE
:
351 query
->result_size
= 16 * rctx
->max_db
;
352 query
->num_cs_dw
= 6;
354 case PIPE_QUERY_TIME_ELAPSED
:
355 query
->result_size
= 16;
356 query
->num_cs_dw
= 8;
358 case PIPE_QUERY_TIMESTAMP
:
359 query
->result_size
= 8;
360 query
->num_cs_dw
= 8;
362 case PIPE_QUERY_PRIMITIVES_EMITTED
:
363 case PIPE_QUERY_PRIMITIVES_GENERATED
:
364 case PIPE_QUERY_SO_STATISTICS
:
365 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
366 /* NumPrimitivesWritten, PrimitiveStorageNeeded. */
367 query
->result_size
= 32;
368 query
->num_cs_dw
= 6;
370 case PIPE_QUERY_PIPELINE_STATISTICS
:
371 /* 11 values on EG, 8 on R600. */
372 query
->result_size
= (rctx
->chip_class
>= EVERGREEN
? 11 : 8) * 16;
373 query
->num_cs_dw
= 8;
375 /* Non-GPU queries. */
376 case R600_QUERY_DRAW_CALLS
:
377 case R600_QUERY_REQUESTED_VRAM
:
378 case R600_QUERY_REQUESTED_GTT
:
379 case R600_QUERY_BUFFER_WAIT_TIME
:
380 skip_allocation
= true;
388 if (!skip_allocation
) {
389 query
->buffer
.buf
= r600_new_query_buffer(rctx
, query_type
);
390 if (!query
->buffer
.buf
) {
395 return (struct pipe_query
*)query
;
398 static void r600_destroy_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
400 struct r600_query
*rquery
= (struct r600_query
*)query
;
401 struct r600_query_buffer
*prev
= rquery
->buffer
.previous
;
403 /* Release all query buffers. */
405 struct r600_query_buffer
*qbuf
= prev
;
406 prev
= prev
->previous
;
407 pipe_resource_reference((struct pipe_resource
**)&qbuf
->buf
, NULL
);
411 pipe_resource_reference((struct pipe_resource
**)&rquery
->buffer
.buf
, NULL
);
415 static void r600_begin_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
417 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
418 struct r600_query
*rquery
= (struct r600_query
*)query
;
419 struct r600_query_buffer
*prev
= rquery
->buffer
.previous
;
421 if (!r600_query_needs_begin(rquery
->type
)) {
426 /* Non-GPU queries. */
427 switch (rquery
->type
) {
428 case R600_QUERY_DRAW_CALLS
:
429 rquery
->begin_result
= rctx
->num_draw_calls
;
431 case R600_QUERY_REQUESTED_VRAM
:
432 case R600_QUERY_REQUESTED_GTT
:
433 rquery
->begin_result
= 0;
435 case R600_QUERY_BUFFER_WAIT_TIME
:
436 rquery
->begin_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_BUFFER_WAIT_TIME_NS
);
440 /* Discard the old query buffers. */
442 struct r600_query_buffer
*qbuf
= prev
;
443 prev
= prev
->previous
;
444 pipe_resource_reference((struct pipe_resource
**)&qbuf
->buf
, NULL
);
448 /* Obtain a new buffer if the current one can't be mapped without a stall. */
449 if (r600_rings_is_buffer_referenced(rctx
, rquery
->buffer
.buf
->cs_buf
, RADEON_USAGE_READWRITE
) ||
450 rctx
->ws
->buffer_is_busy(rquery
->buffer
.buf
->buf
, RADEON_USAGE_READWRITE
)) {
451 pipe_resource_reference((struct pipe_resource
**)&rquery
->buffer
.buf
, NULL
);
452 rquery
->buffer
.buf
= r600_new_query_buffer(rctx
, rquery
->type
);
455 rquery
->buffer
.results_end
= 0;
456 rquery
->buffer
.previous
= NULL
;
458 r600_emit_query_begin(rctx
, rquery
);
460 if (!r600_is_timer_query(rquery
->type
)) {
461 LIST_ADDTAIL(&rquery
->list
, &rctx
->active_nontimer_queries
);
465 static void r600_end_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
467 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
468 struct r600_query
*rquery
= (struct r600_query
*)query
;
470 /* Non-GPU queries. */
471 switch (rquery
->type
) {
472 case R600_QUERY_DRAW_CALLS
:
473 rquery
->end_result
= rctx
->num_draw_calls
;
475 case R600_QUERY_REQUESTED_VRAM
:
476 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_REQUESTED_VRAM_MEMORY
);
478 case R600_QUERY_REQUESTED_GTT
:
479 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_REQUESTED_GTT_MEMORY
);
481 case R600_QUERY_BUFFER_WAIT_TIME
:
482 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_BUFFER_WAIT_TIME_NS
);
486 r600_emit_query_end(rctx
, rquery
);
488 if (r600_query_needs_begin(rquery
->type
) && !r600_is_timer_query(rquery
->type
)) {
489 LIST_DELINIT(&rquery
->list
);
493 static unsigned r600_query_read_result(char *map
, unsigned start_index
, unsigned end_index
,
494 bool test_status_bit
)
496 uint32_t *current_result
= (uint32_t*)map
;
499 start
= (uint64_t)current_result
[start_index
] |
500 (uint64_t)current_result
[start_index
+1] << 32;
501 end
= (uint64_t)current_result
[end_index
] |
502 (uint64_t)current_result
[end_index
+1] << 32;
504 if (!test_status_bit
||
505 ((start
& 0x8000000000000000UL
) && (end
& 0x8000000000000000UL
))) {
511 static boolean
r600_get_query_buffer_result(struct r600_common_context
*ctx
,
512 struct r600_query
*query
,
513 struct r600_query_buffer
*qbuf
,
515 union pipe_query_result
*result
)
517 unsigned results_base
= 0;
520 /* Non-GPU queries. */
521 switch (query
->type
) {
522 case R600_QUERY_DRAW_CALLS
:
523 case R600_QUERY_REQUESTED_VRAM
:
524 case R600_QUERY_REQUESTED_GTT
:
525 case R600_QUERY_BUFFER_WAIT_TIME
:
526 result
->u64
= query
->end_result
- query
->begin_result
;
530 map
= r600_buffer_map_sync_with_rings(ctx
, qbuf
->buf
,
532 (wait
? 0 : PIPE_TRANSFER_DONTBLOCK
));
536 /* count all results across all data blocks */
537 switch (query
->type
) {
538 case PIPE_QUERY_OCCLUSION_COUNTER
:
539 while (results_base
!= qbuf
->results_end
) {
541 r600_query_read_result(map
+ results_base
, 0, 2, true);
545 case PIPE_QUERY_OCCLUSION_PREDICATE
:
546 while (results_base
!= qbuf
->results_end
) {
547 result
->b
= result
->b
||
548 r600_query_read_result(map
+ results_base
, 0, 2, true) != 0;
552 case PIPE_QUERY_TIME_ELAPSED
:
553 while (results_base
!= qbuf
->results_end
) {
555 r600_query_read_result(map
+ results_base
, 0, 2, false);
556 results_base
+= query
->result_size
;
559 case PIPE_QUERY_TIMESTAMP
:
561 uint32_t *current_result
= (uint32_t*)map
;
562 result
->u64
= (uint64_t)current_result
[0] |
563 (uint64_t)current_result
[1] << 32;
566 case PIPE_QUERY_PRIMITIVES_EMITTED
:
567 /* SAMPLE_STREAMOUTSTATS stores this structure:
569 * u64 NumPrimitivesWritten;
570 * u64 PrimitiveStorageNeeded;
572 * We only need NumPrimitivesWritten here. */
573 while (results_base
!= qbuf
->results_end
) {
575 r600_query_read_result(map
+ results_base
, 2, 6, true);
576 results_base
+= query
->result_size
;
579 case PIPE_QUERY_PRIMITIVES_GENERATED
:
580 /* Here we read PrimitiveStorageNeeded. */
581 while (results_base
!= qbuf
->results_end
) {
583 r600_query_read_result(map
+ results_base
, 0, 4, true);
584 results_base
+= query
->result_size
;
587 case PIPE_QUERY_SO_STATISTICS
:
588 while (results_base
!= qbuf
->results_end
) {
589 result
->so_statistics
.num_primitives_written
+=
590 r600_query_read_result(map
+ results_base
, 2, 6, true);
591 result
->so_statistics
.primitives_storage_needed
+=
592 r600_query_read_result(map
+ results_base
, 0, 4, true);
593 results_base
+= query
->result_size
;
596 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
597 while (results_base
!= qbuf
->results_end
) {
598 result
->b
= result
->b
||
599 r600_query_read_result(map
+ results_base
, 2, 6, true) !=
600 r600_query_read_result(map
+ results_base
, 0, 4, true);
601 results_base
+= query
->result_size
;
604 case PIPE_QUERY_PIPELINE_STATISTICS
:
605 if (ctx
->chip_class
>= EVERGREEN
) {
606 while (results_base
!= qbuf
->results_end
) {
607 result
->pipeline_statistics
.ps_invocations
+=
608 r600_query_read_result(map
+ results_base
, 0, 22, false);
609 result
->pipeline_statistics
.c_primitives
+=
610 r600_query_read_result(map
+ results_base
, 2, 24, false);
611 result
->pipeline_statistics
.c_invocations
+=
612 r600_query_read_result(map
+ results_base
, 4, 26, false);
613 result
->pipeline_statistics
.vs_invocations
+=
614 r600_query_read_result(map
+ results_base
, 6, 28, false);
615 result
->pipeline_statistics
.gs_invocations
+=
616 r600_query_read_result(map
+ results_base
, 8, 30, false);
617 result
->pipeline_statistics
.gs_primitives
+=
618 r600_query_read_result(map
+ results_base
, 10, 32, false);
619 result
->pipeline_statistics
.ia_primitives
+=
620 r600_query_read_result(map
+ results_base
, 12, 34, false);
621 result
->pipeline_statistics
.ia_vertices
+=
622 r600_query_read_result(map
+ results_base
, 14, 36, false);
623 result
->pipeline_statistics
.hs_invocations
+=
624 r600_query_read_result(map
+ results_base
, 16, 38, false);
625 result
->pipeline_statistics
.ds_invocations
+=
626 r600_query_read_result(map
+ results_base
, 18, 40, false);
627 result
->pipeline_statistics
.cs_invocations
+=
628 r600_query_read_result(map
+ results_base
, 20, 42, false);
629 results_base
+= query
->result_size
;
632 while (results_base
!= qbuf
->results_end
) {
633 result
->pipeline_statistics
.ps_invocations
+=
634 r600_query_read_result(map
+ results_base
, 0, 16, false);
635 result
->pipeline_statistics
.c_primitives
+=
636 r600_query_read_result(map
+ results_base
, 2, 18, false);
637 result
->pipeline_statistics
.c_invocations
+=
638 r600_query_read_result(map
+ results_base
, 4, 20, false);
639 result
->pipeline_statistics
.vs_invocations
+=
640 r600_query_read_result(map
+ results_base
, 6, 22, false);
641 result
->pipeline_statistics
.gs_invocations
+=
642 r600_query_read_result(map
+ results_base
, 8, 24, false);
643 result
->pipeline_statistics
.gs_primitives
+=
644 r600_query_read_result(map
+ results_base
, 10, 26, false);
645 result
->pipeline_statistics
.ia_primitives
+=
646 r600_query_read_result(map
+ results_base
, 12, 28, false);
647 result
->pipeline_statistics
.ia_vertices
+=
648 r600_query_read_result(map
+ results_base
, 14, 30, false);
649 results_base
+= query
->result_size
;
652 #if 0 /* for testing */
653 printf("Pipeline stats: IA verts=%llu, IA prims=%llu, VS=%llu, HS=%llu, "
654 "DS=%llu, GS=%llu, GS prims=%llu, Clipper=%llu, "
655 "Clipper prims=%llu, PS=%llu, CS=%llu\n",
656 result
->pipeline_statistics
.ia_vertices
,
657 result
->pipeline_statistics
.ia_primitives
,
658 result
->pipeline_statistics
.vs_invocations
,
659 result
->pipeline_statistics
.hs_invocations
,
660 result
->pipeline_statistics
.ds_invocations
,
661 result
->pipeline_statistics
.gs_invocations
,
662 result
->pipeline_statistics
.gs_primitives
,
663 result
->pipeline_statistics
.c_invocations
,
664 result
->pipeline_statistics
.c_primitives
,
665 result
->pipeline_statistics
.ps_invocations
,
666 result
->pipeline_statistics
.cs_invocations
);
673 ctx
->ws
->buffer_unmap(qbuf
->buf
->cs_buf
);
677 static boolean
r600_get_query_result(struct pipe_context
*ctx
,
678 struct pipe_query
*query
,
679 boolean wait
, union pipe_query_result
*result
)
681 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
682 struct r600_query
*rquery
= (struct r600_query
*)query
;
683 struct r600_query_buffer
*qbuf
;
685 util_query_clear_result(result
, rquery
->type
);
687 for (qbuf
= &rquery
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
688 if (!r600_get_query_buffer_result(rctx
, rquery
, qbuf
, wait
, result
)) {
693 /* Convert the time to expected units. */
694 if (rquery
->type
== PIPE_QUERY_TIME_ELAPSED
||
695 rquery
->type
== PIPE_QUERY_TIMESTAMP
) {
696 result
->u64
= (1000000 * result
->u64
) / rctx
->screen
->info
.r600_clock_crystal_freq
;
701 static void r600_render_condition(struct pipe_context
*ctx
,
702 struct pipe_query
*query
,
706 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
707 struct r600_query
*rquery
= (struct r600_query
*)query
;
708 bool wait_flag
= false;
710 rctx
->current_render_cond
= query
;
711 rctx
->current_render_cond_cond
= condition
;
712 rctx
->current_render_cond_mode
= mode
;
715 if (rctx
->predicate_drawing
) {
716 rctx
->predicate_drawing
= false;
717 r600_emit_query_predication(rctx
, NULL
, PREDICATION_OP_CLEAR
, false);
722 if (mode
== PIPE_RENDER_COND_WAIT
||
723 mode
== PIPE_RENDER_COND_BY_REGION_WAIT
) {
727 rctx
->predicate_drawing
= true;
729 switch (rquery
->type
) {
730 case PIPE_QUERY_OCCLUSION_COUNTER
:
731 case PIPE_QUERY_OCCLUSION_PREDICATE
:
732 r600_emit_query_predication(rctx
, rquery
, PREDICATION_OP_ZPASS
, wait_flag
);
734 case PIPE_QUERY_PRIMITIVES_EMITTED
:
735 case PIPE_QUERY_PRIMITIVES_GENERATED
:
736 case PIPE_QUERY_SO_STATISTICS
:
737 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
738 r600_emit_query_predication(rctx
, rquery
, PREDICATION_OP_PRIMCOUNT
, wait_flag
);
745 void r600_suspend_nontimer_queries(struct r600_common_context
*ctx
)
747 struct r600_query
*query
;
749 LIST_FOR_EACH_ENTRY(query
, &ctx
->active_nontimer_queries
, list
) {
750 r600_emit_query_end(ctx
, query
);
752 assert(ctx
->num_cs_dw_nontimer_queries_suspend
== 0);
755 void r600_resume_nontimer_queries(struct r600_common_context
*ctx
)
757 struct r600_query
*query
;
759 assert(ctx
->num_cs_dw_nontimer_queries_suspend
== 0);
761 LIST_FOR_EACH_ENTRY(query
, &ctx
->active_nontimer_queries
, list
) {
762 r600_emit_query_begin(ctx
, query
);
766 /* Get backends mask */
767 void r600_query_init_backend_mask(struct r600_common_context
*ctx
)
769 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
770 struct r600_resource
*buffer
;
772 unsigned num_backends
= ctx
->screen
->info
.r600_num_backends
;
773 unsigned i
, mask
= 0;
776 /* if backend_map query is supported by the kernel */
777 if (ctx
->screen
->info
.r600_backend_map_valid
) {
778 unsigned num_tile_pipes
= ctx
->screen
->info
.r600_num_tile_pipes
;
779 unsigned backend_map
= ctx
->screen
->info
.r600_backend_map
;
780 unsigned item_width
, item_mask
;
782 if (ctx
->chip_class
>= EVERGREEN
) {
790 while(num_tile_pipes
--) {
791 i
= backend_map
& item_mask
;
793 backend_map
>>= item_width
;
796 ctx
->backend_mask
= mask
;
801 /* otherwise backup path for older kernels */
803 /* create buffer for event data */
804 buffer
= (struct r600_resource
*)
805 pipe_buffer_create(ctx
->b
.screen
, PIPE_BIND_CUSTOM
,
806 PIPE_USAGE_STAGING
, ctx
->max_db
*16);
809 va
= r600_resource_va(ctx
->b
.screen
, (void*)buffer
);
811 /* initialize buffer with zeroes */
812 results
= r600_buffer_map_sync_with_rings(ctx
, buffer
, PIPE_TRANSFER_WRITE
);
814 memset(results
, 0, ctx
->max_db
* 4 * 4);
815 ctx
->ws
->buffer_unmap(buffer
->cs_buf
);
817 /* emit EVENT_WRITE for ZPASS_DONE */
818 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_EVENT_WRITE
, 2, 0);
819 cs
->buf
[cs
->cdw
++] = EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1);
820 cs
->buf
[cs
->cdw
++] = va
;
821 cs
->buf
[cs
->cdw
++] = (va
>> 32UL) & 0xFF;
823 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_NOP
, 0, 0);
824 cs
->buf
[cs
->cdw
++] = r600_context_bo_reloc(ctx
, &ctx
->rings
.gfx
, buffer
, RADEON_USAGE_WRITE
);
826 /* analyze results */
827 results
= r600_buffer_map_sync_with_rings(ctx
, buffer
, PIPE_TRANSFER_READ
);
829 for(i
= 0; i
< ctx
->max_db
; i
++) {
830 /* at least highest bit will be set if backend is used */
831 if (results
[i
*4 + 1])
834 ctx
->ws
->buffer_unmap(buffer
->cs_buf
);
838 pipe_resource_reference((struct pipe_resource
**)&buffer
, NULL
);
841 ctx
->backend_mask
= mask
;
846 /* fallback to old method - set num_backends lower bits to 1 */
847 ctx
->backend_mask
= (~((uint32_t)0))>>(32-num_backends
);
851 void r600_query_init(struct r600_common_context
*rctx
)
853 rctx
->b
.create_query
= r600_create_query
;
854 rctx
->b
.destroy_query
= r600_destroy_query
;
855 rctx
->b
.begin_query
= r600_begin_query
;
856 rctx
->b
.end_query
= r600_end_query
;
857 rctx
->b
.get_query_result
= r600_get_query_result
;
859 if (((struct r600_common_screen
*)rctx
->b
.screen
)->info
.r600_num_backends
> 0)
860 rctx
->b
.render_condition
= r600_render_condition
;
862 LIST_INITHEAD(&rctx
->active_nontimer_queries
);