2 * Copyright (C) 2009-2010 Francisco Jerez.
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:
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.
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.
27 #include "nouveau_bufferobj.h"
28 #include "nouveau_util.h"
30 #include "main/bufferobj.h"
31 #include "main/image.h"
33 /* Arbitrary pushbuf length we can assume we can get with a single
34 * call to WAIT_RING. */
35 #define PUSHBUF_DWORDS 65536
37 /* Functions to turn GL arrays or index buffers into nouveau_array
41 get_array_stride(struct gl_context *ctx, const struct gl_client_array *a)
43 struct nouveau_render_state *render = to_render_state(ctx);
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);
53 vbo_init_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
54 const struct gl_client_array **arrays)
56 struct nouveau_render_state *render = to_render_state(ctx);
57 GLboolean imm = (render->mode == IMM);
61 nouveau_init_array(&render->ib, 0, 0, ib->count, ib->type,
62 ib->obj, ib->ptr, GL_TRUE);
64 FOR_EACH_BOUND_ATTR(render, i, attr) {
65 const struct gl_client_array *array = arrays[attr];
67 nouveau_init_array(&render->attrs[attr], attr,
68 get_array_stride(ctx, array),
69 array->Size, array->Type,
70 imm ? array->BufferObj : NULL,
76 vbo_deinit_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
77 const struct gl_client_array **arrays)
79 struct nouveau_render_state *render = to_render_state(ctx);
83 nouveau_cleanup_array(&render->ib);
85 FOR_EACH_BOUND_ATTR(render, i, attr) {
86 struct nouveau_array *a = &render->attrs[attr];
88 if (render->mode == IMM)
89 nouveau_bo_ref(NULL, &a->bo);
91 nouveau_deinit_array(a);
95 render->attr_count = 0;
98 /* Make some rendering decisions from the GL context. */
101 vbo_choose_render_mode(struct gl_context *ctx, const struct gl_client_array **arrays)
103 struct nouveau_render_state *render = to_render_state(ctx);
108 if (ctx->Light.Enabled) {
109 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
110 if (arrays[VERT_ATTRIB_GENERIC0 + i]->StrideB) {
119 vbo_emit_attr(struct gl_context *ctx, const struct gl_client_array **arrays,
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];
128 if (!array->StrideB) {
129 if (attr >= VERT_ATTRIB_GENERIC0)
130 /* nouveau_update_state takes care of materials. */
133 /* Constant attribute. */
134 nouveau_init_array(a, attr, array->StrideB, array->Size,
135 array->Type, array->BufferObj, array->Ptr,
138 nouveau_deinit_array(a);
141 /* Varying attribute. */
142 struct nouveau_attr_info *info = &TAG(vertex_attrs)[attr];
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);
150 render->map[render->attr_count++] = attr;
151 render->vertex_size += 4 * info->imm_fields;
156 #define MAT(a) (VERT_ATTRIB_GENERIC0 + MAT_ATTRIB_##a)
159 vbo_choose_attrs(struct gl_context *ctx, const struct gl_client_array **arrays)
161 struct nouveau_render_state *render = to_render_state(ctx);
164 /* Reset the vertex size. */
165 render->vertex_size = 0;
166 render->attr_count = 0;
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);
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);
177 if (ctx->Fog.Enabled && ctx->Fog.FogCoordinateSource == GL_FOG_COORD)
178 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_FOG);
180 if (ctx->Light.Enabled ||
181 (ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS))
182 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_NORMAL);
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));
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));
198 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_POS);
202 get_max_client_stride(struct gl_context *ctx, const struct gl_client_array **arrays)
204 struct nouveau_render_state *render = to_render_state(ctx);
207 FOR_EACH_BOUND_ATTR(render, i, attr) {
208 const struct gl_client_array *a = arrays[attr];
210 if (!_mesa_is_bufferobj(a->BufferObj))
211 s = MAX2(s, get_array_stride(ctx, a));
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);
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)
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 +
234 vert_avail = get_max_vertices(ctx, NULL, pushbuf_avail),
235 idx_avail = get_max_vertices(ctx, ib, pushbuf_avail);
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);
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,
252 vbo_split_prims(ctx, arrays, prims, nr_prims, ib, min_index,
253 max_index, TAG(vbo_render_prims), &limits);
260 /* VBO rendering path. */
263 check_update_array(struct nouveau_array *a, unsigned offset,
264 struct nouveau_bo *bo, int *pdelta)
271 delta = ((int)offset - (int)a->offset) / a->stride;
273 dirty = (delta < 0 ||
274 offset != (a->offset + delta * a->stride));
279 *pdelta = (dirty ? 0 : delta);
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)
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;
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;
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;
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,
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,
325 dirty |= check_update_array(a, offset[i], bo[i], pdelta);
328 *pdelta -= min_index;
331 /* Buffers changed, update the attribute binding. */
332 FOR_EACH_BOUND_ATTR(render, i, attr) {
333 struct nouveau_array *a = &render->attrs[attr];
335 nouveau_bo_ref(NULL, &a->bo);
336 a->offset = offset[i];
340 TAG(render_bind_vertices)(ctx);
344 FOR_EACH_BOUND_ATTR(render, i, attr)
345 nouveau_bo_ref(NULL, &bo[i]);
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,
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;
362 TAG(render_set_format)(ctx);
364 for (i = 0; i < nr_prims; i++) {
365 unsigned start = prims[i].start,
366 count = prims[i].count;
368 if (i == 0 || basevertex != prims[i].basevertex) {
369 basevertex = prims[i].basevertex;
370 vbo_bind_vertices(ctx, arrays, basevertex, min_index,
374 if (count > get_max_vertices(ctx, ib, AVAIL_RING(chan)))
375 WAIT_RING(chan, PUSHBUF_DWORDS);
377 BATCH_BEGIN(nvgl_primitive(prims[i].mode));
378 dispatch(ctx, start, delta, count);
383 /* Immediate rendering path. */
386 extract_id(struct nouveau_array *a, int i, int j)
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,
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;
403 for (i = 0; i < nr_prims; i++) {
404 unsigned start = prims[i].start,
405 end = start + prims[i].count;
407 if (prims[i].count > get_max_vertices(ctx, ib,
409 WAIT_RING(chan, PUSHBUF_DWORDS);
411 BATCH_BEGIN(nvgl_primitive(prims[i].mode));
413 for (; start < end; start++) {
414 j = prims[i].basevertex +
415 extract(&render->ib, 0, start);
417 FOR_EACH_BOUND_ATTR(render, k, attr)
418 EMIT_IMM(ctx, &render->attrs[attr], j);
425 /* draw_prims entry point when we're doing hw-tnl. */
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)
435 struct nouveau_render_state *render = to_render_state(ctx);
437 if (!index_bounds_valid)
438 vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
440 vbo_choose_render_mode(ctx, arrays);
441 vbo_choose_attrs(ctx, arrays);
443 if (vbo_maybe_split(ctx, arrays, prims, nr_prims, ib, min_index,
447 vbo_init_arrays(ctx, ib, arrays);
449 if (render->mode == VBO)
450 vbo_draw_vbo(ctx, arrays, prims, nr_prims, ib, min_index,
453 vbo_draw_imm(ctx, arrays, prims, nr_prims, ib, min_index,
456 vbo_deinit_arrays(ctx, ib, arrays);
459 /* VBO rendering entry points. */
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)
469 struct nouveau_context *nctx = to_nouveau_context(ctx);
471 nouveau_validate_framebuffer(ctx);
473 if (nctx->fallback == HWTNL)
474 TAG(vbo_render_prims)(ctx, arrays, prims, nr_prims, ib,
475 index_bounds_valid, min_index, max_index);
477 if (nctx->fallback == SWTNL)
478 _tnl_vbo_draw_prims(ctx, arrays, prims, nr_prims, ib,
479 index_bounds_valid, min_index, max_index);
483 TAG(vbo_init)(struct gl_context *ctx)
485 struct nouveau_render_state *render = to_render_state(ctx);
488 for (i = 0; i < VERT_ATTRIB_MAX; i++)
491 vbo_set_draw_func(ctx, TAG(vbo_check_render_prims));
492 vbo_use_buffer_objects(ctx);
496 TAG(vbo_destroy)(struct gl_context *ctx)