Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / i965 / brw_queryobj.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 "main/simple_list.h"
42 #include "main/imports.h"
43
44 #include "brw_context.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_reg.h"
47
48 /** Waits on the query object's BO and totals the results for this query */
49 static void
50 brw_queryobj_get_results(struct brw_query_object *query)
51 {
52 int i;
53 uint64_t *results;
54
55 if (query->bo == NULL)
56 return;
57
58 /* Map and count the pixels from the current query BO */
59 dri_bo_map(query->bo, GL_FALSE);
60 results = query->bo->virtual;
61 for (i = query->first_index; i <= query->last_index; i++) {
62 query->Base.Result += results[i * 2 + 1] - results[i * 2];
63 }
64 dri_bo_unmap(query->bo);
65
66 dri_bo_unreference(query->bo);
67 query->bo = NULL;
68 }
69
70 static struct gl_query_object *
71 brw_new_query_object(GLcontext *ctx, GLuint id)
72 {
73 struct brw_query_object *query;
74
75 query = _mesa_calloc(sizeof(struct brw_query_object));
76
77 query->Base.Id = id;
78 query->Base.Result = 0;
79 query->Base.Active = GL_FALSE;
80 query->Base.Ready = GL_TRUE;
81
82 return &query->Base;
83 }
84
85 static void
86 brw_delete_query(GLcontext *ctx, struct gl_query_object *q)
87 {
88 struct brw_query_object *query = (struct brw_query_object *)q;
89
90 dri_bo_unreference(query->bo);
91 _mesa_free(query);
92 }
93
94 static void
95 brw_begin_query(GLcontext *ctx, struct gl_query_object *q)
96 {
97 struct brw_context *brw = brw_context(ctx);
98 struct intel_context *intel = intel_context(ctx);
99 struct brw_query_object *query = (struct brw_query_object *)q;
100
101 /* Reset our driver's tracking of query state. */
102 dri_bo_unreference(query->bo);
103 query->bo = NULL;
104 query->first_index = -1;
105 query->last_index = -1;
106
107 insert_at_head(&brw->query.active_head, query);
108 intel->stats_wm++;
109 }
110
111 /**
112 * Begin the ARB_occlusion_query query on a query object.
113 */
114 static void
115 brw_end_query(GLcontext *ctx, struct gl_query_object *q)
116 {
117 struct brw_context *brw = brw_context(ctx);
118 struct intel_context *intel = intel_context(ctx);
119 struct brw_query_object *query = (struct brw_query_object *)q;
120
121 /* Flush the batchbuffer in case it has writes to our query BO.
122 * Have later queries write to a new query BO so that further rendering
123 * doesn't delay the collection of our results.
124 */
125 if (query->bo) {
126 brw_emit_query_end(brw);
127 intel_batchbuffer_flush(intel->batch);
128
129 dri_bo_unreference(brw->query.bo);
130 brw->query.bo = NULL;
131 }
132
133 remove_from_list(query);
134
135 intel->stats_wm--;
136 }
137
138 static void brw_wait_query(GLcontext *ctx, struct gl_query_object *q)
139 {
140 struct brw_query_object *query = (struct brw_query_object *)q;
141
142 brw_queryobj_get_results(query);
143 query->Base.Ready = GL_TRUE;
144 }
145
146 static void brw_check_query(GLcontext *ctx, struct gl_query_object *q)
147 {
148 /* XXX: Need to expose dri_bo_is_idle from bufmgr. */
149 #if 0
150 struct brw_query_object *query = (struct brw_query_object *)q;
151
152 if (dri_bo_is_idle(query->bo)) {
153 brw_queryobj_get_results(query);
154 query->Base.Ready = GL_TRUE;
155 }
156 #else
157 brw_wait_query(ctx, q);
158 #endif
159 }
160
161 /** Called to set up the query BO and account for its aperture space */
162 void
163 brw_prepare_query_begin(struct brw_context *brw)
164 {
165 struct intel_context *intel = &brw->intel;
166 dri_bo *aper_array[] = {
167 intel->batch->buf,
168 brw->query.bo,
169 };
170
171 /* Skip if we're not doing any queries. */
172 if (is_empty_list(&brw->query.active_head))
173 return;
174
175 /* Get a new query BO if we're going to need it. */
176 if (brw->query.bo == NULL ||
177 brw->query.index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
178 dri_bo_unreference(brw->query.bo);
179 brw->query.bo = NULL;
180
181 brw->query.bo = dri_bo_alloc(intel->bufmgr, "query", 4096, 1);
182 brw->query.index = 0;
183 }
184
185 if (dri_bufmgr_check_aperture_space(aper_array, ARRAY_SIZE(aper_array)))
186 intel_batchbuffer_flush(intel->batch);
187 }
188
189 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
190 void
191 brw_emit_query_begin(struct brw_context *brw)
192 {
193 struct intel_context *intel = &brw->intel;
194 struct brw_query_object *query;
195
196 /* Skip if we're not doing any queries, or we've emitted the start. */
197 if (brw->query.active || is_empty_list(&brw->query.active_head))
198 return;
199
200 BEGIN_BATCH(4, IGNORE_CLIPRECTS);
201 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
202 PIPE_CONTROL_DEPTH_STALL |
203 PIPE_CONTROL_WRITE_DEPTH_COUNT);
204 /* This object could be mapped cacheable, but we don't have an exposed
205 * mechanism to support that. Since it's going uncached, tell GEM that
206 * we're writing to it. The usual clflush should be all that's required
207 * to pick up the results.
208 */
209 OUT_RELOC(brw->query.bo,
210 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
211 PIPE_CONTROL_GLOBAL_GTT_WRITE |
212 ((brw->query.index * 2) * sizeof(uint64_t)));
213 OUT_BATCH(0);
214 OUT_BATCH(0);
215 ADVANCE_BATCH();
216
217 foreach(query, &brw->query.active_head) {
218 if (query->bo != brw->query.bo) {
219 if (query->bo != NULL)
220 brw_queryobj_get_results(query);
221 dri_bo_reference(brw->query.bo);
222 query->bo = brw->query.bo;
223 query->first_index = brw->query.index;
224 }
225 query->last_index = brw->query.index;
226 }
227 brw->query.active = GL_TRUE;
228 }
229
230 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
231 void
232 brw_emit_query_end(struct brw_context *brw)
233 {
234 struct intel_context *intel = &brw->intel;
235
236 if (!brw->query.active)
237 return;
238
239 BEGIN_BATCH(4, IGNORE_CLIPRECTS);
240 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
241 PIPE_CONTROL_DEPTH_STALL |
242 PIPE_CONTROL_WRITE_DEPTH_COUNT);
243 OUT_RELOC(brw->query.bo,
244 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
245 PIPE_CONTROL_GLOBAL_GTT_WRITE |
246 ((brw->query.index * 2 + 1) * sizeof(uint64_t)));
247 OUT_BATCH(0);
248 OUT_BATCH(0);
249 ADVANCE_BATCH();
250
251 brw->query.active = GL_FALSE;
252 brw->query.index++;
253 }
254
255 void brw_init_queryobj_functions(struct dd_function_table *functions)
256 {
257 functions->NewQueryObject = brw_new_query_object;
258 functions->DeleteQuery = brw_delete_query;
259 functions->BeginQuery = brw_begin_query;
260 functions->EndQuery = brw_end_query;
261 functions->CheckQuery = brw_check_query;
262 functions->WaitQuery = brw_wait_query;
263 }