2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
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. */
23 #include "util/u_memory.h"
24 #include "util/simple_list.h"
26 #include "r300_context.h"
27 #include "r300_screen.h"
28 #include "r300_emit.h"
32 static struct pipe_query
*r300_create_query(struct pipe_context
*pipe
,
36 struct r300_context
*r300
= r300_context(pipe
);
37 struct r300_screen
*r300screen
= r300
->screen
;
40 if (query_type
!= PIPE_QUERY_OCCLUSION_COUNTER
&&
41 query_type
!= PIPE_QUERY_OCCLUSION_PREDICATE
&&
42 query_type
!= PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
&&
43 query_type
!= PIPE_QUERY_GPU_FINISHED
) {
47 q
= CALLOC_STRUCT(r300_query
);
53 if (query_type
== PIPE_QUERY_GPU_FINISHED
) {
54 return (struct pipe_query
*)q
;
57 if (r300screen
->caps
.family
== CHIP_RV530
)
58 q
->num_pipes
= r300screen
->info
.r300_num_z_pipes
;
60 q
->num_pipes
= r300screen
->info
.r300_num_gb_pipes
;
62 q
->buf
= r300
->rws
->buffer_create(r300
->rws
,
63 r300screen
->info
.gart_page_size
,
64 r300screen
->info
.gart_page_size
,
66 RADEON_FLAG_NO_INTERPROCESS_SHARING
);
71 return (struct pipe_query
*)q
;
74 static void r300_destroy_query(struct pipe_context
* pipe
,
75 struct pipe_query
* query
)
77 struct r300_query
* q
= r300_query(query
);
79 pb_reference(&q
->buf
, NULL
);
83 void r300_resume_query(struct r300_context
*r300
,
84 struct r300_query
*query
)
86 r300
->query_current
= query
;
87 r300_mark_atom_dirty(r300
, &r300
->query_start
);
90 static bool r300_begin_query(struct pipe_context
* pipe
,
91 struct pipe_query
* query
)
93 struct r300_context
* r300
= r300_context(pipe
);
94 struct r300_query
* q
= r300_query(query
);
96 if (q
->type
== PIPE_QUERY_GPU_FINISHED
)
99 if (r300
->query_current
!= NULL
) {
100 fprintf(stderr
, "r300: begin_query: "
101 "Some other query has already been started.\n");
107 r300_resume_query(r300
, q
);
111 void r300_stop_query(struct r300_context
*r300
)
113 r300_emit_query_end(r300
);
114 r300
->query_current
= NULL
;
117 static bool r300_end_query(struct pipe_context
* pipe
,
118 struct pipe_query
* query
)
120 struct r300_context
* r300
= r300_context(pipe
);
121 struct r300_query
*q
= r300_query(query
);
123 if (q
->type
== PIPE_QUERY_GPU_FINISHED
) {
124 pb_reference(&q
->buf
, NULL
);
125 r300_flush(pipe
, PIPE_FLUSH_ASYNC
,
126 (struct pipe_fence_handle
**)&q
->buf
);
130 if (q
!= r300
->query_current
) {
131 fprintf(stderr
, "r300: end_query: Got invalid query.\n");
136 r300_stop_query(r300
);
141 static bool r300_get_query_result(struct pipe_context
* pipe
,
142 struct pipe_query
* query
,
144 union pipe_query_result
*vresult
)
146 struct r300_context
* r300
= r300_context(pipe
);
147 struct r300_query
*q
= r300_query(query
);
151 if (q
->type
== PIPE_QUERY_GPU_FINISHED
) {
153 r300
->rws
->buffer_wait(q
->buf
, PIPE_TIMEOUT_INFINITE
,
154 RADEON_USAGE_READWRITE
);
157 vresult
->b
= r300
->rws
->buffer_wait(q
->buf
, 0, RADEON_USAGE_READWRITE
);
162 map
= r300
->rws
->buffer_map(q
->buf
, r300
->cs
,
164 (!wait
? PIPE_TRANSFER_DONTBLOCK
: 0));
168 /* Sum up the results. */
170 for (i
= 0; i
< q
->num_results
; i
++) {
171 /* Convert little endian values written by GPU to CPU byte order */
172 temp
+= util_le32_to_cpu(*map
);
176 if (q
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
177 q
->type
== PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
) {
178 vresult
->b
= temp
!= 0;
185 static void r300_render_condition(struct pipe_context
*pipe
,
186 struct pipe_query
*query
,
188 enum pipe_render_cond_flag mode
)
190 struct r300_context
*r300
= r300_context(pipe
);
191 union pipe_query_result result
;
194 r300
->skip_rendering
= FALSE
;
197 wait
= mode
== PIPE_RENDER_COND_WAIT
||
198 mode
== PIPE_RENDER_COND_BY_REGION_WAIT
;
200 if (r300_get_query_result(pipe
, query
, wait
, &result
)) {
201 if (r300_query(query
)->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
202 r300_query(query
)->type
== PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
) {
203 r300
->skip_rendering
= condition
== result
.b
;
205 r300
->skip_rendering
= condition
== !!result
.u64
;
212 r300_set_active_query_state(struct pipe_context
*pipe
, bool enable
)
216 void r300_init_query_functions(struct r300_context
* r300
)
218 r300
->context
.create_query
= r300_create_query
;
219 r300
->context
.destroy_query
= r300_destroy_query
;
220 r300
->context
.begin_query
= r300_begin_query
;
221 r300
->context
.end_query
= r300_end_query
;
222 r300
->context
.get_query_result
= r300_get_query_result
;
223 r300
->context
.set_active_query_state
= r300_set_active_query_state
;
224 r300
->context
.render_condition
= r300_render_condition
;