summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/r600/r600_query.c
blob: 8b4fe8999f33232d5ad520af3f70a54f0d04b588 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *      Jerome Glisse
 *      Corbin Simpson
 */
#include <errno.h>
#include <util/u_inlines.h>
#include <util/u_format.h>
#include <util/u_memory.h>
#include "r600_screen.h"
#include "r600_context.h"

static struct radeon_state *r600_query_begin(struct r600_context *rctx, struct r600_query *rquery)
{
	struct r600_screen *rscreen = rctx->screen;
	struct radeon_state *rstate;

	rstate = radeon_state(rscreen->rw, R600_QUERY_BEGIN);
	if (rstate == NULL)
		return NULL;
	rstate->states[R600_QUERY__OFFSET] = rquery->num_results;
	rstate->reloc_pm4_id[0] = R600_QUERY__BO_ID;
	rstate->bo[0] = radeon_bo_incref(rscreen->rw, rquery->buffer);
	rstate->nbo = 1;
	rstate->placement[0] = RADEON_GEM_DOMAIN_GTT;
	if (radeon_state_pm4(rstate)) {
		radeon_state_decref(rstate);
		return NULL;
	}
	return rstate;
}

static struct radeon_state *r600_query_end(struct r600_context *rctx, struct r600_query *rquery)
{
	struct r600_screen *rscreen = rctx->screen;
	struct radeon_state *rstate;

	rstate = radeon_state(rscreen->rw, R600_QUERY_END);
	if (rstate == NULL)
		return NULL;
	rstate->states[R600_QUERY__OFFSET] = rquery->num_results + 8;
	rstate->reloc_pm4_id[0] = R600_QUERY__BO_ID;
	rstate->bo[0] = radeon_bo_incref(rscreen->rw, rquery->buffer);
	rstate->nbo = 1;
	rstate->placement[0] = RADEON_GEM_DOMAIN_GTT;
	if (radeon_state_pm4(rstate)) {
		radeon_state_decref(rstate);
		return NULL;
	}
	return rstate;
}

static struct pipe_query *r600_create_query(struct pipe_context *ctx, unsigned query_type)
{
	struct r600_screen *rscreen = r600_screen(ctx->screen);
	struct r600_context *rctx = r600_context(ctx);
	struct r600_query *q;

	if (query_type != PIPE_QUERY_OCCLUSION_COUNTER)
		return NULL;

	q = CALLOC_STRUCT(r600_query);
	if (!q)
		return NULL;

	q->type = query_type;
	LIST_ADDTAIL(&q->list, &rctx->query_list);
	q->buffer_size = 4096;

	q->buffer = radeon_bo(rscreen->rw, 0, q->buffer_size, 1, NULL);
	if (!q->buffer) {
		FREE(q);
		return NULL;
	}
	return (struct pipe_query *)q;
}

static void r600_destroy_query(struct pipe_context *ctx,
			       struct pipe_query *query)
{
	struct r600_screen *rscreen = r600_screen(ctx->screen);
	struct r600_query *q = r600_query(query);

	radeon_bo_decref(rscreen->rw, q->buffer);
	LIST_DEL(&q->list);
	FREE(query);
}

static void r600_query_result(struct pipe_context *ctx, struct r600_query *rquery)
{
	struct r600_screen *rscreen = r600_screen(ctx->screen);
	u64 start, end;
	u32 *results;
	int i;

	radeon_bo_wait(rscreen->rw, rquery->buffer);
	radeon_bo_map(rscreen->rw, rquery->buffer);
	results = rquery->buffer->data;
	for (i = 0; i < rquery->num_results; i += 4) {
		start = (u64)results[i] | (u64)results[i + 1] << 32;
		end = (u64)results[i + 2] | (u64)results[i + 3] << 32;
		if ((start & 0x8000000000000000UL) && (end & 0x8000000000000000UL)) {
			rquery->result += end - start;
		}
	}
	radeon_bo_unmap(rscreen->rw, rquery->buffer);
	rquery->num_results = 0;
}

