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