softpipe: Include missing header in sp_flush.c.
[mesa.git] / src / gallium / drivers / i965 / brw_pipe_query.c
1 /*
2 * Copyright © 2008 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 /** @file support for ARB_query_object
29 *
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.
33 *
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.
40 */
41 #include "util/u_simple_list.h"
42
43 #include "brw_context.h"
44 #include "brw_state.h"
45 #include "brw_batchbuffer.h"
46 #include "brw_reg.h"
47
48 /** Waits on the query object's BO and totals the results for this query */
49 static boolean
50 brw_query_get_result(struct pipe_context *pipe,
51 struct pipe_query *q,
52 boolean wait,
53 void *vresult)
54 {
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;
58
59 /* Map and count the pixels from the current query BO */
60 if (query->bo) {
61 int i;
62 uint64_t *map;
63
64 if (brw->sws->bo_is_busy(query->bo) && !wait)
65 return FALSE;
66
67 map = bo_map_read(brw->sws, query->bo);
68 if (map == NULL)
69 return FALSE;
70
71 for (i = query->first_index; i <= query->last_index; i++) {
72 query->result += map[i * 2 + 1] - map[i * 2];
73 }
74
75 brw->sws->bo_unmap(query->bo);
76 bo_reference(&query->bo, NULL);
77 }
78
79 *result = query->result;
80 return TRUE;
81 }
82
83 static struct pipe_query *
84 brw_query_create(struct pipe_context *pipe, unsigned type )
85 {
86 struct brw_query_object *query;
87
88 switch (type) {
89 case PIPE_QUERY_OCCLUSION_COUNTER:
90 query = CALLOC_STRUCT( brw_query_object );
91 if (query == NULL)
92 return NULL;
93 return (struct pipe_query *)query;
94
95 default:
96 return NULL;
97 }
98 }
99
100 static void
101 brw_query_destroy(struct pipe_context *pipe, struct pipe_query *q)
102 {
103 struct brw_query_object *query = (struct brw_query_object *)q;
104
105 bo_reference(&query->bo, NULL);
106 FREE(query);
107 }
108
109 static void
110 brw_query_begin(struct pipe_context *pipe, struct pipe_query *q)
111 {
112 struct brw_context *brw = brw_context(pipe);
113 struct brw_query_object *query = (struct brw_query_object *)q;
114
115 /* Reset our driver's tracking of query state. */
116 bo_reference(&query->bo, NULL);
117 query->result = 0;
118 query->first_index = -1;
119 query->last_index = -1;
120
121 insert_at_head(&brw->query.active_head, query);
122 brw->query.stats_wm++;
123 brw->state.dirty.mesa |= PIPE_NEW_QUERY;
124 }
125
126 static void
127 brw_query_end(struct pipe_context *pipe, struct pipe_query *q)
128 {
129 struct brw_context *brw = brw_context(pipe);
130 struct brw_query_object *query = (struct brw_query_object *)q;
131
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.
135 */
136 if (query->bo) {
137 brw_emit_query_end(brw);
138 brw_context_flush( brw );
139
140 bo_reference(&brw->query.bo, NULL);
141 }
142
143 remove_from_list(query);
144 brw->query.stats_wm--;
145 brw->state.dirty.mesa |= PIPE_NEW_QUERY;
146 }
147
148 /***********************************************************************
149 * Internal functions and callbacks to implement queries
150 */
151
152 /** Called to set up the query BO and account for its aperture space */
153 enum pipe_error
154 brw_prepare_query_begin(struct brw_context *brw)
155 {
156 enum pipe_error ret;
157
158 /* Skip if we're not doing any queries. */
159 if (is_empty_list(&brw->query.active_head))
160 return PIPE_OK;
161
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)) {
165
166 ret = brw->sws->bo_alloc(brw->sws, BRW_BUFFER_TYPE_QUERY, 4096, 1,
167 &brw->query.bo);
168 if (ret)
169 return ret;
170
171 brw->query.index = 0;
172 }
173
174 brw_add_validated_bo(brw, brw->query.bo);
175
176 return PIPE_OK;
177 }
178
179 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
180 void
181 brw_emit_query_begin(struct brw_context *brw)
182 {
183 struct brw_query_object *query;
184
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))
187 return;
188
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.
197 */
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)));
202 OUT_BATCH(0);
203 OUT_BATCH(0);
204 ADVANCE_BATCH();
205
206 foreach(query, &brw->query.active_head) {
207 if (query->bo != brw->query.bo) {
208 uint64_t tmp;
209
210 /* Propogate the results from this buffer to all of the
211 * active queries, as the bo is going away.
212 */
213 if (query->bo != NULL)
214 brw_query_get_result( &brw->base,
215 (struct pipe_query *)query,
216 FALSE,
217 &tmp );
218
219 bo_reference( &query->bo, brw->query.bo );
220 query->first_index = brw->query.index;
221 }
222 query->last_index = brw->query.index;
223 }
224 brw->query.active = GL_TRUE;
225 }
226
227 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
228 void
229 brw_emit_query_end(struct brw_context *brw)
230 {
231 if (!brw->query.active)
232 return;
233
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)));
242 OUT_BATCH(0);
243 OUT_BATCH(0);
244 ADVANCE_BATCH();
245
246 brw->query.active = GL_FALSE;
247 brw->query.index++;
248 }
249
250 void brw_pipe_query_init( struct brw_context *brw )
251 {
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;
257 }
258
259
260 void brw_pipe_query_cleanup( struct brw_context *brw )
261 {
262 /* Unreference brw->query.bo ??
263 */
264 }