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