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