nvc0: don't visit target blocks of a loop break multiple times
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_push.c
1
2 #include "pipe/p_context.h"
3 #include "pipe/p_state.h"
4 #include "util/u_inlines.h"
5 #include "util/u_format.h"
6 #include "translate/translate.h"
7
8 #include "nvc0_context.h"
9 #include "nvc0_resource.h"
10
11 #include "nvc0_3d.xml.h"
12
13 struct push_context {
14 struct nouveau_channel *chan;
15
16 void *idxbuf;
17
18 float edgeflag;
19 int edgeflag_attr;
20
21 uint32_t vertex_words;
22 uint32_t packet_vertex_limit;
23
24 struct translate *translate;
25
26 boolean primitive_restart;
27 uint32_t prim;
28 uint32_t restart_index;
29 uint32_t instance_id;
30 };
31
32 static INLINE unsigned
33 prim_restart_search_i08(uint8_t *elts, unsigned push, uint8_t index)
34 {
35 unsigned i;
36 for (i = 0; i < push; ++i)
37 if (elts[i] == index)
38 break;
39 return i;
40 }
41
42 static INLINE unsigned
43 prim_restart_search_i16(uint16_t *elts, unsigned push, uint16_t index)
44 {
45 unsigned i;
46 for (i = 0; i < push; ++i)
47 if (elts[i] == index)
48 break;
49 return i;
50 }
51
52 static INLINE unsigned
53 prim_restart_search_i32(uint32_t *elts, unsigned push, uint32_t index)
54 {
55 unsigned i;
56 for (i = 0; i < push; ++i)
57 if (elts[i] == index)
58 break;
59 return i;
60 }
61
62 static void
63 emit_vertices_i08(struct push_context *ctx, unsigned start, unsigned count)
64 {
65 uint8_t *elts = (uint8_t *)ctx->idxbuf + start;
66
67 while (count) {
68 unsigned push = MIN2(count, ctx->packet_vertex_limit);
69 unsigned size, nr;
70
71 nr = push;
72 if (ctx->primitive_restart)
73 nr = prim_restart_search_i08(elts, push, ctx->restart_index);
74
75 size = ctx->vertex_words * nr;
76
77 BEGIN_RING_NI(ctx->chan, RING_3D(VERTEX_DATA), size);
78
79 ctx->translate->run_elts8(ctx->translate, elts, nr, ctx->instance_id,
80 ctx->chan->cur);
81
82 ctx->chan->cur += size;
83 count -= nr;
84 elts += nr;
85
86 if (nr != push) {
87 count--;
88 elts++;
89 BEGIN_RING(ctx->chan, RING_3D(VERTEX_END_GL), 2);
90 OUT_RING (ctx->chan, 0);
91 OUT_RING (ctx->chan, NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_CONT |
92 (ctx->prim & ~NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT));
93 }
94 }
95 }
96
97 static void
98 emit_vertices_i16(struct push_context *ctx, unsigned start, unsigned count)
99 {
100 uint16_t *elts = (uint16_t *)ctx->idxbuf + start;
101
102 while (count) {
103 unsigned push = MIN2(count, ctx->packet_vertex_limit);
104 unsigned size, nr;
105
106 nr = push;
107 if (ctx->primitive_restart)
108 nr = prim_restart_search_i16(elts, push, ctx->restart_index);
109
110 size = ctx->vertex_words * nr;
111
112 BEGIN_RING_NI(ctx->chan, RING_3D(VERTEX_DATA), size);
113
114 ctx->translate->run_elts16(ctx->translate, elts, nr, ctx->instance_id,
115 ctx->chan->cur);
116
117 ctx->chan->cur += size;
118 count -= nr;
119 elts += nr;
120
121 if (nr != push) {
122 count--;
123 elts++;
124 BEGIN_RING(ctx->chan, RING_3D(VERTEX_END_GL), 2);
125 OUT_RING (ctx->chan, 0);
126 OUT_RING (ctx->chan, NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_CONT |
127 (ctx->prim & ~NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT));
128 }
129 }
130 }
131
132 static void
133 emit_vertices_i32(struct push_context *ctx, unsigned start, unsigned count)
134 {
135 uint32_t *elts = (uint32_t *)ctx->idxbuf + start;
136
137 while (count) {
138 unsigned push = MIN2(count, ctx->packet_vertex_limit);
139 unsigned size, nr;
140
141 nr = push;
142 if (ctx->primitive_restart)
143 nr = prim_restart_search_i32(elts, push, ctx->restart_index);
144
145 size = ctx->vertex_words * nr;
146
147 BEGIN_RING_NI(ctx->chan, RING_3D(VERTEX_DATA), size);
148
149 ctx->translate->run_elts(ctx->translate, elts, nr, ctx->instance_id,
150 ctx->chan->cur);
151
152 ctx->chan->cur += size;
153 count -= nr;
154 elts += nr;
155
156 if (nr != push) {
157 count--;
158 elts++;
159 BEGIN_RING(ctx->chan, RING_3D(VERTEX_END_GL), 2);
160 OUT_RING (ctx->chan, 0);
161 OUT_RING (ctx->chan, NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_CONT |
162 (ctx->prim & ~NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT));
163 }
164 }
165 }
166
167 static void
168 emit_vertices_seq(struct push_context *ctx, unsigned start, unsigned count)
169 {
170 while (count) {
171 unsigned push = MIN2(count, ctx->packet_vertex_limit);
172 unsigned size = ctx->vertex_words * push;
173
174 BEGIN_RING_NI(ctx->chan, RING_3D(VERTEX_DATA), size);
175
176 ctx->translate->run(ctx->translate, start, push, ctx->instance_id,
177 ctx->chan->cur);
178 ctx->chan->cur += size;
179 count -= push;
180 start += push;
181 }
182 }
183
184
185 #define NVC0_PRIM_GL_CASE(n) \
186 case PIPE_PRIM_##n: return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_##n
187
188 static INLINE unsigned
189 nvc0_prim_gl(unsigned prim)
190 {
191 switch (prim) {
192 NVC0_PRIM_GL_CASE(POINTS);
193 NVC0_PRIM_GL_CASE(LINES);
194 NVC0_PRIM_GL_CASE(LINE_LOOP);
195 NVC0_PRIM_GL_CASE(LINE_STRIP);
196 NVC0_PRIM_GL_CASE(TRIANGLES);
197 NVC0_PRIM_GL_CASE(TRIANGLE_STRIP);
198 NVC0_PRIM_GL_CASE(TRIANGLE_FAN);
199 NVC0_PRIM_GL_CASE(QUADS);
200 NVC0_PRIM_GL_CASE(QUAD_STRIP);
201 NVC0_PRIM_GL_CASE(POLYGON);
202 NVC0_PRIM_GL_CASE(LINES_ADJACENCY);
203 NVC0_PRIM_GL_CASE(LINE_STRIP_ADJACENCY);
204 NVC0_PRIM_GL_CASE(TRIANGLES_ADJACENCY);
205 NVC0_PRIM_GL_CASE(TRIANGLE_STRIP_ADJACENCY);
206 /*
207 NVC0_PRIM_GL_CASE(PATCHES); */
208 default:
209 return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS;
210 break;
211 }
212 }
213
214 void
215 nvc0_push_vbo(struct nvc0_context *nvc0, const struct pipe_draw_info *info)
216 {
217 struct push_context ctx;
218 unsigned i, index_size;
219 unsigned inst = info->instance_count;
220 boolean apply_bias = info->indexed && info->index_bias;
221
222 ctx.chan = nvc0->screen->base.channel;
223 ctx.translate = nvc0->vertex->translate;
224 ctx.packet_vertex_limit = nvc0->vertex->vtx_per_packet_max;
225 ctx.vertex_words = nvc0->vertex->vtx_size;
226
227 for (i = 0; i < nvc0->num_vtxbufs; ++i) {
228 uint8_t *data;
229 struct pipe_vertex_buffer *vb = &nvc0->vtxbuf[i];
230 struct nvc0_resource *res = nvc0_resource(vb->buffer);
231
232 data = nvc0_resource_map_offset(nvc0, res,
233 vb->buffer_offset, NOUVEAU_BO_RD);
234
235 if (apply_bias && likely(!(nvc0->vertex->instance_bufs & (1 << i))))
236 data += info->index_bias * vb->stride;
237
238 ctx.translate->set_buffer(ctx.translate, i, data, vb->stride, ~0);
239 }
240
241 if (info->indexed) {
242 ctx.idxbuf = nvc0_resource_map_offset(nvc0,
243 nvc0_resource(nvc0->idxbuf.buffer),
244 nvc0->idxbuf.offset, NOUVEAU_BO_RD);
245 if (!ctx.idxbuf)
246 return;
247 index_size = nvc0->idxbuf.index_size;
248 ctx.primitive_restart = info->primitive_restart;
249 ctx.restart_index = info->restart_index;
250 } else {
251 ctx.idxbuf = NULL;
252 index_size = 0;
253 ctx.primitive_restart = FALSE;
254 ctx.restart_index = 0;
255 }
256
257 ctx.instance_id = info->start_instance;
258 ctx.prim = nvc0_prim_gl(info->mode);
259
260 while (inst--) {
261 BEGIN_RING(ctx.chan, RING_3D(VERTEX_BEGIN_GL), 1);
262 OUT_RING (ctx.chan, ctx.prim);
263 switch (index_size) {
264 case 0:
265 emit_vertices_seq(&ctx, info->start, info->count);
266 break;
267 case 1:
268 emit_vertices_i08(&ctx, info->start, info->count);
269 break;
270 case 2:
271 emit_vertices_i16(&ctx, info->start, info->count);
272 break;
273 case 4:
274 emit_vertices_i32(&ctx, info->start, info->count);
275 break;
276 default:
277 assert(0);
278 break;
279 }
280 IMMED_RING(ctx.chan, RING_3D(VERTEX_END_GL), 0);
281
282 ctx.instance_id++;
283 ctx.prim |= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
284 }
285
286 if (info->indexed)
287 nvc0_resource_unmap(nvc0_resource(nvc0->idxbuf.buffer));
288
289 for (i = 0; i < nvc0->num_vtxbufs; ++i)
290 nvc0_resource_unmap(nvc0_resource(nvc0->vtxbuf[i].buffer));
291 }