2 * Copyright © 2008 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
25 * Kenneth Graunke <kenneth@whitecape.org>
28 /** @file gen6_queryobj.c
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+).
34 #include "main/imports.h"
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"
43 write_primitives_generated(struct brw_context
*brw
,
44 drm_intel_bo
*query_bo
, int stream
, int idx
)
46 brw_emit_mi_flush(brw
);
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));
53 brw_store_register_mem64(brw
, query_bo
, CL_INVOCATION_COUNT
,
54 idx
* sizeof(uint64_t));
59 write_xfb_primitives_written(struct brw_context
*brw
,
60 drm_intel_bo
*bo
, int stream
, int idx
)
62 brw_emit_mi_flush(brw
);
65 brw_store_register_mem64(brw
, bo
, GEN7_SO_NUM_PRIMS_WRITTEN(stream
),
66 idx
* sizeof(uint64_t));
68 brw_store_register_mem64(brw
, bo
, GEN6_SO_NUM_PRIMS_WRITTEN
,
69 idx
* sizeof(uint64_t));
73 static inline const int
74 pipeline_target_to_index(int target
)
76 if (target
== GL_GEOMETRY_SHADER_INVOCATIONS
)
77 return MAX_PIPELINE_STATISTICS
- 1;
79 return target
- GL_VERTICES_SUBMITTED_ARB
;
83 emit_pipeline_stat(struct brw_context
*brw
, drm_intel_bo
*bo
,
84 int stream
, int target
, int idx
)
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.
93 /* Everything except GEOMETRY_SHADER_INVOCATIONS can be kept in a simple
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... */
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.
114 if (brw
->gen
== 6 && target
== GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB
)
115 reg
= CL_INVOCATION_COUNT
;
118 /* Emit a flush to make sure various parts of the pipeline are complete and
119 * we get an accurate value
121 brw_emit_mi_flush(brw
);
123 brw_store_register_mem64(brw
, bo
, reg
, idx
* sizeof(uint64_t));
128 * Wait on the query object's BO and calculate the final result.
131 gen6_queryobj_get_results(struct gl_context
*ctx
,
132 struct brw_query_object
*query
)
134 struct brw_context
*brw
= brw_context(ctx
);
136 if (query
->bo
== NULL
)
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.
146 query
->Base
.Result
+= 80 * (results
[1] - results
[0]);
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.
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
163 * The low 32 bits rolls over in ~343 seconds. Our 36-bit result
164 * rolls over every ~69 seconds.
166 * The query BO contains a single timestamp value in results[0].
168 query
->Base
.Result
= 80 * (results
[0] & 0xffffffff);
169 query
->Base
.Result
&= (1ull << 36) - 1;
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.
176 query
->Base
.Result
+= results
[1] - results
[0];
179 case GL_ANY_SAMPLES_PASSED
:
180 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
181 if (results
[0] != results
[1])
182 query
->Base
.Result
= true;
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];
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."
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.
212 if (brw
->gen
== 8 || brw
->is_haswell
)
213 query
->Base
.Result
/= 4;
217 unreachable("Unrecognized query target in brw_queryobj_get_results()");
219 drm_intel_bo_unmap(query
->bo
);
221 /* Now that we've processed the data stored in the query's buffer object,
224 drm_intel_bo_unreference(query
->bo
);
227 query
->Base
.Ready
= true;
231 * Driver hook for glBeginQuery().
233 * Initializes driver structures and emits any GPU commands required to begin
234 * recording data for the query.
237 gen6_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
239 struct brw_context
*brw
= brw_context(ctx
);
240 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
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);
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:
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?
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."
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.
267 brw_write_timestamp(brw
, query
->bo
, 0);
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);
276 case GL_PRIMITIVES_GENERATED
:
277 write_primitives_generated(brw
, query
->bo
, query
->Base
.Stream
, 0);
280 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
281 write_xfb_primitives_written(brw
, query
->bo
, query
->Base
.Stream
, 0);
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);
299 unreachable("Unrecognized query target in brw_begin_query()");
304 * Driver hook for glEndQuery().
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.
312 gen6_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
314 struct brw_context
*brw
= brw_context(ctx
);
315 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
317 switch (query
->Base
.Target
) {
318 case GL_TIME_ELAPSED
:
319 brw_write_timestamp(brw
, query
->bo
, 1);
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);
328 case GL_PRIMITIVES_GENERATED
:
329 write_primitives_generated(brw
, query
->bo
, query
->Base
.Stream
, 1);
332 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
333 write_xfb_primitives_written(brw
, query
->bo
, query
->Base
.Stream
, 1);
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);
352 unreachable("Unrecognized query target in brw_end_query()");
355 /* The current batch contains the commands to handle EndQuery(),
356 * but they won't actually execute until it is flushed.
358 query
->flushed
= false;
362 * Flush the batch if it still references the query object BO.
365 flush_batch_if_needed(struct brw_context
*brw
, struct brw_query_object
*query
)
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.
370 query
->flushed
= query
->flushed
||
371 !drm_intel_bo_references(brw
->batch
.bo
, query
->bo
);
374 intel_batchbuffer_flush(brw
);
378 * The WaitQuery() driver hook.
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.
383 static void gen6_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
385 struct brw_context
*brw
= brw_context(ctx
);
386 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
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).
392 flush_batch_if_needed(brw
, query
);
394 gen6_queryobj_get_results(ctx
, query
);
398 * The CheckQuery() driver hook.
400 * Checks whether a query result is ready yet. If not, flushes.
401 * This is the backing for glGetQueryObjectiv()'s QUERY_RESULT_AVAILABLE pname.
403 static void gen6_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
405 struct brw_context
*brw
= brw_context(ctx
);
406 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
408 /* If query->bo is NULL, we've already gathered the results - this is a
409 * redundant CheckQuery call. Ignore it.
411 if (query
->bo
== NULL
)
414 /* From the GL_ARB_occlusion_query spec:
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.
421 flush_batch_if_needed(brw
, query
);
423 if (!drm_intel_bo_busy(query
->bo
)) {
424 gen6_queryobj_get_results(ctx
, query
);
428 /* Initialize Gen6+-specific query object functions. */
429 void gen6_init_queryobj_functions(struct dd_function_table
*functions
)
431 functions
->BeginQuery
= gen6_begin_query
;
432 functions
->EndQuery
= gen6_end_query
;
433 functions
->CheckQuery
= gen6_check_query
;
434 functions
->WaitQuery
= gen6_wait_query
;