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