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
25#include "r300_context.h"
26#include "r300_screen.h"
27#include "r300_emit.h"
28
29#include <stdio.h>
30
31static struct pipe_query *r300_create_query(struct pipe_context *pipe,
32                                            unsigned query_type,
33                                            unsigned index)
34{
35    struct r300_context *r300 = r300_context(pipe);
36    struct r300_screen *r300screen = r300->screen;
37    struct r300_query *q;
38
39    if (query_type != PIPE_QUERY_OCCLUSION_COUNTER &&
40        query_type != PIPE_QUERY_OCCLUSION_PREDICATE &&
41        query_type != PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE &&
42        query_type != PIPE_QUERY_GPU_FINISHED) {
43        return NULL;
44    }
45
46    q = CALLOC_STRUCT(r300_query);
47    if (!q)
48        return NULL;
49
50    q->type = query_type;
51
52    if (query_type == PIPE_QUERY_GPU_FINISHED) {
53        return (struct pipe_query*)q;
54    }
55
56    if (r300screen->caps.family == CHIP_RV530)
57        q->num_pipes = r300screen->info.r300_num_z_pipes;
58    else
59        q->num_pipes = r300screen->info.r300_num_gb_pipes;
60
61    q->buf = r300->rws->buffer_create(r300->rws,
62                                      r300screen->info.gart_page_size,
63                                      r300screen->info.gart_page_size,
64                                      RADEON_DOMAIN_GTT,
65                                      RADEON_FLAG_NO_INTERPROCESS_SHARING);
66    if (!q->buf) {
67        FREE(q);
68        return NULL;
69    }
70    return (struct pipe_query*)q;
71}
72
73static void r300_destroy_query(struct pipe_context* pipe,
74                               struct pipe_query* query)
75{
76    struct r300_query* q = r300_query(query);
77
78    pb_reference(&q->buf, NULL);
79    FREE(query);
80}
81
82void r300_resume_query(struct r300_context *r300,
83                       struct r300_query *query)
84{
85    r300->query_current = query;
86    r300_mark_atom_dirty(r300, &r300->query_start);
87}
88
89static bool r300_begin_query(struct pipe_context* pipe,
90                             struct pipe_query* query)
91{
92    struct r300_context* r300 = r300_context(pipe);
93    struct r300_query* q = r300_query(query);
94
95    if (q->type == PIPE_QUERY_GPU_FINISHED)
96        return true;
97
98    if (r300->query_current != NULL) {
99        fprintf(stderr, "r300: begin_query: "
100                "Some other query has already been started.\n");
101        assert(0);
102        return false;
103    }
104
105    q->num_results = 0;
106    r300_resume_query(r300, q);
107    return true;
108}
109
110void r300_stop_query(struct r300_context *r300)
111{
112    r300_emit_query_end(r300);
113    r300->query_current = NULL;
114}
115
116static bool r300_end_query(struct pipe_context* pipe,
117	                   struct pipe_query* query)
118{
119    struct r300_context* r300 = r300_context(pipe);
120    struct r300_query *q = r300_query(query);
121
122    if (q->type == PIPE_QUERY_GPU_FINISHED) {
123        pb_reference(&q->buf, NULL);
124        r300_flush(pipe, PIPE_FLUSH_ASYNC,
125                   (struct pipe_fence_handle**)&q->buf);
126        return true;
127    }
128
129    if (q != r300->query_current) {
130        fprintf(stderr, "r300: end_query: Got invalid query.\n");
131        assert(0);
132        return false;
133    }
134
135    r300_stop_query(r300);
136
137    return true;
138}
139
140static bool r300_get_query_result(struct pipe_context* pipe,
141                                  struct pipe_query* query,
142                                  bool wait,
143                                  union pipe_query_result *vresult)
144{
145    struct r300_context* r300 = r300_context(pipe);
146    struct r300_query *q = r300_query(query);
147    unsigned i;
148    uint32_t temp, *map;
149
150    if (q->type == PIPE_QUERY_GPU_FINISHED) {
151        if (wait) {
152            r300->rws->buffer_wait(r300->rws, q->buf, PIPE_TIMEOUT_INFINITE,
153                                   RADEON_USAGE_READWRITE);
154            vresult->b = TRUE;
155        } else {
156            vresult->b = r300->rws->buffer_wait(r300->rws, q->buf, 0, RADEON_USAGE_READWRITE);
157        }
158        return vresult->b;
159    }
160
161    map = r300->rws->buffer_map(r300->rws, q->buf, &r300->cs,
162                                PIPE_MAP_READ |
163                                (!wait ? PIPE_MAP_DONTBLOCK : 0));
164    if (!map)
165        return FALSE;
166
167    /* Sum up the results. */
168    temp = 0;
169    for (i = 0; i < q->num_results; i++) {
170        /* Convert little endian values written by GPU to CPU byte order */
171        temp += util_le32_to_cpu(*map);
172        map++;
173    }
174
175    if (q->type == PIPE_QUERY_OCCLUSION_PREDICATE ||
176        q->type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE) {
177        vresult->b = temp != 0;
178    } else {
179        vresult->u64 = temp;
180    }
181    return TRUE;
182}
183
184static void r300_render_condition(struct pipe_context *pipe,
185                                  struct pipe_query *query,
186                                  bool condition,
187                                  enum pipe_render_cond_flag mode)
188{
189    struct r300_context *r300 = r300_context(pipe);
190    union pipe_query_result result;
191    bool wait;
192
193    r300->skip_rendering = FALSE;
194
195    if (query) {
196        wait = mode == PIPE_RENDER_COND_WAIT ||
197               mode == PIPE_RENDER_COND_BY_REGION_WAIT;
198
199        if (r300_get_query_result(pipe, query, wait, &result)) {
200            if (r300_query(query)->type == PIPE_QUERY_OCCLUSION_PREDICATE ||
201                r300_query(query)->type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE) {
202                r300->skip_rendering = condition == result.b;
203            } else {
204                r300->skip_rendering = condition == !!result.u64;
205            }
206        }
207    }
208}
209
210static void
211r300_set_active_query_state(struct pipe_context *pipe, bool enable)
212{
213}
214
215void r300_init_query_functions(struct r300_context* r300)
216{
217    r300->context.create_query = r300_create_query;
218    r300->context.destroy_query = r300_destroy_query;
219    r300->context.begin_query = r300_begin_query;
220    r300->context.end_query = r300_end_query;
221    r300->context.get_query_result = r300_get_query_result;
222    r300->context.set_active_query_state = r300_set_active_query_state;
223    r300->context.render_condition = r300_render_condition;
224}
225