i965: fix bugs in projective texture coordinates
[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_winsys *winsys = nv20_render->nv20->pipe.winsys;
156
157 if (nv20_render->pbuffer) {
158 return winsys->buffer_map(winsys,
159 nv20_render->pbuffer,
160 PIPE_BUFFER_USAGE_CPU_WRITE);
161 } else if (nv20_render->mbuffer) {
162 return nv20_render->mbuffer;
163 } else
164 assert(0);
165
166 /* warnings be gone */
167 return NULL;
168 }
169
170 static void
171 nv20_vbuf_render_unmap_vertices( struct vbuf_render *render,
172 ushort min_index,
173 ushort max_index )
174 {
175 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
176 struct pipe_winsys *winsys = nv20_render->nv20->pipe.winsys;
177
178 if (nv20_render->pbuffer)
179 winsys->buffer_unmap(winsys, nv20_render->pbuffer);
180 }
181
182 static boolean
183 nv20_vbuf_render_set_primitive( struct vbuf_render *render,
184 unsigned prim )
185 {
186 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
187 unsigned hwp = nvgl_primitive(prim);
188 if (hwp == 0)
189 return FALSE;
190
191 nv20_render->hwprim = hwp;
192 return TRUE;
193 }
194
195 static uint32_t
196 nv20__vtxhwformat(unsigned stride, unsigned fields, unsigned type)
197 {
198 return (stride << NV20TCL_VTXFMT_STRIDE_SHIFT) |
199 (fields << NV20TCL_VTXFMT_SIZE_SHIFT) |
200 (type << NV20TCL_VTXFMT_TYPE_SHIFT);
201 }
202
203 static unsigned
204 nv20__emit_format(struct nv20_context *nv20, enum attrib_emit type, int hwattr)
205 {
206 uint32_t hwfmt = 0;
207 unsigned fields;
208
209 switch (type) {
210 case EMIT_OMIT:
211 hwfmt = nv20__vtxhwformat(0, 0, 2);
212 fields = 0;
213 break;
214 case EMIT_1F:
215 hwfmt = nv20__vtxhwformat(4, 1, 2);
216 fields = 1;
217 break;
218 case EMIT_2F:
219 hwfmt = nv20__vtxhwformat(8, 2, 2);
220 fields = 2;
221 break;
222 case EMIT_3F:
223 hwfmt = nv20__vtxhwformat(12, 3, 2);
224 fields = 3;
225 break;
226 case EMIT_4F:
227 hwfmt = nv20__vtxhwformat(16, 4, 2);
228 fields = 4;
229 break;
230 default:
231 NOUVEAU_ERR("unhandled attrib_emit %d\n", type);
232 return 0;
233 }
234
235 BEGIN_RING(kelvin, NV20TCL_VTXFMT(hwattr), 1);
236 OUT_RING(hwfmt);
237 return fields;
238 }
239
240 static unsigned
241 nv20__emit_vertex_array_format(struct nv20_context *nv20)
242 {
243 struct vertex_info *vinfo = &nv20->vertex_info;
244 int hwattr = NV20TCL_VTXFMT__SIZE;
245 int attr = 0;
246 unsigned nr_fields = 0;
247
248 while (hwattr-- > 0) {
249 if (vinfo->hwfmt[0] & (1 << hwattr)) {
250 nr_fields += nv20__emit_format(nv20,
251 vinfo->attrib[attr].emit, hwattr);
252 attr++;
253 } else
254 nv20__emit_format(nv20, EMIT_OMIT, hwattr);
255 }
256
257 return nr_fields;
258 }
259
260 static void
261 nv20__draw_mbuffer(struct nv20_vbuf_render *nv20_render,
262 const ushort *indices,
263 uint nr_indices)
264 {
265 struct nv20_context *nv20 = nv20_render->nv20;
266 struct vertex_info *vinfo = &nv20->vertex_info;
267 unsigned nr_fields;
268 int max_push;
269 ubyte *data = nv20_render->mbuffer;
270 int vsz = 4 * vinfo->size;
271
272 nr_fields = nv20__emit_vertex_array_format(nv20);
273
274 BEGIN_RING(kelvin, NV20TCL_VERTEX_BEGIN_END, 1);
275 OUT_RING(nv20_render->hwprim);
276
277 max_push = 1200 / nr_fields;
278 while (nr_indices) {
279 int i;
280 int push = MIN2(nr_indices, max_push);
281
282 BEGIN_RING_NI(kelvin, NV20TCL_VERTEX_DATA, push * nr_fields);
283 for (i = 0; i < push; i++) {
284 /* XXX: fixme to handle other than floats? */
285 int f = nr_fields;
286 float *attrv = (float*)&data[indices[i] * vsz];
287 while (f-- > 0)
288 OUT_RINGf(*attrv++);
289 }
290
291 nr_indices -= push;
292 indices += push;
293 }
294
295 BEGIN_RING(kelvin, NV20TCL_VERTEX_BEGIN_END, 1);
296 OUT_RING(NV20TCL_VERTEX_BEGIN_END_STOP);
297 }
298
299 static void
300 nv20__draw_pbuffer(struct nv20_vbuf_render *nv20_render,
301 const ushort *indices,
302 uint nr_indices)
303 {
304 struct nv20_context *nv20 = nv20_render->nv20;
305 int push, i;
306
307 NOUVEAU_ERR("nv20__draw_pbuffer: this path is broken.\n");
308
309 BEGIN_RING(kelvin, NV10TCL_VERTEX_ARRAY_OFFSET_POS, 1);
310 OUT_RELOCl(nv20_render->pbuffer, 0,
311 NOUVEAU_BO_VRAM | NOUVEAU_BO_GART | NOUVEAU_BO_RD);
312
313 BEGIN_RING(kelvin, NV10TCL_VERTEX_BUFFER_BEGIN_END, 1);
314 OUT_RING(nv20_render->hwprim);
315
316 if (nr_indices & 1) {
317 BEGIN_RING(kelvin, NV10TCL_VB_ELEMENT_U32, 1);
318 OUT_RING (indices[0]);
319 indices++; nr_indices--;
320 }
321
322 while (nr_indices) {
323 // XXX too big/small ? check the size
324 push = MIN2(nr_indices, 1200 * 2);
325
326 BEGIN_RING_NI(kelvin, NV10TCL_VB_ELEMENT_U16, push >> 1);
327 for (i = 0; i < push; i+=2)
328 OUT_RING((indices[i+1] << 16) | indices[i]);
329
330 nr_indices -= push;
331 indices += push;
332 }
333
334 BEGIN_RING(kelvin, NV10TCL_VERTEX_BUFFER_BEGIN_END, 1);
335 OUT_RING (0);
336 }
337
338 static void
339 nv20_vbuf_render_draw( struct vbuf_render *render,
340 const ushort *indices,
341 uint nr_indices)
342 {
343 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
344
345 nv20_emit_hw_state(nv20_render->nv20);
346
347 if (nv20_render->pbuffer)
348 nv20__draw_pbuffer(nv20_render, indices, nr_indices);
349 else if (nv20_render->mbuffer)
350 nv20__draw_mbuffer(nv20_render, indices, nr_indices);
351 else
352 assert(0);
353 }
354
355
356 static void
357 nv20_vbuf_render_release_vertices( struct vbuf_render *render )
358 {
359 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
360 struct nv20_context *nv20 = nv20_render->nv20;
361 struct pipe_screen *pscreen = &nv20->screen->pipe;
362
363 if (nv20_render->pbuffer) {
364 pipe_buffer_reference(&nv20_render->pbuffer, NULL);
365 } else if (nv20_render->mbuffer) {
366 FREE(nv20_render->mbuffer);
367 nv20_render->mbuffer = NULL;
368 } else
369 assert(0);
370 }
371
372
373 static void
374 nv20_vbuf_render_destroy( struct vbuf_render *render )
375 {
376 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
377
378 assert(!nv20_render->pbuffer);
379 assert(!nv20_render->mbuffer);
380
381 FREE(nv20_render);
382 }
383
384
385 /**
386 * Create a new primitive render.
387 */
388 static struct vbuf_render *
389 nv20_vbuf_render_create( struct nv20_context *nv20 )
390 {
391 struct nv20_vbuf_render *nv20_render = CALLOC_STRUCT(nv20_vbuf_render);
392
393 nv20_render->nv20 = nv20;
394
395 nv20_render->base.max_vertex_buffer_bytes = 16*1024;
396 nv20_render->base.max_indices = 1024;
397 nv20_render->base.get_vertex_info = nv20_vbuf_render_get_vertex_info;
398 nv20_render->base.allocate_vertices =
399 nv20_vbuf_render_allocate_vertices;
400 nv20_render->base.map_vertices = nv20_vbuf_render_map_vertices;
401 nv20_render->base.unmap_vertices = nv20_vbuf_render_unmap_vertices;
402 nv20_render->base.set_primitive = nv20_vbuf_render_set_primitive;
403 nv20_render->base.draw = nv20_vbuf_render_draw;
404 nv20_render->base.release_vertices = nv20_vbuf_render_release_vertices;
405 nv20_render->base.destroy = nv20_vbuf_render_destroy;
406
407 return &nv20_render->base;
408 }
409
410
411 /**
412 * Create a new primitive vbuf/render stage.
413 */
414 struct draw_stage *nv20_draw_vbuf_stage( struct nv20_context *nv20 )
415 {
416 struct vbuf_render *render;
417 struct draw_stage *stage;
418
419 render = nv20_vbuf_render_create(nv20);
420 if(!render)
421 return NULL;
422
423 stage = draw_vbuf_stage( nv20->draw, render );
424 if(!stage) {
425 render->destroy(render);
426 return NULL;
427 }
428
429 return stage;
430 }