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