r300g: convert query to a state for emitting.
[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;
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 (r300screen->caps->family == CHIP_FAMILY_RV530)
43 query_size = r300screen->caps->num_z_pipes * sizeof(uint32_t);
44 else
45 query_size = r300screen->caps->num_frag_pipes * sizeof(uint32_t);
46
47 if (!is_empty_list(&r300->query_list)) {
48 qptr = last_elem(&r300->query_list);
49 q->offset = qptr->offset + query_size;
50 }
51 insert_at_tail(&r300->query_list, q);
52
53 /* XXX */
54 if (q->offset >= 4096) {
55 q->offset = 0;
56 }
57
58 return (struct pipe_query*)q;
59 }
60
61 static void r300_destroy_query(struct pipe_context* pipe,
62 struct pipe_query* query)
63 {
64 struct r300_query* q = (struct r300_query*)query;
65
66 remove_from_list(q);
67 FREE(query);
68 }
69
70 static void r300_begin_query(struct pipe_context* pipe,
71 struct pipe_query* query)
72 {
73 uint32_t* map;
74 struct r300_context* r300 = r300_context(pipe);
75 struct r300_query* q = (struct r300_query*)query;
76
77 assert(r300->query_current == NULL);
78
79 map = pipe->screen->buffer_map(pipe->screen, r300->oqbo,
80 PIPE_BUFFER_USAGE_CPU_WRITE);
81 map += q->offset / 4;
82 *map = ~0U;
83 pipe->screen->buffer_unmap(pipe->screen, r300->oqbo);
84
85 q->flushed = FALSE;
86 r300->query_current = q;
87 r300->dirty_state |= R300_NEW_QUERY;
88 }
89
90 static void r300_end_query(struct pipe_context* pipe,
91 struct pipe_query* query)
92 {
93 struct r300_context* r300 = r300_context(pipe);
94 struct r300_query* q = (struct r300_query*)query;
95
96 r300_emit_dirty_state(r300);
97 r300_emit_query_end(r300, q);
98
99 r300->query_current = NULL;
100 }
101
102 static boolean r300_get_query_result(struct pipe_context* pipe,
103 struct pipe_query* query,
104 boolean wait,
105 uint64_t* result)
106 {
107 struct r300_context* r300 = r300_context(pipe);
108 struct r300_screen* r300screen = r300_screen(r300->context.screen);
109 struct r300_query *q = (struct r300_query*)query;
110 unsigned flags = PIPE_BUFFER_USAGE_CPU_READ;
111 uint32_t* map;
112 uint32_t temp = 0;
113 unsigned i;
114
115 if (q->flushed == FALSE)
116 pipe->flush(pipe, 0, NULL);
117 if (!wait) {
118 flags |= PIPE_BUFFER_USAGE_DONTBLOCK;
119 }
120
121 map = pipe->screen->buffer_map(pipe->screen, r300->oqbo, flags);
122 if (!map)
123 return FALSE;
124 map += q->offset / 4;
125 for (i = 0; i < r300screen->caps->num_frag_pipes; i++) {
126 if (*map == ~0U) {
127 /* Looks like our results aren't ready yet. */
128 if (wait) {
129 debug_printf("r300: Despite waiting, OQ results haven't"
130 " come in yet.\n");
131 }
132 temp = ~0U;
133 break;
134 }
135 temp += *map;
136 map++;
137 }
138 pipe->screen->buffer_unmap(pipe->screen, r300->oqbo);
139
140 if (temp == ~0U) {
141 /* Our results haven't been written yet... */
142 return FALSE;
143 }
144
145 *result = temp;
146 return TRUE;
147 }
148
149 void r300_init_query_functions(struct r300_context* r300) {
150 r300->context.create_query = r300_create_query;
151 r300->context.destroy_query = r300_destroy_query;
152 r300->context.begin_query = r300_begin_query;
153 r300->context.end_query = r300_end_query;
154 r300->context.get_query_result = r300_get_query_result;
155 }