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 brw_query_object
*query
)
55 if (query
->bo
== NULL
)
58 /* Map and count the pixels from the current query BO */
59 dri_bo_map(query
->bo
, GL_FALSE
);
60 results
= query
->bo
->virtual;
61 for (i
= query
->first_index
; i
<= query
->last_index
; i
++) {
62 query
->Base
.Result
+= results
[i
* 2 + 1] - results
[i
* 2];
64 dri_bo_unmap(query
->bo
);
66 dri_bo_unreference(query
->bo
);
70 static struct gl_query_object
*
71 brw_new_query_object(GLcontext
*ctx
, GLuint id
)
73 struct brw_query_object
*query
;
75 query
= calloc(1, sizeof(struct brw_query_object
));
78 query
->Base
.Result
= 0;
79 query
->Base
.Active
= GL_FALSE
;
80 query
->Base
.Ready
= GL_TRUE
;
86 brw_delete_query(GLcontext
*ctx
, struct gl_query_object
*q
)
88 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
90 dri_bo_unreference(query
->bo
);
95 brw_begin_query(GLcontext
*ctx
, struct gl_query_object
*q
)
97 struct brw_context
*brw
= brw_context(ctx
);
98 struct intel_context
*intel
= intel_context(ctx
);
99 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
101 /* Reset our driver's tracking of query state. */
102 dri_bo_unreference(query
->bo
);
104 query
->first_index
= -1;
105 query
->last_index
= -1;
107 brw
->query
.obj
= query
;
112 * Begin the ARB_occlusion_query query on a query object.
115 brw_end_query(GLcontext
*ctx
, struct gl_query_object
*q
)
117 struct brw_context
*brw
= brw_context(ctx
);
118 struct intel_context
*intel
= intel_context(ctx
);
119 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
121 /* Flush the batchbuffer in case it has writes to our query BO.
122 * Have later queries write to a new query BO so that further rendering
123 * doesn't delay the collection of our results.
126 brw_emit_query_end(brw
);
127 intel_batchbuffer_flush(intel
->batch
);
129 dri_bo_unreference(brw
->query
.bo
);
130 brw
->query
.bo
= NULL
;
133 brw
->query
.obj
= NULL
;
138 static void brw_wait_query(GLcontext
*ctx
, struct gl_query_object
*q
)
140 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
142 brw_queryobj_get_results(query
);
143 query
->Base
.Ready
= GL_TRUE
;
146 static void brw_check_query(GLcontext
*ctx
, struct gl_query_object
*q
)
148 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
150 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
151 brw_queryobj_get_results(query
);
152 query
->Base
.Ready
= GL_TRUE
;
156 /** Called to set up the query BO and account for its aperture space */
158 brw_prepare_query_begin(struct brw_context
*brw
)
160 struct intel_context
*intel
= &brw
->intel
;
162 /* Skip if we're not doing any queries. */
166 /* Get a new query BO if we're going to need it. */
167 if (brw
->query
.bo
== NULL
||
168 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
169 dri_bo_unreference(brw
->query
.bo
);
170 brw
->query
.bo
= NULL
;
172 brw
->query
.bo
= dri_bo_alloc(intel
->bufmgr
, "query", 4096, 1);
173 brw
->query
.index
= 0;
176 brw_add_validated_bo(brw
, brw
->query
.bo
);
179 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
181 brw_emit_query_begin(struct brw_context
*brw
)
183 struct intel_context
*intel
= &brw
->intel
;
184 struct brw_query_object
*query
= brw
->query
.obj
;
186 /* Skip if we're not doing any queries, or we've emitted the start. */
187 if (!query
|| brw
->query
.active
)
191 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
192 PIPE_CONTROL_DEPTH_STALL
|
193 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
194 /* This object could be mapped cacheable, but we don't have an exposed
195 * mechanism to support that. Since it's going uncached, tell GEM that
196 * we're writing to it. The usual clflush should be all that's required
197 * to pick up the results.
199 OUT_RELOC(brw
->query
.bo
,
200 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
201 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
202 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
207 if (query
->bo
!= brw
->query
.bo
) {
208 if (query
->bo
!= NULL
)
209 brw_queryobj_get_results(query
);
210 dri_bo_reference(brw
->query
.bo
);
211 query
->bo
= brw
->query
.bo
;
212 query
->first_index
= brw
->query
.index
;
214 query
->last_index
= brw
->query
.index
;
215 brw
->query
.active
= GL_TRUE
;
218 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
220 brw_emit_query_end(struct brw_context
*brw
)
222 struct intel_context
*intel
= &brw
->intel
;
224 if (!brw
->query
.active
)
228 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
229 PIPE_CONTROL_DEPTH_STALL
|
230 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
231 OUT_RELOC(brw
->query
.bo
,
232 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
233 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
234 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
239 brw
->query
.active
= GL_FALSE
;
243 void brw_init_queryobj_functions(struct dd_function_table
*functions
)
245 functions
->NewQueryObject
= brw_new_query_object
;
246 functions
->DeleteQuery
= brw_delete_query
;
247 functions
->BeginQuery
= brw_begin_query
;
248 functions
->EndQuery
= brw_end_query
;
249 functions
->CheckQuery
= brw_check_query
;
250 functions
->WaitQuery
= brw_wait_query
;