2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
25 * Keith Whitwell <keith@tungstengraphics.com>
28 #include "main/glheader.h"
29 #include "main/bufferobj.h"
30 #include "main/context.h"
31 #include "main/enums.h"
32 #include "main/state.h"
33 #include "main/macros.h"
35 #include "vbo_context.h"
39 vbo_exec_debug_verts( struct vbo_exec_context *exec )
41 GLuint count = exec->vtx.vert_count;
44 _mesa_printf("%s: %u vertices %d primitives, %d vertsize\n",
48 exec->vtx.vertex_size);
50 for (i = 0 ; i < exec->vtx.prim_count ; i++) {
51 struct _mesa_prim *prim = &exec->vtx.prim[i];
52 _mesa_printf(" prim %d: %s%s %d..%d %s %s\n",
54 _mesa_lookup_enum_by_nr(prim->mode),
55 prim->weak ? " (weak)" : "",
57 prim->start + prim->count,
58 prim->begin ? "BEGIN" : "(wrap)",
59 prim->end ? "END" : "(wrap)");
65 * NOTE: Need to have calculated primitives by this point -- do it on the fly.
66 * NOTE: Old 'parity' issue is gone.
69 vbo_copy_vertices( struct vbo_exec_context *exec )
71 GLuint nr = exec->vtx.prim[exec->vtx.prim_count-1].count;
73 GLuint sz = exec->vtx.vertex_size;
74 GLfloat *dst = exec->vtx.copied.buffer;
75 const GLfloat *src = (exec->vtx.buffer_map +
76 exec->vtx.prim[exec->vtx.prim_count-1].start *
77 exec->vtx.vertex_size);
80 switch (exec->ctx->Driver.CurrentExecPrimitive) {
85 for (i = 0 ; i < ovf ; i++)
86 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
90 for (i = 0 ; i < ovf ; i++)
91 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
95 for (i = 0 ; i < ovf ; i++)
96 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
103 _mesa_memcpy( dst, src+(nr-1)*sz, sz * sizeof(GLfloat) );
107 case GL_TRIANGLE_FAN:
113 _mesa_memcpy( dst, src+0, sz * sizeof(GLfloat) );
117 _mesa_memcpy( dst, src+0, sz * sizeof(GLfloat) );
118 _mesa_memcpy( dst+sz, src+(nr-1)*sz, sz * sizeof(GLfloat) );
121 case GL_TRIANGLE_STRIP:
122 /* no parity issue, but need to make sure the tri is not drawn twice */
124 exec->vtx.prim[exec->vtx.prim_count-1].count--;
139 for (i = 0 ; i < ovf ; i++)
140 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
142 case PRIM_OUTSIDE_BEGIN_END:
152 /* TODO: populate these as the vertex is defined:
155 vbo_exec_bind_arrays( GLcontext *ctx )
157 struct vbo_context *vbo = vbo_context(ctx);
158 struct vbo_exec_context *exec = &vbo->exec;
159 struct gl_client_array *arrays = exec->vtx.arrays;
160 const GLuint count = exec->vtx.vert_count;
161 const GLubyte *data = (GLubyte *) exec->vtx.buffer_map;
164 GLbitfield varying_inputs = 0x0;
166 /* Install the default (ie Current) attributes first, then overlay
169 switch (get_program_mode(exec->ctx)) {
171 for (attr = 0; attr < 16; attr++) {
172 exec->vtx.inputs[attr] = &vbo->legacy_currval[attr];
174 for (attr = 0; attr < MAT_ATTRIB_MAX; attr++) {
175 exec->vtx.inputs[attr + 16] = &vbo->mat_currval[attr];
177 map = vbo->map_vp_none;
181 /* The aliasing of attributes for NV vertex programs has already
182 * occurred. NV vertex programs cannot access material values,
183 * nor attributes greater than VERT_ATTRIB_TEX7.
185 for (attr = 0; attr < 16; attr++) {
186 exec->vtx.inputs[attr] = &vbo->legacy_currval[attr];
187 exec->vtx.inputs[attr + 16] = &vbo->generic_currval[attr];
189 map = vbo->map_vp_arb;
191 /* check if VERT_ATTRIB_POS is not read but VERT_BIT_GENERIC0 is read.
192 * In that case we effectively need to route the data from
193 * glVertexAttrib(0, val) calls to feed into the GENERIC0 input.
195 if ((ctx->VertexProgram._Current->Base.InputsRead & VERT_BIT_POS) == 0 &&
196 (ctx->VertexProgram._Current->Base.InputsRead & VERT_BIT_GENERIC0)) {
197 exec->vtx.inputs[16] = exec->vtx.inputs[0];
198 exec->vtx.attrsz[16] = exec->vtx.attrsz[0];
199 exec->vtx.attrptr[16] = exec->vtx.attrptr[0];
200 exec->vtx.attrsz[0] = 0;
207 /* Make all active attributes (including edgeflag) available as
210 for (attr = 0; attr < VERT_ATTRIB_MAX ; attr++) {
211 const GLuint src = map[attr];
213 if (exec->vtx.attrsz[src]) {
214 /* override the default array set above */
215 exec->vtx.inputs[attr] = &arrays[attr];
217 if (exec->vtx.bufferobj->Name) {
218 /* a real buffer obj: Ptr is an offset, not a pointer*/
220 assert(exec->vtx.bufferobj->Pointer); /* buf should be mapped */
221 offset = (GLbyte *) data - (GLbyte *) exec->vtx.bufferobj->Pointer;
223 arrays[attr].Ptr = (void *) offset;
226 /* Ptr into ordinary app memory */
227 arrays[attr].Ptr = (void *) data;
229 arrays[attr].Size = exec->vtx.attrsz[src];
230 arrays[attr].StrideB = exec->vtx.vertex_size * sizeof(GLfloat);
231 arrays[attr].Stride = exec->vtx.vertex_size * sizeof(GLfloat);
232 arrays[attr].Type = GL_FLOAT;
233 arrays[attr].Format = GL_RGBA;
234 arrays[attr].Enabled = 1;
235 _mesa_reference_buffer_object(ctx,
236 &arrays[attr].BufferObj,
237 exec->vtx.bufferobj);
238 arrays[attr]._MaxElement = count; /* ??? */
240 data += exec->vtx.attrsz[src] * sizeof(GLfloat);
241 varying_inputs |= 1 << attr;
245 _mesa_set_varying_vp_inputs( ctx, varying_inputs );
250 vbo_exec_vtx_unmap( struct vbo_exec_context *exec )
252 GLenum target = GL_ARRAY_BUFFER_ARB;
254 if (exec->vtx.bufferobj->Name) {
255 GLcontext *ctx = exec->ctx;
257 if (ctx->Driver.FlushMappedBufferRange) {
258 GLintptr offset = exec->vtx.buffer_used - exec->vtx.bufferobj->Offset;
259 GLsizeiptr length = (exec->vtx.buffer_ptr - exec->vtx.buffer_map) * sizeof(float);
262 ctx->Driver.FlushMappedBufferRange(ctx, target,
264 exec->vtx.bufferobj);
267 exec->vtx.buffer_used += (exec->vtx.buffer_ptr -
268 exec->vtx.buffer_map) * sizeof(float);
270 assert(exec->vtx.buffer_used <= VBO_VERT_BUFFER_SIZE);
271 assert(exec->vtx.buffer_ptr != NULL);
273 ctx->Driver.UnmapBuffer(ctx, target, exec->vtx.bufferobj);
274 exec->vtx.buffer_map = NULL;
275 exec->vtx.buffer_ptr = NULL;
276 exec->vtx.max_vert = 0;
282 vbo_exec_vtx_map( struct vbo_exec_context *exec )
284 GLcontext *ctx = exec->ctx;
285 GLenum target = GL_ARRAY_BUFFER_ARB;
286 GLenum access = GL_READ_WRITE_ARB;
287 GLenum usage = GL_STREAM_DRAW_ARB;
289 if (exec->vtx.bufferobj->Name == 0)
292 if (exec->vtx.buffer_map != NULL) {
294 exec->vtx.buffer_map = NULL;
295 exec->vtx.buffer_ptr = NULL;
298 if (VBO_VERT_BUFFER_SIZE > exec->vtx.buffer_used + 1024 &&
299 ctx->Driver.MapBufferRange) {
300 exec->vtx.buffer_map =
301 (GLfloat *)ctx->Driver.MapBufferRange(ctx,
303 exec->vtx.buffer_used,
304 (VBO_VERT_BUFFER_SIZE -
305 exec->vtx.buffer_used),
307 GL_MAP_INVALIDATE_RANGE_BIT |
308 GL_MAP_UNSYNCHRONIZED_BIT |
309 MESA_MAP_NOWAIT_BIT),
310 exec->vtx.bufferobj);
311 exec->vtx.buffer_ptr = exec->vtx.buffer_map;
314 if (!exec->vtx.buffer_map) {
315 exec->vtx.buffer_used = 0;
317 ctx->Driver.BufferData(ctx, target,
318 VBO_VERT_BUFFER_SIZE,
319 NULL, usage, exec->vtx.bufferobj);
321 exec->vtx.buffer_map = (GLfloat *)
322 ctx->Driver.MapBuffer(ctx, target, access, exec->vtx.bufferobj);
323 exec->vtx.buffer_ptr = exec->vtx.buffer_map;
327 _mesa_printf("map %d..\n", exec->vtx.buffer_used);
333 * Execute the buffer and save copied verts.
336 vbo_exec_vtx_flush( struct vbo_exec_context *exec, GLboolean unmap )
339 vbo_exec_debug_verts( exec );
341 if (exec->vtx.prim_count &&
342 exec->vtx.vert_count) {
344 exec->vtx.copied.nr = vbo_copy_vertices( exec );
346 if (exec->vtx.copied.nr != exec->vtx.vert_count) {
347 GLcontext *ctx = exec->ctx;
349 /* Before the update_state() as this may raise _NEW_ARRAY
350 * from _mesa_set_varying_vp_inputs().
352 vbo_exec_bind_arrays( ctx );
355 _mesa_update_state( ctx );
357 if (exec->vtx.bufferobj->Name) {
358 vbo_exec_vtx_unmap( exec );
362 _mesa_printf("%s %d %d\n", __FUNCTION__, exec->vtx.prim_count,
363 exec->vtx.vert_count);
365 vbo_context(ctx)->draw_prims( ctx,
368 exec->vtx.prim_count,
371 exec->vtx.vert_count - 1);
373 /* If using a real VBO, get new storage -- unless asked not to.
375 if (exec->vtx.bufferobj->Name && !unmap) {
376 vbo_exec_vtx_map( exec );
381 /* May have to unmap explicitly if we didn't draw:
384 exec->vtx.bufferobj->Name &&
385 exec->vtx.buffer_map) {
386 vbo_exec_vtx_unmap( exec );
390 exec->vtx.max_vert = 0;
392 exec->vtx.max_vert = ((VBO_VERT_BUFFER_SIZE - exec->vtx.buffer_used) /
393 (exec->vtx.vertex_size * sizeof(GLfloat)));
395 exec->vtx.buffer_ptr = exec->vtx.buffer_map;
396 exec->vtx.prim_count = 0;
397 exec->vtx.vert_count = 0;