mesa: include mtypes.h less
[mesa.git] / src / mesa / vbo / vbo_exec.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2005 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 * Authors:
25 * Keith Whitwell <keithw@vmware.com>
26 */
27
28
29 #include "main/glheader.h"
30 #include "main/arrayobj.h"
31 #include "main/api_arrayelt.h"
32 #include "main/vtxfmt.h"
33 #include "vbo_private.h"
34
35 const GLubyte
36 _vbo_attribute_alias_map[VP_MODE_MAX][VERT_ATTRIB_MAX] = {
37 /* VP_MODE_FF: */
38 {
39 VBO_ATTRIB_POS, /* VERT_ATTRIB_POS */
40 VBO_ATTRIB_NORMAL, /* VERT_ATTRIB_NORMAL */
41 VBO_ATTRIB_COLOR0, /* VERT_ATTRIB_COLOR0 */
42 VBO_ATTRIB_COLOR1, /* VERT_ATTRIB_COLOR1 */
43 VBO_ATTRIB_FOG, /* VERT_ATTRIB_FOG */
44 VBO_ATTRIB_COLOR_INDEX, /* VERT_ATTRIB_COLOR_INDEX */
45 VBO_ATTRIB_EDGEFLAG, /* VERT_ATTRIB_EDGEFLAG */
46 VBO_ATTRIB_TEX0, /* VERT_ATTRIB_TEX0 */
47 VBO_ATTRIB_TEX1, /* VERT_ATTRIB_TEX1 */
48 VBO_ATTRIB_TEX2, /* VERT_ATTRIB_TEX2 */
49 VBO_ATTRIB_TEX3, /* VERT_ATTRIB_TEX3 */
50 VBO_ATTRIB_TEX4, /* VERT_ATTRIB_TEX4 */
51 VBO_ATTRIB_TEX5, /* VERT_ATTRIB_TEX5 */
52 VBO_ATTRIB_TEX6, /* VERT_ATTRIB_TEX6 */
53 VBO_ATTRIB_TEX7, /* VERT_ATTRIB_TEX7 */
54 VBO_ATTRIB_POINT_SIZE, /* VERT_ATTRIB_POINT_SIZE */
55 VBO_ATTRIB_GENERIC0, /* VERT_ATTRIB_GENERIC0 */
56 VBO_ATTRIB_GENERIC1, /* VERT_ATTRIB_GENERIC1 */
57 VBO_ATTRIB_GENERIC2, /* VERT_ATTRIB_GENERIC2 */
58 VBO_ATTRIB_GENERIC3, /* VERT_ATTRIB_GENERIC3 */
59 VBO_ATTRIB_MAT_FRONT_AMBIENT, /* VERT_ATTRIB_GENERIC4 */
60 VBO_ATTRIB_MAT_BACK_AMBIENT, /* VERT_ATTRIB_GENERIC5 */
61 VBO_ATTRIB_MAT_FRONT_DIFFUSE, /* VERT_ATTRIB_GENERIC6 */
62 VBO_ATTRIB_MAT_BACK_DIFFUSE, /* VERT_ATTRIB_GENERIC7 */
63 VBO_ATTRIB_MAT_FRONT_SPECULAR, /* VERT_ATTRIB_GENERIC8 */
64 VBO_ATTRIB_MAT_BACK_SPECULAR, /* VERT_ATTRIB_GENERIC9 */
65 VBO_ATTRIB_MAT_FRONT_EMISSION, /* VERT_ATTRIB_GENERIC10 */
66 VBO_ATTRIB_MAT_BACK_EMISSION, /* VERT_ATTRIB_GENERIC11 */
67 VBO_ATTRIB_MAT_FRONT_SHININESS, /* VERT_ATTRIB_GENERIC12 */
68 VBO_ATTRIB_MAT_BACK_SHININESS, /* VERT_ATTRIB_GENERIC13 */
69 VBO_ATTRIB_MAT_FRONT_INDEXES, /* VERT_ATTRIB_GENERIC14 */
70 VBO_ATTRIB_MAT_BACK_INDEXES /* VERT_ATTRIB_GENERIC15 */
71 },
72
73 /* VP_MODE_SHADER: */
74 {
75 VBO_ATTRIB_POS, /* VERT_ATTRIB_POS */
76 VBO_ATTRIB_NORMAL, /* VERT_ATTRIB_NORMAL */
77 VBO_ATTRIB_COLOR0, /* VERT_ATTRIB_COLOR0 */
78 VBO_ATTRIB_COLOR1, /* VERT_ATTRIB_COLOR1 */
79 VBO_ATTRIB_FOG, /* VERT_ATTRIB_FOG */
80 VBO_ATTRIB_COLOR_INDEX, /* VERT_ATTRIB_COLOR_INDEX */
81 VBO_ATTRIB_EDGEFLAG, /* VERT_ATTRIB_EDGEFLAG */
82 VBO_ATTRIB_TEX0, /* VERT_ATTRIB_TEX0 */
83 VBO_ATTRIB_TEX1, /* VERT_ATTRIB_TEX1 */
84 VBO_ATTRIB_TEX2, /* VERT_ATTRIB_TEX2 */
85 VBO_ATTRIB_TEX3, /* VERT_ATTRIB_TEX3 */
86 VBO_ATTRIB_TEX4, /* VERT_ATTRIB_TEX4 */
87 VBO_ATTRIB_TEX5, /* VERT_ATTRIB_TEX5 */
88 VBO_ATTRIB_TEX6, /* VERT_ATTRIB_TEX6 */
89 VBO_ATTRIB_TEX7, /* VERT_ATTRIB_TEX7 */
90 VBO_ATTRIB_POINT_SIZE, /* VERT_ATTRIB_POINT_SIZE */
91 VBO_ATTRIB_GENERIC0, /* VERT_ATTRIB_GENERIC0 */
92 VBO_ATTRIB_GENERIC1, /* VERT_ATTRIB_GENERIC1 */
93 VBO_ATTRIB_GENERIC2, /* VERT_ATTRIB_GENERIC2 */
94 VBO_ATTRIB_GENERIC3, /* VERT_ATTRIB_GENERIC3 */
95 VBO_ATTRIB_GENERIC4, /* VERT_ATTRIB_GENERIC4 */
96 VBO_ATTRIB_GENERIC5, /* VERT_ATTRIB_GENERIC5 */
97 VBO_ATTRIB_GENERIC6, /* VERT_ATTRIB_GENERIC6 */
98 VBO_ATTRIB_GENERIC7, /* VERT_ATTRIB_GENERIC7 */
99 VBO_ATTRIB_GENERIC8, /* VERT_ATTRIB_GENERIC8 */
100 VBO_ATTRIB_GENERIC9, /* VERT_ATTRIB_GENERIC9 */
101 VBO_ATTRIB_GENERIC10, /* VERT_ATTRIB_GENERIC10 */
102 VBO_ATTRIB_GENERIC11, /* VERT_ATTRIB_GENERIC11 */
103 VBO_ATTRIB_GENERIC12, /* VERT_ATTRIB_GENERIC12 */
104 VBO_ATTRIB_GENERIC13, /* VERT_ATTRIB_GENERIC13 */
105 VBO_ATTRIB_GENERIC14, /* VERT_ATTRIB_GENERIC14 */
106 VBO_ATTRIB_GENERIC15 /* VERT_ATTRIB_GENERIC15 */
107 }
108 };
109
110
111 void
112 vbo_exec_init(struct gl_context *ctx)
113 {
114 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
115
116 exec->ctx = ctx;
117
118 /* aelt_context should have been created by the caller */
119 assert(ctx->aelt_context);
120
121 vbo_exec_vtx_init(exec);
122
123 ctx->Driver.NeedFlush = 0;
124 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
125
126 /* The aelt_context state should still be dirty from its creation */
127 assert(_ae_is_state_dirty(ctx));
128
129 exec->eval.recalculate_maps = GL_TRUE;
130 }
131
132
133 void vbo_exec_destroy( struct gl_context *ctx )
134 {
135 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
136
137 if (ctx->aelt_context) {
138 _ae_destroy_context( ctx );
139 ctx->aelt_context = NULL;
140 }
141
142 vbo_exec_vtx_destroy( exec );
143 }
144
145
146 /**
147 * In some degenarate cases we can improve our ability to merge
148 * consecutive primitives. For example:
149 * glBegin(GL_LINE_STRIP);
150 * glVertex(1);
151 * glVertex(1);
152 * glEnd();
153 * glBegin(GL_LINE_STRIP);
154 * glVertex(1);
155 * glVertex(1);
156 * glEnd();
157 * Can be merged as a GL_LINES prim with four vertices.
158 *
159 * This function converts 2-vertex line strips/loops into GL_LINES, etc.
160 */
161 void
162 vbo_try_prim_conversion(struct _mesa_prim *p)
163 {
164 if (p->mode == GL_LINE_STRIP && p->count == 2) {
165 /* convert 2-vertex line strip to a separate line */
166 p->mode = GL_LINES;
167 }
168 else if ((p->mode == GL_TRIANGLE_STRIP || p->mode == GL_TRIANGLE_FAN)
169 && p->count == 3) {
170 /* convert 3-vertex tri strip or fan to a separate triangle */
171 p->mode = GL_TRIANGLES;
172 }
173
174 /* Note: we can't convert a 4-vertex quad strip to a separate quad
175 * because the vertex ordering is different. We'd have to muck
176 * around in the vertex data to make it work.
177 */
178 }
179
180
181 /**
182 * Helper function for determining if two subsequent glBegin/glEnd
183 * primitives can be combined. This is only possible for GL_POINTS,
184 * GL_LINES, GL_TRIANGLES and GL_QUADS.
185 * If we return true, it means that we can concatenate p1 onto p0 (and
186 * discard p1).
187 */
188 bool
189 vbo_can_merge_prims(const struct _mesa_prim *p0, const struct _mesa_prim *p1)
190 {
191 if (!p0->begin ||
192 !p1->begin ||
193 !p0->end ||
194 !p1->end)
195 return false;
196
197 /* The prim mode must match (ex: both GL_TRIANGLES) */
198 if (p0->mode != p1->mode)
199 return false;
200
201 /* p1's vertices must come right after p0 */
202 if (p0->start + p0->count != p1->start)
203 return false;
204
205 if (p0->basevertex != p1->basevertex ||
206 p0->num_instances != p1->num_instances ||
207 p0->base_instance != p1->base_instance)
208 return false;
209
210 /* can always merge subsequent GL_POINTS primitives */
211 if (p0->mode == GL_POINTS)
212 return true;
213
214 /* independent lines with no extra vertices */
215 if (p0->mode == GL_LINES && p0->count % 2 == 0 && p1->count % 2 == 0)
216 return true;
217
218 /* independent tris */
219 if (p0->mode == GL_TRIANGLES && p0->count % 3 == 0 && p1->count % 3 == 0)
220 return true;
221
222 /* independent quads */
223 if (p0->mode == GL_QUADS && p0->count % 4 == 0 && p1->count % 4 == 0)
224 return true;
225
226 return false;
227 }
228
229
230 /**
231 * If we've determined that p0 and p1 can be merged, this function
232 * concatenates p1 onto p0.
233 */
234 void
235 vbo_merge_prims(struct _mesa_prim *p0, const struct _mesa_prim *p1)
236 {
237 assert(vbo_can_merge_prims(p0, p1));
238
239 p0->count += p1->count;
240 p0->end = p1->end;
241 }
242
243
244 void
245 _vbo_init_inputs(struct vbo_inputs *inputs)
246 {
247 inputs->current = 0;
248 inputs->vertex_processing_mode = VP_MODE_FF;
249 }
250
251
252 /**
253 * Update the vbo_inputs's arrays to point to the vao->_VertexArray arrays
254 * according to the 'enable' bitmask.
255 * \param enable bitfield of VERT_BIT_x flags.
256 */
257 static inline void
258 update_vao_inputs(struct gl_context *ctx,
259 struct vbo_inputs *inputs, GLbitfield enable)
260 {
261 const struct gl_vertex_array_object *vao = ctx->Array._DrawVAO;
262
263 /* Make sure we process only arrays enabled in the VAO */
264 assert((enable & ~_mesa_get_vao_vp_inputs(vao)) == 0);
265
266 /* Fill in the client arrays from the VAO */
267 const GLubyte *const map = _mesa_vao_attribute_map[vao->_AttributeMapMode];
268 const struct gl_array_attributes *attribs = &vao->VertexAttrib[0];
269 const struct gl_vertex_buffer_binding *bindings = &vao->BufferBinding[0];
270 while (enable) {
271 const int attr = u_bit_scan(&enable);
272 struct gl_vertex_array *input = &inputs->inputs[attr];
273 const struct gl_array_attributes *attrib = &attribs[map[attr]];
274 input->VertexAttrib = attrib;
275 input->BufferBinding = &bindings[attrib->BufferBindingIndex];
276 }
277 }
278
279
280 /**
281 * Update the vbo_inputs's arrays to point to the vbo->currval arrays
282 * according to the 'current' bitmask.
283 * \param current bitfield of VERT_BIT_x flags.
284 */
285 static inline void
286 update_current_inputs(struct gl_context *ctx,
287 struct vbo_inputs *inputs, GLbitfield current)
288 {
289 gl_vertex_processing_mode mode = ctx->VertexProgram._VPMode;
290
291 /* All previously non current array pointers need update. */
292 GLbitfield mask = current & ~inputs->current;
293 /* On mode change, the slots aliasing with materials need update too */
294 if (mode != inputs->vertex_processing_mode)
295 mask |= current & VERT_BIT_MAT_ALL;
296
297 struct vbo_context *vbo = vbo_context(ctx);
298 const struct gl_array_attributes *const currval = &vbo->current[0];
299 const GLubyte *const map = _vbo_attribute_alias_map[mode];
300 while (mask) {
301 const int attr = u_bit_scan(&mask);
302 struct gl_vertex_array *input = &inputs->inputs[attr];
303 input->VertexAttrib = &currval[map[attr]];
304 input->BufferBinding = &vbo->binding;
305 }
306
307 inputs->current = current;
308 inputs->vertex_processing_mode = mode;
309 }
310
311
312 /**
313 * Update the vbo_inputs's arrays to point to the vao->_VertexArray and
314 * vbo->currval arrays according to Array._DrawVAO and
315 * Array._DrawVAOEnableAttribs.
316 */
317 void
318 _vbo_update_inputs(struct gl_context *ctx, struct vbo_inputs *inputs)
319 {
320 const GLbitfield enable = ctx->Array._DrawVAOEnabledAttribs;
321
322 /* Update array input pointers */
323 update_vao_inputs(ctx, inputs, enable);
324
325 /* The rest must be current inputs. */
326 update_current_inputs(ctx, inputs, ~enable & VERT_BIT_ALL);
327 }