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
,
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 nouveau_deinit_array(a
);
92 render
->attr_count
= 0;
93 context_bctx(ctx
, VERTEX
);
96 /* Make some rendering decisions from the GL context. */
99 vbo_choose_render_mode(struct gl_context
*ctx
, const struct gl_client_array
**arrays
)
101 struct nouveau_render_state
*render
= to_render_state(ctx
);
106 if (ctx
->Light
.Enabled
) {
107 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
108 if (arrays
[VERT_ATTRIB_GENERIC0
+ i
]->StrideB
) {
117 vbo_emit_attr(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
120 struct nouveau_channel
*chan
= context_chan(ctx
);
121 struct nouveau_render_state
*render
= to_render_state(ctx
);
122 const struct gl_client_array
*array
= arrays
[attr
];
123 struct nouveau_array
*a
= &render
->attrs
[attr
];
126 if (!array
->StrideB
) {
127 if (attr
>= VERT_ATTRIB_GENERIC0
)
128 /* nouveau_update_state takes care of materials. */
131 /* Constant attribute. */
132 nouveau_init_array(a
, attr
, array
->StrideB
, array
->Size
,
133 array
->Type
, array
->BufferObj
, array
->Ptr
,
136 nouveau_deinit_array(a
);
139 /* Varying attribute. */
140 struct nouveau_attr_info
*info
= &TAG(vertex_attrs
)[attr
];
142 if (render
->mode
== VBO
) {
143 render
->map
[info
->vbo_index
] = attr
;
144 render
->vertex_size
+= array
->_ElementSize
;
145 render
->attr_count
= MAX2(render
->attr_count
,
146 info
->vbo_index
+ 1);
148 render
->map
[render
->attr_count
++] = attr
;
149 render
->vertex_size
+= 4 * info
->imm_fields
;
155 #define MAT(a) (VERT_ATTRIB_GENERIC0 + MAT_ATTRIB_##a)
158 vbo_choose_attrs(struct gl_context
*ctx
, const struct gl_client_array
**arrays
)
160 struct nouveau_render_state
*render
= to_render_state(ctx
);
163 /* Reset the vertex size. */
164 render
->vertex_size
= 0;
165 render
->attr_count
= 0;
167 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_COLOR0
);
168 if (ctx
->Fog
.ColorSumEnabled
&& !ctx
->Light
.Enabled
)
169 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_COLOR1
);
171 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
172 if (ctx
->Texture
._EnabledCoordUnits
& (1 << i
))
173 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_TEX0
+ i
);
176 if (ctx
->Fog
.Enabled
&& ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
)
177 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_FOG
);
179 if (ctx
->Light
.Enabled
||
180 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_NORMALS
))
181 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_NORMAL
);
183 if (ctx
->Light
.Enabled
) {
184 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_AMBIENT
));
185 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_DIFFUSE
));
186 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_SPECULAR
));
187 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_SHININESS
));
189 if (ctx
->Light
.Model
.TwoSide
) {
190 vbo_emit_attr(ctx
, arrays
, MAT(BACK_AMBIENT
));
191 vbo_emit_attr(ctx
, arrays
, MAT(BACK_DIFFUSE
));
192 vbo_emit_attr(ctx
, arrays
, MAT(BACK_SPECULAR
));
193 vbo_emit_attr(ctx
, arrays
, MAT(BACK_SHININESS
));
197 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_POS
);
201 get_max_client_stride(struct gl_context
*ctx
, const struct gl_client_array
**arrays
)
203 struct nouveau_render_state
*render
= to_render_state(ctx
);
206 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
207 const struct gl_client_array
*a
= arrays
[attr
];
209 if (!_mesa_is_bufferobj(a
->BufferObj
))
210 s
= MAX2(s
, get_array_stride(ctx
, a
));
217 TAG(vbo_render_prims
)(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
218 const struct _mesa_prim
*prims
, GLuint nr_prims
,
219 const struct _mesa_index_buffer
*ib
,
220 GLboolean index_bounds_valid
,
221 GLuint min_index
, GLuint max_index
);
224 vbo_maybe_split(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
225 const struct _mesa_prim
*prims
, GLuint nr_prims
,
226 const struct _mesa_index_buffer
*ib
,
227 GLuint min_index
, GLuint max_index
)
229 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
230 struct nouveau_render_state
*render
= to_render_state(ctx
);
231 unsigned pushbuf_avail
= PUSHBUF_DWORDS
- 2 * (nctx
->bo
.count
+
233 vert_avail
= get_max_vertices(ctx
, NULL
, pushbuf_avail
),
234 idx_avail
= get_max_vertices(ctx
, ib
, pushbuf_avail
);
237 /* Try to keep client buffers smaller than the scratch BOs. */
238 if (render
->mode
== VBO
&&
239 (stride
= get_max_client_stride(ctx
, arrays
)))
240 vert_avail
= MIN2(vert_avail
,
241 NOUVEAU_SCRATCH_SIZE
/ stride
);
243 if (max_index
- min_index
> vert_avail
||
244 (ib
&& ib
->count
> idx_avail
)) {
245 struct split_limits limits
= {
246 .max_verts
= vert_avail
,
247 .max_indices
= idx_avail
,
251 vbo_split_prims(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
252 max_index
, TAG(vbo_render_prims
), &limits
);
259 /* VBO rendering path. */
262 vbo_bind_vertices(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
263 GLint basevertex
, GLuint min_index
, GLuint max_index
)
265 struct nouveau_render_state
*render
= to_render_state(ctx
);
268 FOR_EACH_BOUND_ATTR(render
, i
, attr
) {
269 const struct gl_client_array
*array
= arrays
[attr
];
270 struct nouveau_array
*a
= &render
->attrs
[attr
];
271 unsigned delta
= (basevertex
+ min_index
)
275 /* Array in a buffer obj. */
276 a
->offset
= (intptr_t)array
->Ptr
+ delta
;
278 int j
, n
= max_index
- min_index
+ 1;
279 char *sp
= (char *)array
->Ptr
+ delta
;
280 char *dp
= nouveau_get_scratch(
281 ctx
, n
* a
->stride
, &a
->bo
, &a
->offset
);
283 /* Array in client memory, move it to
284 * a scratch buffer obj. */
285 for (j
= 0; j
< n
; j
++)
286 memcpy(dp
+ j
* a
->stride
,
287 sp
+ j
* array
->StrideB
,
292 TAG(render_bind_vertices
)(ctx
);
296 vbo_draw_vbo(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
297 const struct _mesa_prim
*prims
, GLuint nr_prims
,
298 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
301 struct nouveau_channel
*chan
= context_chan(ctx
);
302 dispatch_t dispatch
= get_array_dispatch(&to_render_state(ctx
)->ib
);
303 int i
, delta
= -min_index
, basevertex
= 0;
306 TAG(render_set_format
)(ctx
);
308 for (i
= 0; i
< nr_prims
; i
++) {
309 unsigned start
= prims
[i
].start
,
310 count
= prims
[i
].count
;
312 if (i
== 0 || basevertex
!= prims
[i
].basevertex
) {
313 basevertex
= prims
[i
].basevertex
;
314 vbo_bind_vertices(ctx
, arrays
, basevertex
,
315 min_index
, max_index
);
318 if (count
> get_max_vertices(ctx
, ib
, AVAIL_RING(chan
)))
319 WAIT_RING(chan
, PUSHBUF_DWORDS
);
321 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
322 dispatch(ctx
, start
, delta
, count
);
327 /* Immediate rendering path. */
330 extract_id(struct nouveau_array
*a
, int i
, int j
)
336 vbo_draw_imm(struct gl_context
*ctx
, const struct gl_client_array
**arrays
,
337 const struct _mesa_prim
*prims
, GLuint nr_prims
,
338 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
341 struct nouveau_render_state
*render
= to_render_state(ctx
);
342 struct nouveau_channel
*chan
= context_chan(ctx
);
343 extract_u_t extract
= ib
? render
->ib
.extract_u
: extract_id
;
347 for (i
= 0; i
< nr_prims
; i
++) {
348 unsigned start
= prims
[i
].start
,
349 end
= start
+ prims
[i
].count
;
351 if (prims
[i
].count
> get_max_vertices(ctx
, ib
,
353 WAIT_RING(chan
, PUSHBUF_DWORDS
);
355 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
357 for (; start
< end
; start
++) {
358 j
= prims
[i
].basevertex
+
359 extract(&render
->ib
, 0, start
);
361 FOR_EACH_BOUND_ATTR(render
, k
, attr
)
362 EMIT_IMM(ctx
, &render
->attrs
[attr
], j
);
369 /* draw_prims entry point when we're doing hw-tnl. */
372 TAG(vbo_render_prims
)(struct gl_context
*ctx
,
373 const struct gl_client_array
**arrays
,
374 const struct _mesa_prim
*prims
, GLuint nr_prims
,
375 const struct _mesa_index_buffer
*ib
,
376 GLboolean index_bounds_valid
,
377 GLuint min_index
, GLuint max_index
)
379 struct nouveau_render_state
*render
= to_render_state(ctx
);
381 if (!index_bounds_valid
)
382 vbo_get_minmax_index(ctx
, prims
, ib
, &min_index
, &max_index
);
384 vbo_choose_render_mode(ctx
, arrays
);
385 vbo_choose_attrs(ctx
, arrays
);
387 if (vbo_maybe_split(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
391 vbo_init_arrays(ctx
, ib
, arrays
);
393 if (render
->mode
== VBO
)
394 vbo_draw_vbo(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
397 vbo_draw_imm(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
400 vbo_deinit_arrays(ctx
, ib
, arrays
);