gallium: adjust the query interface to support custom types
[profile/ivi/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     unsigned query_size;
38     struct r300_query *q, *qptr;
39
40     q = CALLOC_STRUCT(r300_query);
41
42     q->type = query_type;
43     assert(q->type == PIPE_QUERY_OCCLUSION_COUNTER);
44
45     if (r300screen->caps.family == CHIP_FAMILY_RV530)
46         query_size = r300screen->caps.num_z_pipes * sizeof(uint32_t);
47     else
48         query_size = r300screen->caps.num_frag_pipes * sizeof(uint32_t);
49
50     if (!is_empty_list(&r300->query_list)) {
51         qptr = last_elem(&r300->query_list);
52         q->offset = qptr->offset + query_size;
53     }
54     insert_at_tail(&r300->query_list, q);
55
56     /* XXX */
57     if (q->offset >= 4096) {
58         q->offset = 0;
59         fprintf(stderr, "r300: Rewinding OQBO...\n");
60     }
61
62     return (struct pipe_query*)q;
63 }
64
65 static void r300_destroy_query(struct pipe_context* pipe,
66                                struct pipe_query* query)
67 {
68     struct r300_query* q = (struct r300_query*)query;
69
70     remove_from_list(q);
71     FREE(query);
72 }
73
74 static void r300_begin_query(struct pipe_context* pipe,
75                              struct pipe_query* query)
76 {
77     uint32_t value = ~0U;
78     struct r300_context* r300 = r300_context(pipe);
79     struct r300_query* q = (struct r300_query*)query;
80
81     if (r300->query_current != NULL) {
82         fprintf(stderr, "r300: begin_query: "
83                 "Some other query has already been started.\n");
84         assert(0);
85         return;
86     }
87
88     pipe_buffer_write(pipe,
89                       r300->oqbo,
90                       q->offset,
91                       sizeof value,
92                       &value);
93
94     q->flushed = FALSE;
95     r300->query_current = q;
96     r300->query_start.dirty = TRUE;
97 }
98
99 static void r300_end_query(struct pipe_context* pipe,
100                            struct pipe_query* query)
101 {
102     struct r300_context* r300 = r300_context(pipe);
103
104     if ((struct r300_query*)query != r300->query_current) {
105         fprintf(stderr, "r300: end_query: Got invalid query.\n");
106         assert(0);
107         return;
108     }
109
110     r300_emit_query_end(r300);
111     r300->query_current = NULL;
112 }
113
114 static boolean r300_get_query_result(struct pipe_context* pipe,
115                                      struct pipe_query* query,
116                                      boolean wait,
117                                      void* vresult)
118 {
119     struct r300_context* r300 = r300_context(pipe);
120     struct r300_screen* r300screen = r300->screen;
121     struct r300_query *q = (struct r300_query*)query;
122     struct pipe_transfer *transfer;
123     unsigned flags = PIPE_TRANSFER_READ;
124     uint32_t* map;
125     uint32_t temp = 0;
126     unsigned i, num_results;
127     uint64_t *result = (uint64_t*)vresult;
128
129     if (q->flushed == FALSE)
130         pipe->flush(pipe, 0, NULL);
131     if (!wait) {
132         flags |= PIPE_TRANSFER_DONTBLOCK;
133     }
134
135     map = pipe_buffer_map(pipe, r300->oqbo, flags, &transfer);
136     if (!map)
137         return FALSE;
138     map += q->offset / 4;
139
140     if (r300screen->caps.family == CHIP_FAMILY_RV530)
141         num_results = r300screen->caps.num_z_pipes;
142     else
143         num_results = r300screen->caps.num_frag_pipes;
144
145     for (i = 0; i < num_results; i++) {
146         if (*map == ~0U) {
147             /* Looks like our results aren't ready yet. */
148             if (wait) {
149                 fprintf(stderr, "r300: Despite waiting, OQ results haven't "
150                                 "come in yet. This is a driver bug.\n"
151                                 "r300: Returning bogus results to avoid "
152                                 "a possible infinite loop...\n");
153                 temp = 987654321;
154             } else {
155                 temp = ~0U;
156             }
157             break;
158         }
159         temp += *map;
160         map++;
161     }
162     pipe_buffer_unmap(pipe, r300->oqbo, transfer);
163
164     if (temp == ~0U) {
165         /* Our results haven't been written yet... */
166         return FALSE;
167     }
168
169     *result = temp;
170     return TRUE;
171 }
172
173 static void r300_render_condition(struct pipe_context *pipe,
174                                   struct pipe_query *query,
175                                   uint mode)
176 {
177     struct r300_context *r300 = r300_context(pipe);
178     uint64_t result;
179     boolean wait;
180
181     if (query) {
182         wait = mode == PIPE_RENDER_COND_WAIT ||
183                mode == PIPE_RENDER_COND_BY_REGION_WAIT;
184
185         if (!r300_get_query_result(pipe, query, wait, &result)) {
186             r300->skip_rendering = FALSE;
187         }
188
189         r300->skip_rendering = result == 0;
190     } else {
191         r300->skip_rendering = FALSE;
192     }
193 }
194
195 void r300_init_query_functions(struct r300_context* r300) {
196     r300->context.create_query = r300_create_query;
197     r300->context.destroy_query = r300_destroy_query;
198     r300->context.begin_query = r300_begin_query;
199     r300->context.end_query = r300_end_query;
200     r300->context.get_query_result = r300_get_query_result;
201     r300->context.render_condition = r300_render_condition;
202 }