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/image.h"
35 /* Arbitrary pushbuf length we can assume we can get with a single
36 * call to WAIT_RING. */
37 #define PUSHBUF_DWORDS 65536
39 /* Functions to turn GL arrays or index buffers into nouveau_array
43 get_array_stride(struct gl_context
*ctx
, const struct gl_client_array
*a
)
45 struct nouveau_render_state
*render
= to_render_state(ctx
);
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);
55 vbo_init_arrays(struct gl_context
*ctx
, const struct _mesa_index_buffer
*ib
,
56 const struct gl_client_array
**arrays
)
58 struct nouveau_render_state
*render
= to_render_state(ctx
);
59 GLboolean imm
= (render
->mode
== IMM
);
63 nouveau_init_array(&render
->ib
, 0, 0, ib
->count
, ib
->type
,
64 ib
->obj
, ib
->ptr
, GL_TRUE
, ctx
);
66 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
67 const struct gl_client_array
*array
= arrays
[attr
];
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
);
78 vbo_deinit_arrays(struct gl_context
*ctx
, const struct _mesa_index_buffer
*ib
,
79 const struct gl_client_array
**arrays
)
81 struct nouveau_render_state
*render
= to_render_state(ctx
);
85 nouveau_cleanup_array(&render
->ib
);
87 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
88 struct nouveau_array
*a
= &render
->attrs
[attr
];
90 if (render
->mode
== IMM
)
91 nouveau_bo_ref(NULL
, &a
->bo
);
93 nouveau_deinit_array(a
);
97 render
->attr_count
= 0;
100 /* Make some rendering decisions from the GL context. */
103 vbo_choose_render_mode(struct gl_context
*ctx
, const struct gl_client_array
**arrays
)
105 struct nouveau_render_state
*render
= to_render_state(ctx
);
110 if (ctx
->Light
.Enabled
) {
111 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
112 if (arrays
[VERT_ATTRIB_GENERIC0
+ i
]->StrideB
) {
121 vbo_emit_attr(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
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
];
130 if (!array
->StrideB
) {
131 if (attr
>= VERT_ATTRIB_GENERIC0
)
132 /* nouveau_update_state takes care of materials. */
135 /* Constant attribute. */
136 nouveau_init_array(a
, attr
, array
->StrideB
, array
->Size
,
137 array
->Type
, array
->BufferObj
, array
->Ptr
,
140 nouveau_deinit_array(a
);
143 /* Varying attribute. */
144 struct nouveau_attr_info
*info
= &TAG(vertex_attrs
)[attr
];
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);
152 render
->map
[render
->attr_count
++] = attr
;
153 render
->vertex_size
+= 4 * info
->imm_fields
;
158 #define MAT(a) (VERT_ATTRIB_GENERIC0 + MAT_ATTRIB_##a)
161 vbo_choose_attrs(struct gl_context
*ctx
, const struct gl_client_array
**arrays
)
163 struct nouveau_render_state
*render
= to_render_state(ctx
);
166 /* Reset the vertex size. */
167 render
->vertex_size
= 0;
168 render
->attr_count
= 0;
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
);
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
);
179 if (ctx
->Fog
.Enabled
&& ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
)
180 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_FOG
);
182 if (ctx
->Light
.Enabled
||
183 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_NORMALS
))
184 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_NORMAL
);
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
));
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
));
200 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_POS
);
204 get_max_client_stride(struct gl_context
*ctx
, const struct gl_client_array
**arrays
)
206 struct nouveau_render_state
*render
= to_render_state(ctx
);
209 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
210 const struct gl_client_array
*a
= arrays
[attr
];
212 if (!_mesa_is_bufferobj(a
->BufferObj
))
213 s
= MAX2(s
, get_array_stride(ctx
, a
));
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
,
227 struct gl_buffer_object
*indirect
);
230 vbo_maybe_split(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
231 const struct _mesa_prim
*prims
, GLuint nr_prims
,
232 const struct _mesa_index_buffer
*ib
,
233 GLuint min_index
, GLuint max_index
)
235 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
236 struct nouveau_render_state
*render
= to_render_state(ctx
);
237 struct nouveau_bufctx
*bufctx
= nctx
->hw
.bufctx
;
238 unsigned pushbuf_avail
= PUSHBUF_DWORDS
- 2 * (bufctx
->relocs
+
240 vert_avail
= get_max_vertices(ctx
, NULL
, pushbuf_avail
),
241 idx_avail
= get_max_vertices(ctx
, ib
, pushbuf_avail
);
244 /* Try to keep client buffers smaller than the scratch BOs. */
245 if (render
->mode
== VBO
&&
246 (stride
= get_max_client_stride(ctx
, arrays
)))
247 vert_avail
= MIN2(vert_avail
,
248 NOUVEAU_SCRATCH_SIZE
/ stride
);
250 if (max_index
- min_index
> vert_avail
||
251 (ib
&& ib
->count
> idx_avail
)) {
252 struct split_limits limits
= {
253 .max_verts
= vert_avail
,
254 .max_indices
= idx_avail
,
258 vbo_split_prims(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
259 max_index
, TAG(vbo_render_prims
), &limits
);
266 /* VBO rendering path. */
269 check_update_array(struct nouveau_array
*a
, unsigned offset
,
270 struct nouveau_bo
*bo
, int *pdelta
)
277 delta
= ((int)offset
- (int)a
->offset
) / a
->stride
;
279 dirty
= (delta
< 0 ||
280 offset
!= (a
->offset
+ delta
* a
->stride
));
285 *pdelta
= (dirty
? 0 : delta
);
290 vbo_bind_vertices(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
291 int base
, unsigned min_index
, unsigned max_index
, int *pdelta
)
293 struct nouveau_render_state
*render
= to_render_state(ctx
);
294 struct nouveau_pushbuf
*push
= context_push(ctx
);
295 struct nouveau_bo
*bo
[NUM_VERTEX_ATTRS
];
296 unsigned offset
[NUM_VERTEX_ATTRS
];
297 GLboolean dirty
= GL_FALSE
;
303 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
304 const struct gl_client_array
*array
= arrays
[attr
];
305 struct gl_buffer_object
*obj
= array
->BufferObj
;
306 struct nouveau_array
*a
= &render
->attrs
[attr
];
307 unsigned delta
= (base
+ min_index
) * array
->StrideB
;
311 if (nouveau_bufferobj_hw(obj
)) {
312 /* Array in a buffer obj. */
313 nouveau_bo_ref(to_nouveau_bufferobj(obj
)->bo
, &bo
[i
]);
314 offset
[i
] = delta
+ (intptr_t)array
->Ptr
;
317 int n
= max_index
- min_index
+ 1;
318 char *sp
= (char *)ADD_POINTERS(
319 nouveau_bufferobj_sys(obj
), array
->Ptr
) + delta
;
320 char *dp
= nouveau_get_scratch(ctx
, n
* a
->stride
,
323 /* Array in client memory, move it to a
324 * scratch buffer obj. */
325 for (j
= 0; j
< n
; j
++)
326 memcpy(dp
+ j
* a
->stride
,
327 sp
+ j
* array
->StrideB
,
331 dirty
|= check_update_array(a
, offset
[i
], bo
[i
], pdelta
);
334 *pdelta
-= min_index
;
337 /* Buffers changed, update the attribute binding. */
338 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
339 struct nouveau_array
*a
= &render
->attrs
[attr
];
341 nouveau_bo_ref(NULL
, &a
->bo
);
342 a
->offset
= offset
[i
];
346 TAG(render_release_vertices
)(ctx
);
347 TAG(render_bind_vertices
)(ctx
);
350 FOR_EACH_BOUND_ATTR(render
, i
, attr
)
351 nouveau_bo_ref(NULL
, &bo
[i
]);
358 vbo_draw_vbo(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
359 const struct _mesa_prim
*prims
, GLuint nr_prims
,
360 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
363 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
364 struct nouveau_pushbuf
*push
= context_push(ctx
);
365 dispatch_t dispatch
= get_array_dispatch(&to_render_state(ctx
)->ib
);
366 int i
, delta
= 0, basevertex
= 0;
369 TAG(render_set_format
)(ctx
);
371 for (i
= 0; i
< nr_prims
; i
++) {
372 unsigned start
= prims
[i
].start
,
373 count
= prims
[i
].count
;
375 if (i
== 0 || basevertex
!= prims
[i
].basevertex
) {
376 basevertex
= prims
[i
].basevertex
;
377 vbo_bind_vertices(ctx
, arrays
, basevertex
, min_index
,
380 nouveau_pushbuf_bufctx(push
, nctx
->hw
.bufctx
);
381 if (nouveau_pushbuf_validate(push
)) {
382 nouveau_pushbuf_bufctx(push
, NULL
);
387 if (count
> get_max_vertices(ctx
, ib
, PUSH_AVAIL(push
)))
388 PUSH_SPACE(push
, PUSHBUF_DWORDS
);
390 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
391 dispatch(ctx
, start
, delta
, count
);
395 nouveau_pushbuf_bufctx(push
, NULL
);
396 TAG(render_release_vertices
)(ctx
);
399 /* Immediate rendering path. */
402 extract_id(struct nouveau_array
*a
, int i
, int j
)
408 vbo_draw_imm(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
409 const struct _mesa_prim
*prims
, GLuint nr_prims
,
410 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
413 struct nouveau_render_state
*render
= to_render_state(ctx
);
414 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
415 struct nouveau_pushbuf
*push
= context_push(ctx
);
416 extract_u_t extract
= ib
? render
->ib
.extract_u
: extract_id
;
420 nouveau_pushbuf_bufctx(push
, nctx
->hw
.bufctx
);
421 if (nouveau_pushbuf_validate(push
)) {
422 nouveau_pushbuf_bufctx(push
, NULL
);
426 for (i
= 0; i
< nr_prims
; i
++) {
427 unsigned start
= prims
[i
].start
,
428 end
= start
+ prims
[i
].count
;
430 if (prims
[i
].count
> get_max_vertices(ctx
, ib
,
432 PUSH_SPACE(push
, PUSHBUF_DWORDS
);
434 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
436 for (; start
< end
; start
++) {
437 j
= prims
[i
].basevertex
+
438 extract(&render
->ib
, 0, start
);
440 FOR_EACH_BOUND_ATTR(render
, k
, attr
)
441 EMIT_IMM(ctx
, &render
->attrs
[attr
], j
);
447 nouveau_pushbuf_bufctx(push
, NULL
);
450 /* draw_prims entry point when we're doing hw-tnl. */
453 TAG(vbo_render_prims
)(struct gl_context
*ctx
,
454 const struct _mesa_prim
*prims
, GLuint nr_prims
,
455 const struct _mesa_index_buffer
*ib
,
456 GLboolean index_bounds_valid
,
457 GLuint min_index
, GLuint max_index
,
458 struct gl_transform_feedback_object
*tfb_vertcount
,
460 struct gl_buffer_object
*indirect
)
462 struct nouveau_render_state
*render
= to_render_state(ctx
);
463 const struct gl_client_array
**arrays
= ctx
->Array
._DrawArrays
;
465 if (!index_bounds_valid
)
466 vbo_get_minmax_indices(ctx
, prims
, ib
, &min_index
, &max_index
,
469 vbo_choose_render_mode(ctx
, arrays
);
470 vbo_choose_attrs(ctx
, arrays
);
472 if (vbo_maybe_split(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
476 vbo_init_arrays(ctx
, ib
, arrays
);
478 if (render
->mode
== VBO
)
479 vbo_draw_vbo(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
482 vbo_draw_imm(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
485 vbo_deinit_arrays(ctx
, ib
, arrays
);
488 /* VBO rendering entry points. */
491 TAG(vbo_check_render_prims
)(struct gl_context
*ctx
,
492 const struct _mesa_prim
*prims
, GLuint nr_prims
,
493 const struct _mesa_index_buffer
*ib
,
494 GLboolean index_bounds_valid
,
495 GLuint min_index
, GLuint max_index
,
496 struct gl_transform_feedback_object
*tfb_vertcount
,
498 struct gl_buffer_object
*indirect
)
500 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
502 nouveau_validate_framebuffer(ctx
);
504 if (nctx
->fallback
== HWTNL
)
505 TAG(vbo_render_prims
)(ctx
, prims
, nr_prims
, ib
,
506 index_bounds_valid
, min_index
, max_index
,
507 tfb_vertcount
, stream
, indirect
);
509 if (nctx
->fallback
== SWTNL
)
510 _tnl_draw_prims(ctx
, prims
, nr_prims
, ib
,
511 index_bounds_valid
, min_index
, max_index
,
512 tfb_vertcount
, stream
, indirect
);
516 TAG(vbo_init
)(struct gl_context
*ctx
)
518 struct nouveau_render_state
*render
= to_render_state(ctx
);
521 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
524 vbo_set_draw_func(ctx
, TAG(vbo_check_render_prims
));
525 vbo_use_buffer_objects(ctx
);
529 TAG(vbo_destroy
)(struct gl_context
*ctx
)