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];
79 assert(!"Unrecognized query target in brw_queryobj_get_results()");
82 drm_intel_bo_unmap(query
->bo
);
84 drm_intel_bo_unreference(query
->bo
);
88 static struct gl_query_object
*
89 brw_new_query_object(struct gl_context
*ctx
, GLuint id
)
91 struct brw_query_object
*query
;
93 query
= calloc(1, sizeof(struct brw_query_object
));
96 query
->Base
.Result
= 0;
97 query
->Base
.Active
= false;
98 query
->Base
.Ready
= true;
104 brw_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
106 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
108 drm_intel_bo_unreference(query
->bo
);
113 brw_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
115 struct brw_context
*brw
= brw_context(ctx
);
116 struct intel_context
*intel
= intel_context(ctx
);
117 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
119 switch (query
->Base
.Target
) {
120 case GL_TIME_ELAPSED_EXT
:
121 drm_intel_bo_unreference(query
->bo
);
122 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "timer query",
125 if (intel
->gen
>= 6) {
127 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
128 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP
);
130 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
131 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
138 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
139 PIPE_CONTROL_WRITE_TIMESTAMP
);
141 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
142 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
150 case GL_SAMPLES_PASSED_ARB
:
151 /* Reset our driver's tracking of query state. */
152 drm_intel_bo_unreference(query
->bo
);
154 query
->first_index
= -1;
155 query
->last_index
= -1;
157 brw
->query
.obj
= query
;
162 assert(!"Unrecognized query target in brw_begin_query()");
168 * Begin the ARB_occlusion_query query on a query object.
171 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
173 struct brw_context
*brw
= brw_context(ctx
);
174 struct intel_context
*intel
= intel_context(ctx
);
175 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
177 switch (query
->Base
.Target
) {
178 case GL_TIME_ELAPSED_EXT
:
179 if (intel
->gen
>= 6) {
181 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
182 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP
);
184 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
185 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
192 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
193 PIPE_CONTROL_WRITE_TIMESTAMP
);
195 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
196 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
203 intel_batchbuffer_flush(intel
);
206 case GL_SAMPLES_PASSED_ARB
:
207 /* Flush the batchbuffer in case it has writes to our query BO.
208 * Have later queries write to a new query BO so that further rendering
209 * doesn't delay the collection of our results.
212 brw_emit_query_end(brw
);
213 intel_batchbuffer_flush(intel
);
215 drm_intel_bo_unreference(brw
->query
.bo
);
216 brw
->query
.bo
= NULL
;
219 brw
->query
.obj
= NULL
;
225 assert(!"Unrecognized query target in brw_end_query()");
230 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
232 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
234 brw_queryobj_get_results(ctx
, query
);
235 query
->Base
.Ready
= true;
238 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
240 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
242 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
243 brw_queryobj_get_results(ctx
, query
);
244 query
->Base
.Ready
= true;
248 /** Called to set up the query BO and account for its aperture space */
250 brw_prepare_query_begin(struct brw_context
*brw
)
252 struct intel_context
*intel
= &brw
->intel
;
254 /* Skip if we're not doing any queries. */
258 /* Get a new query BO if we're going to need it. */
259 if (brw
->query
.bo
== NULL
||
260 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
261 drm_intel_bo_unreference(brw
->query
.bo
);
262 brw
->query
.bo
= NULL
;
264 brw
->query
.bo
= drm_intel_bo_alloc(intel
->bufmgr
, "query", 4096, 1);
266 /* clear target buffer */
267 drm_intel_bo_map(brw
->query
.bo
, true);
268 memset((char *)brw
->query
.bo
->virtual, 0, 4096);
269 drm_intel_bo_unmap(brw
->query
.bo
);
271 brw
->query
.index
= 0;
275 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
277 brw_emit_query_begin(struct brw_context
*brw
)
279 struct intel_context
*intel
= &brw
->intel
;
280 struct gl_context
*ctx
= &intel
->ctx
;
281 struct brw_query_object
*query
= brw
->query
.obj
;
283 /* Skip if we're not doing any queries, or we've emitted the start. */
284 if (!query
|| brw
->query
.active
)
287 if (intel
->gen
>= 6) {
290 /* workaround: CS stall required before depth stall. */
291 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
292 OUT_BATCH(PIPE_CONTROL_CS_STALL
);
293 OUT_BATCH(0); /* write address */
294 OUT_BATCH(0); /* write data */
296 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
297 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
298 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
299 OUT_RELOC(brw
->query
.bo
,
300 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
301 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
302 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
308 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
309 PIPE_CONTROL_DEPTH_STALL
|
310 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
311 /* This object could be mapped cacheable, but we don't have an exposed
312 * mechanism to support that. Since it's going uncached, tell GEM that
313 * we're writing to it. The usual clflush should be all that's required
314 * to pick up the results.
316 OUT_RELOC(brw
->query
.bo
,
317 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
318 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
319 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
325 if (query
->bo
!= brw
->query
.bo
) {
326 if (query
->bo
!= NULL
)
327 brw_queryobj_get_results(ctx
, query
);
328 drm_intel_bo_reference(brw
->query
.bo
);
329 query
->bo
= brw
->query
.bo
;
330 query
->first_index
= brw
->query
.index
;
332 query
->last_index
= brw
->query
.index
;
333 brw
->query
.active
= true;
336 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
338 brw_emit_query_end(struct brw_context
*brw
)
340 struct intel_context
*intel
= &brw
->intel
;
342 if (!brw
->query
.active
)
345 if (intel
->gen
>= 6) {
347 /* workaround: CS stall required before depth stall. */
348 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
349 OUT_BATCH(PIPE_CONTROL_CS_STALL
);
350 OUT_BATCH(0); /* write address */
351 OUT_BATCH(0); /* write data */
353 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
354 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
355 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
356 OUT_RELOC(brw
->query
.bo
,
357 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
358 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
359 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
365 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
366 PIPE_CONTROL_DEPTH_STALL
|
367 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
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 + 1) * sizeof(uint64_t)));
377 brw
->query
.active
= false;
381 void brw_init_queryobj_functions(struct dd_function_table
*functions
)
383 functions
->NewQueryObject
= brw_new_query_object
;
384 functions
->DeleteQuery
= brw_delete_query
;
385 functions
->BeginQuery
= brw_begin_query
;
386 functions
->EndQuery
= brw_end_query
;
387 functions
->CheckQuery
= brw_check_query
;
388 functions
->WaitQuery
= brw_wait_query
;