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
35 #define PUSHBUF_DWORDS 2048
37 /* Functions to set up struct nouveau_array_state from something like
38 * a GL array or index buffer. */
41 vbo_init_array(struct nouveau_array_state
*a
, int attr
, int stride
,
42 int fields
, int type
, struct gl_buffer_object
*obj
,
43 const void *ptr
, GLboolean map
)
50 if (_mesa_is_bufferobj(obj
)) {
51 nouveau_bo_ref(to_nouveau_bufferobj(obj
)->bo
, &a
->bo
);
52 a
->offset
= (intptr_t)ptr
;
55 nouveau_bo_map(a
->bo
, NOUVEAU_BO_RD
);
56 a
->buf
= a
->bo
->map
+ a
->offset
;
62 nouveau_bo_ref(NULL
, &a
->bo
);
72 get_array_extract(a
, &a
->extract_u
, &a
->extract_f
);
76 vbo_deinit_array(struct nouveau_array_state
*a
)
80 nouveau_bo_unmap(a
->bo
);
81 nouveau_bo_ref(NULL
, &a
->bo
);
89 vbo_init_arrays(GLcontext
*ctx
, const struct _mesa_index_buffer
*ib
,
90 const struct gl_client_array
**arrays
)
92 struct nouveau_render_state
*render
= to_render_state(ctx
);
96 vbo_init_array(&render
->ib
, 0, 0, ib
->count
, ib
->type
,
97 ib
->obj
, ib
->ptr
, GL_TRUE
);
99 for (i
= 0; i
< render
->attr_count
; i
++) {
100 int attr
= render
->map
[i
];
103 const struct gl_client_array
*array
= arrays
[attr
];
106 if (render
->mode
== VBO
&&
107 !_mesa_is_bufferobj(array
->BufferObj
))
108 /* Pack client buffers. */
109 stride
= align(_mesa_sizeof_type(array
->Type
)
112 stride
= array
->StrideB
;
114 vbo_init_array(&render
->attrs
[attr
], attr
,
115 stride
, array
->Size
, array
->Type
,
116 array
->BufferObj
, array
->Ptr
,
117 render
->mode
== IMM
);
123 vbo_deinit_arrays(GLcontext
*ctx
, const struct _mesa_index_buffer
*ib
,
124 const struct gl_client_array
**arrays
)
126 struct nouveau_render_state
*render
= to_render_state(ctx
);
130 vbo_deinit_array(&render
->ib
);
132 for (i
= 0; i
< render
->attr_count
; i
++) {
133 int *attr
= &render
->map
[i
];
136 vbo_deinit_array(&render
->attrs
[*attr
]);
141 render
->attr_count
= 0;
144 /* Make some rendering decisions from the GL context. */
147 vbo_choose_render_mode(GLcontext
*ctx
, const struct gl_client_array
**arrays
)
149 struct nouveau_render_state
*render
= to_render_state(ctx
);
154 if (ctx
->Light
.Enabled
) {
155 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
156 if (arrays
[VERT_ATTRIB_GENERIC0
+ i
]->StrideB
) {
163 if (render
->mode
== VBO
)
164 render
->attr_count
= NUM_VERTEX_ATTRS
;
166 render
->attr_count
= 0;
170 vbo_emit_attr(GLcontext
*ctx
, const struct gl_client_array
**arrays
, int attr
)
172 struct nouveau_channel
*chan
= context_chan(ctx
);
173 struct nouveau_render_state
*render
= to_render_state(ctx
);
174 const struct gl_client_array
*array
= arrays
[attr
];
175 struct nouveau_array_state
*a
= &render
->attrs
[attr
];
178 if (!array
->StrideB
) {
179 if (attr
>= VERT_ATTRIB_GENERIC0
)
180 /* nouveau_update_state takes care of materials. */
183 /* Constant attribute. */
184 vbo_init_array(a
, attr
, array
->StrideB
, array
->Size
,
185 array
->Type
, array
->BufferObj
, array
->Ptr
,
191 /* Varying attribute. */
192 struct nouveau_attr_info
*info
= &TAG(vertex_attrs
)[attr
];
194 if (render
->mode
== VBO
) {
195 render
->map
[info
->vbo_index
] = attr
;
196 render
->vertex_size
+= array
->_ElementSize
;
198 render
->map
[render
->attr_count
++] = attr
;
199 render
->vertex_size
+= 4 * info
->imm_fields
;
204 #define MAT(a) (VERT_ATTRIB_GENERIC0 + MAT_ATTRIB_##a)
207 vbo_choose_attrs(GLcontext
*ctx
, const struct gl_client_array
**arrays
)
209 struct nouveau_render_state
*render
= to_render_state(ctx
);
212 /* Reset the vertex size. */
213 render
->vertex_size
= 0;
215 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_COLOR0
);
216 if (ctx
->Fog
.ColorSumEnabled
&& !ctx
->Light
.Enabled
)
217 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_COLOR1
);
219 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
220 if (ctx
->Texture
._EnabledCoordUnits
& (1 << i
))
221 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_TEX0
+ i
);
224 if (ctx
->Fog
.Enabled
&& ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
)
225 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_FOG
);
227 if (ctx
->Light
.Enabled
||
228 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_NORMALS
))
229 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_NORMAL
);
231 if (ctx
->Light
.Enabled
) {
232 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_AMBIENT
));
233 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_DIFFUSE
));
234 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_SPECULAR
));
235 vbo_emit_attr(ctx
, arrays
, MAT(FRONT_SHININESS
));
237 if (ctx
->Light
.Model
.TwoSide
) {
238 vbo_emit_attr(ctx
, arrays
, MAT(BACK_AMBIENT
));
239 vbo_emit_attr(ctx
, arrays
, MAT(BACK_DIFFUSE
));
240 vbo_emit_attr(ctx
, arrays
, MAT(BACK_SPECULAR
));
241 vbo_emit_attr(ctx
, arrays
, MAT(BACK_SHININESS
));
245 vbo_emit_attr(ctx
, arrays
, VERT_ATTRIB_POS
);
249 get_max_client_stride(GLcontext
*ctx
, const struct gl_client_array
**arrays
)
251 struct nouveau_render_state
*render
= to_render_state(ctx
);
254 for (i
= 0; i
< render
->attr_count
; i
++) {
255 int attr
= render
->map
[i
];
258 const struct gl_client_array
*a
= arrays
[attr
];
260 if (!_mesa_is_bufferobj(a
->BufferObj
))
261 s
= MAX2(a
->StrideB
, s
);
269 TAG(vbo_render_prims
)(GLcontext
*ctx
, const struct gl_client_array
**arrays
,
270 const struct _mesa_prim
*prims
, GLuint nr_prims
,
271 const struct _mesa_index_buffer
*ib
,
272 GLboolean index_bounds_valid
,
273 GLuint min_index
, GLuint max_index
);
276 vbo_maybe_split(GLcontext
*ctx
, const struct gl_client_array
**arrays
,
277 const struct _mesa_prim
*prims
, GLuint nr_prims
,
278 const struct _mesa_index_buffer
*ib
,
279 GLuint min_index
, GLuint max_index
)
281 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
282 struct nouveau_render_state
*render
= to_render_state(ctx
);
283 unsigned pushbuf_avail
= PUSHBUF_DWORDS
- 2 * (nctx
->bo
.count
+
285 vert_avail
= get_max_vertices(ctx
, NULL
, pushbuf_avail
),
286 idx_avail
= get_max_vertices(ctx
, ib
, pushbuf_avail
);
289 /* Try to keep client buffers smaller than the scratch BOs. */
290 if (render
->mode
== VBO
&&
291 (stride
= get_max_client_stride(ctx
, arrays
)))
292 vert_avail
= MIN2(vert_avail
,
293 RENDER_SCRATCH_SIZE
/ stride
);
295 if (max_index
- min_index
> vert_avail
||
296 (ib
&& ib
->count
> idx_avail
)) {
297 struct split_limits limits
= {
298 .max_verts
= vert_avail
,
299 .max_indices
= idx_avail
,
303 vbo_split_prims(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
304 max_index
, TAG(vbo_render_prims
), &limits
);
311 /* VBO rendering path. */
314 vbo_bind_vertices(GLcontext
*ctx
, const struct gl_client_array
**arrays
,
315 GLint basevertex
, GLuint min_index
, GLuint max_index
)
317 struct nouveau_render_state
*render
= to_render_state(ctx
);
320 for (i
= 0; i
< NUM_VERTEX_ATTRS
; i
++) {
321 int attr
= render
->map
[i
];
324 const struct gl_client_array
*array
= arrays
[attr
];
325 struct nouveau_array_state
*a
= &render
->attrs
[attr
];
326 unsigned delta
= (basevertex
+ min_index
)
330 a
->offset
= (intptr_t)array
->Ptr
+ delta
;
332 int j
, n
= max_index
- min_index
+ 1;
333 char *sp
= (char *)array
->Ptr
+ delta
;
334 char *dp
= get_scratch_vbo(ctx
, n
* a
->stride
,
337 for (j
= 0; j
< n
; j
++)
338 memcpy(dp
+ j
* a
->stride
,
339 sp
+ j
* array
->StrideB
,
345 TAG(render_bind_vertices
)(ctx
);
349 vbo_draw_vbo(GLcontext
*ctx
, const struct gl_client_array
**arrays
,
350 const struct _mesa_prim
*prims
, GLuint nr_prims
,
351 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
354 struct nouveau_channel
*chan
= context_chan(ctx
);
356 int delta
= -min_index
, basevertex
= 0, i
;
359 get_array_dispatch(&to_render_state(ctx
)->ib
, &dispatch
);
361 TAG(render_set_format
)(ctx
);
363 for (i
= 0; i
< nr_prims
; i
++) {
364 unsigned start
= prims
[i
].start
,
365 count
= prims
[i
].count
;
367 if (i
== 0 || basevertex
!= prims
[i
].basevertex
) {
368 basevertex
= prims
[i
].basevertex
;
369 vbo_bind_vertices(ctx
, arrays
, basevertex
,
370 min_index
, max_index
);
373 if (count
> get_max_vertices(ctx
, ib
, AVAIL_RING(chan
)))
374 WAIT_RING(chan
, PUSHBUF_DWORDS
);
376 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
377 dispatch(ctx
, start
, delta
, count
);
382 /* Immediate rendering path. */
385 extract_id(struct nouveau_array_state
*a
, int i
, int j
)
391 vbo_draw_imm(GLcontext
*ctx
, const struct gl_client_array
**arrays
,
392 const struct _mesa_prim
*prims
, GLuint nr_prims
,
393 const struct _mesa_index_buffer
*ib
, GLuint min_index
,
396 struct nouveau_render_state
*render
= to_render_state(ctx
);
397 struct nouveau_channel
*chan
= context_chan(ctx
);
398 extract_u_t extract
= ib
? render
->ib
.extract_u
: extract_id
;
402 for (i
= 0; i
< nr_prims
; i
++) {
403 unsigned start
= prims
[i
].start
,
404 end
= start
+ prims
[i
].count
;
406 if (prims
[i
].count
> get_max_vertices(ctx
, ib
,
408 WAIT_RING(chan
, PUSHBUF_DWORDS
);
410 BATCH_BEGIN(nvgl_primitive(prims
[i
].mode
));
412 for (; start
< end
; start
++) {
413 j
= prims
[i
].basevertex
+
414 extract(&render
->ib
, 0, start
);
416 for (k
= 0; k
< render
->attr_count
; k
++)
417 EMIT_IMM(ctx
, &render
->attrs
[render
->map
[k
]],
425 /* draw_prims entry point when we're doing hw-tnl. */
428 TAG(vbo_render_prims
)(GLcontext
*ctx
, const struct gl_client_array
**arrays
,
429 const struct _mesa_prim
*prims
, GLuint nr_prims
,
430 const struct _mesa_index_buffer
*ib
,
431 GLboolean index_bounds_valid
,
432 GLuint min_index
, GLuint max_index
)
434 struct nouveau_render_state
*render
= to_render_state(ctx
);
436 if (!index_bounds_valid
)
437 vbo_get_minmax_index(ctx
, prims
, ib
, &min_index
, &max_index
);
439 vbo_choose_render_mode(ctx
, arrays
);
440 vbo_choose_attrs(ctx
, arrays
);
442 if (vbo_maybe_split(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
446 vbo_init_arrays(ctx
, ib
, arrays
);
448 if (render
->mode
== VBO
)
449 vbo_draw_vbo(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
452 vbo_draw_imm(ctx
, arrays
, prims
, nr_prims
, ib
, min_index
,
455 vbo_deinit_arrays(ctx
, ib
, arrays
);