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