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_array_attributes
*attrib
,
57 unsigned size
, const void *pointer
)
59 memset(attrib
, 0, sizeof(*attrib
));
62 attrib
->Type
= GL_FLOAT
;
63 attrib
->Format
= GL_RGBA
;
65 attrib
->_ElementSize
= size
* sizeof(GLfloat
);
66 attrib
->Ptr
= pointer
;
71 * Set up the vbo->currval arrays to point at the context's current
72 * vertex attributes (with strides = 0).
75 init_legacy_currval(struct gl_context
*ctx
)
77 struct vbo_context
*vbo
= vbo_context(ctx
);
80 /* Set up a constant (Stride == 0) array for each current
83 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
84 const unsigned attr
= VERT_ATTRIB_FF(i
);
85 struct gl_array_attributes
*attrib
= &vbo
->current
[attr
];
87 init_array(ctx
, attrib
, check_size(ctx
->Current
.Attrib
[attr
]),
88 ctx
->Current
.Attrib
[attr
]);
94 init_generic_currval(struct gl_context
*ctx
)
96 struct vbo_context
*vbo
= vbo_context(ctx
);
99 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
100 const unsigned attr
= VBO_ATTRIB_GENERIC0
+ i
;
101 struct gl_array_attributes
*attrib
= &vbo
->current
[attr
];
103 init_array(ctx
, attrib
, 1, ctx
->Current
.Attrib
[attr
]);
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 const unsigned attr
= VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
;
119 struct gl_array_attributes
*attrib
= &vbo
->current
[attr
];
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
, attrib
, size
, ctx
->Light
.Material
.Attrib
[i
]);
145 * Fallback for when a driver does not call vbo_set_indirect_draw_func().
148 vbo_draw_indirect_prims(struct gl_context
*ctx
,
150 struct gl_buffer_object
*indirect_buffer
,
151 GLsizeiptr indirect_offset
,
154 struct gl_buffer_object
*indirect_draw_count_buffer
,
155 GLsizeiptr indirect_draw_count_offset
,
156 const struct _mesa_index_buffer
*ib
)
158 struct vbo_context
*vbo
= vbo_context(ctx
);
159 struct _mesa_prim
*prim
;
162 prim
= calloc(draw_count
, sizeof(*prim
));
164 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "gl%sDraw%sIndirect%s",
165 (draw_count
> 1) ? "Multi" : "",
166 ib
? "Elements" : "Arrays",
167 indirect_buffer
? "CountARB" : "");
172 prim
[draw_count
- 1].end
= 1;
173 for (i
= 0; i
< draw_count
; ++i
, indirect_offset
+= stride
) {
175 prim
[i
].indexed
= !!ib
;
176 prim
[i
].indirect_offset
= indirect_offset
;
177 prim
[i
].is_indirect
= 1;
181 /* This should always be true at this time */
182 assert(indirect_buffer
== ctx
->DrawIndirectBuffer
);
184 vbo
->draw_prims(ctx
, prim
, draw_count
,
194 _vbo_install_exec_vtxfmt(struct gl_context
*ctx
)
196 struct vbo_context
*vbo
= vbo_context(ctx
);
198 _mesa_install_exec_vtxfmt(ctx
, &vbo
->exec
.vtxfmt
);
203 vbo_exec_invalidate_state(struct gl_context
*ctx
)
205 struct vbo_context
*vbo
= vbo_context(ctx
);
206 struct vbo_exec_context
*exec
= &vbo
->exec
;
208 if (ctx
->NewState
& (_NEW_PROGRAM
| _NEW_ARRAY
)) {
209 _ae_invalidate_state(ctx
);
211 if (ctx
->NewState
& _NEW_EVAL
)
212 exec
->eval
.recalculate_maps
= GL_TRUE
;
217 _vbo_CreateContext(struct gl_context
*ctx
)
219 struct vbo_context
*vbo
= CALLOC_STRUCT(vbo_context
);
221 ctx
->vbo_context
= vbo
;
223 /* Initialize the arrayelt helper
225 if (!ctx
->aelt_context
&&
226 !_ae_create_context(ctx
)) {
230 vbo
->binding
.Offset
= 0;
231 vbo
->binding
.Stride
= 0;
232 vbo
->binding
.InstanceDivisor
= 0;
233 _mesa_reference_buffer_object(ctx
, &vbo
->binding
.BufferObj
,
234 ctx
->Shared
->NullBufferObj
);
235 init_legacy_currval(ctx
);
236 init_generic_currval(ctx
);
237 init_mat_currval(ctx
);
238 _vbo_init_inputs(&vbo
->draw_arrays
);
239 vbo_set_indirect_draw_func(ctx
, vbo_draw_indirect_prims
);
241 /* make sure all VBO_ATTRIB_ values can fit in an unsigned byte */
242 STATIC_ASSERT(VBO_ATTRIB_MAX
<= 255);
244 /* Hook our functions into exec and compile dispatch tables. These
245 * will pretty much be permanently installed, which means that the
246 * vtxfmt mechanism can be removed now.
249 if (ctx
->API
== API_OPENGL_COMPAT
)
252 vbo
->VAO
= _mesa_new_vao(ctx
, ~((GLuint
)0));
253 /* The exec VAO assumes to have all arributes bound to binding 0 */
254 for (unsigned i
= 0; i
< VERT_ATTRIB_MAX
; ++i
)
255 _mesa_vertex_attrib_binding(ctx
, vbo
->VAO
, i
, 0, false);
264 _vbo_DestroyContext(struct gl_context
*ctx
)
266 struct vbo_context
*vbo
= vbo_context(ctx
);
268 if (ctx
->aelt_context
) {
269 _ae_destroy_context(ctx
);
270 ctx
->aelt_context
= NULL
;
275 _mesa_reference_buffer_object(ctx
, &vbo
->binding
.BufferObj
, NULL
);
277 vbo_exec_destroy(ctx
);
278 if (ctx
->API
== API_OPENGL_COMPAT
)
279 vbo_save_destroy(ctx
);
280 _mesa_reference_vao(ctx
, &vbo
->VAO
, NULL
);
282 ctx
->vbo_context
= NULL
;
288 vbo_set_draw_func(struct gl_context
*ctx
, vbo_draw_func func
)
290 struct vbo_context
*vbo
= vbo_context(ctx
);
291 vbo
->draw_prims
= func
;
296 vbo_set_indirect_draw_func(struct gl_context
*ctx
,
297 vbo_indirect_draw_func func
)
299 struct vbo_context
*vbo
= vbo_context(ctx
);
300 vbo
->draw_indirect_prims
= func
;
305 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
306 * These will point to the arrays to actually use for drawing. Some will
307 * be user-provided arrays, other will be zero-stride const-valued arrays.
310 vbo_bind_arrays(struct gl_context
*ctx
)
312 struct vbo_context
*vbo
= vbo_context(ctx
);
313 struct vbo_exec_context
*exec
= &vbo
->exec
;
315 _mesa_set_drawing_arrays(ctx
, vbo
->draw_arrays
.inputs
);
317 if (exec
->array
.recalculate_inputs
) {
318 /* Finally update the inputs array */
319 _vbo_update_inputs(ctx
, &vbo
->draw_arrays
);
320 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
321 exec
->array
.recalculate_inputs
= GL_FALSE
;
324 assert(ctx
->NewState
== 0);
325 assert(ctx
->Array
._DrawVAO
->NewArrays
== 0);
330 _vbo_draw(struct gl_context
*ctx
, const struct _mesa_prim
*prims
,
331 GLuint nr_prims
, const struct _mesa_index_buffer
*ib
,
332 GLboolean index_bounds_valid
, GLuint min_index
, GLuint max_index
,
333 struct gl_transform_feedback_object
*tfb_vertcount
,
334 unsigned tfb_stream
, struct gl_buffer_object
*indirect
)
336 struct vbo_context
*vbo
= vbo_context(ctx
);
337 vbo_bind_arrays(ctx
);
338 vbo
->draw_prims(ctx
, prims
, nr_prims
, ib
, index_bounds_valid
,
339 min_index
, max_index
, tfb_vertcount
, tfb_stream
, indirect
);
344 _vbo_draw_indirect(struct gl_context
*ctx
, GLuint mode
,
345 struct gl_buffer_object
*indirect_data
,
346 GLsizeiptr indirect_offset
, unsigned draw_count
,
348 struct gl_buffer_object
*indirect_draw_count_buffer
,
349 GLsizeiptr indirect_draw_count_offset
,
350 const struct _mesa_index_buffer
*ib
)
352 struct vbo_context
*vbo
= vbo_context(ctx
);
353 vbo_bind_arrays(ctx
);
354 vbo
->draw_indirect_prims(ctx
, mode
, indirect_data
, indirect_offset
,
355 draw_count
, stride
, indirect_draw_count_buffer
,
356 indirect_draw_count_offset
, ib
);