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