nv50: flatten simple IF/ELSE/ENDIF constructs
[mesa.git] / src / gallium / drivers / nv50 / nv50_query.c
1 /*
2 * Copyright 2008 Ben Skeggs
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
23 #include "pipe/p_context.h"
24 #include "util/u_inlines.h"
25
26 #include "nv50_context.h"
27
28 struct nv50_query {
29 struct nouveau_bo *bo;
30 unsigned type;
31 boolean ready;
32 uint64_t result;
33 };
34
35 static INLINE struct nv50_query *
36 nv50_query(struct pipe_query *pipe)
37 {
38 return (struct nv50_query *)pipe;
39 }
40
41 static struct pipe_query *
42 nv50_query_create(struct pipe_context *pipe, unsigned type)
43 {
44 struct nouveau_device *dev = nouveau_screen(pipe->screen)->device;
45 struct nv50_query *q = CALLOC_STRUCT(nv50_query);
46 int ret;
47
48 assert (q->type == PIPE_QUERY_OCCLUSION_COUNTER);
49 q->type = type;
50
51 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 256,
52 16, &q->bo);
53 if (ret) {
54 FREE(q);
55 return NULL;
56 }
57
58 return (struct pipe_query *)q;
59 }
60
61 static void
62 nv50_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
63 {
64 struct nv50_query *q = nv50_query(pq);
65
66 if (q) {
67 nouveau_bo_ref(NULL, &q->bo);
68 FREE(q);
69 }
70 }
71
72 static void
73 nv50_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
74 {
75 struct nv50_context *nv50 = nv50_context(pipe);
76 struct nouveau_channel *chan = nv50->screen->base.channel;
77 struct nouveau_grobj *tesla = nv50->screen->tesla;
78 struct nv50_query *q = nv50_query(pq);
79
80 BEGIN_RING(chan, tesla, NV50TCL_SAMPLECNT_RESET, 1);
81 OUT_RING (chan, 1);
82 BEGIN_RING(chan, tesla, NV50TCL_SAMPLECNT_ENABLE, 1);
83 OUT_RING (chan, 1);
84
85 q->ready = FALSE;
86 }
87
88 static void
89 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
90 {
91 struct nv50_context *nv50 = nv50_context(pipe);
92 struct nouveau_channel *chan = nv50->screen->base.channel;
93 struct nouveau_grobj *tesla = nv50->screen->tesla;
94 struct nv50_query *q = nv50_query(pq);
95
96 MARK_RING (chan, 5, 2); /* flush on lack of space or relocs */
97 BEGIN_RING(chan, tesla, NV50TCL_QUERY_ADDRESS_HIGH, 4);
98 OUT_RELOCh(chan, q->bo, 0, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
99 OUT_RELOCl(chan, q->bo, 0, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
100 OUT_RING (chan, 0x00000000);
101 OUT_RING (chan, 0x0100f002);
102
103 BEGIN_RING(chan, tesla, NV50TCL_SAMPLECNT_ENABLE, 1);
104 OUT_RING (chan, 0);
105 }
106
107 static boolean
108 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
109 boolean wait, void *vresult)
110 {
111 uint64_t *result = (uint64_t*)vresult;
112 struct nv50_query *q = nv50_query(pq);
113 int ret;
114
115 if (!q->ready) {
116 ret = nouveau_bo_map(q->bo, NOUVEAU_BO_RD |
117 (wait ? 0 : NOUVEAU_BO_NOWAIT));
118 if (ret)
119 return false;
120 q->result = ((uint32_t *)q->bo->map)[1];
121 q->ready = TRUE;
122 nouveau_bo_unmap(q->bo);
123 }
124
125 *result = q->result;
126 return q->ready;
127 }
128
129 static void
130 nv50_render_condition(struct pipe_context *pipe,
131 struct pipe_query *pq, uint mode)
132 {
133 struct nv50_context *nv50 = nv50_context(pipe);
134 struct nouveau_channel *chan = nv50->screen->base.channel;
135 struct nouveau_grobj *tesla = nv50->screen->tesla;
136 struct nv50_query *q;
137
138 if (!pq) {
139 BEGIN_RING(chan, tesla, NV50TCL_COND_MODE, 1);
140 OUT_RING (chan, NV50TCL_COND_MODE_ALWAYS);
141 return;
142 }
143 q = nv50_query(pq);
144
145 if (mode == PIPE_RENDER_COND_WAIT ||
146 mode == PIPE_RENDER_COND_BY_REGION_WAIT) {
147 /* XXX: big fence, FIFO semaphore might be better */
148 BEGIN_RING(chan, tesla, 0x0110, 1);
149 OUT_RING (chan, 0);
150 }
151
152 BEGIN_RING(chan, tesla, NV50TCL_COND_ADDRESS_HIGH, 3);
153 OUT_RELOCh(chan, q->bo, 0, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
154 OUT_RELOCl(chan, q->bo, 0, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
155 OUT_RING (chan, NV50TCL_COND_MODE_RES);
156 }
157
158 void
159 nv50_init_query_functions(struct nv50_context *nv50)
160 {
161 nv50->pipe.create_query = nv50_query_create;
162 nv50->pipe.destroy_query = nv50_query_destroy;
163 nv50->pipe.begin_query = nv50_query_begin;
164 nv50->pipe.end_query = nv50_query_end;
165 nv50->pipe.get_query_result = nv50_query_result;
166 nv50->pipe.render_condition = nv50_render_condition;
167 }