1 /*
2 * Copyright 2012 Red Hat Inc.
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: Ben Skeggs
23 *
24 */
25
26 #include "pipe/p_context.h"
27 #include "pipe/p_state.h"
28 #include "util/u_inlines.h"
29 #include "util/format/u_format.h"
30 #include "translate/translate.h"
31
32 #include "nv_object.xml.h"
33 #include "nv30/nv30-40_3d.xml.h"
34 #include "nv30/nv30_context.h"
35 #include "nv30/nv30_resource.h"
36 #include "nv30/nv30_winsys.h"
37
38 struct push_context {
39 struct nouveau_pushbuf *push;
40
41 const void *idxbuf;
42
43 float edgeflag;
44 int edgeflag_attr;
45
46 uint32_t vertex_words;
47 uint32_t packet_vertex_limit;
48
49 struct translate *translate;
50
51 bool primitive_restart;
52 uint32_t prim;
53 uint32_t restart_index;
54 };
55
56 static inline unsigned
prim_restart_search_i08(uint8_t * elts,unsigned push,uint8_t index)57 prim_restart_search_i08(uint8_t *elts, unsigned push, uint8_t index)
58 {
59 unsigned i;
60 for (i = 0; i < push; ++i)
61 if (elts[i] == index)
62 break;
63 return i;
64 }
65
66 static inline unsigned
prim_restart_search_i16(uint16_t * elts,unsigned push,uint16_t index)67 prim_restart_search_i16(uint16_t *elts, unsigned push, uint16_t index)
68 {
69 unsigned i;
70 for (i = 0; i < push; ++i)
71 if (elts[i] == index)
72 break;
73 return i;
74 }
75
76 static inline unsigned
prim_restart_search_i32(uint32_t * elts,unsigned push,uint32_t index)77 prim_restart_search_i32(uint32_t *elts, unsigned push, uint32_t index)
78 {
79 unsigned i;
80 for (i = 0; i < push; ++i)
81 if (elts[i] == index)
82 break;
83 return i;
84 }
85
86 static void
emit_vertices_i08(struct push_context * ctx,unsigned start,unsigned count)87 emit_vertices_i08(struct push_context *ctx, unsigned start, unsigned count)
88 {
89 uint8_t *elts = (uint8_t *)ctx->idxbuf + start;
90
91 while (count) {
92 unsigned push = MIN2(count, ctx->packet_vertex_limit);
93 unsigned size, nr;
94
95 nr = push;
96 if (ctx->primitive_restart)
97 nr = prim_restart_search_i08(elts, push, ctx->restart_index);
98
99 size = ctx->vertex_words * nr;
100
101 BEGIN_NI04(ctx->push, NV30_3D(VERTEX_DATA), size);
102
103 ctx->translate->run_elts8(ctx->translate, elts, nr, 0, 0, ctx->push->cur);
104
105 ctx->push->cur += size;
106 count -= nr;
107 elts += nr;
108
109 if (nr != push) {
110 BEGIN_NV04(ctx->push, NV30_3D(VB_ELEMENT_U32), 1);
111 PUSH_DATA (ctx->push, ctx->restart_index);
112 count--;
113 elts++;
114 }
115 }
116 }
117
118 static void
emit_vertices_i16(struct push_context * ctx,unsigned start,unsigned count)119 emit_vertices_i16(struct push_context *ctx, unsigned start, unsigned count)
120 {
121 uint16_t *elts = (uint16_t *)ctx->idxbuf + start;
122
123 while (count) {
124 unsigned push = MIN2(count, ctx->packet_vertex_limit);
125 unsigned size, nr;
126
127 nr = push;
128 if (ctx->primitive_restart)
129 nr = prim_restart_search_i16(elts, push, ctx->restart_index);
130
131 size = ctx->vertex_words * nr;
132
133 BEGIN_NI04(ctx->push, NV30_3D(VERTEX_DATA), size);
134
135 ctx->translate->run_elts16(ctx->translate, elts, nr, 0, 0, ctx->push->cur);
136
137 ctx->push->cur += size;
138 count -= nr;
139 elts += nr;
140
141 if (nr != push) {
142 BEGIN_NV04(ctx->push, NV30_3D(VB_ELEMENT_U32), 1);
143 PUSH_DATA (ctx->push, ctx->restart_index);
144 count--;
145 elts++;
146 }
147 }
148 }
149
150 static void
emit_vertices_i32(struct push_context * ctx,unsigned start,unsigned count)151 emit_vertices_i32(struct push_context *ctx, unsigned start, unsigned count)
152 {
153 uint32_t *elts = (uint32_t *)ctx->idxbuf + start;
154
155 while (count) {
156 unsigned push = MIN2(count, ctx->packet_vertex_limit);
157 unsigned size, nr;
158
159 nr = push;
160 if (ctx->primitive_restart)
161 nr = prim_restart_search_i32(elts, push, ctx->restart_index);
162
163 size = ctx->vertex_words * nr;
164
165 BEGIN_NI04(ctx->push, NV30_3D(VERTEX_DATA), size);
166
167 ctx->translate->run_elts(ctx->translate, elts, nr, 0, 0, ctx->push->cur);
168
169 ctx->push->cur += size;
170 count -= nr;
171 elts += nr;
172
173 if (nr != push) {
174 BEGIN_NV04(ctx->push, NV30_3D(VB_ELEMENT_U32), 1);
175 PUSH_DATA (ctx->push, ctx->restart_index);
176 count--;
177 elts++;
178 }
179 }
180 }
181
182 static void
emit_vertices_seq(struct push_context * ctx,unsigned start,unsigned count)183 emit_vertices_seq(struct push_context *ctx, unsigned start, unsigned count)
184 {
185 while (count) {
186 unsigned push = MIN2(count, ctx->packet_vertex_limit);
187 unsigned size = ctx->vertex_words * push;
188
189 BEGIN_NI04(ctx->push, NV30_3D(VERTEX_DATA), size);
190
191 ctx->translate->run(ctx->translate, start, push, 0, 0, ctx->push->cur);
192 ctx->push->cur += size;
193 count -= push;
194 start += push;
195 }
196 }
197
198 void
nv30_push_vbo(struct nv30_context * nv30,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)199 nv30_push_vbo(struct nv30_context *nv30, const struct pipe_draw_info *info,
200 const struct pipe_draw_start_count_bias *draw)
201 {
202 struct push_context ctx;
203 unsigned i, index_size;
204 bool apply_bias = info->index_size && draw->index_bias;
205
206 ctx.push = nv30->base.pushbuf;
207 ctx.translate = nv30->vertex->translate;
208 ctx.packet_vertex_limit = nv30->vertex->vtx_per_packet_max;
209 ctx.vertex_words = nv30->vertex->vtx_size;
210
211 for (i = 0; i < nv30->num_vtxbufs; ++i) {
212 uint8_t *data;
213 struct pipe_vertex_buffer *vb = &nv30->vtxbuf[i];
214 struct nv04_resource *res = nv04_resource(vb->buffer.resource);
215
216 if (!vb->buffer.resource) {
217 continue;
218 }
219
220 data = nouveau_resource_map_offset(&nv30->base, res,
221 vb->buffer_offset, NOUVEAU_BO_RD);
222
223 if (apply_bias)
224 data += draw->index_bias * nv30->vertex->strides[i];
225
226 ctx.translate->set_buffer(ctx.translate, i, data, nv30->vertex->strides[i], ~0);
227 }
228
229 if (info->index_size) {
230 if (!info->has_user_indices)
231 ctx.idxbuf = nouveau_resource_map_offset(&nv30->base,
232 nv04_resource(info->index.resource), 0,
233 NOUVEAU_BO_RD);
234 else
235 ctx.idxbuf = (char*)info->index.user;
236 if (!ctx.idxbuf) {
237 nv30_state_release(nv30);
238 return;
239 }
240 index_size = info->index_size;
241 ctx.primitive_restart = info->primitive_restart;
242 ctx.restart_index = info->restart_index;
243 } else {
244 ctx.idxbuf = NULL;
245 index_size = 0;
246 ctx.primitive_restart = false;
247 ctx.restart_index = 0;
248 }
249
250 if (nv30->screen->eng3d->oclass >= NV40_3D_CLASS) {
251 BEGIN_NV04(ctx.push, NV40_3D(PRIM_RESTART_ENABLE), 2);
252 PUSH_DATA (ctx.push, info->primitive_restart);
253 PUSH_DATA (ctx.push, info->restart_index);
254 nv30->state.prim_restart = info->primitive_restart;
255 }
256
257 ctx.prim = nv30_prim_gl(info->mode);
258
259 PUSH_RESET(ctx.push, BUFCTX_IDXBUF);
260 BEGIN_NV04(ctx.push, NV30_3D(VERTEX_BEGIN_END), 1);
261 PUSH_DATA (ctx.push, ctx.prim);
262 switch (index_size) {
263 case 0:
264 emit_vertices_seq(&ctx, draw->start, draw->count);
265 break;
266 case 1:
267 emit_vertices_i08(&ctx, draw->start, draw->count);
268 break;
269 case 2:
270 emit_vertices_i16(&ctx, draw->start, draw->count);
271 break;
272 case 4:
273 emit_vertices_i32(&ctx, draw->start, draw->count);
274 break;
275 default:
276 assert(0);
277 break;
278 }
279 BEGIN_NV04(ctx.push, NV30_3D(VERTEX_BEGIN_END), 1);
280 PUSH_DATA (ctx.push, NV30_3D_VERTEX_BEGIN_END_STOP);
281
282 if (info->index_size && !info->has_user_indices)
283 nouveau_resource_unmap(nv04_resource(info->index.resource));
284
285 for (i = 0; i < nv30->num_vtxbufs; ++i) {
286 if (nv30->vtxbuf[i].buffer.resource) {
287 nouveau_resource_unmap(nv04_resource(nv30->vtxbuf[i].buffer.resource));
288 }
289 }
290
291 nv30_state_release(nv30);
292 }
293