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
= bo_map_read(brw
->sws
, query
->bo
);
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 bo_reference(&query
->bo
, NULL
);
78 *result
= query
->result
;
82 static struct pipe_query
*
83 brw_query_create(struct pipe_context
*pipe
, unsigned type
)
85 struct brw_query_object
*query
;
88 case PIPE_QUERY_OCCLUSION_COUNTER
:
89 query
= CALLOC_STRUCT( brw_query_object
);
92 return (struct pipe_query
*)query
;
100 brw_query_destroy(struct pipe_context
*pipe
, struct pipe_query
*q
)
102 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
104 bo_reference(&query
->bo
, NULL
);
109 brw_query_begin(struct pipe_context
*pipe
, struct pipe_query
*q
)
111 struct brw_context
*brw
= brw_context(pipe
);
112 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
114 /* Reset our driver's tracking of query state. */
115 bo_reference(&query
->bo
, NULL
);
117 query
->first_index
= -1;
118 query
->last_index
= -1;
120 insert_at_head(&brw
->query
.active_head
, query
);
121 brw
->query
.stats_wm
++;
122 brw
->state
.dirty
.mesa
|= PIPE_NEW_QUERY
;
126 brw_query_end(struct pipe_context
*pipe
, struct pipe_query
*q
)
128 struct brw_context
*brw
= brw_context(pipe
);
129 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
131 /* Flush the batchbuffer in case it has writes to our query BO.
132 * Have later queries write to a new query BO so that further rendering
133 * doesn't delay the collection of our results.
136 brw_emit_query_end(brw
);
137 brw_context_flush( brw
);
139 bo_reference(&brw
->query
.bo
, NULL
);
142 remove_from_list(query
);
143 brw
->query
.stats_wm
--;
144 brw
->state
.dirty
.mesa
|= PIPE_NEW_QUERY
;
147 /***********************************************************************
148 * Internal functions and callbacks to implement queries
151 /** Called to set up the query BO and account for its aperture space */
153 brw_prepare_query_begin(struct brw_context
*brw
)
157 /* Skip if we're not doing any queries. */
158 if (is_empty_list(&brw
->query
.active_head
))
161 /* Get a new query BO if we're going to need it. */
162 if (brw
->query
.bo
== NULL
||
163 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
165 ret
= 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
);
178 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
180 brw_emit_query_begin(struct brw_context
*brw
)
182 struct brw_query_object
*query
;
184 /* Skip if we're not doing any queries, or we've emitted the start. */
185 if (brw
->query
.active
|| is_empty_list(&brw
->query
.active_head
))
188 BEGIN_BATCH(4, IGNORE_CLIPRECTS
);
189 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
190 PIPE_CONTROL_DEPTH_STALL
|
191 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
192 /* This object could be mapped cacheable, but we don't have an exposed
193 * mechanism to support that. Since it's going uncached, tell GEM that
194 * we're writing to it. The usual clflush should be all that's required
195 * to pick up the results.
197 OUT_RELOC(brw
->query
.bo
,
198 BRW_USAGE_QUERY_RESULT
,
199 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
200 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
205 foreach(query
, &brw
->query
.active_head
) {
206 if (query
->bo
!= brw
->query
.bo
) {
209 /* Propogate the results from this buffer to all of the
210 * active queries, as the bo is going away.
212 if (query
->bo
!= NULL
)
213 brw_query_get_result( &brw
->base
,
214 (struct pipe_query
*)query
,
218 bo_reference( &query
->bo
, brw
->query
.bo
);
219 query
->first_index
= brw
->query
.index
;
221 query
->last_index
= brw
->query
.index
;
223 brw
->query
.active
= GL_TRUE
;
226 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
228 brw_emit_query_end(struct brw_context
*brw
)
230 if (!brw
->query
.active
)
233 BEGIN_BATCH(4, IGNORE_CLIPRECTS
);
234 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
235 PIPE_CONTROL_DEPTH_STALL
|
236 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
237 OUT_RELOC(brw
->query
.bo
,
238 BRW_USAGE_QUERY_RESULT
,
239 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
240 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
245 brw
->query
.active
= GL_FALSE
;
249 void brw_pipe_query_init( struct brw_context
*brw
)
251 brw
->base
.create_query
= brw_query_create
;
252 brw
->base
.destroy_query
= brw_query_destroy
;
253 brw
->base
.begin_query
= brw_query_begin
;
254 brw
->base
.end_query
= brw_query_end
;
255 brw
->base
.get_query_result
= brw_query_get_result
;
259 void brw_pipe_query_cleanup( struct brw_context
*brw
)
261 /* Unreference brw->query.bo ??