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