intel: Convert from GLboolean to 'bool' from stdbool.h.
[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 if (query->Base.Target == GL_TIME_ELAPSED_EXT) {
64 if (intel->gen >= 6)
65 query->Base.Result += 80 * (results[1] - results[0]);
66 else
67 query->Base.Result += 1000 * ((results[1] >> 32) - (results[0] >> 32));
68 } else {
69 /* Map and count the pixels from the current query BO */
70 for (i = query->first_index; i <= query->last_index; i++) {
71 query->Base.Result += results[i * 2 + 1] - results[i * 2];
72 }
73 }
74 drm_intel_bo_unmap(query->bo);
75
76 drm_intel_bo_unreference(query->bo);
77 query->bo = NULL;
78 }
79
80 static struct gl_query_object *
81 brw_new_query_object(struct gl_context *ctx, GLuint id)
82 {
83 struct brw_query_object *query;
84
85 query = calloc(1, sizeof(struct brw_query_object));
86
87 query->Base.Id = id;
88 query->Base.Result = 0;
89 query->Base.Active = false;
90 query->Base.Ready = true;
91
92 return &query->Base;
93 }
94
95 static void
96 brw_delete_query(struct gl_context *ctx, struct gl_query_object *q)
97 {
98 struct brw_query_object *query = (struct brw_query_object *)q;
99
100 drm_intel_bo_unreference(query->bo);
101 free(query);
102 }
103
104 static void
105 brw_begin_query(struct gl_context *ctx, struct gl_query_object *q)
106 {
107 struct brw_context *brw = brw_context(ctx);
108 struct intel_context *intel = intel_context(ctx);
109 struct brw_query_object *query = (struct brw_query_object *)q;
110
111 if (query->Base.Target == GL_TIME_ELAPSED_EXT) {
112 drm_intel_bo_unreference(query->bo);
113 query->bo = drm_intel_bo_alloc(intel->bufmgr, "timer query",
114 4096, 4096);
115
116 if (intel->gen >= 6) {
117 BEGIN_BATCH(4);
118 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
119 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
120 OUT_RELOC(query->bo,
121 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
122 PIPE_CONTROL_GLOBAL_GTT_WRITE |
123 0);
124 OUT_BATCH(0);
125 ADVANCE_BATCH();
126
127 } else {
128 BEGIN_BATCH(4);
129 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
130 PIPE_CONTROL_WRITE_TIMESTAMP);
131 OUT_RELOC(query->bo,
132 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
133 PIPE_CONTROL_GLOBAL_GTT_WRITE |
134 0);
135 OUT_BATCH(0);
136 OUT_BATCH(0);
137 ADVANCE_BATCH();
138 }
139 } else {
140 /* Reset our driver's tracking of query state. */
141 drm_intel_bo_unreference(query->bo);
142 query->bo = NULL;
143 query->first_index = -1;
144 query->last_index = -1;
145
146 brw->query.obj = query;
147 intel->stats_wm++;
148 }
149 }
150
151 /**
152 * Begin the ARB_occlusion_query query on a query object.
153 */
154 static void
155 brw_end_query(struct gl_context *ctx, struct gl_query_object *q)
156 {
157 struct brw_context *brw = brw_context(ctx);
158 struct intel_context *intel = intel_context(ctx);
159 struct brw_query_object *query = (struct brw_query_object *)q;
160
161 if (query->Base.Target == GL_TIME_ELAPSED_EXT) {
162 if (intel->gen >= 6) {
163 BEGIN_BATCH(4);
164 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
165 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
166 OUT_RELOC(query->bo,
167 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
168 PIPE_CONTROL_GLOBAL_GTT_WRITE |
169 8);
170 OUT_BATCH(0);
171 ADVANCE_BATCH();
172
173 } else {
174 BEGIN_BATCH(4);
175 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
176 PIPE_CONTROL_WRITE_TIMESTAMP);
177 OUT_RELOC(query->bo,
178 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
179 PIPE_CONTROL_GLOBAL_GTT_WRITE |
180 8);
181 OUT_BATCH(0);
182 OUT_BATCH(0);
183 ADVANCE_BATCH();
184 }
185
186 intel_batchbuffer_flush(intel);
187 } else {
188 /* Flush the batchbuffer in case it has writes to our query BO.
189 * Have later queries write to a new query BO so that further rendering
190 * doesn't delay the collection of our results.
191 */
192 if (query->bo) {
193 brw_emit_query_end(brw);
194 intel_batchbuffer_flush(intel);
195
196 drm_intel_bo_unreference(brw->query.bo);
197 brw->query.bo = NULL;
198 }
199
200 brw->query.obj = NULL;
201
202 intel->stats_wm--;
203 }
204 }
205
206 static void brw_wait_query(struct gl_context *ctx, struct gl_query_object *q)
207 {
208 struct brw_query_object *query = (struct brw_query_object *)q;
209
210 brw_queryobj_get_results(ctx, query);
211 query->Base.Ready = true;
212 }
213
214 static void brw_check_query(struct gl_context *ctx, struct gl_query_object *q)
215 {
216 struct brw_query_object *query = (struct brw_query_object *)q;
217
218 if (query->bo == NULL || !drm_intel_bo_busy(query->bo)) {
219 brw_queryobj_get_results(ctx, query);
220 query->Base.Ready = true;
221 }
222 }
223
224 /** Called to set up the query BO and account for its aperture space */
225 void
226 brw_prepare_query_begin(struct brw_context *brw)
227 {
228 struct intel_context *intel = &brw->intel;
229
230 /* Skip if we're not doing any queries. */
231 if (!brw->query.obj)
232 return;
233
234 /* Get a new query BO if we're going to need it. */
235 if (brw->query.bo == NULL ||
236 brw->query.index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
237 drm_intel_bo_unreference(brw->query.bo);
238 brw->query.bo = NULL;
239
240 brw->query.bo = drm_intel_bo_alloc(intel->bufmgr, "query", 4096, 1);
241
242 /* clear target buffer */
243 drm_intel_bo_map(brw->query.bo, true);
244 memset((char *)brw->query.bo->virtual, 0, 4096);
245 drm_intel_bo_unmap(brw->query.bo);
246
247 brw->query.index = 0;
248 }
249
250 brw_add_validated_bo(brw, brw->query.bo);
251 }
252
253 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
254 void
255 brw_emit_query_begin(struct brw_context *brw)
256 {
257 struct intel_context *intel = &brw->intel;
258 struct gl_context *ctx = &intel->ctx;
259 struct brw_query_object *query = brw->query.obj;
260
261 /* Skip if we're not doing any queries, or we've emitted the start. */
262 if (!query || brw->query.active)
263 return;
264
265 if (intel->gen >= 6) {
266 BEGIN_BATCH(8);
267
268 /* workaround: CS stall required before depth stall. */
269 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
270 OUT_BATCH(PIPE_CONTROL_CS_STALL);
271 OUT_BATCH(0); /* write address */
272 OUT_BATCH(0); /* write data */
273
274 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
275 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
276 PIPE_CONTROL_WRITE_DEPTH_COUNT);
277 OUT_RELOC(brw->query.bo,
278 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
279 PIPE_CONTROL_GLOBAL_GTT_WRITE |
280 ((brw->query.index * 2) * sizeof(uint64_t)));
281 OUT_BATCH(0);
282 ADVANCE_BATCH();
283
284 } else {
285 BEGIN_BATCH(4);
286 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
287 PIPE_CONTROL_DEPTH_STALL |
288 PIPE_CONTROL_WRITE_DEPTH_COUNT);
289 /* This object could be mapped cacheable, but we don't have an exposed
290 * mechanism to support that. Since it's going uncached, tell GEM that
291 * we're writing to it. The usual clflush should be all that's required
292 * to pick up the results.
293 */
294 OUT_RELOC(brw->query.bo,
295 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
296 PIPE_CONTROL_GLOBAL_GTT_WRITE |
297 ((brw->query.index * 2) * sizeof(uint64_t)));
298 OUT_BATCH(0);
299 OUT_BATCH(0);
300 ADVANCE_BATCH();
301 }
302
303 if (query->bo != brw->query.bo) {
304 if (query->bo != NULL)
305 brw_queryobj_get_results(ctx, query);
306 drm_intel_bo_reference(brw->query.bo);
307 query->bo = brw->query.bo;
308 query->first_index = brw->query.index;
309 }
310 query->last_index = brw->query.index;
311 brw->query.active = true;
312 }
313
314 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
315 void
316 brw_emit_query_end(struct brw_context *brw)
317 {
318 struct intel_context *intel = &brw->intel;
319
320 if (!brw->query.active)
321 return;
322
323 if (intel->gen >= 6) {
324 BEGIN_BATCH(8);
325 /* workaround: CS stall required before depth stall. */
326 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
327 OUT_BATCH(PIPE_CONTROL_CS_STALL);
328 OUT_BATCH(0); /* write address */
329 OUT_BATCH(0); /* write data */
330
331 OUT_BATCH(_3DSTATE_PIPE_CONTROL);
332 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
333 PIPE_CONTROL_WRITE_DEPTH_COUNT);
334 OUT_RELOC(brw->query.bo,
335 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
336 PIPE_CONTROL_GLOBAL_GTT_WRITE |
337 ((brw->query.index * 2 + 1) * sizeof(uint64_t)));
338 OUT_BATCH(0);
339 ADVANCE_BATCH();
340
341 } else {
342 BEGIN_BATCH(4);
343 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
344 PIPE_CONTROL_DEPTH_STALL |
345 PIPE_CONTROL_WRITE_DEPTH_COUNT);
346 OUT_RELOC(brw->query.bo,
347 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
348 PIPE_CONTROL_GLOBAL_GTT_WRITE |
349 ((brw->query.index * 2 + 1) * sizeof(uint64_t)));
350 OUT_BATCH(0);
351 OUT_BATCH(0);
352 ADVANCE_BATCH();
353 }
354
355 brw->query.active = false;
356 brw->query.index++;
357 }
358
359 void brw_init_queryobj_functions(struct dd_function_table *functions)
360 {
361 functions->NewQueryObject = brw_new_query_object;
362 functions->DeleteQuery = brw_delete_query;
363 functions->BeginQuery = brw_begin_query;
364 functions->EndQuery = brw_end_query;
365 functions->CheckQuery = brw_check_query;
366 functions->WaitQuery = brw_wait_query;
367 }