nv40: fix typo in one of the previous commits
[mesa.git] / src / mesa / vbo / vbo_exec_draw.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include "main/glheader.h"
29 #include "main/context.h"
30 #include "main/enums.h"
31 #include "main/state.h"
32 #include "main/macros.h"
33
34 #include "vbo_context.h"
35
36
37 static void vbo_exec_debug_verts( struct vbo_exec_context *exec )
38 {
39 GLuint count = exec->vtx.vert_count;
40 GLuint i;
41
42 _mesa_printf("%s: %u vertices %d primitives, %d vertsize\n",
43 __FUNCTION__,
44 count,
45 exec->vtx.prim_count,
46 exec->vtx.vertex_size);
47
48 for (i = 0 ; i < exec->vtx.prim_count ; i++) {
49 struct _mesa_prim *prim = &exec->vtx.prim[i];
50 _mesa_printf(" prim %d: %s%s %d..%d %s %s\n",
51 i,
52 _mesa_lookup_enum_by_nr(prim->mode),
53 prim->weak ? " (weak)" : "",
54 prim->start,
55 prim->start + prim->count,
56 prim->begin ? "BEGIN" : "(wrap)",
57 prim->end ? "END" : "(wrap)");
58 }
59 }
60
61
62 /*
63 * NOTE: Need to have calculated primitives by this point -- do it on the fly.
64 * NOTE: Old 'parity' issue is gone.
65 */
66 static GLuint vbo_copy_vertices( struct vbo_exec_context *exec )
67 {
68 GLuint nr = exec->vtx.prim[exec->vtx.prim_count-1].count;
69 GLuint ovf, i;
70 GLuint sz = exec->vtx.vertex_size;
71 GLfloat *dst = exec->vtx.copied.buffer;
72 GLfloat *src = ((GLfloat *)exec->vtx.buffer_map +
73 exec->vtx.prim[exec->vtx.prim_count-1].start *
74 exec->vtx.vertex_size);
75
76
77 switch( exec->ctx->Driver.CurrentExecPrimitive )
78 {
79 case GL_POINTS:
80 return 0;
81 case GL_LINES:
82 ovf = nr&1;
83 for (i = 0 ; i < ovf ; i++)
84 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
85 return i;
86 case GL_TRIANGLES:
87 ovf = nr%3;
88 for (i = 0 ; i < ovf ; i++)
89 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
90 return i;
91 case GL_QUADS:
92 ovf = nr&3;
93 for (i = 0 ; i < ovf ; i++)
94 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
95 return i;
96 case GL_LINE_STRIP:
97 if (nr == 0)
98 return 0;
99 else {
100 _mesa_memcpy( dst, src+(nr-1)*sz, sz * sizeof(GLfloat) );
101 return 1;
102 }
103 case GL_LINE_LOOP:
104 case GL_TRIANGLE_FAN:
105 case GL_POLYGON:
106 if (nr == 0)
107 return 0;
108 else if (nr == 1) {
109 _mesa_memcpy( dst, src+0, sz * sizeof(GLfloat) );
110 return 1;
111 } else {
112 _mesa_memcpy( dst, src+0, sz * sizeof(GLfloat) );
113 _mesa_memcpy( dst+sz, src+(nr-1)*sz, sz * sizeof(GLfloat) );
114 return 2;
115 }
116 case GL_TRIANGLE_STRIP:
117 /* no parity issue, but need to make sure the tri is not drawn twice */
118 if (nr & 1) {
119 exec->vtx.prim[exec->vtx.prim_count-1].count--;
120 }
121 /* fallthrough */
122 case GL_QUAD_STRIP:
123 switch (nr) {
124 case 0: ovf = 0; break;
125 case 1: ovf = 1; break;
126 default: ovf = 2 + (nr&1); break;
127 }
128 for (i = 0 ; i < ovf ; i++)
129 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
130 return i;
131 case GL_POLYGON+1:
132 return 0;
133 default:
134 assert(0);
135 return 0;
136 }
137 }
138
139
140
141 /* TODO: populate these as the vertex is defined:
142 */
143 static void vbo_exec_bind_arrays( GLcontext *ctx )
144 {
145 struct vbo_context *vbo = vbo_context(ctx);
146 struct vbo_exec_context *exec = &vbo->exec;
147 struct gl_client_array *arrays = exec->vtx.arrays;
148 GLuint count = exec->vtx.vert_count;
149 GLubyte *data = exec->vtx.buffer_map;
150 const GLuint *map;
151 GLuint attr;
152
153 /* Install the default (ie Current) attributes first, then overlay
154 * all active ones.
155 */
156 switch (get_program_mode(exec->ctx)) {
157 case VP_NONE:
158 memcpy(arrays, vbo->legacy_currval, 16 * sizeof(arrays[0]));
159 memcpy(arrays + 16, vbo->mat_currval, MAT_ATTRIB_MAX * sizeof(arrays[0]));
160 map = vbo->map_vp_none;
161 break;
162 case VP_NV:
163 case VP_ARB:
164 /* The aliasing of attributes for NV vertex programs has already
165 * occurred. NV vertex programs cannot access material values,
166 * nor attributes greater than VERT_ATTRIB_TEX7.
167 */
168 memcpy(arrays, vbo->legacy_currval, 16 * sizeof(arrays[0]));
169 memcpy(arrays + 16, vbo->generic_currval, 16 * sizeof(arrays[0]));
170 map = vbo->map_vp_arb;
171 break;
172 }
173
174 /* Make all active attributes (including edgeflag) available as
175 * arrays of floats.
176 */
177 for (attr = 0; attr < VERT_ATTRIB_MAX ; attr++) {
178 const GLuint src = map[attr];
179
180 if (exec->vtx.attrsz[src]) {
181 if (exec->vtx.bufferobj->Name) {
182 /* a real buffer obj: Ptr is an offset, not a pointer*/
183 int offset;
184 assert(exec->vtx.bufferobj->Pointer); /* buf should be mapped */
185 offset = (GLbyte *) data - (GLbyte *) exec->vtx.bufferobj->Pointer;
186 assert(offset >= 0);
187 arrays[attr].Ptr = (void *) offset;
188 }
189 else {
190 /* Ptr into ordinary app memory */
191 arrays[attr].Ptr = (void *) data;
192 }
193 arrays[attr].Size = exec->vtx.attrsz[src];
194 arrays[attr].StrideB = exec->vtx.vertex_size * sizeof(GLfloat);
195 arrays[attr].Stride = exec->vtx.vertex_size * sizeof(GLfloat);
196 arrays[attr].Type = GL_FLOAT;
197 arrays[attr].Enabled = 1;
198 arrays[attr].BufferObj = exec->vtx.bufferobj;
199 arrays[attr]._MaxElement = count; /* ??? */
200
201 data += exec->vtx.attrsz[attr] * sizeof(GLfloat);
202 }
203 }
204 }
205
206
207 /**
208 * Execute the buffer and save copied verts.
209 */
210 void vbo_exec_vtx_flush( struct vbo_exec_context *exec )
211 {
212 if (0)
213 vbo_exec_debug_verts( exec );
214
215
216 if (exec->vtx.prim_count &&
217 exec->vtx.vert_count) {
218
219 exec->vtx.copied.nr = vbo_copy_vertices( exec );
220
221 if (exec->vtx.copied.nr != exec->vtx.vert_count) {
222 GLcontext *ctx = exec->ctx;
223
224 GLenum target = GL_ARRAY_BUFFER_ARB;
225 GLenum access = GL_READ_WRITE_ARB;
226 GLenum usage = GL_STREAM_DRAW_ARB;
227 GLsizei size = VBO_VERT_BUFFER_SIZE * sizeof(GLfloat);
228
229 /* Before the unmap (why?)
230 */
231 vbo_exec_bind_arrays( ctx );
232
233 ctx->Driver.UnmapBuffer(ctx, target, exec->vtx.bufferobj);
234 exec->vtx.buffer_map = NULL;
235
236 vbo_context(ctx)->draw_prims( ctx,
237 exec->vtx.inputs,
238 exec->vtx.prim,
239 exec->vtx.prim_count,
240 NULL,
241 0,
242 exec->vtx.vert_count - 1);
243
244 /* Get new data:
245 */
246 ctx->Driver.BufferData(ctx, target, size, NULL, usage, exec->vtx.bufferobj);
247 exec->vtx.buffer_map
248 = ctx->Driver.MapBuffer(ctx, target, access, exec->vtx.bufferobj);
249 }
250 }
251
252 exec->vtx.prim_count = 0;
253 exec->vtx.vert_count = 0;
254 exec->vtx.vbptr = (GLfloat *)exec->vtx.buffer_map;
255 }