i965: Move intel_context::bufmgr to brw_context.
[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 /**
43 * Emit PIPE_CONTROLs to write the current GPU timestamp into a buffer.
44 */
45 static void
46 write_timestamp(struct brw_context *brw, drm_intel_bo *query_bo, int idx)
47 {
48 struct intel_context *intel = &brw->intel;
49 /* Emit workaround flushes: */
50 if (intel->gen == 6) {
51 /* The timestamp write below is a non-zero post-sync op, which on
52 * Gen6 necessitates a CS stall. CS stalls need stall at scoreboard
53 * set. See the comments for intel_emit_post_sync_nonzero_flush().
54 */
55 BEGIN_BATCH(4);
56 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
57 OUT_BATCH(PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD);
58 OUT_BATCH(0);
59 OUT_BATCH(0);
60 ADVANCE_BATCH();
61 }
62
63 BEGIN_BATCH(5);
64 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
65 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
66 OUT_RELOC(query_bo,
67 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
68 PIPE_CONTROL_GLOBAL_GTT_WRITE |
69 idx * sizeof(uint64_t));
70 OUT_BATCH(0);
71 OUT_BATCH(0);
72 ADVANCE_BATCH();
73 }
74
75 /**
76 * Emit PIPE_CONTROLs to write the PS_DEPTH_COUNT register into a buffer.
77 */
78 static void
79 write_depth_count(struct brw_context *brw, drm_intel_bo *query_bo, int idx)
80 {
81 struct intel_context *intel = &brw->intel;
82 /* Emit Sandybridge workaround flush: */
83 if (intel->gen == 6)
84 intel_emit_post_sync_nonzero_flush(brw);
85
86 BEGIN_BATCH(5);
87 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
88 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
89 PIPE_CONTROL_WRITE_DEPTH_COUNT);
90 OUT_RELOC(query_bo,
91 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
92 PIPE_CONTROL_GLOBAL_GTT_WRITE |
93 (idx * sizeof(uint64_t)));
94 OUT_BATCH(0);
95 OUT_BATCH(0);
96 ADVANCE_BATCH();
97 }
98
99 /*
100 * Write an arbitrary 64-bit register to a buffer via MI_STORE_REGISTER_MEM.
101 *
102 * Only TIMESTAMP and PS_DEPTH_COUNT have special PIPE_CONTROL support; other
103 * counters have to be read via the generic MI_STORE_REGISTER_MEM. This
104 * function also performs a pipeline flush for proper synchronization.
105 */
106 static void
107 write_reg(struct brw_context *brw,
108 drm_intel_bo *query_bo, uint32_t reg, int idx)
109 {
110 struct intel_context *intel = &brw->intel;
111 assert(intel->gen >= 6);
112
113 intel_batchbuffer_emit_mi_flush(brw);
114
115 /* MI_STORE_REGISTER_MEM only stores a single 32-bit value, so to
116 * read a full 64-bit register, we need to do two of them.
117 */
118 BEGIN_BATCH(3);
119 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
120 OUT_BATCH(reg);
121 OUT_RELOC(query_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
122 idx * sizeof(uint64_t));
123 ADVANCE_BATCH();
124
125 BEGIN_BATCH(3);
126 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
127 OUT_BATCH(reg + sizeof(uint32_t));
128 OUT_RELOC(query_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
129 sizeof(uint32_t) + idx * sizeof(uint64_t));
130 ADVANCE_BATCH();
131 }
132
133 static void
134 write_primitives_generated(struct brw_context *brw,
135 drm_intel_bo *query_bo, int idx)
136 {
137 write_reg(brw, query_bo, CL_INVOCATION_COUNT, idx);
138 }
139
140 static void
141 write_xfb_primitives_written(struct brw_context *brw,
142 drm_intel_bo *query_bo, int idx)
143 {
144 struct intel_context *intel = &brw->intel;
145 if (intel->gen >= 7) {
146 write_reg(brw, query_bo, SO_NUM_PRIMS_WRITTEN0_IVB, idx);
147 } else {
148 write_reg(brw, query_bo, SO_NUM_PRIMS_WRITTEN, idx);
149 }
150 }
151
152 /**
153 * Wait on the query object's BO and calculate the final result.
154 */
155 static void
156 gen6_queryobj_get_results(struct gl_context *ctx,
157 struct brw_query_object *query)
158 {
159 struct brw_context *brw = brw_context(ctx);
160 struct intel_context *intel = intel_context(ctx);
161
162 if (query->bo == NULL)
163 return;
164
165 /* If the application has requested the query result, but this batch is
166 * still contributing to it, flush it now so the results will be present
167 * when mapped.
168 */
169 if (drm_intel_bo_references(intel->batch.bo, query->bo))
170 intel_batchbuffer_flush(brw);
171
172 if (unlikely(intel->perf_debug)) {
173 if (drm_intel_bo_busy(query->bo)) {
174 perf_debug("Stalling on the GPU waiting for a query object.\n");
175 }
176 }
177
178 drm_intel_bo_map(query->bo, false);
179 uint64_t *results = query->bo->virtual;
180 switch (query->Base.Target) {
181 case GL_TIME_ELAPSED:
182 /* The query BO contains the starting and ending timestamps.
183 * Subtract the two and convert to nanoseconds.
184 */
185 query->Base.Result += 80 * (results[1] - results[0]);
186 break;
187
188 case GL_TIMESTAMP:
189 /* Our timer is a clock that increments every 80ns (regardless of
190 * other clock scaling in the system). The timestamp register we can
191 * read for glGetTimestamp() masks out the top 32 bits, so we do that
192 * here too to let the two counters be compared against each other.
193 *
194 * If we just multiplied that 32 bits of data by 80, it would roll
195 * over at a non-power-of-two, so an application couldn't use
196 * GL_QUERY_COUNTER_BITS to handle rollover correctly. Instead, we
197 * report 36 bits and truncate at that (rolling over 5 times as often
198 * as the HW counter), and when the 32-bit counter rolls over, it
199 * happens to also be at a rollover in the reported value from near
200 * (1<<36) to 0.
201 *
202 * The low 32 bits rolls over in ~343 seconds. Our 36-bit result
203 * rolls over every ~69 seconds.
204 *
205 * The query BO contains a single timestamp value in results[0].
206 */
207 query->Base.Result = 80 * (results[0] & 0xffffffff);
208 query->Base.Result &= (1ull << 36) - 1;
209 break;
210
211 case GL_SAMPLES_PASSED_ARB:
212 /* We need to use += rather than = here since some BLT-based operations
213 * may have added additional samples to our occlusion query value.
214 */
215 query->Base.Result += results[1] - results[0];
216 break;
217
218 case GL_ANY_SAMPLES_PASSED:
219 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
220 if (results[0] != results[1])
221 query->Base.Result = true;
222 break;
223
224 case GL_PRIMITIVES_GENERATED:
225 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
226 query->Base.Result = results[1] - results[0];
227 break;
228
229 default:
230 assert(!"Unrecognized query target in brw_queryobj_get_results()");
231 break;
232 }
233 drm_intel_bo_unmap(query->bo);
234
235 /* Now that we've processed the data stored in the query's buffer object,
236 * we can release it.
237 */
238 drm_intel_bo_unreference(query->bo);
239 query->bo = NULL;
240 }
241
242 /**
243 * Driver hook for glBeginQuery().
244 *
245 * Initializes driver structures and emits any GPU commands required to begin
246 * recording data for the query.
247 */
248 static void
249 gen6_begin_query(struct gl_context *ctx, struct gl_query_object *q)
250 {
251 struct brw_context *brw = brw_context(ctx);
252 struct brw_query_object *query = (struct brw_query_object *)q;
253
254 /* Since we're starting a new query, we need to throw away old results. */
255 drm_intel_bo_unreference(query->bo);
256 query->bo = drm_intel_bo_alloc(brw->bufmgr, "query results", 4096, 4096);
257
258 switch (query->Base.Target) {
259 case GL_TIME_ELAPSED:
260 /* For timestamp queries, we record the starting time right away so that
261 * we measure the full time between BeginQuery and EndQuery. There's
262 * some debate about whether this is the right thing to do. Our decision
263 * is based on the following text from the ARB_timer_query extension:
264 *
265 * "(5) Should the extension measure total time elapsed between the full
266 * completion of the BeginQuery and EndQuery commands, or just time
267 * spent in the graphics library?
268 *
269 * RESOLVED: This extension will measure the total time elapsed
270 * between the full completion of these commands. Future extensions
271 * may implement a query to determine time elapsed at different stages
272 * of the graphics pipeline."
273 *
274 * We write a starting timestamp now (at index 0). At EndQuery() time,
275 * we'll write a second timestamp (at index 1), and subtract the two to
276 * obtain the time elapsed. Notably, this includes time elapsed while
277 * the system was doing other work, such as running other applications.
278 */
279 write_timestamp(brw, query->bo, 0);
280 break;
281
282 case GL_ANY_SAMPLES_PASSED:
283 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
284 case GL_SAMPLES_PASSED_ARB:
285 write_depth_count(brw, query->bo, 0);
286 break;
287
288 case GL_PRIMITIVES_GENERATED:
289 write_primitives_generated(brw, query->bo, 0);
290 break;
291
292 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
293 write_xfb_primitives_written(brw, query->bo, 0);
294 break;
295
296 default:
297 assert(!"Unrecognized query target in brw_begin_query()");
298 break;
299 }
300 }
301
302 /**
303 * Driver hook for glEndQuery().
304 *
305 * Emits GPU commands to record a final query value, ending any data capturing.
306 * However, the final result isn't necessarily available until the GPU processes
307 * those commands. brw_queryobj_get_results() processes the captured data to
308 * produce the final result.
309 */
310 static void
311 gen6_end_query(struct gl_context *ctx, struct gl_query_object *q)
312 {
313 struct brw_context *brw = brw_context(ctx);
314 struct brw_query_object *query = (struct brw_query_object *)q;
315
316 switch (query->Base.Target) {
317 case GL_TIME_ELAPSED:
318 write_timestamp(brw, query->bo, 1);
319 break;
320
321 case GL_ANY_SAMPLES_PASSED:
322 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
323 case GL_SAMPLES_PASSED_ARB:
324 write_depth_count(brw, query->bo, 1);
325 break;
326
327 case GL_PRIMITIVES_GENERATED:
328 write_primitives_generated(brw, query->bo, 1);
329 break;
330
331 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
332 write_xfb_primitives_written(brw, query->bo, 1);
333 break;
334
335 default:
336 assert(!"Unrecognized query target in brw_end_query()");
337 break;
338 }
339 }
340
341 /**
342 * The WaitQuery() driver hook.
343 *
344 * Wait for a query result to become available and return it. This is the
345 * backing for glGetQueryObjectiv() with the GL_QUERY_RESULT pname.
346 */
347 static void gen6_wait_query(struct gl_context *ctx, struct gl_query_object *q)
348 {
349 struct brw_query_object *query = (struct brw_query_object *)q;
350
351 gen6_queryobj_get_results(ctx, query);
352 query->Base.Ready = true;
353 }
354
355 /**
356 * The CheckQuery() driver hook.
357 *
358 * Checks whether a query result is ready yet. If not, flushes.
359 * This is the backing for glGetQueryObjectiv()'s QUERY_RESULT_AVAILABLE pname.
360 */
361 static void gen6_check_query(struct gl_context *ctx, struct gl_query_object *q)
362 {
363 struct brw_context *brw = brw_context(ctx);
364 struct intel_context *intel = intel_context(ctx);
365 struct brw_query_object *query = (struct brw_query_object *)q;
366
367 /* From the GL_ARB_occlusion_query spec:
368 *
369 * "Instead of allowing for an infinite loop, performing a
370 * QUERY_RESULT_AVAILABLE_ARB will perform a flush if the result is
371 * not ready yet on the first time it is queried. This ensures that
372 * the async query will return true in finite time.
373 */
374 if (query->bo && drm_intel_bo_references(intel->batch.bo, query->bo))
375 intel_batchbuffer_flush(brw);
376
377 if (query->bo == NULL || !drm_intel_bo_busy(query->bo)) {
378 gen6_queryobj_get_results(ctx, query);
379 query->Base.Ready = true;
380 }
381 }
382
383 /* Initialize Gen6+-specific query object functions. */
384 void gen6_init_queryobj_functions(struct dd_function_table *functions)
385 {
386 functions->BeginQuery = gen6_begin_query;
387 functions->EndQuery = gen6_end_query;
388 functions->CheckQuery = gen6_check_query;
389 functions->WaitQuery = gen6_wait_query;
390 }