Merge commit 'origin/gallium-0.1' into gallium-0.2
[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/simple_list.h"
42 #include "main/imports.h"
43
44 #include "brw_context.h"
45 #include "brw_state.h"
46 #include "intel_batchbuffer.h"
47 #include "intel_reg.h"
48
49 /** Waits on the query object's BO and totals the results for this query */
50 static void
51 brw_queryobj_get_results(struct brw_query_object *query)
52 {
53 int i;
54 uint64_t *results;
55
56 if (query->bo == NULL)
57 return;
58
59 /* Map and count the pixels from the current query BO */
60 dri_bo_map(query->bo, GL_FALSE);
61 results = query->bo->virtual;
62 for (i = query->first_index; i <= query->last_index; i++) {
63 query->Base.Result += results[i * 2 + 1] - results[i * 2];
64 }
65 dri_bo_unmap(query->bo);
66
67 dri_bo_unreference(query->bo);
68 query->bo = NULL;
69 }
70
71 static struct gl_query_object *
72 brw_new_query_object(GLcontext *ctx, GLuint id)
73 {
74 struct brw_query_object *query;
75
76 query = _mesa_calloc(sizeof(struct brw_query_object));
77
78 query->Base.Id = id;
79 query->Base.Result = 0;
80 query->Base.Active = GL_FALSE;
81 query->Base.Ready = GL_TRUE;
82
83 return &query->Base;
84 }
85
86 static void
87 brw_delete_query(GLcontext *ctx, struct gl_query_object *q)
88 {
89 struct brw_query_object *query = (struct brw_query_object *)q;
90
91 dri_bo_unreference(query->bo);
92 _mesa_free(query);
93 }
94
95 static void
96 brw_begin_query(GLcontext *ctx, struct gl_query_object *q)
97 {
98 struct brw_context *brw = brw_context(ctx);
99 struct intel_context *intel = intel_context(ctx);
100 struct brw_query_object *query = (struct brw_query_object *)q;
101
102 /* Reset our driver's tracking of query state. */
103 dri_bo_unreference(query->bo);
104 query->bo = NULL;
105 query->first_index = -1;
106 query->last_index = -1;
107
108 insert_at_head(&brw->query.active_head, query);
109 intel->stats_wm++;
110 }
111
112 /**
113 * Begin the ARB_occlusion_query query on a query object.
114 */
115 static void
116 brw_end_query(GLcontext *ctx, struct gl_query_object *q)
117 {
118 struct brw_context *brw = brw_context(ctx);
119 struct intel_context *intel = intel_context(ctx);
120 struct brw_query_object *query = (struct brw_query_object *)q;
121
122 /* Flush the batchbuffer in case it has writes to our query BO.
123 * Have later queries write to a new query BO so that further rendering
124 * doesn't delay the collection of our results.
125 */
126 if (query->bo) {
127 brw_emit_query_end(brw);
128 intel_batchbuffer_flush(intel->batch);
129
130 dri_bo_unreference(brw->query.bo);
131 brw->query.bo = NULL;
132 }
133
134 remove_from_list(query);
135
136 intel->stats_wm--;
137 }
138
139 static void brw_wait_query(GLcontext *ctx, struct gl_query_object *q)
140 {
141 struct brw_query_object *query = (struct brw_query_object *)q;
142
143 brw_queryobj_get_results(query);
144 query->Base.Ready = GL_TRUE;
145 }
146
147 static void brw_check_query(GLcontext *ctx, struct gl_query_object *q)
148 {
149 /* XXX: Need to expose dri_bo_is_idle from bufmgr. */
150 #if 0
151 struct brw_query_object *query = (struct brw_query_object *)q;
152
153 if (dri_bo_is_idle(query->bo)) {
154 brw_queryobj_get_results(query);
155 query->Base.Ready = GL_TRUE;
156 }
157 #else
158 brw_wait_query(ctx, q);
159 #endif
160 }
161
162 /** Called to set up the query BO and account for its aperture space */
163 void
164 brw_prepare_query_begin(struct brw_context *brw)
165 {
166 struct intel_context *intel = &brw->intel;
167
168 /* Skip if we're not doing any queries. */
169 if (is_empty_list(&brw->query.active_head))
170 return;
171
172 /* Get a new query BO if we're going to need it. */
173 if (brw->query.bo == NULL ||
174 brw->query.index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
175 dri_bo_unreference(brw->query.bo);
176 brw->query.bo = NULL;
177
178 brw->query.bo = dri_bo_alloc(intel->bufmgr, "query", 4096, 1);
179 brw->query.index = 0;
180 }
181
182 brw_add_validated_bo(brw, brw->query.bo);
183 }
184
185 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
186 void
187 brw_emit_query_begin(struct brw_context *brw)
188 {
189 struct intel_context *intel = &brw->intel;
190 struct brw_query_object *query;
191
192 /* Skip if we're not doing any queries, or we've emitted the start. */
193 if (brw->query.active || is_empty_list(&brw->query.active_head))
194 return;
195
196 BEGIN_BATCH(4, IGNORE_CLIPRECTS);
197 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
198 PIPE_CONTROL_DEPTH_STALL |
199 PIPE_CONTROL_WRITE_DEPTH_COUNT);
200 /* This object could be mapped cacheable, but we don't have an exposed
201 * mechanism to support that. Since it's going uncached, tell GEM that
202 * we're writing to it. The usual clflush should be all that's required
203 * to pick up the results.
204 */
205 OUT_RELOC(brw->query.bo,
206 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
207 PIPE_CONTROL_GLOBAL_GTT_WRITE |
208 ((brw->query.index * 2) * sizeof(uint64_t)));
209 OUT_BATCH(0);
210 OUT_BATCH(0);
211 ADVANCE_BATCH();
212
213 foreach(query, &brw->query.active_head) {
214 if (query->bo != brw->query.bo) {
215 if (query->bo != NULL)
216 brw_queryobj_get_results(query);
217 dri_bo_reference(brw->query.bo);
218 query->bo = brw->query.bo;
219 query->first_index = brw->query.index;
220 }
221 query->last_index = brw->query.index;
222 }
223 brw->query.active = GL_TRUE;
224 }
225
226 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
227 void
228 brw_emit_query_end(struct brw_context *brw)
229 {
230 struct intel_context *intel = &brw->intel;
231
232 if (!brw->query.active)
233 return;
234
235 BEGIN_BATCH(4, IGNORE_CLIPRECTS);
236 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
237 PIPE_CONTROL_DEPTH_STALL |
238 PIPE_CONTROL_WRITE_DEPTH_COUNT);
239 OUT_RELOC(brw->query.bo,
240 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
241 PIPE_CONTROL_GLOBAL_GTT_WRITE |
242 ((brw->query.index * 2 + 1) * sizeof(uint64_t)));
243 OUT_BATCH(0);
244 OUT_BATCH(0);
245 ADVANCE_BATCH();
246
247 brw->query.active = GL_FALSE;
248 brw->query.index++;
249 }
250
251 void brw_init_queryobj_functions(struct dd_function_table *functions)
252 {
253 functions->NewQueryObject = brw_new_query_object;
254 functions->DeleteQuery = brw_delete_query;
255 functions->BeginQuery = brw_begin_query;
256 functions->EndQuery = brw_end_query;
257 functions->CheckQuery = brw_check_query;
258 functions->WaitQuery = brw_wait_query;
259 }