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"
33 #include "main/arrayobj.h"
34 #include "main/varray.h"
36 #include "vbo_private.h"
40 check_size(const GLfloat
*attr
)
53 * Helper for initializing a vertex array.
56 init_array(struct gl_context
*ctx
, struct gl_vertex_array
*array
,
57 unsigned size
, const void *pointer
)
59 memset(array
, 0, sizeof(*array
));
62 array
->Type
= GL_FLOAT
;
63 array
->Format
= GL_RGBA
;
65 array
->_ElementSize
= array
->Size
* sizeof(GLfloat
);
68 _mesa_reference_buffer_object(ctx
, &array
->BufferObj
,
69 ctx
->Shared
->NullBufferObj
);
74 * Set up the vbo->currval arrays to point at the context's current
75 * vertex attributes (with strides = 0).
78 init_legacy_currval(struct gl_context
*ctx
)
80 struct vbo_context
*vbo
= vbo_context(ctx
);
83 /* Set up a constant (StrideB == 0) array for each current
86 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
87 struct gl_vertex_array
*array
= &vbo
->currval
[VERT_ATTRIB_FF(i
)];
89 init_array(ctx
, array
,
90 check_size(ctx
->Current
.Attrib
[i
]),
91 ctx
->Current
.Attrib
[i
]);
97 init_generic_currval(struct gl_context
*ctx
)
99 struct vbo_context
*vbo
= vbo_context(ctx
);
102 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
103 struct gl_vertex_array
*array
= &vbo
->currval
[VBO_ATTRIB_GENERIC0
+ i
];
105 init_array(ctx
, array
, 1, ctx
->Current
.Attrib
[VERT_ATTRIB_GENERIC0
+ i
]);
111 init_mat_currval(struct gl_context
*ctx
)
113 struct vbo_context
*vbo
= vbo_context(ctx
);
116 /* Set up a constant (StrideB == 0) array for each current
119 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
120 struct gl_vertex_array
*array
=
121 &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
];
124 /* Size is fixed for the material attributes, for others will
125 * be determined at runtime:
128 case MAT_ATTRIB_FRONT_SHININESS
:
129 case MAT_ATTRIB_BACK_SHININESS
:
132 case MAT_ATTRIB_FRONT_INDEXES
:
133 case MAT_ATTRIB_BACK_INDEXES
:
141 init_array(ctx
, array
, size
, ctx
->Light
.Material
.Attrib
[i
]);
147 * Fallback for when a driver does not call vbo_set_indirect_draw_func().
150 vbo_draw_indirect_prims(struct gl_context
*ctx
,
152 struct gl_buffer_object
*indirect_buffer
,
153 GLsizeiptr indirect_offset
,
156 struct gl_buffer_object
*indirect_draw_count_buffer
,
157 GLsizeiptr indirect_draw_count_offset
,
158 const struct _mesa_index_buffer
*ib
)
160 struct vbo_context
*vbo
= vbo_context(ctx
);
161 struct _mesa_prim
*prim
;
164 prim
= calloc(draw_count
, sizeof(*prim
));
166 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "gl%sDraw%sIndirect%s",
167 (draw_count
> 1) ? "Multi" : "",
168 ib
? "Elements" : "Arrays",
169 indirect_buffer
? "CountARB" : "");
174 prim
[draw_count
- 1].end
= 1;
175 for (i
= 0; i
< draw_count
; ++i
, indirect_offset
+= stride
) {
177 prim
[i
].indexed
= !!ib
;
178 prim
[i
].indirect_offset
= indirect_offset
;
179 prim
[i
].is_indirect
= 1;
183 /* This should always be true at this time */
184 assert(indirect_buffer
== ctx
->DrawIndirectBuffer
);
186 vbo
->draw_prims(ctx
, prim
, draw_count
,
196 _vbo_install_exec_vtxfmt(struct gl_context
*ctx
)
198 struct vbo_context
*vbo
= vbo_context(ctx
);
200 _mesa_install_exec_vtxfmt(ctx
, &vbo
->exec
.vtxfmt
);
205 vbo_exec_invalidate_state(struct gl_context
*ctx
)
207 struct vbo_context
*vbo
= vbo_context(ctx
);
208 struct vbo_exec_context
*exec
= &vbo
->exec
;
210 if (ctx
->NewState
& (_NEW_PROGRAM
| _NEW_ARRAY
)) {
211 exec
->array
.recalculate_inputs
= GL_TRUE
;
213 _ae_invalidate_state(ctx
);
215 /* If _mesa_update_state is called in a non draw code path,
216 * changes in the VAO need to be captured.
218 if (ctx
->Array
.VAO
->NewArrays
)
219 exec
->array
.recalculate_inputs
= GL_TRUE
;
221 if (ctx
->NewState
& _NEW_EVAL
)
222 exec
->eval
.recalculate_maps
= GL_TRUE
;
227 _vbo_CreateContext(struct gl_context
*ctx
)
229 struct vbo_context
*vbo
= CALLOC_STRUCT(vbo_context
);
231 ctx
->vbo_context
= vbo
;
233 /* Initialize the arrayelt helper
235 if (!ctx
->aelt_context
&&
236 !_ae_create_context(ctx
)) {
240 init_legacy_currval(ctx
);
241 init_generic_currval(ctx
);
242 init_mat_currval(ctx
);
243 _vbo_init_inputs(&vbo
->draw_arrays
);
244 vbo_set_indirect_draw_func(ctx
, vbo_draw_indirect_prims
);
246 /* make sure all VBO_ATTRIB_ values can fit in an unsigned byte */
247 STATIC_ASSERT(VBO_ATTRIB_MAX
<= 255);
249 /* Hook our functions into exec and compile dispatch tables. These
250 * will pretty much be permanently installed, which means that the
251 * vtxfmt mechanism can be removed now.
254 if (ctx
->API
== API_OPENGL_COMPAT
)
257 vbo
->VAO
= _mesa_new_vao(ctx
, ~((GLuint
)0));
258 /* The exec VAO assumes to have all arributes bound to binding 0 */
259 for (unsigned i
= 0; i
< VERT_ATTRIB_MAX
; ++i
)
260 _mesa_vertex_attrib_binding(ctx
, vbo
->VAO
, i
, 0, false);
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
);
288 _mesa_reference_vao(ctx
, &vbo
->VAO
, NULL
);
290 ctx
->vbo_context
= NULL
;
296 vbo_set_draw_func(struct gl_context
*ctx
, vbo_draw_func func
)
298 struct vbo_context
*vbo
= vbo_context(ctx
);
299 vbo
->draw_prims
= func
;
304 vbo_set_indirect_draw_func(struct gl_context
*ctx
,
305 vbo_indirect_draw_func func
)
307 struct vbo_context
*vbo
= vbo_context(ctx
);
308 vbo
->draw_indirect_prims
= func
;