i965g: more work on compiling
[mesa.git] / src / gallium / drivers / i965 / brw_pipe_query.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 "util/u_simple_list.h"
42
43 #include "brw_context.h"
44 #include "brw_state.h"
45 #include "brw_batchbuffer.h"
46 #include "brw_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 /* Map and count the pixels from the current query BO */
59 dri_bo_map(query->bo, GL_FALSE);
60 results = query->bo->virtual;
61 for (i = query->first_index; i <= query->last_index; i++) {
62 query->Base.Result += results[i * 2 + 1] - results[i * 2];
63 }
64 dri_bo_unmap(query->bo);
65
66 brw->sws->bo_unreference(query->bo);
67 query->bo = NULL;
68 }
69
70 static struct pipe_query *
71 brw_query_create(struct pipe_context *pipe, unsigned type )
72 {
73 struct brw_query_object *query;
74
75 switch (query->type) {
76 case PIPE_QUERY_OCCLUSION_COUNTER:
77 query = CALLOC_STRUCT( brw_query_object );
78 if (query == NULL)
79 return NULL;
80 return &query->Base;
81
82 default:
83 return NULL;
84 }
85 }
86
87 static void
88 brw_query_destroy(struct pipe_context *pipe, struct pipe_query *q)
89 {
90 struct brw_query_object *query = (struct brw_query_object *)q;
91
92 brw->sws->bo_unreference(query->bo);
93 FREE(query);
94 }
95
96 static void
97 brw_begin_query(struct pipe_context *pipe, struct pipe_query *q)
98 {
99 struct brw_context *brw = brw_context(pipe);
100 struct brw_query_object *query = (struct brw_query_object *)q;
101
102 /* Reset our driver's tracking of query state. */
103 brw->sws->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 brw->stats_wm++;
110 brw->dirty.mesa |= PIPE_NEW_QUERY;
111 }
112
113 static void
114 brw_end_query(struct pipe_context *pipe, struct pipe_query *q)
115 {
116 struct brw_context *brw = brw_context(pipe);
117 struct brw_query_object *query = (struct brw_query_object *)q;
118
119 /* Flush the batchbuffer in case it has writes to our query BO.
120 * Have later queries write to a new query BO so that further rendering
121 * doesn't delay the collection of our results.
122 */
123 if (query->bo) {
124 brw_emit_query_end(brw);
125 brw_batchbuffer_flush(brw->batch);
126
127 brw->sws->bo_unreference(brw->query.bo);
128 brw->query.bo = NULL;
129 }
130
131 remove_from_list(query);
132 brw->stats_wm--;
133 brw->dirty.mesa |= PIPE_NEW_QUERY;
134 }
135
136 static void brw_wait_query(struct pipe_context *pipe, struct pipe_query *q)
137 {
138 struct brw_query_object *query = (struct brw_query_object *)q;
139
140 brw_queryobj_get_results(query);
141 query->Base.Ready = GL_TRUE;
142 }
143
144 static void brw_check_query(struct pipe_context *pipe, struct pipe_query *q)
145 {
146 struct brw_query_object *query = (struct brw_query_object *)q;
147
148 if (query->bo == NULL || !drm_intel_bo_busy(query->bo)) {
149 brw_queryobj_get_results(query);
150 query->Base.Ready = GL_TRUE;
151 }
152 }
153
154 /** Called to set up the query BO and account for its aperture space */
155 void
156 brw_prepare_query_begin(struct brw_context *brw)
157 {
158 /* Skip if we're not doing any queries. */
159 if (is_empty_list(&brw->query.active_head))
160 return;
161
162 /* Get a new query BO if we're going to need it. */
163 if (brw->query.bo == NULL ||
164 brw->query.index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
165 brw->sws->bo_unreference(brw->query.bo);
166 brw->query.bo = NULL;
167
168 brw->query.bo = brw->sws->bo_alloc(brw->sws, BRW_BUFFER_TYPE_QUERY, 4096, 1);
169 brw->query.index = 0;
170 }
171
172 brw_add_validated_bo(brw, brw->query.bo);
173 }
174
175 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
176 void
177 brw_emit_query_begin(struct brw_context *brw)
178 {
179 struct brw_query_object *query;
180
181 /* Skip if we're not doing any queries, or we've emitted the start. */
182 if (brw->query.active || is_empty_list(&brw->query.active_head))
183 return;
184
185 BEGIN_BATCH(4, IGNORE_CLIPRECTS);
186 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
187 PIPE_CONTROL_DEPTH_STALL |
188 PIPE_CONTROL_WRITE_DEPTH_COUNT);
189 /* This object could be mapped cacheable, but we don't have an exposed
190 * mechanism to support that. Since it's going uncached, tell GEM that
191 * we're writing to it. The usual clflush should be all that's required
192 * to pick up the results.
193 */
194 OUT_RELOC(brw->query.bo,
195 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
196 PIPE_CONTROL_GLOBAL_GTT_WRITE |
197 ((brw->query.index * 2) * sizeof(uint64_t)));
198 OUT_BATCH(0);
199 OUT_BATCH(0);
200 ADVANCE_BATCH();
201
202 foreach(query, &brw->query.active_head) {
203 if (query->bo != brw->query.bo) {
204 if (query->bo != NULL)
205 brw_queryobj_get_results(query);
206 brw->sws->bo_reference(brw->query.bo);
207 query->bo = brw->query.bo;
208 query->first_index = brw->query.index;
209 }
210 query->last_index = brw->query.index;
211 }
212 brw->query.active = GL_TRUE;
213 }
214
215 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
216 void
217 brw_emit_query_end(struct brw_context *brw)
218 {
219 if (!brw->query.active)
220 return;
221
222 BEGIN_BATCH(4, IGNORE_CLIPRECTS);
223 OUT_BATCH(_3DSTATE_PIPE_CONTROL |
224 PIPE_CONTROL_DEPTH_STALL |
225 PIPE_CONTROL_WRITE_DEPTH_COUNT);
226 OUT_RELOC(brw->query.bo,
227 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
228 PIPE_CONTROL_GLOBAL_GTT_WRITE |
229 ((brw->query.index * 2 + 1) * sizeof(uint64_t)));
230 OUT_BATCH(0);
231 OUT_BATCH(0);
232 ADVANCE_BATCH();
233
234 brw->query.active = GL_FALSE;
235 brw->query.index++;
236 }
237
238 void brw_init_queryobj_functions(struct dd_function_table *functions)
239 {
240 functions->NewQueryObject = brw_new_query_object;
241 functions->DeleteQuery = brw_delete_query;
242 functions->BeginQuery = brw_begin_query;
243 functions->EndQuery = brw_end_query;
244 functions->CheckQuery = brw_check_query;
245 functions->WaitQuery = brw_wait_query;
246 }