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;
176 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
177 /* We don't actually query the hardware for this value; we keep track of
178 * it a software counter. So just reset the counter.
180 brw
->sol
.primitives_written
= 0;
184 assert(!"Unrecognized query target in brw_begin_query()");
190 * Begin the ARB_occlusion_query query on a query object.
193 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
195 struct brw_context
*brw
= brw_context(ctx
);
196 struct intel_context
*intel
= intel_context(ctx
);
197 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
199 switch (query
->Base
.Target
) {
200 case GL_TIME_ELAPSED_EXT
:
201 if (intel
->gen
>= 6) {
203 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
204 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP
);
206 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
207 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
214 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
215 PIPE_CONTROL_WRITE_TIMESTAMP
);
217 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
218 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
225 intel_batchbuffer_flush(intel
);
228 case GL_SAMPLES_PASSED_ARB
:
229 /* Flush the batchbuffer in case it has writes to our query BO.
230 * Have later queries write to a new query BO so that further rendering
231 * doesn't delay the collection of our results.
234 brw_emit_query_end(brw
);
235 intel_batchbuffer_flush(intel
);
237 drm_intel_bo_unreference(brw
->query
.bo
);
238 brw
->query
.bo
= NULL
;
241 brw
->query
.obj
= NULL
;
246 case GL_PRIMITIVES_GENERATED
:
247 /* We don't actually query the hardware for this value; we keep track of
248 * it in a software counter. So just read the counter and store it in
251 query
->Base
.Result
= brw
->sol
.primitives_generated
;
253 /* And set brw->query.obj to NULL so that this query won't try to wait
254 * for any rendering to complete.
259 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
260 /* We don't actually query the hardware for this value; we keep track of
261 * it in a software counter. So just read the counter and store it in
264 query
->Base
.Result
= brw
->sol
.primitives_written
;
266 /* And set brw->query.obj to NULL so that this query won't try to wait
267 * for any rendering to complete.
273 assert(!"Unrecognized query target in brw_end_query()");
278 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
280 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
282 brw_queryobj_get_results(ctx
, query
);
283 query
->Base
.Ready
= true;
286 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
288 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
290 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
291 brw_queryobj_get_results(ctx
, query
);
292 query
->Base
.Ready
= true;
296 /** Called to set up the query BO and account for its aperture space */
298 brw_prepare_query_begin(struct brw_context
*brw
)
300 struct intel_context
*intel
= &brw
->intel
;
302 /* Skip if we're not doing any queries. */
306 /* Get a new query BO if we're going to need it. */
307 if (brw
->query
.bo
== NULL
||
308 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
309 drm_intel_bo_unreference(brw
->query
.bo
);
310 brw
->query
.bo
= NULL
;
312 brw
->query
.bo
= drm_intel_bo_alloc(intel
->bufmgr
, "query", 4096, 1);
314 /* clear target buffer */
315 drm_intel_bo_map(brw
->query
.bo
, true);
316 memset((char *)brw
->query
.bo
->virtual, 0, 4096);
317 drm_intel_bo_unmap(brw
->query
.bo
);
319 brw
->query
.index
= 0;
323 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
325 brw_emit_query_begin(struct brw_context
*brw
)
327 struct intel_context
*intel
= &brw
->intel
;
328 struct gl_context
*ctx
= &intel
->ctx
;
329 struct brw_query_object
*query
= brw
->query
.obj
;
331 /* Skip if we're not doing any queries, or we've emitted the start. */
332 if (!query
|| brw
->query
.active
)
335 if (intel
->gen
>= 6) {
338 /* workaround: CS stall required before depth stall. */
339 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
340 OUT_BATCH(PIPE_CONTROL_CS_STALL
);
341 OUT_BATCH(0); /* write address */
342 OUT_BATCH(0); /* write data */
344 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
345 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
346 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
347 OUT_RELOC(brw
->query
.bo
,
348 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
349 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
350 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
356 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
357 PIPE_CONTROL_DEPTH_STALL
|
358 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
359 /* This object could be mapped cacheable, but we don't have an exposed
360 * mechanism to support that. Since it's going uncached, tell GEM that
361 * we're writing to it. The usual clflush should be all that's required
362 * to pick up the results.
364 OUT_RELOC(brw
->query
.bo
,
365 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
366 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
367 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
373 if (query
->bo
!= brw
->query
.bo
) {
374 if (query
->bo
!= NULL
)
375 brw_queryobj_get_results(ctx
, query
);
376 drm_intel_bo_reference(brw
->query
.bo
);
377 query
->bo
= brw
->query
.bo
;
378 query
->first_index
= brw
->query
.index
;
380 query
->last_index
= brw
->query
.index
;
381 brw
->query
.active
= true;
384 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
386 brw_emit_query_end(struct brw_context
*brw
)
388 struct intel_context
*intel
= &brw
->intel
;
390 if (!brw
->query
.active
)
393 if (intel
->gen
>= 6) {
395 /* workaround: CS stall required before depth stall. */
396 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
397 OUT_BATCH(PIPE_CONTROL_CS_STALL
);
398 OUT_BATCH(0); /* write address */
399 OUT_BATCH(0); /* write data */
401 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
402 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
403 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
404 OUT_RELOC(brw
->query
.bo
,
405 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
406 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
407 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
413 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
414 PIPE_CONTROL_DEPTH_STALL
|
415 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
416 OUT_RELOC(brw
->query
.bo
,
417 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
418 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
419 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
425 brw
->query
.active
= false;
429 void brw_init_queryobj_functions(struct dd_function_table
*functions
)
431 functions
->NewQueryObject
= brw_new_query_object
;
432 functions
->DeleteQuery
= brw_delete_query
;
433 functions
->BeginQuery
= brw_begin_query
;
434 functions
->EndQuery
= brw_end_query
;
435 functions
->CheckQuery
= brw_check_query
;
436 functions
->WaitQuery
= brw_wait_query
;