static void r600_query_resume(struct pipe_context *ctx, struct r600_query *rquery)
{
	struct r600_context *rctx = r600_context(ctx);

	if (rquery->num_results >= ((rquery->buffer_size >> 2) - 2)) {
		/* running out of space */
		if (!rquery->flushed) {
			ctx->flush(ctx, 0, NULL);
		}
		r600_query_result(ctx, rquery);
	}
	rquery->rstate = radeon_state_decref(rquery->rstate);
	rquery->rstate = r600_query_begin(rctx, rquery);
	rquery->flushed = false;
}

static void r600_query_suspend(struct pipe_context *ctx, struct r600_query *rquery)
{
	struct r600_context *rctx = r600_context(ctx);

	rquery->rstate = radeon_state_decref(rquery->rstate);
	rquery->rstate = r600_query_end(rctx, rquery);
	rquery->num_results += 16;
}

static void r600_begin_query(struct pipe_context *ctx, struct pipe_query *query)
{
	struct r600_context *rctx = r600_context(ctx);
	struct r600_query *rquery = r600_query(query);
	int r;

	rquery->state = R600_QUERY_STATE_STARTED;
	rquery->num_results = 0;
	rquery->flushed = false;
	r600_query_resume(ctx, rquery);
	r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
	if (r == -EBUSY) {
		/* this shouldn't happen */
		R600_ERR("had to flush while emitting end query\n");
		ctx->flush(ctx, 0, NULL);
		r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
	}
}

static void r600_end_query(struct pipe_context *ctx, struct pipe_query *query)
{
	struct r600_context *rctx = r600_context(ctx);
	struct r600_query *rquery = r600_query(query);
	int r;

	rquery->state &= ~R600_QUERY_STATE_STARTED;
	rquery->state |= R600_QUERY_STATE_ENDED;
	r600_query_suspend(ctx, rquery);
	r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
	if (r == -EBUSY) {
		/* this shouldn't happen */
		R600_ERR("had to flush while emitting end query\n");
		ctx->flush(ctx, 0, NULL);
		r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
	}
}

void r600_queries_suspend(struct pipe_context *ctx)
{
	struct r600_context *rctx = r600_context(ctx);
	struct r600_query *rquery;
	int r;

	LIST_FOR_EACH_ENTRY(rquery, &rctx->query_list, list) {
		if (rquery->state & R600_QUERY_STATE_STARTED) {
			r600_query_suspend(ctx, rquery);
			r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
			if (r == -EBUSY) {
				/* this shouldn't happen */
				R600_ERR("had to flush while emitting end query\n");
				ctx->flush(ctx, 0, NULL);
				r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
			}
		}
		rquery->state |= R600_QUERY_STATE_SUSPENDED;
	}
}

void r600_queries_resume(struct pipe_context *ctx)
{
	struct r600_context *rctx = r600_context(ctx);
	struct r600_query *rquery;
	int r;

	LIST_FOR_EACH_ENTRY(rquery, &rctx->query_list, list) {
		if (rquery->state & R600_QUERY_STATE_STARTED) {
			r600_query_resume(ctx, rquery);
			r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
			if (r == -EBUSY) {
				/* this shouldn't happen */
				R600_ERR("had to flush while emitting end query\n");
				ctx->flush(ctx, 0, NULL);
				r = radeon_ctx_set_query_state(rctx->ctx, rquery->rstate);
			}
		}
		rquery->state &= ~R600_QUERY_STATE_SUSPENDED;
	}
}

static boolean r600_get_query_result(struct pipe_context *ctx,
					struct pipe_query *query,
					boolean wait, void *vresult)
{
	struct r600_query *rquery = r600_query(query);
	uint64_t *result = (uint64_t*)vresult;

	if (!rquery->flushed) {
		ctx->flush(ctx, 0, NULL);
		rquery->flushed = true;
	}
	r600_query_result(ctx, rquery);
	*result = rquery->result;
	rquery->result = 0;
	return TRUE;
}

void r600_init_query_functions(struct r600_context* rctx)
{
	LIST_INITHEAD(&rctx->query_list);

	rctx->context.create_query = r600_create_query;
	rctx->context.destroy_query = r600_destroy_query;
	rctx->context.begin_query = r600_begin_query;
	rctx->context.end_query = r600_end_query;
	rctx->context.get_query_result = r600_get_query_result;
}