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 brw_write_timestamp(struct brw_context
*brw
, drm_intel_bo
*query_bo
, int idx
)
53 /* Emit Sandybridge workaround flush: */
54 brw_emit_pipe_control_flush(brw
,
55 PIPE_CONTROL_CS_STALL
|
56 PIPE_CONTROL_STALL_AT_SCOREBOARD
);
59 brw_emit_pipe_control_write(brw
, PIPE_CONTROL_WRITE_TIMESTAMP
,
60 query_bo
, idx
* sizeof(uint64_t), 0, 0);
64 * Emit PIPE_CONTROLs to write the PS_DEPTH_COUNT register into a buffer.
67 brw_write_depth_count(struct brw_context
*brw
, drm_intel_bo
*query_bo
, int idx
)
69 brw_emit_pipe_control_write(brw
,
70 PIPE_CONTROL_WRITE_DEPTH_COUNT
|
71 PIPE_CONTROL_DEPTH_STALL
,
72 query_bo
, idx
* sizeof(uint64_t),
77 * Wait on the query object's BO and calculate the final result.
80 brw_queryobj_get_results(struct gl_context
*ctx
,
81 struct brw_query_object
*query
)
83 struct brw_context
*brw
= brw_context(ctx
);
90 if (query
->bo
== NULL
)
93 /* If the application has requested the query result, but this batch is
94 * still contributing to it, flush it now so the results will be present
97 if (drm_intel_bo_references(brw
->batch
.bo
, query
->bo
))
98 intel_batchbuffer_flush(brw
);
100 if (unlikely(brw
->perf_debug
)) {
101 if (drm_intel_bo_busy(query
->bo
)) {
102 perf_debug("Stalling on the GPU waiting for a query object.\n");
106 drm_intel_bo_map(query
->bo
, false);
107 results
= query
->bo
->virtual;
108 switch (query
->Base
.Target
) {
109 case GL_TIME_ELAPSED_EXT
:
110 /* The query BO contains the starting and ending timestamps.
111 * Subtract the two and convert to nanoseconds.
113 query
->Base
.Result
+= 1000 * ((results
[1] >> 32) - (results
[0] >> 32));
117 /* The query BO contains a single timestamp value in results[0]. */
118 query
->Base
.Result
= 1000 * (results
[0] >> 32);
121 case GL_SAMPLES_PASSED_ARB
:
122 /* Loop over pairs of values from the BO, which are the PS_DEPTH_COUNT
123 * value at the start and end of the batchbuffer. Subtract them to
124 * get the number of fragments which passed the depth test in each
125 * individual batch, and add those differences up to get the number
126 * of fragments for the entire query.
128 * Note that query->Base.Result may already be non-zero. We may have
129 * run out of space in the query's BO and allocated a new one. If so,
130 * this function was already called to accumulate the results so far.
132 for (i
= 0; i
< query
->last_index
; i
++) {
133 query
->Base
.Result
+= results
[i
* 2 + 1] - results
[i
* 2];
137 case GL_ANY_SAMPLES_PASSED
:
138 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
139 /* If the starting and ending PS_DEPTH_COUNT from any of the batches
140 * differ, then some fragments passed the depth test.
142 for (i
= 0; i
< query
->last_index
; i
++) {
143 if (results
[i
* 2 + 1] != results
[i
* 2]) {
144 query
->Base
.Result
= GL_TRUE
;
151 unreachable("Unrecognized query target in brw_queryobj_get_results()");
153 drm_intel_bo_unmap(query
->bo
);
155 /* Now that we've processed the data stored in the query's buffer object,
158 drm_intel_bo_unreference(query
->bo
);
163 * The NewQueryObject() driver hook.
165 * Allocates and initializes a new query object.
167 static struct gl_query_object
*
168 brw_new_query_object(struct gl_context
*ctx
, GLuint id
)
170 struct brw_query_object
*query
;
172 query
= calloc(1, sizeof(struct brw_query_object
));
175 query
->Base
.Result
= 0;
176 query
->Base
.Active
= false;
177 query
->Base
.Ready
= true;
183 * The DeleteQuery() driver hook.
186 brw_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
188 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
190 drm_intel_bo_unreference(query
->bo
);
195 * Gen4-5 driver hook for glBeginQuery().
197 * Initializes driver structures and emits any GPU commands required to begin
198 * recording data for the query.
201 brw_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
203 struct brw_context
*brw
= brw_context(ctx
);
204 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
206 assert(brw
->gen
< 6);
208 switch (query
->Base
.Target
) {
209 case GL_TIME_ELAPSED_EXT
:
210 /* For timestamp queries, we record the starting time right away so that
211 * we measure the full time between BeginQuery and EndQuery. There's
212 * some debate about whether this is the right thing to do. Our decision
213 * is based on the following text from the ARB_timer_query extension:
215 * "(5) Should the extension measure total time elapsed between the full
216 * completion of the BeginQuery and EndQuery commands, or just time
217 * spent in the graphics library?
219 * RESOLVED: This extension will measure the total time elapsed
220 * between the full completion of these commands. Future extensions
221 * may implement a query to determine time elapsed at different stages
222 * of the graphics pipeline."
224 * We write a starting timestamp now (at index 0). At EndQuery() time,
225 * we'll write a second timestamp (at index 1), and subtract the two to
226 * obtain the time elapsed. Notably, this includes time elapsed while
227 * the system was doing other work, such as running other applications.
229 drm_intel_bo_unreference(query
->bo
);
230 query
->bo
= drm_intel_bo_alloc(brw
->bufmgr
, "timer query", 4096, 4096);
231 brw_write_timestamp(brw
, query
->bo
, 0);
234 case GL_ANY_SAMPLES_PASSED
:
235 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
236 case GL_SAMPLES_PASSED_ARB
:
237 /* For occlusion queries, we delay taking an initial sample until the
238 * first drawing occurs in this batch. See the reasoning in the comments
239 * for brw_emit_query_begin() below.
241 * Since we're starting a new query, we need to be sure to throw away
242 * any previous occlusion query results.
244 drm_intel_bo_unreference(query
->bo
);
246 query
->last_index
= -1;
248 brw
->query
.obj
= query
;
250 /* Depth statistics on Gen4 require strange workarounds, so we try to
251 * avoid them when necessary. They're required for occlusion queries,
252 * so turn them on now.
255 brw
->ctx
.NewDriverState
|= BRW_NEW_STATS_WM
;
259 unreachable("Unrecognized query target in brw_begin_query()");
264 * Gen4-5 driver hook for glEndQuery().
266 * Emits GPU commands to record a final query value, ending any data capturing.
267 * However, the final result isn't necessarily available until the GPU processes
268 * those commands. brw_queryobj_get_results() processes the captured data to
269 * produce the final result.
272 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
274 struct brw_context
*brw
= brw_context(ctx
);
275 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
277 assert(brw
->gen
< 6);
279 switch (query
->Base
.Target
) {
280 case GL_TIME_ELAPSED_EXT
:
281 /* Write the final timestamp. */
282 brw_write_timestamp(brw
, query
->bo
, 1);
285 case GL_ANY_SAMPLES_PASSED
:
286 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
287 case GL_SAMPLES_PASSED_ARB
:
289 /* No query->bo means that EndQuery was called after BeginQuery with no
290 * intervening drawing. Rather than doing nothing at all here in this
291 * case, we emit the query_begin and query_end state to the
292 * hardware. This is to guarantee that waiting on the result of this
293 * empty state will cause all previous queries to complete at all, as
294 * required by the specification:
296 * It must always be true that if any query object
297 * returns a result available of TRUE, all queries of the
298 * same type issued prior to that query must also return
299 * TRUE. [Open GL 4.3 (Core Profile) Section 4.2.1]
302 brw_emit_query_begin(brw
);
307 brw_emit_query_end(brw
);
309 brw
->query
.obj
= NULL
;
312 brw
->ctx
.NewDriverState
|= BRW_NEW_STATS_WM
;
316 unreachable("Unrecognized query target in brw_end_query()");
321 * The Gen4-5 WaitQuery() driver hook.
323 * Wait for a query result to become available and return it. This is the
324 * backing for glGetQueryObjectiv() with the GL_QUERY_RESULT pname.
326 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
328 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
330 assert(brw_context(ctx
)->gen
< 6);
332 brw_queryobj_get_results(ctx
, query
);
333 query
->Base
.Ready
= true;
337 * The Gen4-5 CheckQuery() driver hook.
339 * Checks whether a query result is ready yet. If not, flushes.
340 * This is the backing for glGetQueryObjectiv()'s QUERY_RESULT_AVAILABLE pname.
342 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
344 struct brw_context
*brw
= brw_context(ctx
);
345 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
347 assert(brw
->gen
< 6);
349 /* From the GL_ARB_occlusion_query spec:
351 * "Instead of allowing for an infinite loop, performing a
352 * QUERY_RESULT_AVAILABLE_ARB will perform a flush if the result is
353 * not ready yet on the first time it is queried. This ensures that
354 * the async query will return true in finite time.
356 if (query
->bo
&& drm_intel_bo_references(brw
->batch
.bo
, query
->bo
))
357 intel_batchbuffer_flush(brw
);
359 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
360 brw_queryobj_get_results(ctx
, query
);
361 query
->Base
.Ready
= true;
366 * Ensure there query's BO has enough space to store a new pair of values.
368 * If not, gather the existing BO's results and create a new buffer of the
372 ensure_bo_has_space(struct gl_context
*ctx
, struct brw_query_object
*query
)
374 struct brw_context
*brw
= brw_context(ctx
);
376 assert(brw
->gen
< 6);
378 if (!query
->bo
|| query
->last_index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
380 if (query
->bo
!= NULL
) {
381 /* The old query BO did not have enough space, so we allocated a new
382 * one. Gather the results so far (adding up the differences) and
383 * release the old BO.
385 brw_queryobj_get_results(ctx
, query
);
388 query
->bo
= drm_intel_bo_alloc(brw
->bufmgr
, "query", 4096, 1);
389 query
->last_index
= 0;
394 * Record the PS_DEPTH_COUNT value (for occlusion queries) just before
397 * In a pre-hardware context world, the single PS_DEPTH_COUNT register is
398 * shared among all applications using the GPU. However, our query value
399 * needs to only include fragments generated by our application/GL context.
401 * To accommodate this, we record PS_DEPTH_COUNT at the start and end of
402 * each batchbuffer (technically, the first primitive drawn and flush time).
403 * Subtracting each pair of values calculates the change in PS_DEPTH_COUNT
404 * caused by a batchbuffer. Since there is no preemption inside batches,
405 * this is guaranteed to only measure the effects of our current application.
407 * Adding each of these differences (in case drawing is done over many batches)
408 * produces the final expected value.
410 * In a world with hardware contexts, PS_DEPTH_COUNT is saved and restored
411 * as part of the context state, so this is unnecessary, and skipped.
414 brw_emit_query_begin(struct brw_context
*brw
)
416 struct gl_context
*ctx
= &brw
->ctx
;
417 struct brw_query_object
*query
= brw
->query
.obj
;
422 /* Skip if we're not doing any queries, or we've already recorded the
423 * initial query value for this batchbuffer.
425 if (!query
|| brw
->query
.begin_emitted
)
428 ensure_bo_has_space(ctx
, query
);
430 brw_write_depth_count(brw
, query
->bo
, query
->last_index
* 2);
432 brw
->query
.begin_emitted
= true;
436 * Called at batchbuffer flush to get an ending PS_DEPTH_COUNT
437 * (for non-hardware context platforms).
439 * See the explanation in brw_emit_query_begin().
442 brw_emit_query_end(struct brw_context
*brw
)
444 struct brw_query_object
*query
= brw
->query
.obj
;
449 if (!brw
->query
.begin_emitted
)
452 brw_write_depth_count(brw
, query
->bo
, query
->last_index
* 2 + 1);
454 brw
->query
.begin_emitted
= false;
459 * Driver hook for glQueryCounter().
461 * This handles GL_TIMESTAMP queries, which perform a pipelined read of the
462 * current GPU time. This is unlike GL_TIME_ELAPSED, which measures the
463 * time while the query is active.
466 brw_query_counter(struct gl_context
*ctx
, struct gl_query_object
*q
)
468 struct brw_context
*brw
= brw_context(ctx
);
469 struct brw_query_object
*query
= (struct brw_query_object
*) q
;
471 assert(q
->Target
== GL_TIMESTAMP
);
473 drm_intel_bo_unreference(query
->bo
);
474 query
->bo
= drm_intel_bo_alloc(brw
->bufmgr
, "timestamp query", 4096, 4096);
475 brw_write_timestamp(brw
, query
->bo
, 0);
477 query
->flushed
= false;
481 * Read the TIMESTAMP register immediately (in a non-pipelined fashion).
483 * This is used to implement the GetTimestamp() driver hook.
486 brw_get_timestamp(struct gl_context
*ctx
)
488 struct brw_context
*brw
= brw_context(ctx
);
491 switch (brw
->intelScreen
->hw_has_timestamp
) {
492 case 3: /* New kernel, always full 36bit accuracy */
493 drm_intel_reg_read(brw
->bufmgr
, TIMESTAMP
| 1, &result
);
495 case 2: /* 64bit kernel, result is left-shifted by 32bits, losing 4bits */
496 drm_intel_reg_read(brw
->bufmgr
, TIMESTAMP
, &result
);
497 result
= result
>> 32;
499 case 1: /* 32bit kernel, result is 36bit wide but may be inaccurate! */
500 drm_intel_reg_read(brw
->bufmgr
, TIMESTAMP
, &result
);
504 /* See logic in brw_queryobj_get_results() */
506 result
&= (1ull << 36) - 1;
510 /* Initialize query object functions used on all generations. */
511 void brw_init_common_queryobj_functions(struct dd_function_table
*functions
)
513 functions
->NewQueryObject
= brw_new_query_object
;
514 functions
->DeleteQuery
= brw_delete_query
;
515 functions
->QueryCounter
= brw_query_counter
;
516 functions
->GetTimestamp
= brw_get_timestamp
;
519 /* Initialize Gen4/5-specific query object functions. */
520 void gen4_init_queryobj_functions(struct dd_function_table
*functions
)
522 functions
->BeginQuery
= brw_begin_query
;
523 functions
->EndQuery
= brw_end_query
;
524 functions
->CheckQuery
= brw_check_query
;
525 functions
->WaitQuery
= brw_wait_query
;