nouveau: gallium directory structure changed again..
[mesa.git] / src / gallium / drivers / nv50 / nv50_vbo.c
1 /*
2 * Copyright 2008 Ben Skeggs
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "pipe/p_context.h"
24 #include "pipe/p_state.h"
25
26 #include "nv50_context.h"
27
28 static INLINE unsigned
29 nv50_prim(unsigned mode)
30 {
31 switch (mode) {
32 case PIPE_PRIM_POINTS: return NV50TCL_VERTEX_BEGIN_POINTS;
33 case PIPE_PRIM_LINES: return NV50TCL_VERTEX_BEGIN_LINES;
34 case PIPE_PRIM_LINE_LOOP: return NV50TCL_VERTEX_BEGIN_LINE_LOOP;
35 case PIPE_PRIM_LINE_STRIP: return NV50TCL_VERTEX_BEGIN_LINE_STRIP;
36 case PIPE_PRIM_TRIANGLES: return NV50TCL_VERTEX_BEGIN_TRIANGLES;
37 case PIPE_PRIM_TRIANGLE_STRIP:
38 return NV50TCL_VERTEX_BEGIN_TRIANGLE_STRIP;
39 case PIPE_PRIM_TRIANGLE_FAN: return NV50TCL_VERTEX_BEGIN_TRIANGLE_FAN;
40 case PIPE_PRIM_QUADS: return NV50TCL_VERTEX_BEGIN_QUADS;
41 case PIPE_PRIM_QUAD_STRIP: return NV50TCL_VERTEX_BEGIN_QUAD_STRIP;
42 case PIPE_PRIM_POLYGON: return NV50TCL_VERTEX_BEGIN_POLYGON;
43 default:
44 break;
45 }
46
47 NOUVEAU_ERR("invalid primitive type %d\n", mode);
48 return NV50TCL_VERTEX_BEGIN_POINTS;
49 }
50
51 boolean
52 nv50_draw_arrays(struct pipe_context *pipe, unsigned mode, unsigned start,
53 unsigned count)
54 {
55 struct nv50_context *nv50 = nv50_context(pipe);
56
57 nv50_state_validate(nv50);
58
59 BEGIN_RING(tesla, 0x142c, 1);
60 OUT_RING (0);
61 BEGIN_RING(tesla, 0x142c, 1);
62 OUT_RING (0);
63
64 BEGIN_RING(tesla, NV50TCL_VERTEX_BEGIN, 1);
65 OUT_RING (nv50_prim(mode));
66 BEGIN_RING(tesla, NV50TCL_VERTEX_BUFFER_FIRST, 2);
67 OUT_RING (start);
68 OUT_RING (count);
69 BEGIN_RING(tesla, NV50TCL_VERTEX_END, 1);
70 OUT_RING (0);
71
72 pipe->flush(pipe, 0, NULL);
73 return TRUE;
74 }
75
76 static INLINE void
77 nv50_draw_elements_inline_u08(struct nv50_context *nv50, uint8_t *map,
78 unsigned start, unsigned count)
79 {
80 map += start;
81
82 if (count & 1) {
83 BEGIN_RING(tesla, 0x15e8, 1);
84 OUT_RING (map[0]);
85 map++;
86 count--;
87 }
88
89 while (count) {
90 unsigned nr = count > 2046 ? 2046 : count;
91 int i;
92
93 BEGIN_RING(tesla, 0x400015f0, nr >> 1);
94 for (i = 0; i < nr; i += 2)
95 OUT_RING ((map[1] << 16) | map[0]);
96
97 count -= nr;
98 map += nr;
99 }
100 }
101
102 static INLINE void
103 nv50_draw_elements_inline_u16(struct nv50_context *nv50, uint16_t *map,
104 unsigned start, unsigned count)
105 {
106 map += start;
107
108 if (count & 1) {
109 BEGIN_RING(tesla, 0x15e8, 1);
110 OUT_RING (map[0]);
111 map++;
112 count--;
113 }
114
115 while (count) {
116 unsigned nr = count > 2046 ? 2046 : count;
117 int i;
118
119 BEGIN_RING(tesla, 0x400015f0, nr >> 1);
120 for (i = 0; i < nr; i += 2)
121 OUT_RING ((map[1] << 16) | map[0]);
122
123 count -= nr;
124 map += nr;
125 }
126 }
127
128 static INLINE void
129 nv50_draw_elements_inline_u32(struct nv50_context *nv50, uint8_t *map,
130 unsigned start, unsigned count)
131 {
132 map += start;
133
134 while (count) {
135 unsigned nr = count > 2047 ? 2047 : count;
136
137 BEGIN_RING(tesla, 0x400015e8, nr);
138 OUT_RINGp (map, nr);
139
140 count -= nr;
141 map += nr;
142 }
143 }
144
145 boolean
146 nv50_draw_elements(struct pipe_context *pipe,
147 struct pipe_buffer *indexBuffer, unsigned indexSize,
148 unsigned mode, unsigned start, unsigned count)
149 {
150 struct nv50_context *nv50 = nv50_context(pipe);
151 struct pipe_winsys *ws = pipe->winsys;
152 void *map = ws->buffer_map(ws, indexBuffer, PIPE_BUFFER_USAGE_CPU_READ);
153
154 nv50_state_validate(nv50);
155
156 BEGIN_RING(tesla, 0x142c, 1);
157 OUT_RING (0);
158 BEGIN_RING(tesla, 0x142c, 1);
159 OUT_RING (0);
160
161 BEGIN_RING(tesla, NV50TCL_VERTEX_BEGIN, 1);
162 OUT_RING (nv50_prim(mode));
163 switch (indexSize) {
164 case 1:
165 nv50_draw_elements_inline_u08(nv50, map, start, count);
166 break;
167 case 2:
168 nv50_draw_elements_inline_u16(nv50, map, start, count);
169 break;
170 case 4:
171 nv50_draw_elements_inline_u32(nv50, map, start, count);
172 break;
173 default:
174 assert(0);
175 }
176 BEGIN_RING(tesla, NV50TCL_VERTEX_END, 1);
177 OUT_RING (0);
178
179 pipe->flush(pipe, 0, NULL);
180 return TRUE;
181 }
182
183 void
184 nv50_vbo_validate(struct nv50_context *nv50)
185 {
186 struct nouveau_grobj *tesla = nv50->screen->tesla;
187 struct nouveau_stateobj *vtxbuf, *vtxfmt;
188 int i, vpi = 0;
189
190 vtxbuf = so_new(nv50->vtxelt_nr * 4, nv50->vtxelt_nr * 2);
191 vtxfmt = so_new(nv50->vtxelt_nr + 1, 0);
192 so_method(vtxfmt, tesla, 0x1ac0, nv50->vtxelt_nr);
193
194 for (i = 0; i < nv50->vtxelt_nr; i++) {
195 struct pipe_vertex_element *ve = &nv50->vtxelt[i];
196 struct pipe_vertex_buffer *vb =
197 &nv50->vtxbuf[ve->vertex_buffer_index];
198
199 switch (ve->src_format) {
200 case PIPE_FORMAT_R32G32B32A32_FLOAT:
201 so_data(vtxfmt, 0x7e080000 | i);
202 break;
203 case PIPE_FORMAT_R32G32B32_FLOAT:
204 so_data(vtxfmt, 0x7e100000 | i);
205 break;
206 case PIPE_FORMAT_R32G32_FLOAT:
207 so_data(vtxfmt, 0x7e200000 | i);
208 break;
209 case PIPE_FORMAT_R32_FLOAT:
210 so_data(vtxfmt, 0x7e900000 | i);
211 break;
212 case PIPE_FORMAT_R8G8B8A8_UNORM:
213 so_data(vtxfmt, 0x24500000 | i);
214 break;
215 default:
216 {
217 NOUVEAU_ERR("invalid vbo format %s\n",
218 pf_name(ve->src_format));
219 assert(0);
220 return;
221 }
222 }
223
224 so_method(vtxbuf, tesla, 0x900 + (i * 16), 3);
225 so_data (vtxbuf, 0x20000000 | vb->pitch);
226 so_reloc (vtxbuf, vb->buffer, vb->buffer_offset +
227 ve->src_offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_GART |
228 NOUVEAU_BO_RD | NOUVEAU_BO_HIGH, 0, 0);
229 so_reloc (vtxbuf, vb->buffer, vb->buffer_offset +
230 ve->src_offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_GART |
231 NOUVEAU_BO_RD | NOUVEAU_BO_LOW, 0, 0);
232 }
233
234 so_ref (vtxfmt, &nv50->state.vtxfmt);
235 so_ref (vtxbuf, &nv50->state.vtxbuf);
236 }
237