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
;
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
, const struct gl_client_array
**arrays
,
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
);
226 vbo_maybe_split(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
227 const struct _mesa_prim
*prims
, GLuint nr_prims
,
228 const struct _mesa_index_buffer
*ib
,
229 GLuint min_index
, GLuint max_index
)
231 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
232 struct nouveau_render_state
*render
= to_render_state(ctx
);
233 unsigned pushbuf_avail
= PUSHBUF_DWORDS
- 2 * (nctx
->bo
.count
+
235 vert_avail
= get_max_vertices(ctx
, NULL
, pushbuf_avail
),
236 idx_avail
= get_max_vertices(ctx
, ib
, pushbuf_avail
);
239 /* Try to keep client buffers smaller than the scratch BOs. */
240 if (render
->mode
== VBO
&&
241 (stride
= get_max_client_stride(ctx
, arrays
)))
242 vert_avail
= MIN2(vert_avail
,
243 NOUVEAU_SCRATCH_SIZE
/ stride
);
245 if (max_index
- min_index
> vert_avail
||
246 (ib
&& ib
->count
> idx_avail
)) {
247 struct split_limits limits
= {
248 .max_verts
= vert_avail
,
249 .max_indices
= idx_avail
,
253 vbo_split_prims(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
254 max_index
, TAG(vbo_render_prims
), &limits
);
261 /* VBO rendering path. */
264 check_update_array(struct nouveau_array
*a
, unsigned offset
,
265 struct nouveau_bo
*bo
, int *pdelta
)
272 delta
= ((int)offset
- (int)a
->offset
) / a
->stride
;
274 dirty
= (delta
< 0 ||
275 offset
!= (a
->offset
+ delta
* a
->stride
));
280 *pdelta
= (dirty
? 0 : delta
);
285 vbo_bind_vertices(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
286 int base
, unsigned min_index
, unsigned max_index
, int *pdelta
)
288 struct nouveau_render_state
*render
= to_render_state(ctx
);
289 struct nouveau_channel
*chan
= context_chan(ctx
);
290 struct nouveau_bo
*bo
[NUM_VERTEX_ATTRS
];
291 unsigned offset
[NUM_VERTEX_ATTRS
];
292 GLboolean dirty
= GL_FALSE
;
298 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
299 const struct gl_client_array
*array
= arrays
[attr
];
300 struct gl_buffer_object
*obj
= array
->BufferObj
;
301 struct nouveau_array
*a
= &render
->attrs
[attr
];
302 unsigned delta
= (base
+ min_index
) * array
->StrideB
;
306 if (nouveau_bufferobj_hw(obj
)) {
307 /* Array in a buffer obj. */
308 nouveau_bo_ref(to_nouveau_bufferobj(obj
)->bo
, &bo
[i
]);
309 offset
[i
] = delta
+ (intptr_t)array
->Ptr
;
312 int n
= max_index
- min_index
+ 1;
313 char *sp
= (char *)ADD_POINTERS(
314 nouveau_bufferobj_sys(obj
), array
->Ptr
) + delta
;
315 char *dp
= nouveau_get_scratch(ctx
, n
* a
->stride
,
318 /* Array in client memory, move it to a
319 * scratch buffer obj. */
320 for (j
= 0; j
< n
; j
++)
321 memcpy(dp
+ j
* a
->stride
,
322 sp
+ j
* array
->StrideB
,
326 dirty
|= check_update_array(a
, offset
[i
], bo
[i
], pdelta
);
329 *pdelta
-= min_index
;
332 /* Buffers changed, update the attribute binding. */
333 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
334 struct nouveau_array
*a
= &render
->attrs
[attr
];
336 nouveau_bo_ref(NULL
, &a
->bo
);
337 a
->offset
= offset
[i
];
341 TAG(render_bind_vertices
)(ctx
);
345 FOR_EACH_BOUND_ATTR(render
, i
, attr
)
346 nouveau_bo_ref(NULL
, &bo
[i
]);
353 vbo_draw_vbo(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
354 const struct _mesa_prim
*prims
, GLuint nr_prims
,
355 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
358 struct nouveau_channel
*chan
= context_chan(ctx
);
359 dispatch_t dispatch
= get_array_dispatch(&to_render_state(ctx
)->ib
);
360 int i
, delta
= 0, basevertex
= 0;
363 TAG(render_set_format
)(ctx
);
365 for (i
= 0; i
< nr_prims
; i
++) {
366 unsigned start
= prims
[i
].start
,
367 count
= prims
[i
].count
;
369 if (i
== 0 || basevertex
!= prims
[i
].basevertex
) {
370 basevertex
= prims
[i
].basevertex
;
371 vbo_bind_vertices(ctx
, arrays
, basevertex
, min_index
,
375 if (count
> get_max_vertices(ctx
, ib
, AVAIL_RING(chan
)))
376 WAIT_RING(chan
, PUSHBUF_DWORDS
);
378 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
379 dispatch(ctx
, start
, delta
, count
);
384 /* Immediate rendering path. */
387 extract_id(struct nouveau_array
*a
, int i
, int j
)
393 vbo_draw_imm(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
394 const struct _mesa_prim
*prims
, GLuint nr_prims
,
395 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
398 struct nouveau_render_state
*render
= to_render_state(ctx
);
399 struct nouveau_channel
*chan
= context_chan(ctx
);
400 extract_u_t extract
= ib
? render
->ib
.extract_u
: extract_id
;
404 for (i
= 0; i
< nr_prims
; i
++) {
405 unsigned start
= prims
[i
].start
,
406 end
= start
+ prims
[i
].count
;
408 if (prims
[i
].count
> get_max_vertices(ctx
, ib
,
410 WAIT_RING(chan
, PUSHBUF_DWORDS
);
412 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
414 for (; start
< end
; start
++) {
415 j
= prims
[i
].basevertex
+
416 extract(&render
->ib
, 0, start
);
418 FOR_EACH_BOUND_ATTR(render
, k
, attr
)
419 EMIT_IMM(ctx
, &render
->attrs
[attr
], j
);
426 /* draw_prims entry point when we're doing hw-tnl. */
429 TAG(vbo_render_prims
)(struct gl_context
*ctx
,
430 const struct gl_client_array
**arrays
,
431 const struct _mesa_prim
*prims
, GLuint nr_prims
,
432 const struct _mesa_index_buffer
*ib
,
433 GLboolean index_bounds_valid
,
434 GLuint min_index
, GLuint max_index
)
436 struct nouveau_render_state
*render
= to_render_state(ctx
);
438 if (!index_bounds_valid
)
439 vbo_get_minmax_index(ctx
, prims
, ib
, &min_index
, &max_index
);
441 vbo_choose_render_mode(ctx
, arrays
);
442 vbo_choose_attrs(ctx
, arrays
);
444 if (vbo_maybe_split(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
448 vbo_init_arrays(ctx
, ib
, arrays
);
450 if (render
->mode
== VBO
)
451 vbo_draw_vbo(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
454 vbo_draw_imm(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
457 vbo_deinit_arrays(ctx
, ib
, arrays
);
460 /* VBO rendering entry points. */
463 TAG(vbo_check_render_prims
)(struct gl_context
*ctx
,
464 const struct gl_client_array
**arrays
,
465 const struct _mesa_prim
*prims
, GLuint nr_prims
,
466 const struct _mesa_index_buffer
*ib
,
467 GLboolean index_bounds_valid
,
468 GLuint min_index
, GLuint max_index
)
470 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
472 nouveau_validate_framebuffer(ctx
);
474 if (nctx
->fallback
== HWTNL
)
475 TAG(vbo_render_prims
)(ctx
, arrays
, prims
, nr_prims
, ib
,
476 index_bounds_valid
, min_index
, max_index
);
478 if (nctx
->fallback
== SWTNL
)
479 _tnl_vbo_draw_prims(ctx
, arrays
, prims
, nr_prims
, ib
,
480 index_bounds_valid
, min_index
, max_index
);
484 TAG(vbo_init
)(struct gl_context
*ctx
)
486 struct nouveau_render_state
*render
= to_render_state(ctx
);
489 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
492 vbo_set_draw_func(ctx
, TAG(vbo_check_render_prims
));
493 vbo_use_buffer_objects(ctx
);
497 TAG(vbo_destroy
)(struct gl_context
*ctx
)