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