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 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 unsigned
247 get_max_client_stride(GLcontext *ctx)
248 {
249 struct nouveau_render_state *render = to_render_state(ctx);
250 int i, s = 0;
251
252 for (i = 0; i < render->attr_count; i++) {
253 int attr = render->map[i];
254 struct nouveau_array_state *a = &render->attrs[attr];
255
256 if (attr >= 0 && !a->bo)
257 s = MAX2(a->stride, s);
258 }
259
260 return s;
261 }
262
263 static void
264 TAG(vbo_render_prims)(GLcontext *ctx, const struct gl_client_array **arrays,
265 const struct _mesa_prim *prims, GLuint nr_prims,
266 const struct _mesa_index_buffer *ib,
267 GLboolean index_bounds_valid,
268 GLuint min_index, GLuint max_index);
269
270 static GLboolean
271 vbo_maybe_split(GLcontext *ctx, const struct gl_client_array **arrays,
272 const struct _mesa_prim *prims, GLuint nr_prims,
273 const struct _mesa_index_buffer *ib,
274 GLuint min_index, GLuint max_index)
275 {
276 struct nouveau_context *nctx = to_nouveau_context(ctx);
277 struct nouveau_render_state *render = to_render_state(ctx);
278 unsigned pushbuf_avail = PUSHBUF_DWORDS - 2 * nctx->bo.count,
279 vert_avail = get_max_vertices(ctx, NULL, pushbuf_avail),
280 idx_avail = get_max_vertices(ctx, ib, pushbuf_avail);
281 int stride;
282
283 /* Try to keep client buffers smaller than the scratch BOs. */
284 if (render->mode == VBO &&
285 (stride = get_max_client_stride(ctx)))
286 vert_avail = MIN2(vert_avail,
287 RENDER_SCRATCH_SIZE / stride);
288
289 if (max_index - min_index > vert_avail ||
290 (ib && ib->count > idx_avail)) {
291 struct split_limits limits = {
292 .max_verts = vert_avail,
293 .max_indices = idx_avail,
294 .max_vb_size = ~0,
295 };
296
297 vbo_split_prims(ctx, arrays, prims, nr_prims, ib, min_index,
298 max_index, TAG(vbo_render_prims), &limits);
299 return GL_TRUE;
300 }
301
302 return GL_FALSE;
303 }
304
305 /* VBO rendering path. */
306
307 static void
308 vbo_bind_vertices(GLcontext *ctx, const struct gl_client_array **arrays,
309 GLint basevertex, GLuint min_index, GLuint max_index)
310 {
311 struct nouveau_render_state *render = to_render_state(ctx);
312 int i;
313
314 for (i = 0; i < NUM_VERTEX_ATTRS; i++) {
315 int attr = render->map[i];
316
317 if (attr >= 0) {
318 const struct gl_client_array *array = arrays[attr];
319 struct nouveau_array_state *a = &render->attrs[attr];
320 unsigned delta = (basevertex + min_index)
321 * array->StrideB;
322
323 if (a->bo) {
324 a->offset = (intptr_t)array->Ptr + delta;
325 } else {
326 int j, n = max_index - min_index + 1;
327 char *sp = (char *)array->Ptr + delta;
328 char *dp = get_scratch_vbo(ctx, n * a->stride,
329 &a->bo, &a->offset);
330
331 for (j = 0; j < n; j++)
332 memcpy(dp + j * a->stride,
333 sp + j * array->StrideB,
334 a->stride);
335 }
336 }
337 }
338
339 TAG(render_bind_vertices)(ctx);
340 }
341
342 static void
343 vbo_draw_vbo(GLcontext *ctx, const struct gl_client_array **arrays,
344 const struct _mesa_prim *prims, GLuint nr_prims,
345 const struct _mesa_index_buffer *ib, GLuint min_index,
346 GLuint max_index)
347 {
348 struct nouveau_channel *chan = context_chan(ctx);
349 dispatch_t dispatch;
350 int delta = -min_index, basevertex = 0, i;
351 RENDER_LOCALS(ctx);
352
353 get_array_dispatch(&to_render_state(ctx)->ib, &dispatch);
354
355 TAG(render_set_format)(ctx);
356
357 for (i = 0; i < nr_prims; i++) {
358 unsigned start = prims[i].start,
359 count = prims[i].count;
360
361 if (i == 0 || basevertex != prims[i].basevertex) {
362 basevertex = prims[i].basevertex;
363 vbo_bind_vertices(ctx, arrays, basevertex,
364 min_index, max_index);
365 }
366
367 if (count > get_max_vertices(ctx, ib, AVAIL_RING(chan)))
368 WAIT_RING(chan, PUSHBUF_DWORDS);
369
370 BATCH_BEGIN(nvgl_primitive(prims[i].mode));
371 dispatch(ctx, start, delta, count);
372 BATCH_END();
373 }
374
375 FIRE_RING(chan);
376 }
377
378 /* Immediate rendering path. */
379
380 static unsigned
381 extract_id(struct nouveau_array_state *a, int i, int j)
382 {
383 return j;
384 }
385
386 static void
387 vbo_draw_imm(GLcontext *ctx, const struct gl_client_array **arrays,
388 const struct _mesa_prim *prims, GLuint nr_prims,
389 const struct _mesa_index_buffer *ib, GLuint min_index,
390 GLuint max_index)
391 {
392 struct nouveau_render_state *render = to_render_state(ctx);
393 struct nouveau_channel *chan = context_chan(ctx);
394 extract_u_t extract = ib ? render->ib.extract_u : extract_id;
395 int i, j, k;
396 RENDER_LOCALS(ctx);
397
398 for (i = 0; i < nr_prims; i++) {
399 unsigned start = prims[i].start,
400 end = start + prims[i].count;
401
402 if (prims[i].count > get_max_vertices(ctx, ib,
403 AVAIL_RING(chan)))
404 WAIT_RING(chan, PUSHBUF_DWORDS);
405
406 BATCH_BEGIN(nvgl_primitive(prims[i].mode));
407
408 for (; start < end; start++) {
409 j = prims[i].basevertex +
410 extract(&render->ib, 0, start);
411
412 for (k = 0; k < render->attr_count; k++)
413 EMIT_IMM(ctx, &render->attrs[render->map[k]],
414 j);
415 }
416
417 BATCH_END();
418 }
419
420 FIRE_RING(chan);
421 }
422
423 /* draw_prims entry point when we're doing hw-tnl. */
424
425 static void
426 TAG(vbo_render_prims)(GLcontext *ctx, const struct gl_client_array **arrays,
427 const struct _mesa_prim *prims, GLuint nr_prims,
428 const struct _mesa_index_buffer *ib,
429 GLboolean index_bounds_valid,
430 GLuint min_index, GLuint max_index)
431 {
432 struct nouveau_render_state *render = to_render_state(ctx);
433
434 if (!index_bounds_valid)
435 vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
436
437 vbo_choose_render_mode(ctx, arrays);
438 vbo_choose_attrs(ctx, arrays);
439
440 if (vbo_maybe_split(ctx, arrays, prims, nr_prims, ib, min_index,
441 max_index))
442 return;
443
444 vbo_init_arrays(ctx, ib, arrays);
445
446 if (render->mode == VBO)
447 vbo_draw_vbo(ctx, arrays, prims, nr_prims, ib, min_index,
448 max_index);
449 else
450 vbo_draw_imm(ctx, arrays, prims, nr_prims, ib, min_index,
451 max_index);
452
453 vbo_deinit_arrays(ctx, ib, arrays);
454 }