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