nv50: handle inverted render conditions
[mesa.git] / src / gallium / drivers / nouveau / nv50 / nv50_query.c
1 /*
2 * Copyright 2011 Nouveau Project
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Christoph Bumiller
23 */
24
25 #define NV50_PUSH_EXPLICIT_SPACE_CHECKING
26
27 #include "nv50/nv50_context.h"
28 #include "nv_object.xml.h"
29
30 /* XXX: Nested queries, and simultaneous queries on multiple gallium contexts
31 * (since we use only a single GPU channel per screen) will not work properly.
32 *
33 * The first is not that big of an issue because OpenGL does not allow nested
34 * queries anyway.
35 */
36
37 struct nv50_query {
38 uint32_t *data;
39 uint16_t type;
40 uint16_t index;
41 uint32_t sequence;
42 struct nouveau_bo *bo;
43 uint32_t base;
44 uint32_t offset; /* base + i * 16 */
45 boolean ready;
46 boolean flushed;
47 boolean is64bit;
48 struct nouveau_mm_allocation *mm;
49 };
50
51 #define NV50_QUERY_ALLOC_SPACE 128
52
53 static INLINE struct nv50_query *
54 nv50_query(struct pipe_query *pipe)
55 {
56 return (struct nv50_query *)pipe;
57 }
58
59 static boolean
60 nv50_query_allocate(struct nv50_context *nv50, struct nv50_query *q, int size)
61 {
62 struct nv50_screen *screen = nv50->screen;
63 int ret;
64
65 if (q->bo) {
66 nouveau_bo_ref(NULL, &q->bo);
67 if (q->mm) {
68 if (q->ready)
69 nouveau_mm_free(q->mm);
70 else
71 nouveau_fence_work(screen->base.fence.current, nouveau_mm_free_work,
72 q->mm);
73 }
74 }
75 if (size) {
76 q->mm = nouveau_mm_allocate(screen->base.mm_GART, size, &q->bo, &q->base);
77 if (!q->bo)
78 return FALSE;
79 q->offset = q->base;
80
81 ret = nouveau_bo_map(q->bo, 0, screen->base.client);
82 if (ret) {
83 nv50_query_allocate(nv50, q, 0);
84 return FALSE;
85 }
86 q->data = (uint32_t *)((uint8_t *)q->bo->map + q->base);
87 }
88 return TRUE;
89 }
90
91 static void
92 nv50_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
93 {
94 nv50_query_allocate(nv50_context(pipe), nv50_query(pq), 0);
95 FREE(nv50_query(pq));
96 }
97
98 static struct pipe_query *
99 nv50_query_create(struct pipe_context *pipe, unsigned type, unsigned index)
100 {
101 struct nv50_context *nv50 = nv50_context(pipe);
102 struct nv50_query *q;
103
104 q = CALLOC_STRUCT(nv50_query);
105 if (!q)
106 return NULL;
107
108 if (!nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE)) {
109 FREE(q);
110 return NULL;
111 }
112
113 q->is64bit = (type == PIPE_QUERY_PRIMITIVES_GENERATED ||
114 type == PIPE_QUERY_PRIMITIVES_EMITTED ||
115 type == PIPE_QUERY_SO_STATISTICS);
116 q->type = type;
117
118 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
119 q->offset -= 16;
120 q->data -= 16 / sizeof(*q->data); /* we advance before query_begin ! */
121 }
122
123 return (struct pipe_query *)q;
124 }
125
126 static void
127 nv50_query_get(struct nouveau_pushbuf *push, struct nv50_query *q,
128 unsigned offset, uint32_t get)
129 {
130 offset += q->offset;
131
132 PUSH_SPACE(push, 5);
133 PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
134 BEGIN_NV04(push, NV50_3D(QUERY_ADDRESS_HIGH), 4);
135 PUSH_DATAh(push, q->bo->offset + offset);
136 PUSH_DATA (push, q->bo->offset + offset);
137 PUSH_DATA (push, q->sequence);
138 PUSH_DATA (push, get);
139 }
140
141 static void
142 nv50_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
143 {
144 struct nv50_context *nv50 = nv50_context(pipe);
145 struct nouveau_pushbuf *push = nv50->base.pushbuf;
146 struct nv50_query *q = nv50_query(pq);
147
148 /* For occlusion queries we have to change the storage, because a previous
149 * query might set the initial render conition to FALSE even *after* we re-
150 * initialized it to TRUE.
151 */
152 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
153 q->offset += 16;
154 q->data += 16 / sizeof(*q->data);
155 if (q->offset - q->base == NV50_QUERY_ALLOC_SPACE)
156 nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE);
157
158 /* XXX: can we do this with the GPU, and sync with respect to a previous
159 * query ?
160 */
161 q->data[0] = q->sequence; /* initialize sequence */
162 q->data[1] = 1; /* initial render condition = TRUE */
163 q->data[4] = q->sequence + 1; /* for comparison COND_MODE */
164 q->data[5] = 0;
165 }
166 if (!q->is64bit)
167 q->data[0] = q->sequence++; /* the previously used one */
168
169 switch (q->type) {
170 case PIPE_QUERY_OCCLUSION_COUNTER:
171 PUSH_SPACE(push, 4);
172 BEGIN_NV04(push, NV50_3D(COUNTER_RESET), 1);
173 PUSH_DATA (push, NV50_3D_COUNTER_RESET_SAMPLECNT);
174 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
175 PUSH_DATA (push, 1);
176 break;
177 case PIPE_QUERY_PRIMITIVES_GENERATED:
178 nv50_query_get(push, q, 0x10, 0x06805002);
179 break;
180 case PIPE_QUERY_PRIMITIVES_EMITTED:
181 nv50_query_get(push, q, 0x10, 0x05805002);
182 break;
183 case PIPE_QUERY_SO_STATISTICS:
184 nv50_query_get(push, q, 0x20, 0x05805002);
185 nv50_query_get(push, q, 0x30, 0x06805002);
186 break;
187 case PIPE_QUERY_TIME_ELAPSED:
188 nv50_query_get(push, q, 0x10, 0x00005002);
189 break;
190 default:
191 break;
192 }
193 q->ready = FALSE;
194 }
195
196 static void
197 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
198 {
199 struct nv50_context *nv50 = nv50_context(pipe);
200 struct nouveau_pushbuf *push = nv50->base.pushbuf;
201 struct nv50_query *q = nv50_query(pq);
202
203 switch (q->type) {
204 case PIPE_QUERY_OCCLUSION_COUNTER:
205 nv50_query_get(push, q, 0, 0x0100f002);
206 PUSH_SPACE(push, 2);
207 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
208 PUSH_DATA (push, 0);
209 break;
210 case PIPE_QUERY_PRIMITIVES_GENERATED:
211 nv50_query_get(push, q, 0, 0x06805002);
212 break;
213 case PIPE_QUERY_PRIMITIVES_EMITTED:
214 nv50_query_get(push, q, 0, 0x05805002);
215 break;
216 case PIPE_QUERY_SO_STATISTICS:
217 nv50_query_get(push, q, 0x00, 0x05805002);
218 nv50_query_get(push, q, 0x10, 0x06805002);
219 break;
220 case PIPE_QUERY_TIMESTAMP:
221 q->sequence++;
222 /* fall through */
223 case PIPE_QUERY_TIME_ELAPSED:
224 nv50_query_get(push, q, 0, 0x00005002);
225 break;
226 case PIPE_QUERY_GPU_FINISHED:
227 q->sequence++;
228 nv50_query_get(push, q, 0, 0x1000f010);
229 break;
230 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET:
231 nv50_query_get(push, q, 0, 0x0d005002 | (q->index << 5));
232 break;
233 case PIPE_QUERY_TIMESTAMP_DISJOINT:
234 break;
235 default:
236 assert(0);
237 break;
238 }
239 q->ready = q->flushed = FALSE;
240 }
241
242 static INLINE boolean
243 nv50_query_ready(struct nv50_query *q)
244 {
245 return q->ready || (!q->is64bit && (q->data[0] == q->sequence));
246 }
247
248 static boolean
249 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
250 boolean wait, union pipe_query_result *result)
251 {
252 struct nv50_context *nv50 = nv50_context(pipe);
253 struct nv50_query *q = nv50_query(pq);
254 uint64_t *res64 = (uint64_t *)result;
255 uint32_t *res32 = (uint32_t *)result;
256 boolean *res8 = (boolean *)result;
257 uint64_t *data64 = (uint64_t *)q->data;
258
259 if (!q->ready) /* update ? */
260 q->ready = nv50_query_ready(q);
261 if (!q->ready) {
262 if (!wait) {
263 /* for broken apps that spin on GL_QUERY_RESULT_AVAILABLE */
264 if (!q->flushed) {
265 q->flushed = TRUE;
266 PUSH_KICK(nv50->base.pushbuf);
267 }
268 return FALSE;
269 }
270 if (nouveau_bo_wait(q->bo, NOUVEAU_BO_RD, nv50->screen->base.client))
271 return FALSE;
272 }
273 q->ready = TRUE;
274
275 switch (q->type) {
276 case PIPE_QUERY_GPU_FINISHED:
277 res8[0] = TRUE;
278 break;
279 case PIPE_QUERY_OCCLUSION_COUNTER: /* u32 sequence, u32 count, u64 time */
280 res64[0] = q->data[1];
281 break;
282 case PIPE_QUERY_PRIMITIVES_GENERATED: /* u64 count, u64 time */
283 case PIPE_QUERY_PRIMITIVES_EMITTED: /* u64 count, u64 time */
284 res64[0] = data64[0] - data64[2];
285 break;
286 case PIPE_QUERY_SO_STATISTICS:
287 res64[0] = data64[0] - data64[4];
288 res64[1] = data64[2] - data64[6];
289 break;
290 case PIPE_QUERY_TIMESTAMP:
291 res64[0] = data64[1];
292 break;
293 case PIPE_QUERY_TIMESTAMP_DISJOINT:
294 res64[0] = 1000000000;
295 res8[8] = FALSE;
296 break;
297 case PIPE_QUERY_TIME_ELAPSED:
298 res64[0] = data64[1] - data64[3];
299 break;
300 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET:
301 res32[0] = q->data[1];
302 break;
303 default:
304 return FALSE;
305 }
306
307 return TRUE;
308 }
309
310 void
311 nv84_query_fifo_wait(struct nouveau_pushbuf *push, struct pipe_query *pq)
312 {
313 struct nv50_query *q = nv50_query(pq);
314 unsigned offset = q->offset;
315
316 PUSH_SPACE(push, 5);
317 PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
318 BEGIN_NV04(push, SUBC_3D(NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH), 4);
319 PUSH_DATAh(push, q->bo->offset + offset);
320 PUSH_DATA (push, q->bo->offset + offset);
321 PUSH_DATA (push, q->sequence);
322 PUSH_DATA (push, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
323 }
324
325 static void
326 nv50_render_condition(struct pipe_context *pipe,
327 struct pipe_query *pq,
328 boolean condition, uint mode)
329 {
330 struct nv50_context *nv50 = nv50_context(pipe);
331 struct nouveau_pushbuf *push = nv50->base.pushbuf;
332 struct nv50_query *q;
333 uint32_t cond;
334 boolean wait =
335 mode != PIPE_RENDER_COND_NO_WAIT &&
336 mode != PIPE_RENDER_COND_BY_REGION_NO_WAIT;
337
338 if (!pq) {
339 cond = NV50_3D_COND_MODE_ALWAYS;
340 }
341 else {
342 q = nv50_query(pq);
343 /* NOTE: comparison of 2 queries only works if both have completed */
344 switch (q->type) {
345 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
346 cond = condition ? NV50_3D_COND_MODE_EQUAL :
347 NV50_3D_COND_MODE_NOT_EQUAL;
348 wait = TRUE;
349 break;
350 case PIPE_QUERY_OCCLUSION_COUNTER:
351 case PIPE_QUERY_OCCLUSION_PREDICATE:
352 if (likely(!condition)) {
353 /* XXX: Placeholder, handle nesting here if available */
354 if (unlikely(false))
355 cond = wait ? NV50_3D_COND_MODE_NOT_EQUAL :
356 NV50_3D_COND_MODE_ALWAYS;
357 else
358 cond = NV50_3D_COND_MODE_RES_NON_ZERO;
359 } else {
360 cond = wait ? NV50_3D_COND_MODE_EQUAL : NV50_3D_COND_MODE_ALWAYS;
361 }
362 break;
363 default:
364 assert(!"render condition query not a predicate");
365 cond = NV50_3D_COND_MODE_ALWAYS;
366 break;
367 }
368 }
369
370 nv50->cond_query = pq;
371 nv50->cond_cond = condition;
372 nv50->cond_condmode = cond;
373 nv50->cond_mode = mode;
374
375 if (!pq) {
376 PUSH_SPACE(push, 2);
377 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
378 PUSH_DATA (push, cond);
379 return;
380 }
381
382 PUSH_SPACE(push, 9);
383
384 if (wait) {
385 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
386 PUSH_DATA (push, 0);
387 }
388
389 PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
390 BEGIN_NV04(push, NV50_3D(COND_ADDRESS_HIGH), 3);
391 PUSH_DATAh(push, q->bo->offset + q->offset);
392 PUSH_DATA (push, q->bo->offset + q->offset);
393 PUSH_DATA (push, cond);
394
395 BEGIN_NV04(push, NV50_2D(COND_ADDRESS_HIGH), 2);
396 PUSH_DATAh(push, q->bo->offset + q->offset);
397 PUSH_DATA (push, q->bo->offset + q->offset);
398 }
399
400 void
401 nv50_query_pushbuf_submit(struct nouveau_pushbuf *push,
402 struct pipe_query *pq, unsigned result_offset)
403 {
404 struct nv50_query *q = nv50_query(pq);
405
406 /* XXX: does this exist ? */
407 #define NV50_IB_ENTRY_1_NO_PREFETCH (0 << (31 - 8))
408
409 nouveau_pushbuf_space(push, 0, 0, 1);
410 nouveau_pushbuf_data(push, q->bo, q->offset + result_offset, 4 |
411 NV50_IB_ENTRY_1_NO_PREFETCH);
412 }
413
414 void
415 nva0_so_target_save_offset(struct pipe_context *pipe,
416 struct pipe_stream_output_target *ptarg,
417 unsigned index, boolean serialize)
418 {
419 struct nv50_so_target *targ = nv50_so_target(ptarg);
420
421 if (serialize) {
422 struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
423 PUSH_SPACE(push, 2);
424 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
425 PUSH_DATA (push, 0);
426 }
427
428 nv50_query(targ->pq)->index = index;
429 nv50_query_end(pipe, targ->pq);
430 }
431
432 void
433 nv50_init_query_functions(struct nv50_context *nv50)
434 {
435 struct pipe_context *pipe = &nv50->base.pipe;
436
437 pipe->create_query = nv50_query_create;
438 pipe->destroy_query = nv50_query_destroy;
439 pipe->begin_query = nv50_query_begin;
440 pipe->end_query = nv50_query_end;
441 pipe->get_query_result = nv50_query_result;
442 pipe->render_condition = nv50_render_condition;
443 }