dd9b85b7208441e4a74c91aeca387d3b6c178e2c
[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 "nv50/nv50_query.h"
29 #include "nv50/nv50_query_hw.h"
30
31 static struct pipe_query *
32 nv50_create_query(struct pipe_context *pipe, unsigned type, unsigned index)
33 {
34 struct nv50_context *nv50 = nv50_context(pipe);
35 struct nv50_query *q;
36
37 q = nv50_hw_create_query(nv50, type, index);
38 return (struct pipe_query *)q;
39 }
40
41 static void
42 nv50_destroy_query(struct pipe_context *pipe, struct pipe_query *pq)
43 {
44 struct nv50_query *q = nv50_query(pq);
45 q->funcs->destroy_query(nv50_context(pipe), q);
46 }
47
48 static boolean
49 nv50_begin_query(struct pipe_context *pipe, struct pipe_query *pq)
50 {
51 struct nv50_query *q = nv50_query(pq);
52 return q->funcs->begin_query(nv50_context(pipe), q);
53 }
54
55 static void
56 nv50_end_query(struct pipe_context *pipe, struct pipe_query *pq)
57 {
58 struct nv50_query *q = nv50_query(pq);
59 q->funcs->end_query(nv50_context(pipe), q);
60 }
61
62 static boolean
63 nv50_get_query_result(struct pipe_context *pipe, struct pipe_query *pq,
64 boolean wait, union pipe_query_result *result)
65 {
66 struct nv50_query *q = nv50_query(pq);
67 return q->funcs->get_query_result(nv50_context(pipe), q, wait, result);
68 }
69
70 static void
71 nv50_render_condition(struct pipe_context *pipe,
72 struct pipe_query *pq,
73 boolean condition, uint mode)
74 {
75 struct nv50_context *nv50 = nv50_context(pipe);
76 struct nouveau_pushbuf *push = nv50->base.pushbuf;
77 struct nv50_query *q = nv50_query(pq);
78 struct nv50_hw_query *hq = nv50_hw_query(q);
79 uint32_t cond;
80 bool wait =
81 mode != PIPE_RENDER_COND_NO_WAIT &&
82 mode != PIPE_RENDER_COND_BY_REGION_NO_WAIT;
83
84 if (!pq) {
85 cond = NV50_3D_COND_MODE_ALWAYS;
86 }
87 else {
88 /* NOTE: comparison of 2 queries only works if both have completed */
89 switch (q->type) {
90 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
91 cond = condition ? NV50_3D_COND_MODE_EQUAL :
92 NV50_3D_COND_MODE_NOT_EQUAL;
93 wait = true;
94 break;
95 case PIPE_QUERY_OCCLUSION_COUNTER:
96 case PIPE_QUERY_OCCLUSION_PREDICATE:
97 if (likely(!condition)) {
98 if (unlikely(hq->nesting))
99 cond = wait ? NV50_3D_COND_MODE_NOT_EQUAL :
100 NV50_3D_COND_MODE_ALWAYS;
101 else
102 cond = NV50_3D_COND_MODE_RES_NON_ZERO;
103 } else {
104 cond = wait ? NV50_3D_COND_MODE_EQUAL : NV50_3D_COND_MODE_ALWAYS;
105 }
106 break;
107 default:
108 assert(!"render condition query not a predicate");
109 cond = NV50_3D_COND_MODE_ALWAYS;
110 break;
111 }
112 }
113
114 nv50->cond_query = pq;
115 nv50->cond_cond = condition;
116 nv50->cond_condmode = cond;
117 nv50->cond_mode = mode;
118
119 if (!pq) {
120 PUSH_SPACE(push, 2);
121 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
122 PUSH_DATA (push, cond);
123 return;
124 }
125
126 PUSH_SPACE(push, 9);
127
128 if (wait) {
129 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
130 PUSH_DATA (push, 0);
131 }
132
133 PUSH_REFN (push, hq->bo, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
134 BEGIN_NV04(push, NV50_3D(COND_ADDRESS_HIGH), 3);
135 PUSH_DATAh(push, hq->bo->offset + hq->offset);
136 PUSH_DATA (push, hq->bo->offset + hq->offset);
137 PUSH_DATA (push, cond);
138
139 BEGIN_NV04(push, NV50_2D(COND_ADDRESS_HIGH), 2);
140 PUSH_DATAh(push, hq->bo->offset + hq->offset);
141 PUSH_DATA (push, hq->bo->offset + hq->offset);
142 }
143
144 void
145 nv50_init_query_functions(struct nv50_context *nv50)
146 {
147 struct pipe_context *pipe = &nv50->base.pipe;
148
149 pipe->create_query = nv50_create_query;
150 pipe->destroy_query = nv50_destroy_query;
151 pipe->begin_query = nv50_begin_query;
152 pipe->end_query = nv50_end_query;
153 pipe->get_query_result = nv50_get_query_result;
154 pipe->render_condition = nv50_render_condition;
155 }