vbo: move vbo_sizeof_ib_type() into vbo_exec_array.c
[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/mtypes.h"
31 #include "main/vtxfmt.h"
32 #include "vbo_context.h"
33
34
35
36 void
37 vbo_exec_init(struct gl_context *ctx)
38 {
39 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
40
41 exec->ctx = ctx;
42
43 /* aelt_context should have been created by the caller */
44 assert(ctx->aelt_context);
45
46 vbo_exec_vtx_init(exec);
47
48 ctx->Driver.NeedFlush = 0;
49 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
50
51 /* The aelt_context state should still be dirty from its creation */
52 assert(_ae_is_state_dirty(ctx));
53
54 exec->array.recalculate_inputs = GL_TRUE;
55 exec->eval.recalculate_maps = GL_TRUE;
56 }
57
58
59 void vbo_exec_destroy( struct gl_context *ctx )
60 {
61 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
62
63 if (ctx->aelt_context) {
64 _ae_destroy_context( ctx );
65 ctx->aelt_context = NULL;
66 }
67
68 vbo_exec_vtx_destroy( exec );
69 }
70
71
72 /**
73 * In some degenarate cases we can improve our ability to merge
74 * consecutive primitives. For example:
75 * glBegin(GL_LINE_STRIP);
76 * glVertex(1);
77 * glVertex(1);
78 * glEnd();
79 * glBegin(GL_LINE_STRIP);
80 * glVertex(1);
81 * glVertex(1);
82 * glEnd();
83 * Can be merged as a GL_LINES prim with four vertices.
84 *
85 * This function converts 2-vertex line strips/loops into GL_LINES, etc.
86 */
87 void
88 vbo_try_prim_conversion(struct _mesa_prim *p)
89 {
90 if (p->mode == GL_LINE_STRIP && p->count == 2) {
91 /* convert 2-vertex line strip to a separate line */
92 p->mode = GL_LINES;
93 }
94 else if ((p->mode == GL_TRIANGLE_STRIP || p->mode == GL_TRIANGLE_FAN)
95 && p->count == 3) {
96 /* convert 3-vertex tri strip or fan to a separate triangle */
97 p->mode = GL_TRIANGLES;
98 }
99
100 /* Note: we can't convert a 4-vertex quad strip to a separate quad
101 * because the vertex ordering is different. We'd have to muck
102 * around in the vertex data to make it work.
103 */
104 }
105
106
107 /**
108 * Helper function for determining if two subsequent glBegin/glEnd
109 * primitives can be combined. This is only possible for GL_POINTS,
110 * GL_LINES, GL_TRIANGLES and GL_QUADS.
111 * If we return true, it means that we can concatenate p1 onto p0 (and
112 * discard p1).
113 */
114 bool
115 vbo_can_merge_prims(const struct _mesa_prim *p0, const struct _mesa_prim *p1)
116 {
117 if (!p0->begin ||
118 !p1->begin ||
119 !p0->end ||
120 !p1->end)
121 return false;
122
123 /* The prim mode must match (ex: both GL_TRIANGLES) */
124 if (p0->mode != p1->mode)
125 return false;
126
127 /* p1's vertices must come right after p0 */
128 if (p0->start + p0->count != p1->start)
129 return false;
130
131 if (p0->basevertex != p1->basevertex ||
132 p0->num_instances != p1->num_instances ||
133 p0->base_instance != p1->base_instance)
134 return false;
135
136 /* can always merge subsequent GL_POINTS primitives */
137 if (p0->mode == GL_POINTS)
138 return true;
139
140 /* independent lines with no extra vertices */
141 if (p0->mode == GL_LINES && p0->count % 2 == 0 && p1->count % 2 == 0)
142 return true;
143
144 /* independent tris */
145 if (p0->mode == GL_TRIANGLES && p0->count % 3 == 0 && p1->count % 3 == 0)
146 return true;
147
148 /* independent quads */
149 if (p0->mode == GL_QUADS && p0->count % 4 == 0 && p1->count % 4 == 0)
150 return true;
151
152 return false;
153 }
154
155
156 /**
157 * If we've determined that p0 and p1 can be merged, this function
158 * concatenates p1 onto p0.
159 */
160 void
161 vbo_merge_prims(struct _mesa_prim *p0, const struct _mesa_prim *p1)
162 {
163 assert(vbo_can_merge_prims(p0, p1));
164
165 p0->count += p1->count;
166 p0->end = p1->end;
167 }