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 /* Emit Sandybridge workaround flush: */
71 intel_emit_post_sync_nonzero_flush(brw
);
73 brw_emit_pipe_control_write(brw
,
74 PIPE_CONTROL_WRITE_DEPTH_COUNT
75 | PIPE_CONTROL_DEPTH_STALL
,
76 query_bo
, idx
* sizeof(uint64_t), 0, 0);
80 * Wait on the query object's BO and calculate the final result.
83 brw_queryobj_get_results(struct gl_context
*ctx
,
84 struct brw_query_object
*query
)
86 struct brw_context
*brw
= brw_context(ctx
);
93 if (query
->bo
== NULL
)
96 /* If the application has requested the query result, but this batch is
97 * still contributing to it, flush it now so the results will be present
100 if (drm_intel_bo_references(brw
->batch
.bo
, query
->bo
))
101 intel_batchbuffer_flush(brw
);
103 if (unlikely(brw
->perf_debug
)) {
104 if (drm_intel_bo_busy(query
->bo
)) {
105 perf_debug("Stalling on the GPU waiting for a query object.\n");
109 drm_intel_bo_map(query
->bo
, false);
110 results
= query
->bo
->virtual;
111 switch (query
->Base
.Target
) {
112 case GL_TIME_ELAPSED_EXT
:
113 /* The query BO contains the starting and ending timestamps.
114 * Subtract the two and convert to nanoseconds.
116 query
->Base
.Result
+= 1000 * ((results
[1] >> 32) - (results
[0] >> 32));
120 /* The query BO contains a single timestamp value in results[0]. */
121 query
->Base
.Result
= 1000 * (results
[0] >> 32);
124 case GL_SAMPLES_PASSED_ARB
:
125 /* Loop over pairs of values from the BO, which are the PS_DEPTH_COUNT
126 * value at the start and end of the batchbuffer. Subtract them to
127 * get the number of fragments which passed the depth test in each
128 * individual batch, and add those differences up to get the number
129 * of fragments for the entire query.
131 * Note that query->Base.Result may already be non-zero. We may have
132 * run out of space in the query's BO and allocated a new one. If so,
133 * this function was already called to accumulate the results so far.
135 for (i
= 0; i
< query
->last_index
; i
++) {
136 query
->Base
.Result
+= results
[i
* 2 + 1] - results
[i
* 2];
140 case GL_ANY_SAMPLES_PASSED
:
141 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
142 /* If the starting and ending PS_DEPTH_COUNT from any of the batches
143 * differ, then some fragments passed the depth test.
145 for (i
= 0; i
< query
->last_index
; i
++) {
146 if (results
[i
* 2 + 1] != results
[i
* 2]) {
147 query
->Base
.Result
= GL_TRUE
;
154 assert(!"Unrecognized query target in brw_queryobj_get_results()");
157 drm_intel_bo_unmap(query
->bo
);
159 /* Now that we've processed the data stored in the query's buffer object,
162 drm_intel_bo_unreference(query
->bo
);
167 * The NewQueryObject() driver hook.
169 * Allocates and initializes a new query object.
171 static struct gl_query_object
*
172 brw_new_query_object(struct gl_context
*ctx
, GLuint id
)
174 struct brw_query_object
*query
;
176 query
= calloc(1, sizeof(struct brw_query_object
));
179 query
->Base
.Result
= 0;
180 query
->Base
.Active
= false;
181 query
->Base
.Ready
= true;
187 * The DeleteQuery() driver hook.
190 brw_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
192 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
194 drm_intel_bo_unreference(query
->bo
);
199 * Gen4-5 driver hook for glBeginQuery().
201 * Initializes driver structures and emits any GPU commands required to begin
202 * recording data for the query.
205 brw_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
207 struct brw_context
*brw
= brw_context(ctx
);
208 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
210 assert(brw
->gen
< 6);
212 switch (query
->Base
.Target
) {
213 case GL_TIME_ELAPSED_EXT
:
214 /* For timestamp queries, we record the starting time right away so that
215 * we measure the full time between BeginQuery and EndQuery. There's
216 * some debate about whether this is the right thing to do. Our decision
217 * is based on the following text from the ARB_timer_query extension:
219 * "(5) Should the extension measure total time elapsed between the full
220 * completion of the BeginQuery and EndQuery commands, or just time
221 * spent in the graphics library?
223 * RESOLVED: This extension will measure the total time elapsed
224 * between the full completion of these commands. Future extensions
225 * may implement a query to determine time elapsed at different stages
226 * of the graphics pipeline."
228 * We write a starting timestamp now (at index 0). At EndQuery() time,
229 * we'll write a second timestamp (at index 1), and subtract the two to
230 * obtain the time elapsed. Notably, this includes time elapsed while
231 * the system was doing other work, such as running other applications.
233 drm_intel_bo_unreference(query
->bo
);
234 query
->bo
= drm_intel_bo_alloc(brw
->bufmgr
, "timer query", 4096, 4096);
235 brw_write_timestamp(brw
, query
->bo
, 0);
238 case GL_ANY_SAMPLES_PASSED
:
239 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
240 case GL_SAMPLES_PASSED_ARB
:
241 /* For occlusion queries, we delay taking an initial sample until the
242 * first drawing occurs in this batch. See the reasoning in the comments
243 * for brw_emit_query_begin() below.
245 * Since we're starting a new query, we need to be sure to throw away
246 * any previous occlusion query results.
248 drm_intel_bo_unreference(query
->bo
);
250 query
->last_index
= -1;
252 brw
->query
.obj
= query
;
254 /* Depth statistics on Gen4 require strange workarounds, so we try to
255 * avoid them when necessary. They're required for occlusion queries,
256 * so turn them on now.
259 brw
->state
.dirty
.brw
|= BRW_NEW_STATS_WM
;
263 assert(!"Unrecognized query target in brw_begin_query()");
269 * Gen4-5 driver hook for glEndQuery().
271 * Emits GPU commands to record a final query value, ending any data capturing.
272 * However, the final result isn't necessarily available until the GPU processes
273 * those commands. brw_queryobj_get_results() processes the captured data to
274 * produce the final result.
277 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
279 struct brw_context
*brw
= brw_context(ctx
);
280 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
282 assert(brw
->gen
< 6);
284 switch (query
->Base
.Target
) {
285 case GL_TIME_ELAPSED_EXT
:
286 /* Write the final timestamp. */
287 brw_write_timestamp(brw
, query
->bo
, 1);
290 case GL_ANY_SAMPLES_PASSED
:
291 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
292 case GL_SAMPLES_PASSED_ARB
:
294 /* No query->bo means that EndQuery was called after BeginQuery with no
295 * intervening drawing. Rather than doing nothing at all here in this
296 * case, we emit the query_begin and query_end state to the
297 * hardware. This is to guarantee that waiting on the result of this
298 * empty state will cause all previous queries to complete at all, as
299 * required by the specification:
301 * It must always be true that if any query object
302 * returns a result available of TRUE, all queries of the
303 * same type issued prior to that query must also return
304 * TRUE. [Open GL 4.3 (Core Profile) Section 4.2.1]
307 brw_emit_query_begin(brw
);
312 brw_emit_query_end(brw
);
314 brw
->query
.obj
= NULL
;
317 brw
->state
.dirty
.brw
|= BRW_NEW_STATS_WM
;
321 assert(!"Unrecognized query target in brw_end_query()");
327 * The Gen4-5 WaitQuery() driver hook.
329 * Wait for a query result to become available and return it. This is the
330 * backing for glGetQueryObjectiv() with the GL_QUERY_RESULT pname.
332 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
334 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
336 assert(brw_context(ctx
)->gen
< 6);
338 brw_queryobj_get_results(ctx
, query
);
339 query
->Base
.Ready
= true;
343 * The Gen4-5 CheckQuery() driver hook.
345 * Checks whether a query result is ready yet. If not, flushes.
346 * This is the backing for glGetQueryObjectiv()'s QUERY_RESULT_AVAILABLE pname.
348 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
350 struct brw_context
*brw
= brw_context(ctx
);
351 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
353 assert(brw
->gen
< 6);
355 /* From the GL_ARB_occlusion_query spec:
357 * "Instead of allowing for an infinite loop, performing a
358 * QUERY_RESULT_AVAILABLE_ARB will perform a flush if the result is
359 * not ready yet on the first time it is queried. This ensures that
360 * the async query will return true in finite time.
362 if (query
->bo
&& drm_intel_bo_references(brw
->batch
.bo
, query
->bo
))
363 intel_batchbuffer_flush(brw
);
365 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
366 brw_queryobj_get_results(ctx
, query
);
367 query
->Base
.Ready
= true;
372 * Ensure there query's BO has enough space to store a new pair of values.
374 * If not, gather the existing BO's results and create a new buffer of the
378 ensure_bo_has_space(struct gl_context
*ctx
, struct brw_query_object
*query
)
380 struct brw_context
*brw
= brw_context(ctx
);
382 assert(brw
->gen
< 6);
384 if (!query
->bo
|| query
->last_index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
386 if (query
->bo
!= NULL
) {
387 /* The old query BO did not have enough space, so we allocated a new
388 * one. Gather the results so far (adding up the differences) and
389 * release the old BO.
391 brw_queryobj_get_results(ctx
, query
);
394 query
->bo
= drm_intel_bo_alloc(brw
->bufmgr
, "query", 4096, 1);
395 query
->last_index
= 0;
400 * Record the PS_DEPTH_COUNT value (for occlusion queries) just before
403 * In a pre-hardware context world, the single PS_DEPTH_COUNT register is
404 * shared among all applications using the GPU. However, our query value
405 * needs to only include fragments generated by our application/GL context.
407 * To accommodate this, we record PS_DEPTH_COUNT at the start and end of
408 * each batchbuffer (technically, the first primitive drawn and flush time).
409 * Subtracting each pair of values calculates the change in PS_DEPTH_COUNT
410 * caused by a batchbuffer. Since there is no preemption inside batches,
411 * this is guaranteed to only measure the effects of our current application.
413 * Adding each of these differences (in case drawing is done over many batches)
414 * produces the final expected value.
416 * In a world with hardware contexts, PS_DEPTH_COUNT is saved and restored
417 * as part of the context state, so this is unnecessary, and skipped.
420 brw_emit_query_begin(struct brw_context
*brw
)
422 struct gl_context
*ctx
= &brw
->ctx
;
423 struct brw_query_object
*query
= brw
->query
.obj
;
428 /* Skip if we're not doing any queries, or we've already recorded the
429 * initial query value for this batchbuffer.
431 if (!query
|| brw
->query
.begin_emitted
)
434 ensure_bo_has_space(ctx
, query
);
436 brw_write_depth_count(brw
, query
->bo
, query
->last_index
* 2);
438 brw
->query
.begin_emitted
= true;
442 * Called at batchbuffer flush to get an ending PS_DEPTH_COUNT
443 * (for non-hardware context platforms).
445 * See the explanation in brw_emit_query_begin().
448 brw_emit_query_end(struct brw_context
*brw
)
450 struct brw_query_object
*query
= brw
->query
.obj
;
455 if (!brw
->query
.begin_emitted
)
458 brw_write_depth_count(brw
, query
->bo
, query
->last_index
* 2 + 1);
460 brw
->query
.begin_emitted
= false;
465 * Driver hook for glQueryCounter().
467 * This handles GL_TIMESTAMP queries, which perform a pipelined read of the
468 * current GPU time. This is unlike GL_TIME_ELAPSED, which measures the
469 * time while the query is active.
472 brw_query_counter(struct gl_context
*ctx
, struct gl_query_object
*q
)
474 struct brw_context
*brw
= brw_context(ctx
);
475 struct brw_query_object
*query
= (struct brw_query_object
*) q
;
477 assert(q
->Target
== GL_TIMESTAMP
);
479 drm_intel_bo_unreference(query
->bo
);
480 query
->bo
= drm_intel_bo_alloc(brw
->bufmgr
, "timestamp query", 4096, 4096);
481 brw_write_timestamp(brw
, query
->bo
, 0);
485 * Read the TIMESTAMP register immediately (in a non-pipelined fashion).
487 * This is used to implement the GetTimestamp() driver hook.
490 brw_get_timestamp(struct gl_context
*ctx
)
492 struct brw_context
*brw
= brw_context(ctx
);
495 drm_intel_reg_read(brw
->bufmgr
, TIMESTAMP
, &result
);
497 /* See logic in brw_queryobj_get_results() */
498 result
= result
>> 32;
500 result
&= (1ull << 36) - 1;
505 /* Initialize query object functions used on all generations. */
506 void brw_init_common_queryobj_functions(struct dd_function_table
*functions
)
508 functions
->NewQueryObject
= brw_new_query_object
;
509 functions
->DeleteQuery
= brw_delete_query
;
510 functions
->QueryCounter
= brw_query_counter
;
511 functions
->GetTimestamp
= brw_get_timestamp
;
514 /* Initialize Gen4/5-specific query object functions. */
515 void gen4_init_queryobj_functions(struct dd_function_table
*functions
)
517 functions
->BeginQuery
= brw_begin_query
;
518 functions
->EndQuery
= brw_end_query
;
519 functions
->CheckQuery
= brw_check_query
;
520 functions
->WaitQuery
= brw_wait_query
;