Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa into pipe-video
[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 "main/context.h"
29 #include "main/glheader.h"
30 #include "main/enums.h"
31 #include "main/imports.h"
32 #include "main/mtypes.h"
33 #include "main/dispatch.h"
34 #include "glapi/glapi.h"
35
36 #include "vbo_context.h"
37
38
39 #if FEATURE_dlist
40
41
42 typedef void (*attr_func)( struct gl_context *ctx, GLint target, const GLfloat * );
43
44
45 /* This file makes heavy use of the aliasing of NV vertex attributes
46 * with the legacy attributes, and also with ARB and Material
47 * attributes as currently implemented.
48 */
49 static void VertexAttrib1fvNV(struct gl_context *ctx, GLint target, const GLfloat *v)
50 {
51 CALL_VertexAttrib1fvNV(ctx->Exec, (target, v));
52 }
53
54 static void VertexAttrib2fvNV(struct gl_context *ctx, GLint target, const GLfloat *v)
55 {
56 CALL_VertexAttrib2fvNV(ctx->Exec, (target, v));
57 }
58
59 static void VertexAttrib3fvNV(struct gl_context *ctx, GLint target, const GLfloat *v)
60 {
61 CALL_VertexAttrib3fvNV(ctx->Exec, (target, v));
62 }
63
64 static void VertexAttrib4fvNV(struct gl_context *ctx, GLint target, const GLfloat *v)
65 {
66 CALL_VertexAttrib4fvNV(ctx->Exec, (target, v));
67 }
68
69 static attr_func vert_attrfunc[4] = {
70 VertexAttrib1fvNV,
71 VertexAttrib2fvNV,
72 VertexAttrib3fvNV,
73 VertexAttrib4fvNV
74 };
75
76 struct loopback_attr {
77 GLint target;
78 GLint sz;
79 attr_func func;
80 };
81
82 /* Don't emit ends and begins on wrapped primitives. Don't replay
83 * wrapped vertices. If we get here, it's probably because the
84 * precalculated wrapping is wrong.
85 */
86 static void loopback_prim( struct gl_context *ctx,
87 const GLfloat *buffer,
88 const struct _mesa_prim *prim,
89 GLuint wrap_count,
90 GLuint vertex_size,
91 const struct loopback_attr *la, GLuint nr )
92 {
93 GLint start = prim->start;
94 GLint end = start + prim->count;
95 const GLfloat *data;
96 GLint j;
97 GLuint k;
98
99 if (0)
100 printf("loopback prim %s(%s,%s) verts %d..%d\n",
101 _mesa_lookup_prim_by_nr(prim->mode),
102 prim->begin ? "begin" : "..",
103 prim->end ? "end" : "..",
104 start,
105 end);
106
107 if (prim->begin) {
108 CALL_Begin(GET_DISPATCH(), ( prim->mode ));
109 }
110 else {
111 assert(start == 0);
112 start += wrap_count;
113 }
114
115 data = buffer + start * vertex_size;
116
117 for (j = start ; j < end ; j++) {
118 const GLfloat *tmp = data + la[0].sz;
119
120 for (k = 1 ; k < nr ; k++) {
121 la[k].func( ctx, la[k].target, tmp );
122 tmp += la[k].sz;
123 }
124
125 /* Fire the vertex
126 */
127 la[0].func( ctx, VBO_ATTRIB_POS, data );
128 data = tmp;
129 }
130
131 if (prim->end) {
132 CALL_End(GET_DISPATCH(), ());
133 }
134 }
135
136 /* Primitives generated by DrawArrays/DrawElements/Rectf may be
137 * caught here. If there is no primitive in progress, execute them
138 * normally, otherwise need to track and discard the generated
139 * primitives.
140 */
141 static void loopback_weak_prim( struct gl_context *ctx,
142 const struct _mesa_prim *prim )
143 {
144 /* Use the prim_weak flag to ensure that if this primitive
145 * wraps, we don't mistake future vertex_lists for part of the
146 * surrounding primitive.
147 *
148 * While this flag is set, we are simply disposing of data
149 * generated by an operation now known to be a noop.
150 */
151 if (prim->begin)
152 ctx->Driver.CurrentExecPrimitive |= VBO_SAVE_PRIM_WEAK;
153 if (prim->end)
154 ctx->Driver.CurrentExecPrimitive &= ~VBO_SAVE_PRIM_WEAK;
155 }
156
157
158 void vbo_loopback_vertex_list( struct gl_context *ctx,
159 const GLfloat *buffer,
160 const GLubyte *attrsz,
161 const struct _mesa_prim *prim,
162 GLuint prim_count,
163 GLuint wrap_count,
164 GLuint vertex_size)
165 {
166 struct loopback_attr la[VBO_ATTRIB_MAX];
167 GLuint i, nr = 0;
168
169 /* All Legacy, NV, ARB and Material attributes are routed through
170 * the NV attributes entrypoints:
171 */
172 for (i = 0 ; i < VBO_ATTRIB_MAX ; i++) {
173 if (attrsz[i]) {
174 la[nr].target = i;
175 la[nr].sz = attrsz[i];
176 la[nr].func = vert_attrfunc[attrsz[i]-1];
177 nr++;
178 }
179 }
180
181 for (i = 0 ; i < prim_count ; i++) {
182 if ((prim[i].mode & VBO_SAVE_PRIM_WEAK) &&
183 (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END))
184 {
185 loopback_weak_prim( ctx, &prim[i] );
186 }
187 else
188 {
189 loopback_prim( ctx, buffer, &prim[i], wrap_count, vertex_size, la, nr );
190 }
191 }
192 }
193
194
195 #endif /* FEATURE_dlist */