Merge branch 'vbo_0_1_branch' into vbo-0.2
[mesa.git] / src / mesa / vbo / vbo_save_loopback.c
1 /**************************************************************************
2 *
3 * Copyright 2005 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * 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
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "swrast_setup/swrast_setup.h"
29 #include "swrast/swrast.h"
30 #include "tnl/tnl.h"
31 #include "context.h"
32
33 #include "vbo_context.h"
34
35 #include "glheader.h"
36 #include "enums.h"
37 #include "glapi.h"
38 #include "imports.h"
39 #include "macros.h"
40 #include "mtypes.h"
41 #include "dispatch.h"
42
43
44 typedef void (*attr_func)( GLcontext *ctx, GLint target, const GLfloat * );
45
46
47 /* This file makes heavy use of the aliasing of NV vertex attributes
48 * with the legacy attributes, and also with ARB and Material
49 * attributes as currently implemented.
50 */
51 static void VertexAttrib1fvNV(GLcontext *ctx, GLint target, const GLfloat *v)
52 {
53 CALL_VertexAttrib1fvNV(ctx->Exec, (target, v));
54 }
55
56 static void VertexAttrib2fvNV(GLcontext *ctx, GLint target, const GLfloat *v)
57 {
58 CALL_VertexAttrib2fvNV(ctx->Exec, (target, v));
59 }
60
61 static void VertexAttrib3fvNV(GLcontext *ctx, GLint target, const GLfloat *v)
62 {
63 CALL_VertexAttrib3fvNV(ctx->Exec, (target, v));
64 }
65
66 static void VertexAttrib4fvNV(GLcontext *ctx, GLint target, const GLfloat *v)
67 {
68 CALL_VertexAttrib4fvNV(ctx->Exec, (target, v));
69 }
70
71 static attr_func vert_attrfunc[4] = {
72 VertexAttrib1fvNV,
73 VertexAttrib2fvNV,
74 VertexAttrib3fvNV,
75 VertexAttrib4fvNV
76 };
77
78 struct loopback_attr {
79 GLint target;
80 GLint sz;
81 attr_func func;
82 };
83
84 /* Don't emit ends and begins on wrapped primitives. Don't replay
85 * wrapped vertices. If we get here, it's probably because the the
86 * precalculated wrapping is wrong.
87 */
88 static void loopback_prim( GLcontext *ctx,
89 const GLfloat *buffer,
90 const struct _mesa_prim *prim,
91 GLuint wrap_count,
92 GLuint vertex_size,
93 const struct loopback_attr *la, GLuint nr )
94 {
95 GLint start = prim->start;
96 GLint end = start + prim->count;
97 const GLfloat *data;
98 GLint j;
99 GLuint k;
100
101 if (0)
102 _mesa_printf("loopback prim %s(%s,%s) verts %d..%d\n",
103 _mesa_lookup_enum_by_nr(prim->mode),
104 prim->begin ? "begin" : "..",
105 prim->end ? "end" : "..",
106 start,
107 end);
108
109 if (prim->begin) {
110 CALL_Begin(GET_DISPATCH(), ( prim->mode ));
111 }
112 else {
113 assert(start == 0);
114 start += wrap_count;
115 }
116
117 data = buffer + start * vertex_size;
118
119 for (j = start ; j < end ; j++) {
120 const GLfloat *tmp = data + la[0].sz;
121
122 for (k = 1 ; k < nr ; k++) {
123 la[k].func( ctx, la[k].target, tmp );
124 tmp += la[k].sz;
125 }
126
127 /* Fire the vertex
128 */
129 la[0].func( ctx, VBO_ATTRIB_POS, data );
130 data = tmp;
131 }
132
133 if (prim->end) {
134 CALL_End(GET_DISPATCH(), ());
135 }
136 }
137
138 /* Primitives generated by DrawArrays/DrawElements/Rectf may be
139 * caught here. If there is no primitive in progress, execute them
140 * normally, otherwise need to track and discard the generated
141 * primitives.
142 */
143 static void loopback_weak_prim( GLcontext *ctx,
144 const struct _mesa_prim *prim )
145 {
146 /* Use the prim_weak flag to ensure that if this primitive
147 * wraps, we don't mistake future vertex_lists for part of the
148 * surrounding primitive.
149 *
150 * While this flag is set, we are simply disposing of data
151 * generated by an operation now known to be a noop.
152 */
153 if (prim->begin)
154 ctx->Driver.CurrentExecPrimitive |= VBO_SAVE_PRIM_WEAK;
155 if (prim->end)
156 ctx->Driver.CurrentExecPrimitive &= ~VBO_SAVE_PRIM_WEAK;
157 }
158
159
160 void vbo_loopback_vertex_list( GLcontext *ctx,
161 const GLfloat *buffer,
162 const GLubyte *attrsz,
163 const struct _mesa_prim *prim,
164 GLuint prim_count,
165 GLuint wrap_count,
166 GLuint vertex_size)
167 {
168 struct loopback_attr la[VBO_ATTRIB_MAX];
169 GLuint i, nr = 0;
170
171 /* All Legacy, NV, ARB and Material attributes are routed through
172 * the NV attributes entrypoints:
173 */
174 for (i = 0 ; i < VBO_ATTRIB_MAX ; i++) {
175 if (attrsz[i]) {
176 la[nr].target = i;
177 la[nr].sz = attrsz[i];
178 la[nr].func = vert_attrfunc[attrsz[i]-1];
179 nr++;
180 }
181 }
182
183 for (i = 0 ; i < prim_count ; i++) {
184 if ((prim[i].mode & VBO_SAVE_PRIM_WEAK) &&
185 (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END))
186 {
187 loopback_weak_prim( ctx, &prim[i] );
188 }
189 else
190 {
191 loopback_prim( ctx, buffer, &prim[i], wrap_count, vertex_size, la, nr );
192 }
193 }
194 }