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/glformats.h"
33 #include "main/varray.h"
34 #include "main/image.h"
36 /* Arbitrary pushbuf length we can assume we can get with a single
37 * call to WAIT_RING. */
38 #define PUSHBUF_DWORDS 65536
40 /* Functions to turn GL arrays or index buffers into nouveau_array
44 get_array_stride(struct gl_context
*ctx
, const struct gl_vertex_array
*a
)
46 struct nouveau_render_state
*render
= to_render_state(ctx
);
47 const struct gl_vertex_buffer_binding
*binding
= a
->BufferBinding
;
49 if (render
->mode
== VBO
&& !_mesa_is_bufferobj(binding
->BufferObj
)) {
50 const struct gl_array_attributes
*attrib
= a
->VertexAttrib
;
51 /* Pack client buffers. */
52 return align(_mesa_sizeof_type(attrib
->Type
) * attrib
->Size
, 4);
54 return binding
->Stride
;
59 vbo_init_arrays(struct gl_context
*ctx
, const struct _mesa_index_buffer
*ib
,
60 const struct gl_vertex_array
*arrays
)
62 struct nouveau_render_state
*render
= to_render_state(ctx
);
63 GLboolean imm
= (render
->mode
== IMM
);
69 if (ib
->index_size
== 4)
70 ib_type
= GL_UNSIGNED_INT
;
71 else if (ib
->index_size
== 2)
72 ib_type
= GL_UNSIGNED_SHORT
;
74 ib_type
= GL_UNSIGNED_BYTE
;
76 nouveau_init_array(&render
->ib
, 0, 0, ib
->count
, ib_type
,
77 ib
->obj
, ib
->ptr
, GL_TRUE
, ctx
);
80 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
81 const struct gl_vertex_array
*array
= &arrays
[attr
];
82 const struct gl_vertex_buffer_binding
*binding
=
84 const struct gl_array_attributes
*attrib
= array
->VertexAttrib
;
85 const GLubyte
*p
= _mesa_vertex_attrib_address(attrib
, binding
);
87 nouveau_init_array(&render
->attrs
[attr
], attr
,
88 get_array_stride(ctx
, array
),
89 attrib
->Size
, attrib
->Type
,
90 imm
? binding
->BufferObj
: NULL
,
96 vbo_deinit_arrays(struct gl_context
*ctx
, const struct _mesa_index_buffer
*ib
,
97 const struct gl_vertex_array
*arrays
)
99 struct nouveau_render_state
*render
= to_render_state(ctx
);
103 nouveau_cleanup_array(&render
->ib
);
105 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
106 struct nouveau_array
*a
= &render
->attrs
[attr
];
108 if (render
->mode
== IMM
)
109 nouveau_bo_ref(NULL
, &a
->bo
);
111 nouveau_deinit_array(a
);
115 render
->attr_count
= 0;
118 /* Make some rendering decisions from the GL context. */
121 vbo_choose_render_mode(struct gl_context
*ctx
, const struct gl_vertex_array
*arrays
)
123 struct nouveau_render_state
*render
= to_render_state(ctx
);
128 if (ctx
->Light
.Enabled
) {
129 for (i
= 0; i
< VERT_ATTRIB_MAT_MAX
; i
++) {
130 if (arrays
[VERT_ATTRIB_MAT(i
)].BufferBinding
->Stride
) {
139 vbo_emit_attr(struct gl_context
*ctx
, const struct gl_vertex_array
*arrays
,
142 struct nouveau_pushbuf
*push
= context_push(ctx
);
143 struct nouveau_render_state
*render
= to_render_state(ctx
);
144 const struct gl_vertex_array
*array
= &arrays
[attr
];
145 const struct gl_vertex_buffer_binding
*binding
= array
->BufferBinding
;
146 const struct gl_array_attributes
*attrib
= array
->VertexAttrib
;
147 const GLubyte
*p
= _mesa_vertex_attrib_address(attrib
, binding
);
148 struct nouveau_array
*a
= &render
->attrs
[attr
];
151 if (!binding
->Stride
) {
152 if (attr
>= VERT_ATTRIB_MAT(0))
153 /* nouveau_update_state takes care of materials. */
156 /* Constant attribute. */
157 nouveau_init_array(a
, attr
, binding
->Stride
, attrib
->Size
,
158 attrib
->Type
, binding
->BufferObj
, p
,
161 nouveau_deinit_array(a
);
164 /* Varying attribute. */
165 struct nouveau_attr_info
*info
= &TAG(vertex_attrs
)[attr
];
167 if (render
->mode
== VBO
) {
168 render
->map
[info
->vbo_index
] = attr
;
169 render
->vertex_size
+= attrib
->_ElementSize
;
170 render
->attr_count
= MAX2(render
->attr_count
,
171 info
->vbo_index
+ 1);
173 render
->map
[render
->attr_count
++] = attr
;
174 render
->vertex_size
+= 4 * info
->imm_fields
;
179 #define MAT(a) VERT_ATTRIB_MAT(MAT_ATTRIB_##a)
182 vbo_choose_attrs(struct gl_context
*ctx
, const struct gl_vertex_array
*arrays
)
184 struct nouveau_render_state
*render
= to_render_state(ctx
);
187 /* Reset the vertex size. */
188 render
->vertex_size
= 0;
189 render
->attr_count
= 0;
191 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_COLOR0
);
192 if (ctx
->Fog
.ColorSumEnabled
&& !ctx
->Light
.Enabled
)
193 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_COLOR1
);
195 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
196 if (ctx
->Texture
._EnabledCoordUnits
& (1 << i
))
197 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_TEX0
+ i
);
200 if (ctx
->Fog
.Enabled
&& ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
)
201 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_FOG
);
203 if (ctx
->Light
.Enabled
||
204 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_NORMALS
))
205 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_NORMAL
);
207 if (ctx
->Light
.Enabled
&& render
->mode
== IMM
) {
208 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_AMBIENT
));
209 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_DIFFUSE
));
210 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_SPECULAR
));
211 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_SHININESS
));
213 if (ctx
->Light
.Model
.TwoSide
) {
214 vbo_emit_attr(ctx
, arrays
, MAT(BACK_AMBIENT
));
215 vbo_emit_attr(ctx
, arrays
, MAT(BACK_DIFFUSE
));
216 vbo_emit_attr(ctx
, arrays
, MAT(BACK_SPECULAR
));
217 vbo_emit_attr(ctx
, arrays
, MAT(BACK_SHININESS
));
221 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_POS
);
225 get_max_client_stride(struct gl_context
*ctx
, const struct gl_vertex_array
*arrays
)
227 struct nouveau_render_state
*render
= to_render_state(ctx
);
230 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
231 const struct gl_vertex_array
*a
= &arrays
[attr
];
233 if (!_mesa_is_bufferobj(a
->BufferBinding
->BufferObj
))
234 s
= MAX2(s
, get_array_stride(ctx
, a
));
241 TAG(vbo_render_prims
)(struct gl_context
*ctx
,
242 const struct _mesa_prim
*prims
, GLuint nr_prims
,
243 const struct _mesa_index_buffer
*ib
,
244 GLboolean index_bounds_valid
,
245 GLuint min_index
, GLuint max_index
,
246 struct gl_transform_feedback_object
*tfb_vertcount
,
248 struct gl_buffer_object
*indirect
);
251 vbo_maybe_split(struct gl_context
*ctx
, const struct gl_vertex_array
*arrays
,
252 const struct _mesa_prim
*prims
, GLuint nr_prims
,
253 const struct _mesa_index_buffer
*ib
,
254 GLuint min_index
, GLuint max_index
)
256 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
257 struct nouveau_render_state
*render
= to_render_state(ctx
);
258 struct nouveau_bufctx
*bufctx
= nctx
->hw
.bufctx
;
259 unsigned pushbuf_avail
= PUSHBUF_DWORDS
- 2 * (bufctx
->relocs
+
261 vert_avail
= get_max_vertices(ctx
, NULL
, pushbuf_avail
),
262 idx_avail
= get_max_vertices(ctx
, ib
, pushbuf_avail
);
265 /* Try to keep client buffers smaller than the scratch BOs. */
266 if (render
->mode
== VBO
&&
267 (stride
= get_max_client_stride(ctx
, arrays
)))
268 vert_avail
= MIN2(vert_avail
,
269 NOUVEAU_SCRATCH_SIZE
/ stride
);
271 if (max_index
- min_index
> vert_avail
||
272 (ib
&& ib
->count
> idx_avail
)) {
273 struct split_limits limits
= {
274 .max_verts
= vert_avail
,
275 .max_indices
= idx_avail
,
279 vbo_split_prims(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
280 max_index
, TAG(vbo_render_prims
), &limits
);
287 /* VBO rendering path. */
290 check_update_array(struct nouveau_array
*a
, unsigned offset
,
291 struct nouveau_bo
*bo
, int *pdelta
)
298 delta
= ((int)offset
- (int)a
->offset
) / a
->stride
;
300 dirty
= (delta
< 0 ||
301 offset
!= (a
->offset
+ delta
* a
->stride
));
306 *pdelta
= (dirty
? 0 : delta
);
311 vbo_bind_vertices(struct gl_context
*ctx
, const struct gl_vertex_array
*arrays
,
312 int base
, unsigned min_index
, unsigned max_index
, int *pdelta
)
314 struct nouveau_render_state
*render
= to_render_state(ctx
);
315 struct nouveau_pushbuf
*push
= context_push(ctx
);
316 struct nouveau_bo
*bo
[NUM_VERTEX_ATTRS
];
317 unsigned offset
[NUM_VERTEX_ATTRS
];
318 GLboolean dirty
= GL_FALSE
;
324 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
325 const struct gl_vertex_array
*array
= &arrays
[attr
];
326 const struct gl_vertex_buffer_binding
*binding
=
327 array
->BufferBinding
;
328 const struct gl_array_attributes
*attrib
= array
->VertexAttrib
;
329 const GLubyte
*p
= _mesa_vertex_attrib_address(attrib
, binding
);
330 struct gl_buffer_object
*obj
= binding
->BufferObj
;
331 struct nouveau_array
*a
= &render
->attrs
[attr
];
332 unsigned delta
= (base
+ min_index
) * binding
->Stride
;
336 if (nouveau_bufferobj_hw(obj
)) {
337 /* Array in a buffer obj. */
338 nouveau_bo_ref(to_nouveau_bufferobj(obj
)->bo
, &bo
[i
]);
339 offset
[i
] = delta
+ (intptr_t)p
;
342 int n
= max_index
- min_index
+ 1;
343 char *sp
= (char *)ADD_POINTERS(
344 nouveau_bufferobj_sys(obj
), p
) + delta
;
345 char *dp
= nouveau_get_scratch(ctx
, n
* a
->stride
,
348 /* Array in client memory, move it to a
349 * scratch buffer obj. */
350 for (j
= 0; j
< n
; j
++)
351 memcpy(dp
+ j
* a
->stride
,
352 sp
+ j
* binding
->Stride
,
356 dirty
|= check_update_array(a
, offset
[i
], bo
[i
], pdelta
);
359 *pdelta
-= min_index
;
362 /* Buffers changed, update the attribute binding. */
363 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
364 struct nouveau_array
*a
= &render
->attrs
[attr
];
366 nouveau_bo_ref(NULL
, &a
->bo
);
367 a
->offset
= offset
[i
];
371 TAG(render_release_vertices
)(ctx
);
372 TAG(render_bind_vertices
)(ctx
);
375 FOR_EACH_BOUND_ATTR(render
, i
, attr
)
376 nouveau_bo_ref(NULL
, &bo
[i
]);
383 vbo_draw_vbo(struct gl_context
*ctx
, const struct gl_vertex_array
*arrays
,
384 const struct _mesa_prim
*prims
, GLuint nr_prims
,
385 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
388 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
389 struct nouveau_pushbuf
*push
= context_push(ctx
);
390 dispatch_t dispatch
= get_array_dispatch(&to_render_state(ctx
)->ib
);
391 int i
, delta
= 0, basevertex
= 0;
394 TAG(render_set_format
)(ctx
);
396 for (i
= 0; i
< nr_prims
; i
++) {
397 unsigned start
= prims
[i
].start
,
398 count
= prims
[i
].count
;
400 if (i
== 0 || basevertex
!= prims
[i
].basevertex
) {
401 basevertex
= prims
[i
].basevertex
;
402 vbo_bind_vertices(ctx
, arrays
, basevertex
, min_index
,
405 nouveau_pushbuf_bufctx(push
, nctx
->hw
.bufctx
);
406 if (nouveau_pushbuf_validate(push
)) {
407 nouveau_pushbuf_bufctx(push
, NULL
);
412 if (count
> get_max_vertices(ctx
, ib
, PUSH_AVAIL(push
)))
413 PUSH_SPACE(push
, PUSHBUF_DWORDS
);
415 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
416 dispatch(ctx
, start
, delta
, count
);
420 nouveau_pushbuf_bufctx(push
, NULL
);
421 TAG(render_release_vertices
)(ctx
);
424 /* Immediate rendering path. */
427 extract_id(struct nouveau_array
*a
, int i
, int j
)
433 vbo_draw_imm(struct gl_context
*ctx
, const struct gl_vertex_array
*arrays
,
434 const struct _mesa_prim
*prims
, GLuint nr_prims
,
435 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
438 struct nouveau_render_state
*render
= to_render_state(ctx
);
439 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
440 struct nouveau_pushbuf
*push
= context_push(ctx
);
441 extract_u_t extract
= ib
? render
->ib
.extract_u
: extract_id
;
445 nouveau_pushbuf_bufctx(push
, nctx
->hw
.bufctx
);
446 if (nouveau_pushbuf_validate(push
)) {
447 nouveau_pushbuf_bufctx(push
, NULL
);
451 for (i
= 0; i
< nr_prims
; i
++) {
452 unsigned start
= prims
[i
].start
,
453 end
= start
+ prims
[i
].count
;
455 if (prims
[i
].count
> get_max_vertices(ctx
, ib
,
457 PUSH_SPACE(push
, PUSHBUF_DWORDS
);
459 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
461 for (; start
< end
; start
++) {
462 j
= prims
[i
].basevertex
+
463 extract(&render
->ib
, 0, start
);
465 FOR_EACH_BOUND_ATTR(render
, k
, attr
)
466 EMIT_IMM(ctx
, &render
->attrs
[attr
], j
);
472 nouveau_pushbuf_bufctx(push
, NULL
);
475 /* draw_prims entry point when we're doing hw-tnl. */
478 TAG(vbo_render_prims
)(struct gl_context
*ctx
,
479 const struct _mesa_prim
*prims
, GLuint nr_prims
,
480 const struct _mesa_index_buffer
*ib
,
481 GLboolean index_bounds_valid
,
482 GLuint min_index
, GLuint max_index
,
483 struct gl_transform_feedback_object
*tfb_vertcount
,
485 struct gl_buffer_object
*indirect
)
487 struct nouveau_render_state
*render
= to_render_state(ctx
);
488 const struct gl_vertex_array
*arrays
= ctx
->Array
._DrawArrays
;
490 if (!index_bounds_valid
)
491 vbo_get_minmax_indices(ctx
, prims
, ib
, &min_index
, &max_index
,
494 vbo_choose_render_mode(ctx
, arrays
);
495 vbo_choose_attrs(ctx
, arrays
);
497 if (vbo_maybe_split(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
501 vbo_init_arrays(ctx
, ib
, arrays
);
503 if (render
->mode
== VBO
)
504 vbo_draw_vbo(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
507 vbo_draw_imm(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
510 vbo_deinit_arrays(ctx
, ib
, arrays
);
513 /* VBO rendering entry points. */
516 TAG(vbo_check_render_prims
)(struct gl_context
*ctx
,
517 const struct _mesa_prim
*prims
, GLuint nr_prims
,
518 const struct _mesa_index_buffer
*ib
,
519 GLboolean index_bounds_valid
,
520 GLuint min_index
, GLuint max_index
,
521 struct gl_transform_feedback_object
*tfb_vertcount
,
523 struct gl_buffer_object
*indirect
)
525 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
527 nouveau_validate_framebuffer(ctx
);
529 if (nctx
->fallback
== HWTNL
)
530 TAG(vbo_render_prims
)(ctx
, prims
, nr_prims
, ib
,
531 index_bounds_valid
, min_index
, max_index
,
532 tfb_vertcount
, stream
, indirect
);
534 if (nctx
->fallback
== SWTNL
)
535 _tnl_draw_prims(ctx
, prims
, nr_prims
, ib
,
536 index_bounds_valid
, min_index
, max_index
,
537 tfb_vertcount
, stream
, indirect
);
541 TAG(vbo_init
)(struct gl_context
*ctx
)
543 struct nouveau_render_state
*render
= to_render_state(ctx
);
546 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
549 vbo_set_draw_func(ctx
, TAG(vbo_check_render_prims
));
550 vbo_use_buffer_objects(ctx
);
554 TAG(vbo_destroy
)(struct gl_context
*ctx
)