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_DEBUG & DEBUG_PERF)) {
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 brw->query.obj 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 brw->query.obj 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 * Record the PS_DEPTH_COUNT value (for occlusion queries) just before
495 * In a pre-hardware context world, the single PS_DEPTH_COUNT register is
496 * shared among all applications using the GPU. However, our query value
497 * needs to only include fragments generated by our application/GL context.
499 * To accommodate this, we record PS_DEPTH_COUNT at the start and end of
500 * each batchbuffer (technically, the first primitive drawn and flush time).
501 * Subtracting each pair of values calculates the change in PS_DEPTH_COUNT
502 * caused by a batchbuffer. Since there is no preemption inside batches,
503 * this is guaranteed to only measure the effects of our current application.
505 * Adding each of these differences (in case drawing is done over many batches)
506 * produces the final expected value.
508 * In a world with hardware contexts, PS_DEPTH_COUNT is saved and restored
509 * as part of the context state, so this is unnecessary. We could simply
510 * read two values and subtract them. However, it's safe to continue using
514 brw_emit_query_begin(struct brw_context *brw)
516 struct intel_context *intel = &brw->intel;
517 struct gl_context *ctx = &intel->ctx;
518 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 the buffer has enough space to store a new pair of values.
527 * If not, create a new one of the same size; we'll gather the existing
528 * buffer's results momentarily.
530 if (!query->bo || query->last_index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
532 if (query->bo != NULL) {
533 /* The old query BO did not have enough space, so we allocated a new
534 * one. Gather the results so far (adding up the differences) and
535 * release the old BO.
537 brw_queryobj_get_results(ctx, query);
540 query->bo = drm_intel_bo_alloc(intel->bufmgr, "query", 4096, 1);
542 /* Fill the buffer with zeroes. This is probably superfluous. */
543 drm_intel_bo_map(query->bo, true);
544 memset((char *) query->bo->virtual, 0, 4096);
545 drm_intel_bo_unmap(query->bo);
547 query->last_index = 0;
550 write_depth_count(intel, query->bo, query->last_index * 2);
552 brw->query.begin_emitted = true;
556 * Called at batchbuffer flush to get an ending PS_DEPTH_COUNT.
558 * See the explanation in brw_emit_query_begin().
561 brw_emit_query_end(struct brw_context *brw)
563 struct intel_context *intel = &brw->intel;
564 struct brw_query_object *query = brw->query.obj;
566 if (!brw->query.begin_emitted)
569 write_depth_count(intel, query->bo, query->last_index * 2 + 1);
571 brw->query.begin_emitted = false;
576 * Driver hook for glQueryCounter().
578 * This handles GL_TIMESTAMP queries, which perform a pipelined read of the
579 * current GPU time. This is unlike GL_TIME_ELAPSED, which measures the
580 * time while the query is active.
583 brw_query_counter(struct gl_context *ctx, struct gl_query_object *q)
585 struct intel_context *intel = intel_context(ctx);
586 struct brw_query_object *query = (struct brw_query_object *) q;
588 assert(q->Target == GL_TIMESTAMP);
590 drm_intel_bo_unreference(query->bo);
591 query->bo = drm_intel_bo_alloc(intel->bufmgr, "timestamp query", 4096, 4096);
592 write_timestamp(intel, query->bo, 0);
596 * Read the TIMESTAMP register immediately (in a non-pipelined fashion).
598 * This is used to implement the GetTimestamp() driver hook.
601 brw_get_timestamp(struct gl_context *ctx)
603 struct intel_context *intel = intel_context(ctx);
606 drm_intel_reg_read(intel->bufmgr, TIMESTAMP, &result);
608 /* See logic in brw_queryobj_get_results() */
609 result = result >> 32;
611 result &= (1ull << 36) - 1;
616 void brw_init_queryobj_functions(struct dd_function_table *functions)
618 functions->NewQueryObject = brw_new_query_object;
619 functions->DeleteQuery = brw_delete_query;
620 functions->BeginQuery = brw_begin_query;
621 functions->EndQuery = brw_end_query;
622 functions->QueryCounter = brw_query_counter;
623 functions->CheckQuery = brw_check_query;
624 functions->WaitQuery = brw_wait_query;
625 functions->GetTimestamp = brw_get_timestamp;