Merge commit 'origin/gallium-master-merge'
[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 "pipe/p_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_winsys *winsys = nv20_render->nv20->pipe.winsys;
116 nv20_render->pbuffer = winsys->buffer_create(winsys, 64,
117 PIPE_BUFFER_USAGE_VERTEX, size);
118 return winsys->buffer_map(winsys,
119 nv20_render->pbuffer,
120 PIPE_BUFFER_USAGE_CPU_WRITE);
121 }
122
123 static void *
124 nv20_vbuf_render_allocate_vertices( struct vbuf_render *render,
125 ushort vertex_size,
126 ushort nr_vertices )
127 {
128 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
129 size_t size = (size_t)vertex_size * (size_t)nr_vertices;
130 void *buf;
131
132 assert(!nv20_render->pbuffer);
133 assert(!nv20_render->mbuffer);
134
135 /*
136 * For small amount of vertices, don't bother with pipe vertex
137 * buffer, the data will be passed directly via the fifo.
138 */
139 /* XXX: Pipe vertex buffers don't work. */
140 if (0 && size > 16 * 1024)
141 buf = nv20__allocate_pbuffer(nv20_render, size);
142 else
143 buf = nv20__allocate_mbuffer(nv20_render, size);
144
145 if (buf)
146 nv20_render->nv20->dirty |= NV20_NEW_VTXARRAYS;
147
148 return buf;
149 }
150
151 static boolean
152 nv20_vbuf_render_set_primitive( struct vbuf_render *render,
153 unsigned prim )
154 {
155 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
156 unsigned hwp = nvgl_primitive(prim);
157 if (hwp == 0)
158 return FALSE;
159
160 nv20_render->hwprim = hwp;
161 return TRUE;
162 }
163
164 static uint32_t
165 nv20__vtxhwformat(unsigned stride, unsigned fields, unsigned type)
166 {
167 return (stride << NV20TCL_VTXFMT_STRIDE_SHIFT) |
168 (fields << NV20TCL_VTXFMT_SIZE_SHIFT) |
169 (type << NV20TCL_VTXFMT_TYPE_SHIFT);
170 }
171
172 static unsigned
173 nv20__emit_format(struct nv20_context *nv20, enum attrib_emit type, int hwattr)
174 {
175 uint32_t hwfmt = 0;
176 unsigned fields;
177
178 switch (type) {
179 case EMIT_OMIT:
180 hwfmt = nv20__vtxhwformat(0, 0, 2);
181 fields = 0;
182 break;
183 case EMIT_1F:
184 hwfmt = nv20__vtxhwformat(4, 1, 2);
185 fields = 1;
186 break;
187 case EMIT_2F:
188 hwfmt = nv20__vtxhwformat(8, 2, 2);
189 fields = 2;
190 break;
191 case EMIT_3F:
192 hwfmt = nv20__vtxhwformat(12, 3, 2);
193 fields = 3;
194 break;
195 case EMIT_4F:
196 hwfmt = nv20__vtxhwformat(16, 4, 2);
197 fields = 4;
198 break;
199 default:
200 NOUVEAU_ERR("unhandled attrib_emit %d\n", type);
201 return 0;
202 }
203
204 BEGIN_RING(kelvin, NV20TCL_VTXFMT(hwattr), 1);
205 OUT_RING(hwfmt);
206 return fields;
207 }
208
209 static unsigned
210 nv20__emit_vertex_array_format(struct nv20_context *nv20)
211 {
212 struct vertex_info *vinfo = &nv20->vertex_info;
213 int hwattr = NV20TCL_VTXFMT__SIZE;
214 int attr = 0;
215 unsigned nr_fields = 0;
216
217 while (hwattr-- > 0) {
218 if (vinfo->hwfmt[0] & (1 << hwattr)) {
219 nr_fields += nv20__emit_format(nv20,
220 vinfo->attrib[attr].emit, hwattr);
221 attr++;
222 } else
223 nv20__emit_format(nv20, EMIT_OMIT, hwattr);
224 }
225
226 return nr_fields;
227 }
228
229 static void
230 nv20__draw_mbuffer(struct nv20_vbuf_render *nv20_render,
231 const ushort *indices,
232 uint nr_indices)
233 {
234 struct nv20_context *nv20 = nv20_render->nv20;
235 struct vertex_info *vinfo = &nv20->vertex_info;
236 unsigned nr_fields;
237 int max_push;
238 ubyte *data = nv20_render->mbuffer;
239 int vsz = 4 * vinfo->size;
240
241 nr_fields = nv20__emit_vertex_array_format(nv20);
242
243 BEGIN_RING(kelvin, NV20TCL_VERTEX_BEGIN_END, 1);
244 OUT_RING(nv20_render->hwprim);
245
246 max_push = 1200 / nr_fields;
247 while (nr_indices) {
248 int i;
249 int push = MIN2(nr_indices, max_push);
250
251 BEGIN_RING_NI(kelvin, NV20TCL_VERTEX_DATA, push * nr_fields);
252 for (i = 0; i < push; i++) {
253 /* XXX: fixme to handle other than floats? */
254 int f = nr_fields;
255 float *attrv = (float*)&data[indices[i] * vsz];
256 while (f-- > 0)
257 OUT_RINGf(*attrv++);
258 }
259
260 nr_indices -= push;
261 indices += push;
262 }
263
264 BEGIN_RING(kelvin, NV20TCL_VERTEX_BEGIN_END, 1);
265 OUT_RING(NV20TCL_VERTEX_BEGIN_END_STOP);
266 }
267
268 static void
269 nv20__draw_pbuffer(struct nv20_vbuf_render *nv20_render,
270 const ushort *indices,
271 uint nr_indices)
272 {
273 struct nv20_context *nv20 = nv20_render->nv20;
274 int push, i;
275
276 NOUVEAU_ERR("nv20__draw_pbuffer: this path is broken.\n");
277
278 BEGIN_RING(kelvin, NV10TCL_VERTEX_ARRAY_OFFSET_POS, 1);
279 OUT_RELOCl(nv20_render->pbuffer, 0,
280 NOUVEAU_BO_VRAM | NOUVEAU_BO_GART | NOUVEAU_BO_RD);
281
282 BEGIN_RING(kelvin, NV10TCL_VERTEX_BUFFER_BEGIN_END, 1);
283 OUT_RING(nv20_render->hwprim);
284
285 if (nr_indices & 1) {
286 BEGIN_RING(kelvin, NV10TCL_VB_ELEMENT_U32, 1);
287 OUT_RING (indices[0]);
288 indices++; nr_indices--;
289 }
290
291 while (nr_indices) {
292 // XXX too big/small ? check the size
293 push = MIN2(nr_indices, 1200 * 2);
294
295 BEGIN_RING_NI(kelvin, NV10TCL_VB_ELEMENT_U16, push >> 1);
296 for (i = 0; i < push; i+=2)
297 OUT_RING((indices[i+1] << 16) | indices[i]);
298
299 nr_indices -= push;
300 indices += push;
301 }
302
303 BEGIN_RING(kelvin, NV10TCL_VERTEX_BUFFER_BEGIN_END, 1);
304 OUT_RING (0);
305 }
306
307 static void
308 nv20_vbuf_render_draw( struct vbuf_render *render,
309 const ushort *indices,
310 uint nr_indices)
311 {
312 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
313
314 nv20_emit_hw_state(nv20_render->nv20);
315
316 if (nv20_render->pbuffer)
317 nv20__draw_pbuffer(nv20_render, indices, nr_indices);
318 else if (nv20_render->mbuffer)
319 nv20__draw_mbuffer(nv20_render, indices, nr_indices);
320 else
321 assert(0);
322 }
323
324
325 static void
326 nv20_vbuf_render_release_vertices( struct vbuf_render *render,
327 void *vertices,
328 unsigned vertex_size,
329 unsigned vertices_used )
330 {
331 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
332 struct nv20_context *nv20 = nv20_render->nv20;
333 struct pipe_winsys *winsys = nv20->pipe.winsys;
334 struct pipe_screen *pscreen = &nv20->screen->pipe;
335
336 if (nv20_render->pbuffer) {
337 winsys->buffer_unmap(winsys, nv20_render->pbuffer);
338 pipe_buffer_reference(pscreen, &nv20_render->pbuffer, NULL);
339 } else if (nv20_render->mbuffer) {
340 FREE(nv20_render->mbuffer);
341 nv20_render->mbuffer = NULL;
342 } else
343 assert(0);
344 }
345
346
347 static void
348 nv20_vbuf_render_destroy( struct vbuf_render *render )
349 {
350 struct nv20_vbuf_render *nv20_render = nv20_vbuf_render(render);
351
352 assert(!nv20_render->pbuffer);
353 assert(!nv20_render->mbuffer);
354
355 FREE(nv20_render);
356 }
357
358
359 /**
360 * Create a new primitive render.
361 */
362 static struct vbuf_render *
363 nv20_vbuf_render_create( struct nv20_context *nv20 )
364 {
365 struct nv20_vbuf_render *nv20_render = CALLOC_STRUCT(nv20_vbuf_render);
366
367 nv20_render->nv20 = nv20;
368
369 nv20_render->base.max_vertex_buffer_bytes = 16*1024;
370 nv20_render->base.max_indices = 1024;
371 nv20_render->base.get_vertex_info = nv20_vbuf_render_get_vertex_info;
372 nv20_render->base.allocate_vertices =
373 nv20_vbuf_render_allocate_vertices;
374 nv20_render->base.set_primitive = nv20_vbuf_render_set_primitive;
375 nv20_render->base.draw = nv20_vbuf_render_draw;
376 nv20_render->base.release_vertices = nv20_vbuf_render_release_vertices;
377 nv20_render->base.destroy = nv20_vbuf_render_destroy;
378
379 return &nv20_render->base;
380 }
381
382
383 /**
384 * Create a new primitive vbuf/render stage.
385 */
386 struct draw_stage *nv20_draw_vbuf_stage( struct nv20_context *nv20 )
387 {
388 struct vbuf_render *render;
389 struct draw_stage *stage;
390
391 render = nv20_vbuf_render_create(nv20);
392 if(!render)
393 return NULL;
394
395 stage = draw_vbuf_stage( nv20->draw, render );
396 if(!stage) {
397 render->destroy(render);
398 return NULL;
399 }
400
401 return stage;
402 }