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 if (intel
->gen
>= 6) {
98 /* Emit Sandybridge workaround flush: */
100 intel_emit_post_sync_nonzero_flush(intel
);
103 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (5 - 2));
104 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
105 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
107 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
108 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
109 (idx
* sizeof(uint64_t)));
115 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2) |
116 PIPE_CONTROL_DEPTH_STALL
|
117 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
118 /* This object could be mapped cacheable, but we don't have an exposed
119 * mechanism to support that. Since it's going uncached, tell GEM that
120 * we're writing to it. The usual clflush should be all that's required
121 * to pick up the results.
124 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
125 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
126 (idx
* sizeof(uint64_t)));
134 * Wait on the query object's BO and calculate the final result.
137 brw_queryobj_get_results(struct gl_context
*ctx
,
138 struct brw_query_object
*query
)
140 struct intel_context
*intel
= intel_context(ctx
);
145 if (query
->bo
== NULL
)
148 /* If the application has requested the query result, but this batch is
149 * still contributing to it, flush it now so the results will be present
152 if (drm_intel_bo_references(intel
->batch
.bo
, query
->bo
))
153 intel_batchbuffer_flush(intel
);
155 if (unlikely(intel
->perf_debug
)) {
156 if (drm_intel_bo_busy(query
->bo
)) {
157 perf_debug("Stalling on the GPU waiting for a query object.\n");
161 drm_intel_bo_map(query
->bo
, false);
162 results
= query
->bo
->virtual;
163 switch (query
->Base
.Target
) {
164 case GL_TIME_ELAPSED_EXT
:
165 /* The query BO contains the starting and ending timestamps.
166 * Subtract the two and convert to nanoseconds.
169 query
->Base
.Result
+= 80 * (results
[1] - results
[0]);
171 query
->Base
.Result
+= 1000 * ((results
[1] >> 32) - (results
[0] >> 32));
175 /* The query BO contains a single timestamp value in results[0]. */
176 if (intel
->gen
>= 6) {
177 /* Our timer is a clock that increments every 80ns (regardless of
178 * other clock scaling in the system). The timestamp register we can
179 * read for glGetTimestamp() masks out the top 32 bits, so we do that
180 * here too to let the two counters be compared against each other.
182 * If we just multiplied that 32 bits of data by 80, it would roll
183 * over at a non-power-of-two, so an application couldn't use
184 * GL_QUERY_COUNTER_BITS to handle rollover correctly. Instead, we
185 * report 36 bits and truncate at that (rolling over 5 times as often
186 * as the HW counter), and when the 32-bit counter rolls over, it
187 * happens to also be at a rollover in the reported value from near
190 * The low 32 bits rolls over in ~343 seconds. Our 36-bit result
191 * rolls over every ~69 seconds.
193 query
->Base
.Result
= 80 * (results
[0] & 0xffffffff);
194 query
->Base
.Result
&= (1ull << 36) - 1;
196 query
->Base
.Result
= 1000 * (results
[0] >> 32);
200 case GL_SAMPLES_PASSED_ARB
:
201 /* Loop over pairs of values from the BO, which are the PS_DEPTH_COUNT
202 * value at the start and end of the batchbuffer. Subtract them to
203 * get the number of fragments which passed the depth test in each
204 * individual batch, and add those differences up to get the number
205 * of fragments for the entire query.
207 * Note that query->Base.Result may already be non-zero. We may have
208 * run out of space in the query's BO and allocated a new one. If so,
209 * this function was already called to accumulate the results so far.
211 for (i
= 0; i
< query
->last_index
; i
++) {
212 query
->Base
.Result
+= results
[i
* 2 + 1] - results
[i
* 2];
216 case GL_ANY_SAMPLES_PASSED
:
217 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
218 /* If the starting and ending PS_DEPTH_COUNT from any of the batches
219 * differ, then some fragments passed the depth test.
221 for (i
= 0; i
< query
->last_index
; i
++) {
222 if (results
[i
* 2 + 1] != results
[i
* 2]) {
223 query
->Base
.Result
= GL_TRUE
;
229 case GL_PRIMITIVES_GENERATED
:
230 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
231 /* We don't actually query the hardware for this value, so query->bo
232 * should always be NULL and execution should never reach here.
234 assert(!"Unreachable");
238 assert(!"Unrecognized query target in brw_queryobj_get_results()");
241 drm_intel_bo_unmap(query
->bo
);
243 /* Now that we've processed the data stored in the query's buffer object,
246 drm_intel_bo_unreference(query
->bo
);
251 * The NewQueryObject() driver hook.
253 * Allocates and initializes a new query object.
255 static struct gl_query_object
*
256 brw_new_query_object(struct gl_context
*ctx
, GLuint id
)
258 struct brw_query_object
*query
;
260 query
= calloc(1, sizeof(struct brw_query_object
));
263 query
->Base
.Result
= 0;
264 query
->Base
.Active
= false;
265 query
->Base
.Ready
= true;
271 * The DeleteQuery() driver hook.
274 brw_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
276 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
278 drm_intel_bo_unreference(query
->bo
);
283 * Driver hook for glBeginQuery().
285 * Initializes driver structures and emits any GPU commands required to begin
286 * recording data for the query.
289 brw_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
291 struct brw_context
*brw
= brw_context(ctx
);
292 struct intel_context
*intel
= intel_context(ctx
);
293 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
295 switch (query
->Base
.Target
) {
296 case GL_TIME_ELAPSED_EXT
:
297 /* For timestamp queries, we record the starting time right away so that
298 * we measure the full time between BeginQuery and EndQuery. There's
299 * some debate about whether this is the right thing to do. Our decision
300 * is based on the following text from the ARB_timer_query extension:
302 * "(5) Should the extension measure total time elapsed between the full
303 * completion of the BeginQuery and EndQuery commands, or just time
304 * spent in the graphics library?
306 * RESOLVED: This extension will measure the total time elapsed
307 * between the full completion of these commands. Future extensions
308 * may implement a query to determine time elapsed at different stages
309 * of the graphics pipeline."
311 * We write a starting timestamp now (at index 0). At EndQuery() time,
312 * we'll write a second timestamp (at index 1), and subtract the two to
313 * obtain the time elapsed. Notably, this includes time elapsed while
314 * the system was doing other work, such as running other applications.
316 drm_intel_bo_unreference(query
->bo
);
317 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "timer query", 4096, 4096);
318 write_timestamp(intel
, query
->bo
, 0);
321 case GL_ANY_SAMPLES_PASSED
:
322 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
323 case GL_SAMPLES_PASSED_ARB
:
324 /* For occlusion queries, we delay taking an initial sample until the
325 * first drawing occurs in this batch. See the reasoning in the comments
326 * for brw_emit_query_begin() below.
328 * Since we're starting a new query, we need to be sure to throw away
329 * any previous occlusion query results.
331 drm_intel_bo_unreference(query
->bo
);
333 query
->last_index
= -1;
335 brw
->query
.obj
= query
;
337 /* Depth statistics on Gen4 require strange workarounds, so we try to
338 * avoid them when necessary. They're required for occlusion queries,
339 * so turn them on now.
344 case GL_PRIMITIVES_GENERATED
:
345 /* We don't actually query the hardware for this value; we keep track of
346 * it a software counter. So just reset the counter.
348 brw
->sol
.primitives_generated
= 0;
349 brw
->sol
.counting_primitives_generated
= true;
352 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
353 /* We don't actually query the hardware for this value; we keep track of
354 * it a software counter. So just reset the counter.
356 brw
->sol
.primitives_written
= 0;
357 brw
->sol
.counting_primitives_written
= true;
361 assert(!"Unrecognized query target in brw_begin_query()");
367 * Driver hook for glEndQuery().
369 * Emits GPU commands to record a final query value, ending any data capturing.
370 * However, the final result isn't necessarily available until the GPU processes
371 * those commands. brw_queryobj_get_results() processes the captured data to
372 * produce the final result.
375 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
377 struct brw_context
*brw
= brw_context(ctx
);
378 struct intel_context
*intel
= intel_context(ctx
);
379 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
381 switch (query
->Base
.Target
) {
382 case GL_TIME_ELAPSED_EXT
:
383 /* Write the final timestamp. */
384 write_timestamp(intel
, query
->bo
, 1);
387 case GL_ANY_SAMPLES_PASSED
:
388 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
389 case GL_SAMPLES_PASSED_ARB
:
391 /* No query->bo means that EndQuery was called after BeginQuery with no
392 * intervening drawing. Rather than doing nothing at all here in this
393 * case, we emit the query_begin and query_end state to the
394 * hardware. This is to guarantee that waiting on the result of this
395 * empty state will cause all previous queries to complete at all, as
396 * required by the specification:
398 * It must always be true that if any query object
399 * returns a result available of TRUE, all queries of the
400 * same type issued prior to that query must also return
401 * TRUE. [Open GL 4.3 (Core Profile) Section 4.2.1]
404 brw_emit_query_begin(brw
);
409 brw_emit_query_end(brw
);
411 brw
->query
.obj
= NULL
;
416 case GL_PRIMITIVES_GENERATED
:
417 /* We don't actually query the hardware for this value; we keep track of
418 * it in a software counter. So just read the counter and store it in
421 query
->Base
.Result
= brw
->sol
.primitives_generated
;
422 brw
->sol
.counting_primitives_generated
= false;
424 /* And set query->bo to NULL so that this query won't try to wait
425 * for any rendering to complete.
430 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
431 /* We don't actually query the hardware for this value; we keep track of
432 * it in a software counter. So just read the counter and store it in
435 query
->Base
.Result
= brw
->sol
.primitives_written
;
436 brw
->sol
.counting_primitives_written
= false;
438 /* And set query->bo to NULL so that this query won't try to wait
439 * for any rendering to complete.
445 assert(!"Unrecognized query target in brw_end_query()");
451 * The WaitQuery() driver hook.
453 * Wait for a query result to become available and return it. This is the
454 * backing for glGetQueryObjectiv() with the GL_QUERY_RESULT pname.
456 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
458 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
460 brw_queryobj_get_results(ctx
, query
);
461 query
->Base
.Ready
= true;
465 * The CheckQuery() driver hook.
467 * Checks whether a query result is ready yet. If not, flushes.
468 * This is the backing for glGetQueryObjectiv()'s QUERY_RESULT_AVAILABLE pname.
470 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
472 struct intel_context
*intel
= intel_context(ctx
);
473 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
475 /* From the GL_ARB_occlusion_query spec:
477 * "Instead of allowing for an infinite loop, performing a
478 * QUERY_RESULT_AVAILABLE_ARB will perform a flush if the result is
479 * not ready yet on the first time it is queried. This ensures that
480 * the async query will return true in finite time.
482 if (query
->bo
&& drm_intel_bo_references(intel
->batch
.bo
, query
->bo
))
483 intel_batchbuffer_flush(intel
);
485 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
486 brw_queryobj_get_results(ctx
, query
);
487 query
->Base
.Ready
= true;
492 * Ensure there query's BO has enough space to store a new pair of values.
494 * If not, gather the existing BO's results and create a new buffer of the
498 ensure_bo_has_space(struct gl_context
*ctx
, struct brw_query_object
*query
)
500 struct intel_context
*intel
= intel_context(ctx
);
502 if (!query
->bo
|| query
->last_index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
504 if (query
->bo
!= NULL
) {
505 /* The old query BO did not have enough space, so we allocated a new
506 * one. Gather the results so far (adding up the differences) and
507 * release the old BO.
509 brw_queryobj_get_results(ctx
, query
);
512 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "query", 4096, 1);
513 query
->last_index
= 0;
518 * Record the PS_DEPTH_COUNT value (for occlusion queries) just before
521 * In a pre-hardware context world, the single PS_DEPTH_COUNT register is
522 * shared among all applications using the GPU. However, our query value
523 * needs to only include fragments generated by our application/GL context.
525 * To accommodate this, we record PS_DEPTH_COUNT at the start and end of
526 * each batchbuffer (technically, the first primitive drawn and flush time).
527 * Subtracting each pair of values calculates the change in PS_DEPTH_COUNT
528 * caused by a batchbuffer. Since there is no preemption inside batches,
529 * this is guaranteed to only measure the effects of our current application.
531 * Adding each of these differences (in case drawing is done over many batches)
532 * produces the final expected value.
534 * In a world with hardware contexts, PS_DEPTH_COUNT is saved and restored
535 * as part of the context state, so this is unnecessary. We could simply
536 * read two values and subtract them. However, it's safe to continue using
540 brw_emit_query_begin(struct brw_context
*brw
)
542 struct intel_context
*intel
= &brw
->intel
;
543 struct gl_context
*ctx
= &intel
->ctx
;
544 struct brw_query_object
*query
= brw
->query
.obj
;
546 /* Skip if we're not doing any queries, or we've already recorded the
547 * initial query value for this batchbuffer.
549 if (!query
|| brw
->query
.begin_emitted
)
552 ensure_bo_has_space(ctx
, query
);
554 write_depth_count(intel
, query
->bo
, query
->last_index
* 2);
556 brw
->query
.begin_emitted
= true;
560 * Called at batchbuffer flush to get an ending PS_DEPTH_COUNT.
562 * See the explanation in brw_emit_query_begin().
565 brw_emit_query_end(struct brw_context
*brw
)
567 struct intel_context
*intel
= &brw
->intel
;
568 struct brw_query_object
*query
= brw
->query
.obj
;
570 if (!brw
->query
.begin_emitted
)
573 write_depth_count(intel
, query
->bo
, query
->last_index
* 2 + 1);
575 brw
->query
.begin_emitted
= false;
580 * Driver hook for glQueryCounter().
582 * This handles GL_TIMESTAMP queries, which perform a pipelined read of the
583 * current GPU time. This is unlike GL_TIME_ELAPSED, which measures the
584 * time while the query is active.
587 brw_query_counter(struct gl_context
*ctx
, struct gl_query_object
*q
)
589 struct intel_context
*intel
= intel_context(ctx
);
590 struct brw_query_object
*query
= (struct brw_query_object
*) q
;
592 assert(q
->Target
== GL_TIMESTAMP
);
594 drm_intel_bo_unreference(query
->bo
);
595 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "timestamp query", 4096, 4096);
596 write_timestamp(intel
, query
->bo
, 0);
600 * Read the TIMESTAMP register immediately (in a non-pipelined fashion).
602 * This is used to implement the GetTimestamp() driver hook.
605 brw_get_timestamp(struct gl_context
*ctx
)
607 struct intel_context
*intel
= intel_context(ctx
);
610 drm_intel_reg_read(intel
->bufmgr
, TIMESTAMP
, &result
);
612 /* See logic in brw_queryobj_get_results() */
613 result
= result
>> 32;
615 result
&= (1ull << 36) - 1;
620 void brw_init_queryobj_functions(struct dd_function_table
*functions
)
622 functions
->NewQueryObject
= brw_new_query_object
;
623 functions
->DeleteQuery
= brw_delete_query
;
624 functions
->BeginQuery
= brw_begin_query
;
625 functions
->EndQuery
= brw_end_query
;
626 functions
->QueryCounter
= brw_query_counter
;
627 functions
->CheckQuery
= brw_check_query
;
628 functions
->WaitQuery
= brw_wait_query
;
629 functions
->GetTimestamp
= brw_get_timestamp
;