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