r300g/winsys: rename r300->radeon and do a little cleanup
[mesa.git] / src / gallium / drivers / r300 / r300_query.c
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/u_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
32 static struct pipe_query *r300_create_query(struct pipe_context *pipe,
33 unsigned query_type)
34 {
35 struct r300_context *r300 = r300_context(pipe);
36 struct r300_screen *r300screen = r300->screen;
37 struct r300_query *q;
38
39 if (query_type != PIPE_QUERY_OCCLUSION_COUNTER) {
40 return NULL;
41 }
42
43 q = CALLOC_STRUCT(r300_query);
44 if (!q)
45 return NULL;
46
47 q->type = query_type;
48 q->domain = RADEON_DOMAIN_GTT;
49 q->buffer_size = 4096;
50
51 if (r300screen->caps.family == CHIP_FAMILY_RV530)
52 q->num_pipes = r300screen->caps.num_z_pipes;
53 else
54 q->num_pipes = r300screen->caps.num_frag_pipes;
55
56 insert_at_tail(&r300->query_list, q);
57
58 /* Open up the occlusion query buffer. */
59 q->buf = r300->rws->buffer_create(r300->rws, q->buffer_size, 4096,
60 PIPE_BIND_CUSTOM, PIPE_USAGE_STREAM,
61 q->domain);
62 q->cs_buf = r300->rws->buffer_get_cs_handle(q->buf);
63
64 return (struct pipe_query*)q;
65 }
66
67 static void r300_destroy_query(struct pipe_context* pipe,
68 struct pipe_query* query)
69 {
70 struct r300_query* q = r300_query(query);
71
72 pb_reference(&q->buf, NULL);
73 remove_from_list(q);
74 FREE(query);
75 }
76
77 void r300_resume_query(struct r300_context *r300,
78 struct r300_query *query)
79 {
80 r300->query_current = query;
81 r300_mark_atom_dirty(r300, &r300->query_start);
82 }
83
84 static void r300_begin_query(struct pipe_context* pipe,
85 struct pipe_query* query)
86 {
87 struct r300_context* r300 = r300_context(pipe);
88 struct r300_query* q = r300_query(query);
89
90 if (r300->query_current != NULL) {
91 fprintf(stderr, "r300: begin_query: "
92 "Some other query has already been started.\n");
93 assert(0);
94 return;
95 }
96
97 q->num_results = 0;
98 r300_resume_query(r300, q);
99 }
100
101 void r300_stop_query(struct r300_context *r300)
102 {
103 r300_emit_query_end(r300);
104 r300->query_current = NULL;
105 }
106
107 static void r300_end_query(struct pipe_context* pipe,
108 struct pipe_query* query)
109 {
110 struct r300_context* r300 = r300_context(pipe);
111 struct r300_query *q = r300_query(query);
112
113 if (q != r300->query_current) {
114 fprintf(stderr, "r300: end_query: Got invalid query.\n");
115 assert(0);
116 return;
117 }
118
119 r300_stop_query(r300);
120 }
121
122 static boolean r300_get_query_result(struct pipe_context* pipe,
123 struct pipe_query* query,
124 boolean wait,
125 void* vresult)
126 {
127 struct r300_context* r300 = r300_context(pipe);
128 struct r300_query *q = r300_query(query);
129 unsigned i;
130 uint32_t temp, *map;
131
132 map = r300->rws->buffer_map(q->buf, r300->cs,
133 PIPE_TRANSFER_READ |
134 (!wait ? PIPE_TRANSFER_DONTBLOCK : 0));
135 if (!map)
136 return FALSE;
137
138 /* Sum up the results. */
139 temp = 0;
140 for (i = 0; i < q->num_results; i++) {
141 temp += *map;
142 map++;
143 }
144
145 r300->rws->buffer_unmap(q->buf);
146
147 *((uint64_t*)vresult) = temp;
148 return TRUE;
149 }
150
151 static void r300_render_condition(struct pipe_context *pipe,
152 struct pipe_query *query,
153 uint mode)
154 {
155 struct r300_context *r300 = r300_context(pipe);
156 uint64_t result = 0;
157 boolean wait;
158
159 if (query) {
160 wait = mode == PIPE_RENDER_COND_WAIT ||
161 mode == PIPE_RENDER_COND_BY_REGION_WAIT;
162
163 if (!r300_get_query_result(pipe, query, wait, &result)) {
164 r300->skip_rendering = FALSE;
165 } else {
166 r300->skip_rendering = result == 0;
167 }
168 } else {
169 r300->skip_rendering = FALSE;
170 }
171 }
172
173 /***************************************************************************
174 * Fake occlusion queries (for debugging)
175 ***************************************************************************/
176
177 static unsigned r300_fake_query;
178
179 static struct pipe_query *r300_fake_create_query(struct pipe_context *pipe,
180 unsigned query_type)
181 {
182 return (struct pipe_query*)&r300_fake_query;
183 }
184
185 static void r300_fake_destroy_query(struct pipe_context* pipe,
186 struct pipe_query* query)
187 {
188 }
189
190 static void r300_fake_begin_query(struct pipe_context* pipe,
191 struct pipe_query* query)
192 {
193 }
194
195 static void r300_fake_end_query(struct pipe_context* pipe,
196 struct pipe_query* query)
197 {
198 }
199
200 static boolean r300_fake_get_query_result(struct pipe_context* pipe,
201 struct pipe_query* query,
202 boolean wait, void* vresult)
203 {
204 uint64_t *result = (uint64_t*)vresult;
205 *result = 1000000;
206 return TRUE;
207 }
208
209 static void r300_fake_render_condition(struct pipe_context *pipe,
210 struct pipe_query *query, uint mode)
211 {
212 }
213
214 void r300_init_query_functions(struct r300_context* r300) {
215 if (DBG_ON(r300, DBG_FAKE_OCC)) {
216 r300->context.create_query = r300_fake_create_query;
217 r300->context.destroy_query = r300_fake_destroy_query;
218 r300->context.begin_query = r300_fake_begin_query;
219 r300->context.end_query = r300_fake_end_query;
220 r300->context.get_query_result = r300_fake_get_query_result;
221 r300->context.render_condition = r300_fake_render_condition;
222 } else {
223 r300->context.create_query = r300_create_query;
224 r300->context.destroy_query = r300_destroy_query;
225 r300->context.begin_query = r300_begin_query;
226 r300->context.end_query = r300_end_query;
227 r300->context.get_query_result = r300_get_query_result;
228 r300->context.render_condition = r300_render_condition;
229 }
230 }