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 if (query
->Base
.Target
== GL_TIME_ELAPSED_EXT
) {
65 query
->Base
.Result
+= 80 * (results
[1] - results
[0]);
67 query
->Base
.Result
+= 1000 * ((results
[1] >> 32) - (results
[0] >> 32));
69 /* Map and count the pixels from the current query BO */
70 for (i
= query
->first_index
; i
<= query
->last_index
; i
++) {
71 query
->Base
.Result
+= results
[i
* 2 + 1] - results
[i
* 2];
74 drm_intel_bo_unmap(query
->bo
);
76 drm_intel_bo_unreference(query
->bo
);
80 static struct gl_query_object
*
81 brw_new_query_object(struct gl_context
*ctx
, GLuint id
)
83 struct brw_query_object
*query
;
85 query
= calloc(1, sizeof(struct brw_query_object
));
88 query
->Base
.Result
= 0;
89 query
->Base
.Active
= false;
90 query
->Base
.Ready
= true;
96 brw_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
98 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
100 drm_intel_bo_unreference(query
->bo
);
105 brw_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
107 struct brw_context
*brw
= brw_context(ctx
);
108 struct intel_context
*intel
= intel_context(ctx
);
109 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
111 if (query
->Base
.Target
== GL_TIME_ELAPSED_EXT
) {
112 drm_intel_bo_unreference(query
->bo
);
113 query
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "timer query",
116 if (intel
->gen
>= 6) {
118 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
119 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP
);
121 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
122 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
129 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
130 PIPE_CONTROL_WRITE_TIMESTAMP
);
132 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
133 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
140 /* Reset our driver's tracking of query state. */
141 drm_intel_bo_unreference(query
->bo
);
143 query
->first_index
= -1;
144 query
->last_index
= -1;
146 brw
->query
.obj
= query
;
152 * Begin the ARB_occlusion_query query on a query object.
155 brw_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
157 struct brw_context
*brw
= brw_context(ctx
);
158 struct intel_context
*intel
= intel_context(ctx
);
159 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
161 if (query
->Base
.Target
== GL_TIME_ELAPSED_EXT
) {
162 if (intel
->gen
>= 6) {
164 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
165 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP
);
167 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
168 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
175 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
176 PIPE_CONTROL_WRITE_TIMESTAMP
);
178 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
179 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
186 intel_batchbuffer_flush(intel
);
188 /* Flush the batchbuffer in case it has writes to our query BO.
189 * Have later queries write to a new query BO so that further rendering
190 * doesn't delay the collection of our results.
193 brw_emit_query_end(brw
);
194 intel_batchbuffer_flush(intel
);
196 drm_intel_bo_unreference(brw
->query
.bo
);
197 brw
->query
.bo
= NULL
;
200 brw
->query
.obj
= NULL
;
206 static void brw_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
208 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
210 brw_queryobj_get_results(ctx
, query
);
211 query
->Base
.Ready
= true;
214 static void brw_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
216 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
218 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
219 brw_queryobj_get_results(ctx
, query
);
220 query
->Base
.Ready
= true;
224 /** Called to set up the query BO and account for its aperture space */
226 brw_prepare_query_begin(struct brw_context
*brw
)
228 struct intel_context
*intel
= &brw
->intel
;
230 /* Skip if we're not doing any queries. */
234 /* Get a new query BO if we're going to need it. */
235 if (brw
->query
.bo
== NULL
||
236 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
237 drm_intel_bo_unreference(brw
->query
.bo
);
238 brw
->query
.bo
= NULL
;
240 brw
->query
.bo
= drm_intel_bo_alloc(intel
->bufmgr
, "query", 4096, 1);
242 /* clear target buffer */
243 drm_intel_bo_map(brw
->query
.bo
, true);
244 memset((char *)brw
->query
.bo
->virtual, 0, 4096);
245 drm_intel_bo_unmap(brw
->query
.bo
);
247 brw
->query
.index
= 0;
251 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
253 brw_emit_query_begin(struct brw_context
*brw
)
255 struct intel_context
*intel
= &brw
->intel
;
256 struct gl_context
*ctx
= &intel
->ctx
;
257 struct brw_query_object
*query
= brw
->query
.obj
;
259 /* Skip if we're not doing any queries, or we've emitted the start. */
260 if (!query
|| brw
->query
.active
)
263 if (intel
->gen
>= 6) {
266 /* workaround: CS stall required before depth stall. */
267 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
268 OUT_BATCH(PIPE_CONTROL_CS_STALL
);
269 OUT_BATCH(0); /* write address */
270 OUT_BATCH(0); /* write data */
272 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
273 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
|
274 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
275 OUT_RELOC(brw
->query
.bo
,
276 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
277 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
278 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
284 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
285 PIPE_CONTROL_DEPTH_STALL
|
286 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
287 /* This object could be mapped cacheable, but we don't have an exposed
288 * mechanism to support that. Since it's going uncached, tell GEM that
289 * we're writing to it. The usual clflush should be all that's required
290 * to pick up the results.
292 OUT_RELOC(brw
->query
.bo
,
293 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
294 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
295 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
301 if (query
->bo
!= brw
->query
.bo
) {
302 if (query
->bo
!= NULL
)
303 brw_queryobj_get_results(ctx
, query
);
304 drm_intel_bo_reference(brw
->query
.bo
);
305 query
->bo
= brw
->query
.bo
;
306 query
->first_index
= brw
->query
.index
;
308 query
->last_index
= brw
->query
.index
;
309 brw
->query
.active
= true;
312 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
314 brw_emit_query_end(struct brw_context
*brw
)
316 struct intel_context
*intel
= &brw
->intel
;
318 if (!brw
->query
.active
)
321 if (intel
->gen
>= 6) {
323 /* workaround: CS stall required before depth stall. */
324 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
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
);
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 + 1) * sizeof(uint64_t)));
341 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
342 PIPE_CONTROL_DEPTH_STALL
|
343 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
344 OUT_RELOC(brw
->query
.bo
,
345 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
346 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
347 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
353 brw
->query
.active
= false;
357 void brw_init_queryobj_functions(struct dd_function_table
*functions
)
359 functions
->NewQueryObject
= brw_new_query_object
;
360 functions
->DeleteQuery
= brw_delete_query
;
361 functions
->BeginQuery
= brw_begin_query
;
362 functions
->EndQuery
= brw_end_query
;
363 functions
->CheckQuery
= brw_check_query
;
364 functions
->WaitQuery
= brw_wait_query
;