1 /*
2 * Copyright © 2014 Broadcom
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * Gallium query object support.
26 *
27 * The HW has native support for occlusion queries, with the query result
28 * being loaded and stored by the TLB unit. From a SW perspective, we have to
29 * be careful to make sure that the jobs that need to be tracking queries are
30 * bracketed by the start and end of counting, even across FBO transitions.
31 *
32 * For the transform feedback PRIMITIVES_GENERATED/WRITTEN queries, we have to
33 * do the calculations in software at draw time.
34 */
35
36 #include "v3d_query.h"
37
38 struct v3d_query_pipe
39 {
40 struct v3d_query base;
41
42 enum pipe_query_type type;
43 struct v3d_bo *bo;
44
45 uint32_t start, end;
46 uint32_t result;
47 };
48
49 static void
v3d_destroy_query_pipe(struct v3d_context * v3d,struct v3d_query * query)50 v3d_destroy_query_pipe(struct v3d_context *v3d, struct v3d_query *query)
51 {
52 struct v3d_query_pipe *pquery = (struct v3d_query_pipe *)query;
53
54 v3d_bo_unreference(&pquery->bo);
55 free(pquery);
56 }
57
58 static bool
v3d_begin_query_pipe(struct v3d_context * v3d,struct v3d_query * query)59 v3d_begin_query_pipe(struct v3d_context *v3d, struct v3d_query *query)
60 {
61 struct v3d_query_pipe *pquery = (struct v3d_query_pipe *)query;
62
63 switch (pquery->type) {
64 case PIPE_QUERY_PRIMITIVES_GENERATED:
65 /* If we are using PRIMITIVE_COUNTS_FEEDBACK to retrieve
66 * primitive counts from the GPU (which we need when a GS
67 * is present), then we need to update our counters now
68 * to discard any primitives generated before this.
69 */
70 if (v3d->prog.gs)
71 v3d_update_primitive_counters(v3d);
72 pquery->start = v3d->prims_generated;
73 v3d->n_primitives_generated_queries_in_flight++;
74 break;
75 case PIPE_QUERY_PRIMITIVES_EMITTED:
76 /* If we are inside transform feedback we need to update the
77 * primitive counts to skip primitives recorded before this.
78 */
79 if (v3d->streamout.num_targets > 0)
80 v3d_update_primitive_counters(v3d);
81 pquery->start = v3d->tf_prims_generated;
82 break;
83 case PIPE_QUERY_OCCLUSION_COUNTER:
84 case PIPE_QUERY_OCCLUSION_PREDICATE:
85 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
86 v3d_bo_unreference(&pquery->bo);
87 pquery->bo = v3d_bo_alloc(v3d->screen, 4096, "query");
88 uint32_t *map = v3d_bo_map(pquery->bo);
89 *map = 0;
90
91 v3d->current_oq = pquery->bo;
92 v3d->dirty |= V3D_DIRTY_OQ;
93 break;
94 default:
95 unreachable("unsupported query type");
96 }
97
98 return true;
99 }
100
101 static bool
v3d_end_query_pipe(struct v3d_context * v3d,struct v3d_query * query)102 v3d_end_query_pipe(struct v3d_context *v3d, struct v3d_query *query)
103 {
104 struct v3d_query_pipe *pquery = (struct v3d_query_pipe *)query;
105
106 switch (pquery->type) {
107 case PIPE_QUERY_PRIMITIVES_GENERATED:
108 /* If we are using PRIMITIVE_COUNTS_FEEDBACK to retrieve
109 * primitive counts from the GPU (which we need when a GS
110 * is present), then we need to update our counters now.
111 */
112 if (v3d->prog.gs)
113 v3d_update_primitive_counters(v3d);
114 pquery->end = v3d->prims_generated;
115 v3d->n_primitives_generated_queries_in_flight--;
116 break;
117 case PIPE_QUERY_PRIMITIVES_EMITTED:
118 /* If transform feedback has ended, then we have already
119 * updated the primitive counts at glEndTransformFeedback()
120 * time. Otherwise, we have to do it now.
121 */
122 if (v3d->streamout.num_targets > 0)
123 v3d_update_primitive_counters(v3d);
124 pquery->end = v3d->tf_prims_generated;
125 break;
126 case PIPE_QUERY_OCCLUSION_COUNTER:
127 case PIPE_QUERY_OCCLUSION_PREDICATE:
128 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
129 v3d->current_oq = NULL;
130 v3d->dirty |= V3D_DIRTY_OQ;
131 break;
132 default:
133 unreachable("unsupported query type");
134 }
135
136 return true;
137 }
138
139 static bool
v3d_get_query_result_pipe(struct v3d_context * v3d,struct v3d_query * query,bool wait,union pipe_query_result * vresult)140 v3d_get_query_result_pipe(struct v3d_context *v3d, struct v3d_query *query,
141 bool wait, union pipe_query_result *vresult)
142 {
143 struct v3d_query_pipe *pquery = (struct v3d_query_pipe *)query;
144
145 if (pquery->bo) {
146 v3d_flush_jobs_using_bo(v3d, pquery->bo);
147
148 if (wait) {
149 if (!v3d_bo_wait(pquery->bo, ~0ull, "query"))
150 return false;
151 } else {
152 if (!v3d_bo_wait(pquery->bo, 0, "query"))
153 return false;
154 }
155
156 /* XXX: Sum up per-core values. */
157 uint32_t *map = v3d_bo_map(pquery->bo);
158 pquery->result = *map;
159
160 v3d_bo_unreference(&pquery->bo);
161 }
162
163 switch (pquery->type) {
164 case PIPE_QUERY_OCCLUSION_COUNTER:
165 vresult->u64 = pquery->result;
166 break;
167 case PIPE_QUERY_OCCLUSION_PREDICATE:
168 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
169 vresult->b = pquery->result != 0;
170 break;
171 case PIPE_QUERY_PRIMITIVES_GENERATED:
172 case PIPE_QUERY_PRIMITIVES_EMITTED:
173 vresult->u64 = pquery->end - pquery->start;
174 break;
175 default:
176 unreachable("unsupported query type");
177 }
178
179 return true;
180 }
181
182 static const struct v3d_query_funcs pipe_query_funcs = {
183 .destroy_query = v3d_destroy_query_pipe,
184 .begin_query = v3d_begin_query_pipe,
185 .end_query = v3d_end_query_pipe,
186 .get_query_result = v3d_get_query_result_pipe,
187 };
188
189 struct pipe_query *
v3d_create_query_pipe(struct v3d_context * v3d,unsigned query_type,unsigned index)190 v3d_create_query_pipe(struct v3d_context *v3d, unsigned query_type, unsigned index)
191 {
192 if (query_type >= PIPE_QUERY_DRIVER_SPECIFIC)
193 return NULL;
194
195 struct v3d_query_pipe *pquery = calloc(1, sizeof(*pquery));
196 struct v3d_query *query = &pquery->base;
197
198 pquery->type = query_type;
199 query->funcs = &pipe_query_funcs;
200
201 /* Note that struct pipe_query isn't actually defined anywhere. */
202 return (struct pipe_query *)query;
203 }
204