83fb9596d58ad403433189d51da7040ded4d1c68
[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_driver.h"
28 #include "nouveau_bufferobj.h"
29 #include "nouveau_util.h"
30
31 #include "main/bufferobj.h"
32 #include "main/glformats.h"
33 #include "main/varray.h"
34 #include "main/image.h"
35
36 /* Arbitrary pushbuf length we can assume we can get with a single
37 * call to WAIT_RING. */
38 #define PUSHBUF_DWORDS 65536
39
40 /* Functions to turn GL arrays or index buffers into nouveau_array
41 * structures. */
42
43 static int
44 get_array_stride(struct gl_context *ctx, const struct tnl_vertex_array *a)
45 {
46 struct nouveau_render_state *render = to_render_state(ctx);
47 const struct gl_vertex_buffer_binding *binding = a->BufferBinding;
48
49 if (render->mode == VBO && !_mesa_is_bufferobj(binding->BufferObj)) {
50 const struct gl_array_attributes *attrib = a->VertexAttrib;
51 /* Pack client buffers. */
52 return align(attrib->Format._ElementSize, 4);
53 } else {
54 return binding->Stride;
55 }
56 }
57
58 static void
59 vbo_init_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
60 const struct tnl_vertex_array *arrays)
61 {
62 struct nouveau_render_state *render = to_render_state(ctx);
63 GLboolean imm = (render->mode == IMM);
64 int i, attr;
65
66 if (ib) {
67 GLenum ib_type;
68
69 if (ib->index_size == 4)
70 ib_type = GL_UNSIGNED_INT;
71 else if (ib->index_size == 2)
72 ib_type = GL_UNSIGNED_SHORT;
73 else
74 ib_type = GL_UNSIGNED_BYTE;
75
76 nouveau_init_array(&render->ib, 0, 0, ib->count, ib_type,
77 ib->obj, ib->ptr, GL_TRUE, ctx);
78 }
79
80 FOR_EACH_BOUND_ATTR(render, i, attr) {
81 const struct tnl_vertex_array *array = &arrays[attr];
82 const struct gl_vertex_buffer_binding *binding =
83 array->BufferBinding;
84 const struct gl_array_attributes *attrib = array->VertexAttrib;
85 const GLubyte *p = _mesa_vertex_attrib_address(attrib, binding);
86
87 nouveau_init_array(&render->attrs[attr], attr,
88 get_array_stride(ctx, array),
89 attrib->Format.Size, attrib->Format.Type,
90 imm ? binding->BufferObj : NULL,
91 p, imm, ctx);
92 }
93 }
94
95 static void
96 vbo_deinit_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
97 const struct tnl_vertex_array *arrays)
98 {
99 struct nouveau_render_state *render = to_render_state(ctx);
100 int i, attr;
101
102 if (ib)
103 nouveau_cleanup_array(&render->ib);
104
105 FOR_EACH_BOUND_ATTR(render, i, attr) {
106 struct nouveau_array *a = &render->attrs[attr];
107
108 if (render->mode == IMM)
109 nouveau_bo_ref(NULL, &a->bo);
110
111 nouveau_deinit_array(a);
112 render->map[i] = -1;
113 }
114
115 render->attr_count = 0;
116 }
117
118 /* Make some rendering decisions from the GL context. */
119
120 static void
121 vbo_choose_render_mode(struct gl_context *ctx, const struct tnl_vertex_array *arrays)
122 {
123 struct nouveau_render_state *render = to_render_state(ctx);
124 int i;
125
126 render->mode = VBO;
127
128 if (ctx->Light.Enabled) {
129 for (i = 0; i < VERT_ATTRIB_MAT_MAX; i++) {
130 if (arrays[VERT_ATTRIB_MAT(i)].BufferBinding->Stride) {
131 render->mode = IMM;
132 break;
133 }
134 }
135 }
136 }
137
138 static void
139 vbo_emit_attr(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
140 int attr)
141 {
142 struct nouveau_pushbuf *push = context_push(ctx);
143 struct nouveau_render_state *render = to_render_state(ctx);
144 const struct tnl_vertex_array *array = &arrays[attr];
145 const struct gl_vertex_buffer_binding *binding = array->BufferBinding;
146 const struct gl_array_attributes *attrib = array->VertexAttrib;
147 const GLubyte *p = _mesa_vertex_attrib_address(attrib, binding);
148 struct nouveau_array *a = &render->attrs[attr];
149 RENDER_LOCALS(ctx);
150
151 if (!binding->Stride) {
152 if (attr >= VERT_ATTRIB_MAT(0))
153 /* nouveau_update_state takes care of materials. */
154 return;
155
156 /* Constant attribute. */
157 nouveau_init_array(a, attr, binding->Stride, attrib->Format.Size,
158 attrib->Format.Type, binding->BufferObj, p,
159 GL_TRUE, ctx);
160 EMIT_IMM(ctx, a, 0);
161 nouveau_deinit_array(a);
162
163 } else {
164 /* Varying attribute. */
165 struct nouveau_attr_info *info = &TAG(vertex_attrs)[attr];
166
167 if (render->mode == VBO) {
168 render->map[info->vbo_index] = attr;
169 render->vertex_size += attrib->Format._ElementSize;
170 render->attr_count = MAX2(render->attr_count,
171 info->vbo_index + 1);
172 } else {
173 render->map[render->attr_count++] = attr;
174 render->vertex_size += 4 * info->imm_fields;
175 }
176 }
177 }
178
179 #define MAT(a) VERT_ATTRIB_MAT(MAT_ATTRIB_##a)
180
181 static void
182 vbo_choose_attrs(struct gl_context *ctx, const struct tnl_vertex_array *arrays)
183 {
184 struct nouveau_render_state *render = to_render_state(ctx);
185 int i;
186
187 /* Reset the vertex size. */
188 render->vertex_size = 0;
189 render->attr_count = 0;
190
191 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_COLOR0);
192 if (ctx->Fog.ColorSumEnabled && !ctx->Light.Enabled)
193 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_COLOR1);
194
195 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
196 if (ctx->Texture._EnabledCoordUnits & (1 << i))
197 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_TEX0 + i);
198 }
199
200 if (ctx->Fog.Enabled && ctx->Fog.FogCoordinateSource == GL_FOG_COORD)
201 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_FOG);
202
203 if (ctx->Light.Enabled ||
204 (ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS))
205 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_NORMAL);
206
207 if (ctx->Light.Enabled && render->mode == IMM) {
208 vbo_emit_attr(ctx, arrays, MAT(FRONT_AMBIENT));
209 vbo_emit_attr(ctx, arrays, MAT(FRONT_DIFFUSE));
210 vbo_emit_attr(ctx, arrays, MAT(FRONT_SPECULAR));
211 vbo_emit_attr(ctx, arrays, MAT(FRONT_SHININESS));
212
213 if (ctx->Light.Model.TwoSide) {
214 vbo_emit_attr(ctx, arrays, MAT(BACK_AMBIENT));
215 vbo_emit_attr(ctx, arrays, MAT(BACK_DIFFUSE));
216 vbo_emit_attr(ctx, arrays, MAT(BACK_SPECULAR));
217 vbo_emit_attr(ctx, arrays, MAT(BACK_SHININESS));
218 }
219 }
220
221 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_POS);
222 }
223
224 static int
225 get_max_client_stride(struct gl_context *ctx, const struct tnl_vertex_array *arrays)
226 {
227 struct nouveau_render_state *render = to_render_state(ctx);
228 int i, attr, s = 0;
229
230 FOR_EACH_BOUND_ATTR(render, i, attr) {
231 const struct tnl_vertex_array *a = &arrays[attr];
232
233 if (!_mesa_is_bufferobj(a->BufferBinding->BufferObj))
234 s = MAX2(s, get_array_stride(ctx, a));
235 }
236
237 return s;
238 }
239
240 static void
241 TAG(vbo_render_prims)(struct gl_context *ctx,
242 const struct tnl_vertex_array *arrays,
243 const struct _mesa_prim *prims, GLuint nr_prims,
244 const struct _mesa_index_buffer *ib,
245 GLboolean index_bounds_valid,
246 GLuint min_index, GLuint max_index,
247 GLuint num_instances, GLuint base_instance,
248 struct gl_transform_feedback_object *tfb_vertcount,
249 unsigned stream);
250
251 static GLboolean
252 vbo_maybe_split(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
253 const struct _mesa_prim *prims, GLuint nr_prims,
254 const struct _mesa_index_buffer *ib,
255 GLuint min_index, GLuint max_index,
256 GLuint num_instances, GLuint base_instance)
257 {
258 struct nouveau_context *nctx = to_nouveau_context(ctx);
259 struct nouveau_render_state *render = to_render_state(ctx);
260 struct nouveau_bufctx *bufctx = nctx->hw.bufctx;
261 unsigned pushbuf_avail = PUSHBUF_DWORDS - 2 * (bufctx->relocs +
262 render->attr_count),
263 vert_avail = get_max_vertices(ctx, NULL, pushbuf_avail),
264 idx_avail = get_max_vertices(ctx, ib, pushbuf_avail);
265 int stride;
266
267 /* Try to keep client buffers smaller than the scratch BOs. */
268 if (render->mode == VBO &&
269 (stride = get_max_client_stride(ctx, arrays)))
270 vert_avail = MIN2(vert_avail,
271 NOUVEAU_SCRATCH_SIZE / stride);
272
273 if (max_index - min_index > vert_avail ||
274 (ib && ib->count > idx_avail)) {
275 struct split_limits limits = {
276 .max_verts = vert_avail,
277 .max_indices = idx_avail,
278 .max_vb_size = ~0,
279 };
280
281 _tnl_split_prims(ctx, arrays, prims, nr_prims, ib, min_index,
282 max_index, num_instances, base_instance,
283 TAG(vbo_render_prims), &limits);
284 return GL_TRUE;
285 }
286
287 return GL_FALSE;
288 }
289
290 /* VBO rendering path. */
291
292 static GLboolean
293 check_update_array(struct nouveau_array *a, unsigned offset,
294 struct nouveau_bo *bo, int *pdelta)
295 {
296 int delta = *pdelta;
297 GLboolean dirty;
298
299 if (a->bo == bo) {
300 if (delta < 0)
301 delta = ((int)offset - (int)a->offset) / a->stride;
302
303 dirty = (delta < 0 ||
304 offset != (a->offset + delta * a->stride));
305 } else {
306 dirty = GL_TRUE;
307 }
308
309 *pdelta = (dirty ? 0 : delta);
310 return dirty;
311 }
312
313 static void
314 vbo_bind_vertices(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
315 int base, unsigned min_index, unsigned max_index, int *pdelta)
316 {
317 struct nouveau_render_state *render = to_render_state(ctx);
318 struct nouveau_pushbuf *push = context_push(ctx);
319 struct nouveau_bo *bo[NUM_VERTEX_ATTRS];
320 unsigned offset[NUM_VERTEX_ATTRS];
321 GLboolean dirty = GL_FALSE;
322 int i, j, attr;
323 RENDER_LOCALS(ctx);
324
325 *pdelta = -1;
326
327 FOR_EACH_BOUND_ATTR(render, i, attr) {
328 const struct tnl_vertex_array *array = &arrays[attr];
329 const struct gl_vertex_buffer_binding *binding =
330 array->BufferBinding;
331 const struct gl_array_attributes *attrib = array->VertexAttrib;
332 const GLubyte *p = _mesa_vertex_attrib_address(attrib, binding);
333 struct gl_buffer_object *obj = binding->BufferObj;
334 struct nouveau_array *a = &render->attrs[attr];
335 unsigned delta = (base + min_index) * binding->Stride;
336
337 bo[i] = NULL;
338
339 if (nouveau_bufferobj_hw(obj)) {
340 /* Array in a buffer obj. */
341 nouveau_bo_ref(to_nouveau_bufferobj(obj)->bo, &bo[i]);
342 offset[i] = delta + (intptr_t)p;
343
344 } else {
345 int n = max_index - min_index + 1;
346 char *sp = (char *)ADD_POINTERS(
347 nouveau_bufferobj_sys(obj), p) + delta;
348 char *dp = nouveau_get_scratch(ctx, n * a->stride,
349 &bo[i], &offset[i]);
350
351 /* Array in client memory, move it to a
352 * scratch buffer obj. */
353 for (j = 0; j < n; j++)
354 memcpy(dp + j * a->stride,
355 sp + j * binding->Stride,
356 a->stride);
357 }
358
359 dirty |= check_update_array(a, offset[i], bo[i], pdelta);
360 }
361
362 *pdelta -= min_index;
363
364 if (dirty) {
365 /* Buffers changed, update the attribute binding. */
366 FOR_EACH_BOUND_ATTR(render, i, attr) {
367 struct nouveau_array *a = &render->attrs[attr];
368
369 nouveau_bo_ref(NULL, &a->bo);
370 a->offset = offset[i];
371 a->bo = bo[i];
372 }
373
374 TAG(render_release_vertices)(ctx);
375 TAG(render_bind_vertices)(ctx);
376 } else {
377 /* Just cleanup. */
378 FOR_EACH_BOUND_ATTR(render, i, attr)
379 nouveau_bo_ref(NULL, &bo[i]);
380 }
381
382 BATCH_VALIDATE();
383 }
384
385 static void
386 vbo_draw_vbo(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
387 const struct _mesa_prim *prims, GLuint nr_prims,
388 const struct _mesa_index_buffer *ib, GLuint min_index,
389 GLuint max_index)
390 {
391 struct nouveau_context *nctx = to_nouveau_context(ctx);
392 struct nouveau_pushbuf *push = context_push(ctx);
393 dispatch_t dispatch = get_array_dispatch(&to_render_state(ctx)->ib);
394 int i, delta = 0, basevertex = 0;
395 RENDER_LOCALS(ctx);
396
397 TAG(render_set_format)(ctx);
398
399 for (i = 0; i < nr_prims; i++) {
400 unsigned start = prims[i].start,
401 count = prims[i].count;
402
403 if (i == 0 || basevertex != prims[i].basevertex) {
404 basevertex = prims[i].basevertex;
405 vbo_bind_vertices(ctx, arrays, basevertex, min_index,
406 max_index, &delta);
407
408 nouveau_pushbuf_bufctx(push, nctx->hw.bufctx);
409 if (nouveau_pushbuf_validate(push)) {
410 nouveau_pushbuf_bufctx(push, NULL);
411 return;
412 }
413 }
414
415 if (count > get_max_vertices(ctx, ib, PUSH_AVAIL(push)))
416 PUSH_SPACE(push, PUSHBUF_DWORDS);
417
418 BATCH_BEGIN(nvgl_primitive(prims[i].mode));
419 dispatch(ctx, start, delta, count);
420 BATCH_END();
421 }
422
423 nouveau_pushbuf_bufctx(push, NULL);
424 TAG(render_release_vertices)(ctx);
425 }
426
427 /* Immediate rendering path. */
428
429 static unsigned
430 extract_id(struct nouveau_array *a, int i, int j)
431 {
432 return j;
433 }
434
435 static void
436 vbo_draw_imm(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
437 const struct _mesa_prim *prims, GLuint nr_prims,
438 const struct _mesa_index_buffer *ib, GLuint min_index,
439 GLuint max_index)
440 {
441 struct nouveau_render_state *render = to_render_state(ctx);
442 struct nouveau_context *nctx = to_nouveau_context(ctx);
443 struct nouveau_pushbuf *push = context_push(ctx);
444 extract_u_t extract = ib ? render->ib.extract_u : extract_id;
445 int i, j, k, attr;
446 RENDER_LOCALS(ctx);
447
448 nouveau_pushbuf_bufctx(push, nctx->hw.bufctx);
449 if (nouveau_pushbuf_validate(push)) {
450 nouveau_pushbuf_bufctx(push, NULL);
451 return;
452 }
453
454 for (i = 0; i < nr_prims; i++) {
455 unsigned start = prims[i].start,
456 end = start + prims[i].count;
457
458 if (prims[i].count > get_max_vertices(ctx, ib,
459 PUSH_AVAIL(push)))
460 PUSH_SPACE(push, PUSHBUF_DWORDS);
461
462 BATCH_BEGIN(nvgl_primitive(prims[i].mode));
463
464 for (; start < end; start++) {
465 j = prims[i].basevertex +
466 extract(&render->ib, 0, start);
467
468 FOR_EACH_BOUND_ATTR(render, k, attr)
469 EMIT_IMM(ctx, &render->attrs[attr], j);
470 }
471
472 BATCH_END();
473 }
474
475 nouveau_pushbuf_bufctx(push, NULL);
476 }
477
478 /* draw_prims entry point when we're doing hw-tnl. */
479
480 static void
481 TAG(vbo_render_prims)(struct gl_context *ctx,
482 const struct tnl_vertex_array *arrays,
483 const struct _mesa_prim *prims, GLuint nr_prims,
484 const struct _mesa_index_buffer *ib,
485 GLboolean index_bounds_valid,
486 GLuint min_index, GLuint max_index,
487 GLuint num_instances, GLuint base_instance,
488 struct gl_transform_feedback_object *tfb_vertcount,
489 unsigned stream)
490 {
491 struct nouveau_render_state *render = to_render_state(ctx);
492
493 if (!index_bounds_valid)
494 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index,
495 nr_prims);
496
497 vbo_choose_render_mode(ctx, arrays);
498 vbo_choose_attrs(ctx, arrays);
499
500 if (vbo_maybe_split(ctx, arrays, prims, nr_prims, ib, min_index,
501 max_index, num_instances, base_instance))
502 return;
503
504 vbo_init_arrays(ctx, ib, arrays);
505
506 if (render->mode == VBO)
507 vbo_draw_vbo(ctx, arrays, prims, nr_prims, ib, min_index,
508 max_index);
509 else
510 vbo_draw_imm(ctx, arrays, prims, nr_prims, ib, min_index,
511 max_index);
512
513 vbo_deinit_arrays(ctx, ib, arrays);
514 }
515
516 /* VBO rendering entry points. */
517
518 static void
519 TAG(vbo_check_render_prims)(struct gl_context *ctx,
520 const struct tnl_vertex_array *arrays,
521 const struct _mesa_prim *prims, GLuint nr_prims,
522 const struct _mesa_index_buffer *ib,
523 GLboolean index_bounds_valid,
524 GLuint min_index, GLuint max_index,
525 GLuint num_instances, GLuint base_instance,
526 struct gl_transform_feedback_object *tfb_vertcount,
527 unsigned stream)
528 {
529 struct nouveau_context *nctx = to_nouveau_context(ctx);
530
531 nouveau_validate_framebuffer(ctx);
532
533 if (nctx->fallback == HWTNL)
534 TAG(vbo_render_prims)(ctx, arrays, prims, nr_prims, ib,
535 index_bounds_valid, min_index, max_index,
536 num_instances, base_instance,
537 tfb_vertcount, stream);
538
539 if (nctx->fallback == SWTNL)
540 _tnl_draw_prims(ctx, arrays, prims, nr_prims, ib,
541 index_bounds_valid, min_index, max_index,
542 num_instances, base_instance,
543 tfb_vertcount, stream);
544 }
545
546 static void
547 TAG(vbo_draw)(struct gl_context *ctx,
548 const struct _mesa_prim *prims, GLuint nr_prims,
549 const struct _mesa_index_buffer *ib,
550 GLboolean index_bounds_valid,
551 GLuint min_index, GLuint max_index,
552 GLuint num_instances, GLuint base_instance,
553 struct gl_transform_feedback_object *tfb_vertcount,
554 unsigned stream)
555 {
556 /* Borrow and update the inputs list from the tnl context */
557 const struct tnl_vertex_array* arrays = _tnl_bind_inputs(ctx);
558
559 TAG(vbo_check_render_prims)(ctx, arrays,
560 prims, nr_prims, ib,
561 index_bounds_valid, min_index, max_index,
562 num_instances, base_instance,
563 tfb_vertcount, stream);
564 }
565
566 void
567 TAG(vbo_init)(struct gl_context *ctx)
568 {
569 struct nouveau_render_state *render = to_render_state(ctx);
570 int i;
571
572 for (i = 0; i < VERT_ATTRIB_MAX; i++)
573 render->map[i] = -1;
574
575 /* Overwrite our draw function */
576 ctx->Driver.Draw = TAG(vbo_draw);
577 }
578
579 void
580 TAG(vbo_destroy)(struct gl_context *ctx)
581 {
582 }