gallium: make p_winsys internal
[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 BEGIN_RING(tesla, 0x1440, 1);
64 OUT_RING (0);
65 BEGIN_RING(tesla, 0x1334, 1);
66 OUT_RING (0);
67
68 BEGIN_RING(tesla, NV50TCL_VERTEX_BEGIN, 1);
69 OUT_RING (nv50_prim(mode));
70 BEGIN_RING(tesla, NV50TCL_VERTEX_BUFFER_FIRST, 2);
71 OUT_RING (start);
72 OUT_RING (count);
73 BEGIN_RING(tesla, NV50TCL_VERTEX_END, 1);
74 OUT_RING (0);
75
76 pipe->flush(pipe, 0, NULL);
77 return TRUE;
78 }
79
80 static INLINE void
81 nv50_draw_elements_inline_u08(struct nv50_context *nv50, uint8_t *map,
82 unsigned start, unsigned count)
83 {
84 map += start;
85
86 if (count & 1) {
87 BEGIN_RING(tesla, 0x15e8, 1);
88 OUT_RING (map[0]);
89 map++;
90 count--;
91 }
92
93 while (count) {
94 unsigned nr = count > 2046 ? 2046 : count;
95 int i;
96
97 BEGIN_RING(tesla, 0x400015f0, nr >> 1);
98 for (i = 0; i < nr; i += 2)
99 OUT_RING ((map[1] << 16) | map[0]);
100
101 count -= nr;
102 map += nr;
103 }
104 }
105
106 static INLINE void
107 nv50_draw_elements_inline_u16(struct nv50_context *nv50, uint16_t *map,
108 unsigned start, unsigned count)
109 {
110 map += start;
111
112 if (count & 1) {
113 BEGIN_RING(tesla, 0x15e8, 1);
114 OUT_RING (map[0]);
115 map++;
116 count--;
117 }
118
119 while (count) {
120 unsigned nr = count > 2046 ? 2046 : count;
121 int i;
122
123 BEGIN_RING(tesla, 0x400015f0, nr >> 1);
124 for (i = 0; i < nr; i += 2)
125 OUT_RING ((map[1] << 16) | map[0]);
126
127 count -= nr;
128 map += nr;
129 }
130 }
131
132 static INLINE void
133 nv50_draw_elements_inline_u32(struct nv50_context *nv50, uint8_t *map,
134 unsigned start, unsigned count)
135 {
136 map += start;
137
138 while (count) {
139 unsigned nr = count > 2047 ? 2047 : count;
140
141 BEGIN_RING(tesla, 0x400015e8, nr);
142 OUT_RINGp (map, nr);
143
144 count -= nr;
145 map += nr;
146 }
147 }
148
149 boolean
150 nv50_draw_elements(struct pipe_context *pipe,
151 struct pipe_buffer *indexBuffer, unsigned indexSize,
152 unsigned mode, unsigned start, unsigned count)
153 {
154 struct nv50_context *nv50 = nv50_context(pipe);
155 struct pipe_winsys *ws = pipe->winsys;
156 void *map = ws->buffer_map(ws, indexBuffer, PIPE_BUFFER_USAGE_CPU_READ);
157
158 nv50_state_validate(nv50);
159
160 BEGIN_RING(tesla, 0x142c, 1);
161 OUT_RING (0);
162 BEGIN_RING(tesla, 0x142c, 1);
163 OUT_RING (0);
164
165 BEGIN_RING(tesla, NV50TCL_VERTEX_BEGIN, 1);
166 OUT_RING (nv50_prim(mode));
167 switch (indexSize) {
168 case 1:
169 nv50_draw_elements_inline_u08(nv50, map, start, count);
170 break;
171 case 2:
172 nv50_draw_elements_inline_u16(nv50, map, start, count);
173 break;
174 case 4:
175 nv50_draw_elements_inline_u32(nv50, map, start, count);
176 break;
177 default:
178 assert(0);
179 }
180 BEGIN_RING(tesla, NV50TCL_VERTEX_END, 1);
181 OUT_RING (0);
182
183 pipe->flush(pipe, 0, NULL);
184 return TRUE;
185 }
186
187 void
188 nv50_vbo_validate(struct nv50_context *nv50)
189 {
190 struct nouveau_grobj *tesla = nv50->screen->tesla;
191 struct nouveau_stateobj *vtxbuf, *vtxfmt;
192 int i, vpi = 0;
193
194 vtxbuf = so_new(nv50->vtxelt_nr * 4, nv50->vtxelt_nr * 2);
195 vtxfmt = so_new(nv50->vtxelt_nr + 1, 0);
196 so_method(vtxfmt, tesla, 0x1ac0, nv50->vtxelt_nr);
197
198 for (i = 0; i < nv50->vtxelt_nr; i++) {
199 struct pipe_vertex_element *ve = &nv50->vtxelt[i];
200 struct pipe_vertex_buffer *vb =
201 &nv50->vtxbuf[ve->vertex_buffer_index];
202
203 switch (ve->src_format) {
204 case PIPE_FORMAT_R32G32B32A32_FLOAT:
205 so_data(vtxfmt, 0x7e080000 | i);
206 break;
207 case PIPE_FORMAT_R32G32B32_FLOAT:
208 so_data(vtxfmt, 0x7e100000 | i);
209 break;
210 case PIPE_FORMAT_R32G32_FLOAT:
211 so_data(vtxfmt, 0x7e200000 | i);
212 break;
213 case PIPE_FORMAT_R32_FLOAT:
214 so_data(vtxfmt, 0x7e900000 | i);
215 break;
216 case PIPE_FORMAT_R8G8B8A8_UNORM:
217 so_data(vtxfmt, 0x24500000 | i);
218 break;
219 default:
220 {
221 NOUVEAU_ERR("invalid vbo format %s\n",
222 pf_name(ve->src_format));
223 assert(0);
224 return;
225 }
226 }
227
228 so_method(vtxbuf, tesla, 0x900 + (i * 16), 3);
229 so_data (vtxbuf, 0x20000000 | vb->stride);
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_HIGH, 0, 0);
233 so_reloc (vtxbuf, vb->buffer, vb->buffer_offset +
234 ve->src_offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_GART |
235 NOUVEAU_BO_RD | NOUVEAU_BO_LOW, 0, 0);
236 }
237
238 so_ref (vtxfmt, &nv50->state.vtxfmt);
239 so_ref (vtxbuf, &nv50->state.vtxbuf);
240 }
241