Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / drivers / nv10 / nv10_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 "draw/draw_vbuf.h"
42 #include "pipe/p_debug.h"
43 #include "pipe/p_util.h"
44 #include "pipe/p_inlines.h"
45 #include "pipe/p_winsys.h"
46
47 #include "nv10_context.h"
48 #include "nv10_state.h"
49
50
51 /**
52 * Primitive renderer for nv10.
53 */
54 struct nv10_vbuf_render {
55 struct vbuf_render base;
56
57 struct nv10_context *nv10;
58
59 /** Vertex buffer */
60 struct pipe_buffer* buffer;
61
62 /** Vertex size in bytes */
63 unsigned vertex_size;
64
65 /** Hardware primitive */
66 unsigned hwprim;
67 };
68
69
70 void nv10_vtxbuf_bind( struct nv10_context* nv10 )
71 {
72 int i;
73 for(i = 0; i < 8; i++) {
74 BEGIN_RING(celsius, NV10TCL_VERTEX_ARRAY_ATTRIB_OFFSET(i), 1);
75 OUT_RING(0/*nv10->vtxbuf*/);
76 BEGIN_RING(celsius, NV10TCL_VERTEX_ARRAY_ATTRIB_FORMAT(i) ,1);
77 OUT_RING(0/*XXX*/);
78 }
79 }
80
81 /**
82 * Basically a cast wrapper.
83 */
84 static INLINE struct nv10_vbuf_render *
85 nv10_vbuf_render( struct vbuf_render *render )
86 {
87 assert(render);
88 return (struct nv10_vbuf_render *)render;
89 }
90
91
92 static const struct vertex_info *
93 nv10_vbuf_render_get_vertex_info( struct vbuf_render *render )
94 {
95 struct nv10_vbuf_render *nv10_render = nv10_vbuf_render(render);
96 struct nv10_context *nv10 = nv10_render->nv10;
97
98 nv10_emit_hw_state(nv10);
99
100 return &nv10->vertex_info;
101 }
102
103
104 static void *
105 nv10_vbuf_render_allocate_vertices( struct vbuf_render *render,
106 ushort vertex_size,
107 ushort nr_vertices )
108 {
109 struct nv10_vbuf_render *nv10_render = nv10_vbuf_render(render);
110 struct nv10_context *nv10 = nv10_render->nv10;
111 struct pipe_winsys *winsys = nv10->pipe.winsys;
112 size_t size = (size_t)vertex_size * (size_t)nr_vertices;
113
114 assert(!nv10_render->buffer);
115 nv10_render->buffer = winsys->buffer_create(winsys, 64, PIPE_BUFFER_USAGE_VERTEX, size);
116
117 nv10->dirty |= NV10_NEW_VTXARRAYS;
118
119 return winsys->buffer_map(winsys,
120 nv10_render->buffer,
121 PIPE_BUFFER_USAGE_CPU_WRITE);
122 }
123
124
125 static void
126 nv10_vbuf_render_set_primitive( struct vbuf_render *render,
127 unsigned prim )
128 {
129 struct nv10_vbuf_render *nv10_render = nv10_vbuf_render(render);
130 nv10_render->hwprim = prim + 1;
131 }
132
133
134 static void
135 nv10_vbuf_render_draw( struct vbuf_render *render,
136 const ushort *indices,
137 uint nr_indices)
138 {
139 struct nv10_vbuf_render *nv10_render = nv10_vbuf_render(render);
140 struct nv10_context *nv10 = nv10_render->nv10;
141 int push, i;
142
143 nv10_emit_hw_state(nv10);
144
145 BEGIN_RING(celsius, NV10TCL_VERTEX_ARRAY_OFFSET_POS, 1);
146 OUT_RELOCl(nv10_render->buffer, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_GART | NOUVEAU_BO_RD);
147
148 BEGIN_RING(celsius, NV10TCL_VERTEX_BUFFER_BEGIN_END, 1);
149 OUT_RING(nv10_render->hwprim);
150
151 if (nr_indices & 1) {
152 BEGIN_RING(celsius, NV10TCL_VB_ELEMENT_U32, 1);
153 OUT_RING (indices[0]);
154 indices++; nr_indices--;
155 }
156
157 while (nr_indices) {
158 // XXX too big/small ? check the size
159 push = MIN2(nr_indices, 1200 * 2);
160
161 BEGIN_RING_NI(celsius, NV10TCL_VB_ELEMENT_U16, push >> 1);
162 for (i = 0; i < push; i+=2)
163 OUT_RING((indices[i+1] << 16) | indices[i]);
164
165 nr_indices -= push;
166 indices += push;
167 }
168
169 BEGIN_RING(celsius, NV10TCL_VERTEX_BUFFER_BEGIN_END, 1);
170 OUT_RING (0);
171 }
172
173
174 static void
175 nv10_vbuf_render_release_vertices( struct vbuf_render *render,
176 void *vertices,
177 unsigned vertex_size,
178 unsigned vertices_used )
179 {
180 struct nv10_vbuf_render *nv10_render = nv10_vbuf_render(render);
181 struct nv10_context *nv10 = nv10_render->nv10;
182 struct pipe_winsys *winsys = nv10->pipe.winsys;
183
184 assert(nv10_render->buffer);
185 winsys->buffer_unmap(winsys, nv10_render->buffer);
186 pipe_buffer_reference(winsys, &nv10_render->buffer, NULL);
187 }
188
189
190 static void
191 nv10_vbuf_render_destroy( struct vbuf_render *render )
192 {
193 struct nv10_vbuf_render *nv10_render = nv10_vbuf_render(render);
194 FREE(nv10_render);
195 }
196
197
198 /**
199 * Create a new primitive render.
200 */
201 static struct vbuf_render *
202 nv10_vbuf_render_create( struct nv10_context *nv10 )
203 {
204 struct nv10_vbuf_render *nv10_render = CALLOC_STRUCT(nv10_vbuf_render);
205
206 nv10_render->nv10 = nv10;
207
208 nv10_render->base.max_vertex_buffer_bytes = 16*1024;
209 nv10_render->base.max_indices = 1024;
210 nv10_render->base.get_vertex_info = nv10_vbuf_render_get_vertex_info;
211 nv10_render->base.allocate_vertices = nv10_vbuf_render_allocate_vertices;
212 nv10_render->base.set_primitive = nv10_vbuf_render_set_primitive;
213 nv10_render->base.draw = nv10_vbuf_render_draw;
214 nv10_render->base.release_vertices = nv10_vbuf_render_release_vertices;
215 nv10_render->base.destroy = nv10_vbuf_render_destroy;
216
217 return &nv10_render->base;
218 }
219
220
221 /**
222 * Create a new primitive vbuf/render stage.
223 */
224 struct draw_stage *nv10_draw_vbuf_stage( struct nv10_context *nv10 )
225 {
226 struct vbuf_render *render;
227 struct draw_stage *stage;
228
229 render = nv10_vbuf_render_create(nv10);
230 if(!render)
231 return NULL;
232
233 stage = draw_vbuf_stage( nv10->draw, render );
234 if(!stage) {
235 render->destroy(render);
236 return NULL;
237 }
238
239 return stage;
240 }