mesa: use new _vbo_install_exec_vtxfmt() function
[mesa.git] / src / mesa / vbo / vbo_context.h
1 /*
2 * mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 /**
26 * \file vbo_context.h
27 * \brief VBO builder module datatypes and definitions.
28 * \author Keith Whitwell
29 */
30
31
32 /**
33 * \mainpage The VBO builder module
34 *
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.
40 *
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.
44 *
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.
48 */
49
50
51 #ifndef _VBO_CONTEXT_H
52 #define _VBO_CONTEXT_H
53
54 #include "vbo.h"
55 #include "vbo_attrib.h"
56 #include "vbo_exec.h"
57 #include "vbo_save.h"
58
59 #include "main/api_arrayelt.h"
60 #include "main/macros.h"
61
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65
66 struct vbo_context {
67 struct gl_vertex_array currval[VBO_ATTRIB_MAX];
68
69 /** Map VERT_ATTRIB_x to VBO_ATTRIB_y */
70 GLubyte map_vp_none[VERT_ATTRIB_MAX];
71 GLubyte map_vp_arb[VERT_ATTRIB_MAX];
72
73 struct vbo_exec_context exec;
74 struct vbo_save_context save;
75
76 /* Callback into the driver. This must always succeed, the driver
77 * is responsible for initiating any fallback actions required:
78 */
79 vbo_draw_func draw_prims;
80
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
83 * indirect parameter.
84 */
85 vbo_indirect_draw_func draw_indirect_prims;
86 };
87
88
89 static inline struct vbo_context *vbo_context(struct gl_context *ctx)
90 {
91 return ctx->vbo_context;
92 }
93
94
95 static inline void
96 vbo_exec_invalidate_state(struct gl_context *ctx)
97 {
98 struct vbo_context *vbo = vbo_context(ctx);
99 struct vbo_exec_context *exec = &vbo->exec;
100
101 if (ctx->NewState & (_NEW_PROGRAM | _NEW_ARRAY)) {
102 if (!exec->validating)
103 exec->array.recalculate_inputs = GL_TRUE;
104
105 _ae_invalidate_state(ctx);
106 }
107
108 if (ctx->NewState & _NEW_EVAL)
109 exec->eval.recalculate_maps = GL_TRUE;
110 }
111
112
113 void
114 _vbo_install_exec_vtxfmt(struct gl_context *ctx);
115
116
117 /**
118 * Return VP_x token to indicate whether we're running fixed-function
119 * vertex transformation, an NV vertex program or ARB vertex program/shader.
120 */
121 static inline enum vp_mode
122 get_program_mode( struct gl_context *ctx )
123 {
124 if (!ctx->VertexProgram._Current)
125 return VP_NONE;
126 else if (ctx->VertexProgram._Current == ctx->VertexProgram._TnlProgram)
127 return VP_NONE;
128 else
129 return VP_ARB;
130 }
131
132
133 /**
134 * This is called by glBegin, glDrawArrays and glDrawElements (and
135 * variations of those calls). When we transition from immediate mode
136 * drawing to array drawing we need to invalidate the array state.
137 *
138 * glBegin/End builds vertex arrays. Those arrays may look identical
139 * to glDrawArrays arrays except that the position of the elements may
140 * be different. For example, arrays of (position3v, normal3f) vs. arrays
141 * of (normal3f, position3f). So we need to make sure we notify drivers
142 * that arrays may be changing.
143 */
144 static inline void
145 vbo_draw_method(struct vbo_context *vbo, gl_draw_method method)
146 {
147 struct gl_context *ctx = vbo->exec.ctx;
148
149 if (ctx->Array.DrawMethod != method) {
150 switch (method) {
151 case DRAW_ARRAYS:
152 ctx->Array._DrawArrays = vbo->exec.array.inputs;
153 break;
154 case DRAW_BEGIN_END:
155 ctx->Array._DrawArrays = vbo->exec.vtx.inputs;
156 break;
157 case DRAW_DISPLAY_LIST:
158 ctx->Array._DrawArrays = vbo->save.inputs;
159 break;
160 default:
161 unreachable("Bad VBO drawing method");
162 }
163
164 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
165 ctx->Array.DrawMethod = method;
166 }
167 }
168
169 /**
170 * Return if format is integer. The immediate mode commands only emit floats
171 * for non-integer types, thus everything else is integer.
172 */
173 static inline GLboolean
174 vbo_attrtype_to_integer_flag(GLenum format)
175 {
176 switch (format) {
177 case GL_FLOAT:
178 case GL_DOUBLE:
179 return GL_FALSE;
180 case GL_INT:
181 case GL_UNSIGNED_INT:
182 case GL_UNSIGNED_INT64_ARB:
183 return GL_TRUE;
184 default:
185 unreachable("Bad vertex attribute type");
186 return GL_FALSE;
187 }
188 }
189
190 static inline GLboolean
191 vbo_attrtype_to_double_flag(GLenum format)
192 {
193 switch (format) {
194 case GL_FLOAT:
195 case GL_INT:
196 case GL_UNSIGNED_INT:
197 case GL_UNSIGNED_INT64_ARB:
198 return GL_FALSE;
199 case GL_DOUBLE:
200 return GL_TRUE;
201 default:
202 unreachable("Bad vertex attribute type");
203 return GL_FALSE;
204 }
205 }
206
207 /**
208 * Return default component values for the given format.
209 * The return type is an array of fi_types, because that's how we declare
210 * the vertex storage : floats , integers or unsigned integers.
211 */
212 static inline const fi_type *
213 vbo_get_default_vals_as_union(GLenum format)
214 {
215 static const GLfloat default_float[4] = { 0, 0, 0, 1 };
216 static const GLint default_int[4] = { 0, 0, 0, 1 };
217
218 switch (format) {
219 case GL_FLOAT:
220 return (fi_type *)default_float;
221 case GL_INT:
222 case GL_UNSIGNED_INT:
223 return (fi_type *)default_int;
224 default:
225 unreachable("Bad vertex format");
226 return NULL;
227 }
228 }
229
230
231 /**
232 * Compute the max number of vertices which can be stored in
233 * a vertex buffer, given the current vertex size, and the amount
234 * of space already used.
235 */
236 static inline unsigned
237 vbo_compute_max_verts(const struct vbo_exec_context *exec)
238 {
239 unsigned n = (VBO_VERT_BUFFER_SIZE - exec->vtx.buffer_used) /
240 (exec->vtx.vertex_size * sizeof(GLfloat));
241 if (n == 0)
242 return 0;
243 /* Subtract one so we're always sure to have room for an extra
244 * vertex for GL_LINE_LOOP -> GL_LINE_STRIP conversion.
245 */
246 n--;
247 return n;
248 }
249
250
251 #ifdef __cplusplus
252 } // extern "C"
253 #endif
254
255 #endif