Merge branch 'mesa_7_5_branch'
[mesa.git] / src / gallium / drivers / nv20 / nv20_prim_vbuf.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * \file
30 * Build post-transformation, post-clipping vertex buffers and element
31 * lists by hooking into the end of the primitive pipeline and
32 * manipulating the vertex_id field in the vertex headers.
33 *
34 * XXX: work in progress
35 *
36 * \author José Fonseca <jrfonseca@tungstengraphics.com>
37 * \author Keith Whitwell <keith@tungstengraphics.com>
38 */
39
40
41 #include "util/u_debug.h"
42 #include "pipe/p_inlines.h"
43 #include "pipe/internal/p_winsys_screen.h"
44
45 #include "nv20_context.h"
46 #include "nv20_state.h"
47
48 #include "draw/draw_vbuf.h"
49
50 /**
51 * Primitive renderer for nv20.
52 */
53 struct nv20_vbuf_render {
54 struct vbuf_render base;
55
56 struct nv20_context *nv20;
57
58 /** Vertex buffer in VRAM */
59 struct pipe_buffer *pbuffer;
60
61 /** Vertex buffer in normal memory */
62 void *mbuffer;
63
64 /** Vertex size in bytes */
65 /*unsigned vertex_size;*/
66
67 /** Hardware primitive */
68 unsigned hwprim;
69 };
70
71 /**
72 * Basically a cast wrapper.
73 */
74 static INLINE struct nv20_vbuf_render *
75 nv20_vbuf_render(struct vbuf_render *render)
76 {
77 assert(render);
78 return (struct nv20_vbuf_render *)render;
79 }
80
81 void nv20_vtxbuf_bind( struct nv20_context* nv20 )
82 {
83 #if 0
84 int i;
85 for(i = 0; i < NV20TCL_VTXBUF_ADDRESS__SIZE; i++) {
86 BEGIN_RING(kelvin, NV20TCL_VTXBUF_ADDRESS(i), 1);
87 OUT_RING(0/*nv20->vtxbuf*/);
88 BEGIN_RING(kelvin, NV20TCL_VTXFMT(i) ,1);
89 OUT_RING(0/*XXX*/);
90 }
91 #endif
92 }
93
94 static const struct vertex_info *
95 nv20_vbuf_render_get_vertex_info( struct vbuf_render *render )
96 {
97 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
98 struct nv20_context *nv20 = nv20_render->nv20;
99
100 nv20_emit_hw_state(nv20);
101
102 return &nv20->vertex_info;
103 }
104
105 static void *
106 nv20__allocate_mbuffer(struct nv20_vbuf_render *nv20_render, size_t size)
107 {
108 nv20_render->mbuffer = MALLOC(size);
109 return nv20_render->mbuffer;
110 }
111
112 static void
113 nv20__allocate_pbuffer(struct nv20_vbuf_render *nv20_render, size_t size)
114 {
115 struct pipe_screen *screen = nv20_render->nv20->pipe.screen;
116 nv20_render->pbuffer = screen->buffer_create(screen, 64,
117 PIPE_BUFFER_USAGE_VERTEX, size);
118 }
119
120 static boolean
121 nv20_vbuf_render_allocate_vertices( struct vbuf_render *render,
122 ushort vertex_size,
123 ushort nr_vertices )
124 {
125 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
126 size_t size = (size_t)vertex_size * (size_t)nr_vertices;
127 void *buf;
128
129 assert(!nv20_render->pbuffer);
130 assert(!nv20_render->mbuffer);
131
132 /*
133 * For small amount of vertices, don't bother with pipe vertex
134 * buffer, the data will be passed directly via the fifo.
135 */
136 /* XXX: Pipe vertex buffers don't work. */
137 if (0 && size > 16 * 1024) {
138 nv20__allocate_pbuffer(nv20_render, size);
139 /* umm yeah so this is ugly */
140 buf = nv20_render->pbuffer;
141 } else {
142 buf = nv20__allocate_mbuffer(nv20_render, size);
143 }
144
145 if (buf)
146 nv20_render->nv20->dirty |= NV20_NEW_VTXARRAYS;
147
148 return buf ? TRUE : FALSE;
149 }
150
151 static void *
152 nv20_vbuf_render_map_vertices( struct vbuf_render *render )
153 {
154 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
155 struct pipe_screen *pscreen = nv20_render->nv20->pipe.screen;
156
157 if (nv20_render->pbuffer) {
158 return pipe_buffer_map(pscreen, nv20_render->pbuffer,
159 PIPE_BUFFER_USAGE_CPU_WRITE);
160 } else if (nv20_render->mbuffer) {
161 return nv20_render->mbuffer;
162 } else
163 assert(0);
164
165 /* warnings be gone */
166 return NULL;
167 }
168
169 static void
170 nv20_vbuf_render_unmap_vertices( struct vbuf_render *render,
171 ushort min_index,
172 ushort max_index )
173 {
174 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
175 struct pipe_screen *pscreen = nv20_render->nv20->pipe.screen;
176
177 if (nv20_render->pbuffer)
178 pipe_buffer_unmap(pscreen, nv20_render->pbuffer);
179 }
180
181 static boolean
182 nv20_vbuf_render_set_primitive( struct vbuf_render *render,
183 unsigned prim )
184 {
185 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
186 unsigned hwp = nvgl_primitive(prim);
187 if (hwp == 0)
188 return FALSE;
189
190 nv20_render->hwprim = hwp;
191 return TRUE;
192 }
193
194 static uint32_t
195 nv20__vtxhwformat(unsigned stride, unsigned fields, unsigned type)
196 {
197 return (stride << NV20TCL_VTXFMT_STRIDE_SHIFT) |
198 (fields << NV20TCL_VTXFMT_SIZE_SHIFT) |
199 (type << NV20TCL_VTXFMT_TYPE_SHIFT);
200 }
201
202 static unsigned
203 nv20__emit_format(struct nv20_context *nv20, enum attrib_emit type, int hwattr)
204 {
205 uint32_t hwfmt = 0;
206 unsigned fields;
207
208 switch (type) {
209 case EMIT_OMIT:
210 hwfmt = nv20__vtxhwformat(0, 0, 2);
211 fields = 0;
212 break;
213 case EMIT_1F:
214 hwfmt = nv20__vtxhwformat(4, 1, 2);
215 fields = 1;
216 break;
217 case EMIT_2F:
218 hwfmt = nv20__vtxhwformat(8, 2, 2);
219 fields = 2;
220 break;
221 case EMIT_3F:
222 hwfmt = nv20__vtxhwformat(12, 3, 2);
223 fields = 3;
224 break;
225 case EMIT_4F:
226 hwfmt = nv20__vtxhwformat(16, 4, 2);
227 fields = 4;
228 break;
229 default:
230 NOUVEAU_ERR("unhandled attrib_emit %d\n", type);
231 return 0;
232 }
233
234 BEGIN_RING(kelvin, NV20TCL_VTXFMT(hwattr), 1);
235 OUT_RING(hwfmt);
236 return fields;
237 }
238
239 static unsigned
240 nv20__emit_vertex_array_format(struct nv20_context *nv20)
241 {
242 struct vertex_info *vinfo = &nv20->vertex_info;
243 int hwattr = NV20TCL_VTXFMT__SIZE;
244 int attr = 0;
245 unsigned nr_fields = 0;
246
247 while (hwattr-- > 0) {
248 if (vinfo->hwfmt[0] & (1 << hwattr)) {
249 nr_fields += nv20__emit_format(nv20,
250 vinfo->attrib[attr].emit, hwattr);
251 attr++;
252 } else
253 nv20__emit_format(nv20, EMIT_OMIT, hwattr);
254 }
255
256 return nr_fields;
257 }
258
259 static void
260 nv20__draw_mbuffer(struct nv20_vbuf_render *nv20_render,
261 const ushort *indices,
262 uint nr_indices)
263 {
264 struct nv20_context *nv20 = nv20_render->nv20;
265 struct vertex_info *vinfo = &nv20->vertex_info;
266 unsigned nr_fields;
267 int max_push;
268 ubyte *data = nv20_render->mbuffer;
269 int vsz = 4 * vinfo->size;
270
271 nr_fields = nv20__emit_vertex_array_format(nv20);
272
273 BEGIN_RING(kelvin, NV20TCL_VERTEX_BEGIN_END, 1);
274 OUT_RING(nv20_render->hwprim);
275
276 max_push = 1200 / nr_fields;
277 while (nr_indices) {
278 int i;
279 int push = MIN2(nr_indices, max_push);
280
281 BEGIN_RING_NI(kelvin, NV20TCL_VERTEX_DATA, push * nr_fields);
282 for (i = 0; i < push; i++) {
283 /* XXX: fixme to handle other than floats? */
284 int f = nr_fields;
285 float *attrv = (float*)&data[indices[i] * vsz];
286 while (f-- > 0)
287 OUT_RINGf(*attrv++);
288 }
289
290 nr_indices -= push;
291 indices += push;
292 }
293
294 BEGIN_RING(kelvin, NV20TCL_VERTEX_BEGIN_END, 1);
295 OUT_RING(NV20TCL_VERTEX_BEGIN_END_STOP);
296 }
297
298 static void
299 nv20__draw_pbuffer(struct nv20_vbuf_render *nv20_render,
300 const ushort *indices,
301 uint nr_indices)
302 {
303 struct nv20_context *nv20 = nv20_render->nv20;
304 int push, i;
305
306 NOUVEAU_ERR("nv20__draw_pbuffer: this path is broken.\n");
307
308 BEGIN_RING(kelvin, NV10TCL_VERTEX_ARRAY_OFFSET_POS, 1);
309 OUT_RELOCl(nv20_render->pbuffer, 0,
310 NOUVEAU_BO_VRAM | NOUVEAU_BO_GART | NOUVEAU_BO_RD);
311
312 BEGIN_RING(kelvin, NV10TCL_VERTEX_BUFFER_BEGIN_END, 1);
313 OUT_RING(nv20_render->hwprim);
314
315 if (nr_indices & 1) {
316 BEGIN_RING(kelvin, NV10TCL_VB_ELEMENT_U32, 1);
317 OUT_RING (indices[0]);
318 indices++; nr_indices--;
319 }
320
321 while (nr_indices) {
322 // XXX too big/small ? check the size
323 push = MIN2(nr_indices, 1200 * 2);
324
325 BEGIN_RING_NI(kelvin, NV10TCL_VB_ELEMENT_U16, push >> 1);
326 for (i = 0; i < push; i+=2)
327 OUT_RING((indices[i+1] << 16) | indices[i]);
328
329 nr_indices -= push;
330 indices += push;
331 }
332
333 BEGIN_RING(kelvin, NV10TCL_VERTEX_BUFFER_BEGIN_END, 1);
334 OUT_RING (0);
335 }
336
337 static void
338 nv20_vbuf_render_draw( struct vbuf_render *render,
339 const ushort *indices,
340 uint nr_indices)
341 {
342 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
343
344 nv20_emit_hw_state(nv20_render->nv20);
345
346 if (nv20_render->pbuffer)
347 nv20__draw_pbuffer(nv20_render, indices, nr_indices);
348 else if (nv20_render->mbuffer)
349 nv20__draw_mbuffer(nv20_render, indices, nr_indices);
350 else
351 assert(0);
352 }
353
354
355 static void
356 nv20_vbuf_render_release_vertices( struct vbuf_render *render )
357 {
358 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
359 struct nv20_context *nv20 = nv20_render->nv20;
360
361 if (nv20_render->pbuffer) {
362 pipe_buffer_reference(&nv20_render->pbuffer, NULL);
363 } else if (nv20_render->mbuffer) {
364 FREE(nv20_render->mbuffer);
365 nv20_render->mbuffer = NULL;
366 } else
367 assert(0);
368 }
369
370
371 static void
372 nv20_vbuf_render_destroy( struct vbuf_render *render )
373 {
374 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
375
376 assert(!nv20_render->pbuffer);
377 assert(!nv20_render->mbuffer);
378
379 FREE(nv20_render);
380 }
381
382
383 /**
384 * Create a new primitive render.
385 */
386 static struct vbuf_render *
387 nv20_vbuf_render_create( struct nv20_context *nv20 )
388 {
389 struct nv20_vbuf_render *nv20_render = CALLOC_STRUCT(nv20_vbuf_render);
390
391 nv20_render->nv20 = nv20;
392
393 nv20_render->base.max_vertex_buffer_bytes = 16*1024;
394 nv20_render->base.max_indices = 1024;
395 nv20_render->base.get_vertex_info = nv20_vbuf_render_get_vertex_info;
396 nv20_render->base.allocate_vertices =
397 nv20_vbuf_render_allocate_vertices;
398 nv20_render->base.map_vertices = nv20_vbuf_render_map_vertices;
399 nv20_render->base.unmap_vertices = nv20_vbuf_render_unmap_vertices;
400 nv20_render->base.set_primitive = nv20_vbuf_render_set_primitive;
401 nv20_render->base.draw = nv20_vbuf_render_draw;
402 nv20_render->base.release_vertices = nv20_vbuf_render_release_vertices;
403 nv20_render->base.destroy = nv20_vbuf_render_destroy;
404
405 return &nv20_render->base;
406 }
407
408
409 /**
410 * Create a new primitive vbuf/render stage.
411 */
412 struct draw_stage *nv20_draw_vbuf_stage( struct nv20_context *nv20 )
413 {
414 struct vbuf_render *render;
415 struct draw_stage *stage;
416
417 render = nv20_vbuf_render_create(nv20);
418 if(!render)
419 return NULL;
420
421 stage = draw_vbuf_stage( nv20->draw, render );
422 if(!stage) {
423 render->destroy(render);
424 return NULL;
425 }
426
427 return stage;
428 }