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