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