dri/nouveau: Pack client arrays as they're copied to the real BO.
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_vbo_t.c
1 /*
2 * Copyright (C) 2009-2010 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "nouveau_bufferobj.h"
28 #include "nouveau_util.h"
29
30 #include "main/bufferobj.h"
31 #include "main/image.h"
32
33 /* Arbitrary pushbuf length we can assume we can get with a single
34 * WAIT_RING. */
35 #define PUSHBUF_DWORDS 2048
36
37 /* Functions to set up struct nouveau_array_state from something like
38 * a GL array or index buffer. */
39
40 static void
41 vbo_init_array(struct nouveau_array_state *a, int attr, int stride,
42 int fields, int type, struct gl_buffer_object *obj,
43 const void *ptr, GLboolean map)
44 {
45 a->attr = attr;
46 a->stride = stride;
47 a->fields = fields;
48 a->type = type;
49
50 if (_mesa_is_bufferobj(obj)) {
51 nouveau_bo_ref(to_nouveau_bufferobj(obj)->bo, &a->bo);
52 a->offset = (intptr_t)ptr;
53
54 if (map) {
55 nouveau_bo_map(a->bo, NOUVEAU_BO_RD);
56 a->buf = a->bo->map + a->offset;
57 } else {
58 a->buf = NULL;
59 }
60
61 } else {
62 nouveau_bo_ref(NULL, &a->bo);
63 a->offset = 0;
64
65 if (map)
66 a->buf = ptr;
67 else
68 a->buf = NULL;
69 }
70
71 if (a->buf)
72 get_array_extract(a, &a->extract_u, &a->extract_f);
73 }
74
75 static void
76 vbo_deinit_array(struct nouveau_array_state *a)
77 {
78 if (a->bo) {
79 if (a->bo->map)
80 nouveau_bo_unmap(a->bo);
81 nouveau_bo_ref(NULL, &a->bo);
82 }
83
84 a->buf = NULL;
85 a->fields = 0;
86 }
87
88 static void
89 vbo_init_arrays(GLcontext *ctx, const struct _mesa_index_buffer *ib,
90 const struct gl_client_array **arrays)
91 {
92 struct nouveau_render_state *render = to_render_state(ctx);
93 int i;
94
95 if (ib)
96 vbo_init_array(&render->ib, 0, 0, ib->count, ib->type,
97 ib->obj, ib->ptr, GL_TRUE);
98
99 for (i = 0; i < render->attr_count; i++) {
100 int attr = render->map[i];
101
102 if (attr >= 0) {
103 const struct gl_client_array *array = arrays[attr];
104 int stride;
105
106 if (render->mode == VBO &&
107 !_mesa_is_bufferobj(array->BufferObj))
108 /* Pack client buffers. */
109 stride = align(_mesa_sizeof_type(array->Type)
110 * array->Size, 4);
111 else
112 stride = array->StrideB;
113
114 vbo_init_array(&render->attrs[attr], attr,
115 stride, array->Size, array->Type,
116 array->BufferObj, array->Ptr,
117 render->mode == IMM);
118 }
119 }
120 }
121
122 static void
123 vbo_deinit_arrays(GLcontext *ctx, const struct _mesa_index_buffer *ib,
124 const struct gl_client_array **arrays)
125 {
126 struct nouveau_render_state *render = to_render_state(ctx);
127 int i;
128
129 if (ib)
130 vbo_deinit_array(&render->ib);
131
132 for (i = 0; i < render->attr_count; i++) {
133 int *attr = &render->map[i];
134
135 if (*attr >= 0) {
136 vbo_deinit_array(&render->attrs[*attr]);
137 *attr = -1;
138 }
139 }
140
141 render->attr_count = 0;
142 }
143
144 /* Make some rendering decisions from the GL context. */
145
146 static void
147 vbo_choose_render_mode(GLcontext *ctx, const struct gl_client_array **arrays)
148 {
149 struct nouveau_render_state *render = to_render_state(ctx);
150 int i;
151
152 render->mode = VBO;
153
154 if (ctx->Light.Enabled) {
155 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
156 if (arrays[VERT_ATTRIB_GENERIC0 + i]->StrideB) {
157 render->mode = IMM;
158 break;
159 }
160 }
161 }
162
163 if (render->mode == VBO)
164 render->attr_count = NUM_VERTEX_ATTRS;
165 else
166 render->attr_count = 0;
167 }
168
169 static void
170 vbo_emit_attr(GLcontext *ctx, const struct gl_client_array **arrays, int attr)
171 {
172 struct nouveau_channel *chan = context_chan(ctx);
173 struct nouveau_render_state *render = to_render_state(ctx);
174 const struct gl_client_array *array = arrays[attr];
175 struct nouveau_array_state *a = &render->attrs[attr];
176 RENDER_LOCALS(ctx);
177
178 if (!array->StrideB) {
179 if (attr >= VERT_ATTRIB_GENERIC0)
180 /* nouveau_update_state takes care of materials. */
181 return;
182
183 /* Constant attribute. */
184 vbo_init_array(a, attr, array->StrideB, array->Size,
185 array->Type, array->BufferObj, array->Ptr,
186 GL_TRUE);
187 EMIT_IMM(ctx, a, 0);
188 vbo_deinit_array(a);
189
190 } else {
191 /* Varying attribute. */
192 struct nouveau_attr_info *info = &TAG(vertex_attrs)[attr];
193
194 if (render->mode == VBO) {
195 render->map[info->vbo_index] = attr;
196 render->vertex_size += array->_ElementSize;
197 } else {
198 render->map[render->attr_count++] = attr;
199 render->vertex_size += 4 * info->imm_fields;
200 }
201 }
202 }
203
204 #define MAT(a) (VERT_ATTRIB_GENERIC0 + MAT_ATTRIB_##a)
205
206 static void
207 vbo_choose_attrs(GLcontext *ctx, const struct gl_client_array **arrays)
208 {
209 struct nouveau_render_state *render = to_render_state(ctx);
210 int i;
211
212 /* Reset the vertex size. */
213 render->vertex_size = 0;
214
215 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_COLOR0);
216 if (ctx->Fog.ColorSumEnabled && !ctx->Light.Enabled)
217 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_COLOR1);
218
219 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
220 if (ctx->Texture._EnabledCoordUnits & (1 << i))
221 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_TEX0 + i);
222 }
223
224 if (ctx->Fog.Enabled && ctx->Fog.FogCoordinateSource == GL_FOG_COORD)
225 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_FOG);
226
227 if (ctx->Light.Enabled) {
228 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_NORMAL);
229
230 vbo_emit_attr(ctx, arrays, MAT(FRONT_AMBIENT));
231 vbo_emit_attr(ctx, arrays, MAT(FRONT_DIFFUSE));
232 vbo_emit_attr(ctx, arrays, MAT(FRONT_SPECULAR));
233 vbo_emit_attr(ctx, arrays, MAT(FRONT_SHININESS));
234
235 if (ctx->Light.Model.TwoSide) {
236 vbo_emit_attr(ctx, arrays, MAT(BACK_AMBIENT));
237 vbo_emit_attr(ctx, arrays, MAT(BACK_DIFFUSE));
238 vbo_emit_attr(ctx, arrays, MAT(BACK_SPECULAR));
239 vbo_emit_attr(ctx, arrays, MAT(BACK_SHININESS));
240 }
241 }
242
243 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_POS);
244 }
245
246 static void
247 TAG(vbo_render_prims)(GLcontext *ctx, const struct gl_client_array **arrays,
248 const struct _mesa_prim *prims, GLuint nr_prims,
249 const struct _mesa_index_buffer *ib,
250 GLboolean index_bounds_valid,
251 GLuint min_index, GLuint max_index);
252
253 static GLboolean
254 vbo_maybe_split(GLcontext *ctx, const struct gl_client_array **arrays,
255 const struct _mesa_prim *prims, GLuint nr_prims,
256 const struct _mesa_index_buffer *ib,
257 GLuint min_index, GLuint max_index)
258 {
259 struct nouveau_context *nctx = to_nouveau_context(ctx);
260 unsigned pushbuf_avail = PUSHBUF_DWORDS - 2 * nctx->bo.count,
261 vert_avail = get_max_vertices(ctx, NULL, pushbuf_avail),
262 idx_avail = get_max_vertices(ctx, ib, pushbuf_avail);
263
264 if ((ib && ib->count > idx_avail) ||
265 (!ib && max_index - min_index > vert_avail)) {
266 struct split_limits limits = {
267 .max_verts = vert_avail,
268 .max_indices = idx_avail,
269 .max_vb_size = ~0,
270 };
271
272 vbo_split_prims(ctx, arrays, prims, nr_prims, ib, min_index,
273 max_index, TAG(vbo_render_prims), &limits);
274 return GL_TRUE;
275 }
276
277 return GL_FALSE;
278 }
279
280 /* VBO rendering path. */
281
282 static void
283 vbo_bind_vertices(GLcontext *ctx, const struct gl_client_array **arrays,
284 GLint basevertex, GLuint min_index, GLuint max_index)
285 {
286 struct nouveau_render_state *render = to_render_state(ctx);
287 int i;
288
289 for (i = 0; i < NUM_VERTEX_ATTRS; i++) {
290 int attr = render->map[i];
291
292 if (attr >= 0) {
293 const struct gl_client_array *array = arrays[attr];
294 struct nouveau_array_state *a = &render->attrs[attr];
295 unsigned delta = (basevertex + min_index)
296 * array->StrideB;
297
298 if (a->bo) {
299 a->offset = (intptr_t)array->Ptr + delta;
300 } else {
301 int j, n = max_index - min_index + 1;
302 char *sp = (char *)array->Ptr + delta;
303 char *dp = get_scratch_vbo(ctx, n * a->stride,
304 &a->bo, &a->offset);
305
306 for (j = 0; j < n; j++)
307 memcpy(dp + j * a->stride,
308 sp + j * array->StrideB,
309 a->stride);
310 }
311 }
312 }
313
314 TAG(render_bind_vertices)(ctx);
315 }
316
317 static void
318 vbo_draw_vbo(GLcontext *ctx, const struct gl_client_array **arrays,
319 const struct _mesa_prim *prims, GLuint nr_prims,
320 const struct _mesa_index_buffer *ib, GLuint min_index,
321 GLuint max_index)
322 {
323 struct nouveau_channel *chan = context_chan(ctx);
324 dispatch_t dispatch;
325 int delta = -min_index, basevertex = 0, i;
326 RENDER_LOCALS(ctx);
327
328 get_array_dispatch(&to_render_state(ctx)->ib, &dispatch);
329
330 TAG(render_set_format)(ctx);
331
332 for (i = 0; i < nr_prims; i++) {
333 unsigned start = prims[i].start,
334 count = prims[i].count;
335
336 if (i == 0 || basevertex != prims[i].basevertex) {
337 basevertex = prims[i].basevertex;
338 vbo_bind_vertices(ctx, arrays, basevertex,
339 min_index, max_index);
340 }
341
342 if (count > get_max_vertices(ctx, ib, AVAIL_RING(chan)))
343 WAIT_RING(chan, PUSHBUF_DWORDS);
344
345 BATCH_BEGIN(nvgl_primitive(prims[i].mode));
346 dispatch(ctx, start, delta, count);
347 BATCH_END();
348 }
349
350 FIRE_RING(chan);
351 }
352
353 /* Immediate rendering path. */
354
355 static unsigned
356 extract_id(struct nouveau_array_state *a, int i, int j)
357 {
358 return j;
359 }
360
361 static void
362 vbo_draw_imm(GLcontext *ctx, const struct gl_client_array **arrays,
363 const struct _mesa_prim *prims, GLuint nr_prims,
364 const struct _mesa_index_buffer *ib, GLuint min_index,
365 GLuint max_index)
366 {
367 struct nouveau_render_state *render = to_render_state(ctx);
368 struct nouveau_channel *chan = context_chan(ctx);
369 extract_u_t extract = ib ? render->ib.extract_u : extract_id;
370 int i, j, k;
371 RENDER_LOCALS(ctx);
372
373 for (i = 0; i < nr_prims; i++) {
374 unsigned start = prims[i].start,
375 end = start + prims[i].count;
376
377 if (prims[i].count > get_max_vertices(ctx, ib,
378 AVAIL_RING(chan)))
379 WAIT_RING(chan, PUSHBUF_DWORDS);
380
381 BATCH_BEGIN(nvgl_primitive(prims[i].mode));
382
383 for (; start < end; start++) {
384 j = prims[i].basevertex +
385 extract(&render->ib, 0, start);
386
387 for (k = 0; k < render->attr_count; k++)
388 EMIT_IMM(ctx, &render->attrs[render->map[k]],
389 j);
390 }
391
392 BATCH_END();
393 }
394
395 FIRE_RING(chan);
396 }
397
398 /* draw_prims entry point when we're doing hw-tnl. */
399
400 static void
401 TAG(vbo_render_prims)(GLcontext *ctx, const struct gl_client_array **arrays,
402 const struct _mesa_prim *prims, GLuint nr_prims,
403 const struct _mesa_index_buffer *ib,
404 GLboolean index_bounds_valid,
405 GLuint min_index, GLuint max_index)
406 {
407 struct nouveau_render_state *render = to_render_state(ctx);
408
409 if (!index_bounds_valid)
410 vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
411
412 vbo_choose_render_mode(ctx, arrays);
413 vbo_choose_attrs(ctx, arrays);
414
415 if (vbo_maybe_split(ctx, arrays, prims, nr_prims, ib, min_index,
416 max_index))
417 return;
418
419 vbo_init_arrays(ctx, ib, arrays);
420
421 if (render->mode == VBO)
422 vbo_draw_vbo(ctx, arrays, prims, nr_prims, ib, min_index,
423 max_index);
424 else
425 vbo_draw_imm(ctx, arrays, prims, nr_prims, ib, min_index,
426 max_index);
427
428 vbo_deinit_arrays(ctx, ib, arrays);
429 }