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
vbo_exec_init(struct gl_context * ctx,bool use_buffer_objects)112 vbo_exec_init(struct gl_context *ctx, bool use_buffer_objects)
113 {
114    struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
115 
116    exec->ctx = ctx;
117 
118    vbo_exec_vtx_init(exec, use_buffer_objects);
119 
120    ctx->Driver.NeedFlush = 0;
121    ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
122 
123    exec->eval.recalculate_maps = GL_TRUE;
124 }
125 
126 
vbo_exec_destroy(struct gl_context * ctx)127 void vbo_exec_destroy( struct gl_context *ctx )
128 {
129    struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
130 
131    vbo_exec_vtx_destroy( exec );
132 }
133 
134 
135 /**
136  * In some degenarate cases we can improve our ability to merge
137  * consecutive primitives.  For example:
138  * glBegin(GL_LINE_STRIP);
139  * glVertex(1);
140  * glVertex(1);
141  * glEnd();
142  * glBegin(GL_LINE_STRIP);
143  * glVertex(1);
144  * glVertex(1);
145  * glEnd();
146  * Can be merged as a GL_LINES prim with four vertices.
147  *
148  * This function converts 2-vertex line strips/loops into GL_LINES, etc.
149  */
150 void
vbo_try_prim_conversion(struct _mesa_prim * p)151 vbo_try_prim_conversion(struct _mesa_prim *p)
152 {
153    if (p->mode == GL_LINE_STRIP && p->count == 2) {
154       /* convert 2-vertex line strip to a separate line */
155       p->mode = GL_LINES;
156    }
157    else if ((p->mode == GL_TRIANGLE_STRIP || p->mode == GL_TRIANGLE_FAN)
158        && p->count == 3) {
159       /* convert 3-vertex tri strip or fan to a separate triangle */
160       p->mode = GL_TRIANGLES;
161    }
162 
163    /* Note: we can't convert a 4-vertex quad strip to a separate quad
164     * because the vertex ordering is different.  We'd have to muck
165     * around in the vertex data to make it work.
166     */
167 }
168 
169 
170 /**
171  * Function for merging two subsequent glBegin/glEnd draws.
172  * Return true if p1 was concatenated onto p0 (to discard p1 in the caller).
173  */
174 bool
vbo_merge_draws(struct gl_context * ctx,bool in_dlist,struct _mesa_prim * p0,const struct _mesa_prim * p1)175 vbo_merge_draws(struct gl_context *ctx, bool in_dlist,
176                 struct _mesa_prim *p0, const struct _mesa_prim *p1)
177 {
178    /* The prim mode must match (ex: both GL_TRIANGLES) */
179    if (p0->mode != p1->mode)
180       return false;
181 
182    /* p1's vertices must come right after p0 */
183    if (p0->start + p0->count != p1->start)
184       return false;
185 
186    /* This checks whether mode is equal to any line primitive type, taking
187     * advantage of the fact that primitives types go from 0 to 14.
188     */
189    if ((1 << p0->mode) &
190        ((1 << GL_LINES) |
191         (1 << GL_LINE_LOOP) |
192         (1 << GL_LINE_STRIP) |
193         (1 << GL_LINES_ADJACENCY) |
194         (1 << GL_LINE_STRIP_ADJACENCY))) {
195       /* "begin" resets the line stipple pattern during line stipple emulation
196        * in tnl.
197        *
198        * StippleFlag can be unknown when compiling a display list.
199        *
200        * Other uses of "begin" are internal to the vbo module, and in those
201        * cases, "begin" is not used after merging draws.
202        */
203       if (p1->begin == 1 && (in_dlist || ctx->Line.StippleFlag))
204          return false;
205 
206       /* _mesa_prim::end is irrelevant at this point and is only used
207        * before this function is called.
208        */
209    }
210 
211    assert(p0->basevertex == p1->basevertex);
212 
213    switch (p0->mode) {
214    case GL_POINTS:
215       /* can always merge subsequent GL_POINTS primitives */
216       break;
217    /* check independent primitives with no extra vertices */
218    case GL_LINES:
219       if (p0->count % 2)
220          return false;
221       break;
222    case GL_TRIANGLES:
223       if (p0->count % 3)
224          return false;
225       break;
226    case GL_QUADS:
227    case GL_LINES_ADJACENCY:
228       if (p0->count % 4)
229          return false;
230       break;
231    case GL_TRIANGLES_ADJACENCY:
232       if (p0->count % 6)
233          return false;
234       break;
235    case GL_PATCHES:
236       /* "patch_vertices" can be unknown when compiling a display list. */
237       if (in_dlist ||
238           p0->count % ctx->TessCtrlProgram.patch_vertices)
239          return false;
240       break;
241    default:
242       return false;
243    }
244 
245    /* Merge draws. */
246    p0->count += p1->count;
247    p0->end = p1->end;
248    return true;
249 }
250 
251 /**
252  * Copy zero, one or two vertices from the current vertex buffer into
253  * the temporary "copy" buffer.
254  * This is used when a single primitive overflows a vertex buffer and
255  * we need to continue the primitive in a new vertex buffer.
256  * The temporary "copy" buffer holds the vertices which need to get
257  * copied from the old buffer to the new one.
258  */
259 unsigned
vbo_copy_vertices(struct gl_context * ctx,GLenum mode,struct _mesa_prim * last_prim,unsigned vertex_size,bool in_dlist,fi_type * dst,const fi_type * src)260 vbo_copy_vertices(struct gl_context *ctx,
261                   GLenum mode,
262                   struct _mesa_prim *last_prim,
263                   unsigned vertex_size,
264                   bool in_dlist,
265                   fi_type *dst,
266                   const fi_type *src)
267 {
268    const unsigned count = last_prim->count;
269    unsigned copy = 0;
270 
271    switch (mode) {
272    case GL_POINTS:
273       return 0;
274    case GL_LINES:
275       copy = count % 2;
276       break;
277    case GL_TRIANGLES:
278       copy = count % 3;
279       break;
280    case GL_QUADS:
281    case GL_LINES_ADJACENCY:
282       copy = count % 4;
283       break;
284    case GL_TRIANGLES_ADJACENCY:
285       copy = count % 6;
286       break;
287    case GL_LINE_STRIP:
288       copy = MIN2(1, count);
289       break;
290    case GL_LINE_STRIP_ADJACENCY:
291       /* We need to copy 3 vertices, because:
292        *    Last strip:  ---o---o---x     (last line)
293        *    Next strip:     x---o---o---  (next line)
294        */
295       copy = MIN2(3, count);
296       break;
297    case GL_PATCHES:
298       if (in_dlist) {
299          /* We don't know the value of GL_PATCH_VERTICES when compiling
300           * a display list.
301           *
302           * Fail an assertion in debug builds and use the value of 3
303           * in release builds, which is more likely than any other value.
304           */
305          assert(!"patch_vertices is unknown");
306          copy = count % 3;
307       } else {
308          copy = count % ctx->TessCtrlProgram.patch_vertices;
309       }
310       break;
311    case GL_LINE_LOOP:
312       if (!in_dlist && last_prim->begin == 0) {
313          /* We're dealing with the second or later section of a split/wrapped
314           * GL_LINE_LOOP.  Since we're converting line loops to line strips,
315           * we've already incremented the last_prim->start counter by one to
316           * skip the 0th vertex in the loop.  We need to undo that (effectively
317           * subtract one from last_prim->start) so that we copy the 0th vertex
318           * to the next vertex buffer.
319           */
320          assert(last_prim->start > 0);
321          src -= vertex_size;
322       }
323       /* fall-through */
324    case GL_TRIANGLE_FAN:
325    case GL_POLYGON:
326       if (count == 0) {
327          return 0;
328       } else if (count == 1) {
329          memcpy(dst, src + 0, vertex_size * sizeof(GLfloat));
330          return 1;
331       } else {
332          memcpy(dst, src + 0, vertex_size * sizeof(GLfloat));
333          memcpy(dst + vertex_size, src + (count - 1) * vertex_size,
334                 vertex_size * sizeof(GLfloat));
335          return 2;
336       }
337    case GL_TRIANGLE_STRIP:
338       /* Draw an even number of triangles to keep front/back facing the same. */
339       last_prim->count -= count % 2;
340       /* fallthrough */
341    case GL_QUAD_STRIP:
342       if (count <= 1)
343          copy = count;
344       else
345          copy = 2 + (count % 2);
346       break;
347    case PRIM_OUTSIDE_BEGIN_END:
348       return 0;
349    case GL_TRIANGLE_STRIP_ADJACENCY:
350       /* TODO: Splitting tri strips with adjacency is too complicated. */
351    default:
352       unreachable("Unexpected primitive type");
353       return 0;
354    }
355 
356    memcpy(dst, src + (count - copy) * vertex_size,
357           copy * vertex_size * sizeof(GLfloat));
358    return copy;
359 }
360