1 /*
2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
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 #include "util/u_memory.h"
24 #include "util/simple_list.h"
25
26 #include "r300_context.h"
27 #include "r300_screen.h"
28 #include "r300_emit.h"
29
30 #include <stdio.h>
31
r300_create_query(struct pipe_context * pipe,unsigned query_type,unsigned index)32 static struct pipe_query *r300_create_query(struct pipe_context *pipe,
33 unsigned query_type,
34 unsigned index)
35 {
36 struct r300_context *r300 = r300_context(pipe);
37 struct r300_screen *r300screen = r300->screen;
38 struct r300_query *q;
39
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) {
44 return NULL;
45 }
46
47 q = CALLOC_STRUCT(r300_query);
48 if (!q)
49 return NULL;
50
51 q->type = query_type;
52
53 if (query_type == PIPE_QUERY_GPU_FINISHED) {
54 return (struct pipe_query*)q;
55 }
56
57 if (r300screen->caps.family == CHIP_RV530)
58 q->num_pipes = r300screen->info.r300_num_z_pipes;
59 else
60 q->num_pipes = r300screen->info.r300_num_gb_pipes;
61
62 q->buf = r300->rws->buffer_create(r300->rws,
63 r300screen->info.gart_page_size,
64 r300screen->info.gart_page_size,
65 RADEON_DOMAIN_GTT,
66 RADEON_FLAG_NO_INTERPROCESS_SHARING);
67 if (!q->buf) {
68 FREE(q);
69 return NULL;
70 }
71 return (struct pipe_query*)q;
72 }
73
r300_destroy_query(struct pipe_context * pipe,struct pipe_query * query)74 static void r300_destroy_query(struct pipe_context* pipe,
75 struct pipe_query* query)
76 {
77 struct r300_query* q = r300_query(query);
78
79 pb_reference(&q->buf, NULL);
80 FREE(query);
81 }
82
r300_resume_query(struct r300_context * r300,struct r300_query * query)83 void r300_resume_query(struct r300_context *r300,
84 struct r300_query *query)
85 {
86 r300->query_current = query;
87 r300_mark_atom_dirty(r300, &r300->query_start);
88 }
89
r300_begin_query(struct pipe_context * pipe,struct pipe_query * query)90 static bool r300_begin_query(struct pipe_context* pipe,
91 struct pipe_query* query)
92 {
93 struct r300_context* r300 = r300_context(pipe);
94 struct r300_query* q = r300_query(query);
95
96 if (q->type == PIPE_QUERY_GPU_FINISHED)
97 return true;
98
99 if (r300->query_current != NULL) {
100 fprintf(stderr, "r300: begin_query: "
101 "Some other query has already been started.\n");
102 assert(0);
103 return false;
104 }
105
106 q->num_results = 0;
107 r300_resume_query(r300, q);
108 return true;
109 }
110
r300_stop_query(struct r300_context * r300)111 void r300_stop_query(struct r300_context *r300)
112 {
113 r300_emit_query_end(r300);
114 r300->query_current = NULL;
115 }
116
r300_end_query(struct pipe_context * pipe,struct pipe_query * query)117 static bool r300_end_query(struct pipe_context* pipe,
118 struct pipe_query* query)
119 {
120 struct r300_context* r300 = r300_context(pipe);
121 struct r300_query *q = r300_query(query);
122
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);
127 return true;
128 }
129
130 if (q != r300->query_current) {
131 fprintf(stderr, "r300: end_query: Got invalid query.\n");
132 assert(0);
133 return false;
134 }
135
136 r300_stop_query(r300);
137
138 return true;
139 }
140
r300_get_query_result(struct pipe_context * pipe,struct pipe_query * query,bool wait,union pipe_query_result * vresult)141 static bool r300_get_query_result(struct pipe_context* pipe,
142 struct pipe_query* query,
143 bool wait,
144 union pipe_query_result *vresult)
145 {
146 struct r300_context* r300 = r300_context(pipe);
147 struct r300_query *q = r300_query(query);
148 unsigned i;
149 uint32_t temp, *map;
150
151 if (q->type == PIPE_QUERY_GPU_FINISHED) {
152 if (wait) {
153 r300->rws->buffer_wait(q->buf, PIPE_TIMEOUT_INFINITE,
154 RADEON_USAGE_READWRITE);
155 vresult->b = TRUE;
156 } else {
157 vresult->b = r300->rws->buffer_wait(q->buf, 0, RADEON_USAGE_READWRITE);
158 }
159 return vresult->b;
160 }
161
162 map = r300->rws->buffer_map(q->buf, r300->cs,
163 PIPE_MAP_READ |
164 (!wait ? PIPE_MAP_DONTBLOCK : 0));
165 if (!map)
166 return FALSE;
167
168 /* Sum up the results. */
169 temp = 0;
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);
173 map++;
174 }
175
176 if (q->type == PIPE_QUERY_OCCLUSION_PREDICATE ||
177 q->type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE) {
178 vresult->b = temp != 0;
179 } else {
180 vresult->u64 = temp;
181 }
182 return TRUE;
183 }
184
r300_render_condition(struct pipe_context * pipe,struct pipe_query * query,bool condition,enum pipe_render_cond_flag mode)185 static void r300_render_condition(struct pipe_context *pipe,
186 struct pipe_query *query,
187 bool condition,
188 enum pipe_render_cond_flag mode)
189 {
190 struct r300_context *r300 = r300_context(pipe);
191 union pipe_query_result result;
192 bool wait;
193
194 r300->skip_rendering = FALSE;
195
196 if (query) {
197 wait = mode == PIPE_RENDER_COND_WAIT ||
198 mode == PIPE_RENDER_COND_BY_REGION_WAIT;
199
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;
204 } else {
205 r300->skip_rendering = condition == !!result.u64;
206 }
207 }
208 }
209 }
210
211 static void
r300_set_active_query_state(struct pipe_context * pipe,bool enable)212 r300_set_active_query_state(struct pipe_context *pipe, bool enable)
213 {
214 }
215
r300_init_query_functions(struct r300_context * r300)216 void r300_init_query_functions(struct r300_context* r300)
217 {
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;
225 }
226