r600g: add occlusion query support
[mesa.git] / src / gallium / drivers / r600 / r600_query.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 * Corbin Simpson
26 */
27 #include <errno.h>
28 #include <util/u_inlines.h>
29 #include <util/u_format.h>
30 #include <util/u_memory.h>
31 #include "r600_screen.h"
32 #include "r600_context.h"
33
34 static struct radeon_state *r600_query_begin(struct r600_context *rctx, struct r600_query *rquery)
35 {
36 struct r600_screen *rscreen = rctx->screen;
37 struct radeon_state *rstate;
38
39 rstate = radeon_state(rscreen->rw, R600_QUERY_BEGIN_TYPE, R600_QUERY_BEGIN);
40 if (rstate == NULL)
41 return NULL;
42 rstate->states[R600_QUERY__OFFSET] = rquery->num_results;
43 rstate->bo[0] = radeon_bo_incref(rscreen->rw, rquery->buffer);
44 rstate->nbo = 1;
45 rstate->placement[0] = RADEON_GEM_DOMAIN_GTT;
46 if (radeon_state_pm4(rstate)) {
47 radeon_state_decref(rstate);
48 return NULL;
49 }
50 return rstate;
51 }
52
53 static struct radeon_state *r600_query_end(struct r600_context *rctx, struct r600_query *rquery)
54 {
55 struct r600_screen *rscreen = rctx->screen;
56 struct radeon_state *rstate;
57
58 rstate = radeon_state(rscreen->rw, R600_QUERY_END_TYPE, R600_QUERY_END);
59 if (rstate == NULL)
60 return NULL;
61 rstate->states[R600_QUERY__OFFSET] = rquery->num_results + 8;
62 rstate->bo[0] = radeon_bo_incref(rscreen->rw, rquery->buffer);
63 rstate->nbo = 1;
64 rstate->placement[0] = RADEON_GEM_DOMAIN_GTT;
65 if (radeon_state_pm4(rstate)) {
66 radeon_state_decref(rstate);
67 return NULL;
68 }
69 return rstate;
70 }
71
72 static struct pipe_query *r600_create_query(struct pipe_context *ctx, unsigned query_type)
73 {
74 struct r600_screen *rscreen = r600_screen(ctx->screen);
75 struct r600_context *rctx = r600_context(ctx);
76 struct r600_query *q;
77
78 if (query_type != PIPE_QUERY_OCCLUSION_COUNTER)
79 return NULL;
80
81 q = CALLOC_STRUCT(r600_query);
82 if (!q)
83 return NULL;
84
85 q->type = query_type;
86 LIST_ADDTAIL(&q->list, &rctx->query_list);
87 q->buffer_size = 4096;
88
89 q->buffer = radeon_bo(rscreen->rw, 0, q->buffer_size, 1, NULL);
90 if (!q->buffer) {
91 FREE(q);
92 return NULL;
93 }
94 return (struct pipe_query *)q;
95 }
96
97 static void r600_destroy_query(struct pipe_context *ctx,
98 struct pipe_query *query)
99 {
100 struct r600_screen *rscreen = r600_screen(ctx->screen);
101 struct r600_query *q = r600_query(query);
102
103 radeon_bo_decref(rscreen->rw, q->buffer);
104 LIST_DEL(&q->list);
105 FREE(query);
106 }
107
108 static void r600_query_result(struct pipe_context *ctx, struct r600_query *rquery)
109 {
110 struct r600_screen *rscreen = r600_screen(ctx->screen);
111 u64 start, end;
112 u32 *results;
113 int i;
114
115 radeon_bo_wait(rscreen->rw, rquery->buffer);
116 radeon_bo_map(rscreen->rw, rquery->buffer);
117 results = rquery->buffer->data;
118 for (i = 0; i < rquery->num_results; i += 4) {
119 start = (u64)results[i] | (u64)results[i + 1] << 32;
120 end = (u64)results[i + 2] | (u64)results[i + 3] << 32;
121 if ((start & 0x8000000000000000UL) && (end & 0x8000000000000000UL)) {
122 rquery->result += end - start;
123 }
124 }
125 radeon_bo_unmap(rscreen->rw, rquery->buffer);
126 rquery->num_results = 0;
127 }
128
129 static void r600_query_resume(struct pipe_context *ctx, struct r600_query *rquery)
130 {
131 struct r600_context *rctx = r600_context(ctx);
132
133 if (rquery->num_results >= ((rquery->buffer_size >> 2) - 2)) {
134 /* running out of space */
135 if (!rquery->flushed) {
136 ctx->flush(ctx, 0, NULL);
137 }
138 r600_query_result(ctx, rquery);
139 }
140 rquery->rstate = radeon_state_decref(rquery->rstate);
141 rquery->rstate = r600_query_begin(rctx, rquery);
142 rquery->flushed = false;
143 }
144
145 static void r600_query_suspend(struct pipe_context *ctx, struct r600_query *rquery)
146 {
147 struct r600_context *rctx = r600_context(ctx);
148
149 rquery->rstate = radeon_state_decref(rquery->rstate);
150 rquery->rstate = r600_query_end(rctx, rquery);
151 rquery->num_results += 16;
152 }
153
154 static void r600_begin_query(struct pipe_context *ctx, struct pipe_query *query)
155 {
156 struct r600_context *rctx = r600_context(ctx);
157 struct r600_query *rquery = r600_query(query);
158 int r;
159
160 rquery->state = R600_QUERY_STATE_STARTED;
161 rquery->num_results = 0;
162 rquery->flushed = false;
163 r600_query_resume(ctx, rquery);
164 r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
165 if (r == -EBUSY) {
166 /* this shouldn't happen */
167 R600_ERR("had to flush while emitting end query\n");
168 ctx->flush(ctx, 0, NULL);
169 r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
170 }
171 }
172
173 static void r600_end_query(struct pipe_context *ctx, struct pipe_query *query)
174 {
175 struct r600_context *rctx = r600_context(ctx);
176 struct r600_query *rquery = r600_query(query);
177 int r;
178
179 rquery->state &= ~R600_QUERY_STATE_STARTED;
180 rquery->state |= R600_QUERY_STATE_ENDED;
181 r600_query_suspend(ctx, rquery);
182 r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
183 if (r == -EBUSY) {
184 /* this shouldn't happen */
185 R600_ERR("had to flush while emitting end query\n");
186 ctx->flush(ctx, 0, NULL);
187 r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
188 }
189 }
190
191 void r600_queries_suspend(struct pipe_context *ctx)
192 {
193 struct r600_context *rctx = r600_context(ctx);
194 struct r600_query *rquery;
195 int r;
196
197 LIST_FOR_EACH_ENTRY(rquery, &rctx->query_list, list) {
198 if (rquery->state & R600_QUERY_STATE_STARTED) {
199 r600_query_suspend(ctx, rquery);
200 r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
201 if (r == -EBUSY) {
202 /* this shouldn't happen */
203 R600_ERR("had to flush while emitting end query\n");
204 ctx->flush(ctx, 0, NULL);
205 r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
206 }
207 }
208 rquery->state |= R600_QUERY_STATE_SUSPENDED;
209 }
210 }
211
212 void r600_queries_resume(struct pipe_context *ctx)
213 {
214 struct r600_context *rctx = r600_context(ctx);
215 struct r600_query *rquery;
216 int r;
217
218 LIST_FOR_EACH_ENTRY(rquery, &rctx->query_list, list) {
219 if (rquery->state & R600_QUERY_STATE_STARTED) {
220 r600_query_resume(ctx, rquery);
221 r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
222 if (r == -EBUSY) {
223 /* this shouldn't happen */
224 R600_ERR("had to flush while emitting end query\n");
225 ctx->flush(ctx, 0, NULL);
226 r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
227 }
228 }
229 rquery->state &= ~R600_QUERY_STATE_SUSPENDED;
230 }
231 }
232
233 static boolean r600_get_query_result(struct pipe_context *ctx,
234 struct pipe_query *query,
235 boolean wait, void *vresult)
236 {
237 struct r600_query *rquery = r600_query(query);
238 uint64_t *result = (uint64_t*)vresult;
239
240 if (!rquery->flushed) {
241 ctx->flush(ctx, 0, NULL);
242 rquery->flushed = true;
243 }
244 r600_query_result(ctx, rquery);
245 *result = rquery->result;
246 rquery->result = 0;
247 return TRUE;
248 }
249
250 void r600_init_query_functions(struct r600_context* rctx)
251 {
252 LIST_INITHEAD(&rctx->query_list);
253
254 rctx->context.create_query = r600_create_query;
255 rctx->context.destroy_query = r600_destroy_query;
256 rctx->context.begin_query = r600_begin_query;
257 rctx->context.end_query = r600_end_query;
258 rctx->context.get_query_result = r600_get_query_result;
259 }