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