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) {
53 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (5 - 2));
54 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP
);
56 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
57 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
58 idx
* sizeof(uint64_t));
64 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2) |
65 PIPE_CONTROL_WRITE_TIMESTAMP
);
67 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
68 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
69 idx
* sizeof(uint64_t));
76 /** Waits on the query object's BO and totals the results for this query */
78 brw_queryobj_get_results(struct gl_context
*ctx
,
79 struct brw_query_object
*query
)
81 struct intel_context
*intel
= intel_context(ctx
);
86 if (query
->bo
== NULL
)
89 drm_intel_bo_map(query
->bo
, false);
90 results
= query
->bo
->virtual;
91 switch (query
->Base
.Target
) {
92 case GL_TIME_ELAPSED_EXT
:
94 query
->Base
.Result
+= 80 * (results
[1] - results
[0]);
96 query
->Base
.Result
+= 1000 * ((results
[1] >> 32) - (results
[0] >> 32));
99 case GL_SAMPLES_PASSED_ARB
:
100 /* Map and count the pixels from the current query BO */
101 for (i
= query
->first_index
; i
<= query
->last_index
; i
++) {
102 query
->Base
.Result
+= results
[i
* 2 + 1] - results
[i
* 2];
106 case GL_PRIMITIVES_GENERATED
:
107 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
108 /* We don't actually query the hardware for this value, so query->bo
109 * should always be NULL and execution should never reach here.
111 assert(!"Unreachable");
115 assert(!"Unrecognized query target in brw_queryobj_get_results()");
118 drm_intel_bo_unmap(query
->bo
);
120 drm_intel_bo_unreference(query
->bo
);
124 static struct gl_query_object
*
125 brw_new_query_object(struct gl_context
*ctx
, GLuint id
)
127 struct brw_query_object
*query
;
129 query
= calloc(1, sizeof(struct brw_query_object
));
132 query
->Base
.Result
= 0;
133 query
->Base
.Active
= false;
134 query
->Base
.Ready
= true;
140 brw_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
142 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
144 drm_intel_bo_unreference(query
->bo
);
149 brw_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
151 struct brw_context
*brw
= brw_context(ctx
);
152 struct intel_context
*intel
= intel_context(ctx
);
153 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
155 switch (query
->Base
.Target
) {
156 case GL_TIME_ELAPSED_EXT
:
157 drm_intel_bo_unreference(query
->bo
);
158 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "timer query", 4096, 4096);
159 write_timestamp(intel
, query
->bo
, 0);
162 case GL_SAMPLES_PASSED_ARB
:
163 /* Reset our driver's tracking of query state. */
164 drm_intel_bo_unreference(query
->bo
);
166 query
->first_index
= -1;
167 query
->last_index
= -1;
169 brw
->query
.obj
= query
;
173 case GL_PRIMITIVES_GENERATED
:
174 /* We don't actually query the hardware for this value; we keep track of
175 * it a software counter. So just reset the counter.
177 brw
->sol
.primitives_generated
= 0;
178 brw
->sol
.counting_primitives_generated
= true;
181 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
182 /* We don't actually query the hardware for this value; we keep track of
183 * it a software counter. So just reset the counter.
185 brw
->sol
.primitives_written
= 0;
186 brw
->sol
.counting_primitives_written
= true;
190 assert(!"Unrecognized query target in brw_begin_query()");
196 * Begin the ARB_occlusion_query query on a query object.
199 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
201 struct brw_context
*brw
= brw_context(ctx
);
202 struct intel_context
*intel
= intel_context(ctx
);
203 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
205 switch (query
->Base
.Target
) {
206 case GL_TIME_ELAPSED_EXT
:
207 write_timestamp(intel
, query
->bo
, 1);
208 intel_batchbuffer_flush(intel
);
211 case GL_SAMPLES_PASSED_ARB
:
212 /* Flush the batchbuffer in case it has writes to our query BO.
213 * Have later queries write to a new query BO so that further rendering
214 * doesn't delay the collection of our results.
217 brw_emit_query_end(brw
);
218 intel_batchbuffer_flush(intel
);
220 drm_intel_bo_unreference(brw
->query
.bo
);
221 brw
->query
.bo
= NULL
;
224 brw
->query
.obj
= NULL
;
229 case GL_PRIMITIVES_GENERATED
:
230 /* We don't actually query the hardware for this value; we keep track of
231 * it in a software counter. So just read the counter and store it in
234 query
->Base
.Result
= brw
->sol
.primitives_generated
;
235 brw
->sol
.counting_primitives_generated
= false;
237 /* And set brw->query.obj to NULL so that this query won't try to wait
238 * for any rendering to complete.
243 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
244 /* We don't actually query the hardware for this value; we keep track of
245 * it in a software counter. So just read the counter and store it in
248 query
->Base
.Result
= brw
->sol
.primitives_written
;
249 brw
->sol
.counting_primitives_written
= false;
251 /* And set brw->query.obj to NULL so that this query won't try to wait
252 * for any rendering to complete.
258 assert(!"Unrecognized query target in brw_end_query()");
263 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
265 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
267 brw_queryobj_get_results(ctx
, query
);
268 query
->Base
.Ready
= true;
271 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
273 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
275 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
276 brw_queryobj_get_results(ctx
, query
);
277 query
->Base
.Ready
= true;
281 /** Called to set up the query BO and account for its aperture space */
283 brw_prepare_query_begin(struct brw_context
*brw
)
285 struct intel_context
*intel
= &brw
->intel
;
287 /* Skip if we're not doing any queries. */
291 /* Get a new query BO if we're going to need it. */
292 if (brw
->query
.bo
== NULL
||
293 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
294 drm_intel_bo_unreference(brw
->query
.bo
);
295 brw
->query
.bo
= NULL
;
297 brw
->query
.bo
= drm_intel_bo_alloc(intel
->bufmgr
, "query", 4096, 1);
299 /* clear target buffer */
300 drm_intel_bo_map(brw
->query
.bo
, true);
301 memset((char *)brw
->query
.bo
->virtual, 0, 4096);
302 drm_intel_bo_unmap(brw
->query
.bo
);
304 brw
->query
.index
= 0;
308 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
310 brw_emit_query_begin(struct brw_context
*brw
)
312 struct intel_context
*intel
= &brw
->intel
;
313 struct gl_context
*ctx
= &intel
->ctx
;
314 struct brw_query_object
*query
= brw
->query
.obj
;
316 /* Skip if we're not doing any queries, or we've emitted the start. */
317 if (!query
|| brw
->query
.active
)
320 if (intel
->gen
>= 6) {
323 /* workaround: CS stall required before depth stall. */
324 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
325 OUT_BATCH(PIPE_CONTROL_CS_STALL
);
326 OUT_BATCH(0); /* write address */
327 OUT_BATCH(0); /* write data */
329 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
330 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
331 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
332 OUT_RELOC(brw
->query
.bo
,
333 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
334 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
335 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
341 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2) |
342 PIPE_CONTROL_DEPTH_STALL
|
343 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
344 /* This object could be mapped cacheable, but we don't have an exposed
345 * mechanism to support that. Since it's going uncached, tell GEM that
346 * we're writing to it. The usual clflush should be all that's required
347 * to pick up the results.
349 OUT_RELOC(brw
->query
.bo
,
350 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
351 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
352 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
358 if (query
->bo
!= brw
->query
.bo
) {
359 if (query
->bo
!= NULL
)
360 brw_queryobj_get_results(ctx
, query
);
361 drm_intel_bo_reference(brw
->query
.bo
);
362 query
->bo
= brw
->query
.bo
;
363 query
->first_index
= brw
->query
.index
;
365 query
->last_index
= brw
->query
.index
;
366 brw
->query
.active
= true;
369 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
371 brw_emit_query_end(struct brw_context
*brw
)
373 struct intel_context
*intel
= &brw
->intel
;
375 if (!brw
->query
.active
)
378 if (intel
->gen
>= 6) {
380 /* workaround: CS stall required before depth stall. */
381 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
382 OUT_BATCH(PIPE_CONTROL_CS_STALL
);
383 OUT_BATCH(0); /* write address */
384 OUT_BATCH(0); /* write data */
386 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
387 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
388 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
389 OUT_RELOC(brw
->query
.bo
,
390 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
391 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
392 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
398 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2) |
399 PIPE_CONTROL_DEPTH_STALL
|
400 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
401 OUT_RELOC(brw
->query
.bo
,
402 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
403 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
404 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
410 brw
->query
.active
= false;
414 void brw_init_queryobj_functions(struct dd_function_table
*functions
)
416 functions
->NewQueryObject
= brw_new_query_object
;
417 functions
->DeleteQuery
= brw_delete_query
;
418 functions
->BeginQuery
= brw_begin_query
;
419 functions
->EndQuery
= brw_end_query
;
420 functions
->CheckQuery
= brw_check_query
;
421 functions
->WaitQuery
= brw_wait_query
;