2 * Copyright 2011 Nouveau Project
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
22 * Authors: Christoph Bumiller
25 #include "nvc0_context.h"
26 #include "nouveau/nv_object.xml.h"
28 /* XXX: Nested queries, and simultaneous queries on multiple gallium contexts
29 * (since we use only a single GPU channel per screen) will not work properly.
31 * The first is not that big of an issue because OpenGL does not allow nested
39 struct nouveau_bo
*bo
;
41 uint32_t offset
; /* base + i * 16 */
44 struct nouveau_mm_allocation
*mm
;
47 #define NVC0_QUERY_ALLOC_SPACE 128
49 static INLINE
struct nvc0_query
*
50 nvc0_query(struct pipe_query
*pipe
)
52 return (struct nvc0_query
*)pipe
;
56 nvc0_query_allocate(struct nvc0_context
*nvc0
, struct nvc0_query
*q
, int size
)
58 struct nvc0_screen
*screen
= nvc0
->screen
;
62 nouveau_bo_ref(NULL
, &q
->bo
);
65 nouveau_mm_free(q
->mm
);
67 nouveau_fence_work(screen
->base
.fence
.current
, nouveau_mm_free_work
, q
->mm
);
71 q
->mm
= nouveau_mm_allocate(screen
->base
.mm_GART
, size
, &q
->bo
, &q
->base
);
76 ret
= nouveau_bo_map_range(q
->bo
, q
->base
, size
, NOUVEAU_BO_RD
|
79 nvc0_query_allocate(nvc0
, q
, 0);
83 nouveau_bo_unmap(q
->bo
);
89 nvc0_query_destroy(struct pipe_context
*pipe
, struct pipe_query
*pq
)
91 nvc0_query_allocate(nvc0_context(pipe
), nvc0_query(pq
), 0);
95 static struct pipe_query
*
96 nvc0_query_create(struct pipe_context
*pipe
, unsigned type
)
98 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
101 q
= CALLOC_STRUCT(nvc0_query
);
105 if (!nvc0_query_allocate(nvc0
, q
, NVC0_QUERY_ALLOC_SPACE
)) {
110 q
->is64bit
= (type
== PIPE_QUERY_PRIMITIVES_GENERATED
||
111 type
== PIPE_QUERY_PRIMITIVES_EMITTED
||
112 type
== PIPE_QUERY_SO_STATISTICS
);
115 if (q
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
117 q
->data
-= 16 / sizeof(*q
->data
); /* we advance before query_begin ! */
120 return (struct pipe_query
*)q
;
124 nvc0_query_get(struct nouveau_channel
*chan
, struct nvc0_query
*q
,
125 unsigned offset
, uint32_t get
)
129 MARK_RING (chan
, 5, 2);
130 BEGIN_RING(chan
, RING_3D(QUERY_ADDRESS_HIGH
), 4);
131 OUT_RELOCh(chan
, q
->bo
, offset
, NOUVEAU_BO_GART
| NOUVEAU_BO_WR
);
132 OUT_RELOCl(chan
, q
->bo
, offset
, NOUVEAU_BO_GART
| NOUVEAU_BO_WR
);
133 OUT_RING (chan
, q
->sequence
);
134 OUT_RING (chan
, get
);
138 nvc0_query_begin(struct pipe_context
*pipe
, struct pipe_query
*pq
)
140 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
141 struct nouveau_channel
*chan
= nvc0
->screen
->base
.channel
;
142 struct nvc0_query
*q
= nvc0_query(pq
);
144 /* For occlusion queries we have to change the storage, because a previous
145 * query might set the initial render conition to FALSE even *after* we re-
146 * initialized it to TRUE.
148 if (q
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
150 q
->data
+= 16 / sizeof(*q
->data
);
151 if (q
->offset
- q
->base
== NVC0_QUERY_ALLOC_SPACE
)
152 nvc0_query_allocate(nvc0
, q
, NVC0_QUERY_ALLOC_SPACE
);
154 /* XXX: can we do this with the GPU, and sync with respect to a previous
157 q
->data
[1] = 1; /* initial render condition = TRUE */
160 q
->data
[0] = q
->sequence
++; /* the previously used one */
163 case PIPE_QUERY_OCCLUSION_COUNTER
:
164 IMMED_RING(chan
, RING_3D(COUNTER_RESET
), NVC0_3D_COUNTER_RESET_SAMPLECNT
);
165 IMMED_RING(chan
, RING_3D(SAMPLECNT_ENABLE
), 1);
167 case PIPE_QUERY_PRIMITIVES_GENERATED
: /* store before & after instead ? */
168 IMMED_RING(chan
, RING_3D(COUNTER_RESET
),
169 NVC0_3D_COUNTER_RESET_GENERATED_PRIMITIVES
);
171 case PIPE_QUERY_PRIMITIVES_EMITTED
:
172 IMMED_RING(chan
, RING_3D(COUNTER_RESET
),
173 NVC0_3D_COUNTER_RESET_EMITTED_PRIMITIVES
);
175 case PIPE_QUERY_SO_STATISTICS
:
176 BEGIN_RING_NI(chan
, RING_3D(COUNTER_RESET
), 2);
177 OUT_RING (chan
, NVC0_3D_COUNTER_RESET_EMITTED_PRIMITIVES
);
178 OUT_RING (chan
, NVC0_3D_COUNTER_RESET_GENERATED_PRIMITIVES
);
180 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
181 case PIPE_QUERY_TIME_ELAPSED
:
182 nvc0_query_get(chan
, q
, 0x10, 0x00005002);
191 nvc0_query_end(struct pipe_context
*pipe
, struct pipe_query
*pq
)
193 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
194 struct nouveau_channel
*chan
= nvc0
->screen
->base
.channel
;
195 struct nvc0_query
*q
= nvc0_query(pq
);
197 const int index
= 0; /* for multiple vertex streams */
200 case PIPE_QUERY_OCCLUSION_COUNTER
:
201 nvc0_query_get(chan
, q
, 0, 0x0100f002);
202 BEGIN_RING(chan
, RING_3D(SAMPLECNT_ENABLE
), 1);
205 case PIPE_QUERY_PRIMITIVES_GENERATED
:
206 nvc0_query_get(chan
, q
, 0, 0x09005002 | (index
<< 5));
208 case PIPE_QUERY_PRIMITIVES_EMITTED
:
209 nvc0_query_get(chan
, q
, 0, 0x05805002 | (index
<< 5));
211 case PIPE_QUERY_SO_STATISTICS
:
212 nvc0_query_get(chan
, q
, 0x00, 0x05805002 | (index
<< 5));
213 nvc0_query_get(chan
, q
, 0x10, 0x09005002 | (index
<< 5));
215 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
216 case PIPE_QUERY_TIME_ELAPSED
:
217 nvc0_query_get(chan
, q
, 0, 0x00005002);
219 case PIPE_QUERY_GPU_FINISHED
:
220 nvc0_query_get(chan
, q
, 0, 0x1000f010);
228 static INLINE boolean
229 nvc0_query_ready(struct nvc0_query
*q
)
231 return q
->ready
|| (!q
->is64bit
&& (q
->data
[0] == q
->sequence
));
234 static INLINE boolean
235 nvc0_query_wait(struct nvc0_query
*q
)
237 int ret
= nouveau_bo_map(q
->bo
, NOUVEAU_BO_RD
);
240 nouveau_bo_unmap(q
->bo
);
245 nvc0_query_result(struct pipe_context
*pipe
, struct pipe_query
*pq
,
246 boolean wait
, void *result
)
248 struct nvc0_query
*q
= nvc0_query(pq
);
249 uint64_t *res64
= result
;
250 uint32_t *res32
= result
;
251 boolean
*res8
= result
;
252 uint64_t *data64
= (uint64_t *)q
->data
;
254 if (q
->type
== PIPE_QUERY_GPU_FINISHED
) {
255 res8
[0] = nvc0_query_ready(q
);
259 if (!q
->ready
) /* update ? */
260 q
->ready
= nvc0_query_ready(q
);
262 struct nouveau_channel
*chan
= nvc0_context(pipe
)->screen
->base
.channel
;
264 if (nouveau_bo_pending(q
->bo
) & NOUVEAU_BO_WR
) /* for daft apps */
268 if (!nvc0_query_wait(q
))
274 case PIPE_QUERY_OCCLUSION_COUNTER
: /* u32 sequence, u32 count, u64 time */
275 res32
[0] = q
->data
[1];
277 case PIPE_QUERY_PRIMITIVES_GENERATED
: /* u64 count, u64 time */
278 case PIPE_QUERY_PRIMITIVES_EMITTED
: /* u64 count, u64 time */
279 res64
[0] = data64
[0];
281 case PIPE_QUERY_SO_STATISTICS
:
282 res64
[0] = data64
[0];
283 res64
[1] = data64
[1];
285 case PIPE_QUERY_TIMESTAMP_DISJOINT
: /* u32 sequence, u32 0, u64 time */
286 res64
[0] = 1000000000;
287 res8
[8] = (data64
[0] == data64
[2]) ? FALSE
: TRUE
;
289 case PIPE_QUERY_TIME_ELAPSED
:
290 res64
[0] = data64
[1] - data64
[3];
300 nvc0_render_condition(struct pipe_context
*pipe
,
301 struct pipe_query
*pq
, uint mode
)
303 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
304 struct nouveau_channel
*chan
= nvc0
->screen
->base
.channel
;
305 struct nvc0_query
*q
;
308 IMMED_RING(chan
, RING_3D(COND_MODE
), NVC0_3D_COND_MODE_ALWAYS
);
313 if (mode
== PIPE_RENDER_COND_WAIT
||
314 mode
== PIPE_RENDER_COND_BY_REGION_WAIT
) {
315 MARK_RING (chan
, 5, 2);
316 BEGIN_RING(chan
, RING_3D_(NV84_SUBCHAN_QUERY_ADDRESS_HIGH
), 4);
317 OUT_RELOCh(chan
, q
->bo
, q
->offset
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
);
318 OUT_RELOCl(chan
, q
->bo
, q
->offset
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
);
319 OUT_RING (chan
, q
->sequence
);
320 OUT_RING (chan
, 0x00001001);
323 MARK_RING (chan
, 4, 2);
324 BEGIN_RING(chan
, RING_3D(COND_ADDRESS_HIGH
), 3);
325 OUT_RELOCh(chan
, q
->bo
, q
->offset
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
);
326 OUT_RELOCl(chan
, q
->bo
, q
->offset
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
);
327 OUT_RING (chan
, NVC0_3D_COND_MODE_RES_NON_ZERO
);
331 nvc0_init_query_functions(struct nvc0_context
*nvc0
)
333 struct pipe_context
*pipe
= &nvc0
->base
.pipe
;
335 pipe
->create_query
= nvc0_query_create
;
336 pipe
->destroy_query
= nvc0_query_destroy
;
337 pipe
->begin_query
= nvc0_query_begin
;
338 pipe
->end_query
= nvc0_query_end
;
339 pipe
->get_query_result
= nvc0_query_result
;
340 pipe
->render_condition
= nvc0_render_condition
;