gallium/tgsi: Redefine the TGSI_TEXTURE_UNKNOWN texture target.
[mesa.git] / src / gallium / drivers / 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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 *
22 * Authors: Christoph Bumiller
23 */
24
25 #define NV50_PUSH_EXPLICIT_SPACE_CHECKING
26
27 #include "nv50_context.h"
28 #include "nouveau/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)
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[1] = 1; /* initial render condition = TRUE */
162 }
163 if (!q->is64bit)
164 q->data[0] = q->sequence++; /* the previously used one */
165
166 switch (q->type) {
167 case PIPE_QUERY_OCCLUSION_COUNTER:
168 PUSH_SPACE(push, 4);
169 BEGIN_NV04(push, NV50_3D(COUNTER_RESET), 1);
170 PUSH_DATA (push, NV50_3D_COUNTER_RESET_SAMPLECNT);
171 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
172 PUSH_DATA (push, 1);
173 break;
174 case PIPE_QUERY_PRIMITIVES_GENERATED:
175 nv50_query_get(push, q, 0x10, 0x06805002);
176 break;
177 case PIPE_QUERY_PRIMITIVES_EMITTED:
178 nv50_query_get(push, q, 0x10, 0x05805002);
179 break;
180 case PIPE_QUERY_SO_STATISTICS:
181 nv50_query_get(push, q, 0x20, 0x05805002);
182 nv50_query_get(push, q, 0x30, 0x06805002);
183 break;
184 case PIPE_QUERY_TIMESTAMP_DISJOINT:
185 case PIPE_QUERY_TIME_ELAPSED:
186 nv50_query_get(push, q, 0x10, 0x00005002);
187 break;
188 default:
189 break;
190 }
191 q->ready = FALSE;
192 }
193
194 static void
195 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
196 {
197 struct nv50_context *nv50 = nv50_context(pipe);
198 struct nouveau_pushbuf *push = nv50->base.pushbuf;
199 struct nv50_query *q = nv50_query(pq);
200
201 switch (q->type) {
202 case PIPE_QUERY_OCCLUSION_COUNTER:
203 nv50_query_get(push, q, 0, 0x0100f002);
204 PUSH_SPACE(push, 2);
205 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
206 PUSH_DATA (push, 0);
207 break;
208 case PIPE_QUERY_PRIMITIVES_GENERATED:
209 nv50_query_get(push, q, 0, 0x06805002);
210 break;
211 case PIPE_QUERY_PRIMITIVES_EMITTED:
212 nv50_query_get(push, q, 0, 0x05805002);
213 break;
214 case PIPE_QUERY_SO_STATISTICS:
215 nv50_query_get(push, q, 0x00, 0x05805002);
216 nv50_query_get(push, q, 0x10, 0x06805002);
217 break;
218 case PIPE_QUERY_TIMESTAMP_DISJOINT:
219 case PIPE_QUERY_TIME_ELAPSED:
220 nv50_query_get(push, q, 0, 0x00005002);
221 break;
222 case PIPE_QUERY_GPU_FINISHED:
223 nv50_query_get(push, q, 0, 0x1000f010);
224 break;
225 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET:
226 nv50_query_get(push, q, 0, 0x0d005002 | (q->index << 5));
227 break;
228 default:
229 assert(0);
230 break;
231 }
232 q->flushed = FALSE;
233 }
234
235 static INLINE boolean
236 nv50_query_ready(struct nv50_query *q)
237 {
238 return q->ready || (!q->is64bit && (q->data[0] == q->sequence));
239 }
240
241 static boolean
242 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
243 boolean wait, union pipe_query_result *result)
244 {
245 struct nv50_context *nv50 = nv50_context(pipe);
246 struct nv50_query *q = nv50_query(pq);
247 uint64_t *res64 = (uint64_t *)result;
248 uint32_t *res32 = (uint32_t *)result;
249 boolean *res8 = (boolean *)result;
250 uint64_t *data64 = (uint64_t *)q->data;
251
252 if (!q->ready) /* update ? */
253 q->ready = nv50_query_ready(q);
254 if (!q->ready) {
255 if (!wait) {
256 /* for broken apps that spin on GL_QUERY_RESULT_AVAILABLE */
257 if (!q->flushed) {
258 q->flushed = TRUE;
259 PUSH_KICK(nv50->base.pushbuf);
260 }
261 return FALSE;
262 }
263 if (nouveau_bo_wait(q->bo, NOUVEAU_BO_RD, nv50->screen->base.client))
264 return FALSE;
265 }
266 q->ready = TRUE;
267
268 switch (q->type) {
269 case PIPE_QUERY_GPU_FINISHED:
270 res8[0] = TRUE;
271 break;
272 case PIPE_QUERY_OCCLUSION_COUNTER: /* u32 sequence, u32 count, u64 time */
273 res64[0] = q->data[1];
274 break;
275 case PIPE_QUERY_PRIMITIVES_GENERATED: /* u64 count, u64 time */
276 case PIPE_QUERY_PRIMITIVES_EMITTED: /* u64 count, u64 time */
277 res64[0] = data64[0] - data64[2];
278 break;
279 case PIPE_QUERY_SO_STATISTICS:
280 res64[0] = data64[0] - data64[4];
281 res64[1] = data64[2] - data64[6];
282 break;
283 case PIPE_QUERY_TIMESTAMP_DISJOINT: /* u32 sequence, u32 0, u64 time */
284 res64[0] = 1000000000;
285 res8[8] = (data64[0] == data64[2]) ? FALSE : TRUE;
286 break;
287 case PIPE_QUERY_TIME_ELAPSED:
288 res64[0] = data64[1] - data64[3];
289 break;
290 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET:
291 res32[0] = q->data[1];
292 break;
293 default:
294 return FALSE;
295 }
296
297 return TRUE;
298 }
299
300 void
301 nv84_query_fifo_wait(struct nouveau_pushbuf *push, struct pipe_query *pq)
302 {
303 struct nv50_query *q = nv50_query(pq);
304 unsigned offset = q->offset;
305
306 PUSH_SPACE(push, 5);
307 PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
308 BEGIN_NV04(push, SUBC_3D(NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH), 4);
309 PUSH_DATAh(push, q->bo->offset + offset);
310 PUSH_DATA (push, q->bo->offset + offset);
311 PUSH_DATA (push, q->sequence);
312 PUSH_DATA (push, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
313 }
314
315 static void
316 nv50_render_condition(struct pipe_context *pipe,
317 struct pipe_query *pq, uint mode)
318 {
319 struct nv50_context *nv50 = nv50_context(pipe);
320 struct nouveau_pushbuf *push = nv50->base.pushbuf;
321 struct nv50_query *q;
322
323 PUSH_SPACE(push, 6);
324
325 if (!pq) {
326 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
327 PUSH_DATA (push, NV50_3D_COND_MODE_ALWAYS);
328 return;
329 }
330 q = nv50_query(pq);
331
332 if (mode == PIPE_RENDER_COND_WAIT ||
333 mode == PIPE_RENDER_COND_BY_REGION_WAIT) {
334 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
335 PUSH_DATA (push, 0);
336 }
337
338 BEGIN_NV04(push, NV50_3D(COND_ADDRESS_HIGH), 3);
339 PUSH_DATAh(push, q->bo->offset + q->offset);
340 PUSH_DATA (push, q->bo->offset + q->offset);
341 PUSH_DATA (push, NV50_3D_COND_MODE_RES_NON_ZERO);
342 }
343
344 void
345 nv50_query_pushbuf_submit(struct nouveau_pushbuf *push,
346 struct pipe_query *pq, unsigned result_offset)
347 {
348 struct nv50_query *q = nv50_query(pq);
349
350 /* XXX: does this exist ? */
351 #define NV50_IB_ENTRY_1_NO_PREFETCH (0 << (31 - 8))
352
353 nouveau_pushbuf_space(push, 0, 0, 1);
354 nouveau_pushbuf_data(push, q->bo, q->offset + result_offset, 4 |
355 NV50_IB_ENTRY_1_NO_PREFETCH);
356 }
357
358 void
359 nva0_so_target_save_offset(struct pipe_context *pipe,
360 struct pipe_stream_output_target *ptarg,
361 unsigned index, boolean serialize)
362 {
363 struct nv50_so_target *targ = nv50_so_target(ptarg);
364
365 if (serialize) {
366 struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
367 PUSH_SPACE(push, 2);
368 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
369 PUSH_DATA (push, 0);
370 }
371
372 nv50_query(targ->pq)->index = index;
373 nv50_query_end(pipe, targ->pq);
374 }
375
376 void
377 nv50_init_query_functions(struct nv50_context *nv50)
378 {
379 struct pipe_context *pipe = &nv50->base.pipe;
380
381 pipe->create_query = nv50_query_create;
382 pipe->destroy_query = nv50_query_destroy;
383 pipe->begin_query = nv50_query_begin;
384 pipe->end_query = nv50_query_end;
385 pipe->get_query_result = nv50_query_result;
386 pipe->render_condition = nv50_render_condition;
387 }