i965: Drop brw_bo_map[_gtt] wrappers which issue perf warnings.
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_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 * Kenneth Graunke <kenneth@whitecape.org>
26 */
27
28 /** @file gen6_queryobj.c
29 *
30 * Support for query objects (GL_ARB_occlusion_query, GL_ARB_timer_query,
31 * GL_EXT_transform_feedback, and friends) on platforms that support
32 * hardware contexts (Gen6+).
33 */
34 #include "main/imports.h"
35
36 #include "brw_context.h"
37 #include "brw_defines.h"
38 #include "brw_state.h"
39 #include "intel_batchbuffer.h"
40 #include "intel_buffer_objects.h"
41
42 static inline void
43 set_query_availability(struct brw_context *brw, struct brw_query_object *query,
44 bool available)
45 {
46 /* For platforms that support ARB_query_buffer_object, we write the
47 * query availability for "pipelined" queries.
48 *
49 * Most counter snapshots are written by the command streamer, by
50 * doing a CS stall and then MI_STORE_REGISTER_MEM. For these
51 * counters, the CS stall guarantees that the results will be
52 * available when subsequent CS commands run. So we don't need to
53 * do any additional tracking.
54 *
55 * Other counters (occlusion queries and timestamp) are written by
56 * PIPE_CONTROL, without a CS stall. This means that we can't be
57 * sure whether the writes have landed yet or not. Performing a
58 * PIPE_CONTROL with an immediate write will synchronize with
59 * those earlier writes, so we write 1 when the value has landed.
60 */
61 if (brw->ctx.Extensions.ARB_query_buffer_object &&
62 brw_is_query_pipelined(query)) {
63 brw_emit_pipe_control_write(brw,
64 PIPE_CONTROL_WRITE_IMMEDIATE,
65 query->bo, 2 * sizeof(uint64_t),
66 available, 0);
67 }
68 }
69
70 static void
71 write_primitives_generated(struct brw_context *brw,
72 drm_bacon_bo *query_bo, int stream, int idx)
73 {
74 brw_emit_mi_flush(brw);
75
76 if (brw->gen >= 7 && stream > 0) {
77 brw_store_register_mem64(brw, query_bo,
78 GEN7_SO_PRIM_STORAGE_NEEDED(stream),
79 idx * sizeof(uint64_t));
80 } else {
81 brw_store_register_mem64(brw, query_bo, CL_INVOCATION_COUNT,
82 idx * sizeof(uint64_t));
83 }
84 }
85
86 static void
87 write_xfb_primitives_written(struct brw_context *brw,
88 drm_bacon_bo *bo, int stream, int idx)
89 {
90 brw_emit_mi_flush(brw);
91
92 if (brw->gen >= 7) {
93 brw_store_register_mem64(brw, bo, GEN7_SO_NUM_PRIMS_WRITTEN(stream),
94 idx * sizeof(uint64_t));
95 } else {
96 brw_store_register_mem64(brw, bo, GEN6_SO_NUM_PRIMS_WRITTEN,
97 idx * sizeof(uint64_t));
98 }
99 }
100
101 static void
102 write_xfb_overflow_streams(struct gl_context *ctx,
103 drm_bacon_bo *bo, int stream, int count,
104 int idx)
105 {
106 struct brw_context *brw = brw_context(ctx);
107
108 brw_emit_mi_flush(brw);
109
110 for (int i = 0; i < count; i++) {
111 int w_idx = 4 * i + idx;
112 int g_idx = 4 * i + idx + 2;
113
114 if (brw->gen >= 7) {
115 brw_store_register_mem64(brw, bo,
116 GEN7_SO_NUM_PRIMS_WRITTEN(stream + i),
117 g_idx * sizeof(uint64_t));
118 brw_store_register_mem64(brw, bo,
119 GEN7_SO_PRIM_STORAGE_NEEDED(stream + i),
120 w_idx * sizeof(uint64_t));
121 } else {
122 brw_store_register_mem64(brw, bo,
123 GEN6_SO_NUM_PRIMS_WRITTEN,
124 g_idx * sizeof(uint64_t));
125 brw_store_register_mem64(brw, bo,
126 GEN6_SO_PRIM_STORAGE_NEEDED,
127 w_idx * sizeof(uint64_t));
128 }
129 }
130 }
131
132 static bool
133 check_xfb_overflow_streams(uint64_t *results, int count)
134 {
135 bool overflow = false;
136
137 for (int i = 0; i < count; i++) {
138 uint64_t *result_i = &results[4 * i];
139
140 if ((result_i[3] - result_i[2]) != (result_i[1] - result_i[0])) {
141 overflow = true;
142 break;
143 }
144 }
145
146 return overflow;
147 }
148
149 static inline int
150 pipeline_target_to_index(int target)
151 {
152 if (target == GL_GEOMETRY_SHADER_INVOCATIONS)
153 return MAX_PIPELINE_STATISTICS - 1;
154 else
155 return target - GL_VERTICES_SUBMITTED_ARB;
156 }
157
158 static void
159 emit_pipeline_stat(struct brw_context *brw, drm_bacon_bo *bo,
160 int stream, int target, int idx)
161 {
162 /* One source of confusion is the tessellation shader statistics. The
163 * hardware has no statistics specific to the TE unit. Ideally we could have
164 * the HS primitives for TESS_CONTROL_SHADER_PATCHES_ARB, and the DS
165 * invocations as the register for TESS_CONTROL_SHADER_PATCHES_ARB.
166 * Unfortunately we don't have HS primitives, we only have HS invocations.
167 */
168
169 /* Everything except GEOMETRY_SHADER_INVOCATIONS can be kept in a simple
170 * lookup table
171 */
172 static const uint32_t target_to_register[] = {
173 IA_VERTICES_COUNT, /* VERTICES_SUBMITTED */
174 IA_PRIMITIVES_COUNT, /* PRIMITIVES_SUBMITTED */
175 VS_INVOCATION_COUNT, /* VERTEX_SHADER_INVOCATIONS */
176 HS_INVOCATION_COUNT, /* TESS_CONTROL_SHADER_PATCHES */
177 DS_INVOCATION_COUNT, /* TESS_EVALUATION_SHADER_INVOCATIONS */
178 GS_PRIMITIVES_COUNT, /* GEOMETRY_SHADER_PRIMITIVES_EMITTED */
179 PS_INVOCATION_COUNT, /* FRAGMENT_SHADER_INVOCATIONS */
180 CS_INVOCATION_COUNT, /* COMPUTE_SHADER_INVOCATIONS */
181 CL_INVOCATION_COUNT, /* CLIPPING_INPUT_PRIMITIVES */
182 CL_PRIMITIVES_COUNT, /* CLIPPING_OUTPUT_PRIMITIVES */
183 GS_INVOCATION_COUNT /* This one is special... */
184 };
185 STATIC_ASSERT(ARRAY_SIZE(target_to_register) == MAX_PIPELINE_STATISTICS);
186 uint32_t reg = target_to_register[pipeline_target_to_index(target)];
187 /* Gen6 GS code counts full primitives, that is, it won't count individual
188 * triangles in a triangle strip. Use CL_INVOCATION_COUNT for that.
189 */
190 if (brw->gen == 6 && target == GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB)
191 reg = CL_INVOCATION_COUNT;
192 assert(reg != 0);
193
194 /* Emit a flush to make sure various parts of the pipeline are complete and
195 * we get an accurate value
196 */
197 brw_emit_mi_flush(brw);
198
199 brw_store_register_mem64(brw, bo, reg, idx * sizeof(uint64_t));
200 }
201
202
203 /**
204 * Wait on the query object's BO and calculate the final result.
205 */
206 static void
207 gen6_queryobj_get_results(struct gl_context *ctx,
208 struct brw_query_object *query)
209 {
210 struct brw_context *brw = brw_context(ctx);
211
212 if (query->bo == NULL)
213 return;
214
215 drm_bacon_bo_map(query->bo, false);
216 uint64_t *results = query->bo->virtual;
217 switch (query->Base.Target) {
218 case GL_TIME_ELAPSED:
219 /* The query BO contains the starting and ending timestamps.
220 * Subtract the two and convert to nanoseconds.
221 */
222 query->Base.Result = brw_raw_timestamp_delta(brw, results[0], results[1]);
223 query->Base.Result = brw_timebase_scale(brw, query->Base.Result);
224 break;
225
226 case GL_TIMESTAMP:
227 /* The query BO contains a single timestamp value in results[0]. */
228 query->Base.Result = brw_timebase_scale(brw, results[0]);
229
230 /* Ensure the scaled timestamp overflows according to
231 * GL_QUERY_COUNTER_BITS
232 */
233 query->Base.Result &= (1ull << ctx->Const.QueryCounterBits.Timestamp) - 1;
234 break;
235
236 case GL_SAMPLES_PASSED_ARB:
237 /* We need to use += rather than = here since some BLT-based operations
238 * may have added additional samples to our occlusion query value.
239 */
240 query->Base.Result += results[1] - results[0];
241 break;
242
243 case GL_ANY_SAMPLES_PASSED:
244 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
245 if (results[0] != results[1])
246 query->Base.Result = true;
247 break;
248
249 case GL_PRIMITIVES_GENERATED:
250 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
251 case GL_VERTICES_SUBMITTED_ARB:
252 case GL_PRIMITIVES_SUBMITTED_ARB:
253 case GL_VERTEX_SHADER_INVOCATIONS_ARB:
254 case GL_GEOMETRY_SHADER_INVOCATIONS:
255 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
256 case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
257 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
258 case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
259 case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
260 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
261 query->Base.Result = results[1] - results[0];
262 break;
263
264 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB:
265 query->Base.Result = check_xfb_overflow_streams(results, 1);
266 break;
267
268 case GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB:
269 query->Base.Result = check_xfb_overflow_streams(results, MAX_VERTEX_STREAMS);
270 break;
271
272 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
273 query->Base.Result = (results[1] - results[0]);
274 /* Implement the "WaDividePSInvocationCountBy4:HSW,BDW" workaround:
275 * "Invocation counter is 4 times actual. WA: SW to divide HW reported
276 * PS Invocations value by 4."
277 *
278 * Prior to Haswell, invocation count was counted by the WM, and it
279 * buggily counted invocations in units of subspans (2x2 unit). To get the
280 * correct value, the CS multiplied this by 4. With HSW the logic moved,
281 * and correctly emitted the number of pixel shader invocations, but,
282 * whomever forgot to undo the multiply by 4.
283 */
284 if (brw->gen == 8 || brw->is_haswell)
285 query->Base.Result /= 4;
286 break;
287
288 default:
289 unreachable("Unrecognized query target in brw_queryobj_get_results()");
290 }
291 drm_bacon_bo_unmap(query->bo);
292
293 /* Now that we've processed the data stored in the query's buffer object,
294 * we can release it.
295 */
296 drm_bacon_bo_unreference(query->bo);
297 query->bo = NULL;
298
299 query->Base.Ready = true;
300 }
301
302 /**
303 * Driver hook for glBeginQuery().
304 *
305 * Initializes driver structures and emits any GPU commands required to begin
306 * recording data for the query.
307 */
308 static void
309 gen6_begin_query(struct gl_context *ctx, struct gl_query_object *q)
310 {
311 struct brw_context *brw = brw_context(ctx);
312 struct brw_query_object *query = (struct brw_query_object *)q;
313
314 /* Since we're starting a new query, we need to throw away old results. */
315 drm_bacon_bo_unreference(query->bo);
316 query->bo = drm_bacon_bo_alloc(brw->bufmgr, "query results", 4096, 4096);
317
318 /* For ARB_query_buffer_object: The result is not available */
319 set_query_availability(brw, query, false);
320
321 switch (query->Base.Target) {
322 case GL_TIME_ELAPSED:
323 /* For timestamp queries, we record the starting time right away so that
324 * we measure the full time between BeginQuery and EndQuery. There's
325 * some debate about whether this is the right thing to do. Our decision
326 * is based on the following text from the ARB_timer_query extension:
327 *
328 * "(5) Should the extension measure total time elapsed between the full
329 * completion of the BeginQuery and EndQuery commands, or just time
330 * spent in the graphics library?
331 *
332 * RESOLVED: This extension will measure the total time elapsed
333 * between the full completion of these commands. Future extensions
334 * may implement a query to determine time elapsed at different stages
335 * of the graphics pipeline."
336 *
337 * We write a starting timestamp now (at index 0). At EndQuery() time,
338 * we'll write a second timestamp (at index 1), and subtract the two to
339 * obtain the time elapsed. Notably, this includes time elapsed while
340 * the system was doing other work, such as running other applications.
341 */
342 brw_write_timestamp(brw, query->bo, 0);
343 break;
344
345 case GL_ANY_SAMPLES_PASSED:
346 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
347 case GL_SAMPLES_PASSED_ARB:
348 brw_write_depth_count(brw, query->bo, 0);
349 break;
350
351 case GL_PRIMITIVES_GENERATED:
352 write_primitives_generated(brw, query->bo, query->Base.Stream, 0);
353 if (query->Base.Stream == 0)
354 ctx->NewDriverState |= BRW_NEW_RASTERIZER_DISCARD;
355 break;
356
357 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
358 write_xfb_primitives_written(brw, query->bo, query->Base.Stream, 0);
359 break;
360
361 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB:
362 write_xfb_overflow_streams(ctx, query->bo, query->Base.Stream, 1, 0);
363 break;
364
365 case GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB:
366 write_xfb_overflow_streams(ctx, query->bo, 0, MAX_VERTEX_STREAMS, 0);
367 break;
368
369 case GL_VERTICES_SUBMITTED_ARB:
370 case GL_PRIMITIVES_SUBMITTED_ARB:
371 case GL_VERTEX_SHADER_INVOCATIONS_ARB:
372 case GL_GEOMETRY_SHADER_INVOCATIONS:
373 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
374 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
375 case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
376 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
377 case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
378 case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
379 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
380 emit_pipeline_stat(brw, query->bo, query->Base.Stream, query->Base.Target, 0);
381 break;
382
383 default:
384 unreachable("Unrecognized query target in brw_begin_query()");
385 }
386 }
387
388 /**
389 * Driver hook for glEndQuery().
390 *
391 * Emits GPU commands to record a final query value, ending any data capturing.
392 * However, the final result isn't necessarily available until the GPU processes
393 * those commands. brw_queryobj_get_results() processes the captured data to
394 * produce the final result.
395 */
396 static void
397 gen6_end_query(struct gl_context *ctx, struct gl_query_object *q)
398 {
399 struct brw_context *brw = brw_context(ctx);
400 struct brw_query_object *query = (struct brw_query_object *)q;
401
402 switch (query->Base.Target) {
403 case GL_TIME_ELAPSED:
404 brw_write_timestamp(brw, query->bo, 1);
405 break;
406
407 case GL_ANY_SAMPLES_PASSED:
408 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
409 case GL_SAMPLES_PASSED_ARB:
410 brw_write_depth_count(brw, query->bo, 1);
411 break;
412
413 case GL_PRIMITIVES_GENERATED:
414 write_primitives_generated(brw, query->bo, query->Base.Stream, 1);
415 if (query->Base.Stream == 0)
416 ctx->NewDriverState |= BRW_NEW_RASTERIZER_DISCARD;
417 break;
418
419 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
420 write_xfb_primitives_written(brw, query->bo, query->Base.Stream, 1);
421 break;
422
423 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB:
424 write_xfb_overflow_streams(ctx, query->bo, query->Base.Stream, 1, 1);
425 break;
426
427 case GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB:
428 write_xfb_overflow_streams(ctx, query->bo, 0, MAX_VERTEX_STREAMS, 1);
429 break;
430
431 /* calculate overflow here */
432 case GL_VERTICES_SUBMITTED_ARB:
433 case GL_PRIMITIVES_SUBMITTED_ARB:
434 case GL_VERTEX_SHADER_INVOCATIONS_ARB:
435 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
436 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
437 case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
438 case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
439 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
440 case GL_GEOMETRY_SHADER_INVOCATIONS:
441 case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
442 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
443 emit_pipeline_stat(brw, query->bo,
444 query->Base.Stream, query->Base.Target, 1);
445 break;
446
447 default:
448 unreachable("Unrecognized query target in brw_end_query()");
449 }
450
451 /* The current batch contains the commands to handle EndQuery(),
452 * but they won't actually execute until it is flushed.
453 */
454 query->flushed = false;
455
456 /* For ARB_query_buffer_object: The result is now available */
457 set_query_availability(brw, query, true);
458 }
459
460 /**
461 * Flush the batch if it still references the query object BO.
462 */
463 static void
464 flush_batch_if_needed(struct brw_context *brw, struct brw_query_object *query)
465 {
466 /* If the batch doesn't reference the BO, it must have been flushed
467 * (for example, due to being full). Record that it's been flushed.
468 */
469 query->flushed = query->flushed ||
470 !brw_batch_references(&brw->batch, query->bo);
471
472 if (!query->flushed)
473 intel_batchbuffer_flush(brw);
474 }
475
476 /**
477 * The WaitQuery() driver hook.
478 *
479 * Wait for a query result to become available and return it. This is the
480 * backing for glGetQueryObjectiv() with the GL_QUERY_RESULT pname.
481 */
482 static void gen6_wait_query(struct gl_context *ctx, struct gl_query_object *q)
483 {
484 struct brw_context *brw = brw_context(ctx);
485 struct brw_query_object *query = (struct brw_query_object *)q;
486
487 /* If the application has requested the query result, but this batch is
488 * still contributing to it, flush it now to finish that work so the
489 * result will become available (eventually).
490 */
491 flush_batch_if_needed(brw, query);
492
493 gen6_queryobj_get_results(ctx, query);
494 }
495
496 /**
497 * The CheckQuery() driver hook.
498 *
499 * Checks whether a query result is ready yet. If not, flushes.
500 * This is the backing for glGetQueryObjectiv()'s QUERY_RESULT_AVAILABLE pname.
501 */
502 static void gen6_check_query(struct gl_context *ctx, struct gl_query_object *q)
503 {
504 struct brw_context *brw = brw_context(ctx);
505 struct brw_query_object *query = (struct brw_query_object *)q;
506
507 /* If query->bo is NULL, we've already gathered the results - this is a
508 * redundant CheckQuery call. Ignore it.
509 */
510 if (query->bo == NULL)
511 return;
512
513 /* From the GL_ARB_occlusion_query spec:
514 *
515 * "Instead of allowing for an infinite loop, performing a
516 * QUERY_RESULT_AVAILABLE_ARB will perform a flush if the result is
517 * not ready yet on the first time it is queried. This ensures that
518 * the async query will return true in finite time.
519 */
520 flush_batch_if_needed(brw, query);
521
522 if (!drm_bacon_bo_busy(query->bo)) {
523 gen6_queryobj_get_results(ctx, query);
524 }
525 }
526
527 static void
528 gen6_query_counter(struct gl_context *ctx, struct gl_query_object *q)
529 {
530 struct brw_context *brw = brw_context(ctx);
531 struct brw_query_object *query = (struct brw_query_object *)q;
532 brw_query_counter(ctx, q);
533 set_query_availability(brw, query, true);
534 }
535
536 /* Initialize Gen6+-specific query object functions. */
537 void gen6_init_queryobj_functions(struct dd_function_table *functions)
538 {
539 functions->BeginQuery = gen6_begin_query;
540 functions->EndQuery = gen6_end_query;
541 functions->CheckQuery = gen6_check_query;
542 functions->WaitQuery = gen6_wait_query;
543 functions->QueryCounter = gen6_query_counter;
544 }