vbo: s/[0]/[VERT_ATTRIB_POS]/ in recalculate_input_bindings()
[mesa.git] / src / mesa / vbo / vbo_private.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 /**
27 * Types, functions, etc which are private to the VBO module.
28 */
29
30
31 #ifndef VBO_PRIVATE_H
32 #define VBO_PRIVATE_H
33
34
35 #include "vbo/vbo_attrib.h"
36 #include "vbo/vbo_exec.h"
37 #include "vbo/vbo_save.h"
38 #include "main/mtypes.h"
39
40
41 struct _glapi_table;
42 struct _mesa_prim;
43
44
45 struct vbo_context {
46 struct gl_vertex_array currval[VBO_ATTRIB_MAX];
47
48 /** Map VERT_ATTRIB_x to VBO_ATTRIB_y */
49 GLubyte map_vp_none[VERT_ATTRIB_MAX];
50 GLubyte map_vp_arb[VERT_ATTRIB_MAX];
51
52 struct vbo_exec_context exec;
53 struct vbo_save_context save;
54
55 /* Callback into the driver. This must always succeed, the driver
56 * is responsible for initiating any fallback actions required:
57 */
58 vbo_draw_func draw_prims;
59
60 /* Optional callback for indirect draws. This allows multidraws to not be
61 * broken up, as well as for the actual count to be passed in as a separate
62 * indirect parameter.
63 */
64 vbo_indirect_draw_func draw_indirect_prims;
65 };
66
67
68 static inline struct vbo_context *
69 vbo_context(struct gl_context *ctx)
70 {
71 return ctx->vbo_context;
72 }
73
74
75 /**
76 * Current vertex processing mode: fixed function vs. shader.
77 * In reality, fixed function is probably implemented by a shader but that's
78 * not what we care about here.
79 */
80 enum vp_mode {
81 VP_FF, /**< legacy / fixed function */
82 VP_SHADER /**< ARB vertex program or GLSL vertex shader */
83 };
84
85
86 /**
87 * Get current vertex processing mode (fixed function vs. shader).
88 */
89 static inline enum vp_mode
90 get_vp_mode( struct gl_context *ctx )
91 {
92 if (!ctx->VertexProgram._Current)
93 return VP_FF;
94 else if (ctx->VertexProgram._Current == ctx->VertexProgram._TnlProgram)
95 return VP_FF;
96 else
97 return VP_SHADER;
98 }
99
100
101 /**
102 * This is called by glBegin, glDrawArrays and glDrawElements (and
103 * variations of those calls). When we transition from immediate mode
104 * drawing to array drawing we need to invalidate the array state.
105 *
106 * glBegin/End builds vertex arrays. Those arrays may look identical
107 * to glDrawArrays arrays except that the position of the elements may
108 * be different. For example, arrays of (position3v, normal3f) vs. arrays
109 * of (normal3f, position3f). So we need to make sure we notify drivers
110 * that arrays may be changing.
111 */
112 static inline void
113 vbo_draw_method(struct vbo_context *vbo, gl_draw_method method)
114 {
115 struct gl_context *ctx = vbo->exec.ctx;
116
117 if (ctx->Array.DrawMethod != method) {
118 switch (method) {
119 case DRAW_ARRAYS:
120 ctx->Array._DrawArrays = vbo->exec.array.inputs;
121 break;
122 case DRAW_BEGIN_END:
123 ctx->Array._DrawArrays = vbo->exec.vtx.inputs;
124 break;
125 case DRAW_DISPLAY_LIST:
126 ctx->Array._DrawArrays = vbo->save.inputs;
127 break;
128 default:
129 unreachable("Bad VBO drawing method");
130 }
131
132 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
133 ctx->Array.DrawMethod = method;
134 }
135 }
136
137
138 /**
139 * Return if format is integer. The immediate mode commands only emit floats
140 * for non-integer types, thus everything else is integer.
141 */
142 static inline GLboolean
143 vbo_attrtype_to_integer_flag(GLenum format)
144 {
145 switch (format) {
146 case GL_FLOAT:
147 case GL_DOUBLE:
148 return GL_FALSE;
149 case GL_INT:
150 case GL_UNSIGNED_INT:
151 case GL_UNSIGNED_INT64_ARB:
152 return GL_TRUE;
153 default:
154 unreachable("Bad vertex attribute type");
155 return GL_FALSE;
156 }
157 }
158
159 static inline GLboolean
160 vbo_attrtype_to_double_flag(GLenum format)
161 {
162 switch (format) {
163 case GL_FLOAT:
164 case GL_INT:
165 case GL_UNSIGNED_INT:
166 case GL_UNSIGNED_INT64_ARB:
167 return GL_FALSE;
168 case GL_DOUBLE:
169 return GL_TRUE;
170 default:
171 unreachable("Bad vertex attribute type");
172 return GL_FALSE;
173 }
174 }
175
176
177 /**
178 * Return default component values for the given format.
179 * The return type is an array of fi_types, because that's how we declare
180 * the vertex storage : floats , integers or unsigned integers.
181 */
182 static inline const fi_type *
183 vbo_get_default_vals_as_union(GLenum format)
184 {
185 static const GLfloat default_float[4] = { 0, 0, 0, 1 };
186 static const GLint default_int[4] = { 0, 0, 0, 1 };
187
188 switch (format) {
189 case GL_FLOAT:
190 return (fi_type *)default_float;
191 case GL_INT:
192 case GL_UNSIGNED_INT:
193 return (fi_type *)default_int;
194 default:
195 unreachable("Bad vertex format");
196 return NULL;
197 }
198 }
199
200
201 /**
202 * Compute the max number of vertices which can be stored in
203 * a vertex buffer, given the current vertex size, and the amount
204 * of space already used.
205 */
206 static inline unsigned
207 vbo_compute_max_verts(const struct vbo_exec_context *exec)
208 {
209 unsigned n = (VBO_VERT_BUFFER_SIZE - exec->vtx.buffer_used) /
210 (exec->vtx.vertex_size * sizeof(GLfloat));
211 if (n == 0)
212 return 0;
213 /* Subtract one so we're always sure to have room for an extra
214 * vertex for GL_LINE_LOOP -> GL_LINE_STRIP conversion.
215 */
216 n--;
217 return n;
218 }
219
220
221 void
222 vbo_try_prim_conversion(struct _mesa_prim *p);
223
224 bool
225 vbo_can_merge_prims(const struct _mesa_prim *p0, const struct _mesa_prim *p1);
226
227 void
228 vbo_merge_prims(struct _mesa_prim *p0, const struct _mesa_prim *p1);
229
230
231 #endif /* VBO_PRIVATE_H */