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 drm_intel_bo_map(query
->bo
, false);
143 results
= query
->bo
->virtual;
144 switch (query
->Base
.Target
) {
145 case GL_TIME_ELAPSED_EXT
:
147 query
->Base
.Result
+= 80 * (results
[1] - results
[0]);
149 query
->Base
.Result
+= 1000 * ((results
[1] >> 32) - (results
[0] >> 32));
152 case GL_SAMPLES_PASSED_ARB
:
153 /* Map and count the pixels from the current query BO */
154 for (i
= query
->first_index
; i
<= query
->last_index
; i
++) {
155 query
->Base
.Result
+= results
[i
* 2 + 1] - results
[i
* 2];
159 case GL_PRIMITIVES_GENERATED
:
160 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
161 /* We don't actually query the hardware for this value, so query->bo
162 * should always be NULL and execution should never reach here.
164 assert(!"Unreachable");
168 assert(!"Unrecognized query target in brw_queryobj_get_results()");
171 drm_intel_bo_unmap(query
->bo
);
173 drm_intel_bo_unreference(query
->bo
);
177 static struct gl_query_object
*
178 brw_new_query_object(struct gl_context
*ctx
, GLuint id
)
180 struct brw_query_object
*query
;
182 query
= calloc(1, sizeof(struct brw_query_object
));
185 query
->Base
.Result
= 0;
186 query
->Base
.Active
= false;
187 query
->Base
.Ready
= true;
193 brw_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
195 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
197 drm_intel_bo_unreference(query
->bo
);
202 brw_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
204 struct brw_context
*brw
= brw_context(ctx
);
205 struct intel_context
*intel
= intel_context(ctx
);
206 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
208 switch (query
->Base
.Target
) {
209 case GL_TIME_ELAPSED_EXT
:
210 drm_intel_bo_unreference(query
->bo
);
211 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "timer query", 4096, 4096);
212 write_timestamp(intel
, query
->bo
, 0);
215 case GL_SAMPLES_PASSED_ARB
:
216 /* Reset our driver's tracking of query state. */
217 drm_intel_bo_unreference(query
->bo
);
219 query
->first_index
= -1;
220 query
->last_index
= -1;
222 brw
->query
.obj
= query
;
226 case GL_PRIMITIVES_GENERATED
:
227 /* We don't actually query the hardware for this value; we keep track of
228 * it a software counter. So just reset the counter.
230 brw
->sol
.primitives_generated
= 0;
231 brw
->sol
.counting_primitives_generated
= true;
234 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
235 /* We don't actually query the hardware for this value; we keep track of
236 * it a software counter. So just reset the counter.
238 brw
->sol
.primitives_written
= 0;
239 brw
->sol
.counting_primitives_written
= true;
243 assert(!"Unrecognized query target in brw_begin_query()");
249 * Begin the ARB_occlusion_query query on a query object.
252 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
254 struct brw_context
*brw
= brw_context(ctx
);
255 struct intel_context
*intel
= intel_context(ctx
);
256 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
258 switch (query
->Base
.Target
) {
259 case GL_TIME_ELAPSED_EXT
:
260 write_timestamp(intel
, query
->bo
, 1);
261 intel_batchbuffer_flush(intel
);
264 case GL_SAMPLES_PASSED_ARB
:
265 /* Flush the batchbuffer in case it has writes to our query BO.
266 * Have later queries write to a new query BO so that further rendering
267 * doesn't delay the collection of our results.
270 brw_emit_query_end(brw
);
271 intel_batchbuffer_flush(intel
);
273 drm_intel_bo_unreference(brw
->query
.bo
);
274 brw
->query
.bo
= NULL
;
277 brw
->query
.obj
= NULL
;
282 case GL_PRIMITIVES_GENERATED
:
283 /* We don't actually query the hardware for this value; we keep track of
284 * it in a software counter. So just read the counter and store it in
287 query
->Base
.Result
= brw
->sol
.primitives_generated
;
288 brw
->sol
.counting_primitives_generated
= false;
290 /* And set brw->query.obj to NULL so that this query won't try to wait
291 * for any rendering to complete.
296 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
297 /* We don't actually query the hardware for this value; we keep track of
298 * it in a software counter. So just read the counter and store it in
301 query
->Base
.Result
= brw
->sol
.primitives_written
;
302 brw
->sol
.counting_primitives_written
= false;
304 /* And set brw->query.obj to NULL so that this query won't try to wait
305 * for any rendering to complete.
311 assert(!"Unrecognized query target in brw_end_query()");
316 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
318 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
320 brw_queryobj_get_results(ctx
, query
);
321 query
->Base
.Ready
= true;
324 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
326 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
328 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
329 brw_queryobj_get_results(ctx
, query
);
330 query
->Base
.Ready
= true;
334 /** Called to set up the query BO and account for its aperture space */
336 brw_prepare_query_begin(struct brw_context
*brw
)
338 struct intel_context
*intel
= &brw
->intel
;
340 /* Skip if we're not doing any queries. */
344 /* Get a new query BO if we're going to need it. */
345 if (brw
->query
.bo
== NULL
||
346 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
347 drm_intel_bo_unreference(brw
->query
.bo
);
348 brw
->query
.bo
= NULL
;
350 brw
->query
.bo
= drm_intel_bo_alloc(intel
->bufmgr
, "query", 4096, 1);
352 /* clear target buffer */
353 drm_intel_bo_map(brw
->query
.bo
, true);
354 memset((char *)brw
->query
.bo
->virtual, 0, 4096);
355 drm_intel_bo_unmap(brw
->query
.bo
);
357 brw
->query
.index
= 0;
361 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
363 brw_emit_query_begin(struct brw_context
*brw
)
365 struct intel_context
*intel
= &brw
->intel
;
366 struct gl_context
*ctx
= &intel
->ctx
;
367 struct brw_query_object
*query
= brw
->query
.obj
;
369 /* Skip if we're not doing any queries, or we've emitted the start. */
370 if (!query
|| brw
->query
.active
)
373 write_depth_count(intel
, brw
->query
.bo
, brw
->query
.index
* 2);
375 if (query
->bo
!= brw
->query
.bo
) {
376 if (query
->bo
!= NULL
)
377 brw_queryobj_get_results(ctx
, query
);
378 drm_intel_bo_reference(brw
->query
.bo
);
379 query
->bo
= brw
->query
.bo
;
380 query
->first_index
= brw
->query
.index
;
382 query
->last_index
= brw
->query
.index
;
383 brw
->query
.active
= true;
386 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
388 brw_emit_query_end(struct brw_context
*brw
)
390 struct intel_context
*intel
= &brw
->intel
;
392 if (!brw
->query
.active
)
395 write_depth_count(intel
, brw
->query
.bo
, brw
->query
.index
* 2 + 1);
397 brw
->query
.active
= false;
401 void brw_init_queryobj_functions(struct dd_function_table
*functions
)
403 functions
->NewQueryObject
= brw_new_query_object
;
404 functions
->DeleteQuery
= brw_delete_query
;
405 functions
->BeginQuery
= brw_begin_query
;
406 functions
->EndQuery
= brw_end_query
;
407 functions
->CheckQuery
= brw_check_query
;
408 functions
->WaitQuery
= brw_wait_query
;