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"
48 /** Waits on the query object's BO and totals the results for this query */
50 brw_queryobj_get_results(struct gl_context
*ctx
,
51 struct brw_query_object
*query
)
53 struct intel_context
*intel
= intel_context(ctx
);
58 if (query
->bo
== NULL
)
61 drm_intel_bo_map(query
->bo
, false);
62 results
= query
->bo
->virtual;
63 switch (query
->Base
.Target
) {
64 case GL_TIME_ELAPSED_EXT
:
66 query
->Base
.Result
+= 80 * (results
[1] - results
[0]);
68 query
->Base
.Result
+= 1000 * ((results
[1] >> 32) - (results
[0] >> 32));
71 case GL_SAMPLES_PASSED_ARB
:
72 /* Map and count the pixels from the current query BO */
73 for (i
= query
->first_index
; i
<= query
->last_index
; i
++) {
74 query
->Base
.Result
+= results
[i
* 2 + 1] - results
[i
* 2];
78 case GL_PRIMITIVES_GENERATED
:
79 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
80 /* We don't actually query the hardware for this value, so query->bo
81 * should always be NULL and execution should never reach here.
83 assert(!"Unreachable");
87 assert(!"Unrecognized query target in brw_queryobj_get_results()");
90 drm_intel_bo_unmap(query
->bo
);
92 drm_intel_bo_unreference(query
->bo
);
96 static struct gl_query_object
*
97 brw_new_query_object(struct gl_context
*ctx
, GLuint id
)
99 struct brw_query_object
*query
;
101 query
= calloc(1, sizeof(struct brw_query_object
));
104 query
->Base
.Result
= 0;
105 query
->Base
.Active
= false;
106 query
->Base
.Ready
= true;
112 brw_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
114 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
116 drm_intel_bo_unreference(query
->bo
);
121 brw_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
123 struct brw_context
*brw
= brw_context(ctx
);
124 struct intel_context
*intel
= intel_context(ctx
);
125 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
127 switch (query
->Base
.Target
) {
128 case GL_TIME_ELAPSED_EXT
:
129 drm_intel_bo_unreference(query
->bo
);
130 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "timer query",
133 if (intel
->gen
>= 6) {
135 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
136 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP
);
138 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
139 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
146 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
147 PIPE_CONTROL_WRITE_TIMESTAMP
);
149 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
150 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
158 case GL_SAMPLES_PASSED_ARB
:
159 /* Reset our driver's tracking of query state. */
160 drm_intel_bo_unreference(query
->bo
);
162 query
->first_index
= -1;
163 query
->last_index
= -1;
165 brw
->query
.obj
= query
;
169 case GL_PRIMITIVES_GENERATED
:
170 /* We don't actually query the hardware for this value; we keep track of
171 * it a software counter. So just reset the counter.
173 brw
->sol
.primitives_generated
= 0;
174 brw
->sol
.counting_primitives_generated
= true;
177 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
178 /* We don't actually query the hardware for this value; we keep track of
179 * it a software counter. So just reset the counter.
181 brw
->sol
.primitives_written
= 0;
182 brw
->sol
.counting_primitives_written
= true;
186 assert(!"Unrecognized query target in brw_begin_query()");
192 * Begin the ARB_occlusion_query query on a query object.
195 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
197 struct brw_context
*brw
= brw_context(ctx
);
198 struct intel_context
*intel
= intel_context(ctx
);
199 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
201 switch (query
->Base
.Target
) {
202 case GL_TIME_ELAPSED_EXT
:
203 if (intel
->gen
>= 6) {
205 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
206 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP
);
208 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
209 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
216 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
217 PIPE_CONTROL_WRITE_TIMESTAMP
);
219 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
220 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
227 intel_batchbuffer_flush(intel
);
230 case GL_SAMPLES_PASSED_ARB
:
231 /* Flush the batchbuffer in case it has writes to our query BO.
232 * Have later queries write to a new query BO so that further rendering
233 * doesn't delay the collection of our results.
236 brw_emit_query_end(brw
);
237 intel_batchbuffer_flush(intel
);
239 drm_intel_bo_unreference(brw
->query
.bo
);
240 brw
->query
.bo
= NULL
;
243 brw
->query
.obj
= NULL
;
248 case GL_PRIMITIVES_GENERATED
:
249 /* We don't actually query the hardware for this value; we keep track of
250 * it in a software counter. So just read the counter and store it in
253 query
->Base
.Result
= brw
->sol
.primitives_generated
;
254 brw
->sol
.counting_primitives_generated
= false;
256 /* And set brw->query.obj to NULL so that this query won't try to wait
257 * for any rendering to complete.
262 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
263 /* We don't actually query the hardware for this value; we keep track of
264 * it in a software counter. So just read the counter and store it in
267 query
->Base
.Result
= brw
->sol
.primitives_written
;
268 brw
->sol
.counting_primitives_written
= false;
270 /* And set brw->query.obj to NULL so that this query won't try to wait
271 * for any rendering to complete.
277 assert(!"Unrecognized query target in brw_end_query()");
282 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
284 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
286 brw_queryobj_get_results(ctx
, query
);
287 query
->Base
.Ready
= true;
290 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
292 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
294 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
295 brw_queryobj_get_results(ctx
, query
);
296 query
->Base
.Ready
= true;
300 /** Called to set up the query BO and account for its aperture space */
302 brw_prepare_query_begin(struct brw_context
*brw
)
304 struct intel_context
*intel
= &brw
->intel
;
306 /* Skip if we're not doing any queries. */
310 /* Get a new query BO if we're going to need it. */
311 if (brw
->query
.bo
== NULL
||
312 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
313 drm_intel_bo_unreference(brw
->query
.bo
);
314 brw
->query
.bo
= NULL
;
316 brw
->query
.bo
= drm_intel_bo_alloc(intel
->bufmgr
, "query", 4096, 1);
318 /* clear target buffer */
319 drm_intel_bo_map(brw
->query
.bo
, true);
320 memset((char *)brw
->query
.bo
->virtual, 0, 4096);
321 drm_intel_bo_unmap(brw
->query
.bo
);
323 brw
->query
.index
= 0;
327 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
329 brw_emit_query_begin(struct brw_context
*brw
)
331 struct intel_context
*intel
= &brw
->intel
;
332 struct gl_context
*ctx
= &intel
->ctx
;
333 struct brw_query_object
*query
= brw
->query
.obj
;
335 /* Skip if we're not doing any queries, or we've emitted the start. */
336 if (!query
|| brw
->query
.active
)
339 if (intel
->gen
>= 6) {
342 /* workaround: CS stall required before depth stall. */
343 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
344 OUT_BATCH(PIPE_CONTROL_CS_STALL
);
345 OUT_BATCH(0); /* write address */
346 OUT_BATCH(0); /* write data */
348 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
349 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
350 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
351 OUT_RELOC(brw
->query
.bo
,
352 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
353 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
354 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
360 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
361 PIPE_CONTROL_DEPTH_STALL
|
362 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
363 /* This object could be mapped cacheable, but we don't have an exposed
364 * mechanism to support that. Since it's going uncached, tell GEM that
365 * we're writing to it. The usual clflush should be all that's required
366 * to pick up the results.
368 OUT_RELOC(brw
->query
.bo
,
369 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
370 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
371 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
377 if (query
->bo
!= brw
->query
.bo
) {
378 if (query
->bo
!= NULL
)
379 brw_queryobj_get_results(ctx
, query
);
380 drm_intel_bo_reference(brw
->query
.bo
);
381 query
->bo
= brw
->query
.bo
;
382 query
->first_index
= brw
->query
.index
;
384 query
->last_index
= brw
->query
.index
;
385 brw
->query
.active
= true;
388 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
390 brw_emit_query_end(struct brw_context
*brw
)
392 struct intel_context
*intel
= &brw
->intel
;
394 if (!brw
->query
.active
)
397 if (intel
->gen
>= 6) {
399 /* workaround: CS stall required before depth stall. */
400 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
401 OUT_BATCH(PIPE_CONTROL_CS_STALL
);
402 OUT_BATCH(0); /* write address */
403 OUT_BATCH(0); /* write data */
405 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
406 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
407 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
408 OUT_RELOC(brw
->query
.bo
,
409 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
410 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
411 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
417 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
418 PIPE_CONTROL_DEPTH_STALL
|
419 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
420 OUT_RELOC(brw
->query
.bo
,
421 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
422 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
423 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
429 brw
->query
.active
= false;
433 void brw_init_queryobj_functions(struct dd_function_table
*functions
)
435 functions
->NewQueryObject
= brw_new_query_object
;
436 functions
->DeleteQuery
= brw_delete_query
;
437 functions
->BeginQuery
= brw_begin_query
;
438 functions
->EndQuery
= brw_end_query
;
439 functions
->CheckQuery
= brw_check_query
;
440 functions
->WaitQuery
= brw_wait_query
;