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 an arbitrary 64-bit register to a buffer via MI_STORE_REGISTER_MEM.
45 * Only TIMESTAMP and PS_DEPTH_COUNT have special PIPE_CONTROL support; other
46 * counters have to be read via the generic MI_STORE_REGISTER_MEM.
48 * Callers must explicitly flush the pipeline to ensure the desired value is
52 brw_store_register_mem64(struct brw_context
*brw
,
53 drm_intel_bo
*bo
, uint32_t reg
, int idx
)
55 assert(brw
->gen
>= 6);
57 /* MI_STORE_REGISTER_MEM only stores a single 32-bit value, so to
58 * read a full 64-bit register, we need to do two of them.
62 OUT_BATCH(MI_STORE_REGISTER_MEM
| (4 - 2));
64 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
65 idx
* sizeof(uint64_t));
66 OUT_BATCH(MI_STORE_REGISTER_MEM
| (4 - 2));
67 OUT_BATCH(reg
+ sizeof(uint32_t));
68 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
69 sizeof(uint32_t) + idx
* sizeof(uint64_t));
73 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
75 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
76 idx
* sizeof(uint64_t));
77 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
78 OUT_BATCH(reg
+ sizeof(uint32_t));
79 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
80 sizeof(uint32_t) + idx
* sizeof(uint64_t));
86 write_primitives_generated(struct brw_context
*brw
,
87 drm_intel_bo
*query_bo
, int idx
)
89 intel_batchbuffer_emit_mi_flush(brw
);
91 brw_store_register_mem64(brw
, query_bo
, CL_INVOCATION_COUNT
, idx
);
95 write_xfb_primitives_written(struct brw_context
*brw
,
96 drm_intel_bo
*bo
, int stream
, int idx
)
98 intel_batchbuffer_emit_mi_flush(brw
);
101 brw_store_register_mem64(brw
, bo
, GEN7_SO_NUM_PRIMS_WRITTEN(stream
), idx
);
103 brw_store_register_mem64(brw
, bo
, GEN6_SO_NUM_PRIMS_WRITTEN
, idx
);
108 * Wait on the query object's BO and calculate the final result.
111 gen6_queryobj_get_results(struct gl_context
*ctx
,
112 struct brw_query_object
*query
)
114 struct brw_context
*brw
= brw_context(ctx
);
116 if (query
->bo
== NULL
)
119 /* If the application has requested the query result, but this batch is
120 * still contributing to it, flush it now so the results will be present
123 if (drm_intel_bo_references(brw
->batch
.bo
, query
->bo
))
124 intel_batchbuffer_flush(brw
);
126 if (unlikely(brw
->perf_debug
)) {
127 if (drm_intel_bo_busy(query
->bo
)) {
128 perf_debug("Stalling on the GPU waiting for a query object.\n");
132 drm_intel_bo_map(query
->bo
, false);
133 uint64_t *results
= query
->bo
->virtual;
134 switch (query
->Base
.Target
) {
135 case GL_TIME_ELAPSED
:
136 /* The query BO contains the starting and ending timestamps.
137 * Subtract the two and convert to nanoseconds.
139 query
->Base
.Result
+= 80 * (results
[1] - results
[0]);
143 /* Our timer is a clock that increments every 80ns (regardless of
144 * other clock scaling in the system). The timestamp register we can
145 * read for glGetTimestamp() masks out the top 32 bits, so we do that
146 * here too to let the two counters be compared against each other.
148 * If we just multiplied that 32 bits of data by 80, it would roll
149 * over at a non-power-of-two, so an application couldn't use
150 * GL_QUERY_COUNTER_BITS to handle rollover correctly. Instead, we
151 * report 36 bits and truncate at that (rolling over 5 times as often
152 * as the HW counter), and when the 32-bit counter rolls over, it
153 * happens to also be at a rollover in the reported value from near
156 * The low 32 bits rolls over in ~343 seconds. Our 36-bit result
157 * rolls over every ~69 seconds.
159 * The query BO contains a single timestamp value in results[0].
161 query
->Base
.Result
= 80 * (results
[0] & 0xffffffff);
162 query
->Base
.Result
&= (1ull << 36) - 1;
165 case GL_SAMPLES_PASSED_ARB
:
166 /* We need to use += rather than = here since some BLT-based operations
167 * may have added additional samples to our occlusion query value.
169 query
->Base
.Result
+= results
[1] - results
[0];
172 case GL_ANY_SAMPLES_PASSED
:
173 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
174 if (results
[0] != results
[1])
175 query
->Base
.Result
= true;
178 case GL_PRIMITIVES_GENERATED
:
179 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
180 query
->Base
.Result
= results
[1] - results
[0];
184 unreachable("Unrecognized query target in brw_queryobj_get_results()");
186 drm_intel_bo_unmap(query
->bo
);
188 /* Now that we've processed the data stored in the query's buffer object,
191 drm_intel_bo_unreference(query
->bo
);
196 * Driver hook for glBeginQuery().
198 * Initializes driver structures and emits any GPU commands required to begin
199 * recording data for the query.
202 gen6_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
204 struct brw_context
*brw
= brw_context(ctx
);
205 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
207 /* Since we're starting a new query, we need to throw away old results. */
208 drm_intel_bo_unreference(query
->bo
);
209 query
->bo
= drm_intel_bo_alloc(brw
->bufmgr
, "query results", 4096, 4096);
211 switch (query
->Base
.Target
) {
212 case GL_TIME_ELAPSED
:
213 /* For timestamp queries, we record the starting time right away so that
214 * we measure the full time between BeginQuery and EndQuery. There's
215 * some debate about whether this is the right thing to do. Our decision
216 * is based on the following text from the ARB_timer_query extension:
218 * "(5) Should the extension measure total time elapsed between the full
219 * completion of the BeginQuery and EndQuery commands, or just time
220 * spent in the graphics library?
222 * RESOLVED: This extension will measure the total time elapsed
223 * between the full completion of these commands. Future extensions
224 * may implement a query to determine time elapsed at different stages
225 * of the graphics pipeline."
227 * We write a starting timestamp now (at index 0). At EndQuery() time,
228 * we'll write a second timestamp (at index 1), and subtract the two to
229 * obtain the time elapsed. Notably, this includes time elapsed while
230 * the system was doing other work, such as running other applications.
232 brw_write_timestamp(brw
, query
->bo
, 0);
235 case GL_ANY_SAMPLES_PASSED
:
236 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
237 case GL_SAMPLES_PASSED_ARB
:
238 brw_write_depth_count(brw
, query
->bo
, 0);
241 case GL_PRIMITIVES_GENERATED
:
242 write_primitives_generated(brw
, query
->bo
, 0);
245 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
246 write_xfb_primitives_written(brw
, query
->bo
, query
->Base
.Stream
, 0);
250 unreachable("Unrecognized query target in brw_begin_query()");
255 * Driver hook for glEndQuery().
257 * Emits GPU commands to record a final query value, ending any data capturing.
258 * However, the final result isn't necessarily available until the GPU processes
259 * those commands. brw_queryobj_get_results() processes the captured data to
260 * produce the final result.
263 gen6_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
265 struct brw_context
*brw
= brw_context(ctx
);
266 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
268 switch (query
->Base
.Target
) {
269 case GL_TIME_ELAPSED
:
270 brw_write_timestamp(brw
, query
->bo
, 1);
273 case GL_ANY_SAMPLES_PASSED
:
274 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
275 case GL_SAMPLES_PASSED_ARB
:
276 brw_write_depth_count(brw
, query
->bo
, 1);
279 case GL_PRIMITIVES_GENERATED
:
280 write_primitives_generated(brw
, query
->bo
, 1);
283 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
284 write_xfb_primitives_written(brw
, query
->bo
, query
->Base
.Stream
, 1);
288 unreachable("Unrecognized query target in brw_end_query()");
293 * The WaitQuery() driver hook.
295 * Wait for a query result to become available and return it. This is the
296 * backing for glGetQueryObjectiv() with the GL_QUERY_RESULT pname.
298 static void gen6_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
300 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
302 gen6_queryobj_get_results(ctx
, query
);
303 query
->Base
.Ready
= true;
307 * The CheckQuery() driver hook.
309 * Checks whether a query result is ready yet. If not, flushes.
310 * This is the backing for glGetQueryObjectiv()'s QUERY_RESULT_AVAILABLE pname.
312 static void gen6_check_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 /* From the GL_ARB_occlusion_query spec:
319 * "Instead of allowing for an infinite loop, performing a
320 * QUERY_RESULT_AVAILABLE_ARB will perform a flush if the result is
321 * not ready yet on the first time it is queried. This ensures that
322 * the async query will return true in finite time.
324 if (query
->bo
&& drm_intel_bo_references(brw
->batch
.bo
, query
->bo
))
325 intel_batchbuffer_flush(brw
);
327 if (query
->bo
== NULL
|| !drm_intel_bo_busy(query
->bo
)) {
328 gen6_queryobj_get_results(ctx
, query
);
329 query
->Base
.Ready
= true;
333 /* Initialize Gen6+-specific query object functions. */
334 void gen6_init_queryobj_functions(struct dd_function_table
*functions
)
336 functions
->BeginQuery
= gen6_begin_query
;
337 functions
->EndQuery
= gen6_end_query
;
338 functions
->CheckQuery
= gen6_check_query
;
339 functions
->WaitQuery
= gen6_wait_query
;