i965: Convert if/else to switch statements in brw_queryobj.c
[mesa.git] / src / mesa / drivers / dri / i965 / brw_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 *
26 */
27
28 /** @file support for ARB_query_object
29 *
30 * ARB_query_object is implemented by using the PIPE_CONTROL command to stall
31 * execution on the completion of previous depth tests, and write the
32 * current PS_DEPTH_COUNT to a buffer object.
33 *
34 * We use before and after counts when drawing during a query so that
35 * we don't pick up other clients' query data in ours. To reduce overhead,
36 * a single BO is used to record the query data for all active queries at
37 * once. This also gives us a simple bound on how much batchbuffer space is
38 * required for handling queries, so that we can be sure that we won't
39 * have to emit a batchbuffer without getting the ending PS_DEPTH_COUNT.
40 */
41 #include "main/imports.h"
42
43 #include "brw_context.h"
44 #include "brw_state.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_reg.h"
47
48 /** Waits on the query object's BO and totals the results for this query */
49 static void
50 brw_queryobj_get_results(struct gl_context *ctx,
51 struct brw_query_object *query)
52 {
53 struct intel_context *intel = intel_context(ctx);
54
55 int i;
56 uint64_t *results;
57
58 if (query->bo == NULL)
59 return;
60
61 drm_intel_bo_map(query->bo, false);
62 results = query->bo->virtual;
63 switch (query->Base.Target) {
64 case GL_TIME_ELAPSED_EXT:
65 if (intel->gen >= 6)
66 query->Base.Result += 80 * (results[1] - results[0]);
67 else
68 query->Base.Result += 1000 * ((results[1] >> 32) - (results[0] >> 32));
69 break;
70
71 case GL_SAMPLES_PASSED_ARB:
72 /* Map and count the pixels from the current query BO */
73 for (i = query->first_index; i <= query->last_index; i++) {
74 query->Base.Result += results[i * 2 + 1] - results[i * 2];
75 }
76 break;
77
78 default:
79 assert(!"Unrecognized query target in brw_queryobj_get_results()");
80 break;
81 }
82 drm_intel_bo_unmap(query->bo);
83
84 drm_intel_bo_unreference(query->bo);
85 query->bo = NULL;
86 }
87
88 static struct gl_query_object *
89 brw_new_query_object(struct gl_context *ctx, GLuint id)
90 {
91 struct brw_query_object *query;
92
93 query = calloc(1, sizeof(struct brw_query_object));
94
95 query->Base.Id = id;
96 query->Base.Result = 0;
97 query->Base.Active = false;
98 query->Base.Ready = true;
99
100 return &query->Base;
101 }
102
103 static void
104 brw_delete_query(struct gl_context *ctx, struct gl_query_object *q)
105 {
106 struct brw_query_object *query = (struct brw_query_object *)q;
107
108 drm_intel_bo_unreference(query->bo);
109 free(query);
110 }
111
112 static void
113 brw_begin_query(struct gl_context *ctx, struct gl_query_object *q)
114 {
115 struct brw_context *brw = brw_context(ctx);
116 struct intel_context *intel = intel_context(ctx);
117 struct brw_query_object *query = (struct brw_query_object *)q;
118
119 switch (query->Base.Target) {
120 case GL_TIME_ELAPSED_EXT:
121 drm_intel_bo_unreference(query->bo);
122 query->bo = drm_intel_bo_alloc(intel->bufmgr, "timer query",
123 4096, 4096);
124
125 if (intel->gen >= 6) {
126 BEGIN_BATCH(4);
127 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
128 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
129 OUT_RELOC(query->bo,
130 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
131 PIPE_CONTROL_GLOBAL_GTT_WRITE |
132 0);
133 OUT_BATCH(0);
134 ADVANCE_BATCH();
135
136 } else {
137 BEGIN_BATCH(4);
138 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
139 PIPE_CONTROL_WRITE_TIMESTAMP);
140 OUT_RELOC(query->bo,
141 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
142 PIPE_CONTROL_GLOBAL_GTT_WRITE |
143 0);
144 OUT_BATCH(0);
145 OUT_BATCH(0);
146 ADVANCE_BATCH();
147 }
148 break;
149
150 case GL_SAMPLES_PASSED_ARB:
151 /* Reset our driver's tracking of query state. */
152 drm_intel_bo_unreference(query->bo);
153 query->bo = NULL;
154 query->first_index = -1;
155 query->last_index = -1;
156
157 brw->query.obj = query;
158 intel->stats_wm++;
159 break;
160
161 default:
162 assert(!"Unrecognized query target in brw_begin_query()");
163 break;
164 }
165 }
166
167 /**
168 * Begin the ARB_occlusion_query query on a query object.
169 */
170 static void
171 brw_end_query(struct gl_context *ctx, struct gl_query_object *q)
172 {
173 struct brw_context *brw = brw_context(ctx);
174 struct intel_context *intel = intel_context(ctx);
175 struct brw_query_object *query = (struct brw_query_object *)q;
176
177 switch (query->Base.Target) {
178 case GL_TIME_ELAPSED_EXT:
179 if (intel->gen >= 6) {
180 BEGIN_BATCH(4);
181 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
182 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
183 OUT_RELOC(query->bo,
184 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
185 PIPE_CONTROL_GLOBAL_GTT_WRITE |
186 8);
187 OUT_BATCH(0);
188 ADVANCE_BATCH();
189
190 } else {
191 BEGIN_BATCH(4);
192 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
193 PIPE_CONTROL_WRITE_TIMESTAMP);
194 OUT_RELOC(query->bo,
195 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
196 PIPE_CONTROL_GLOBAL_GTT_WRITE |
197 8);
198 OUT_BATCH(0);
199 OUT_BATCH(0);
200 ADVANCE_BATCH();
201 }
202
203 intel_batchbuffer_flush(intel);
204 break;
205
206 case GL_SAMPLES_PASSED_ARB:
207 /* Flush the batchbuffer in case it has writes to our query BO.
208 * Have later queries write to a new query BO so that further rendering
209 * doesn't delay the collection of our results.
210 */
211 if (query->bo) {
212 brw_emit_query_end(brw);
213 intel_batchbuffer_flush(intel);
214
215 drm_intel_bo_unreference(brw->query.bo);
216 brw->query.bo = NULL;
217 }
218
219 brw->query.obj = NULL;
220
221 intel->stats_wm--;
222 break;
223
224 default:
225 assert(!"Unrecognized query target in brw_end_query()");
226 break;
227 }
228 }
229
230 static void brw_wait_query(struct gl_context *ctx, struct gl_query_object *q)
231 {
232 struct brw_query_object *query = (struct brw_query_object *)q;
233
234 brw_queryobj_get_results(ctx, query);
235 query->Base.Ready = true;
236 }
237
238 static void brw_check_query(struct gl_context *ctx, struct gl_query_object *q)
239 {
240 struct brw_query_object *query = (struct brw_query_object *)q;
241
242 if (query->bo == NULL || !drm_intel_bo_busy(query->bo)) {
243 brw_queryobj_get_results(ctx, query);
244 query->Base.Ready = true;
245 }
246 }
247
248 /** Called to set up the query BO and account for its aperture space */
249 void
250 brw_prepare_query_begin(struct brw_context *brw)
251 {
252 struct intel_context *intel = &brw->intel;
253
254 /* Skip if we're not doing any queries. */
255 if (!brw->query.obj)
256 return;
257
258 /* Get a new query BO if we're going to need it. */
259 if (brw->query.bo == NULL ||
260 brw->query.index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
261 drm_intel_bo_unreference(brw->query.bo);
262 brw->query.bo = NULL;
263
264 brw->query.bo = drm_intel_bo_alloc(intel->bufmgr, "query", 4096, 1);
265
266 /* clear target buffer */
267 drm_intel_bo_map(brw->query.bo, true);
268 memset((char *)brw->query.bo->virtual, 0, 4096);
269 drm_intel_bo_unmap(brw->query.bo);
270
271 brw->query.index = 0;
272 }
273 }
274
275 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
276 void
277 brw_emit_query_begin(struct brw_context *brw)
278 {
279 struct intel_context *intel = &brw->intel;
280 struct gl_context *ctx = &intel->ctx;
281 struct brw_query_object *query = brw->query.obj;
282
283 /* Skip if we're not doing any queries, or we've emitted the start. */
284 if (!query || brw->query.active)
285 return;
286
287 if (intel->gen >= 6) {
288 BEGIN_BATCH(8);
289
290 /* workaround: CS stall required before depth stall. */
291 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
292 OUT_BATCH(PIPE_CONTROL_CS_STALL);
293 OUT_BATCH(0); /* write address */
294 OUT_BATCH(0); /* write data */
295
296 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
297 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
298 PIPE_CONTROL_WRITE_DEPTH_COUNT);
299 OUT_RELOC(brw->query.bo,
300 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
301 PIPE_CONTROL_GLOBAL_GTT_WRITE |
302 ((brw->query.index * 2) * sizeof(uint64_t)));
303 OUT_BATCH(0);
304 ADVANCE_BATCH();
305
306 } else {
307 BEGIN_BATCH(4);
308 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
309 PIPE_CONTROL_DEPTH_STALL |
310 PIPE_CONTROL_WRITE_DEPTH_COUNT);
311 /* This object could be mapped cacheable, but we don't have an exposed
312 * mechanism to support that. Since it's going uncached, tell GEM that
313 * we're writing to it. The usual clflush should be all that's required
314 * to pick up the results.
315 */
316 OUT_RELOC(brw->query.bo,
317 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
318 PIPE_CONTROL_GLOBAL_GTT_WRITE |
319 ((brw->query.index * 2) * sizeof(uint64_t)));
320 OUT_BATCH(0);
321 OUT_BATCH(0);
322 ADVANCE_BATCH();
323 }
324
325 if (query->bo != brw->query.bo) {
326 if (query->bo != NULL)
327 brw_queryobj_get_results(ctx, query);
328 drm_intel_bo_reference(brw->query.bo);
329 query->bo = brw->query.bo;
330 query->first_index = brw->query.index;
331 }
332 query->last_index = brw->query.index;
333 brw->query.active = true;
334 }
335
336 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
337 void
338 brw_emit_query_end(struct brw_context *brw)
339 {
340 struct intel_context *intel = &brw->intel;
341
342 if (!brw->query.active)
343 return;
344
345 if (intel->gen >= 6) {
346 BEGIN_BATCH(8);
347 /* workaround: CS stall required before depth stall. */
348 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
349 OUT_BATCH(PIPE_CONTROL_CS_STALL);
350 OUT_BATCH(0); /* write address */
351 OUT_BATCH(0); /* write data */
352
353 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
354 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
355 PIPE_CONTROL_WRITE_DEPTH_COUNT);
356 OUT_RELOC(brw->query.bo,
357 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
358 PIPE_CONTROL_GLOBAL_GTT_WRITE |
359 ((brw->query.index * 2 + 1) * sizeof(uint64_t)));
360 OUT_BATCH(0);
361 ADVANCE_BATCH();
362
363 } else {
364 BEGIN_BATCH(4);
365 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
366 PIPE_CONTROL_DEPTH_STALL |
367 PIPE_CONTROL_WRITE_DEPTH_COUNT);
368 OUT_RELOC(brw->query.bo,
369 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
370 PIPE_CONTROL_GLOBAL_GTT_WRITE |
371 ((brw->query.index * 2 + 1) * sizeof(uint64_t)));
372 OUT_BATCH(0);
373 OUT_BATCH(0);
374 ADVANCE_BATCH();
375 }
376
377 brw->query.active = false;
378 brw->query.index++;
379 }
380
381 void brw_init_queryobj_functions(struct dd_function_table *functions)
382 {
383 functions->NewQueryObject = brw_new_query_object;
384 functions->DeleteQuery = brw_delete_query;
385 functions->BeginQuery = brw_begin_query;
386 functions->EndQuery = brw_end_query;
387 functions->CheckQuery = brw_check_query;
388 functions->WaitQuery = brw_wait_query;
389 }