freedreno/a6xx: MSAA
[mesa.git] / src / gallium / drivers / freedreno / a6xx / fd6_query.c
1 /*
2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors:
25 * Rob Clark <robclark@freedesktop.org>
26 */
27
28 /* NOTE: see https://github.com/freedreno/freedreno/wiki/A5xx-Queries */
29
30 #include "freedreno_query_acc.h"
31 #include "freedreno_resource.h"
32
33 #include "fd6_context.h"
34 #include "fd6_emit.h"
35 #include "fd6_format.h"
36 #include "fd6_query.h"
37
38 struct PACKED fd6_query_sample {
39 uint64_t start;
40 uint64_t result;
41 uint64_t stop;
42 };
43
44 #define query_sample(aq, field) \
45 fd_resource((aq)->prsc)->bo, \
46 offsetof(struct fd6_query_sample, field), \
47 0, 0
48
49 /*
50 * Occlusion Query:
51 *
52 * OCCLUSION_COUNTER and OCCLUSION_PREDICATE differ only in how they
53 * interpret results
54 */
55
56 static void
57 occlusion_resume(struct fd_acc_query *aq, struct fd_batch *batch)
58 {
59 struct fd_ringbuffer *ring = batch->draw;
60
61 OUT_PKT4(ring, REG_A6XX_RB_SAMPLE_COUNT_CONTROL, 1);
62 OUT_RING(ring, A6XX_RB_SAMPLE_COUNT_CONTROL_COPY);
63
64 OUT_PKT4(ring, REG_A6XX_RB_SAMPLE_COUNT_ADDR_LO, 2);
65 OUT_RELOCW(ring, query_sample(aq, start));
66
67 fd6_event_write(batch, ring, ZPASS_DONE, false);
68
69 fd6_context(batch->ctx)->samples_passed_queries++;
70 }
71
72 static void
73 occlusion_pause(struct fd_acc_query *aq, struct fd_batch *batch)
74 {
75 struct fd_ringbuffer *ring = batch->draw;
76
77 OUT_PKT7(ring, CP_MEM_WRITE, 4);
78 OUT_RELOCW(ring, query_sample(aq, stop));
79 OUT_RING(ring, 0xffffffff);
80 OUT_RING(ring, 0xffffffff);
81
82 OUT_PKT7(ring, CP_WAIT_MEM_WRITES, 0);
83
84 OUT_PKT4(ring, REG_A6XX_RB_SAMPLE_COUNT_CONTROL, 1);
85 OUT_RING(ring, A6XX_RB_SAMPLE_COUNT_CONTROL_COPY);
86
87 OUT_PKT4(ring, REG_A6XX_RB_SAMPLE_COUNT_ADDR_LO, 2);
88 OUT_RELOCW(ring, query_sample(aq, stop));
89
90 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
91 OUT_RING(ring, ZPASS_DONE);
92 fd_reset_wfi(batch);
93
94 OUT_PKT7(ring, CP_WAIT_REG_MEM, 6);
95 OUT_RING(ring, 0x00000014); // XXX
96 OUT_RELOC(ring, query_sample(aq, stop));
97 OUT_RING(ring, 0xffffffff);
98 OUT_RING(ring, 0xffffffff);
99 OUT_RING(ring, 0x00000010); // XXX
100
101 /* result += stop - start: */
102 OUT_PKT7(ring, CP_MEM_TO_MEM, 9);
103 OUT_RING(ring, CP_MEM_TO_MEM_0_DOUBLE |
104 CP_MEM_TO_MEM_0_NEG_C);
105 OUT_RELOCW(ring, query_sample(aq, result)); /* dst */
106 OUT_RELOC(ring, query_sample(aq, result)); /* srcA */
107 OUT_RELOC(ring, query_sample(aq, stop)); /* srcB */
108 OUT_RELOC(ring, query_sample(aq, start)); /* srcC */
109
110 fd6_context(batch->ctx)->samples_passed_queries--;
111 }
112
113 static void
114 occlusion_counter_result(struct fd_acc_query *aq, void *buf,
115 union pipe_query_result *result)
116 {
117 struct fd6_query_sample *sp = buf;
118 result->u64 = sp->result;
119 }
120
121 static void
122 occlusion_predicate_result(struct fd_acc_query *aq, void *buf,
123 union pipe_query_result *result)
124 {
125 struct fd6_query_sample *sp = buf;
126 result->b = !!sp->result;
127 }
128
129 static const struct fd_acc_sample_provider occlusion_counter = {
130 .query_type = PIPE_QUERY_OCCLUSION_COUNTER,
131 .active = FD_STAGE_DRAW,
132 .size = sizeof(struct fd6_query_sample),
133 .resume = occlusion_resume,
134 .pause = occlusion_pause,
135 .result = occlusion_counter_result,
136 };
137
138 static const struct fd_acc_sample_provider occlusion_predicate = {
139 .query_type = PIPE_QUERY_OCCLUSION_PREDICATE,
140 .active = FD_STAGE_DRAW,
141 .size = sizeof(struct fd6_query_sample),
142 .resume = occlusion_resume,
143 .pause = occlusion_pause,
144 .result = occlusion_predicate_result,
145 };
146
147 static const struct fd_acc_sample_provider occlusion_predicate_conservative = {
148 .query_type = PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE,
149 .active = FD_STAGE_DRAW,
150 .size = sizeof(struct fd6_query_sample),
151 .resume = occlusion_resume,
152 .pause = occlusion_pause,
153 .result = occlusion_predicate_result,
154 };
155
156 /*
157 * Timestamp Queries:
158 */
159
160 static void
161 timestamp_resume(struct fd_acc_query *aq, struct fd_batch *batch)
162 {
163 struct fd_ringbuffer *ring = batch->draw;
164
165 OUT_PKT7(ring, CP_EVENT_WRITE, 4);
166 OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_AND_INV_EVENT) |
167 CP_EVENT_WRITE_0_TIMESTAMP);
168 OUT_RELOCW(ring, query_sample(aq, start));
169 OUT_RING(ring, 0x00000000);
170
171 fd_reset_wfi(batch);
172 }
173
174 static void
175 timestamp_pause(struct fd_acc_query *aq, struct fd_batch *batch)
176 {
177 struct fd_ringbuffer *ring = batch->draw;
178
179 OUT_PKT7(ring, CP_EVENT_WRITE, 4);
180 OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_AND_INV_EVENT) |
181 CP_EVENT_WRITE_0_TIMESTAMP);
182 OUT_RELOCW(ring, query_sample(aq, stop));
183 OUT_RING(ring, 0x00000000);
184
185 fd_reset_wfi(batch);
186 fd_wfi(batch, ring);
187
188 /* result += stop - start: */
189 OUT_PKT7(ring, CP_MEM_TO_MEM, 9);
190 OUT_RING(ring, CP_MEM_TO_MEM_0_DOUBLE |
191 CP_MEM_TO_MEM_0_NEG_C);
192 OUT_RELOCW(ring, query_sample(aq, result)); /* dst */
193 OUT_RELOC(ring, query_sample(aq, result)); /* srcA */
194 OUT_RELOC(ring, query_sample(aq, stop)); /* srcB */
195 OUT_RELOC(ring, query_sample(aq, start)); /* srcC */
196 }
197
198 static uint64_t
199 ticks_to_ns(uint32_t ts)
200 {
201 /* This is based on the 19.2MHz always-on rbbm timer.
202 *
203 * TODO we should probably query this value from kernel..
204 */
205 return ts * (1000000000 / 19200000);
206 }
207
208 static void
209 time_elapsed_accumulate_result(struct fd_acc_query *aq, void *buf,
210 union pipe_query_result *result)
211 {
212 struct fd6_query_sample *sp = buf;
213 result->u64 = ticks_to_ns(sp->result);
214 }
215
216 static void
217 timestamp_accumulate_result(struct fd_acc_query *aq, void *buf,
218 union pipe_query_result *result)
219 {
220 struct fd6_query_sample *sp = buf;
221 result->u64 = ticks_to_ns(sp->result);
222 }
223
224 static const struct fd_acc_sample_provider time_elapsed = {
225 .query_type = PIPE_QUERY_TIME_ELAPSED,
226 .active = FD_STAGE_DRAW | FD_STAGE_CLEAR,
227 .size = sizeof(struct fd6_query_sample),
228 .resume = timestamp_resume,
229 .pause = timestamp_pause,
230 .result = time_elapsed_accumulate_result,
231 };
232
233 /* NOTE: timestamp query isn't going to give terribly sensible results
234 * on a tiler. But it is needed by qapitrace profile heatmap. If you
235 * add in a binning pass, the results get even more non-sensical. So
236 * we just return the timestamp on the first tile and hope that is
237 * kind of good enough.
238 */
239
240 static const struct fd_acc_sample_provider timestamp = {
241 .query_type = PIPE_QUERY_TIMESTAMP,
242 .active = FD_STAGE_ALL,
243 .size = sizeof(struct fd6_query_sample),
244 .resume = timestamp_resume,
245 .pause = timestamp_pause,
246 .result = timestamp_accumulate_result,
247 };
248
249 void
250 fd6_query_context_init(struct pipe_context *pctx)
251 {
252 struct fd_context *ctx = fd_context(pctx);
253
254 ctx->create_query = fd_acc_create_query;
255 ctx->query_set_stage = fd_acc_query_set_stage;
256
257 fd_acc_query_register_provider(pctx, &occlusion_counter);
258 fd_acc_query_register_provider(pctx, &occlusion_predicate);
259 fd_acc_query_register_provider(pctx, &occlusion_predicate_conservative);
260
261 fd_acc_query_register_provider(pctx, &time_elapsed);
262 fd_acc_query_register_provider(pctx, &timestamp);
263 }