Merge remote branch 'origin/master' into pipe-video
[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/imports.h"
42
43 #include "brw_context.h"
44 #include "brw_state.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 drm_intel_bo_map(query->bo, GL_FALSE);
59 results = query->bo->virtual;
60 if (query->Base.Target == GL_TIME_ELAPSED_EXT) {
61 query->Base.Result += 1000 * ((results[1] >> 32) - (results[0] >> 32));
62 } else {
63 /* Map and count the pixels from the current query BO */
64 for (i = query->first_index; i <= query->last_index; i++) {
65 query->Base.Result += results[i * 2 + 1] - results[i * 2];
66 }
67 }
68 drm_intel_bo_unmap(query->bo);
69
70 drm_intel_bo_unreference(query->bo);
71 query->bo = NULL;
72 }
73
74 static struct gl_query_object *
75 brw_new_query_object(struct gl_context *ctx, GLuint id)
76 {
77 struct brw_query_object *query;
78
79 query = calloc(1, sizeof(struct brw_query_object));
80
81 query->Base.Id = id;
82 query->Base.Result = 0;
83 query->Base.Active = GL_FALSE;
84 query->Base.Ready = GL_TRUE;
85
86 return &query->Base;
87 }
88
89 static void
90 brw_delete_query(struct gl_context *ctx, struct gl_query_object *q)
91 {
92 struct brw_query_object *query = (struct brw_query_object *)q;
93
94 drm_intel_bo_unreference(query->bo);
95 free(query);
96 }
97
98 static void
99 brw_begin_query(struct gl_context *ctx, struct gl_query_object *q)
100 {
101 struct brw_context *brw = brw_context(ctx);
102 struct intel_context *intel = intel_context(ctx);
103 struct brw_query_object *query = (struct brw_query_object *)q;
104
105 if (query->Base.Target == GL_TIME_ELAPSED_EXT) {
106 drm_intel_bo_unreference(query->bo);
107 query->bo = drm_intel_bo_alloc(intel->bufmgr, "timer query",
108 4096, 4096);
109
110 if (intel->gen >= 6) {
111 BEGIN_BATCH(4);
112 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
113 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
114 OUT_RELOC(query->bo,
115 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
116 PIPE_CONTROL_GLOBAL_GTT_WRITE |
117 0);
118 OUT_BATCH(0);
119 ADVANCE_BATCH();
120
121 } else {
122 BEGIN_BATCH(4);
123 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
124 PIPE_CONTROL_WRITE_TIMESTAMP);
125 OUT_RELOC(query->bo,
126 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
127 PIPE_CONTROL_GLOBAL_GTT_WRITE |
128 0);
129 OUT_BATCH(0);
130 OUT_BATCH(0);
131 ADVANCE_BATCH();
132 }
133 } else {
134 /* Reset our driver's tracking of query state. */
135 drm_intel_bo_unreference(query->bo);
136 query->bo = NULL;
137 query->first_index = -1;
138 query->last_index = -1;
139
140 brw->query.obj = query;
141 intel->stats_wm++;
142 }
143 }
144
145 /**
146 * Begin the ARB_occlusion_query query on a query object.
147 */
148 static void
149 brw_end_query(struct gl_context *ctx, struct gl_query_object *q)
150 {
151 struct brw_context *brw = brw_context(ctx);
152 struct intel_context *intel = intel_context(ctx);
153 struct brw_query_object *query = (struct brw_query_object *)q;
154
155 if (query->Base.Target == GL_TIME_ELAPSED_EXT) {
156 if (intel->gen >= 6) {
157 BEGIN_BATCH(4);
158 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
159 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
160 OUT_RELOC(query->bo,
161 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
162 PIPE_CONTROL_GLOBAL_GTT_WRITE |
163 8);
164 OUT_BATCH(0);
165 ADVANCE_BATCH();
166
167 } else {
168 BEGIN_BATCH(4);
169 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
170 PIPE_CONTROL_WRITE_TIMESTAMP);
171 OUT_RELOC(query->bo,
172 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
173 PIPE_CONTROL_GLOBAL_GTT_WRITE |
174 8);
175 OUT_BATCH(0);
176 OUT_BATCH(0);
177 ADVANCE_BATCH();
178 }
179
180 intel_batchbuffer_flush(intel);
181 } else {
182 /* Flush the batchbuffer in case it has writes to our query BO.
183 * Have later queries write to a new query BO so that further rendering
184 * doesn't delay the collection of our results.
185 */
186 if (query->bo) {
187 brw_emit_query_end(brw);
188 intel_batchbuffer_flush(intel);
189
190 drm_intel_bo_unreference(brw->query.bo);
191 brw->query.bo = NULL;
192 }
193
194 brw->query.obj = NULL;
195
196 intel->stats_wm--;
197 }
198 }
199
200 static void brw_wait_query(struct gl_context *ctx, struct gl_query_object *q)
201 {
202 struct brw_query_object *query = (struct brw_query_object *)q;
203
204 brw_queryobj_get_results(query);
205 query->Base.Ready = GL_TRUE;
206 }
207
208 static void brw_check_query(struct gl_context *ctx, struct gl_query_object *q)
209 {
210 struct brw_query_object *query = (struct brw_query_object *)q;
211
212 if (query->bo == NULL || !drm_intel_bo_busy(query->bo)) {
213 brw_queryobj_get_results(query);
214 query->Base.Ready = GL_TRUE;
215 }
216 }
217
218 /** Called to set up the query BO and account for its aperture space */
219 void
220 brw_prepare_query_begin(struct brw_context *brw)
221 {
222 struct intel_context *intel = &brw->intel;
223
224 /* Skip if we're not doing any queries. */
225 if (!brw->query.obj)
226 return;
227
228 /* Get a new query BO if we're going to need it. */
229 if (brw->query.bo == NULL ||
230 brw->query.index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
231 drm_intel_bo_unreference(brw->query.bo);
232 brw->query.bo = NULL;
233
234 brw->query.bo = drm_intel_bo_alloc(intel->bufmgr, "query", 4096, 1);
235
236 /* clear target buffer */
237 drm_intel_bo_map(brw->query.bo, GL_TRUE);
238 memset((char *)brw->query.bo->virtual, 0, 4096);
239 drm_intel_bo_unmap(brw->query.bo);
240
241 brw->query.index = 0;
242 }
243
244 brw_add_validated_bo(brw, brw->query.bo);
245 }
246
247 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
248 void
249 brw_emit_query_begin(struct brw_context *brw)
250 {
251 struct intel_context *intel = &brw->intel;
252 struct brw_query_object *query = brw->query.obj;
253
254 /* Skip if we're not doing any queries, or we've emitted the start. */
255 if (!query || brw->query.active)
256 return;
257
258 if (intel->gen >= 6) {
259 BEGIN_BATCH(8);
260
261 /* workaround: CS stall required before depth stall. */
262 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
263 OUT_BATCH(PIPE_CONTROL_CS_STALL);
264 OUT_BATCH(0); /* write address */
265 OUT_BATCH(0); /* write data */
266
267 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
268 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
269 PIPE_CONTROL_WRITE_DEPTH_COUNT);
270 OUT_RELOC(brw->query.bo,
271 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
272 PIPE_CONTROL_GLOBAL_GTT_WRITE |
273 ((brw->query.index * 2) * sizeof(uint64_t)));
274 OUT_BATCH(0);
275 ADVANCE_BATCH();
276
277 } else {
278 BEGIN_BATCH(4);
279 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
280 PIPE_CONTROL_DEPTH_STALL |
281 PIPE_CONTROL_WRITE_DEPTH_COUNT);
282 /* This object could be mapped cacheable, but we don't have an exposed
283 * mechanism to support that. Since it's going uncached, tell GEM that
284 * we're writing to it. The usual clflush should be all that's required
285 * to pick up the results.
286 */
287 OUT_RELOC(brw->query.bo,
288 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
289 PIPE_CONTROL_GLOBAL_GTT_WRITE |
290 ((brw->query.index * 2) * sizeof(uint64_t)));
291 OUT_BATCH(0);
292 OUT_BATCH(0);
293 ADVANCE_BATCH();
294 }
295
296 if (query->bo != brw->query.bo) {
297 if (query->bo != NULL)
298 brw_queryobj_get_results(query);
299 drm_intel_bo_reference(brw->query.bo);
300 query->bo = brw->query.bo;
301 query->first_index = brw->query.index;
302 }
303 query->last_index = brw->query.index;
304 brw->query.active = GL_TRUE;
305 }
306
307 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
308 void
309 brw_emit_query_end(struct brw_context *brw)
310 {
311 struct intel_context *intel = &brw->intel;
312
313 if (!brw->query.active)
314 return;
315
316 if (intel->gen >= 6) {
317 BEGIN_BATCH(8);
318 /* workaround: CS stall required before depth stall. */
319 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
320 OUT_BATCH(PIPE_CONTROL_CS_STALL);
321 OUT_BATCH(0); /* write address */
322 OUT_BATCH(0); /* write data */
323
324 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
325 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
326 PIPE_CONTROL_WRITE_DEPTH_COUNT);
327 OUT_RELOC(brw->query.bo,
328 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
329 PIPE_CONTROL_GLOBAL_GTT_WRITE |
330 ((brw->query.index * 2 + 1) * sizeof(uint64_t)));
331 OUT_BATCH(0);
332 ADVANCE_BATCH();
333
334 } else {
335 BEGIN_BATCH(4);
336 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
337 PIPE_CONTROL_DEPTH_STALL |
338 PIPE_CONTROL_WRITE_DEPTH_COUNT);
339 OUT_RELOC(brw->query.bo,
340 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
341 PIPE_CONTROL_GLOBAL_GTT_WRITE |
342 ((brw->query.index * 2 + 1) * sizeof(uint64_t)));
343 OUT_BATCH(0);
344 OUT_BATCH(0);
345 ADVANCE_BATCH();
346 }
347
348 brw->query.active = GL_FALSE;
349 brw->query.index++;
350 }
351
352 void brw_init_queryobj_functions(struct dd_function_table *functions)
353 {
354 functions->NewQueryObject = brw_new_query_object;
355 functions->DeleteQuery = brw_delete_query;
356 functions->BeginQuery = brw_begin_query;
357 functions->EndQuery = brw_end_query;
358 functions->CheckQuery = brw_check_query;
359 functions->WaitQuery = brw_wait_query;
360 }