i965: Rework the extra flushes surrounding occlusion queries.
[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 static void
49 write_timestamp(struct intel_context *intel, drm_intel_bo *query_bo, int idx)
50 {
51 if (intel->gen >= 6) {
52 /* Emit workaround flushes: */
53 if (intel->gen == 6) {
54 /* The timestamp write below is a non-zero post-sync op, which on
55 * Gen6 necessitates a CS stall. CS stalls need stall at scoreboard
56 * set. See the comments for intel_emit_post_sync_nonzero_flush().
57 */
58 BEGIN_BATCH(4);
59 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
60 OUT_BATCH(PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD);
61 OUT_BATCH(0);
62 OUT_BATCH(0);
63 ADVANCE_BATCH();
64 }
65
66 BEGIN_BATCH(5);
67 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
68 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
69 OUT_RELOC(query_bo,
70 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
71 PIPE_CONTROL_GLOBAL_GTT_WRITE |
72 idx * sizeof(uint64_t));
73 OUT_BATCH(0);
74 OUT_BATCH(0);
75 ADVANCE_BATCH();
76 } else {
77 BEGIN_BATCH(4);
78 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2) |
79 PIPE_CONTROL_WRITE_TIMESTAMP);
80 OUT_RELOC(query_bo,
81 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
82 PIPE_CONTROL_GLOBAL_GTT_WRITE |
83 idx * sizeof(uint64_t));
84 OUT_BATCH(0);
85 OUT_BATCH(0);
86 ADVANCE_BATCH();
87 }
88 }
89
90 static void
91 write_depth_count(struct intel_context *intel, drm_intel_bo *query_bo, int idx)
92 {
93 if (intel->gen >= 6) {
94 /* Emit Sandybridge workaround flush: */
95 if (intel->gen == 6)
96 intel_emit_post_sync_nonzero_flush(intel);
97
98 BEGIN_BATCH(5);
99 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
100 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
101 PIPE_CONTROL_WRITE_DEPTH_COUNT);
102 OUT_RELOC(query_bo,
103 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
104 PIPE_CONTROL_GLOBAL_GTT_WRITE |
105 (idx * sizeof(uint64_t)));
106 OUT_BATCH(0);
107 OUT_BATCH(0);
108 ADVANCE_BATCH();
109 } else {
110 BEGIN_BATCH(4);
111 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2) |
112 PIPE_CONTROL_DEPTH_STALL |
113 PIPE_CONTROL_WRITE_DEPTH_COUNT);
114 /* This object could be mapped cacheable, but we don't have an exposed
115 * mechanism to support that. Since it's going uncached, tell GEM that
116 * we're writing to it. The usual clflush should be all that's required
117 * to pick up the results.
118 */
119 OUT_RELOC(query_bo,
120 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
121 PIPE_CONTROL_GLOBAL_GTT_WRITE |
122 (idx * sizeof(uint64_t)));
123 OUT_BATCH(0);
124 OUT_BATCH(0);
125 ADVANCE_BATCH();
126 }
127 }
128
129 /** Waits on the query object's BO and totals the results for this query */
130 static void
131 brw_queryobj_get_results(struct gl_context *ctx,
132 struct brw_query_object *query)
133 {
134 struct intel_context *intel = intel_context(ctx);
135
136 int i;
137 uint64_t *results;
138
139 if (query->bo == NULL)
140 return;
141
142 drm_intel_bo_map(query->bo, false);
143 results = query->bo->virtual;
144 switch (query->Base.Target) {
145 case GL_TIME_ELAPSED_EXT:
146 if (intel->gen >= 6)
147 query->Base.Result += 80 * (results[1] - results[0]);
148 else
149 query->Base.Result += 1000 * ((results[1] >> 32) - (results[0] >> 32));
150 break;
151
152 case GL_SAMPLES_PASSED_ARB:
153 /* Map and count the pixels from the current query BO */
154 for (i = query->first_index; i <= query->last_index; i++) {
155 query->Base.Result += results[i * 2 + 1] - results[i * 2];
156 }
157 break;
158
159 case GL_PRIMITIVES_GENERATED:
160 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
161 /* We don't actually query the hardware for this value, so query->bo
162 * should always be NULL and execution should never reach here.
163 */
164 assert(!"Unreachable");
165 break;
166
167 default:
168 assert(!"Unrecognized query target in brw_queryobj_get_results()");
169 break;
170 }
171 drm_intel_bo_unmap(query->bo);
172
173 drm_intel_bo_unreference(query->bo);
174 query->bo = NULL;
175 }
176
177 static struct gl_query_object *
178 brw_new_query_object(struct gl_context *ctx, GLuint id)
179 {
180 struct brw_query_object *query;
181
182 query = calloc(1, sizeof(struct brw_query_object));
183
184 query->Base.Id = id;
185 query->Base.Result = 0;
186 query->Base.Active = false;
187 query->Base.Ready = true;
188
189 return &query->Base;
190 }
191
192 static void
193 brw_delete_query(struct gl_context *ctx, struct gl_query_object *q)
194 {
195 struct brw_query_object *query = (struct brw_query_object *)q;
196
197 drm_intel_bo_unreference(query->bo);
198 free(query);
199 }
200
201 static void
202 brw_begin_query(struct gl_context *ctx, struct gl_query_object *q)
203 {
204 struct brw_context *brw = brw_context(ctx);
205 struct intel_context *intel = intel_context(ctx);
206 struct brw_query_object *query = (struct brw_query_object *)q;
207
208 switch (query->Base.Target) {
209 case GL_TIME_ELAPSED_EXT:
210 drm_intel_bo_unreference(query->bo);
211 query->bo = drm_intel_bo_alloc(intel->bufmgr, "timer query", 4096, 4096);
212 write_timestamp(intel, query->bo, 0);
213 break;
214
215 case GL_SAMPLES_PASSED_ARB:
216 /* Reset our driver's tracking of query state. */
217 drm_intel_bo_unreference(query->bo);
218 query->bo = NULL;
219 query->first_index = -1;
220 query->last_index = -1;
221
222 brw->query.obj = query;
223 intel->stats_wm++;
224 break;
225
226 case GL_PRIMITIVES_GENERATED:
227 /* We don't actually query the hardware for this value; we keep track of
228 * it a software counter. So just reset the counter.
229 */
230 brw->sol.primitives_generated = 0;
231 brw->sol.counting_primitives_generated = true;
232 break;
233
234 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
235 /* We don't actually query the hardware for this value; we keep track of
236 * it a software counter. So just reset the counter.
237 */
238 brw->sol.primitives_written = 0;
239 brw->sol.counting_primitives_written = true;
240 break;
241
242 default:
243 assert(!"Unrecognized query target in brw_begin_query()");
244 break;
245 }
246 }
247
248 /**
249 * Begin the ARB_occlusion_query query on a query object.
250 */
251 static void
252 brw_end_query(struct gl_context *ctx, struct gl_query_object *q)
253 {
254 struct brw_context *brw = brw_context(ctx);
255 struct intel_context *intel = intel_context(ctx);
256 struct brw_query_object *query = (struct brw_query_object *)q;
257
258 switch (query->Base.Target) {
259 case GL_TIME_ELAPSED_EXT:
260 write_timestamp(intel, query->bo, 1);
261 intel_batchbuffer_flush(intel);
262 break;
263
264 case GL_SAMPLES_PASSED_ARB:
265 /* Flush the batchbuffer in case it has writes to our query BO.
266 * Have later queries write to a new query BO so that further rendering
267 * doesn't delay the collection of our results.
268 */
269 if (query->bo) {
270 brw_emit_query_end(brw);
271 intel_batchbuffer_flush(intel);
272
273 drm_intel_bo_unreference(brw->query.bo);
274 brw->query.bo = NULL;
275 }
276
277 brw->query.obj = NULL;
278
279 intel->stats_wm--;
280 break;
281
282 case GL_PRIMITIVES_GENERATED:
283 /* We don't actually query the hardware for this value; we keep track of
284 * it in a software counter. So just read the counter and store it in
285 * the query object.
286 */
287 query->Base.Result = brw->sol.primitives_generated;
288 brw->sol.counting_primitives_generated = false;
289
290 /* And set brw->query.obj to NULL so that this query won't try to wait
291 * for any rendering to complete.
292 */
293 query->bo = NULL;
294 break;
295
296 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
297 /* We don't actually query the hardware for this value; we keep track of
298 * it in a software counter. So just read the counter and store it in
299 * the query object.
300 */
301 query->Base.Result = brw->sol.primitives_written;
302 brw->sol.counting_primitives_written = false;
303
304 /* And set brw->query.obj to NULL so that this query won't try to wait
305 * for any rendering to complete.
306 */
307 query->bo = NULL;
308 break;
309
310 default:
311 assert(!"Unrecognized query target in brw_end_query()");
312 break;
313 }
314 }
315
316 static void brw_wait_query(struct gl_context *ctx, struct gl_query_object *q)
317 {
318 struct brw_query_object *query = (struct brw_query_object *)q;
319
320 brw_queryobj_get_results(ctx, query);
321 query->Base.Ready = true;
322 }
323
324 static void brw_check_query(struct gl_context *ctx, struct gl_query_object *q)
325 {
326 struct brw_query_object *query = (struct brw_query_object *)q;
327
328 if (query->bo == NULL || !drm_intel_bo_busy(query->bo)) {
329 brw_queryobj_get_results(ctx, query);
330 query->Base.Ready = true;
331 }
332 }
333
334 /** Called to set up the query BO and account for its aperture space */
335 void
336 brw_prepare_query_begin(struct brw_context *brw)
337 {
338 struct intel_context *intel = &brw->intel;
339
340 /* Skip if we're not doing any queries. */
341 if (!brw->query.obj)
342 return;
343
344 /* Get a new query BO if we're going to need it. */
345 if (brw->query.bo == NULL ||
346 brw->query.index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
347 drm_intel_bo_unreference(brw->query.bo);
348 brw->query.bo = NULL;
349
350 brw->query.bo = drm_intel_bo_alloc(intel->bufmgr, "query", 4096, 1);
351
352 /* clear target buffer */
353 drm_intel_bo_map(brw->query.bo, true);
354 memset((char *)brw->query.bo->virtual, 0, 4096);
355 drm_intel_bo_unmap(brw->query.bo);
356
357 brw->query.index = 0;
358 }
359 }
360
361 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
362 void
363 brw_emit_query_begin(struct brw_context *brw)
364 {
365 struct intel_context *intel = &brw->intel;
366 struct gl_context *ctx = &intel->ctx;
367 struct brw_query_object *query = brw->query.obj;
368
369 /* Skip if we're not doing any queries, or we've emitted the start. */
370 if (!query || brw->query.active)
371 return;
372
373 write_depth_count(intel, brw->query.bo, brw->query.index * 2);
374
375 if (query->bo != brw->query.bo) {
376 if (query->bo != NULL)
377 brw_queryobj_get_results(ctx, query);
378 drm_intel_bo_reference(brw->query.bo);
379 query->bo = brw->query.bo;
380 query->first_index = brw->query.index;
381 }
382 query->last_index = brw->query.index;
383 brw->query.active = true;
384 }
385
386 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
387 void
388 brw_emit_query_end(struct brw_context *brw)
389 {
390 struct intel_context *intel = &brw->intel;
391
392 if (!brw->query.active)
393 return;
394
395 write_depth_count(intel, brw->query.bo, brw->query.index * 2 + 1);
396
397 brw->query.active = false;
398 brw->query.index++;
399 }
400
401 void brw_init_queryobj_functions(struct dd_function_table *functions)
402 {
403 functions->NewQueryObject = brw_new_query_object;
404 functions->DeleteQuery = brw_delete_query;
405 functions->BeginQuery = brw_begin_query;
406 functions->EndQuery = brw_end_query;
407 functions->CheckQuery = brw_check_query;
408 functions->WaitQuery = brw_wait_query;
409 }