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
;
57 uint64_t *result
= (uint64_t*)vresult
;
59 /* Map and count the pixels from the current query BO */
64 if (brw
->sws
->bo_is_busy(query
->bo
) && !wait
)
67 map
= bo_map_read(brw
->sws
, query
->bo
);
71 for (i
= query
->first_index
; i
<= query
->last_index
; i
++) {
72 query
->result
+= map
[i
* 2 + 1] - map
[i
* 2];
75 brw
->sws
->bo_unmap(query
->bo
);
76 bo_reference(&query
->bo
, NULL
);
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_query_object
*query
= (struct brw_query_object
*)q
;
105 bo_reference(&query
->bo
, NULL
);
110 brw_query_begin(struct pipe_context
*pipe
, struct pipe_query
*q
)
112 struct brw_context
*brw
= brw_context(pipe
);
113 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
115 /* Reset our driver's tracking of query state. */
116 bo_reference(&query
->bo
, NULL
);
118 query
->first_index
= -1;
119 query
->last_index
= -1;
121 insert_at_head(&brw
->query
.active_head
, query
);
122 brw
->query
.stats_wm
++;
123 brw
->state
.dirty
.mesa
|= PIPE_NEW_QUERY
;
127 brw_query_end(struct pipe_context
*pipe
, struct pipe_query
*q
)
129 struct brw_context
*brw
= brw_context(pipe
);
130 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
132 /* Flush the batchbuffer in case it has writes to our query BO.
133 * Have later queries write to a new query BO so that further rendering
134 * doesn't delay the collection of our results.
137 brw_emit_query_end(brw
);
138 brw_context_flush( brw
);
140 bo_reference(&brw
->query
.bo
, NULL
);
143 remove_from_list(query
);
144 brw
->query
.stats_wm
--;
145 brw
->state
.dirty
.mesa
|= PIPE_NEW_QUERY
;
148 /***********************************************************************
149 * Internal functions and callbacks to implement queries
152 /** Called to set up the query BO and account for its aperture space */
154 brw_prepare_query_begin(struct brw_context
*brw
)
158 /* Skip if we're not doing any queries. */
159 if (is_empty_list(&brw
->query
.active_head
))
162 /* Get a new query BO if we're going to need it. */
163 if (brw
->query
.bo
== NULL
||
164 brw
->query
.index
* 2 + 1 >= 4096 / sizeof(uint64_t)) {
166 ret
= brw
->sws
->bo_alloc(brw
->sws
, BRW_BUFFER_TYPE_QUERY
, 4096, 1,
171 brw
->query
.index
= 0;
174 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 brw_query_object
*query
;
185 /* Skip if we're not doing any queries, or we've emitted the start. */
186 if (brw
->query
.active
|| is_empty_list(&brw
->query
.active_head
))
189 BEGIN_BATCH(4, IGNORE_CLIPRECTS
);
190 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
191 PIPE_CONTROL_DEPTH_STALL
|
192 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
193 /* This object could be mapped cacheable, but we don't have an exposed
194 * mechanism to support that. Since it's going uncached, tell GEM that
195 * we're writing to it. The usual clflush should be all that's required
196 * to pick up the results.
198 OUT_RELOC(brw
->query
.bo
,
199 BRW_USAGE_QUERY_RESULT
,
200 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
201 ((brw
->query
.index
* 2) * sizeof(uint64_t)));
206 foreach(query
, &brw
->query
.active_head
) {
207 if (query
->bo
!= brw
->query
.bo
) {
210 /* Propogate the results from this buffer to all of the
211 * active queries, as the bo is going away.
213 if (query
->bo
!= NULL
)
214 brw_query_get_result( &brw
->base
,
215 (struct pipe_query
*)query
,
219 bo_reference( &query
->bo
, brw
->query
.bo
);
220 query
->first_index
= brw
->query
.index
;
222 query
->last_index
= brw
->query
.index
;
224 brw
->query
.active
= GL_TRUE
;
227 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
229 brw_emit_query_end(struct brw_context
*brw
)
231 if (!brw
->query
.active
)
234 BEGIN_BATCH(4, IGNORE_CLIPRECTS
);
235 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
236 PIPE_CONTROL_DEPTH_STALL
|
237 PIPE_CONTROL_WRITE_DEPTH_COUNT
);
238 OUT_RELOC(brw
->query
.bo
,
239 BRW_USAGE_QUERY_RESULT
,
240 PIPE_CONTROL_GLOBAL_GTT_WRITE
|
241 ((brw
->query
.index
* 2 + 1) * sizeof(uint64_t)));
246 brw
->query
.active
= GL_FALSE
;
250 void brw_pipe_query_init( struct brw_context
*brw
)
252 brw
->base
.create_query
= brw_query_create
;
253 brw
->base
.destroy_query
= brw_query_destroy
;
254 brw
->base
.begin_query
= brw_query_begin
;
255 brw
->base
.end_query
= brw_query_end
;
256 brw
->base
.get_query_result
= brw_query_get_result
;
260 void brw_pipe_query_cleanup( struct brw_context
*brw
)
262 /* Unreference brw->query.bo ??