Merge branch 'mesa_7_6_branch'
[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 "r300_query.h"
24
25 #include "r300_emit.h"
26
27 static struct pipe_query* r300_create_query(struct pipe_context* pipe,
28                                             unsigned query_type)
29 {
30     struct r300_context* r300 = r300_context(pipe);
31     struct r300_screen* r300screen = r300_screen(r300->context.screen);
32     unsigned query_size = r300screen->caps->num_frag_pipes * 4;
33     struct r300_query* q, * qptr;
34
35     q = CALLOC_STRUCT(r300_query);
36
37     q->type = query_type;
38     assert(q->type == PIPE_QUERY_OCCLUSION_COUNTER);
39
40     q->active = FALSE;
41
42     if (!r300->query_list) {
43         r300->query_list = q;
44     } else if (!is_empty_list(r300->query_list)) {
45         qptr = last_elem(r300->query_list);
46         q->offset = qptr->offset + query_size;
47         insert_at_tail(r300->query_list, q);
48     }
49
50     /* XXX */
51     if (q->offset >= 4096) {
52         q->offset = 0;
53     }
54
55     return (struct pipe_query*)q;
56 }
57
58 static void r300_destroy_query(struct pipe_context* pipe,
59                                struct pipe_query* query)
60 {
61     struct r300_query* q = (struct r300_query*)query;
62
63     remove_from_list(q);
64     FREE(query);
65 }
66
67 static void r300_begin_query(struct pipe_context* pipe,
68                              struct pipe_query* query)
69 {
70     uint32_t* map;
71     struct r300_context* r300 = r300_context(pipe);
72     struct r300_query* q = (struct r300_query*)query;
73
74     map = pipe->screen->buffer_map(pipe->screen, r300->oqbo,
75             PIPE_BUFFER_USAGE_CPU_WRITE);
76     map += q->offset / 4;
77     *map = ~0;
78     pipe->screen->buffer_unmap(pipe->screen, r300->oqbo);
79
80     r300_emit_dirty_state(r300);
81     r300_emit_query_begin(r300, q);
82 }
83
84 static void r300_end_query(struct pipe_context* pipe,
85                            struct pipe_query* query)
86 {
87     struct r300_context* r300 = r300_context(pipe);
88     struct r300_query* q = (struct r300_query*)query;
89
90     r300_emit_dirty_state(r300);
91     r300_emit_query_end(r300, q);
92 }
93
94 static boolean r300_get_query_result(struct pipe_context* pipe,
95                                      struct pipe_query* query,
96                                      boolean wait,
97                                      uint64_t* result)
98 {
99     struct r300_context* r300 = r300_context(pipe);
100     struct r300_screen* r300screen = r300_screen(r300->context.screen);
101     struct r300_query* q = (struct r300_query*)query;
102     unsigned flags = PIPE_BUFFER_USAGE_CPU_READ;
103     uint32_t* map;
104     uint32_t temp;
105     unsigned i;
106
107     if (wait) {
108         pipe->flush(pipe, 0, NULL);
109     } else {
110         flags |= PIPE_BUFFER_USAGE_DONTBLOCK;
111     }
112
113     map = pipe->screen->buffer_map(pipe->screen, r300->oqbo, flags);
114     map += q->offset / 4;
115     for (i = 0; i < r300screen->caps->num_frag_pipes; i++) {
116         if (*map == ~0) {
117             /* Looks like our results aren't ready yet. */
118             if (wait) {
119                 debug_printf("r300: Despite waiting, OQ results haven't"
120                         " come in yet.\n");
121             }
122             temp = ~0;
123             break;
124         }
125         temp += *map;
126         map++;
127     }
128     pipe->screen->buffer_unmap(pipe->screen, r300->oqbo);
129
130     if (temp == ~0) {
131         /* Our results haven't been written yet... */
132         return FALSE;
133     }
134
135     *result = temp;
136     return TRUE;
137 }
138
139 void r300_init_query_functions(struct r300_context* r300) {
140     r300->context.create_query = r300_create_query;
141     r300->context.destroy_query = r300_destroy_query;
142     r300->context.begin_query = r300_begin_query;
143     r300->context.end_query = r300_end_query;
144     r300->context.get_query_result = r300_get_query_result;
145 }