1 /**********************************************************
2 * Copyright 2008-2015 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
29 #include "util/u_bitmask.h"
30 #include "util/u_memory.h"
33 #include "svga_context.h"
34 #include "svga_screen.h"
35 #include "svga_resource_buffer.h"
36 #include "svga_winsys.h"
37 #include "svga_debug.h"
40 /* Fixme: want a public base class for all pipe structs, even if there
48 struct pipe_query base
;
49 unsigned type
; /**< PIPE_QUERY_x or SVGA_QUERY_x */
50 SVGA3dQueryType svga_type
; /**< SVGA3D_QUERYTYPE_x or unused */
52 unsigned id
; /** Per-context query identifier */
54 struct pipe_fence_handle
*fence
;
56 /** For PIPE_QUERY_OCCLUSION_COUNTER / SVGA3D_QUERYTYPE_OCCLUSION */
59 struct svga_winsys_buffer
*hwbuf
;
60 volatile SVGA3dQueryResult
*queryResult
;
63 struct svga_winsys_gb_query
*gb_query
;
64 SVGA3dDXQueryFlags flags
;
65 unsigned offset
; /**< offset to the gb_query memory */
66 struct pipe_query
*predicate
; /** The associated query that can be used for predicate */
68 /** For non-GPU SVGA_QUERY_x queries */
69 uint64_t begin_count
, end_count
;
74 static inline struct svga_query
*
75 svga_query( struct pipe_query
*q
)
77 return (struct svga_query
*)q
;
82 svga_get_query_result(struct pipe_context
*pipe
,
85 union pipe_query_result
*result
);
87 static enum pipe_error
88 define_query_vgpu9(struct svga_context
*svga
,
89 struct svga_query
*sq
)
91 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
93 sq
->hwbuf
= svga_winsys_buffer_create(svga
, 1,
94 SVGA_BUFFER_USAGE_PINNED
,
95 sizeof *sq
->queryResult
);
97 return PIPE_ERROR_OUT_OF_MEMORY
;
99 sq
->queryResult
= (SVGA3dQueryResult
*)
100 sws
->buffer_map(sws
, sq
->hwbuf
, PIPE_TRANSFER_WRITE
);
101 if (!sq
->queryResult
) {
102 sws
->buffer_destroy(sws
, sq
->hwbuf
);
103 return PIPE_ERROR_OUT_OF_MEMORY
;
106 sq
->queryResult
->totalSize
= sizeof *sq
->queryResult
;
107 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_NEW
;
109 /* We request the buffer to be pinned and assume it is always mapped.
110 * The reason is that we don't want to wait for fences when checking the
113 sws
->buffer_unmap(sws
, sq
->hwbuf
);
118 static enum pipe_error
119 begin_query_vgpu9(struct svga_context
*svga
, struct svga_query
*sq
)
121 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
122 enum pipe_error ret
= PIPE_OK
;
124 if (sq
->queryResult
->state
== SVGA3D_QUERYSTATE_PENDING
) {
125 /* The application doesn't care for the pending query result.
126 * We cannot let go of the existing buffer and just get a new one
127 * because its storage may be reused for other purposes and clobbered
128 * by the host when it determines the query result. So the only
129 * option here is to wait for the existing query's result -- not a
130 * big deal, given that no sane application would do this.
133 svga_get_query_result(&svga
->pipe
, &sq
->base
, TRUE
, (void*)&result
);
134 assert(sq
->queryResult
->state
!= SVGA3D_QUERYSTATE_PENDING
);
137 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_NEW
;
138 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
140 ret
= SVGA3D_BeginQuery(svga
->swc
, sq
->svga_type
);
141 if (ret
!= PIPE_OK
) {
142 svga_context_flush(svga
, NULL
);
143 ret
= SVGA3D_BeginQuery(svga
->swc
, sq
->svga_type
);
148 static enum pipe_error
149 end_query_vgpu9(struct svga_context
*svga
, struct svga_query
*sq
)
151 enum pipe_error ret
= PIPE_OK
;
153 /* Set to PENDING before sending EndQuery. */
154 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_PENDING
;
156 ret
= SVGA3D_EndQuery(svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
157 if (ret
!= PIPE_OK
) {
158 svga_context_flush(svga
, NULL
);
159 ret
= SVGA3D_EndQuery(svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
165 get_query_result_vgpu9(struct svga_context
*svga
, struct svga_query
*sq
,
166 boolean wait
, uint64_t *result
)
168 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
170 SVGA3dQueryState state
;
173 /* The query status won't be updated by the host unless
174 * SVGA_3D_CMD_WAIT_FOR_QUERY is emitted. Unfortunately this will cause
175 * a synchronous wait on the host.
177 ret
= SVGA3D_WaitForQuery(svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
178 if (ret
!= PIPE_OK
) {
179 svga_context_flush(svga
, NULL
);
180 ret
= SVGA3D_WaitForQuery(svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
182 assert (ret
== PIPE_OK
);
183 svga_context_flush(svga
, &sq
->fence
);
187 state
= sq
->queryResult
->state
;
188 if (state
== SVGA3D_QUERYSTATE_PENDING
) {
191 sws
->fence_finish(sws
, sq
->fence
, SVGA_FENCE_FLAG_QUERY
);
192 state
= sq
->queryResult
->state
;
195 assert(state
== SVGA3D_QUERYSTATE_SUCCEEDED
||
196 state
== SVGA3D_QUERYSTATE_FAILED
);
198 *result
= (uint64_t)sq
->queryResult
->result32
;
206 * There is one query mob allocated for each context to be shared by all
207 * query types. The mob is used to hold queries's state and result. Since
208 * each query result type is of different length, to ease the query allocation
209 * management, the mob is divided into memory blocks. Each memory block
210 * will hold queries of the same type. Multiple memory blocks can be allocated
211 * for a particular query type.
213 * Currently each memory block is of 184 bytes. We support up to 128
214 * memory blocks. The query memory size is arbitrary right now.
215 * Each occlusion query takes about 8 bytes. One memory block can accomodate
216 * 23 occlusion queries. 128 of those blocks can support up to 2944 occlusion
217 * queries. That seems reasonable for now. If we think this limit is
218 * not enough, we can increase the limit or try to grow the mob in runtime.
219 * Note, SVGA device does not impose one mob per context for queries,
220 * we could allocate multiple mobs for queries; however, wddm KMD does not
221 * currently support that.
223 * Also note that the GL guest driver does not issue any of the
224 * following commands: DXMoveQuery, DXBindAllQuery & DXReadbackAllQuery.
226 #define SVGA_QUERY_MEM_BLOCK_SIZE (sizeof(SVGADXQueryResultUnion) * 2)
227 #define SVGA_QUERY_MEM_SIZE (128 * SVGA_QUERY_MEM_BLOCK_SIZE)
229 struct svga_qmem_alloc_entry
231 unsigned start_offset
; /* start offset of the memory block */
232 unsigned block_index
; /* block index of the memory block */
233 unsigned query_size
; /* query size in this memory block */
234 unsigned nquery
; /* number of queries allocated */
235 struct util_bitmask
*alloc_mask
; /* allocation mask */
236 struct svga_qmem_alloc_entry
*next
; /* next memory block */
241 * Allocate a memory block from the query object memory
242 * \return -1 if out of memory, else index of the query memory block
245 allocate_query_block(struct svga_context
*svga
)
250 /* Find the next available query block */
251 index
= util_bitmask_add(svga
->gb_query_alloc_mask
);
253 if (index
== UTIL_BITMASK_INVALID_INDEX
)
256 offset
= index
* SVGA_QUERY_MEM_BLOCK_SIZE
;
257 if (offset
>= svga
->gb_query_len
) {
261 * All the memory blocks are allocated, lets see if there is
262 * any empty memory block around that can be freed up.
265 for (i
= 0; i
< SVGA_QUERY_MAX
&& index
== -1; i
++) {
266 struct svga_qmem_alloc_entry
*alloc_entry
;
267 struct svga_qmem_alloc_entry
*prev_alloc_entry
= NULL
;
269 alloc_entry
= svga
->gb_query_map
[i
];
270 while (alloc_entry
&& index
== -1) {
271 if (alloc_entry
->nquery
== 0) {
272 /* This memory block is empty, it can be recycled. */
273 if (prev_alloc_entry
) {
274 prev_alloc_entry
->next
= alloc_entry
->next
;
276 svga
->gb_query_map
[i
] = alloc_entry
->next
;
278 index
= alloc_entry
->block_index
;
280 prev_alloc_entry
= alloc_entry
;
281 alloc_entry
= alloc_entry
->next
;
291 * Allocate a slot in the specified memory block.
292 * All slots in this memory block are of the same size.
294 * \return -1 if out of memory, else index of the query slot
297 allocate_query_slot(struct svga_context
*svga
,
298 struct svga_qmem_alloc_entry
*alloc
)
303 /* Find the next available slot */
304 index
= util_bitmask_add(alloc
->alloc_mask
);
306 if (index
== UTIL_BITMASK_INVALID_INDEX
)
309 offset
= index
* alloc
->query_size
;
310 if (offset
>= SVGA_QUERY_MEM_BLOCK_SIZE
)
319 * Deallocate the specified slot in the memory block.
320 * If all slots are freed up, then deallocate the memory block
321 * as well, so it can be allocated for other query type
324 deallocate_query_slot(struct svga_context
*svga
,
325 struct svga_qmem_alloc_entry
*alloc
,
328 assert(index
!= UTIL_BITMASK_INVALID_INDEX
);
330 util_bitmask_clear(alloc
->alloc_mask
, index
);
334 * Don't worry about deallocating the empty memory block here.
335 * The empty memory block will be recycled when no more memory block
340 static struct svga_qmem_alloc_entry
*
341 allocate_query_block_entry(struct svga_context
*svga
,
344 struct svga_qmem_alloc_entry
*alloc_entry
;
345 int block_index
= -1;
347 block_index
= allocate_query_block(svga
);
348 if (block_index
== -1)
350 alloc_entry
= CALLOC_STRUCT(svga_qmem_alloc_entry
);
354 alloc_entry
->block_index
= block_index
;
355 alloc_entry
->start_offset
= block_index
* SVGA_QUERY_MEM_BLOCK_SIZE
;
356 alloc_entry
->nquery
= 0;
357 alloc_entry
->alloc_mask
= util_bitmask_create();
358 alloc_entry
->next
= NULL
;
359 alloc_entry
->query_size
= len
;
365 * Allocate a memory slot for a query of the specified type.
366 * It will first search through the memory blocks that are allocated
367 * for the query type. If no memory slot is available, it will try
368 * to allocate another memory block within the query object memory for
372 allocate_query(struct svga_context
*svga
,
373 SVGA3dQueryType type
,
376 struct svga_qmem_alloc_entry
*alloc_entry
;
380 assert(type
< SVGA_QUERY_MAX
);
382 alloc_entry
= svga
->gb_query_map
[type
];
386 * No query memory block has been allocated for this query type,
389 alloc_entry
= allocate_query_block_entry(svga
, len
);
392 svga
->gb_query_map
[type
] = alloc_entry
;
395 /* Allocate a slot within the memory block allocated for this query type */
396 slot_index
= allocate_query_slot(svga
, alloc_entry
);
398 if (slot_index
== -1) {
399 /* This query memory block is full, allocate another one */
400 alloc_entry
= allocate_query_block_entry(svga
, len
);
403 alloc_entry
->next
= svga
->gb_query_map
[type
];
404 svga
->gb_query_map
[type
] = alloc_entry
;
405 slot_index
= allocate_query_slot(svga
, alloc_entry
);
408 assert(slot_index
!= -1);
409 offset
= slot_index
* len
+ alloc_entry
->start_offset
;
416 * Deallocate memory slot allocated for the specified query
419 deallocate_query(struct svga_context
*svga
,
420 struct svga_query
*sq
)
422 struct svga_qmem_alloc_entry
*alloc_entry
;
424 unsigned offset
= sq
->offset
;
426 alloc_entry
= svga
->gb_query_map
[sq
->svga_type
];
428 while (alloc_entry
) {
429 if (offset
>= alloc_entry
->start_offset
&&
430 offset
< alloc_entry
->start_offset
+ SVGA_QUERY_MEM_BLOCK_SIZE
) {
432 /* The slot belongs to this memory block, deallocate it */
433 slot_index
= (offset
- alloc_entry
->start_offset
) /
434 alloc_entry
->query_size
;
435 deallocate_query_slot(svga
, alloc_entry
, slot_index
);
438 alloc_entry
= alloc_entry
->next
;
445 * Destroy the gb query object and all the related query structures
448 destroy_gb_query_obj(struct svga_context
*svga
)
450 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
453 for (i
= 0; i
< SVGA_QUERY_MAX
; i
++) {
454 struct svga_qmem_alloc_entry
*alloc_entry
, *next
;
455 alloc_entry
= svga
->gb_query_map
[i
];
456 while (alloc_entry
) {
457 next
= alloc_entry
->next
;
458 util_bitmask_destroy(alloc_entry
->alloc_mask
);
462 svga
->gb_query_map
[i
] = NULL
;
466 sws
->query_destroy(sws
, svga
->gb_query
);
467 svga
->gb_query
= NULL
;
469 util_bitmask_destroy(svga
->gb_query_alloc_mask
);
473 * Define query and create the gb query object if it is not already created.
474 * There is only one gb query object per context which will be shared by
475 * queries of all types.
477 static enum pipe_error
478 define_query_vgpu10(struct svga_context
*svga
,
479 struct svga_query
*sq
, int resultLen
)
481 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
483 enum pipe_error ret
= PIPE_OK
;
485 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
487 if (svga
->gb_query
== NULL
) {
488 /* Create a gb query object */
489 svga
->gb_query
= sws
->query_create(sws
, SVGA_QUERY_MEM_SIZE
);
491 return PIPE_ERROR_OUT_OF_MEMORY
;
492 svga
->gb_query_len
= SVGA_QUERY_MEM_SIZE
;
493 memset (svga
->gb_query_map
, 0, sizeof(svga
->gb_query_map
));
494 svga
->gb_query_alloc_mask
= util_bitmask_create();
496 /* Bind the query object to the context */
497 if (svga
->swc
->query_bind(svga
->swc
, svga
->gb_query
,
498 SVGA_QUERY_FLAG_SET
) != PIPE_OK
) {
499 svga_context_flush(svga
, NULL
);
500 svga
->swc
->query_bind(svga
->swc
, svga
->gb_query
,
501 SVGA_QUERY_FLAG_SET
);
505 sq
->gb_query
= svga
->gb_query
;
507 /* Allocate an integer ID for this query */
508 sq
->id
= util_bitmask_add(svga
->query_id_bm
);
509 if (sq
->id
== UTIL_BITMASK_INVALID_INDEX
)
510 return PIPE_ERROR_OUT_OF_MEMORY
;
512 /* Find a slot for this query in the gb object */
513 qlen
= resultLen
+ sizeof(SVGA3dQueryState
);
514 sq
->offset
= allocate_query(svga
, sq
->svga_type
, qlen
);
515 if (sq
->offset
== -1)
516 return PIPE_ERROR_OUT_OF_MEMORY
;
518 SVGA_DBG(DEBUG_QUERY
, " query type=%d qid=0x%x offset=%d\n",
519 sq
->svga_type
, sq
->id
, sq
->offset
);
522 * Send SVGA3D commands to define the query
524 ret
= SVGA3D_vgpu10_DefineQuery(svga
->swc
, sq
->id
, sq
->svga_type
, sq
->flags
);
525 if (ret
!= PIPE_OK
) {
526 svga_context_flush(svga
, NULL
);
527 ret
= SVGA3D_vgpu10_DefineQuery(svga
->swc
, sq
->id
, sq
->svga_type
, sq
->flags
);
530 return PIPE_ERROR_OUT_OF_MEMORY
;
532 ret
= SVGA3D_vgpu10_BindQuery(svga
->swc
, sq
->gb_query
, sq
->id
);
533 if (ret
!= PIPE_OK
) {
534 svga_context_flush(svga
, NULL
);
535 ret
= SVGA3D_vgpu10_BindQuery(svga
->swc
, sq
->gb_query
, sq
->id
);
537 assert(ret
== PIPE_OK
);
539 ret
= SVGA3D_vgpu10_SetQueryOffset(svga
->swc
, sq
->id
, sq
->offset
);
540 if (ret
!= PIPE_OK
) {
541 svga_context_flush(svga
, NULL
);
542 ret
= SVGA3D_vgpu10_SetQueryOffset(svga
->swc
, sq
->id
, sq
->offset
);
544 assert(ret
== PIPE_OK
);
549 static enum pipe_error
550 destroy_query_vgpu10(struct svga_context
*svga
, struct svga_query
*sq
)
554 ret
= SVGA3D_vgpu10_DestroyQuery(svga
->swc
, sq
->id
);
556 /* Deallocate the memory slot allocated for this query */
557 deallocate_query(svga
, sq
);
564 * Rebind queryies to the context.
567 rebind_vgpu10_query(struct svga_context
*svga
)
569 if (svga
->swc
->query_bind(svga
->swc
, svga
->gb_query
,
570 SVGA_QUERY_FLAG_REF
) != PIPE_OK
) {
571 svga_context_flush(svga
, NULL
);
572 svga
->swc
->query_bind(svga
->swc
, svga
->gb_query
,
573 SVGA_QUERY_FLAG_REF
);
576 svga
->rebind
.flags
.query
= FALSE
;
580 static enum pipe_error
581 begin_query_vgpu10(struct svga_context
*svga
, struct svga_query
*sq
)
583 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
584 enum pipe_error ret
= PIPE_OK
;
587 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
589 /* Initialize the query state to NEW */
590 status
= sws
->query_init(sws
, sq
->gb_query
, sq
->offset
, SVGA3D_QUERYSTATE_NEW
);
594 if (svga
->rebind
.flags
.query
) {
595 rebind_vgpu10_query(svga
);
598 /* Send the BeginQuery command to the device */
599 ret
= SVGA3D_vgpu10_BeginQuery(svga
->swc
, sq
->id
);
600 if (ret
!= PIPE_OK
) {
601 svga_context_flush(svga
, NULL
);
602 ret
= SVGA3D_vgpu10_BeginQuery(svga
->swc
, sq
->id
);
607 static enum pipe_error
608 end_query_vgpu10(struct svga_context
*svga
, struct svga_query
*sq
)
610 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
611 enum pipe_error ret
= PIPE_OK
;
613 if (svga
->rebind
.flags
.query
) {
614 rebind_vgpu10_query(svga
);
617 ret
= SVGA3D_vgpu10_EndQuery(svga
->swc
, sq
->id
);
618 if (ret
!= PIPE_OK
) {
619 svga_context_flush(svga
, NULL
);
620 ret
= SVGA3D_vgpu10_EndQuery(svga
->swc
, sq
->id
);
623 /* Finish fence is copied here from get_query_result_vgpu10. This helps
624 * with cases where svga_begin_query might be called again before
625 * svga_get_query_result, such as GL_TIME_ELAPSED.
628 svga_context_flush(svga
, &sq
->fence
);
630 sws
->fence_finish(sws
, sq
->fence
, SVGA_FENCE_FLAG_QUERY
);
636 get_query_result_vgpu10(struct svga_context
*svga
, struct svga_query
*sq
,
637 boolean wait
, void *result
, int resultLen
)
639 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
640 SVGA3dQueryState queryState
;
642 if (svga
->rebind
.flags
.query
) {
643 rebind_vgpu10_query(svga
);
646 sws
->query_get_result(sws
, sq
->gb_query
, sq
->offset
, &queryState
, result
, resultLen
);
648 if (queryState
== SVGA3D_QUERYSTATE_PENDING
) {
651 sws
->fence_finish(sws
, sq
->fence
, SVGA_FENCE_FLAG_QUERY
);
652 sws
->query_get_result(sws
, sq
->gb_query
, sq
->offset
, &queryState
, result
, resultLen
);
655 assert(queryState
== SVGA3D_QUERYSTATE_SUCCEEDED
||
656 queryState
== SVGA3D_QUERYSTATE_FAILED
);
661 static struct pipe_query
*
662 svga_create_query(struct pipe_context
*pipe
,
666 struct svga_context
*svga
= svga_context(pipe
);
667 struct svga_query
*sq
;
669 assert(query_type
< SVGA_QUERY_MAX
);
671 sq
= CALLOC_STRUCT(svga_query
);
675 /* Allocate an integer ID for the query */
676 sq
->id
= util_bitmask_add(svga
->query_id_bm
);
677 if (sq
->id
== UTIL_BITMASK_INVALID_INDEX
)
680 SVGA_DBG(DEBUG_QUERY
, "%s type=%d sq=0x%x id=%d\n", __FUNCTION__
,
681 query_type
, sq
, sq
->id
);
683 switch (query_type
) {
684 case PIPE_QUERY_OCCLUSION_COUNTER
:
685 sq
->svga_type
= SVGA3D_QUERYTYPE_OCCLUSION
;
686 if (svga_have_vgpu10(svga
)) {
687 define_query_vgpu10(svga
, sq
, sizeof(SVGADXOcclusionQueryResult
));
690 * In OpenGL, occlusion counter query can be used in conditional
691 * rendering; however, in DX10, only OCCLUSION_PREDICATE query can
692 * be used for predication. Hence, we need to create an occlusion
693 * predicate query along with the occlusion counter query. So when
694 * the occlusion counter query is used for predication, the associated
695 * query of occlusion predicate type will be used
696 * in the SetPredication command.
698 sq
->predicate
= svga_create_query(pipe
, PIPE_QUERY_OCCLUSION_PREDICATE
, index
);
701 define_query_vgpu9(svga
, sq
);
704 case PIPE_QUERY_OCCLUSION_PREDICATE
:
705 assert(svga_have_vgpu10(svga
));
706 sq
->svga_type
= SVGA3D_QUERYTYPE_OCCLUSIONPREDICATE
;
707 define_query_vgpu10(svga
, sq
, sizeof(SVGADXOcclusionPredicateQueryResult
));
709 case PIPE_QUERY_PRIMITIVES_GENERATED
:
710 case PIPE_QUERY_PRIMITIVES_EMITTED
:
711 case PIPE_QUERY_SO_STATISTICS
:
712 assert(svga_have_vgpu10(svga
));
713 sq
->svga_type
= SVGA3D_QUERYTYPE_STREAMOUTPUTSTATS
;
714 define_query_vgpu10(svga
, sq
,
715 sizeof(SVGADXStreamOutStatisticsQueryResult
));
717 case PIPE_QUERY_TIMESTAMP
:
718 assert(svga_have_vgpu10(svga
));
719 sq
->svga_type
= SVGA3D_QUERYTYPE_TIMESTAMP
;
720 define_query_vgpu10(svga
, sq
,
721 sizeof(SVGADXTimestampQueryResult
));
723 case SVGA_QUERY_NUM_DRAW_CALLS
:
724 case SVGA_QUERY_NUM_FALLBACKS
:
725 case SVGA_QUERY_NUM_FLUSHES
:
726 case SVGA_QUERY_NUM_VALIDATIONS
:
727 case SVGA_QUERY_MAP_BUFFER_TIME
:
728 case SVGA_QUERY_NUM_RESOURCES_MAPPED
:
729 case SVGA_QUERY_NUM_BYTES_UPLOADED
:
730 case SVGA_QUERY_COMMAND_BUFFER_SIZE
:
731 case SVGA_QUERY_FLUSH_TIME
:
732 case SVGA_QUERY_SURFACE_WRITE_FLUSHES
:
733 case SVGA_QUERY_MEMORY_USED
:
734 case SVGA_QUERY_NUM_SHADERS
:
735 case SVGA_QUERY_NUM_RESOURCES
:
736 case SVGA_QUERY_NUM_STATE_OBJECTS
:
737 case SVGA_QUERY_NUM_SURFACE_VIEWS
:
738 case SVGA_QUERY_NUM_GENERATE_MIPMAP
:
739 case SVGA_QUERY_NUM_READBACKS
:
740 case SVGA_QUERY_NUM_RESOURCE_UPDATES
:
741 case SVGA_QUERY_NUM_BUFFER_UPLOADS
:
742 case SVGA_QUERY_NUM_CONST_BUF_UPDATES
:
743 case SVGA_QUERY_NUM_CONST_UPDATES
:
746 assert(!"unexpected query type in svga_create_query()");
749 sq
->type
= query_type
;
759 svga_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
761 struct svga_context
*svga
= svga_context(pipe
);
762 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
763 struct svga_query
*sq
;
766 destroy_gb_query_obj(svga
);
772 SVGA_DBG(DEBUG_QUERY
, "%s sq=0x%x id=%d\n", __FUNCTION__
,
776 case PIPE_QUERY_OCCLUSION_COUNTER
:
777 if (svga_have_vgpu10(svga
)) {
778 /* make sure to also destroy any associated predicate query */
780 svga_destroy_query(pipe
, sq
->predicate
);
781 destroy_query_vgpu10(svga
, sq
);
783 sws
->buffer_destroy(sws
, sq
->hwbuf
);
785 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
787 case PIPE_QUERY_OCCLUSION_PREDICATE
:
788 assert(svga_have_vgpu10(svga
));
789 destroy_query_vgpu10(svga
, sq
);
790 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
792 case PIPE_QUERY_PRIMITIVES_GENERATED
:
793 case PIPE_QUERY_PRIMITIVES_EMITTED
:
794 case PIPE_QUERY_SO_STATISTICS
:
795 case PIPE_QUERY_TIMESTAMP
:
796 assert(svga_have_vgpu10(svga
));
797 destroy_query_vgpu10(svga
, sq
);
798 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
800 case SVGA_QUERY_NUM_DRAW_CALLS
:
801 case SVGA_QUERY_NUM_FALLBACKS
:
802 case SVGA_QUERY_NUM_FLUSHES
:
803 case SVGA_QUERY_NUM_VALIDATIONS
:
804 case SVGA_QUERY_MAP_BUFFER_TIME
:
805 case SVGA_QUERY_NUM_RESOURCES_MAPPED
:
806 case SVGA_QUERY_NUM_BYTES_UPLOADED
:
807 case SVGA_QUERY_COMMAND_BUFFER_SIZE
:
808 case SVGA_QUERY_FLUSH_TIME
:
809 case SVGA_QUERY_SURFACE_WRITE_FLUSHES
:
810 case SVGA_QUERY_MEMORY_USED
:
811 case SVGA_QUERY_NUM_SHADERS
:
812 case SVGA_QUERY_NUM_RESOURCES
:
813 case SVGA_QUERY_NUM_STATE_OBJECTS
:
814 case SVGA_QUERY_NUM_SURFACE_VIEWS
:
815 case SVGA_QUERY_NUM_GENERATE_MIPMAP
:
816 case SVGA_QUERY_NUM_READBACKS
:
817 case SVGA_QUERY_NUM_RESOURCE_UPDATES
:
818 case SVGA_QUERY_NUM_BUFFER_UPLOADS
:
819 case SVGA_QUERY_NUM_CONST_BUF_UPDATES
:
820 case SVGA_QUERY_NUM_CONST_UPDATES
:
824 assert(!"svga: unexpected query type in svga_destroy_query()");
827 /* Free the query id */
828 util_bitmask_clear(svga
->query_id_bm
, sq
->id
);
835 svga_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
837 struct svga_context
*svga
= svga_context(pipe
);
838 struct svga_query
*sq
= svga_query(q
);
842 assert(sq
->type
< SVGA_QUERY_MAX
);
844 SVGA_DBG(DEBUG_QUERY
, "%s sq=0x%x id=%d\n", __FUNCTION__
,
847 /* Need to flush out buffered drawing commands so that they don't
848 * get counted in the query results.
850 svga_hwtnl_flush_retry(svga
);
853 case PIPE_QUERY_OCCLUSION_COUNTER
:
854 if (svga_have_vgpu10(svga
)) {
855 ret
= begin_query_vgpu10(svga
, sq
);
856 /* also need to start the associated occlusion predicate query */
858 enum pipe_error status
;
859 status
= begin_query_vgpu10(svga
, svga_query(sq
->predicate
));
860 assert(status
== PIPE_OK
);
864 ret
= begin_query_vgpu9(svga
, sq
);
866 assert(ret
== PIPE_OK
);
869 case PIPE_QUERY_OCCLUSION_PREDICATE
:
870 assert(svga_have_vgpu10(svga
));
871 ret
= begin_query_vgpu10(svga
, sq
);
872 assert(ret
== PIPE_OK
);
874 case PIPE_QUERY_PRIMITIVES_GENERATED
:
875 case PIPE_QUERY_PRIMITIVES_EMITTED
:
876 case PIPE_QUERY_SO_STATISTICS
:
877 case PIPE_QUERY_TIMESTAMP
:
878 assert(svga_have_vgpu10(svga
));
879 ret
= begin_query_vgpu10(svga
, sq
);
880 assert(ret
== PIPE_OK
);
882 case SVGA_QUERY_NUM_DRAW_CALLS
:
883 sq
->begin_count
= svga
->hud
.num_draw_calls
;
885 case SVGA_QUERY_NUM_FALLBACKS
:
886 sq
->begin_count
= svga
->hud
.num_fallbacks
;
888 case SVGA_QUERY_NUM_FLUSHES
:
889 sq
->begin_count
= svga
->hud
.num_flushes
;
891 case SVGA_QUERY_NUM_VALIDATIONS
:
892 sq
->begin_count
= svga
->hud
.num_validations
;
894 case SVGA_QUERY_MAP_BUFFER_TIME
:
895 sq
->begin_count
= svga
->hud
.map_buffer_time
;
897 case SVGA_QUERY_NUM_RESOURCES_MAPPED
:
898 sq
->begin_count
= svga
->hud
.num_resources_mapped
;
900 case SVGA_QUERY_NUM_BYTES_UPLOADED
:
901 sq
->begin_count
= svga
->hud
.num_bytes_uploaded
;
903 case SVGA_QUERY_COMMAND_BUFFER_SIZE
:
904 sq
->begin_count
= svga
->hud
.command_buffer_size
;
906 case SVGA_QUERY_FLUSH_TIME
:
907 sq
->begin_count
= svga
->hud
.flush_time
;
909 case SVGA_QUERY_SURFACE_WRITE_FLUSHES
:
910 sq
->begin_count
= svga
->hud
.surface_write_flushes
;
912 case SVGA_QUERY_NUM_READBACKS
:
913 sq
->begin_count
= svga
->hud
.num_readbacks
;
915 case SVGA_QUERY_NUM_RESOURCE_UPDATES
:
916 sq
->begin_count
= svga
->hud
.num_resource_updates
;
918 case SVGA_QUERY_NUM_BUFFER_UPLOADS
:
919 sq
->begin_count
= svga
->hud
.num_buffer_uploads
;
921 case SVGA_QUERY_NUM_CONST_BUF_UPDATES
:
922 sq
->begin_count
= svga
->hud
.num_const_buf_updates
;
924 case SVGA_QUERY_NUM_CONST_UPDATES
:
925 sq
->begin_count
= svga
->hud
.num_const_updates
;
927 case SVGA_QUERY_MEMORY_USED
:
928 case SVGA_QUERY_NUM_SHADERS
:
929 case SVGA_QUERY_NUM_RESOURCES
:
930 case SVGA_QUERY_NUM_STATE_OBJECTS
:
931 case SVGA_QUERY_NUM_SURFACE_VIEWS
:
932 case SVGA_QUERY_NUM_GENERATE_MIPMAP
:
936 assert(!"unexpected query type in svga_begin_query()");
939 svga
->sq
[sq
->type
] = sq
;
946 svga_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
948 struct svga_context
*svga
= svga_context(pipe
);
949 struct svga_query
*sq
= svga_query(q
);
953 assert(sq
->type
< SVGA_QUERY_MAX
);
955 SVGA_DBG(DEBUG_QUERY
, "%s sq=0x%x id=%d\n", __FUNCTION__
,
958 if (sq
->type
== PIPE_QUERY_TIMESTAMP
&& svga
->sq
[sq
->type
] != sq
)
959 svga_begin_query(pipe
, q
);
961 svga_hwtnl_flush_retry(svga
);
963 assert(svga
->sq
[sq
->type
] == sq
);
966 case PIPE_QUERY_OCCLUSION_COUNTER
:
967 if (svga_have_vgpu10(svga
)) {
968 ret
= end_query_vgpu10(svga
, sq
);
969 /* also need to end the associated occlusion predicate query */
971 enum pipe_error status
;
972 status
= end_query_vgpu10(svga
, svga_query(sq
->predicate
));
973 assert(status
== PIPE_OK
);
977 ret
= end_query_vgpu9(svga
, sq
);
979 assert(ret
== PIPE_OK
);
981 /* TODO: Delay flushing. We don't really need to flush here, just ensure
982 * that there is one flush before svga_get_query_result attempts to get
985 svga_context_flush(svga
, NULL
);
987 case PIPE_QUERY_OCCLUSION_PREDICATE
:
988 assert(svga_have_vgpu10(svga
));
989 ret
= end_query_vgpu10(svga
, sq
);
990 assert(ret
== PIPE_OK
);
992 case PIPE_QUERY_PRIMITIVES_GENERATED
:
993 case PIPE_QUERY_PRIMITIVES_EMITTED
:
994 case PIPE_QUERY_SO_STATISTICS
:
995 case PIPE_QUERY_TIMESTAMP
:
996 assert(svga_have_vgpu10(svga
));
997 ret
= end_query_vgpu10(svga
, sq
);
998 assert(ret
== PIPE_OK
);
1000 case SVGA_QUERY_NUM_DRAW_CALLS
:
1001 sq
->end_count
= svga
->hud
.num_draw_calls
;
1003 case SVGA_QUERY_NUM_FALLBACKS
:
1004 sq
->end_count
= svga
->hud
.num_fallbacks
;
1006 case SVGA_QUERY_NUM_FLUSHES
:
1007 sq
->end_count
= svga
->hud
.num_flushes
;
1009 case SVGA_QUERY_NUM_VALIDATIONS
:
1010 sq
->end_count
= svga
->hud
.num_validations
;
1012 case SVGA_QUERY_MAP_BUFFER_TIME
:
1013 sq
->end_count
= svga
->hud
.map_buffer_time
;
1015 case SVGA_QUERY_NUM_RESOURCES_MAPPED
:
1016 sq
->end_count
= svga
->hud
.num_resources_mapped
;
1018 case SVGA_QUERY_NUM_BYTES_UPLOADED
:
1019 sq
->end_count
= svga
->hud
.num_bytes_uploaded
;
1021 case SVGA_QUERY_COMMAND_BUFFER_SIZE
:
1022 sq
->end_count
= svga
->hud
.command_buffer_size
;
1024 case SVGA_QUERY_FLUSH_TIME
:
1025 sq
->end_count
= svga
->hud
.flush_time
;
1027 case SVGA_QUERY_SURFACE_WRITE_FLUSHES
:
1028 sq
->end_count
= svga
->hud
.surface_write_flushes
;
1030 case SVGA_QUERY_NUM_READBACKS
:
1031 sq
->end_count
= svga
->hud
.num_readbacks
;
1033 case SVGA_QUERY_NUM_RESOURCE_UPDATES
:
1034 sq
->end_count
= svga
->hud
.num_resource_updates
;
1036 case SVGA_QUERY_NUM_BUFFER_UPLOADS
:
1037 sq
->end_count
= svga
->hud
.num_buffer_uploads
;
1039 case SVGA_QUERY_NUM_CONST_BUF_UPDATES
:
1040 sq
->end_count
= svga
->hud
.num_const_buf_updates
;
1042 case SVGA_QUERY_NUM_CONST_UPDATES
:
1043 sq
->end_count
= svga
->hud
.num_const_updates
;
1045 case SVGA_QUERY_MEMORY_USED
:
1046 case SVGA_QUERY_NUM_SHADERS
:
1047 case SVGA_QUERY_NUM_RESOURCES
:
1048 case SVGA_QUERY_NUM_STATE_OBJECTS
:
1049 case SVGA_QUERY_NUM_SURFACE_VIEWS
:
1050 case SVGA_QUERY_NUM_GENERATE_MIPMAP
:
1054 assert(!"unexpected query type in svga_end_query()");
1056 svga
->sq
[sq
->type
] = NULL
;
1061 svga_get_query_result(struct pipe_context
*pipe
,
1062 struct pipe_query
*q
,
1064 union pipe_query_result
*vresult
)
1066 struct svga_screen
*svgascreen
= svga_screen(pipe
->screen
);
1067 struct svga_context
*svga
= svga_context(pipe
);
1068 struct svga_query
*sq
= svga_query(q
);
1069 uint64_t *result
= (uint64_t *)vresult
;
1074 SVGA_DBG(DEBUG_QUERY
, "%s sq=0x%x id=%d wait: %d\n",
1075 __FUNCTION__
, sq
, sq
->id
, wait
);
1078 case PIPE_QUERY_OCCLUSION_COUNTER
:
1079 if (svga_have_vgpu10(svga
)) {
1080 SVGADXOcclusionQueryResult occResult
;
1081 ret
= get_query_result_vgpu10(svga
, sq
, wait
,
1082 (void *)&occResult
, sizeof(occResult
));
1083 *result
= (uint64_t)occResult
.samplesRendered
;
1085 ret
= get_query_result_vgpu9(svga
, sq
, wait
, (uint64_t *)result
);
1088 case PIPE_QUERY_OCCLUSION_PREDICATE
: {
1089 SVGADXOcclusionPredicateQueryResult occResult
;
1090 assert(svga_have_vgpu10(svga
));
1091 ret
= get_query_result_vgpu10(svga
, sq
, wait
,
1092 (void *)&occResult
, sizeof(occResult
));
1093 vresult
->b
= occResult
.anySamplesRendered
!= 0;
1096 case PIPE_QUERY_SO_STATISTICS
: {
1097 SVGADXStreamOutStatisticsQueryResult sResult
;
1098 struct pipe_query_data_so_statistics
*pResult
=
1099 (struct pipe_query_data_so_statistics
*)vresult
;
1101 assert(svga_have_vgpu10(svga
));
1102 ret
= get_query_result_vgpu10(svga
, sq
, wait
,
1103 (void *)&sResult
, sizeof(sResult
));
1104 pResult
->num_primitives_written
= sResult
.numPrimitivesWritten
;
1105 pResult
->primitives_storage_needed
= sResult
.numPrimitivesRequired
;
1108 case PIPE_QUERY_TIMESTAMP
: {
1109 SVGADXTimestampQueryResult sResult
;
1111 assert(svga_have_vgpu10(svga
));
1112 ret
= get_query_result_vgpu10(svga
, sq
, wait
,
1113 (void *)&sResult
, sizeof(sResult
));
1114 *result
= (uint64_t)sResult
.timestamp
;
1117 case PIPE_QUERY_PRIMITIVES_GENERATED
: {
1118 SVGADXStreamOutStatisticsQueryResult sResult
;
1120 assert(svga_have_vgpu10(svga
));
1121 ret
= get_query_result_vgpu10(svga
, sq
, wait
,
1122 (void *)&sResult
, sizeof sResult
);
1123 *result
= (uint64_t)sResult
.numPrimitivesRequired
;
1126 case PIPE_QUERY_PRIMITIVES_EMITTED
: {
1127 SVGADXStreamOutStatisticsQueryResult sResult
;
1129 assert(svga_have_vgpu10(svga
));
1130 ret
= get_query_result_vgpu10(svga
, sq
, wait
,
1131 (void *)&sResult
, sizeof sResult
);
1132 *result
= (uint64_t)sResult
.numPrimitivesWritten
;
1135 /* These are per-frame counters */
1136 case SVGA_QUERY_NUM_DRAW_CALLS
:
1137 case SVGA_QUERY_NUM_FALLBACKS
:
1138 case SVGA_QUERY_NUM_FLUSHES
:
1139 case SVGA_QUERY_NUM_VALIDATIONS
:
1140 case SVGA_QUERY_MAP_BUFFER_TIME
:
1141 case SVGA_QUERY_NUM_RESOURCES_MAPPED
:
1142 case SVGA_QUERY_NUM_BYTES_UPLOADED
:
1143 case SVGA_QUERY_COMMAND_BUFFER_SIZE
:
1144 case SVGA_QUERY_FLUSH_TIME
:
1145 case SVGA_QUERY_SURFACE_WRITE_FLUSHES
:
1146 case SVGA_QUERY_NUM_READBACKS
:
1147 case SVGA_QUERY_NUM_RESOURCE_UPDATES
:
1148 case SVGA_QUERY_NUM_BUFFER_UPLOADS
:
1149 case SVGA_QUERY_NUM_CONST_BUF_UPDATES
:
1150 case SVGA_QUERY_NUM_CONST_UPDATES
:
1151 vresult
->u64
= sq
->end_count
- sq
->begin_count
;
1153 /* These are running total counters */
1154 case SVGA_QUERY_MEMORY_USED
:
1155 vresult
->u64
= svgascreen
->hud
.total_resource_bytes
;
1157 case SVGA_QUERY_NUM_SHADERS
:
1158 vresult
->u64
= svga
->hud
.num_shaders
;
1160 case SVGA_QUERY_NUM_RESOURCES
:
1161 vresult
->u64
= svgascreen
->hud
.num_resources
;
1163 case SVGA_QUERY_NUM_STATE_OBJECTS
:
1164 vresult
->u64
= svga
->hud
.num_state_objects
;
1166 case SVGA_QUERY_NUM_SURFACE_VIEWS
:
1167 vresult
->u64
= svga
->hud
.num_surface_views
;
1169 case SVGA_QUERY_NUM_GENERATE_MIPMAP
:
1170 vresult
->u64
= svga
->hud
.num_generate_mipmap
;
1173 assert(!"unexpected query type in svga_get_query_result");
1176 SVGA_DBG(DEBUG_QUERY
, "%s result %d\n", __FUNCTION__
, *((uint64_t *)vresult
));
1182 svga_render_condition(struct pipe_context
*pipe
, struct pipe_query
*q
,
1183 boolean condition
, uint mode
)
1185 struct svga_context
*svga
= svga_context(pipe
);
1186 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
1187 struct svga_query
*sq
= svga_query(q
);
1188 SVGA3dQueryId queryId
;
1189 enum pipe_error ret
;
1191 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
1193 assert(svga_have_vgpu10(svga
));
1195 queryId
= SVGA3D_INVALID_ID
;
1198 assert(sq
->svga_type
== SVGA3D_QUERYTYPE_OCCLUSION
||
1199 sq
->svga_type
== SVGA3D_QUERYTYPE_OCCLUSIONPREDICATE
);
1201 if (sq
->svga_type
== SVGA3D_QUERYTYPE_OCCLUSION
) {
1202 assert(sq
->predicate
);
1204 * For conditional rendering, make sure to use the associated
1207 sq
= svga_query(sq
->predicate
);
1211 if ((mode
== PIPE_RENDER_COND_WAIT
||
1212 mode
== PIPE_RENDER_COND_BY_REGION_WAIT
) && sq
->fence
) {
1213 sws
->fence_finish(sws
, sq
->fence
, SVGA_FENCE_FLAG_QUERY
);
1217 ret
= SVGA3D_vgpu10_SetPredication(svga
->swc
, queryId
,
1218 (uint32
) condition
);
1219 if (ret
!= PIPE_OK
) {
1220 svga_context_flush(svga
, NULL
);
1221 ret
= SVGA3D_vgpu10_SetPredication(svga
->swc
, queryId
,
1222 (uint32
) condition
);
1228 * This function is a workaround because we lack the ability to query
1229 * renderer's time synchornously.
1232 svga_get_timestamp(struct pipe_context
*pipe
)
1234 struct pipe_query
*q
= svga_create_query(pipe
, PIPE_QUERY_TIMESTAMP
, 0);
1235 union pipe_query_result result
;
1237 svga_begin_query(pipe
, q
);
1238 svga_end_query(pipe
,q
);
1239 svga_get_query_result(pipe
, q
, TRUE
, &result
);
1240 svga_destroy_query(pipe
, q
);
1247 svga_init_query_functions(struct svga_context
*svga
)
1249 svga
->pipe
.create_query
= svga_create_query
;
1250 svga
->pipe
.destroy_query
= svga_destroy_query
;
1251 svga
->pipe
.begin_query
= svga_begin_query
;
1252 svga
->pipe
.end_query
= svga_end_query
;
1253 svga
->pipe
.get_query_result
= svga_get_query_result
;
1254 svga
->pipe
.render_condition
= svga_render_condition
;
1255 svga
->pipe
.get_timestamp
= svga_get_timestamp
;