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_driver.h"
28 #include "nouveau_bufferobj.h"
29 #include "nouveau_util.h"
31 #include "main/bufferobj.h"
32 #include "main/image.h"
34 /* Arbitrary pushbuf length we can assume we can get with a single
35 * call to WAIT_RING. */
36 #define PUSHBUF_DWORDS 65536
38 /* Functions to turn GL arrays or index buffers into nouveau_array
42 get_array_stride(struct gl_context
*ctx
, const struct gl_client_array
*a
)
44 struct nouveau_render_state
*render
= to_render_state(ctx
);
46 if (render
->mode
== VBO
&& !_mesa_is_bufferobj(a
->BufferObj
))
47 /* Pack client buffers. */
48 return align(_mesa_sizeof_type(a
->Type
) * a
->Size
, 4);
54 vbo_init_arrays(struct gl_context
*ctx
, const struct _mesa_index_buffer
*ib
,
55 const struct gl_client_array
**arrays
)
57 struct nouveau_render_state
*render
= to_render_state(ctx
);
58 GLboolean imm
= (render
->mode
== IMM
);
62 nouveau_init_array(&render
->ib
, 0, 0, ib
->count
, ib
->type
,
63 ib
->obj
, ib
->ptr
, GL_TRUE
, ctx
);
65 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
66 const struct gl_client_array
*array
= arrays
[attr
];
68 nouveau_init_array(&render
->attrs
[attr
], attr
,
69 get_array_stride(ctx
, array
),
70 array
->Size
, array
->Type
,
71 imm
? array
->BufferObj
: NULL
,
72 array
->Ptr
, imm
, ctx
);
77 vbo_deinit_arrays(struct gl_context
*ctx
, const struct _mesa_index_buffer
*ib
,
78 const struct gl_client_array
**arrays
)
80 struct nouveau_render_state
*render
= to_render_state(ctx
);
84 nouveau_cleanup_array(&render
->ib
);
86 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
87 struct nouveau_array
*a
= &render
->attrs
[attr
];
89 if (render
->mode
== IMM
)
90 nouveau_bo_ref(NULL
, &a
->bo
);
92 nouveau_deinit_array(a
);
96 render
->attr_count
= 0;
99 /* Make some rendering decisions from the GL context. */
102 vbo_choose_render_mode(struct gl_context
*ctx
, const struct gl_client_array
**arrays
)
104 struct nouveau_render_state
*render
= to_render_state(ctx
);
109 if (ctx
->Light
.Enabled
) {
110 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
111 if (arrays
[VERT_ATTRIB_GENERIC0
+ i
]->StrideB
) {
120 vbo_emit_attr(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
123 struct nouveau_pushbuf
*push
= context_push(ctx
);
124 struct nouveau_render_state
*render
= to_render_state(ctx
);
125 const struct gl_client_array
*array
= arrays
[attr
];
126 struct nouveau_array
*a
= &render
->attrs
[attr
];
129 if (!array
->StrideB
) {
130 if (attr
>= VERT_ATTRIB_GENERIC0
)
131 /* nouveau_update_state takes care of materials. */
134 /* Constant attribute. */
135 nouveau_init_array(a
, attr
, array
->StrideB
, array
->Size
,
136 array
->Type
, array
->BufferObj
, array
->Ptr
,
139 nouveau_deinit_array(a
);
142 /* Varying attribute. */
143 struct nouveau_attr_info
*info
= &TAG(vertex_attrs
)[attr
];
145 if (render
->mode
== VBO
) {
146 render
->map
[info
->vbo_index
] = attr
;
147 render
->vertex_size
+= array
->_ElementSize
;
148 render
->attr_count
= MAX2(render
->attr_count
,
149 info
->vbo_index
+ 1);
151 render
->map
[render
->attr_count
++] = attr
;
152 render
->vertex_size
+= 4 * info
->imm_fields
;
157 #define MAT(a) (VERT_ATTRIB_GENERIC0 + MAT_ATTRIB_##a)
160 vbo_choose_attrs(struct gl_context
*ctx
, const struct gl_client_array
**arrays
)
162 struct nouveau_render_state
*render
= to_render_state(ctx
);
165 /* Reset the vertex size. */
166 render
->vertex_size
= 0;
167 render
->attr_count
= 0;
169 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_COLOR0
);
170 if (ctx
->Fog
.ColorSumEnabled
&& !ctx
->Light
.Enabled
)
171 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_COLOR1
);
173 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
174 if (ctx
->Texture
._EnabledCoordUnits
& (1 << i
))
175 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_TEX0
+ i
);
178 if (ctx
->Fog
.Enabled
&& ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
)
179 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_FOG
);
181 if (ctx
->Light
.Enabled
||
182 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_NORMALS
))
183 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_NORMAL
);
185 if (ctx
->Light
.Enabled
&& render
->mode
== IMM
) {
186 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_AMBIENT
));
187 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_DIFFUSE
));
188 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_SPECULAR
));
189 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_SHININESS
));
191 if (ctx
->Light
.Model
.TwoSide
) {
192 vbo_emit_attr(ctx
, arrays
, MAT(BACK_AMBIENT
));
193 vbo_emit_attr(ctx
, arrays
, MAT(BACK_DIFFUSE
));
194 vbo_emit_attr(ctx
, arrays
, MAT(BACK_SPECULAR
));
195 vbo_emit_attr(ctx
, arrays
, MAT(BACK_SHININESS
));
199 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_POS
);
203 get_max_client_stride(struct gl_context
*ctx
, const struct gl_client_array
**arrays
)
205 struct nouveau_render_state
*render
= to_render_state(ctx
);
208 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
209 const struct gl_client_array
*a
= arrays
[attr
];
211 if (!_mesa_is_bufferobj(a
->BufferObj
))
212 s
= MAX2(s
, get_array_stride(ctx
, a
));
219 TAG(vbo_render_prims
)(struct gl_context
*ctx
,
220 const struct _mesa_prim
*prims
, GLuint nr_prims
,
221 const struct _mesa_index_buffer
*ib
,
222 GLboolean index_bounds_valid
,
223 GLuint min_index
, GLuint max_index
,
224 struct gl_transform_feedback_object
*tfb_vertcount
);
227 vbo_maybe_split(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
228 const struct _mesa_prim
*prims
, GLuint nr_prims
,
229 const struct _mesa_index_buffer
*ib
,
230 GLuint min_index
, GLuint max_index
)
232 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
233 struct nouveau_render_state
*render
= to_render_state(ctx
);
234 struct nouveau_bufctx
*bufctx
= nctx
->hw
.bufctx
;
235 unsigned pushbuf_avail
= PUSHBUF_DWORDS
- 2 * (bufctx
->relocs
+
237 vert_avail
= get_max_vertices(ctx
, NULL
, pushbuf_avail
),
238 idx_avail
= get_max_vertices(ctx
, ib
, pushbuf_avail
);
241 /* Try to keep client buffers smaller than the scratch BOs. */
242 if (render
->mode
== VBO
&&
243 (stride
= get_max_client_stride(ctx
, arrays
)))
244 vert_avail
= MIN2(vert_avail
,
245 NOUVEAU_SCRATCH_SIZE
/ stride
);
247 if (max_index
- min_index
> vert_avail
||
248 (ib
&& ib
->count
> idx_avail
)) {
249 struct split_limits limits
= {
250 .max_verts
= vert_avail
,
251 .max_indices
= idx_avail
,
255 vbo_split_prims(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
256 max_index
, TAG(vbo_render_prims
), &limits
);
263 /* VBO rendering path. */
266 check_update_array(struct nouveau_array
*a
, unsigned offset
,
267 struct nouveau_bo
*bo
, int *pdelta
)
274 delta
= ((int)offset
- (int)a
->offset
) / a
->stride
;
276 dirty
= (delta
< 0 ||
277 offset
!= (a
->offset
+ delta
* a
->stride
));
282 *pdelta
= (dirty
? 0 : delta
);
287 vbo_bind_vertices(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
288 int base
, unsigned min_index
, unsigned max_index
, int *pdelta
)
290 struct nouveau_render_state
*render
= to_render_state(ctx
);
291 struct nouveau_pushbuf
*push
= context_push(ctx
);
292 struct nouveau_bo
*bo
[NUM_VERTEX_ATTRS
];
293 unsigned offset
[NUM_VERTEX_ATTRS
];
294 GLboolean dirty
= GL_FALSE
;
300 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
301 const struct gl_client_array
*array
= arrays
[attr
];
302 struct gl_buffer_object
*obj
= array
->BufferObj
;
303 struct nouveau_array
*a
= &render
->attrs
[attr
];
304 unsigned delta
= (base
+ min_index
) * array
->StrideB
;
308 if (nouveau_bufferobj_hw(obj
)) {
309 /* Array in a buffer obj. */
310 nouveau_bo_ref(to_nouveau_bufferobj(obj
)->bo
, &bo
[i
]);
311 offset
[i
] = delta
+ (intptr_t)array
->Ptr
;
314 int n
= max_index
- min_index
+ 1;
315 char *sp
= (char *)ADD_POINTERS(
316 nouveau_bufferobj_sys(obj
), array
->Ptr
) + delta
;
317 char *dp
= nouveau_get_scratch(ctx
, n
* a
->stride
,
320 /* Array in client memory, move it to a
321 * scratch buffer obj. */
322 for (j
= 0; j
< n
; j
++)
323 memcpy(dp
+ j
* a
->stride
,
324 sp
+ j
* array
->StrideB
,
328 dirty
|= check_update_array(a
, offset
[i
], bo
[i
], pdelta
);
331 *pdelta
-= min_index
;
334 /* Buffers changed, update the attribute binding. */
335 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
336 struct nouveau_array
*a
= &render
->attrs
[attr
];
338 nouveau_bo_ref(NULL
, &a
->bo
);
339 a
->offset
= offset
[i
];
343 TAG(render_release_vertices
)(ctx
);
344 TAG(render_bind_vertices
)(ctx
);
347 FOR_EACH_BOUND_ATTR(render
, i
, attr
)
348 nouveau_bo_ref(NULL
, &bo
[i
]);
355 vbo_draw_vbo(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
356 const struct _mesa_prim
*prims
, GLuint nr_prims
,
357 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
360 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
361 struct nouveau_pushbuf
*push
= context_push(ctx
);
362 dispatch_t dispatch
= get_array_dispatch(&to_render_state(ctx
)->ib
);
363 int i
, delta
= 0, basevertex
= 0;
366 TAG(render_set_format
)(ctx
);
368 for (i
= 0; i
< nr_prims
; i
++) {
369 unsigned start
= prims
[i
].start
,
370 count
= prims
[i
].count
;
372 if (i
== 0 || basevertex
!= prims
[i
].basevertex
) {
373 basevertex
= prims
[i
].basevertex
;
374 vbo_bind_vertices(ctx
, arrays
, basevertex
, min_index
,
377 nouveau_pushbuf_bufctx(push
, nctx
->hw
.bufctx
);
378 if (nouveau_pushbuf_validate(push
)) {
379 nouveau_pushbuf_bufctx(push
, NULL
);
384 if (count
> get_max_vertices(ctx
, ib
, PUSH_AVAIL(push
)))
385 PUSH_SPACE(push
, PUSHBUF_DWORDS
);
387 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
388 dispatch(ctx
, start
, delta
, count
);
392 nouveau_pushbuf_bufctx(push
, NULL
);
393 TAG(render_release_vertices
)(ctx
);
396 /* Immediate rendering path. */
399 extract_id(struct nouveau_array
*a
, int i
, int j
)
405 vbo_draw_imm(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
406 const struct _mesa_prim
*prims
, GLuint nr_prims
,
407 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
410 struct nouveau_render_state
*render
= to_render_state(ctx
);
411 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
412 struct nouveau_pushbuf
*push
= context_push(ctx
);
413 extract_u_t extract
= ib
? render
->ib
.extract_u
: extract_id
;
417 nouveau_pushbuf_bufctx(push
, nctx
->hw
.bufctx
);
418 if (nouveau_pushbuf_validate(push
)) {
419 nouveau_pushbuf_bufctx(push
, NULL
);
423 for (i
= 0; i
< nr_prims
; i
++) {
424 unsigned start
= prims
[i
].start
,
425 end
= start
+ prims
[i
].count
;
427 if (prims
[i
].count
> get_max_vertices(ctx
, ib
,
429 PUSH_SPACE(push
, PUSHBUF_DWORDS
);
431 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
433 for (; start
< end
; start
++) {
434 j
= prims
[i
].basevertex
+
435 extract(&render
->ib
, 0, start
);
437 FOR_EACH_BOUND_ATTR(render
, k
, attr
)
438 EMIT_IMM(ctx
, &render
->attrs
[attr
], j
);
444 nouveau_pushbuf_bufctx(push
, NULL
);
447 /* draw_prims entry point when we're doing hw-tnl. */
450 TAG(vbo_render_prims
)(struct gl_context
*ctx
,
451 const struct _mesa_prim
*prims
, GLuint nr_prims
,
452 const struct _mesa_index_buffer
*ib
,
453 GLboolean index_bounds_valid
,
454 GLuint min_index
, GLuint max_index
,
455 struct gl_transform_feedback_object
*tfb_vertcount
)
457 struct nouveau_render_state
*render
= to_render_state(ctx
);
458 const struct gl_client_array
**arrays
= ctx
->Array
._DrawArrays
;
460 if (!index_bounds_valid
)
461 vbo_get_minmax_indices(ctx
, prims
, ib
, &min_index
, &max_index
,
464 vbo_choose_render_mode(ctx
, arrays
);
465 vbo_choose_attrs(ctx
, arrays
);
467 if (vbo_maybe_split(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
471 vbo_init_arrays(ctx
, ib
, arrays
);
473 if (render
->mode
== VBO
)
474 vbo_draw_vbo(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
477 vbo_draw_imm(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
480 vbo_deinit_arrays(ctx
, ib
, arrays
);
483 /* VBO rendering entry points. */
486 TAG(vbo_check_render_prims
)(struct gl_context
*ctx
,
487 const struct _mesa_prim
*prims
, GLuint nr_prims
,
488 const struct _mesa_index_buffer
*ib
,
489 GLboolean index_bounds_valid
,
490 GLuint min_index
, GLuint max_index
,
491 struct gl_transform_feedback_object
*tfb_vertcount
)
493 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
495 nouveau_validate_framebuffer(ctx
);
497 if (nctx
->fallback
== HWTNL
)
498 TAG(vbo_render_prims
)(ctx
, prims
, nr_prims
, ib
,
499 index_bounds_valid
, min_index
, max_index
,
502 if (nctx
->fallback
== SWTNL
)
503 _tnl_vbo_draw_prims(ctx
, prims
, nr_prims
, ib
,
504 index_bounds_valid
, min_index
, max_index
,
509 TAG(vbo_init
)(struct gl_context
*ctx
)
511 struct nouveau_render_state
*render
= to_render_state(ctx
);
514 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
517 vbo_set_draw_func(ctx
, TAG(vbo_check_render_prims
));
518 vbo_use_buffer_objects(ctx
);
522 TAG(vbo_destroy
)(struct gl_context
*ctx
)