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 "util/u_simple_list.h"
43 #include "brw_context.h"
44 #include "brw_state.h"
45 #include "brw_batchbuffer.h"
48 /** Waits on the query object's BO and totals the results for this query */
50 brw_query_get_result(struct pipe_context
*pipe
,
55 struct brw_context
*brw
= brw_context(pipe
);
56 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
58 /* Map and count the pixels from the current query BO */
63 if (brw
->sws
->bo_is_busy(query
->bo
) && !wait
)
66 map
= brw
->sws
->bo_map(query
->bo
, GL_FALSE
);
70 for (i
= query
->first_index
; i
<= query
->last_index
; i
++) {
71 query
->result
+= map
[i
* 2 + 1] - map
[i
* 2];
74 brw
->sws
->bo_unmap(query
->bo
);
75 brw
->sws
->bo_unreference(query
->bo
);
79 *result
= query
->result
;
83 static struct pipe_query
*
84 brw_query_create(struct pipe_context
*pipe
, unsigned type
)
86 struct brw_query_object
*query
;
89 case PIPE_QUERY_OCCLUSION_COUNTER
:
90 query
= CALLOC_STRUCT( brw_query_object
);
93 return (struct pipe_query
*)query
;
101 brw_query_destroy(struct pipe_context
*pipe
, struct pipe_query
*q
)
103 struct brw_context
*brw
= brw_context(pipe
);
104 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
106 brw
->sws
->bo_unreference(query
->bo
);
111 brw_query_begin(struct pipe_context
*pipe
, struct pipe_query
*q
)
113 struct brw_context
*brw
= brw_context(pipe
);
114 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
116 /* Reset our driver's tracking of query state. */
117 brw
->sws
->bo_unreference(query
->bo
);
120 query
->first_index
= -1;
121 query
->last_index
= -1;
123 insert_at_head(&brw
->query
.active_head
, query
);
124 brw
->query
.stats_wm
++;
125 brw
->state
.dirty
.mesa
|= PIPE_NEW_QUERY
;
129 brw_query_end(struct pipe_context
*pipe
, struct pipe_query
*q
)
131 struct brw_context
*brw
= brw_context(pipe
);
132 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
134 /* Flush the batchbuffer in case it has writes to our query BO.
135 * Have later queries write to a new query BO so that further rendering
136 * doesn't delay the collection of our results.
139 brw_emit_query_end(brw
);
140 brw_batchbuffer_flush(brw
->batch
);
142 brw
->sws
->bo_unreference(brw
->query
.bo
);
143 brw
->query
.bo
= NULL
;
146 remove_from_list(query
);
147 brw
->query
.stats_wm
--;
148 brw
->state
.dirty
.mesa
|= PIPE_NEW_QUERY
;
151 /***********************************************************************
152 * Internal functions and callbacks to implement queries
155 /** Called to set up the query BO and account for its aperture space */
157 brw_prepare_query_begin(struct brw_context
*brw
)
159 /* Skip if we're not doing any queries. */
160 if (is_empty_list(&brw
->query
.active_head
))
163 /* Get a new query BO if we're going to need it. */
164 if (brw
->query
.bo
== NULL
||
165 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
166 brw
->sws
->bo_unreference(brw
->query
.bo
);
167 brw
->query
.bo
= NULL
;
169 brw
->query
.bo
= brw
->sws
->bo_alloc(brw
->sws
, BRW_BUFFER_TYPE_QUERY
, 4096, 1);
170 brw
->query
.index
= 0;
173 brw_add_validated_bo(brw
, brw
->query
.bo
);
176 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
178 brw_emit_query_begin(struct brw_context
*brw
)
180 struct brw_query_object
*query
;
182 /* Skip if we're not doing any queries, or we've emitted the start. */
183 if (brw
->query
.active
|| is_empty_list(&brw
->query
.active_head
))
186 BEGIN_BATCH(4, IGNORE_CLIPRECTS
);
187 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
188 PIPE_CONTROL_DEPTH_STALL
|
189 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
190 /* This object could be mapped cacheable, but we don't have an exposed
191 * mechanism to support that. Since it's going uncached, tell GEM that
192 * we're writing to it. The usual clflush should be all that's required
193 * to pick up the results.
195 OUT_RELOC(brw
->query
.bo
,
196 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
197 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
198 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
203 foreach(query
, &brw
->query
.active_head
) {
204 if (query
->bo
!= brw
->query
.bo
) {
207 /* Propogate the results from this buffer to all of the
208 * active queries, as the bo is going away.
210 if (query
->bo
!= NULL
)
211 brw_query_get_result( &brw
->base
,
212 (struct pipe_query
*)query
,
216 brw
->sws
->bo_reference(brw
->query
.bo
);
217 query
->bo
= brw
->query
.bo
;
218 query
->first_index
= brw
->query
.index
;
220 query
->last_index
= brw
->query
.index
;
222 brw
->query
.active
= GL_TRUE
;
225 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
227 brw_emit_query_end(struct brw_context
*brw
)
229 if (!brw
->query
.active
)
232 BEGIN_BATCH(4, IGNORE_CLIPRECTS
);
233 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
234 PIPE_CONTROL_DEPTH_STALL
|
235 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
236 OUT_RELOC(brw
->query
.bo
,
237 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
238 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
239 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
244 brw
->query
.active
= GL_FALSE
;
248 void brw_pipe_query_init( struct brw_context
*brw
)
250 brw
->base
.create_query
= brw_query_create
;
251 brw
->base
.destroy_query
= brw_query_destroy
;
252 brw
->base
.begin_query
= brw_query_begin
;
253 brw
->base
.end_query
= brw_query_end
;
254 brw
->base
.get_query_result
= brw_query_get_result
;
258 void brw_pipe_query_cleanup( struct brw_context
*brw
)
260 /* Unreference brw->query.bo ??