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 support for ARB_query_object
30 * ARB_query_object is implemented by using the PIPE_CONTROL command to stall
31 * execution on the completion of previous depth tests, and write the
32 * current PS_DEPTH_COUNT to a buffer object.
34 * We use before and after counts when drawing during a query so that
35 * we don't pick up other clients' query data in ours. To reduce overhead,
36 * a single BO is used to record the query data for all active queries at
37 * once. This also gives us a simple bound on how much batchbuffer space is
38 * required for handling queries, so that we can be sure that we won't
39 * have to emit a batchbuffer without getting the ending PS_DEPTH_COUNT.
41 #include "main/imports.h"
43 #include "brw_context.h"
44 #include "brw_state.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_reg.h"
49 write_timestamp(struct intel_context
*intel
, drm_intel_bo
*query_bo
, int idx
)
51 if (intel
->gen
>= 6) {
52 /* Emit workaround flushes: */
53 if (intel
->gen
== 6) {
54 /* The timestamp write below is a non-zero post-sync op, which on
55 * Gen6 necessitates a CS stall. CS stalls need stall at scoreboard
56 * set. See the comments for intel_emit_post_sync_nonzero_flush().
59 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
60 OUT_BATCH(PIPE_CONTROL_CS_STALL
| PIPE_CONTROL_STALL_AT_SCOREBOARD
);
67 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (5 - 2));
68 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP
);
70 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
71 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
72 idx
* sizeof(uint64_t));
78 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2) |
79 PIPE_CONTROL_WRITE_TIMESTAMP
);
81 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
82 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
83 idx
* sizeof(uint64_t));
91 write_depth_count(struct intel_context
*intel
, drm_intel_bo
*query_bo
, int idx
)
93 if (intel
->gen
>= 6) {
94 /* Emit Sandybridge workaround flush: */
96 intel_emit_post_sync_nonzero_flush(intel
);
99 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (5 - 2));
100 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
101 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
103 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
104 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
105 (idx
* sizeof(uint64_t)));
111 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2) |
112 PIPE_CONTROL_DEPTH_STALL
|
113 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
114 /* This object could be mapped cacheable, but we don't have an exposed
115 * mechanism to support that. Since it's going uncached, tell GEM that
116 * we're writing to it. The usual clflush should be all that's required
117 * to pick up the results.
120 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
121 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
122 (idx
* sizeof(uint64_t)));
129 /** Waits on the query object's BO and totals the results for this query */
131 brw_queryobj_get_results(struct gl_context
*ctx
,
132 struct brw_query_object
*query
)
134 struct intel_context
*intel
= intel_context(ctx
);
139 if (query
->bo
== NULL
)
142 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
)) {
143 if (drm_intel_bo_busy(query
->bo
)) {
144 perf_debug("Stalling on the GPU waiting for a query object.\n");
148 drm_intel_bo_map(query
->bo
, false);
149 results
= query
->bo
->virtual;
150 switch (query
->Base
.Target
) {
151 case GL_TIME_ELAPSED_EXT
:
153 query
->Base
.Result
+= 80 * (results
[1] - results
[0]);
155 query
->Base
.Result
+= 1000 * ((results
[1] >> 32) - (results
[0] >> 32));
158 case GL_SAMPLES_PASSED_ARB
:
159 /* Map and count the pixels from the current query BO */
160 for (i
= query
->first_index
; i
<= query
->last_index
; i
++) {
161 query
->Base
.Result
+= results
[i
* 2 + 1] - results
[i
* 2];
165 case GL_PRIMITIVES_GENERATED
:
166 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
167 /* We don't actually query the hardware for this value, so query->bo
168 * should always be NULL and execution should never reach here.
170 assert(!"Unreachable");
174 assert(!"Unrecognized query target in brw_queryobj_get_results()");
177 drm_intel_bo_unmap(query
->bo
);
179 drm_intel_bo_unreference(query
->bo
);
183 static struct gl_query_object
*
184 brw_new_query_object(struct gl_context
*ctx
, GLuint id
)
186 struct brw_query_object
*query
;
188 query
= calloc(1, sizeof(struct brw_query_object
));
191 query
->Base
.Result
= 0;
192 query
->Base
.Active
= false;
193 query
->Base
.Ready
= true;
199 brw_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
201 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
203 drm_intel_bo_unreference(query
->bo
);
208 brw_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
210 struct brw_context
*brw
= brw_context(ctx
);
211 struct intel_context
*intel
= intel_context(ctx
);
212 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
214 switch (query
->Base
.Target
) {
215 case GL_TIME_ELAPSED_EXT
:
216 drm_intel_bo_unreference(query
->bo
);
217 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "timer query", 4096, 4096);
218 write_timestamp(intel
, query
->bo
, 0);
221 case GL_SAMPLES_PASSED_ARB
:
222 /* Reset our driver's tracking of query state. */
223 drm_intel_bo_unreference(query
->bo
);
225 query
->first_index
= -1;
226 query
->last_index
= -1;
228 brw
->query
.obj
= query
;
232 case GL_PRIMITIVES_GENERATED
:
233 /* We don't actually query the hardware for this value; we keep track of
234 * it a software counter. So just reset the counter.
236 brw
->sol
.primitives_generated
= 0;
237 brw
->sol
.counting_primitives_generated
= true;
240 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
241 /* We don't actually query the hardware for this value; we keep track of
242 * it a software counter. So just reset the counter.
244 brw
->sol
.primitives_written
= 0;
245 brw
->sol
.counting_primitives_written
= true;
249 assert(!"Unrecognized query target in brw_begin_query()");
255 * Begin the ARB_occlusion_query query on a query object.
258 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
260 struct brw_context
*brw
= brw_context(ctx
);
261 struct intel_context
*intel
= intel_context(ctx
);
262 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
264 switch (query
->Base
.Target
) {
265 case GL_TIME_ELAPSED_EXT
:
266 write_timestamp(intel
, query
->bo
, 1);
267 intel_batchbuffer_flush(intel
);
270 case GL_SAMPLES_PASSED_ARB
:
271 /* Flush the batchbuffer in case it has writes to our query BO.
272 * Have later queries write to a new query BO so that further rendering
273 * doesn't delay the collection of our results.
276 brw_emit_query_end(brw
);
277 intel_batchbuffer_flush(intel
);
279 drm_intel_bo_unreference(brw
->query
.bo
);
280 brw
->query
.bo
= NULL
;
283 brw
->query
.obj
= NULL
;
288 case GL_PRIMITIVES_GENERATED
:
289 /* We don't actually query the hardware for this value; we keep track of
290 * it in a software counter. So just read the counter and store it in
293 query
->Base
.Result
= brw
->sol
.primitives_generated
;
294 brw
->sol
.counting_primitives_generated
= false;
296 /* And set brw->query.obj to NULL so that this query won't try to wait
297 * for any rendering to complete.
302 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
303 /* We don't actually query the hardware for this value; we keep track of
304 * it in a software counter. So just read the counter and store it in
307 query
->Base
.Result
= brw
->sol
.primitives_written
;
308 brw
->sol
.counting_primitives_written
= false;
310 /* And set brw->query.obj to NULL so that this query won't try to wait
311 * for any rendering to complete.
317 assert(!"Unrecognized query target in brw_end_query()");
322 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
324 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
326 brw_queryobj_get_results(ctx
, query
);
327 query
->Base
.Ready
= true;
330 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
332 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
334 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
335 brw_queryobj_get_results(ctx
, query
);
336 query
->Base
.Ready
= true;
340 /** Called to set up the query BO and account for its aperture space */
342 brw_prepare_query_begin(struct brw_context
*brw
)
344 struct intel_context
*intel
= &brw
->intel
;
346 /* Skip if we're not doing any queries. */
350 /* Get a new query BO if we're going to need it. */
351 if (brw
->query
.bo
== NULL
||
352 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
353 drm_intel_bo_unreference(brw
->query
.bo
);
354 brw
->query
.bo
= NULL
;
356 brw
->query
.bo
= drm_intel_bo_alloc(intel
->bufmgr
, "query", 4096, 1);
358 /* clear target buffer */
359 drm_intel_bo_map(brw
->query
.bo
, true);
360 memset((char *)brw
->query
.bo
->virtual, 0, 4096);
361 drm_intel_bo_unmap(brw
->query
.bo
);
363 brw
->query
.index
= 0;
367 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
369 brw_emit_query_begin(struct brw_context
*brw
)
371 struct intel_context
*intel
= &brw
->intel
;
372 struct gl_context
*ctx
= &intel
->ctx
;
373 struct brw_query_object
*query
= brw
->query
.obj
;
375 /* Skip if we're not doing any queries, or we've emitted the start. */
376 if (!query
|| brw
->query
.active
)
379 write_depth_count(intel
, brw
->query
.bo
, brw
->query
.index
* 2);
381 if (query
->bo
!= brw
->query
.bo
) {
382 if (query
->bo
!= NULL
)
383 brw_queryobj_get_results(ctx
, query
);
384 drm_intel_bo_reference(brw
->query
.bo
);
385 query
->bo
= brw
->query
.bo
;
386 query
->first_index
= brw
->query
.index
;
388 query
->last_index
= brw
->query
.index
;
389 brw
->query
.active
= true;
392 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
394 brw_emit_query_end(struct brw_context
*brw
)
396 struct intel_context
*intel
= &brw
->intel
;
398 if (!brw
->query
.active
)
401 write_depth_count(intel
, brw
->query
.bo
, brw
->query
.index
* 2 + 1);
403 brw
->query
.active
= false;
407 void brw_init_queryobj_functions(struct dd_function_table
*functions
)
409 functions
->NewQueryObject
= brw_new_query_object
;
410 functions
->DeleteQuery
= brw_delete_query
;
411 functions
->BeginQuery
= brw_begin_query
;
412 functions
->EndQuery
= brw_end_query
;
413 functions
->CheckQuery
= brw_check_query
;
414 functions
->WaitQuery
= brw_wait_query
;