2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keithw@vmware.com>
28 #include "main/mtypes.h"
29 #include "main/bufferobj.h"
30 #include "math/m_eval.h"
31 #include "main/vtxfmt.h"
32 #include "main/api_arrayelt.h"
34 #include "vbo_private.h"
38 check_size(const GLfloat
*attr
)
51 * Helper for initializing a vertex array.
54 init_array(struct gl_context
*ctx
, struct gl_vertex_array
*cl
,
55 unsigned size
, const void *pointer
)
57 memset(cl
, 0, sizeof(*cl
));
63 cl
->_ElementSize
= cl
->Size
* sizeof(GLfloat
);
66 _mesa_reference_buffer_object(ctx
, &cl
->BufferObj
,
67 ctx
->Shared
->NullBufferObj
);
72 * Set up the vbo->currval arrays to point at the context's current
73 * vertex attributes (with strides = 0).
76 init_legacy_currval(struct gl_context
*ctx
)
78 struct vbo_context
*vbo
= vbo_context(ctx
);
81 /* Set up a constant (StrideB == 0) array for each current
84 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
85 struct gl_vertex_array
*cl
= &vbo
->currval
[VERT_ATTRIB_FF(i
)];
88 check_size(ctx
->Current
.Attrib
[i
]),
89 ctx
->Current
.Attrib
[i
]);
95 init_generic_currval(struct gl_context
*ctx
)
97 struct vbo_context
*vbo
= vbo_context(ctx
);
100 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
101 struct gl_vertex_array
*cl
= &vbo
->currval
[VBO_ATTRIB_GENERIC0
+ i
];
103 init_array(ctx
, cl
, 1, ctx
->Current
.Attrib
[VERT_ATTRIB_GENERIC0
+ i
]);
109 init_mat_currval(struct gl_context
*ctx
)
111 struct vbo_context
*vbo
= vbo_context(ctx
);
114 /* Set up a constant (StrideB == 0) array for each current
117 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
118 struct gl_vertex_array
*cl
=
119 &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
];
122 /* Size is fixed for the material attributes, for others will
123 * be determined at runtime:
126 case MAT_ATTRIB_FRONT_SHININESS
:
127 case MAT_ATTRIB_BACK_SHININESS
:
130 case MAT_ATTRIB_FRONT_INDEXES
:
131 case MAT_ATTRIB_BACK_INDEXES
:
139 init_array(ctx
, cl
, size
, ctx
->Light
.Material
.Attrib
[i
]);
144 vbo_draw_indirect_prims(struct gl_context
*ctx
,
146 struct gl_buffer_object
*indirect_data
,
147 GLsizeiptr indirect_offset
,
150 struct gl_buffer_object
*indirect_params
,
151 GLsizeiptr indirect_params_offset
,
152 const struct _mesa_index_buffer
*ib
)
154 struct vbo_context
*vbo
= vbo_context(ctx
);
155 struct _mesa_prim
*prim
;
158 prim
= calloc(draw_count
, sizeof(*prim
));
160 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "gl%sDraw%sIndirect%s",
161 (draw_count
> 1) ? "Multi" : "",
162 ib
? "Elements" : "Arrays",
163 indirect_params
? "CountARB" : "");
168 prim
[draw_count
- 1].end
= 1;
169 for (i
= 0; i
< draw_count
; ++i
, indirect_offset
+= stride
) {
171 prim
[i
].indexed
= !!ib
;
172 prim
[i
].indirect_offset
= indirect_offset
;
173 prim
[i
].is_indirect
= 1;
177 vbo
->draw_prims(ctx
, prim
, draw_count
,
180 ctx
->DrawIndirectBuffer
);
187 _vbo_install_exec_vtxfmt(struct gl_context
*ctx
)
189 struct vbo_context
*vbo
= vbo_context(ctx
);
191 _mesa_install_exec_vtxfmt(ctx
, &vbo
->exec
.vtxfmt
);
196 vbo_exec_invalidate_state(struct gl_context
*ctx
)
198 struct vbo_context
*vbo
= vbo_context(ctx
);
199 struct vbo_exec_context
*exec
= &vbo
->exec
;
201 if (ctx
->NewState
& (_NEW_PROGRAM
| _NEW_ARRAY
)) {
202 if (!exec
->validating
)
203 exec
->array
.recalculate_inputs
= GL_TRUE
;
205 _ae_invalidate_state(ctx
);
208 if (ctx
->NewState
& _NEW_EVAL
)
209 exec
->eval
.recalculate_maps
= GL_TRUE
;
214 _vbo_CreateContext(struct gl_context
*ctx
)
216 struct vbo_context
*vbo
= CALLOC_STRUCT(vbo_context
);
218 ctx
->vbo_context
= vbo
;
220 /* Initialize the arrayelt helper
222 if (!ctx
->aelt_context
&&
223 !_ae_create_context(ctx
)) {
227 init_legacy_currval(ctx
);
228 init_generic_currval(ctx
);
229 init_mat_currval(ctx
);
230 vbo_set_indirect_draw_func(ctx
, vbo_draw_indirect_prims
);
232 /* Build mappings from VERT_ATTRIB -> VBO_ATTRIB depending on type
233 * of vertex program active.
238 /* make sure all VBO_ATTRIB_ values can fit in an unsigned byte */
239 STATIC_ASSERT(VBO_ATTRIB_MAX
<= 255);
241 /* identity mapping */
242 for (i
= 0; i
< ARRAY_SIZE(vbo
->map_vp_none
); i
++)
243 vbo
->map_vp_none
[i
] = i
;
244 /* map material attribs to generic slots */
245 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++)
246 vbo
->map_vp_none
[VERT_ATTRIB_GENERIC(i
)]
247 = VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
;
249 for (i
= 0; i
< ARRAY_SIZE(vbo
->map_vp_arb
); i
++)
250 vbo
->map_vp_arb
[i
] = i
;
254 /* Hook our functions into exec and compile dispatch tables. These
255 * will pretty much be permanently installed, which means that the
256 * vtxfmt mechanism can be removed now.
259 if (ctx
->API
== API_OPENGL_COMPAT
)
269 _vbo_DestroyContext(struct gl_context
*ctx
)
271 struct vbo_context
*vbo
= vbo_context(ctx
);
273 if (ctx
->aelt_context
) {
274 _ae_destroy_context(ctx
);
275 ctx
->aelt_context
= NULL
;
281 for (i
= 0; i
< VBO_ATTRIB_MAX
; i
++) {
282 _mesa_reference_buffer_object(ctx
, &vbo
->currval
[i
].BufferObj
, NULL
);
285 vbo_exec_destroy(ctx
);
286 if (ctx
->API
== API_OPENGL_COMPAT
)
287 vbo_save_destroy(ctx
);
289 ctx
->vbo_context
= NULL
;
295 vbo_set_draw_func(struct gl_context
*ctx
, vbo_draw_func func
)
297 struct vbo_context
*vbo
= vbo_context(ctx
);
298 vbo
->draw_prims
= func
;
303 vbo_set_indirect_draw_func(struct gl_context
*ctx
,
304 vbo_indirect_draw_func func
)
306 struct vbo_context
*vbo
= vbo_context(ctx
);
307 vbo
->draw_indirect_prims
= func
;