Merge branch '7.8'
[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 uint64_t *result)
54 {
55 struct brw_context *brw = brw_context(pipe);
56 struct brw_query_object *query = (struct brw_query_object *)q;
57
58 /* Map and count the pixels from the current query BO */
59 if (query->bo) {
60 int i;
61 uint64_t *map;
62
63 if (brw->sws->bo_is_busy(query->bo) && !wait)
64 return FALSE;
65
66 map = bo_map_read(brw->sws, query->bo);
67 if (map == NULL)
68 return FALSE;
69
70 for (i = query->first_index; i <= query->last_index; i++) {
71 query->result += map[i * 2 + 1] - map[i * 2];
72 }
73
74 brw->sws->bo_unmap(query->bo);
75 bo_reference(&query->bo, NULL);
76 }
77
78 *result = query->result;
79 return TRUE;
80 }
81
82 static struct pipe_query *
83 brw_query_create(struct pipe_context *pipe, unsigned type )
84 {
85 struct brw_query_object *query;
86
87 switch (type) {
88 case PIPE_QUERY_OCCLUSION_COUNTER:
89 query = CALLOC_STRUCT( brw_query_object );
90 if (query == NULL)
91 return NULL;
92 return (struct pipe_query *)query;
93
94 default:
95 return NULL;
96 }
97 }
98
99 static void
100 brw_query_destroy(struct pipe_context *pipe, struct pipe_query *q)
101 {
102 struct brw_query_object *query = (struct brw_query_object *)q;
103
104 bo_reference(&query->bo, NULL);
105 FREE(query);
106 }
107
108 static void
109 brw_query_begin(struct pipe_context *pipe, struct pipe_query *q)
110 {
111 struct brw_context *brw = brw_context(pipe);
112 struct brw_query_object *query = (struct brw_query_object *)q;
113
114 /* Reset our driver's tracking of query state. */
115 bo_reference(&query->bo, NULL);
116 query->result = 0;
117 query->first_index = -1;
118 query->last_index = -1;
119
120 insert_at_head(&brw->query.active_head, query);
121 brw->query.stats_wm++;
122 brw->state.dirty.mesa |= PIPE_NEW_QUERY;
123 }
124
125 static void
126 brw_query_end(struct pipe_context *pipe, struct pipe_query *q)
127 {
128 struct brw_context *brw = brw_context(pipe);
129 struct brw_query_object *query = (struct brw_query_object *)q;
130
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.
134 */
135 if (query->bo) {
136 brw_emit_query_end(brw);
137 brw_context_flush( brw );
138
139 bo_reference(&brw->query.bo, NULL);
140 }
141
142 remove_from_list(query);
143 brw->query.stats_wm--;
144 brw->state.dirty.mesa |= PIPE_NEW_QUERY;
145 }
146
147 /***********************************************************************
148 * Internal functions and callbacks to implement queries
149 */
150
151 /** Called to set up the query BO and account for its aperture space */
152 enum pipe_error
153 brw_prepare_query_begin(struct brw_context *brw)
154 {
155 enum pipe_error ret;
156
157 /* Skip if we're not doing any queries. */
158 if (is_empty_list(&brw->query.active_head))
159 return PIPE_OK;
160
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)) {
164
165 ret = brw->sws->bo_alloc(brw->sws, BRW_BUFFER_TYPE_QUERY, 4096, 1,
166 &brw->query.bo);
167 if (ret)
168 return ret;
169
170 brw->query.index = 0;
171 }
172
173 brw_add_validated_bo(brw, brw->query.bo);
174
175 return PIPE_OK;
176 }
177
178 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
179 void
180 brw_emit_query_begin(struct brw_context *brw)
181 {
182 struct brw_query_object *query;
183
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))
186 return;
187
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.
196 */
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)));
201 OUT_BATCH(0);
202 OUT_BATCH(0);
203 ADVANCE_BATCH();
204
205 foreach(query, &brw->query.active_head) {
206 if (query->bo != brw->query.bo) {
207 uint64_t tmp;
208
209 /* Propogate the results from this buffer to all of the
210 * active queries, as the bo is going away.
211 */
212 if (query->bo != NULL)
213 brw_query_get_result( &brw->base,
214 (struct pipe_query *)query,
215 FALSE,
216 &tmp );
217
218 bo_reference( &query->bo, brw->query.bo );
219 query->first_index = brw->query.index;
220 }
221 query->last_index = brw->query.index;
222 }
223 brw->query.active = GL_TRUE;
224 }
225
226 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
227 void
228 brw_emit_query_end(struct brw_context *brw)
229 {
230 if (!brw->query.active)
231 return;
232
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)));
241 OUT_BATCH(0);
242 OUT_BATCH(0);
243 ADVANCE_BATCH();
244
245 brw->query.active = GL_FALSE;
246 brw->query.index++;
247 }
248
249 void brw_pipe_query_init( struct brw_context *brw )
250 {
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;
256 }
257
258
259 void brw_pipe_query_cleanup( struct brw_context *brw )
260 {
261 /* Unreference brw->query.bo ??
262 */
263 }