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