2 * Copyright © 2008 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
28 /** @file brw_queryobj.c
30 * Support for query objects (GL_ARB_occlusion_query, GL_ARB_timer_query,
31 * GL_EXT_transform_feedback, and friends).
33 * The hardware provides a PIPE_CONTROL command that can report the number of
34 * fragments that passed the depth test, or the hardware timer. They are
35 * appropriately synced with the stage of the pipeline for our extensions'
38 #include "main/imports.h"
40 #include "brw_context.h"
41 #include "brw_defines.h"
42 #include "brw_state.h"
43 #include "intel_batchbuffer.h"
44 #include "intel_reg.h"
47 * Emit PIPE_CONTROLs to write the current GPU timestamp into a buffer.
50 write_timestamp(struct intel_context
*intel
, drm_intel_bo
*query_bo
, int idx
)
52 if (intel
->gen
>= 6) {
53 /* Emit workaround flushes: */
54 if (intel
->gen
== 6) {
55 /* The timestamp write below is a non-zero post-sync op, which on
56 * Gen6 necessitates a CS stall. CS stalls need stall at scoreboard
57 * set. See the comments for intel_emit_post_sync_nonzero_flush().
60 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
61 OUT_BATCH(PIPE_CONTROL_CS_STALL
| PIPE_CONTROL_STALL_AT_SCOREBOARD
);
68 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (5 - 2));
69 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP
);
71 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
72 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
73 idx
* sizeof(uint64_t));
79 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2) |
80 PIPE_CONTROL_WRITE_TIMESTAMP
);
82 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
83 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
84 idx
* sizeof(uint64_t));
92 * Emit PIPE_CONTROLs to write the PS_DEPTH_COUNT register into a buffer.
95 write_depth_count(struct intel_context
*intel
, drm_intel_bo
*query_bo
, int idx
)
97 assert(intel
->gen
< 6);
100 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2) |
101 PIPE_CONTROL_DEPTH_STALL
| PIPE_CONTROL_WRITE_DEPTH_COUNT
);
102 /* This object could be mapped cacheable, but we don't have an exposed
103 * mechanism to support that. Since it's going uncached, tell GEM that
104 * we're writing to it. The usual clflush should be all that's required
105 * to pick up the results.
108 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
109 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
110 (idx
* sizeof(uint64_t)));
117 * Wait on the query object's BO and calculate the final result.
120 brw_queryobj_get_results(struct gl_context
*ctx
,
121 struct brw_query_object
*query
)
123 struct intel_context
*intel
= intel_context(ctx
);
128 assert(intel
->gen
< 6);
130 if (query
->bo
== NULL
)
133 /* If the application has requested the query result, but this batch is
134 * still contributing to it, flush it now so the results will be present
137 if (drm_intel_bo_references(intel
->batch
.bo
, query
->bo
))
138 intel_batchbuffer_flush(intel
);
140 if (unlikely(intel
->perf_debug
)) {
141 if (drm_intel_bo_busy(query
->bo
)) {
142 perf_debug("Stalling on the GPU waiting for a query object.\n");
146 drm_intel_bo_map(query
->bo
, false);
147 results
= query
->bo
->virtual;
148 switch (query
->Base
.Target
) {
149 case GL_TIME_ELAPSED_EXT
:
150 /* The query BO contains the starting and ending timestamps.
151 * Subtract the two and convert to nanoseconds.
153 query
->Base
.Result
+= 1000 * ((results
[1] >> 32) - (results
[0] >> 32));
157 /* The query BO contains a single timestamp value in results[0]. */
158 query
->Base
.Result
= 1000 * (results
[0] >> 32);
161 case GL_SAMPLES_PASSED_ARB
:
162 /* Loop over pairs of values from the BO, which are the PS_DEPTH_COUNT
163 * value at the start and end of the batchbuffer. Subtract them to
164 * get the number of fragments which passed the depth test in each
165 * individual batch, and add those differences up to get the number
166 * of fragments for the entire query.
168 * Note that query->Base.Result may already be non-zero. We may have
169 * run out of space in the query's BO and allocated a new one. If so,
170 * this function was already called to accumulate the results so far.
172 for (i
= 0; i
< query
->last_index
; i
++) {
173 query
->Base
.Result
+= results
[i
* 2 + 1] - results
[i
* 2];
177 case GL_ANY_SAMPLES_PASSED
:
178 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
179 /* If the starting and ending PS_DEPTH_COUNT from any of the batches
180 * differ, then some fragments passed the depth test.
182 for (i
= 0; i
< query
->last_index
; i
++) {
183 if (results
[i
* 2 + 1] != results
[i
* 2]) {
184 query
->Base
.Result
= GL_TRUE
;
190 case GL_PRIMITIVES_GENERATED
:
191 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
192 /* We don't actually query the hardware for this value, so query->bo
193 * should always be NULL and execution should never reach here.
195 assert(!"Unreachable");
199 assert(!"Unrecognized query target in brw_queryobj_get_results()");
202 drm_intel_bo_unmap(query
->bo
);
204 /* Now that we've processed the data stored in the query's buffer object,
207 drm_intel_bo_unreference(query
->bo
);
212 * The NewQueryObject() driver hook.
214 * Allocates and initializes a new query object.
216 static struct gl_query_object
*
217 brw_new_query_object(struct gl_context
*ctx
, GLuint id
)
219 struct brw_query_object
*query
;
221 query
= calloc(1, sizeof(struct brw_query_object
));
224 query
->Base
.Result
= 0;
225 query
->Base
.Active
= false;
226 query
->Base
.Ready
= true;
232 * The DeleteQuery() driver hook.
235 brw_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
237 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
239 drm_intel_bo_unreference(query
->bo
);
244 * Gen4-5 driver hook for glBeginQuery().
246 * Initializes driver structures and emits any GPU commands required to begin
247 * recording data for the query.
250 brw_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
252 struct brw_context
*brw
= brw_context(ctx
);
253 struct intel_context
*intel
= intel_context(ctx
);
254 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
256 assert(intel
->gen
< 6);
258 switch (query
->Base
.Target
) {
259 case GL_TIME_ELAPSED_EXT
:
260 /* For timestamp queries, we record the starting time right away so that
261 * we measure the full time between BeginQuery and EndQuery. There's
262 * some debate about whether this is the right thing to do. Our decision
263 * is based on the following text from the ARB_timer_query extension:
265 * "(5) Should the extension measure total time elapsed between the full
266 * completion of the BeginQuery and EndQuery commands, or just time
267 * spent in the graphics library?
269 * RESOLVED: This extension will measure the total time elapsed
270 * between the full completion of these commands. Future extensions
271 * may implement a query to determine time elapsed at different stages
272 * of the graphics pipeline."
274 * We write a starting timestamp now (at index 0). At EndQuery() time,
275 * we'll write a second timestamp (at index 1), and subtract the two to
276 * obtain the time elapsed. Notably, this includes time elapsed while
277 * the system was doing other work, such as running other applications.
279 drm_intel_bo_unreference(query
->bo
);
280 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "timer query", 4096, 4096);
281 write_timestamp(intel
, query
->bo
, 0);
284 case GL_ANY_SAMPLES_PASSED
:
285 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
286 case GL_SAMPLES_PASSED_ARB
:
287 /* For occlusion queries, we delay taking an initial sample until the
288 * first drawing occurs in this batch. See the reasoning in the comments
289 * for brw_emit_query_begin() below.
291 * Since we're starting a new query, we need to be sure to throw away
292 * any previous occlusion query results.
294 drm_intel_bo_unreference(query
->bo
);
296 query
->last_index
= -1;
298 brw
->query
.obj
= query
;
300 /* Depth statistics on Gen4 require strange workarounds, so we try to
301 * avoid them when necessary. They're required for occlusion queries,
302 * so turn them on now.
305 brw
->state
.dirty
.brw
|= BRW_NEW_STATS_WM
;
308 case GL_PRIMITIVES_GENERATED
:
309 /* We don't actually query the hardware for this value; we keep track of
310 * it a software counter. So just reset the counter.
312 brw
->sol
.primitives_generated
= 0;
313 brw
->sol
.counting_primitives_generated
= true;
316 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
317 /* We don't actually query the hardware for this value; we keep track of
318 * it a software counter. So just reset the counter.
320 brw
->sol
.primitives_written
= 0;
321 brw
->sol
.counting_primitives_written
= true;
325 assert(!"Unrecognized query target in brw_begin_query()");
331 * Gen4-5 driver hook for glEndQuery().
333 * Emits GPU commands to record a final query value, ending any data capturing.
334 * However, the final result isn't necessarily available until the GPU processes
335 * those commands. brw_queryobj_get_results() processes the captured data to
336 * produce the final result.
339 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
341 struct brw_context
*brw
= brw_context(ctx
);
342 struct intel_context
*intel
= intel_context(ctx
);
343 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
345 assert(intel
->gen
< 6);
347 switch (query
->Base
.Target
) {
348 case GL_TIME_ELAPSED_EXT
:
349 /* Write the final timestamp. */
350 write_timestamp(intel
, query
->bo
, 1);
353 case GL_ANY_SAMPLES_PASSED
:
354 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
355 case GL_SAMPLES_PASSED_ARB
:
357 /* No query->bo means that EndQuery was called after BeginQuery with no
358 * intervening drawing. Rather than doing nothing at all here in this
359 * case, we emit the query_begin and query_end state to the
360 * hardware. This is to guarantee that waiting on the result of this
361 * empty state will cause all previous queries to complete at all, as
362 * required by the specification:
364 * It must always be true that if any query object
365 * returns a result available of TRUE, all queries of the
366 * same type issued prior to that query must also return
367 * TRUE. [Open GL 4.3 (Core Profile) Section 4.2.1]
370 brw_emit_query_begin(brw
);
375 brw_emit_query_end(brw
);
377 brw
->query
.obj
= NULL
;
380 brw
->state
.dirty
.brw
|= BRW_NEW_STATS_WM
;
383 case GL_PRIMITIVES_GENERATED
:
384 /* We don't actually query the hardware for this value; we keep track of
385 * it in a software counter. So just read the counter and store it in
388 query
->Base
.Result
= brw
->sol
.primitives_generated
;
389 brw
->sol
.counting_primitives_generated
= false;
391 /* And set query->bo to NULL so that this query won't try to wait
392 * for any rendering to complete.
397 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
398 /* We don't actually query the hardware for this value; we keep track of
399 * it in a software counter. So just read the counter and store it in
402 query
->Base
.Result
= brw
->sol
.primitives_written
;
403 brw
->sol
.counting_primitives_written
= false;
405 /* And set query->bo to NULL so that this query won't try to wait
406 * for any rendering to complete.
412 assert(!"Unrecognized query target in brw_end_query()");
418 * The Gen4-5 WaitQuery() driver hook.
420 * Wait for a query result to become available and return it. This is the
421 * backing for glGetQueryObjectiv() with the GL_QUERY_RESULT pname.
423 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
425 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
427 assert(intel_context(ctx
)->gen
< 6);
429 brw_queryobj_get_results(ctx
, query
);
430 query
->Base
.Ready
= true;
434 * The Gen4-5 CheckQuery() driver hook.
436 * Checks whether a query result is ready yet. If not, flushes.
437 * This is the backing for glGetQueryObjectiv()'s QUERY_RESULT_AVAILABLE pname.
439 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
441 struct intel_context
*intel
= intel_context(ctx
);
442 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
444 assert(intel
->gen
< 6);
446 /* From the GL_ARB_occlusion_query spec:
448 * "Instead of allowing for an infinite loop, performing a
449 * QUERY_RESULT_AVAILABLE_ARB will perform a flush if the result is
450 * not ready yet on the first time it is queried. This ensures that
451 * the async query will return true in finite time.
453 if (query
->bo
&& drm_intel_bo_references(intel
->batch
.bo
, query
->bo
))
454 intel_batchbuffer_flush(intel
);
456 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
457 brw_queryobj_get_results(ctx
, query
);
458 query
->Base
.Ready
= true;
463 * Ensure there query's BO has enough space to store a new pair of values.
465 * If not, gather the existing BO's results and create a new buffer of the
469 ensure_bo_has_space(struct gl_context
*ctx
, struct brw_query_object
*query
)
471 struct intel_context
*intel
= intel_context(ctx
);
473 assert(intel
->gen
< 6);
475 if (!query
->bo
|| query
->last_index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
477 if (query
->bo
!= NULL
) {
478 /* The old query BO did not have enough space, so we allocated a new
479 * one. Gather the results so far (adding up the differences) and
480 * release the old BO.
482 brw_queryobj_get_results(ctx
, query
);
485 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "query", 4096, 1);
486 query
->last_index
= 0;
491 * Record the PS_DEPTH_COUNT value (for occlusion queries) just before
494 * In a pre-hardware context world, the single PS_DEPTH_COUNT register is
495 * shared among all applications using the GPU. However, our query value
496 * needs to only include fragments generated by our application/GL context.
498 * To accommodate this, we record PS_DEPTH_COUNT at the start and end of
499 * each batchbuffer (technically, the first primitive drawn and flush time).
500 * Subtracting each pair of values calculates the change in PS_DEPTH_COUNT
501 * caused by a batchbuffer. Since there is no preemption inside batches,
502 * this is guaranteed to only measure the effects of our current application.
504 * Adding each of these differences (in case drawing is done over many batches)
505 * produces the final expected value.
507 * In a world with hardware contexts, PS_DEPTH_COUNT is saved and restored
508 * as part of the context state, so this is unnecessary, and skipped.
511 brw_emit_query_begin(struct brw_context
*brw
)
513 struct intel_context
*intel
= &brw
->intel
;
514 struct gl_context
*ctx
= &intel
->ctx
;
515 struct brw_query_object
*query
= brw
->query
.obj
;
520 /* Skip if we're not doing any queries, or we've already recorded the
521 * initial query value for this batchbuffer.
523 if (!query
|| brw
->query
.begin_emitted
)
526 ensure_bo_has_space(ctx
, query
);
528 write_depth_count(intel
, query
->bo
, query
->last_index
* 2);
530 brw
->query
.begin_emitted
= true;
534 * Called at batchbuffer flush to get an ending PS_DEPTH_COUNT
535 * (for non-hardware context platforms).
537 * See the explanation in brw_emit_query_begin().
540 brw_emit_query_end(struct brw_context
*brw
)
542 struct intel_context
*intel
= &brw
->intel
;
543 struct brw_query_object
*query
= brw
->query
.obj
;
548 if (!brw
->query
.begin_emitted
)
551 write_depth_count(intel
, query
->bo
, query
->last_index
* 2 + 1);
553 brw
->query
.begin_emitted
= false;
558 * Driver hook for glQueryCounter().
560 * This handles GL_TIMESTAMP queries, which perform a pipelined read of the
561 * current GPU time. This is unlike GL_TIME_ELAPSED, which measures the
562 * time while the query is active.
565 brw_query_counter(struct gl_context
*ctx
, struct gl_query_object
*q
)
567 struct intel_context
*intel
= intel_context(ctx
);
568 struct brw_query_object
*query
= (struct brw_query_object
*) q
;
570 assert(q
->Target
== GL_TIMESTAMP
);
572 drm_intel_bo_unreference(query
->bo
);
573 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "timestamp query", 4096, 4096);
574 write_timestamp(intel
, query
->bo
, 0);
578 * Read the TIMESTAMP register immediately (in a non-pipelined fashion).
580 * This is used to implement the GetTimestamp() driver hook.
583 brw_get_timestamp(struct gl_context
*ctx
)
585 struct intel_context
*intel
= intel_context(ctx
);
588 drm_intel_reg_read(intel
->bufmgr
, TIMESTAMP
, &result
);
590 /* See logic in brw_queryobj_get_results() */
591 result
= result
>> 32;
593 result
&= (1ull << 36) - 1;
598 /* Initialize query object functions used on all generations. */
599 void brw_init_common_queryobj_functions(struct dd_function_table
*functions
)
601 functions
->NewQueryObject
= brw_new_query_object
;
602 functions
->DeleteQuery
= brw_delete_query
;
603 functions
->QueryCounter
= brw_query_counter
;
604 functions
->GetTimestamp
= brw_get_timestamp
;
607 /* Initialize Gen4/5-specific query object functions. */
608 void gen4_init_queryobj_functions(struct dd_function_table
*functions
)
610 functions
->BeginQuery
= brw_begin_query
;
611 functions
->EndQuery
= brw_end_query
;
612 functions
->CheckQuery
= brw_check_query
;
613 functions
->WaitQuery
= brw_wait_query
;