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