2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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:
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
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.
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"
34 static struct radeon_state
*r600_query_begin(struct r600_context
*rctx
, struct r600_query
*rquery
)
36 struct r600_screen
*rscreen
= rctx
->screen
;
37 struct radeon_state
*rstate
;
39 rstate
= radeon_state(rscreen
->rw
, R600_QUERY_BEGIN_TYPE
, R600_QUERY_BEGIN
);
42 rstate
->states
[R600_QUERY__OFFSET
] = rquery
->num_results
;
43 rstate
->bo
[0] = radeon_bo_incref(rscreen
->rw
, rquery
->buffer
);
45 rstate
->placement
[0] = RADEON_GEM_DOMAIN_GTT
;
46 if (radeon_state_pm4(rstate
)) {
47 radeon_state_decref(rstate
);
53 static struct radeon_state
*r600_query_end(struct r600_context
*rctx
, struct r600_query
*rquery
)
55 struct r600_screen
*rscreen
= rctx
->screen
;
56 struct radeon_state
*rstate
;
58 rstate
= radeon_state(rscreen
->rw
, R600_QUERY_END_TYPE
, R600_QUERY_END
);
61 rstate
->states
[R600_QUERY__OFFSET
] = rquery
->num_results
+ 8;
62 rstate
->bo
[0] = radeon_bo_incref(rscreen
->rw
, rquery
->buffer
);
64 rstate
->placement
[0] = RADEON_GEM_DOMAIN_GTT
;
65 if (radeon_state_pm4(rstate
)) {
66 radeon_state_decref(rstate
);
72 static struct pipe_query
*r600_create_query(struct pipe_context
*ctx
, unsigned query_type
)
74 struct r600_screen
*rscreen
= r600_screen(ctx
->screen
);
75 struct r600_context
*rctx
= r600_context(ctx
);
78 if (query_type
!= PIPE_QUERY_OCCLUSION_COUNTER
)
81 q
= CALLOC_STRUCT(r600_query
);
86 LIST_ADDTAIL(&q
->list
, &rctx
->query_list
);
87 q
->buffer_size
= 4096;
89 q
->buffer
= radeon_bo(rscreen
->rw
, 0, q
->buffer_size
, 1, NULL
);
94 return (struct pipe_query
*)q
;
97 static void r600_destroy_query(struct pipe_context
*ctx
,
98 struct pipe_query
*query
)
100 struct r600_screen
*rscreen
= r600_screen(ctx
->screen
);
101 struct r600_query
*q
= r600_query(query
);
103 radeon_bo_decref(rscreen
->rw
, q
->buffer
);
108 static void r600_query_result(struct pipe_context
*ctx
, struct r600_query
*rquery
)
110 struct r600_screen
*rscreen
= r600_screen(ctx
->screen
);
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
;
125 radeon_bo_unmap(rscreen
->rw
, rquery
->buffer
);
126 rquery
->num_results
= 0;
129 static void r600_query_resume(struct pipe_context
*ctx
, struct r600_query
*rquery
)
131 struct r600_context
*rctx
= r600_context(ctx
);
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
);
138 r600_query_result(ctx
, rquery
);
140 rquery
->rstate
= radeon_state_decref(rquery
->rstate
);
141 rquery
->rstate
= r600_query_begin(rctx
, rquery
);
142 rquery
->flushed
= false;
145 static void r600_query_suspend(struct pipe_context
*ctx
, struct r600_query
*rquery
)
147 struct r600_context
*rctx
= r600_context(ctx
);
149 rquery
->rstate
= radeon_state_decref(rquery
->rstate
);
150 rquery
->rstate
= r600_query_end(rctx
, rquery
);
151 rquery
->num_results
+= 16;
154 static void r600_begin_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
156 struct r600_context
*rctx
= r600_context(ctx
);
157 struct r600_query
*rquery
= r600_query(query
);
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
);
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
);
173 static void r600_end_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
175 struct r600_context
*rctx
= r600_context(ctx
);
176 struct r600_query
*rquery
= r600_query(query
);
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
);
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
);
191 void r600_queries_suspend(struct pipe_context
*ctx
)
193 struct r600_context
*rctx
= r600_context(ctx
);
194 struct r600_query
*rquery
;
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
);
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
);
208 rquery
->state
|= R600_QUERY_STATE_SUSPENDED
;
212 void r600_queries_resume(struct pipe_context
*ctx
)
214 struct r600_context
*rctx
= r600_context(ctx
);
215 struct r600_query
*rquery
;
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
);
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
);
229 rquery
->state
&= ~R600_QUERY_STATE_SUSPENDED
;
233 static boolean
r600_get_query_result(struct pipe_context
*ctx
,
234 struct pipe_query
*query
,
235 boolean wait
, void *vresult
)
237 struct r600_query
*rquery
= r600_query(query
);
238 uint64_t *result
= (uint64_t*)vresult
;
240 if (!rquery
->flushed
) {
241 ctx
->flush(ctx
, 0, NULL
);
242 rquery
->flushed
= true;
244 r600_query_result(ctx
, rquery
);
245 *result
= rquery
->result
;
250 void r600_init_query_functions(struct r600_context
* rctx
)
252 LIST_INITHEAD(&rctx
->query_list
);
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
;