2 * mesa 3-D graphics library
4 * Copyright (C) 1999-2006 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.
27 * \brief VBO builder module datatypes and definitions.
28 * \author Keith Whitwell
33 * \mainpage The VBO builder module
35 * This module hooks into the GL dispatch table and catches all vertex
36 * building and drawing commands, such as glVertex3f, glBegin and
37 * glDrawArrays. The module stores all incoming vertex data as arrays
38 * in GL vertex buffer objects (VBOs), and translates all drawing
39 * commands into calls to a driver supplied DrawPrimitives() callback.
41 * The module captures both immediate mode and display list drawing,
42 * and manages the allocation, reference counting and deallocation of
43 * vertex buffer objects itself.
45 * The DrawPrimitives() callback can be either implemented by the
46 * driver itself or hooked to the tnl module's _tnl_draw_primitives()
47 * function for hardware without tnl capablilties or during fallbacks.
51 #ifndef _VBO_CONTEXT_H
52 #define _VBO_CONTEXT_H
55 #include "vbo_attrib.h"
59 #include "main/api_arrayelt.h"
60 #include "main/macros.h"
67 struct gl_vertex_array currval
[VBO_ATTRIB_MAX
];
69 /** Map VERT_ATTRIB_x to VBO_ATTRIB_y */
70 GLuint map_vp_none
[VERT_ATTRIB_MAX
];
71 GLuint map_vp_arb
[VERT_ATTRIB_MAX
];
73 struct vbo_exec_context exec
;
74 struct vbo_save_context save
;
76 /* Callback into the driver. This must always succeed, the driver
77 * is responsible for initiating any fallback actions required:
79 vbo_draw_func draw_prims
;
81 /* Optional callback for indirect draws. This allows multidraws to not be
82 * broken up, as well as for the actual count to be passed in as a separate
85 vbo_indirect_draw_func draw_indirect_prims
;
89 static inline struct vbo_context
*vbo_context(struct gl_context
*ctx
)
91 return ctx
->vbo_context
;
96 vbo_exec_invalidate_state(struct gl_context
*ctx
)
98 struct vbo_context
*vbo
= vbo_context(ctx
);
99 struct vbo_exec_context
*exec
= &vbo
->exec
;
101 if (ctx
->NewState
& (_NEW_PROGRAM
| _NEW_ARRAY
)) {
102 if (!exec
->validating
)
103 exec
->array
.recalculate_inputs
= GL_TRUE
;
105 _ae_invalidate_state(ctx
);
108 if (ctx
->NewState
& _NEW_EVAL
)
109 exec
->eval
.recalculate_maps
= GL_TRUE
;
114 * Return VP_x token to indicate whether we're running fixed-function
115 * vertex transformation, an NV vertex program or ARB vertex program/shader.
117 static inline enum vp_mode
118 get_program_mode( struct gl_context
*ctx
)
120 if (!ctx
->VertexProgram
._Current
)
122 else if (ctx
->VertexProgram
._Current
== ctx
->VertexProgram
._TnlProgram
)
130 * This is called by glBegin, glDrawArrays and glDrawElements (and
131 * variations of those calls). When we transition from immediate mode
132 * drawing to array drawing we need to invalidate the array state.
134 * glBegin/End builds vertex arrays. Those arrays may look identical
135 * to glDrawArrays arrays except that the position of the elements may
136 * be different. For example, arrays of (position3v, normal3f) vs. arrays
137 * of (normal3f, position3f). So we need to make sure we notify drivers
138 * that arrays may be changing.
141 vbo_draw_method(struct vbo_context
*vbo
, gl_draw_method method
)
143 struct gl_context
*ctx
= vbo
->exec
.ctx
;
145 if (ctx
->Array
.DrawMethod
!= method
) {
148 ctx
->Array
._DrawArrays
= vbo
->exec
.array
.inputs
;
151 ctx
->Array
._DrawArrays
= vbo
->exec
.vtx
.inputs
;
153 case DRAW_DISPLAY_LIST
:
154 ctx
->Array
._DrawArrays
= vbo
->save
.inputs
;
160 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
161 ctx
->Array
.DrawMethod
= method
;
166 * Return if format is integer. The immediate mode commands only emit floats
167 * for non-integer types, thus everything else is integer.
169 static inline GLboolean
170 vbo_attrtype_to_integer_flag(GLenum format
)
177 case GL_UNSIGNED_INT
:
178 case GL_UNSIGNED_INT64_ARB
:
186 static inline GLboolean
187 vbo_attrtype_to_double_flag(GLenum format
)
192 case GL_UNSIGNED_INT
:
193 case GL_UNSIGNED_INT64_ARB
:
204 * Return default component values for the given format.
205 * The return type is an array of fi_types, because that's how we declare
206 * the vertex storage : floats , integers or unsigned integers.
208 static inline const fi_type
*
209 vbo_get_default_vals_as_union(GLenum format
)
211 static const GLfloat default_float
[4] = { 0, 0, 0, 1 };
212 static const GLint default_int
[4] = { 0, 0, 0, 1 };
216 return (fi_type
*)default_float
;
218 case GL_UNSIGNED_INT
:
219 return (fi_type
*)default_int
;
228 * Compute the max number of vertices which can be stored in
229 * a vertex buffer, given the current vertex size, and the amount
230 * of space already used.
232 static inline unsigned
233 vbo_compute_max_verts(const struct vbo_exec_context
*exec
)
235 unsigned n
= (VBO_VERT_BUFFER_SIZE
- exec
->vtx
.buffer_used
) /
236 (exec
->vtx
.vertex_size
* sizeof(GLfloat
));
239 /* Subtract one so we're always sure to have room for an extra
240 * vertex for GL_LINE_LOOP -> GL_LINE_STRIP conversion.