r300g: rewrite occlusion queries
[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 assert(query_type == PIPE_QUERY_OCCLUSION_COUNTER);
41
42 q = CALLOC_STRUCT(r300_query);
43 if (!q)
44 return NULL;
45
46 q->type = query_type;
47 q->domain = R300_DOMAIN_GTT;
48 q->buffer_size = 4096;
49
50 if (r300screen->caps.family == CHIP_FAMILY_RV530)
51 q->num_pipes = r300screen->caps.num_z_pipes;
52 else
53 q->num_pipes = r300screen->caps.num_frag_pipes;
54
55 insert_at_tail(&r300->query_list, q);
56
57 /* Open up the occlusion query buffer. */
58 q->buffer = r300->rws->buffer_create(r300->rws, 4096, 0, q->domain, q->buffer_size);
59
60 return (struct pipe_query*)q;
61 }
62
63 static void r300_destroy_query(struct pipe_context* pipe,
64 struct pipe_query* query)
65 {
66 struct r300_context *r300 = r300_context(pipe);
67 struct r300_query* q = r300_query(query);
68
69 r300->rws->buffer_reference(r300->rws, &q->buffer, NULL);
70 remove_from_list(q);
71 FREE(query);
72 }
73
74 void r300_resume_query(struct r300_context *r300,
75 struct r300_query *query)
76 {
77 r300->query_current = query;
78 r300->query_start.dirty = TRUE;
79 }
80
81 static void r300_begin_query(struct pipe_context* pipe,
82 struct pipe_query* query)
83 {
84 struct r300_context* r300 = r300_context(pipe);
85 struct r300_query* q = r300_query(query);
86
87 if (r300->query_current != NULL) {
88 fprintf(stderr, "r300: begin_query: "
89 "Some other query has already been started.\n");
90 assert(0);
91 return;
92 }
93
94 q->num_results = 0;
95 r300_resume_query(r300, q);
96 }
97
98 void r300_stop_query(struct r300_context *r300)
99 {
100 r300_emit_query_end(r300);
101 r300->query_current = NULL;
102 }
103
104 static void r300_end_query(struct pipe_context* pipe,
105 struct pipe_query* query)
106 {
107 struct r300_context* r300 = r300_context(pipe);
108 struct r300_query *q = r300_query(query);
109
110 if (q != r300->query_current) {
111 fprintf(stderr, "r300: end_query: Got invalid query.\n");
112 assert(0);
113 return;
114 }
115
116 r300_stop_query(r300);
117 }
118
119 static boolean r300_get_query_result(struct pipe_context* pipe,
120 struct pipe_query* query,
121 boolean wait,
122 void* vresult)
123 {
124 struct r300_context* r300 = r300_context(pipe);
125 struct r300_query *q = r300_query(query);
126 unsigned flags, i;
127 uint32_t temp, *map;
128 uint64_t *result = (uint64_t*)vresult;
129
130 if (!q->flushed)
131 pipe->flush(pipe, 0, NULL);
132
133 flags = PIPE_TRANSFER_READ | (!wait ? PIPE_TRANSFER_DONTBLOCK : 0);
134
135 map = r300->rws->buffer_map(r300->rws, q->buffer, flags);
136 if (!map)
137 return FALSE;
138
139 /* Sum up the results. */
140 temp = 0;
141 for (i = 0; i < q->num_results; i++) {
142 temp += *map;
143 map++;
144 }
145
146 r300->rws->buffer_unmap(r300->rws, q->buffer);
147
148 *result = temp;
149 return TRUE;
150 }
151
152 static void r300_render_condition(struct pipe_context *pipe,
153 struct pipe_query *query,
154 uint mode)
155 {
156 struct r300_context *r300 = r300_context(pipe);
157 uint64_t result;
158 boolean wait;
159
160 if (query) {
161 wait = mode == PIPE_RENDER_COND_WAIT ||
162 mode == PIPE_RENDER_COND_BY_REGION_WAIT;
163
164 if (!r300_get_query_result(pipe, query, wait, &result)) {
165 r300->skip_rendering = FALSE;
166 }
167
168 r300->skip_rendering = result == 0;
169 } else {
170 r300->skip_rendering = FALSE;
171 }
172 }
173
174 void r300_init_query_functions(struct r300_context* r300) {
175 r300->context.create_query = r300_create_query;
176 r300->context.destroy_query = r300_destroy_query;
177 r300->context.begin_query = r300_begin_query;
178 r300->context.end_query = r300_end_query;
179 r300->context.get_query_result = r300_get_query_result;
180 r300->context.render_condition = r300_render_condition;
181 }