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