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