i965: Write TIMESTAMP query values into the first buffer element.
[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 brw_queryobj.c
29 *
30 * Support for query objects (GL_ARB_occlusion_query, GL_ARB_timer_query,
31 * GL_EXT_transform_feedback, and friends).
32 *
33 * The hardware provides a PIPE_CONTROL command that can report the number of
34 * fragments that passed the depth test, or the hardware timer. They are
35 * appropriately synced with the stage of the pipeline for our extensions'
36 * needs.
37 *
38 * To avoid getting samples from another context's rendering in our results,
39 * we capture the counts at the start and end of every batchbuffer while the
40 * query is active, and sum up the differences. (We should do so for
41 * GL_TIME_ELAPSED as well, but don't).
42 */
43 #include "main/imports.h"
44
45 #include "brw_context.h"
46 #include "brw_defines.h"
47 #include "brw_state.h"
48 #include "intel_batchbuffer.h"
49 #include "intel_reg.h"
50
51 static void
52 write_timestamp(struct intel_context *intel, drm_intel_bo *query_bo, int idx)
53 {
54 if (intel->gen >= 6) {
55 /* Emit workaround flushes: */
56 if (intel->gen == 6) {
57 /* The timestamp write below is a non-zero post-sync op, which on
58 * Gen6 necessitates a CS stall. CS stalls need stall at scoreboard
59 * set. See the comments for intel_emit_post_sync_nonzero_flush().
60 */
61 BEGIN_BATCH(4);
62 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
63 OUT_BATCH(PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD);
64 OUT_BATCH(0);
65 OUT_BATCH(0);
66 ADVANCE_BATCH();
67 }
68
69 BEGIN_BATCH(5);
70 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
71 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
72 OUT_RELOC(query_bo,
73 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
74 PIPE_CONTROL_GLOBAL_GTT_WRITE |
75 idx * sizeof(uint64_t));
76 OUT_BATCH(0);
77 OUT_BATCH(0);
78 ADVANCE_BATCH();
79 } else {
80 BEGIN_BATCH(4);
81 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2) |
82 PIPE_CONTROL_WRITE_TIMESTAMP);
83 OUT_RELOC(query_bo,
84 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
85 PIPE_CONTROL_GLOBAL_GTT_WRITE |
86 idx * sizeof(uint64_t));
87 OUT_BATCH(0);
88 OUT_BATCH(0);
89 ADVANCE_BATCH();
90 }
91 }
92
93 static void
94 write_depth_count(struct intel_context *intel, drm_intel_bo *query_bo, int idx)
95 {
96 if (intel->gen >= 6) {
97 /* Emit Sandybridge workaround flush: */
98 if (intel->gen == 6)
99 intel_emit_post_sync_nonzero_flush(intel);
100
101 BEGIN_BATCH(5);
102 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
103 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
104 PIPE_CONTROL_WRITE_DEPTH_COUNT);
105 OUT_RELOC(query_bo,
106 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
107 PIPE_CONTROL_GLOBAL_GTT_WRITE |
108 (idx * sizeof(uint64_t)));
109 OUT_BATCH(0);
110 OUT_BATCH(0);
111 ADVANCE_BATCH();
112 } else {
113 BEGIN_BATCH(4);
114 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2) |
115 PIPE_CONTROL_DEPTH_STALL |
116 PIPE_CONTROL_WRITE_DEPTH_COUNT);
117 /* This object could be mapped cacheable, but we don't have an exposed
118 * mechanism to support that. Since it's going uncached, tell GEM that
119 * we're writing to it. The usual clflush should be all that's required
120 * to pick up the results.
121 */
122 OUT_RELOC(query_bo,
123 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
124 PIPE_CONTROL_GLOBAL_GTT_WRITE |
125 (idx * sizeof(uint64_t)));
126 OUT_BATCH(0);
127 OUT_BATCH(0);
128 ADVANCE_BATCH();
129 }
130 }
131
132 /** Waits on the query object's BO and totals the results for this query */
133 static void
134 brw_queryobj_get_results(struct gl_context *ctx,
135 struct brw_query_object *query)
136 {
137 struct intel_context *intel = intel_context(ctx);
138
139 int i;
140 uint64_t *results;
141
142 if (query->bo == NULL)
143 return;
144
145 if (drm_intel_bo_references(intel->batch.bo, query->bo))
146 intel_batchbuffer_flush(intel);
147
148 if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
149 if (drm_intel_bo_busy(query->bo)) {
150 perf_debug("Stalling on the GPU waiting for a query object.\n");
151 }
152 }
153
154 drm_intel_bo_map(query->bo, false);
155 results = query->bo->virtual;
156 switch (query->Base.Target) {
157 case GL_TIME_ELAPSED_EXT:
158 if (intel->gen >= 6)
159 query->Base.Result += 80 * (results[1] - results[0]);
160 else
161 query->Base.Result += 1000 * ((results[1] >> 32) - (results[0] >> 32));
162 break;
163
164 case GL_TIMESTAMP:
165 if (intel->gen >= 6) {
166 /* Our timer is a clock that increments every 80ns (regardless of
167 * other clock scaling in the system). The timestamp register we can
168 * read for glGetTimestamp() masks out the top 32 bits, so we do that
169 * here too to let the two counters be compared against each other.
170 *
171 * If we just multiplied that 32 bits of data by 80, it would roll
172 * over at a non-power-of-two, so an application couldn't use
173 * GL_QUERY_COUNTER_BITS to handle rollover correctly. Instead, we
174 * report 36 bits and truncate at that (rolling over 5 times as often
175 * as the HW counter), and when the 32-bit counter rolls over, it
176 * happens to also be at a rollover in the reported value from near
177 * (1<<36) to 0.
178 *
179 * The low 32 bits rolls over in ~343 seconds. Our 36-bit result
180 * rolls over every ~69 seconds.
181 */
182 query->Base.Result = 80 * (results[0] & 0xffffffff);
183 query->Base.Result &= (1ull << 36) - 1;
184 } else {
185 query->Base.Result = 1000 * (results[0] >> 32);
186 }
187 break;
188
189 case GL_SAMPLES_PASSED_ARB:
190 /* Map and count the pixels from the current query BO */
191 for (i = query->first_index; i <= query->last_index; i++) {
192 query->Base.Result += results[i * 2 + 1] - results[i * 2];
193 }
194 break;
195
196 case GL_ANY_SAMPLES_PASSED:
197 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
198 /* Set true if any of the sub-queries passed. */
199 for (i = query->first_index; i <= query->last_index; i++) {
200 if (results[i * 2 + 1] != results[i * 2]) {
201 query->Base.Result = GL_TRUE;
202 break;
203 }
204 }
205 break;
206
207 case GL_PRIMITIVES_GENERATED:
208 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
209 /* We don't actually query the hardware for this value, so query->bo
210 * should always be NULL and execution should never reach here.
211 */
212 assert(!"Unreachable");
213 break;
214
215 default:
216 assert(!"Unrecognized query target in brw_queryobj_get_results()");
217 break;
218 }
219 drm_intel_bo_unmap(query->bo);
220
221 drm_intel_bo_unreference(query->bo);
222 query->bo = NULL;
223 }
224
225 static struct gl_query_object *
226 brw_new_query_object(struct gl_context *ctx, GLuint id)
227 {
228 struct brw_query_object *query;
229
230 query = calloc(1, sizeof(struct brw_query_object));
231
232 query->Base.Id = id;
233 query->Base.Result = 0;
234 query->Base.Active = false;
235 query->Base.Ready = true;
236
237 return &query->Base;
238 }
239
240 static void
241 brw_delete_query(struct gl_context *ctx, struct gl_query_object *q)
242 {
243 struct brw_query_object *query = (struct brw_query_object *)q;
244
245 drm_intel_bo_unreference(query->bo);
246 free(query);
247 }
248
249 static void
250 brw_begin_query(struct gl_context *ctx, struct gl_query_object *q)
251 {
252 struct brw_context *brw = brw_context(ctx);
253 struct intel_context *intel = intel_context(ctx);
254 struct brw_query_object *query = (struct brw_query_object *)q;
255
256 switch (query->Base.Target) {
257 case GL_TIME_ELAPSED_EXT:
258 drm_intel_bo_unreference(query->bo);
259 query->bo = drm_intel_bo_alloc(intel->bufmgr, "timer query", 4096, 4096);
260 write_timestamp(intel, query->bo, 0);
261 break;
262
263 case GL_ANY_SAMPLES_PASSED:
264 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
265 case GL_SAMPLES_PASSED_ARB:
266 /* Reset our driver's tracking of query state. */
267 drm_intel_bo_unreference(query->bo);
268 query->bo = NULL;
269 query->first_index = -1;
270 query->last_index = -1;
271
272 brw->query.obj = query;
273 intel->stats_wm++;
274 break;
275
276 case GL_PRIMITIVES_GENERATED:
277 /* We don't actually query the hardware for this value; we keep track of
278 * it a software counter. So just reset the counter.
279 */
280 brw->sol.primitives_generated = 0;
281 brw->sol.counting_primitives_generated = true;
282 break;
283
284 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
285 /* We don't actually query the hardware for this value; we keep track of
286 * it a software counter. So just reset the counter.
287 */
288 brw->sol.primitives_written = 0;
289 brw->sol.counting_primitives_written = true;
290 break;
291
292 default:
293 assert(!"Unrecognized query target in brw_begin_query()");
294 break;
295 }
296 }
297
298 /**
299 * Begin the ARB_occlusion_query query on a query object.
300 */
301 static void
302 brw_end_query(struct gl_context *ctx, struct gl_query_object *q)
303 {
304 struct brw_context *brw = brw_context(ctx);
305 struct intel_context *intel = intel_context(ctx);
306 struct brw_query_object *query = (struct brw_query_object *)q;
307
308 switch (query->Base.Target) {
309 case GL_TIME_ELAPSED_EXT:
310 write_timestamp(intel, query->bo, 1);
311 break;
312
313 case GL_ANY_SAMPLES_PASSED:
314 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
315 case GL_SAMPLES_PASSED_ARB:
316
317 /* No query->bo means that EndQuery was called after BeginQuery with no
318 * intervening drawing. Rather than doing nothing at all here in this
319 * case, we emit the query_begin and query_end state to the
320 * hardware. This is to guarantee that waiting on the result of this
321 * empty state will cause all previous queries to complete at all, as
322 * required by the specification:
323 *
324 * It must always be true that if any query object
325 * returns a result available of TRUE, all queries of the
326 * same type issued prior to that query must also return
327 * TRUE. [Open GL 4.3 (Core Profile) Section 4.2.1]
328 */
329 if (!query->bo) {
330 brw_emit_query_begin(brw);
331 }
332
333 if (query->bo) {
334 brw_emit_query_end(brw);
335
336 drm_intel_bo_unreference(brw->query.bo);
337 brw->query.bo = NULL;
338 }
339
340 brw->query.obj = NULL;
341
342 intel->stats_wm--;
343 break;
344
345 case GL_PRIMITIVES_GENERATED:
346 /* We don't actually query the hardware for this value; we keep track of
347 * it in a software counter. So just read the counter and store it in
348 * the query object.
349 */
350 query->Base.Result = brw->sol.primitives_generated;
351 brw->sol.counting_primitives_generated = false;
352
353 /* And set brw->query.obj to NULL so that this query won't try to wait
354 * for any rendering to complete.
355 */
356 query->bo = NULL;
357 break;
358
359 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
360 /* We don't actually query the hardware for this value; we keep track of
361 * it in a software counter. So just read the counter and store it in
362 * the query object.
363 */
364 query->Base.Result = brw->sol.primitives_written;
365 brw->sol.counting_primitives_written = false;
366
367 /* And set brw->query.obj to NULL so that this query won't try to wait
368 * for any rendering to complete.
369 */
370 query->bo = NULL;
371 break;
372
373 default:
374 assert(!"Unrecognized query target in brw_end_query()");
375 break;
376 }
377 }
378
379 static void brw_wait_query(struct gl_context *ctx, struct gl_query_object *q)
380 {
381 struct brw_query_object *query = (struct brw_query_object *)q;
382
383 brw_queryobj_get_results(ctx, query);
384 query->Base.Ready = true;
385 }
386
387 static void brw_check_query(struct gl_context *ctx, struct gl_query_object *q)
388 {
389 struct intel_context *intel = intel_context(ctx);
390 struct brw_query_object *query = (struct brw_query_object *)q;
391
392 /* From the GL_ARB_occlusion_query spec:
393 *
394 * "Instead of allowing for an infinite loop, performing a
395 * QUERY_RESULT_AVAILABLE_ARB will perform a flush if the result is
396 * not ready yet on the first time it is queried. This ensures that
397 * the async query will return true in finite time.
398 */
399 if (query->bo && drm_intel_bo_references(intel->batch.bo, query->bo))
400 intel_batchbuffer_flush(intel);
401
402 if (query->bo == NULL || !drm_intel_bo_busy(query->bo)) {
403 brw_queryobj_get_results(ctx, query);
404 query->Base.Ready = true;
405 }
406 }
407
408 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
409 void
410 brw_emit_query_begin(struct brw_context *brw)
411 {
412 struct intel_context *intel = &brw->intel;
413 struct gl_context *ctx = &intel->ctx;
414 struct brw_query_object *query = brw->query.obj;
415
416 /* Skip if we're not doing any queries, or we've emitted the start. */
417 if (!query || brw->query.begin_emitted)
418 return;
419
420 /* Get a new query BO if we're going to need it. */
421 if (brw->query.bo == NULL ||
422 brw->query.index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
423 drm_intel_bo_unreference(brw->query.bo);
424 brw->query.bo = NULL;
425
426 brw->query.bo = drm_intel_bo_alloc(intel->bufmgr, "query", 4096, 1);
427
428 /* clear target buffer */
429 drm_intel_bo_map(brw->query.bo, true);
430 memset((char *)brw->query.bo->virtual, 0, 4096);
431 drm_intel_bo_unmap(brw->query.bo);
432
433 brw->query.index = 0;
434 }
435
436 write_depth_count(intel, brw->query.bo, brw->query.index * 2);
437
438 if (query->bo != brw->query.bo) {
439 if (query->bo != NULL)
440 brw_queryobj_get_results(ctx, query);
441 drm_intel_bo_reference(brw->query.bo);
442 query->bo = brw->query.bo;
443 query->first_index = brw->query.index;
444 }
445 query->last_index = brw->query.index;
446 brw->query.begin_emitted = true;
447 }
448
449 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
450 void
451 brw_emit_query_end(struct brw_context *brw)
452 {
453 struct intel_context *intel = &brw->intel;
454
455 if (!brw->query.begin_emitted)
456 return;
457
458 write_depth_count(intel, brw->query.bo, brw->query.index * 2 + 1);
459
460 brw->query.begin_emitted = false;
461 brw->query.index++;
462 }
463
464 /**
465 * Driver hook for glQueryCounter().
466 *
467 * This handles GL_TIMESTAMP queries, which perform a pipelined read of the
468 * current GPU time. This is unlike GL_TIME_ELAPSED, which measures the
469 * time while the query is active.
470 */
471 static void
472 brw_query_counter(struct gl_context *ctx, struct gl_query_object *q)
473 {
474 struct intel_context *intel = intel_context(ctx);
475 struct brw_query_object *query = (struct brw_query_object *) q;
476
477 assert(q->Target == GL_TIMESTAMP);
478
479 drm_intel_bo_unreference(query->bo);
480 query->bo = drm_intel_bo_alloc(intel->bufmgr, "timestamp query", 4096, 4096);
481 write_timestamp(intel, query->bo, 0);
482 }
483
484 static uint64_t
485 brw_get_timestamp(struct gl_context *ctx)
486 {
487 struct intel_context *intel = intel_context(ctx);
488 uint64_t result = 0;
489
490 drm_intel_reg_read(intel->bufmgr, TIMESTAMP, &result);
491
492 /* See logic in brw_queryobj_get_results() */
493 result = result >> 32;
494 result *= 80;
495 result &= (1ull << 36) - 1;
496
497 return result;
498 }
499
500 void brw_init_queryobj_functions(struct dd_function_table *functions)
501 {
502 functions->NewQueryObject = brw_new_query_object;
503 functions->DeleteQuery = brw_delete_query;
504 functions->BeginQuery = brw_begin_query;
505 functions->EndQuery = brw_end_query;
506 functions->QueryCounter = brw_query_counter;
507 functions->CheckQuery = brw_check_query;
508 functions->WaitQuery = brw_wait_query;
509 functions->GetTimestamp = brw_get_timestamp;
510 }